namei.c 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440
  1. /*
  2. * linux/fs/namei.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * Some corrections by tytso.
  8. */
  9. /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
  10. * lookup logic.
  11. */
  12. /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
  13. */
  14. #include <linux/init.h>
  15. #include <linux/export.h>
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. #include <linux/fs.h>
  19. #include <linux/namei.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/fsnotify.h>
  22. #include <linux/personality.h>
  23. #include <linux/security.h>
  24. #include <linux/ima.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/mount.h>
  27. #include <linux/audit.h>
  28. #include <linux/capability.h>
  29. #include <linux/file.h>
  30. #include <linux/fcntl.h>
  31. #include <linux/device_cgroup.h>
  32. #include <linux/fs_struct.h>
  33. #include <linux/posix_acl.h>
  34. #include <asm/uaccess.h>
  35. #include "internal.h"
  36. #include "mount.h"
  37. /* [Feb-1997 T. Schoebel-Theuer]
  38. * Fundamental changes in the pathname lookup mechanisms (namei)
  39. * were necessary because of omirr. The reason is that omirr needs
  40. * to know the _real_ pathname, not the user-supplied one, in case
  41. * of symlinks (and also when transname replacements occur).
  42. *
  43. * The new code replaces the old recursive symlink resolution with
  44. * an iterative one (in case of non-nested symlink chains). It does
  45. * this with calls to <fs>_follow_link().
  46. * As a side effect, dir_namei(), _namei() and follow_link() are now
  47. * replaced with a single function lookup_dentry() that can handle all
  48. * the special cases of the former code.
  49. *
  50. * With the new dcache, the pathname is stored at each inode, at least as
  51. * long as the refcount of the inode is positive. As a side effect, the
  52. * size of the dcache depends on the inode cache and thus is dynamic.
  53. *
  54. * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
  55. * resolution to correspond with current state of the code.
  56. *
  57. * Note that the symlink resolution is not *completely* iterative.
  58. * There is still a significant amount of tail- and mid- recursion in
  59. * the algorithm. Also, note that <fs>_readlink() is not used in
  60. * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
  61. * may return different results than <fs>_follow_link(). Many virtual
  62. * filesystems (including /proc) exhibit this behavior.
  63. */
  64. /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
  65. * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
  66. * and the name already exists in form of a symlink, try to create the new
  67. * name indicated by the symlink. The old code always complained that the
  68. * name already exists, due to not following the symlink even if its target
  69. * is nonexistent. The new semantics affects also mknod() and link() when
  70. * the name is a symlink pointing to a non-existent name.
  71. *
  72. * I don't know which semantics is the right one, since I have no access
  73. * to standards. But I found by trial that HP-UX 9.0 has the full "new"
  74. * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
  75. * "old" one. Personally, I think the new semantics is much more logical.
  76. * Note that "ln old new" where "new" is a symlink pointing to a non-existing
  77. * file does succeed in both HP-UX and SunOs, but not in Solaris
  78. * and in the old Linux semantics.
  79. */
  80. /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
  81. * semantics. See the comments in "open_namei" and "do_link" below.
  82. *
  83. * [10-Sep-98 Alan Modra] Another symlink change.
  84. */
  85. /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
  86. * inside the path - always follow.
  87. * in the last component in creation/removal/renaming - never follow.
  88. * if LOOKUP_FOLLOW passed - follow.
  89. * if the pathname has trailing slashes - follow.
  90. * otherwise - don't follow.
  91. * (applied in that order).
  92. *
  93. * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
  94. * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
  95. * During the 2.4 we need to fix the userland stuff depending on it -
  96. * hopefully we will be able to get rid of that wart in 2.5. So far only
  97. * XEmacs seems to be relying on it...
  98. */
  99. /*
  100. * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
  101. * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
  102. * any extra contention...
  103. */
  104. /* In order to reduce some races, while at the same time doing additional
  105. * checking and hopefully speeding things up, we copy filenames to the
  106. * kernel data space before using them..
  107. *
  108. * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
  109. * PATH_MAX includes the nul terminator --RR.
  110. */
  111. void final_putname(struct filename *name)
  112. {
  113. if (name->separate) {
  114. __putname(name->name);
  115. kfree(name);
  116. } else {
  117. __putname(name);
  118. }
  119. }
  120. #define EMBEDDED_NAME_MAX (PATH_MAX - sizeof(struct filename))
  121. static struct filename *
  122. getname_flags(const char __user *filename, int flags, int *empty)
  123. {
  124. struct filename *result, *err;
  125. int len;
  126. long max;
  127. char *kname;
  128. result = audit_reusename(filename);
  129. if (result)
  130. return result;
  131. result = __getname();
  132. if (unlikely(!result))
  133. return ERR_PTR(-ENOMEM);
  134. /*
  135. * First, try to embed the struct filename inside the names_cache
  136. * allocation
  137. */
  138. kname = (char *)result + sizeof(*result);
  139. result->name = kname;
  140. result->separate = false;
  141. max = EMBEDDED_NAME_MAX;
  142. recopy:
  143. len = strncpy_from_user(kname, filename, max);
  144. if (unlikely(len < 0)) {
  145. err = ERR_PTR(len);
  146. goto error;
  147. }
  148. /*
  149. * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
  150. * separate struct filename so we can dedicate the entire
  151. * names_cache allocation for the pathname, and re-do the copy from
  152. * userland.
  153. */
  154. if (len == EMBEDDED_NAME_MAX && max == EMBEDDED_NAME_MAX) {
  155. kname = (char *)result;
  156. result = kzalloc(sizeof(*result), GFP_KERNEL);
  157. if (!result) {
  158. err = ERR_PTR(-ENOMEM);
  159. result = (struct filename *)kname;
  160. goto error;
  161. }
  162. result->name = kname;
  163. result->separate = true;
  164. max = PATH_MAX;
  165. goto recopy;
  166. }
  167. /* The empty path is special. */
  168. if (unlikely(!len)) {
  169. if (empty)
  170. *empty = 1;
  171. err = ERR_PTR(-ENOENT);
  172. if (!(flags & LOOKUP_EMPTY))
  173. goto error;
  174. }
  175. err = ERR_PTR(-ENAMETOOLONG);
  176. if (unlikely(len >= PATH_MAX))
  177. goto error;
  178. result->uptr = filename;
  179. result->aname = NULL;
  180. audit_getname(result);
  181. return result;
  182. error:
  183. final_putname(result);
  184. return err;
  185. }
  186. struct filename *
  187. getname(const char __user * filename)
  188. {
  189. return getname_flags(filename, 0, NULL);
  190. }
  191. /*
  192. * The "getname_kernel()" interface doesn't do pathnames longer
  193. * than EMBEDDED_NAME_MAX. Deal with it - you're a kernel user.
  194. */
  195. struct filename *
  196. getname_kernel(const char * filename)
  197. {
  198. struct filename *result;
  199. char *kname;
  200. int len;
  201. len = strlen(filename);
  202. if (len >= EMBEDDED_NAME_MAX)
  203. return ERR_PTR(-ENAMETOOLONG);
  204. result = __getname();
  205. if (unlikely(!result))
  206. return ERR_PTR(-ENOMEM);
  207. kname = (char *)result + sizeof(*result);
  208. result->name = kname;
  209. result->uptr = NULL;
  210. result->aname = NULL;
  211. result->separate = false;
  212. strlcpy(kname, filename, EMBEDDED_NAME_MAX);
  213. return result;
  214. }
  215. #ifdef CONFIG_AUDITSYSCALL
  216. void putname(struct filename *name)
  217. {
  218. if (unlikely(!audit_dummy_context()))
  219. return audit_putname(name);
  220. final_putname(name);
  221. }
  222. #endif
  223. static int check_acl(struct inode *inode, int mask)
  224. {
  225. #ifdef CONFIG_FS_POSIX_ACL
  226. struct posix_acl *acl;
  227. if (mask & MAY_NOT_BLOCK) {
  228. acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
  229. if (!acl)
  230. return -EAGAIN;
  231. /* no ->get_acl() calls in RCU mode... */
  232. if (acl == ACL_NOT_CACHED)
  233. return -ECHILD;
  234. return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
  235. }
  236. acl = get_acl(inode, ACL_TYPE_ACCESS);
  237. if (IS_ERR(acl))
  238. return PTR_ERR(acl);
  239. if (acl) {
  240. int error = posix_acl_permission(inode, acl, mask);
  241. posix_acl_release(acl);
  242. return error;
  243. }
  244. #endif
  245. return -EAGAIN;
  246. }
  247. /*
  248. * This does the basic permission checking
  249. */
  250. static int acl_permission_check(struct inode *inode, int mask)
  251. {
  252. unsigned int mode = inode->i_mode;
  253. if (likely(uid_eq(current_fsuid(), inode->i_uid)))
  254. mode >>= 6;
  255. else {
  256. if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
  257. int error = check_acl(inode, mask);
  258. if (error != -EAGAIN)
  259. return error;
  260. }
  261. if (in_group_p(inode->i_gid))
  262. mode >>= 3;
  263. }
  264. /*
  265. * If the DACs are ok we don't need any capability check.
  266. */
  267. if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  268. return 0;
  269. return -EACCES;
  270. }
  271. /**
  272. * generic_permission - check for access rights on a Posix-like filesystem
  273. * @inode: inode to check access rights for
  274. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
  275. *
  276. * Used to check for read/write/execute permissions on a file.
  277. * We use "fsuid" for this, letting us set arbitrary permissions
  278. * for filesystem access without changing the "normal" uids which
  279. * are used for other things.
  280. *
  281. * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
  282. * request cannot be satisfied (eg. requires blocking or too much complexity).
  283. * It would then be called again in ref-walk mode.
  284. */
  285. int generic_permission(struct inode *inode, int mask)
  286. {
  287. int ret;
  288. /*
  289. * Do the basic permission checks.
  290. */
  291. ret = acl_permission_check(inode, mask);
  292. if (ret != -EACCES)
  293. return ret;
  294. if (S_ISDIR(inode->i_mode)) {
  295. /* DACs are overridable for directories */
  296. if (inode_capable(inode, CAP_DAC_OVERRIDE))
  297. return 0;
  298. if (!(mask & MAY_WRITE))
  299. if (inode_capable(inode, CAP_DAC_READ_SEARCH))
  300. return 0;
  301. return -EACCES;
  302. }
  303. /*
  304. * Read/write DACs are always overridable.
  305. * Executable DACs are overridable when there is
  306. * at least one exec bit set.
  307. */
  308. if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
  309. if (inode_capable(inode, CAP_DAC_OVERRIDE))
  310. return 0;
  311. /*
  312. * Searching includes executable on directories, else just read.
  313. */
  314. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  315. if (mask == MAY_READ)
  316. if (inode_capable(inode, CAP_DAC_READ_SEARCH))
  317. return 0;
  318. return -EACCES;
  319. }
  320. /*
  321. * We _really_ want to just do "generic_permission()" without
  322. * even looking at the inode->i_op values. So we keep a cache
  323. * flag in inode->i_opflags, that says "this has not special
  324. * permission function, use the fast case".
  325. */
  326. static inline int do_inode_permission(struct inode *inode, int mask)
  327. {
  328. if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
  329. if (likely(inode->i_op->permission))
  330. return inode->i_op->permission(inode, mask);
  331. /* This gets set once for the inode lifetime */
  332. spin_lock(&inode->i_lock);
  333. inode->i_opflags |= IOP_FASTPERM;
  334. spin_unlock(&inode->i_lock);
  335. }
  336. return generic_permission(inode, mask);
  337. }
  338. /**
  339. * __inode_permission - Check for access rights to a given inode
  340. * @inode: Inode to check permission on
  341. * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  342. *
  343. * Check for read/write/execute permissions on an inode.
  344. *
  345. * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
  346. *
  347. * This does not check for a read-only file system. You probably want
  348. * inode_permission().
  349. */
  350. int __inode_permission(struct inode *inode, int mask)
  351. {
  352. int retval;
  353. if (unlikely(mask & MAY_WRITE)) {
  354. /*
  355. * Nobody gets write access to an immutable file.
  356. */
  357. if (IS_IMMUTABLE(inode))
  358. return -EACCES;
  359. }
  360. retval = do_inode_permission(inode, mask);
  361. if (retval)
  362. return retval;
  363. retval = devcgroup_inode_permission(inode, mask);
  364. if (retval)
  365. return retval;
  366. return security_inode_permission(inode, mask);
  367. }
  368. /**
  369. * sb_permission - Check superblock-level permissions
  370. * @sb: Superblock of inode to check permission on
  371. * @inode: Inode to check permission on
  372. * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  373. *
  374. * Separate out file-system wide checks from inode-specific permission checks.
  375. */
  376. static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
  377. {
  378. if (unlikely(mask & MAY_WRITE)) {
  379. umode_t mode = inode->i_mode;
  380. /* Nobody gets write access to a read-only fs. */
  381. if ((sb->s_flags & MS_RDONLY) &&
  382. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  383. return -EROFS;
  384. }
  385. return 0;
  386. }
  387. /**
  388. * inode_permission - Check for access rights to a given inode
  389. * @inode: Inode to check permission on
  390. * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  391. *
  392. * Check for read/write/execute permissions on an inode. We use fs[ug]id for
  393. * this, letting us set arbitrary permissions for filesystem access without
  394. * changing the "normal" UIDs which are used for other things.
  395. *
  396. * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
  397. */
  398. int inode_permission(struct inode *inode, int mask)
  399. {
  400. int retval;
  401. retval = sb_permission(inode->i_sb, inode, mask);
  402. if (retval)
  403. return retval;
  404. return __inode_permission(inode, mask);
  405. }
  406. /**
  407. * path_get - get a reference to a path
  408. * @path: path to get the reference to
  409. *
  410. * Given a path increment the reference count to the dentry and the vfsmount.
  411. */
  412. void path_get(const struct path *path)
  413. {
  414. mntget(path->mnt);
  415. dget(path->dentry);
  416. }
  417. EXPORT_SYMBOL(path_get);
  418. /**
  419. * path_put - put a reference to a path
  420. * @path: path to put the reference to
  421. *
  422. * Given a path decrement the reference count to the dentry and the vfsmount.
  423. */
  424. void path_put(const struct path *path)
  425. {
  426. dput(path->dentry);
  427. mntput(path->mnt);
  428. }
  429. EXPORT_SYMBOL(path_put);
  430. /*
  431. * Path walking has 2 modes, rcu-walk and ref-walk (see
  432. * Documentation/filesystems/path-lookup.txt). In situations when we can't
  433. * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
  434. * normal reference counts on dentries and vfsmounts to transition to rcu-walk
  435. * mode. Refcounts are grabbed at the last known good point before rcu-walk
  436. * got stuck, so ref-walk may continue from there. If this is not successful
  437. * (eg. a seqcount has changed), then failure is returned and it's up to caller
  438. * to restart the path walk from the beginning in ref-walk mode.
  439. */
  440. /**
  441. * unlazy_walk - try to switch to ref-walk mode.
  442. * @nd: nameidata pathwalk data
  443. * @dentry: child of nd->path.dentry or NULL
  444. * Returns: 0 on success, -ECHILD on failure
  445. *
  446. * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
  447. * for ref-walk mode. @dentry must be a path found by a do_lookup call on
  448. * @nd or NULL. Must be called from rcu-walk context.
  449. */
  450. static int unlazy_walk(struct nameidata *nd, struct dentry *dentry)
  451. {
  452. struct fs_struct *fs = current->fs;
  453. struct dentry *parent = nd->path.dentry;
  454. BUG_ON(!(nd->flags & LOOKUP_RCU));
  455. /*
  456. * After legitimizing the bastards, terminate_walk()
  457. * will do the right thing for non-RCU mode, and all our
  458. * subsequent exit cases should rcu_read_unlock()
  459. * before returning. Do vfsmount first; if dentry
  460. * can't be legitimized, just set nd->path.dentry to NULL
  461. * and rely on dput(NULL) being a no-op.
  462. */
  463. if (!legitimize_mnt(nd->path.mnt, nd->m_seq))
  464. return -ECHILD;
  465. nd->flags &= ~LOOKUP_RCU;
  466. if (!lockref_get_not_dead(&parent->d_lockref)) {
  467. nd->path.dentry = NULL;
  468. goto out;
  469. }
  470. /*
  471. * For a negative lookup, the lookup sequence point is the parents
  472. * sequence point, and it only needs to revalidate the parent dentry.
  473. *
  474. * For a positive lookup, we need to move both the parent and the
  475. * dentry from the RCU domain to be properly refcounted. And the
  476. * sequence number in the dentry validates *both* dentry counters,
  477. * since we checked the sequence number of the parent after we got
  478. * the child sequence number. So we know the parent must still
  479. * be valid if the child sequence number is still valid.
  480. */
  481. if (!dentry) {
  482. if (read_seqcount_retry(&parent->d_seq, nd->seq))
  483. goto out;
  484. BUG_ON(nd->inode != parent->d_inode);
  485. } else {
  486. if (!lockref_get_not_dead(&dentry->d_lockref))
  487. goto out;
  488. if (read_seqcount_retry(&dentry->d_seq, nd->seq))
  489. goto drop_dentry;
  490. }
  491. /*
  492. * Sequence counts matched. Now make sure that the root is
  493. * still valid and get it if required.
  494. */
  495. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  496. spin_lock(&fs->lock);
  497. if (nd->root.mnt != fs->root.mnt || nd->root.dentry != fs->root.dentry)
  498. goto unlock_and_drop_dentry;
  499. path_get(&nd->root);
  500. spin_unlock(&fs->lock);
  501. }
  502. rcu_read_unlock();
  503. return 0;
  504. unlock_and_drop_dentry:
  505. spin_unlock(&fs->lock);
  506. drop_dentry:
  507. rcu_read_unlock();
  508. dput(dentry);
  509. goto drop_root_mnt;
  510. out:
  511. rcu_read_unlock();
  512. drop_root_mnt:
  513. if (!(nd->flags & LOOKUP_ROOT))
  514. nd->root.mnt = NULL;
  515. return -ECHILD;
  516. }
  517. static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
  518. {
  519. return dentry->d_op->d_revalidate(dentry, flags);
  520. }
  521. /**
  522. * complete_walk - successful completion of path walk
  523. * @nd: pointer nameidata
  524. *
  525. * If we had been in RCU mode, drop out of it and legitimize nd->path.
  526. * Revalidate the final result, unless we'd already done that during
  527. * the path walk or the filesystem doesn't ask for it. Return 0 on
  528. * success, -error on failure. In case of failure caller does not
  529. * need to drop nd->path.
  530. */
  531. static int complete_walk(struct nameidata *nd)
  532. {
  533. struct dentry *dentry = nd->path.dentry;
  534. int status;
  535. if (nd->flags & LOOKUP_RCU) {
  536. nd->flags &= ~LOOKUP_RCU;
  537. if (!(nd->flags & LOOKUP_ROOT))
  538. nd->root.mnt = NULL;
  539. if (!legitimize_mnt(nd->path.mnt, nd->m_seq)) {
  540. rcu_read_unlock();
  541. return -ECHILD;
  542. }
  543. if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) {
  544. rcu_read_unlock();
  545. mntput(nd->path.mnt);
  546. return -ECHILD;
  547. }
  548. if (read_seqcount_retry(&dentry->d_seq, nd->seq)) {
  549. rcu_read_unlock();
  550. dput(dentry);
  551. mntput(nd->path.mnt);
  552. return -ECHILD;
  553. }
  554. rcu_read_unlock();
  555. }
  556. if (likely(!(nd->flags & LOOKUP_JUMPED)))
  557. return 0;
  558. if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
  559. return 0;
  560. status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
  561. if (status > 0)
  562. return 0;
  563. if (!status)
  564. status = -ESTALE;
  565. path_put(&nd->path);
  566. return status;
  567. }
  568. static __always_inline void set_root(struct nameidata *nd)
  569. {
  570. if (!nd->root.mnt)
  571. get_fs_root(current->fs, &nd->root);
  572. }
  573. static int link_path_walk(const char *, struct nameidata *);
  574. static __always_inline void set_root_rcu(struct nameidata *nd)
  575. {
  576. if (!nd->root.mnt) {
  577. struct fs_struct *fs = current->fs;
  578. unsigned seq;
  579. do {
  580. seq = read_seqcount_begin(&fs->seq);
  581. nd->root = fs->root;
  582. nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
  583. } while (read_seqcount_retry(&fs->seq, seq));
  584. }
  585. }
  586. static void path_put_conditional(struct path *path, struct nameidata *nd)
  587. {
  588. dput(path->dentry);
  589. if (path->mnt != nd->path.mnt)
  590. mntput(path->mnt);
  591. }
  592. static inline void path_to_nameidata(const struct path *path,
  593. struct nameidata *nd)
  594. {
  595. if (!(nd->flags & LOOKUP_RCU)) {
  596. dput(nd->path.dentry);
  597. if (nd->path.mnt != path->mnt)
  598. mntput(nd->path.mnt);
  599. }
  600. nd->path.mnt = path->mnt;
  601. nd->path.dentry = path->dentry;
  602. }
  603. /*
  604. * Helper to directly jump to a known parsed path from ->follow_link,
  605. * caller must have taken a reference to path beforehand.
  606. */
  607. void nd_jump_link(struct nameidata *nd, struct path *path)
  608. {
  609. path_put(&nd->path);
  610. nd->path = *path;
  611. nd->inode = nd->path.dentry->d_inode;
  612. nd->flags |= LOOKUP_JUMPED;
  613. }
  614. static inline void put_link(struct nameidata *nd, struct path *link, void *cookie)
  615. {
  616. struct inode *inode = link->dentry->d_inode;
  617. if (inode->i_op->put_link)
  618. inode->i_op->put_link(link->dentry, nd, cookie);
  619. path_put(link);
  620. }
  621. int sysctl_protected_symlinks __read_mostly = 0;
  622. int sysctl_protected_hardlinks __read_mostly = 0;
  623. /**
  624. * may_follow_link - Check symlink following for unsafe situations
  625. * @link: The path of the symlink
  626. * @nd: nameidata pathwalk data
  627. *
  628. * In the case of the sysctl_protected_symlinks sysctl being enabled,
  629. * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
  630. * in a sticky world-writable directory. This is to protect privileged
  631. * processes from failing races against path names that may change out
  632. * from under them by way of other users creating malicious symlinks.
  633. * It will permit symlinks to be followed only when outside a sticky
  634. * world-writable directory, or when the uid of the symlink and follower
  635. * match, or when the directory owner matches the symlink's owner.
  636. *
  637. * Returns 0 if following the symlink is allowed, -ve on error.
  638. */
  639. static inline int may_follow_link(struct path *link, struct nameidata *nd)
  640. {
  641. const struct inode *inode;
  642. const struct inode *parent;
  643. if (!sysctl_protected_symlinks)
  644. return 0;
  645. /* Allowed if owner and follower match. */
  646. inode = link->dentry->d_inode;
  647. if (uid_eq(current_cred()->fsuid, inode->i_uid))
  648. return 0;
  649. /* Allowed if parent directory not sticky and world-writable. */
  650. parent = nd->path.dentry->d_inode;
  651. if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
  652. return 0;
  653. /* Allowed if parent directory and link owner match. */
  654. if (uid_eq(parent->i_uid, inode->i_uid))
  655. return 0;
  656. audit_log_link_denied("follow_link", link);
  657. path_put_conditional(link, nd);
  658. path_put(&nd->path);
  659. return -EACCES;
  660. }
  661. /**
  662. * safe_hardlink_source - Check for safe hardlink conditions
  663. * @inode: the source inode to hardlink from
  664. *
  665. * Return false if at least one of the following conditions:
  666. * - inode is not a regular file
  667. * - inode is setuid
  668. * - inode is setgid and group-exec
  669. * - access failure for read and write
  670. *
  671. * Otherwise returns true.
  672. */
  673. static bool safe_hardlink_source(struct inode *inode)
  674. {
  675. umode_t mode = inode->i_mode;
  676. /* Special files should not get pinned to the filesystem. */
  677. if (!S_ISREG(mode))
  678. return false;
  679. /* Setuid files should not get pinned to the filesystem. */
  680. if (mode & S_ISUID)
  681. return false;
  682. /* Executable setgid files should not get pinned to the filesystem. */
  683. if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
  684. return false;
  685. /* Hardlinking to unreadable or unwritable sources is dangerous. */
  686. if (inode_permission(inode, MAY_READ | MAY_WRITE))
  687. return false;
  688. return true;
  689. }
  690. /**
  691. * may_linkat - Check permissions for creating a hardlink
  692. * @link: the source to hardlink from
  693. *
  694. * Block hardlink when all of:
  695. * - sysctl_protected_hardlinks enabled
  696. * - fsuid does not match inode
  697. * - hardlink source is unsafe (see safe_hardlink_source() above)
  698. * - not CAP_FOWNER
  699. *
  700. * Returns 0 if successful, -ve on error.
  701. */
  702. static int may_linkat(struct path *link)
  703. {
  704. const struct cred *cred;
  705. struct inode *inode;
  706. if (!sysctl_protected_hardlinks)
  707. return 0;
  708. cred = current_cred();
  709. inode = link->dentry->d_inode;
  710. /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
  711. * otherwise, it must be a safe source.
  712. */
  713. if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) ||
  714. capable(CAP_FOWNER))
  715. return 0;
  716. audit_log_link_denied("linkat", link);
  717. return -EPERM;
  718. }
  719. static __always_inline int
  720. follow_link(struct path *link, struct nameidata *nd, void **p)
  721. {
  722. struct dentry *dentry = link->dentry;
  723. int error;
  724. char *s;
  725. BUG_ON(nd->flags & LOOKUP_RCU);
  726. if (link->mnt == nd->path.mnt)
  727. mntget(link->mnt);
  728. error = -ELOOP;
  729. if (unlikely(current->total_link_count >= 40))
  730. goto out_put_nd_path;
  731. cond_resched();
  732. current->total_link_count++;
  733. touch_atime(link);
  734. nd_set_link(nd, NULL);
  735. error = security_inode_follow_link(link->dentry, nd);
  736. if (error)
  737. goto out_put_nd_path;
  738. nd->last_type = LAST_BIND;
  739. *p = dentry->d_inode->i_op->follow_link(dentry, nd);
  740. error = PTR_ERR(*p);
  741. if (IS_ERR(*p))
  742. goto out_put_nd_path;
  743. error = 0;
  744. s = nd_get_link(nd);
  745. if (s) {
  746. if (unlikely(IS_ERR(s))) {
  747. path_put(&nd->path);
  748. put_link(nd, link, *p);
  749. return PTR_ERR(s);
  750. }
  751. if (*s == '/') {
  752. set_root(nd);
  753. path_put(&nd->path);
  754. nd->path = nd->root;
  755. path_get(&nd->root);
  756. nd->flags |= LOOKUP_JUMPED;
  757. }
  758. nd->inode = nd->path.dentry->d_inode;
  759. error = link_path_walk(s, nd);
  760. if (unlikely(error))
  761. put_link(nd, link, *p);
  762. }
  763. return error;
  764. out_put_nd_path:
  765. *p = NULL;
  766. path_put(&nd->path);
  767. path_put(link);
  768. return error;
  769. }
  770. static int follow_up_rcu(struct path *path)
  771. {
  772. struct mount *mnt = real_mount(path->mnt);
  773. struct mount *parent;
  774. struct dentry *mountpoint;
  775. parent = mnt->mnt_parent;
  776. if (&parent->mnt == path->mnt)
  777. return 0;
  778. mountpoint = mnt->mnt_mountpoint;
  779. path->dentry = mountpoint;
  780. path->mnt = &parent->mnt;
  781. return 1;
  782. }
  783. /*
  784. * follow_up - Find the mountpoint of path's vfsmount
  785. *
  786. * Given a path, find the mountpoint of its source file system.
  787. * Replace @path with the path of the mountpoint in the parent mount.
  788. * Up is towards /.
  789. *
  790. * Return 1 if we went up a level and 0 if we were already at the
  791. * root.
  792. */
  793. int follow_up(struct path *path)
  794. {
  795. struct mount *mnt = real_mount(path->mnt);
  796. struct mount *parent;
  797. struct dentry *mountpoint;
  798. read_seqlock_excl(&mount_lock);
  799. parent = mnt->mnt_parent;
  800. if (parent == mnt) {
  801. read_sequnlock_excl(&mount_lock);
  802. return 0;
  803. }
  804. mntget(&parent->mnt);
  805. mountpoint = dget(mnt->mnt_mountpoint);
  806. read_sequnlock_excl(&mount_lock);
  807. dput(path->dentry);
  808. path->dentry = mountpoint;
  809. mntput(path->mnt);
  810. path->mnt = &parent->mnt;
  811. return 1;
  812. }
  813. /*
  814. * Perform an automount
  815. * - return -EISDIR to tell follow_managed() to stop and return the path we
  816. * were called with.
  817. */
  818. static int follow_automount(struct path *path, unsigned flags,
  819. bool *need_mntput)
  820. {
  821. struct vfsmount *mnt;
  822. int err;
  823. if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
  824. return -EREMOTE;
  825. /* We don't want to mount if someone's just doing a stat -
  826. * unless they're stat'ing a directory and appended a '/' to
  827. * the name.
  828. *
  829. * We do, however, want to mount if someone wants to open or
  830. * create a file of any type under the mountpoint, wants to
  831. * traverse through the mountpoint or wants to open the
  832. * mounted directory. Also, autofs may mark negative dentries
  833. * as being automount points. These will need the attentions
  834. * of the daemon to instantiate them before they can be used.
  835. */
  836. if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
  837. LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
  838. path->dentry->d_inode)
  839. return -EISDIR;
  840. current->total_link_count++;
  841. if (current->total_link_count >= 40)
  842. return -ELOOP;
  843. mnt = path->dentry->d_op->d_automount(path);
  844. if (IS_ERR(mnt)) {
  845. /*
  846. * The filesystem is allowed to return -EISDIR here to indicate
  847. * it doesn't want to automount. For instance, autofs would do
  848. * this so that its userspace daemon can mount on this dentry.
  849. *
  850. * However, we can only permit this if it's a terminal point in
  851. * the path being looked up; if it wasn't then the remainder of
  852. * the path is inaccessible and we should say so.
  853. */
  854. if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT))
  855. return -EREMOTE;
  856. return PTR_ERR(mnt);
  857. }
  858. if (!mnt) /* mount collision */
  859. return 0;
  860. if (!*need_mntput) {
  861. /* lock_mount() may release path->mnt on error */
  862. mntget(path->mnt);
  863. *need_mntput = true;
  864. }
  865. err = finish_automount(mnt, path);
  866. switch (err) {
  867. case -EBUSY:
  868. /* Someone else made a mount here whilst we were busy */
  869. return 0;
  870. case 0:
  871. path_put(path);
  872. path->mnt = mnt;
  873. path->dentry = dget(mnt->mnt_root);
  874. return 0;
  875. default:
  876. return err;
  877. }
  878. }
  879. /*
  880. * Handle a dentry that is managed in some way.
  881. * - Flagged for transit management (autofs)
  882. * - Flagged as mountpoint
  883. * - Flagged as automount point
  884. *
  885. * This may only be called in refwalk mode.
  886. *
  887. * Serialization is taken care of in namespace.c
  888. */
  889. static int follow_managed(struct path *path, unsigned flags)
  890. {
  891. struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
  892. unsigned managed;
  893. bool need_mntput = false;
  894. int ret = 0;
  895. /* Given that we're not holding a lock here, we retain the value in a
  896. * local variable for each dentry as we look at it so that we don't see
  897. * the components of that value change under us */
  898. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  899. managed &= DCACHE_MANAGED_DENTRY,
  900. unlikely(managed != 0)) {
  901. /* Allow the filesystem to manage the transit without i_mutex
  902. * being held. */
  903. if (managed & DCACHE_MANAGE_TRANSIT) {
  904. BUG_ON(!path->dentry->d_op);
  905. BUG_ON(!path->dentry->d_op->d_manage);
  906. ret = path->dentry->d_op->d_manage(path->dentry, false);
  907. if (ret < 0)
  908. break;
  909. }
  910. /* Transit to a mounted filesystem. */
  911. if (managed & DCACHE_MOUNTED) {
  912. struct vfsmount *mounted = lookup_mnt(path);
  913. if (mounted) {
  914. dput(path->dentry);
  915. if (need_mntput)
  916. mntput(path->mnt);
  917. path->mnt = mounted;
  918. path->dentry = dget(mounted->mnt_root);
  919. need_mntput = true;
  920. continue;
  921. }
  922. /* Something is mounted on this dentry in another
  923. * namespace and/or whatever was mounted there in this
  924. * namespace got unmounted before lookup_mnt() could
  925. * get it */
  926. }
  927. /* Handle an automount point */
  928. if (managed & DCACHE_NEED_AUTOMOUNT) {
  929. ret = follow_automount(path, flags, &need_mntput);
  930. if (ret < 0)
  931. break;
  932. continue;
  933. }
  934. /* We didn't change the current path point */
  935. break;
  936. }
  937. if (need_mntput && path->mnt == mnt)
  938. mntput(path->mnt);
  939. if (ret == -EISDIR)
  940. ret = 0;
  941. return ret < 0 ? ret : need_mntput;
  942. }
  943. int follow_down_one(struct path *path)
  944. {
  945. struct vfsmount *mounted;
  946. mounted = lookup_mnt(path);
  947. if (mounted) {
  948. dput(path->dentry);
  949. mntput(path->mnt);
  950. path->mnt = mounted;
  951. path->dentry = dget(mounted->mnt_root);
  952. return 1;
  953. }
  954. return 0;
  955. }
  956. static inline bool managed_dentry_might_block(struct dentry *dentry)
  957. {
  958. return (dentry->d_flags & DCACHE_MANAGE_TRANSIT &&
  959. dentry->d_op->d_manage(dentry, true) < 0);
  960. }
  961. /*
  962. * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
  963. * we meet a managed dentry that would need blocking.
  964. */
  965. static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
  966. struct inode **inode)
  967. {
  968. for (;;) {
  969. struct mount *mounted;
  970. /*
  971. * Don't forget we might have a non-mountpoint managed dentry
  972. * that wants to block transit.
  973. */
  974. if (unlikely(managed_dentry_might_block(path->dentry)))
  975. return false;
  976. if (!d_mountpoint(path->dentry))
  977. return true;
  978. mounted = __lookup_mnt(path->mnt, path->dentry);
  979. if (!mounted)
  980. break;
  981. path->mnt = &mounted->mnt;
  982. path->dentry = mounted->mnt.mnt_root;
  983. nd->flags |= LOOKUP_JUMPED;
  984. nd->seq = read_seqcount_begin(&path->dentry->d_seq);
  985. /*
  986. * Update the inode too. We don't need to re-check the
  987. * dentry sequence number here after this d_inode read,
  988. * because a mount-point is always pinned.
  989. */
  990. *inode = path->dentry->d_inode;
  991. }
  992. return read_seqretry(&mount_lock, nd->m_seq);
  993. }
  994. static int follow_dotdot_rcu(struct nameidata *nd)
  995. {
  996. set_root_rcu(nd);
  997. while (1) {
  998. if (nd->path.dentry == nd->root.dentry &&
  999. nd->path.mnt == nd->root.mnt) {
  1000. break;
  1001. }
  1002. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  1003. struct dentry *old = nd->path.dentry;
  1004. struct dentry *parent = old->d_parent;
  1005. unsigned seq;
  1006. seq = read_seqcount_begin(&parent->d_seq);
  1007. if (read_seqcount_retry(&old->d_seq, nd->seq))
  1008. goto failed;
  1009. nd->path.dentry = parent;
  1010. nd->seq = seq;
  1011. break;
  1012. }
  1013. if (!follow_up_rcu(&nd->path))
  1014. break;
  1015. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  1016. }
  1017. while (d_mountpoint(nd->path.dentry)) {
  1018. struct mount *mounted;
  1019. mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
  1020. if (!mounted)
  1021. break;
  1022. nd->path.mnt = &mounted->mnt;
  1023. nd->path.dentry = mounted->mnt.mnt_root;
  1024. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  1025. if (!read_seqretry(&mount_lock, nd->m_seq))
  1026. goto failed;
  1027. }
  1028. nd->inode = nd->path.dentry->d_inode;
  1029. return 0;
  1030. failed:
  1031. nd->flags &= ~LOOKUP_RCU;
  1032. if (!(nd->flags & LOOKUP_ROOT))
  1033. nd->root.mnt = NULL;
  1034. rcu_read_unlock();
  1035. return -ECHILD;
  1036. }
  1037. /*
  1038. * Follow down to the covering mount currently visible to userspace. At each
  1039. * point, the filesystem owning that dentry may be queried as to whether the
  1040. * caller is permitted to proceed or not.
  1041. */
  1042. int follow_down(struct path *path)
  1043. {
  1044. unsigned managed;
  1045. int ret;
  1046. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  1047. unlikely(managed & DCACHE_MANAGED_DENTRY)) {
  1048. /* Allow the filesystem to manage the transit without i_mutex
  1049. * being held.
  1050. *
  1051. * We indicate to the filesystem if someone is trying to mount
  1052. * something here. This gives autofs the chance to deny anyone
  1053. * other than its daemon the right to mount on its
  1054. * superstructure.
  1055. *
  1056. * The filesystem may sleep at this point.
  1057. */
  1058. if (managed & DCACHE_MANAGE_TRANSIT) {
  1059. BUG_ON(!path->dentry->d_op);
  1060. BUG_ON(!path->dentry->d_op->d_manage);
  1061. ret = path->dentry->d_op->d_manage(
  1062. path->dentry, false);
  1063. if (ret < 0)
  1064. return ret == -EISDIR ? 0 : ret;
  1065. }
  1066. /* Transit to a mounted filesystem. */
  1067. if (managed & DCACHE_MOUNTED) {
  1068. struct vfsmount *mounted = lookup_mnt(path);
  1069. if (!mounted)
  1070. break;
  1071. dput(path->dentry);
  1072. mntput(path->mnt);
  1073. path->mnt = mounted;
  1074. path->dentry = dget(mounted->mnt_root);
  1075. continue;
  1076. }
  1077. /* Don't handle automount points here */
  1078. break;
  1079. }
  1080. return 0;
  1081. }
  1082. /*
  1083. * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
  1084. */
  1085. static void follow_mount(struct path *path)
  1086. {
  1087. while (d_mountpoint(path->dentry)) {
  1088. struct vfsmount *mounted = lookup_mnt(path);
  1089. if (!mounted)
  1090. break;
  1091. dput(path->dentry);
  1092. mntput(path->mnt);
  1093. path->mnt = mounted;
  1094. path->dentry = dget(mounted->mnt_root);
  1095. }
  1096. }
  1097. static void follow_dotdot(struct nameidata *nd)
  1098. {
  1099. set_root(nd);
  1100. while(1) {
  1101. struct dentry *old = nd->path.dentry;
  1102. if (nd->path.dentry == nd->root.dentry &&
  1103. nd->path.mnt == nd->root.mnt) {
  1104. break;
  1105. }
  1106. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  1107. /* rare case of legitimate dget_parent()... */
  1108. nd->path.dentry = dget_parent(nd->path.dentry);
  1109. dput(old);
  1110. break;
  1111. }
  1112. if (!follow_up(&nd->path))
  1113. break;
  1114. }
  1115. follow_mount(&nd->path);
  1116. nd->inode = nd->path.dentry->d_inode;
  1117. }
  1118. /*
  1119. * This looks up the name in dcache, possibly revalidates the old dentry and
  1120. * allocates a new one if not found or not valid. In the need_lookup argument
  1121. * returns whether i_op->lookup is necessary.
  1122. *
  1123. * dir->d_inode->i_mutex must be held
  1124. */
  1125. static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir,
  1126. unsigned int flags, bool *need_lookup)
  1127. {
  1128. struct dentry *dentry;
  1129. int error;
  1130. *need_lookup = false;
  1131. dentry = d_lookup(dir, name);
  1132. if (dentry) {
  1133. if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
  1134. error = d_revalidate(dentry, flags);
  1135. if (unlikely(error <= 0)) {
  1136. if (error < 0) {
  1137. dput(dentry);
  1138. return ERR_PTR(error);
  1139. } else if (!d_invalidate(dentry)) {
  1140. dput(dentry);
  1141. dentry = NULL;
  1142. }
  1143. }
  1144. }
  1145. }
  1146. if (!dentry) {
  1147. dentry = d_alloc(dir, name);
  1148. if (unlikely(!dentry))
  1149. return ERR_PTR(-ENOMEM);
  1150. *need_lookup = true;
  1151. }
  1152. return dentry;
  1153. }
  1154. /*
  1155. * Call i_op->lookup on the dentry. The dentry must be negative and
  1156. * unhashed.
  1157. *
  1158. * dir->d_inode->i_mutex must be held
  1159. */
  1160. static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
  1161. unsigned int flags)
  1162. {
  1163. struct dentry *old;
  1164. /* Don't create child dentry for a dead directory. */
  1165. if (unlikely(IS_DEADDIR(dir))) {
  1166. dput(dentry);
  1167. return ERR_PTR(-ENOENT);
  1168. }
  1169. old = dir->i_op->lookup(dir, dentry, flags);
  1170. if (unlikely(old)) {
  1171. dput(dentry);
  1172. dentry = old;
  1173. }
  1174. return dentry;
  1175. }
  1176. static struct dentry *__lookup_hash(struct qstr *name,
  1177. struct dentry *base, unsigned int flags)
  1178. {
  1179. bool need_lookup;
  1180. struct dentry *dentry;
  1181. dentry = lookup_dcache(name, base, flags, &need_lookup);
  1182. if (!need_lookup)
  1183. return dentry;
  1184. return lookup_real(base->d_inode, dentry, flags);
  1185. }
  1186. /*
  1187. * It's more convoluted than I'd like it to be, but... it's still fairly
  1188. * small and for now I'd prefer to have fast path as straight as possible.
  1189. * It _is_ time-critical.
  1190. */
  1191. static int lookup_fast(struct nameidata *nd,
  1192. struct path *path, struct inode **inode)
  1193. {
  1194. struct vfsmount *mnt = nd->path.mnt;
  1195. struct dentry *dentry, *parent = nd->path.dentry;
  1196. int need_reval = 1;
  1197. int status = 1;
  1198. int err;
  1199. /*
  1200. * Rename seqlock is not required here because in the off chance
  1201. * of a false negative due to a concurrent rename, we're going to
  1202. * do the non-racy lookup, below.
  1203. */
  1204. if (nd->flags & LOOKUP_RCU) {
  1205. unsigned seq;
  1206. dentry = __d_lookup_rcu(parent, &nd->last, &seq);
  1207. if (!dentry)
  1208. goto unlazy;
  1209. /*
  1210. * This sequence count validates that the inode matches
  1211. * the dentry name information from lookup.
  1212. */
  1213. *inode = dentry->d_inode;
  1214. if (read_seqcount_retry(&dentry->d_seq, seq))
  1215. return -ECHILD;
  1216. /*
  1217. * This sequence count validates that the parent had no
  1218. * changes while we did the lookup of the dentry above.
  1219. *
  1220. * The memory barrier in read_seqcount_begin of child is
  1221. * enough, we can use __read_seqcount_retry here.
  1222. */
  1223. if (__read_seqcount_retry(&parent->d_seq, nd->seq))
  1224. return -ECHILD;
  1225. nd->seq = seq;
  1226. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  1227. status = d_revalidate(dentry, nd->flags);
  1228. if (unlikely(status <= 0)) {
  1229. if (status != -ECHILD)
  1230. need_reval = 0;
  1231. goto unlazy;
  1232. }
  1233. }
  1234. path->mnt = mnt;
  1235. path->dentry = dentry;
  1236. if (unlikely(!__follow_mount_rcu(nd, path, inode)))
  1237. goto unlazy;
  1238. if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
  1239. goto unlazy;
  1240. return 0;
  1241. unlazy:
  1242. if (unlazy_walk(nd, dentry))
  1243. return -ECHILD;
  1244. } else {
  1245. dentry = __d_lookup(parent, &nd->last);
  1246. }
  1247. if (unlikely(!dentry))
  1248. goto need_lookup;
  1249. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
  1250. status = d_revalidate(dentry, nd->flags);
  1251. if (unlikely(status <= 0)) {
  1252. if (status < 0) {
  1253. dput(dentry);
  1254. return status;
  1255. }
  1256. if (!d_invalidate(dentry)) {
  1257. dput(dentry);
  1258. goto need_lookup;
  1259. }
  1260. }
  1261. path->mnt = mnt;
  1262. path->dentry = dentry;
  1263. err = follow_managed(path, nd->flags);
  1264. if (unlikely(err < 0)) {
  1265. path_put_conditional(path, nd);
  1266. return err;
  1267. }
  1268. if (err)
  1269. nd->flags |= LOOKUP_JUMPED;
  1270. *inode = path->dentry->d_inode;
  1271. return 0;
  1272. need_lookup:
  1273. return 1;
  1274. }
  1275. /* Fast lookup failed, do it the slow way */
  1276. static int lookup_slow(struct nameidata *nd, struct path *path)
  1277. {
  1278. struct dentry *dentry, *parent;
  1279. int err;
  1280. parent = nd->path.dentry;
  1281. BUG_ON(nd->inode != parent->d_inode);
  1282. mutex_lock(&parent->d_inode->i_mutex);
  1283. dentry = __lookup_hash(&nd->last, parent, nd->flags);
  1284. mutex_unlock(&parent->d_inode->i_mutex);
  1285. if (IS_ERR(dentry))
  1286. return PTR_ERR(dentry);
  1287. path->mnt = nd->path.mnt;
  1288. path->dentry = dentry;
  1289. err = follow_managed(path, nd->flags);
  1290. if (unlikely(err < 0)) {
  1291. path_put_conditional(path, nd);
  1292. return err;
  1293. }
  1294. if (err)
  1295. nd->flags |= LOOKUP_JUMPED;
  1296. return 0;
  1297. }
  1298. static inline int may_lookup(struct nameidata *nd)
  1299. {
  1300. if (nd->flags & LOOKUP_RCU) {
  1301. int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
  1302. if (err != -ECHILD)
  1303. return err;
  1304. if (unlazy_walk(nd, NULL))
  1305. return -ECHILD;
  1306. }
  1307. return inode_permission(nd->inode, MAY_EXEC);
  1308. }
  1309. static inline int handle_dots(struct nameidata *nd, int type)
  1310. {
  1311. if (type == LAST_DOTDOT) {
  1312. if (nd->flags & LOOKUP_RCU) {
  1313. if (follow_dotdot_rcu(nd))
  1314. return -ECHILD;
  1315. } else
  1316. follow_dotdot(nd);
  1317. }
  1318. return 0;
  1319. }
  1320. static void terminate_walk(struct nameidata *nd)
  1321. {
  1322. if (!(nd->flags & LOOKUP_RCU)) {
  1323. path_put(&nd->path);
  1324. } else {
  1325. nd->flags &= ~LOOKUP_RCU;
  1326. if (!(nd->flags & LOOKUP_ROOT))
  1327. nd->root.mnt = NULL;
  1328. rcu_read_unlock();
  1329. }
  1330. }
  1331. /*
  1332. * Do we need to follow links? We _really_ want to be able
  1333. * to do this check without having to look at inode->i_op,
  1334. * so we keep a cache of "no, this doesn't need follow_link"
  1335. * for the common case.
  1336. */
  1337. static inline int should_follow_link(struct dentry *dentry, int follow)
  1338. {
  1339. return unlikely(d_is_symlink(dentry)) ? follow : 0;
  1340. }
  1341. static inline int walk_component(struct nameidata *nd, struct path *path,
  1342. int follow)
  1343. {
  1344. struct inode *inode;
  1345. int err;
  1346. /*
  1347. * "." and ".." are special - ".." especially so because it has
  1348. * to be able to know about the current root directory and
  1349. * parent relationships.
  1350. */
  1351. if (unlikely(nd->last_type != LAST_NORM))
  1352. return handle_dots(nd, nd->last_type);
  1353. err = lookup_fast(nd, path, &inode);
  1354. if (unlikely(err)) {
  1355. if (err < 0)
  1356. goto out_err;
  1357. err = lookup_slow(nd, path);
  1358. if (err < 0)
  1359. goto out_err;
  1360. inode = path->dentry->d_inode;
  1361. }
  1362. err = -ENOENT;
  1363. if (!inode)
  1364. goto out_path_put;
  1365. if (should_follow_link(path->dentry, follow)) {
  1366. if (nd->flags & LOOKUP_RCU) {
  1367. if (unlikely(unlazy_walk(nd, path->dentry))) {
  1368. err = -ECHILD;
  1369. goto out_err;
  1370. }
  1371. }
  1372. BUG_ON(inode != path->dentry->d_inode);
  1373. return 1;
  1374. }
  1375. path_to_nameidata(path, nd);
  1376. nd->inode = inode;
  1377. return 0;
  1378. out_path_put:
  1379. path_to_nameidata(path, nd);
  1380. out_err:
  1381. terminate_walk(nd);
  1382. return err;
  1383. }
  1384. /*
  1385. * This limits recursive symlink follows to 8, while
  1386. * limiting consecutive symlinks to 40.
  1387. *
  1388. * Without that kind of total limit, nasty chains of consecutive
  1389. * symlinks can cause almost arbitrarily long lookups.
  1390. */
  1391. static inline int nested_symlink(struct path *path, struct nameidata *nd)
  1392. {
  1393. int res;
  1394. if (unlikely(current->link_count >= MAX_NESTED_LINKS)) {
  1395. path_put_conditional(path, nd);
  1396. path_put(&nd->path);
  1397. return -ELOOP;
  1398. }
  1399. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  1400. nd->depth++;
  1401. current->link_count++;
  1402. do {
  1403. struct path link = *path;
  1404. void *cookie;
  1405. res = follow_link(&link, nd, &cookie);
  1406. if (res)
  1407. break;
  1408. res = walk_component(nd, path, LOOKUP_FOLLOW);
  1409. put_link(nd, &link, cookie);
  1410. } while (res > 0);
  1411. current->link_count--;
  1412. nd->depth--;
  1413. return res;
  1414. }
  1415. /*
  1416. * We can do the critical dentry name comparison and hashing
  1417. * operations one word at a time, but we are limited to:
  1418. *
  1419. * - Architectures with fast unaligned word accesses. We could
  1420. * do a "get_unaligned()" if this helps and is sufficiently
  1421. * fast.
  1422. *
  1423. * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
  1424. * do not trap on the (extremely unlikely) case of a page
  1425. * crossing operation.
  1426. *
  1427. * - Furthermore, we need an efficient 64-bit compile for the
  1428. * 64-bit case in order to generate the "number of bytes in
  1429. * the final mask". Again, that could be replaced with a
  1430. * efficient population count instruction or similar.
  1431. */
  1432. #ifdef CONFIG_DCACHE_WORD_ACCESS
  1433. #include <asm/word-at-a-time.h>
  1434. #ifdef CONFIG_64BIT
  1435. static inline unsigned int fold_hash(unsigned long hash)
  1436. {
  1437. hash += hash >> (8*sizeof(int));
  1438. return hash;
  1439. }
  1440. #else /* 32-bit case */
  1441. #define fold_hash(x) (x)
  1442. #endif
  1443. unsigned int full_name_hash(const unsigned char *name, unsigned int len)
  1444. {
  1445. unsigned long a, mask;
  1446. unsigned long hash = 0;
  1447. for (;;) {
  1448. a = load_unaligned_zeropad(name);
  1449. if (len < sizeof(unsigned long))
  1450. break;
  1451. hash += a;
  1452. hash *= 9;
  1453. name += sizeof(unsigned long);
  1454. len -= sizeof(unsigned long);
  1455. if (!len)
  1456. goto done;
  1457. }
  1458. mask = bytemask_from_count(len);
  1459. hash += mask & a;
  1460. done:
  1461. return fold_hash(hash);
  1462. }
  1463. EXPORT_SYMBOL(full_name_hash);
  1464. /*
  1465. * Calculate the length and hash of the path component, and
  1466. * return the length of the component;
  1467. */
  1468. static inline unsigned long hash_name(const char *name, unsigned int *hashp)
  1469. {
  1470. unsigned long a, b, adata, bdata, mask, hash, len;
  1471. const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
  1472. hash = a = 0;
  1473. len = -sizeof(unsigned long);
  1474. do {
  1475. hash = (hash + a) * 9;
  1476. len += sizeof(unsigned long);
  1477. a = load_unaligned_zeropad(name+len);
  1478. b = a ^ REPEAT_BYTE('/');
  1479. } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
  1480. adata = prep_zero_mask(a, adata, &constants);
  1481. bdata = prep_zero_mask(b, bdata, &constants);
  1482. mask = create_zero_mask(adata | bdata);
  1483. hash += a & zero_bytemask(mask);
  1484. *hashp = fold_hash(hash);
  1485. return len + find_zero(mask);
  1486. }
  1487. #else
  1488. unsigned int full_name_hash(const unsigned char *name, unsigned int len)
  1489. {
  1490. unsigned long hash = init_name_hash();
  1491. while (len--)
  1492. hash = partial_name_hash(*name++, hash);
  1493. return end_name_hash(hash);
  1494. }
  1495. EXPORT_SYMBOL(full_name_hash);
  1496. /*
  1497. * We know there's a real path component here of at least
  1498. * one character.
  1499. */
  1500. static inline unsigned long hash_name(const char *name, unsigned int *hashp)
  1501. {
  1502. unsigned long hash = init_name_hash();
  1503. unsigned long len = 0, c;
  1504. c = (unsigned char)*name;
  1505. do {
  1506. len++;
  1507. hash = partial_name_hash(c, hash);
  1508. c = (unsigned char)name[len];
  1509. } while (c && c != '/');
  1510. *hashp = end_name_hash(hash);
  1511. return len;
  1512. }
  1513. #endif
  1514. /*
  1515. * Name resolution.
  1516. * This is the basic name resolution function, turning a pathname into
  1517. * the final dentry. We expect 'base' to be positive and a directory.
  1518. *
  1519. * Returns 0 and nd will have valid dentry and mnt on success.
  1520. * Returns error and drops reference to input namei data on failure.
  1521. */
  1522. static int link_path_walk(const char *name, struct nameidata *nd)
  1523. {
  1524. struct path next;
  1525. int err;
  1526. while (*name=='/')
  1527. name++;
  1528. if (!*name)
  1529. return 0;
  1530. /* At this point we know we have a real path component. */
  1531. for(;;) {
  1532. struct qstr this;
  1533. long len;
  1534. int type;
  1535. err = may_lookup(nd);
  1536. if (err)
  1537. break;
  1538. len = hash_name(name, &this.hash);
  1539. this.name = name;
  1540. this.len = len;
  1541. type = LAST_NORM;
  1542. if (name[0] == '.') switch (len) {
  1543. case 2:
  1544. if (name[1] == '.') {
  1545. type = LAST_DOTDOT;
  1546. nd->flags |= LOOKUP_JUMPED;
  1547. }
  1548. break;
  1549. case 1:
  1550. type = LAST_DOT;
  1551. }
  1552. if (likely(type == LAST_NORM)) {
  1553. struct dentry *parent = nd->path.dentry;
  1554. nd->flags &= ~LOOKUP_JUMPED;
  1555. if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
  1556. err = parent->d_op->d_hash(parent, &this);
  1557. if (err < 0)
  1558. break;
  1559. }
  1560. }
  1561. nd->last = this;
  1562. nd->last_type = type;
  1563. if (!name[len])
  1564. return 0;
  1565. /*
  1566. * If it wasn't NUL, we know it was '/'. Skip that
  1567. * slash, and continue until no more slashes.
  1568. */
  1569. do {
  1570. len++;
  1571. } while (unlikely(name[len] == '/'));
  1572. if (!name[len])
  1573. return 0;
  1574. name += len;
  1575. err = walk_component(nd, &next, LOOKUP_FOLLOW);
  1576. if (err < 0)
  1577. return err;
  1578. if (err) {
  1579. err = nested_symlink(&next, nd);
  1580. if (err)
  1581. return err;
  1582. }
  1583. if (!d_is_directory(nd->path.dentry)) {
  1584. err = -ENOTDIR;
  1585. break;
  1586. }
  1587. }
  1588. terminate_walk(nd);
  1589. return err;
  1590. }
  1591. static int path_init(int dfd, const char *name, unsigned int flags,
  1592. struct nameidata *nd, struct file **fp)
  1593. {
  1594. int retval = 0;
  1595. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1596. nd->flags = flags | LOOKUP_JUMPED;
  1597. nd->depth = 0;
  1598. if (flags & LOOKUP_ROOT) {
  1599. struct dentry *root = nd->root.dentry;
  1600. struct inode *inode = root->d_inode;
  1601. if (*name) {
  1602. if (!d_is_directory(root))
  1603. return -ENOTDIR;
  1604. retval = inode_permission(inode, MAY_EXEC);
  1605. if (retval)
  1606. return retval;
  1607. }
  1608. nd->path = nd->root;
  1609. nd->inode = inode;
  1610. if (flags & LOOKUP_RCU) {
  1611. rcu_read_lock();
  1612. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1613. nd->m_seq = read_seqbegin(&mount_lock);
  1614. } else {
  1615. path_get(&nd->path);
  1616. }
  1617. return 0;
  1618. }
  1619. nd->root.mnt = NULL;
  1620. nd->m_seq = read_seqbegin(&mount_lock);
  1621. if (*name=='/') {
  1622. if (flags & LOOKUP_RCU) {
  1623. rcu_read_lock();
  1624. set_root_rcu(nd);
  1625. } else {
  1626. set_root(nd);
  1627. path_get(&nd->root);
  1628. }
  1629. nd->path = nd->root;
  1630. } else if (dfd == AT_FDCWD) {
  1631. if (flags & LOOKUP_RCU) {
  1632. struct fs_struct *fs = current->fs;
  1633. unsigned seq;
  1634. rcu_read_lock();
  1635. do {
  1636. seq = read_seqcount_begin(&fs->seq);
  1637. nd->path = fs->pwd;
  1638. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1639. } while (read_seqcount_retry(&fs->seq, seq));
  1640. } else {
  1641. get_fs_pwd(current->fs, &nd->path);
  1642. }
  1643. } else {
  1644. /* Caller must check execute permissions on the starting path component */
  1645. struct fd f = fdget_raw(dfd);
  1646. struct dentry *dentry;
  1647. if (!f.file)
  1648. return -EBADF;
  1649. dentry = f.file->f_path.dentry;
  1650. if (*name) {
  1651. if (!d_is_directory(dentry)) {
  1652. fdput(f);
  1653. return -ENOTDIR;
  1654. }
  1655. }
  1656. nd->path = f.file->f_path;
  1657. if (flags & LOOKUP_RCU) {
  1658. if (f.flags & FDPUT_FPUT)
  1659. *fp = f.file;
  1660. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1661. rcu_read_lock();
  1662. } else {
  1663. path_get(&nd->path);
  1664. fdput(f);
  1665. }
  1666. }
  1667. nd->inode = nd->path.dentry->d_inode;
  1668. return 0;
  1669. }
  1670. static inline int lookup_last(struct nameidata *nd, struct path *path)
  1671. {
  1672. if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
  1673. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1674. nd->flags &= ~LOOKUP_PARENT;
  1675. return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW);
  1676. }
  1677. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1678. static int path_lookupat(int dfd, const char *name,
  1679. unsigned int flags, struct nameidata *nd)
  1680. {
  1681. struct file *base = NULL;
  1682. struct path path;
  1683. int err;
  1684. /*
  1685. * Path walking is largely split up into 2 different synchronisation
  1686. * schemes, rcu-walk and ref-walk (explained in
  1687. * Documentation/filesystems/path-lookup.txt). These share much of the
  1688. * path walk code, but some things particularly setup, cleanup, and
  1689. * following mounts are sufficiently divergent that functions are
  1690. * duplicated. Typically there is a function foo(), and its RCU
  1691. * analogue, foo_rcu().
  1692. *
  1693. * -ECHILD is the error number of choice (just to avoid clashes) that
  1694. * is returned if some aspect of an rcu-walk fails. Such an error must
  1695. * be handled by restarting a traditional ref-walk (which will always
  1696. * be able to complete).
  1697. */
  1698. err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base);
  1699. if (unlikely(err))
  1700. return err;
  1701. current->total_link_count = 0;
  1702. err = link_path_walk(name, nd);
  1703. if (!err && !(flags & LOOKUP_PARENT)) {
  1704. err = lookup_last(nd, &path);
  1705. while (err > 0) {
  1706. void *cookie;
  1707. struct path link = path;
  1708. err = may_follow_link(&link, nd);
  1709. if (unlikely(err))
  1710. break;
  1711. nd->flags |= LOOKUP_PARENT;
  1712. err = follow_link(&link, nd, &cookie);
  1713. if (err)
  1714. break;
  1715. err = lookup_last(nd, &path);
  1716. put_link(nd, &link, cookie);
  1717. }
  1718. }
  1719. if (!err)
  1720. err = complete_walk(nd);
  1721. if (!err && nd->flags & LOOKUP_DIRECTORY) {
  1722. if (!d_is_directory(nd->path.dentry)) {
  1723. path_put(&nd->path);
  1724. err = -ENOTDIR;
  1725. }
  1726. }
  1727. if (base)
  1728. fput(base);
  1729. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  1730. path_put(&nd->root);
  1731. nd->root.mnt = NULL;
  1732. }
  1733. return err;
  1734. }
  1735. static int filename_lookup(int dfd, struct filename *name,
  1736. unsigned int flags, struct nameidata *nd)
  1737. {
  1738. int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd);
  1739. if (unlikely(retval == -ECHILD))
  1740. retval = path_lookupat(dfd, name->name, flags, nd);
  1741. if (unlikely(retval == -ESTALE))
  1742. retval = path_lookupat(dfd, name->name,
  1743. flags | LOOKUP_REVAL, nd);
  1744. if (likely(!retval))
  1745. audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT);
  1746. return retval;
  1747. }
  1748. static int do_path_lookup(int dfd, const char *name,
  1749. unsigned int flags, struct nameidata *nd)
  1750. {
  1751. struct filename filename = { .name = name };
  1752. return filename_lookup(dfd, &filename, flags, nd);
  1753. }
  1754. /* does lookup, returns the object with parent locked */
  1755. struct dentry *kern_path_locked(const char *name, struct path *path)
  1756. {
  1757. struct nameidata nd;
  1758. struct dentry *d;
  1759. int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd);
  1760. if (err)
  1761. return ERR_PTR(err);
  1762. if (nd.last_type != LAST_NORM) {
  1763. path_put(&nd.path);
  1764. return ERR_PTR(-EINVAL);
  1765. }
  1766. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  1767. d = __lookup_hash(&nd.last, nd.path.dentry, 0);
  1768. if (IS_ERR(d)) {
  1769. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  1770. path_put(&nd.path);
  1771. return d;
  1772. }
  1773. *path = nd.path;
  1774. return d;
  1775. }
  1776. int kern_path(const char *name, unsigned int flags, struct path *path)
  1777. {
  1778. struct nameidata nd;
  1779. int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
  1780. if (!res)
  1781. *path = nd.path;
  1782. return res;
  1783. }
  1784. /**
  1785. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  1786. * @dentry: pointer to dentry of the base directory
  1787. * @mnt: pointer to vfs mount of the base directory
  1788. * @name: pointer to file name
  1789. * @flags: lookup flags
  1790. * @path: pointer to struct path to fill
  1791. */
  1792. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  1793. const char *name, unsigned int flags,
  1794. struct path *path)
  1795. {
  1796. struct nameidata nd;
  1797. int err;
  1798. nd.root.dentry = dentry;
  1799. nd.root.mnt = mnt;
  1800. BUG_ON(flags & LOOKUP_PARENT);
  1801. /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
  1802. err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd);
  1803. if (!err)
  1804. *path = nd.path;
  1805. return err;
  1806. }
  1807. /*
  1808. * Restricted form of lookup. Doesn't follow links, single-component only,
  1809. * needs parent already locked. Doesn't follow mounts.
  1810. * SMP-safe.
  1811. */
  1812. static struct dentry *lookup_hash(struct nameidata *nd)
  1813. {
  1814. return __lookup_hash(&nd->last, nd->path.dentry, nd->flags);
  1815. }
  1816. /**
  1817. * lookup_one_len - filesystem helper to lookup single pathname component
  1818. * @name: pathname component to lookup
  1819. * @base: base directory to lookup from
  1820. * @len: maximum length @len should be interpreted to
  1821. *
  1822. * Note that this routine is purely a helper for filesystem usage and should
  1823. * not be called by generic code. Also note that by using this function the
  1824. * nameidata argument is passed to the filesystem methods and a filesystem
  1825. * using this helper needs to be prepared for that.
  1826. */
  1827. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  1828. {
  1829. struct qstr this;
  1830. unsigned int c;
  1831. int err;
  1832. WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
  1833. this.name = name;
  1834. this.len = len;
  1835. this.hash = full_name_hash(name, len);
  1836. if (!len)
  1837. return ERR_PTR(-EACCES);
  1838. if (unlikely(name[0] == '.')) {
  1839. if (len < 2 || (len == 2 && name[1] == '.'))
  1840. return ERR_PTR(-EACCES);
  1841. }
  1842. while (len--) {
  1843. c = *(const unsigned char *)name++;
  1844. if (c == '/' || c == '\0')
  1845. return ERR_PTR(-EACCES);
  1846. }
  1847. /*
  1848. * See if the low-level filesystem might want
  1849. * to use its own hash..
  1850. */
  1851. if (base->d_flags & DCACHE_OP_HASH) {
  1852. int err = base->d_op->d_hash(base, &this);
  1853. if (err < 0)
  1854. return ERR_PTR(err);
  1855. }
  1856. err = inode_permission(base->d_inode, MAY_EXEC);
  1857. if (err)
  1858. return ERR_PTR(err);
  1859. return __lookup_hash(&this, base, 0);
  1860. }
  1861. int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
  1862. struct path *path, int *empty)
  1863. {
  1864. struct nameidata nd;
  1865. struct filename *tmp = getname_flags(name, flags, empty);
  1866. int err = PTR_ERR(tmp);
  1867. if (!IS_ERR(tmp)) {
  1868. BUG_ON(flags & LOOKUP_PARENT);
  1869. err = filename_lookup(dfd, tmp, flags, &nd);
  1870. putname(tmp);
  1871. if (!err)
  1872. *path = nd.path;
  1873. }
  1874. return err;
  1875. }
  1876. int user_path_at(int dfd, const char __user *name, unsigned flags,
  1877. struct path *path)
  1878. {
  1879. return user_path_at_empty(dfd, name, flags, path, NULL);
  1880. }
  1881. /*
  1882. * NB: most callers don't do anything directly with the reference to the
  1883. * to struct filename, but the nd->last pointer points into the name string
  1884. * allocated by getname. So we must hold the reference to it until all
  1885. * path-walking is complete.
  1886. */
  1887. static struct filename *
  1888. user_path_parent(int dfd, const char __user *path, struct nameidata *nd,
  1889. unsigned int flags)
  1890. {
  1891. struct filename *s = getname(path);
  1892. int error;
  1893. /* only LOOKUP_REVAL is allowed in extra flags */
  1894. flags &= LOOKUP_REVAL;
  1895. if (IS_ERR(s))
  1896. return s;
  1897. error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd);
  1898. if (error) {
  1899. putname(s);
  1900. return ERR_PTR(error);
  1901. }
  1902. return s;
  1903. }
  1904. /**
  1905. * mountpoint_last - look up last component for umount
  1906. * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
  1907. * @path: pointer to container for result
  1908. *
  1909. * This is a special lookup_last function just for umount. In this case, we
  1910. * need to resolve the path without doing any revalidation.
  1911. *
  1912. * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
  1913. * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
  1914. * in almost all cases, this lookup will be served out of the dcache. The only
  1915. * cases where it won't are if nd->last refers to a symlink or the path is
  1916. * bogus and it doesn't exist.
  1917. *
  1918. * Returns:
  1919. * -error: if there was an error during lookup. This includes -ENOENT if the
  1920. * lookup found a negative dentry. The nd->path reference will also be
  1921. * put in this case.
  1922. *
  1923. * 0: if we successfully resolved nd->path and found it to not to be a
  1924. * symlink that needs to be followed. "path" will also be populated.
  1925. * The nd->path reference will also be put.
  1926. *
  1927. * 1: if we successfully resolved nd->last and found it to be a symlink
  1928. * that needs to be followed. "path" will be populated with the path
  1929. * to the link, and nd->path will *not* be put.
  1930. */
  1931. static int
  1932. mountpoint_last(struct nameidata *nd, struct path *path)
  1933. {
  1934. int error = 0;
  1935. struct dentry *dentry;
  1936. struct dentry *dir = nd->path.dentry;
  1937. /* If we're in rcuwalk, drop out of it to handle last component */
  1938. if (nd->flags & LOOKUP_RCU) {
  1939. if (unlazy_walk(nd, NULL)) {
  1940. error = -ECHILD;
  1941. goto out;
  1942. }
  1943. }
  1944. nd->flags &= ~LOOKUP_PARENT;
  1945. if (unlikely(nd->last_type != LAST_NORM)) {
  1946. error = handle_dots(nd, nd->last_type);
  1947. if (error)
  1948. goto out;
  1949. dentry = dget(nd->path.dentry);
  1950. goto done;
  1951. }
  1952. mutex_lock(&dir->d_inode->i_mutex);
  1953. dentry = d_lookup(dir, &nd->last);
  1954. if (!dentry) {
  1955. /*
  1956. * No cached dentry. Mounted dentries are pinned in the cache,
  1957. * so that means that this dentry is probably a symlink or the
  1958. * path doesn't actually point to a mounted dentry.
  1959. */
  1960. dentry = d_alloc(dir, &nd->last);
  1961. if (!dentry) {
  1962. error = -ENOMEM;
  1963. mutex_unlock(&dir->d_inode->i_mutex);
  1964. goto out;
  1965. }
  1966. dentry = lookup_real(dir->d_inode, dentry, nd->flags);
  1967. error = PTR_ERR(dentry);
  1968. if (IS_ERR(dentry)) {
  1969. mutex_unlock(&dir->d_inode->i_mutex);
  1970. goto out;
  1971. }
  1972. }
  1973. mutex_unlock(&dir->d_inode->i_mutex);
  1974. done:
  1975. if (!dentry->d_inode) {
  1976. error = -ENOENT;
  1977. dput(dentry);
  1978. goto out;
  1979. }
  1980. path->dentry = dentry;
  1981. path->mnt = mntget(nd->path.mnt);
  1982. if (should_follow_link(dentry, nd->flags & LOOKUP_FOLLOW))
  1983. return 1;
  1984. follow_mount(path);
  1985. error = 0;
  1986. out:
  1987. terminate_walk(nd);
  1988. return error;
  1989. }
  1990. /**
  1991. * path_mountpoint - look up a path to be umounted
  1992. * @dfd: directory file descriptor to start walk from
  1993. * @name: full pathname to walk
  1994. * @path: pointer to container for result
  1995. * @flags: lookup flags
  1996. *
  1997. * Look up the given name, but don't attempt to revalidate the last component.
  1998. * Returns 0 and "path" will be valid on success; Returns error otherwise.
  1999. */
  2000. static int
  2001. path_mountpoint(int dfd, const char *name, struct path *path, unsigned int flags)
  2002. {
  2003. struct file *base = NULL;
  2004. struct nameidata nd;
  2005. int err;
  2006. err = path_init(dfd, name, flags | LOOKUP_PARENT, &nd, &base);
  2007. if (unlikely(err))
  2008. return err;
  2009. current->total_link_count = 0;
  2010. err = link_path_walk(name, &nd);
  2011. if (err)
  2012. goto out;
  2013. err = mountpoint_last(&nd, path);
  2014. while (err > 0) {
  2015. void *cookie;
  2016. struct path link = *path;
  2017. err = may_follow_link(&link, &nd);
  2018. if (unlikely(err))
  2019. break;
  2020. nd.flags |= LOOKUP_PARENT;
  2021. err = follow_link(&link, &nd, &cookie);
  2022. if (err)
  2023. break;
  2024. err = mountpoint_last(&nd, path);
  2025. put_link(&nd, &link, cookie);
  2026. }
  2027. out:
  2028. if (base)
  2029. fput(base);
  2030. if (nd.root.mnt && !(nd.flags & LOOKUP_ROOT))
  2031. path_put(&nd.root);
  2032. return err;
  2033. }
  2034. static int
  2035. filename_mountpoint(int dfd, struct filename *s, struct path *path,
  2036. unsigned int flags)
  2037. {
  2038. int error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_RCU);
  2039. if (unlikely(error == -ECHILD))
  2040. error = path_mountpoint(dfd, s->name, path, flags);
  2041. if (unlikely(error == -ESTALE))
  2042. error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_REVAL);
  2043. if (likely(!error))
  2044. audit_inode(s, path->dentry, 0);
  2045. return error;
  2046. }
  2047. /**
  2048. * user_path_mountpoint_at - lookup a path from userland in order to umount it
  2049. * @dfd: directory file descriptor
  2050. * @name: pathname from userland
  2051. * @flags: lookup flags
  2052. * @path: pointer to container to hold result
  2053. *
  2054. * A umount is a special case for path walking. We're not actually interested
  2055. * in the inode in this situation, and ESTALE errors can be a problem. We
  2056. * simply want track down the dentry and vfsmount attached at the mountpoint
  2057. * and avoid revalidating the last component.
  2058. *
  2059. * Returns 0 and populates "path" on success.
  2060. */
  2061. int
  2062. user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
  2063. struct path *path)
  2064. {
  2065. struct filename *s = getname(name);
  2066. int error;
  2067. if (IS_ERR(s))
  2068. return PTR_ERR(s);
  2069. error = filename_mountpoint(dfd, s, path, flags);
  2070. putname(s);
  2071. return error;
  2072. }
  2073. int
  2074. kern_path_mountpoint(int dfd, const char *name, struct path *path,
  2075. unsigned int flags)
  2076. {
  2077. struct filename s = {.name = name};
  2078. return filename_mountpoint(dfd, &s, path, flags);
  2079. }
  2080. EXPORT_SYMBOL(kern_path_mountpoint);
  2081. /*
  2082. * It's inline, so penalty for filesystems that don't use sticky bit is
  2083. * minimal.
  2084. */
  2085. static inline int check_sticky(struct inode *dir, struct inode *inode)
  2086. {
  2087. kuid_t fsuid = current_fsuid();
  2088. if (!(dir->i_mode & S_ISVTX))
  2089. return 0;
  2090. if (uid_eq(inode->i_uid, fsuid))
  2091. return 0;
  2092. if (uid_eq(dir->i_uid, fsuid))
  2093. return 0;
  2094. return !inode_capable(inode, CAP_FOWNER);
  2095. }
  2096. /*
  2097. * Check whether we can remove a link victim from directory dir, check
  2098. * whether the type of victim is right.
  2099. * 1. We can't do it if dir is read-only (done in permission())
  2100. * 2. We should have write and exec permissions on dir
  2101. * 3. We can't remove anything from append-only dir
  2102. * 4. We can't do anything with immutable dir (done in permission())
  2103. * 5. If the sticky bit on dir is set we should either
  2104. * a. be owner of dir, or
  2105. * b. be owner of victim, or
  2106. * c. have CAP_FOWNER capability
  2107. * 6. If the victim is append-only or immutable we can't do antyhing with
  2108. * links pointing to it.
  2109. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  2110. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  2111. * 9. We can't remove a root or mountpoint.
  2112. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  2113. * nfs_async_unlink().
  2114. */
  2115. static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
  2116. {
  2117. struct inode *inode = victim->d_inode;
  2118. int error;
  2119. if (d_is_negative(victim))
  2120. return -ENOENT;
  2121. BUG_ON(!inode);
  2122. BUG_ON(victim->d_parent->d_inode != dir);
  2123. audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
  2124. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  2125. if (error)
  2126. return error;
  2127. if (IS_APPEND(dir))
  2128. return -EPERM;
  2129. if (check_sticky(dir, inode) || IS_APPEND(inode) ||
  2130. IS_IMMUTABLE(inode) || IS_SWAPFILE(inode))
  2131. return -EPERM;
  2132. if (isdir) {
  2133. if (!d_is_directory(victim) && !d_is_autodir(victim))
  2134. return -ENOTDIR;
  2135. if (IS_ROOT(victim))
  2136. return -EBUSY;
  2137. } else if (d_is_directory(victim) || d_is_autodir(victim))
  2138. return -EISDIR;
  2139. if (IS_DEADDIR(dir))
  2140. return -ENOENT;
  2141. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  2142. return -EBUSY;
  2143. return 0;
  2144. }
  2145. /* Check whether we can create an object with dentry child in directory
  2146. * dir.
  2147. * 1. We can't do it if child already exists (open has special treatment for
  2148. * this case, but since we are inlined it's OK)
  2149. * 2. We can't do it if dir is read-only (done in permission())
  2150. * 3. We should have write and exec permissions on dir
  2151. * 4. We can't do it if dir is immutable (done in permission())
  2152. */
  2153. static inline int may_create(struct inode *dir, struct dentry *child)
  2154. {
  2155. audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
  2156. if (child->d_inode)
  2157. return -EEXIST;
  2158. if (IS_DEADDIR(dir))
  2159. return -ENOENT;
  2160. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  2161. }
  2162. /*
  2163. * p1 and p2 should be directories on the same fs.
  2164. */
  2165. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  2166. {
  2167. struct dentry *p;
  2168. if (p1 == p2) {
  2169. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  2170. return NULL;
  2171. }
  2172. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  2173. p = d_ancestor(p2, p1);
  2174. if (p) {
  2175. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  2176. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  2177. return p;
  2178. }
  2179. p = d_ancestor(p1, p2);
  2180. if (p) {
  2181. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  2182. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  2183. return p;
  2184. }
  2185. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  2186. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  2187. return NULL;
  2188. }
  2189. void unlock_rename(struct dentry *p1, struct dentry *p2)
  2190. {
  2191. mutex_unlock(&p1->d_inode->i_mutex);
  2192. if (p1 != p2) {
  2193. mutex_unlock(&p2->d_inode->i_mutex);
  2194. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  2195. }
  2196. }
  2197. int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  2198. bool want_excl)
  2199. {
  2200. int error = may_create(dir, dentry);
  2201. if (error)
  2202. return error;
  2203. if (!dir->i_op->create)
  2204. return -EACCES; /* shouldn't it be ENOSYS? */
  2205. mode &= S_IALLUGO;
  2206. mode |= S_IFREG;
  2207. error = security_inode_create(dir, dentry, mode);
  2208. if (error)
  2209. return error;
  2210. error = dir->i_op->create(dir, dentry, mode, want_excl);
  2211. if (!error)
  2212. fsnotify_create(dir, dentry);
  2213. return error;
  2214. }
  2215. static int may_open(struct path *path, int acc_mode, int flag)
  2216. {
  2217. struct dentry *dentry = path->dentry;
  2218. struct inode *inode = dentry->d_inode;
  2219. int error;
  2220. /* O_PATH? */
  2221. if (!acc_mode)
  2222. return 0;
  2223. if (!inode)
  2224. return -ENOENT;
  2225. switch (inode->i_mode & S_IFMT) {
  2226. case S_IFLNK:
  2227. return -ELOOP;
  2228. case S_IFDIR:
  2229. if (acc_mode & MAY_WRITE)
  2230. return -EISDIR;
  2231. break;
  2232. case S_IFBLK:
  2233. case S_IFCHR:
  2234. if (path->mnt->mnt_flags & MNT_NODEV)
  2235. return -EACCES;
  2236. /*FALLTHRU*/
  2237. case S_IFIFO:
  2238. case S_IFSOCK:
  2239. flag &= ~O_TRUNC;
  2240. break;
  2241. }
  2242. error = inode_permission(inode, acc_mode);
  2243. if (error)
  2244. return error;
  2245. /*
  2246. * An append-only file must be opened in append mode for writing.
  2247. */
  2248. if (IS_APPEND(inode)) {
  2249. if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
  2250. return -EPERM;
  2251. if (flag & O_TRUNC)
  2252. return -EPERM;
  2253. }
  2254. /* O_NOATIME can only be set by the owner or superuser */
  2255. if (flag & O_NOATIME && !inode_owner_or_capable(inode))
  2256. return -EPERM;
  2257. return 0;
  2258. }
  2259. static int handle_truncate(struct file *filp)
  2260. {
  2261. struct path *path = &filp->f_path;
  2262. struct inode *inode = path->dentry->d_inode;
  2263. int error = get_write_access(inode);
  2264. if (error)
  2265. return error;
  2266. /*
  2267. * Refuse to truncate files with mandatory locks held on them.
  2268. */
  2269. error = locks_verify_locked(inode);
  2270. if (!error)
  2271. error = security_path_truncate(path);
  2272. if (!error) {
  2273. error = do_truncate(path->dentry, 0,
  2274. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  2275. filp);
  2276. }
  2277. put_write_access(inode);
  2278. return error;
  2279. }
  2280. static inline int open_to_namei_flags(int flag)
  2281. {
  2282. if ((flag & O_ACCMODE) == 3)
  2283. flag--;
  2284. return flag;
  2285. }
  2286. static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode)
  2287. {
  2288. int error = security_path_mknod(dir, dentry, mode, 0);
  2289. if (error)
  2290. return error;
  2291. error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
  2292. if (error)
  2293. return error;
  2294. return security_inode_create(dir->dentry->d_inode, dentry, mode);
  2295. }
  2296. /*
  2297. * Attempt to atomically look up, create and open a file from a negative
  2298. * dentry.
  2299. *
  2300. * Returns 0 if successful. The file will have been created and attached to
  2301. * @file by the filesystem calling finish_open().
  2302. *
  2303. * Returns 1 if the file was looked up only or didn't need creating. The
  2304. * caller will need to perform the open themselves. @path will have been
  2305. * updated to point to the new dentry. This may be negative.
  2306. *
  2307. * Returns an error code otherwise.
  2308. */
  2309. static int atomic_open(struct nameidata *nd, struct dentry *dentry,
  2310. struct path *path, struct file *file,
  2311. const struct open_flags *op,
  2312. bool got_write, bool need_lookup,
  2313. int *opened)
  2314. {
  2315. struct inode *dir = nd->path.dentry->d_inode;
  2316. unsigned open_flag = open_to_namei_flags(op->open_flag);
  2317. umode_t mode;
  2318. int error;
  2319. int acc_mode;
  2320. int create_error = 0;
  2321. struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
  2322. bool excl;
  2323. BUG_ON(dentry->d_inode);
  2324. /* Don't create child dentry for a dead directory. */
  2325. if (unlikely(IS_DEADDIR(dir))) {
  2326. error = -ENOENT;
  2327. goto out;
  2328. }
  2329. mode = op->mode;
  2330. if ((open_flag & O_CREAT) && !IS_POSIXACL(dir))
  2331. mode &= ~current_umask();
  2332. excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT);
  2333. if (excl)
  2334. open_flag &= ~O_TRUNC;
  2335. /*
  2336. * Checking write permission is tricky, bacuse we don't know if we are
  2337. * going to actually need it: O_CREAT opens should work as long as the
  2338. * file exists. But checking existence breaks atomicity. The trick is
  2339. * to check access and if not granted clear O_CREAT from the flags.
  2340. *
  2341. * Another problem is returing the "right" error value (e.g. for an
  2342. * O_EXCL open we want to return EEXIST not EROFS).
  2343. */
  2344. if (((open_flag & (O_CREAT | O_TRUNC)) ||
  2345. (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) {
  2346. if (!(open_flag & O_CREAT)) {
  2347. /*
  2348. * No O_CREATE -> atomicity not a requirement -> fall
  2349. * back to lookup + open
  2350. */
  2351. goto no_open;
  2352. } else if (open_flag & (O_EXCL | O_TRUNC)) {
  2353. /* Fall back and fail with the right error */
  2354. create_error = -EROFS;
  2355. goto no_open;
  2356. } else {
  2357. /* No side effects, safe to clear O_CREAT */
  2358. create_error = -EROFS;
  2359. open_flag &= ~O_CREAT;
  2360. }
  2361. }
  2362. if (open_flag & O_CREAT) {
  2363. error = may_o_create(&nd->path, dentry, mode);
  2364. if (error) {
  2365. create_error = error;
  2366. if (open_flag & O_EXCL)
  2367. goto no_open;
  2368. open_flag &= ~O_CREAT;
  2369. }
  2370. }
  2371. if (nd->flags & LOOKUP_DIRECTORY)
  2372. open_flag |= O_DIRECTORY;
  2373. file->f_path.dentry = DENTRY_NOT_SET;
  2374. file->f_path.mnt = nd->path.mnt;
  2375. error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode,
  2376. opened);
  2377. if (error < 0) {
  2378. if (create_error && error == -ENOENT)
  2379. error = create_error;
  2380. goto out;
  2381. }
  2382. if (error) { /* returned 1, that is */
  2383. if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
  2384. error = -EIO;
  2385. goto out;
  2386. }
  2387. if (file->f_path.dentry) {
  2388. dput(dentry);
  2389. dentry = file->f_path.dentry;
  2390. }
  2391. if (*opened & FILE_CREATED)
  2392. fsnotify_create(dir, dentry);
  2393. if (!dentry->d_inode) {
  2394. WARN_ON(*opened & FILE_CREATED);
  2395. if (create_error) {
  2396. error = create_error;
  2397. goto out;
  2398. }
  2399. } else {
  2400. if (excl && !(*opened & FILE_CREATED)) {
  2401. error = -EEXIST;
  2402. goto out;
  2403. }
  2404. }
  2405. goto looked_up;
  2406. }
  2407. /*
  2408. * We didn't have the inode before the open, so check open permission
  2409. * here.
  2410. */
  2411. acc_mode = op->acc_mode;
  2412. if (*opened & FILE_CREATED) {
  2413. WARN_ON(!(open_flag & O_CREAT));
  2414. fsnotify_create(dir, dentry);
  2415. acc_mode = MAY_OPEN;
  2416. }
  2417. error = may_open(&file->f_path, acc_mode, open_flag);
  2418. if (error)
  2419. fput(file);
  2420. out:
  2421. dput(dentry);
  2422. return error;
  2423. no_open:
  2424. if (need_lookup) {
  2425. dentry = lookup_real(dir, dentry, nd->flags);
  2426. if (IS_ERR(dentry))
  2427. return PTR_ERR(dentry);
  2428. if (create_error) {
  2429. int open_flag = op->open_flag;
  2430. error = create_error;
  2431. if ((open_flag & O_EXCL)) {
  2432. if (!dentry->d_inode)
  2433. goto out;
  2434. } else if (!dentry->d_inode) {
  2435. goto out;
  2436. } else if ((open_flag & O_TRUNC) &&
  2437. S_ISREG(dentry->d_inode->i_mode)) {
  2438. goto out;
  2439. }
  2440. /* will fail later, go on to get the right error */
  2441. }
  2442. }
  2443. looked_up:
  2444. path->dentry = dentry;
  2445. path->mnt = nd->path.mnt;
  2446. return 1;
  2447. }
  2448. /*
  2449. * Look up and maybe create and open the last component.
  2450. *
  2451. * Must be called with i_mutex held on parent.
  2452. *
  2453. * Returns 0 if the file was successfully atomically created (if necessary) and
  2454. * opened. In this case the file will be returned attached to @file.
  2455. *
  2456. * Returns 1 if the file was not completely opened at this time, though lookups
  2457. * and creations will have been performed and the dentry returned in @path will
  2458. * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
  2459. * specified then a negative dentry may be returned.
  2460. *
  2461. * An error code is returned otherwise.
  2462. *
  2463. * FILE_CREATE will be set in @*opened if the dentry was created and will be
  2464. * cleared otherwise prior to returning.
  2465. */
  2466. static int lookup_open(struct nameidata *nd, struct path *path,
  2467. struct file *file,
  2468. const struct open_flags *op,
  2469. bool got_write, int *opened)
  2470. {
  2471. struct dentry *dir = nd->path.dentry;
  2472. struct inode *dir_inode = dir->d_inode;
  2473. struct dentry *dentry;
  2474. int error;
  2475. bool need_lookup;
  2476. *opened &= ~FILE_CREATED;
  2477. dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup);
  2478. if (IS_ERR(dentry))
  2479. return PTR_ERR(dentry);
  2480. /* Cached positive dentry: will open in f_op->open */
  2481. if (!need_lookup && dentry->d_inode)
  2482. goto out_no_open;
  2483. if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) {
  2484. return atomic_open(nd, dentry, path, file, op, got_write,
  2485. need_lookup, opened);
  2486. }
  2487. if (need_lookup) {
  2488. BUG_ON(dentry->d_inode);
  2489. dentry = lookup_real(dir_inode, dentry, nd->flags);
  2490. if (IS_ERR(dentry))
  2491. return PTR_ERR(dentry);
  2492. }
  2493. /* Negative dentry, just create the file */
  2494. if (!dentry->d_inode && (op->open_flag & O_CREAT)) {
  2495. umode_t mode = op->mode;
  2496. if (!IS_POSIXACL(dir->d_inode))
  2497. mode &= ~current_umask();
  2498. /*
  2499. * This write is needed to ensure that a
  2500. * rw->ro transition does not occur between
  2501. * the time when the file is created and when
  2502. * a permanent write count is taken through
  2503. * the 'struct file' in finish_open().
  2504. */
  2505. if (!got_write) {
  2506. error = -EROFS;
  2507. goto out_dput;
  2508. }
  2509. *opened |= FILE_CREATED;
  2510. error = security_path_mknod(&nd->path, dentry, mode, 0);
  2511. if (error)
  2512. goto out_dput;
  2513. error = vfs_create(dir->d_inode, dentry, mode,
  2514. nd->flags & LOOKUP_EXCL);
  2515. if (error)
  2516. goto out_dput;
  2517. }
  2518. out_no_open:
  2519. path->dentry = dentry;
  2520. path->mnt = nd->path.mnt;
  2521. return 1;
  2522. out_dput:
  2523. dput(dentry);
  2524. return error;
  2525. }
  2526. /*
  2527. * Handle the last step of open()
  2528. */
  2529. static int do_last(struct nameidata *nd, struct path *path,
  2530. struct file *file, const struct open_flags *op,
  2531. int *opened, struct filename *name)
  2532. {
  2533. struct dentry *dir = nd->path.dentry;
  2534. int open_flag = op->open_flag;
  2535. bool will_truncate = (open_flag & O_TRUNC) != 0;
  2536. bool got_write = false;
  2537. int acc_mode = op->acc_mode;
  2538. struct inode *inode;
  2539. bool symlink_ok = false;
  2540. struct path save_parent = { .dentry = NULL, .mnt = NULL };
  2541. bool retried = false;
  2542. int error;
  2543. nd->flags &= ~LOOKUP_PARENT;
  2544. nd->flags |= op->intent;
  2545. if (nd->last_type != LAST_NORM) {
  2546. error = handle_dots(nd, nd->last_type);
  2547. if (error)
  2548. return error;
  2549. goto finish_open;
  2550. }
  2551. if (!(open_flag & O_CREAT)) {
  2552. if (nd->last.name[nd->last.len])
  2553. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  2554. if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
  2555. symlink_ok = true;
  2556. /* we _can_ be in RCU mode here */
  2557. error = lookup_fast(nd, path, &inode);
  2558. if (likely(!error))
  2559. goto finish_lookup;
  2560. if (error < 0)
  2561. goto out;
  2562. BUG_ON(nd->inode != dir->d_inode);
  2563. } else {
  2564. /* create side of things */
  2565. /*
  2566. * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
  2567. * has been cleared when we got to the last component we are
  2568. * about to look up
  2569. */
  2570. error = complete_walk(nd);
  2571. if (error)
  2572. return error;
  2573. audit_inode(name, dir, LOOKUP_PARENT);
  2574. error = -EISDIR;
  2575. /* trailing slashes? */
  2576. if (nd->last.name[nd->last.len])
  2577. goto out;
  2578. }
  2579. retry_lookup:
  2580. if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
  2581. error = mnt_want_write(nd->path.mnt);
  2582. if (!error)
  2583. got_write = true;
  2584. /*
  2585. * do _not_ fail yet - we might not need that or fail with
  2586. * a different error; let lookup_open() decide; we'll be
  2587. * dropping this one anyway.
  2588. */
  2589. }
  2590. mutex_lock(&dir->d_inode->i_mutex);
  2591. error = lookup_open(nd, path, file, op, got_write, opened);
  2592. mutex_unlock(&dir->d_inode->i_mutex);
  2593. if (error <= 0) {
  2594. if (error)
  2595. goto out;
  2596. if ((*opened & FILE_CREATED) ||
  2597. !S_ISREG(file_inode(file)->i_mode))
  2598. will_truncate = false;
  2599. audit_inode(name, file->f_path.dentry, 0);
  2600. goto opened;
  2601. }
  2602. if (*opened & FILE_CREATED) {
  2603. /* Don't check for write permission, don't truncate */
  2604. open_flag &= ~O_TRUNC;
  2605. will_truncate = false;
  2606. acc_mode = MAY_OPEN;
  2607. path_to_nameidata(path, nd);
  2608. goto finish_open_created;
  2609. }
  2610. /*
  2611. * create/update audit record if it already exists.
  2612. */
  2613. if (d_is_positive(path->dentry))
  2614. audit_inode(name, path->dentry, 0);
  2615. /*
  2616. * If atomic_open() acquired write access it is dropped now due to
  2617. * possible mount and symlink following (this might be optimized away if
  2618. * necessary...)
  2619. */
  2620. if (got_write) {
  2621. mnt_drop_write(nd->path.mnt);
  2622. got_write = false;
  2623. }
  2624. error = -EEXIST;
  2625. if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))
  2626. goto exit_dput;
  2627. error = follow_managed(path, nd->flags);
  2628. if (error < 0)
  2629. goto exit_dput;
  2630. if (error)
  2631. nd->flags |= LOOKUP_JUMPED;
  2632. BUG_ON(nd->flags & LOOKUP_RCU);
  2633. inode = path->dentry->d_inode;
  2634. finish_lookup:
  2635. /* we _can_ be in RCU mode here */
  2636. error = -ENOENT;
  2637. if (d_is_negative(path->dentry)) {
  2638. path_to_nameidata(path, nd);
  2639. goto out;
  2640. }
  2641. if (should_follow_link(path->dentry, !symlink_ok)) {
  2642. if (nd->flags & LOOKUP_RCU) {
  2643. if (unlikely(unlazy_walk(nd, path->dentry))) {
  2644. error = -ECHILD;
  2645. goto out;
  2646. }
  2647. }
  2648. BUG_ON(inode != path->dentry->d_inode);
  2649. return 1;
  2650. }
  2651. if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) {
  2652. path_to_nameidata(path, nd);
  2653. } else {
  2654. save_parent.dentry = nd->path.dentry;
  2655. save_parent.mnt = mntget(path->mnt);
  2656. nd->path.dentry = path->dentry;
  2657. }
  2658. nd->inode = inode;
  2659. /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
  2660. finish_open:
  2661. error = complete_walk(nd);
  2662. if (error) {
  2663. path_put(&save_parent);
  2664. return error;
  2665. }
  2666. audit_inode(name, nd->path.dentry, 0);
  2667. error = -EISDIR;
  2668. if ((open_flag & O_CREAT) &&
  2669. (d_is_directory(nd->path.dentry) || d_is_autodir(nd->path.dentry)))
  2670. goto out;
  2671. error = -ENOTDIR;
  2672. if ((nd->flags & LOOKUP_DIRECTORY) && !d_is_directory(nd->path.dentry))
  2673. goto out;
  2674. if (!S_ISREG(nd->inode->i_mode))
  2675. will_truncate = false;
  2676. if (will_truncate) {
  2677. error = mnt_want_write(nd->path.mnt);
  2678. if (error)
  2679. goto out;
  2680. got_write = true;
  2681. }
  2682. finish_open_created:
  2683. error = may_open(&nd->path, acc_mode, open_flag);
  2684. if (error)
  2685. goto out;
  2686. file->f_path.mnt = nd->path.mnt;
  2687. error = finish_open(file, nd->path.dentry, NULL, opened);
  2688. if (error) {
  2689. if (error == -EOPENSTALE)
  2690. goto stale_open;
  2691. goto out;
  2692. }
  2693. opened:
  2694. error = open_check_o_direct(file);
  2695. if (error)
  2696. goto exit_fput;
  2697. error = ima_file_check(file, op->acc_mode);
  2698. if (error)
  2699. goto exit_fput;
  2700. if (will_truncate) {
  2701. error = handle_truncate(file);
  2702. if (error)
  2703. goto exit_fput;
  2704. }
  2705. out:
  2706. if (got_write)
  2707. mnt_drop_write(nd->path.mnt);
  2708. path_put(&save_parent);
  2709. terminate_walk(nd);
  2710. return error;
  2711. exit_dput:
  2712. path_put_conditional(path, nd);
  2713. goto out;
  2714. exit_fput:
  2715. fput(file);
  2716. goto out;
  2717. stale_open:
  2718. /* If no saved parent or already retried then can't retry */
  2719. if (!save_parent.dentry || retried)
  2720. goto out;
  2721. BUG_ON(save_parent.dentry != dir);
  2722. path_put(&nd->path);
  2723. nd->path = save_parent;
  2724. nd->inode = dir->d_inode;
  2725. save_parent.mnt = NULL;
  2726. save_parent.dentry = NULL;
  2727. if (got_write) {
  2728. mnt_drop_write(nd->path.mnt);
  2729. got_write = false;
  2730. }
  2731. retried = true;
  2732. goto retry_lookup;
  2733. }
  2734. static int do_tmpfile(int dfd, struct filename *pathname,
  2735. struct nameidata *nd, int flags,
  2736. const struct open_flags *op,
  2737. struct file *file, int *opened)
  2738. {
  2739. static const struct qstr name = QSTR_INIT("/", 1);
  2740. struct dentry *dentry, *child;
  2741. struct inode *dir;
  2742. int error = path_lookupat(dfd, pathname->name,
  2743. flags | LOOKUP_DIRECTORY, nd);
  2744. if (unlikely(error))
  2745. return error;
  2746. error = mnt_want_write(nd->path.mnt);
  2747. if (unlikely(error))
  2748. goto out;
  2749. /* we want directory to be writable */
  2750. error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC);
  2751. if (error)
  2752. goto out2;
  2753. dentry = nd->path.dentry;
  2754. dir = dentry->d_inode;
  2755. if (!dir->i_op->tmpfile) {
  2756. error = -EOPNOTSUPP;
  2757. goto out2;
  2758. }
  2759. child = d_alloc(dentry, &name);
  2760. if (unlikely(!child)) {
  2761. error = -ENOMEM;
  2762. goto out2;
  2763. }
  2764. nd->flags &= ~LOOKUP_DIRECTORY;
  2765. nd->flags |= op->intent;
  2766. dput(nd->path.dentry);
  2767. nd->path.dentry = child;
  2768. error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode);
  2769. if (error)
  2770. goto out2;
  2771. audit_inode(pathname, nd->path.dentry, 0);
  2772. error = may_open(&nd->path, op->acc_mode, op->open_flag);
  2773. if (error)
  2774. goto out2;
  2775. file->f_path.mnt = nd->path.mnt;
  2776. error = finish_open(file, nd->path.dentry, NULL, opened);
  2777. if (error)
  2778. goto out2;
  2779. error = open_check_o_direct(file);
  2780. if (error) {
  2781. fput(file);
  2782. } else if (!(op->open_flag & O_EXCL)) {
  2783. struct inode *inode = file_inode(file);
  2784. spin_lock(&inode->i_lock);
  2785. inode->i_state |= I_LINKABLE;
  2786. spin_unlock(&inode->i_lock);
  2787. }
  2788. out2:
  2789. mnt_drop_write(nd->path.mnt);
  2790. out:
  2791. path_put(&nd->path);
  2792. return error;
  2793. }
  2794. static struct file *path_openat(int dfd, struct filename *pathname,
  2795. struct nameidata *nd, const struct open_flags *op, int flags)
  2796. {
  2797. struct file *base = NULL;
  2798. struct file *file;
  2799. struct path path;
  2800. int opened = 0;
  2801. int error;
  2802. file = get_empty_filp();
  2803. if (IS_ERR(file))
  2804. return file;
  2805. file->f_flags = op->open_flag;
  2806. if (unlikely(file->f_flags & __O_TMPFILE)) {
  2807. error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened);
  2808. goto out;
  2809. }
  2810. error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base);
  2811. if (unlikely(error))
  2812. goto out;
  2813. current->total_link_count = 0;
  2814. error = link_path_walk(pathname->name, nd);
  2815. if (unlikely(error))
  2816. goto out;
  2817. error = do_last(nd, &path, file, op, &opened, pathname);
  2818. while (unlikely(error > 0)) { /* trailing symlink */
  2819. struct path link = path;
  2820. void *cookie;
  2821. if (!(nd->flags & LOOKUP_FOLLOW)) {
  2822. path_put_conditional(&path, nd);
  2823. path_put(&nd->path);
  2824. error = -ELOOP;
  2825. break;
  2826. }
  2827. error = may_follow_link(&link, nd);
  2828. if (unlikely(error))
  2829. break;
  2830. nd->flags |= LOOKUP_PARENT;
  2831. nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
  2832. error = follow_link(&link, nd, &cookie);
  2833. if (unlikely(error))
  2834. break;
  2835. error = do_last(nd, &path, file, op, &opened, pathname);
  2836. put_link(nd, &link, cookie);
  2837. }
  2838. out:
  2839. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
  2840. path_put(&nd->root);
  2841. if (base)
  2842. fput(base);
  2843. if (!(opened & FILE_OPENED)) {
  2844. BUG_ON(!error);
  2845. put_filp(file);
  2846. }
  2847. if (unlikely(error)) {
  2848. if (error == -EOPENSTALE) {
  2849. if (flags & LOOKUP_RCU)
  2850. error = -ECHILD;
  2851. else
  2852. error = -ESTALE;
  2853. }
  2854. file = ERR_PTR(error);
  2855. }
  2856. return file;
  2857. }
  2858. struct file *do_filp_open(int dfd, struct filename *pathname,
  2859. const struct open_flags *op)
  2860. {
  2861. struct nameidata nd;
  2862. int flags = op->lookup_flags;
  2863. struct file *filp;
  2864. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
  2865. if (unlikely(filp == ERR_PTR(-ECHILD)))
  2866. filp = path_openat(dfd, pathname, &nd, op, flags);
  2867. if (unlikely(filp == ERR_PTR(-ESTALE)))
  2868. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
  2869. return filp;
  2870. }
  2871. struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  2872. const char *name, const struct open_flags *op)
  2873. {
  2874. struct nameidata nd;
  2875. struct file *file;
  2876. struct filename filename = { .name = name };
  2877. int flags = op->lookup_flags | LOOKUP_ROOT;
  2878. nd.root.mnt = mnt;
  2879. nd.root.dentry = dentry;
  2880. if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
  2881. return ERR_PTR(-ELOOP);
  2882. file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU);
  2883. if (unlikely(file == ERR_PTR(-ECHILD)))
  2884. file = path_openat(-1, &filename, &nd, op, flags);
  2885. if (unlikely(file == ERR_PTR(-ESTALE)))
  2886. file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL);
  2887. return file;
  2888. }
  2889. struct dentry *kern_path_create(int dfd, const char *pathname,
  2890. struct path *path, unsigned int lookup_flags)
  2891. {
  2892. struct dentry *dentry = ERR_PTR(-EEXIST);
  2893. struct nameidata nd;
  2894. int err2;
  2895. int error;
  2896. bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
  2897. /*
  2898. * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
  2899. * other flags passed in are ignored!
  2900. */
  2901. lookup_flags &= LOOKUP_REVAL;
  2902. error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd);
  2903. if (error)
  2904. return ERR_PTR(error);
  2905. /*
  2906. * Yucky last component or no last component at all?
  2907. * (foo/., foo/.., /////)
  2908. */
  2909. if (nd.last_type != LAST_NORM)
  2910. goto out;
  2911. nd.flags &= ~LOOKUP_PARENT;
  2912. nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  2913. /* don't fail immediately if it's r/o, at least try to report other errors */
  2914. err2 = mnt_want_write(nd.path.mnt);
  2915. /*
  2916. * Do the final lookup.
  2917. */
  2918. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2919. dentry = lookup_hash(&nd);
  2920. if (IS_ERR(dentry))
  2921. goto unlock;
  2922. error = -EEXIST;
  2923. if (d_is_positive(dentry))
  2924. goto fail;
  2925. /*
  2926. * Special case - lookup gave negative, but... we had foo/bar/
  2927. * From the vfs_mknod() POV we just have a negative dentry -
  2928. * all is fine. Let's be bastards - you had / on the end, you've
  2929. * been asking for (non-existent) directory. -ENOENT for you.
  2930. */
  2931. if (unlikely(!is_dir && nd.last.name[nd.last.len])) {
  2932. error = -ENOENT;
  2933. goto fail;
  2934. }
  2935. if (unlikely(err2)) {
  2936. error = err2;
  2937. goto fail;
  2938. }
  2939. *path = nd.path;
  2940. return dentry;
  2941. fail:
  2942. dput(dentry);
  2943. dentry = ERR_PTR(error);
  2944. unlock:
  2945. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2946. if (!err2)
  2947. mnt_drop_write(nd.path.mnt);
  2948. out:
  2949. path_put(&nd.path);
  2950. return dentry;
  2951. }
  2952. EXPORT_SYMBOL(kern_path_create);
  2953. void done_path_create(struct path *path, struct dentry *dentry)
  2954. {
  2955. dput(dentry);
  2956. mutex_unlock(&path->dentry->d_inode->i_mutex);
  2957. mnt_drop_write(path->mnt);
  2958. path_put(path);
  2959. }
  2960. EXPORT_SYMBOL(done_path_create);
  2961. struct dentry *user_path_create(int dfd, const char __user *pathname,
  2962. struct path *path, unsigned int lookup_flags)
  2963. {
  2964. struct filename *tmp = getname(pathname);
  2965. struct dentry *res;
  2966. if (IS_ERR(tmp))
  2967. return ERR_CAST(tmp);
  2968. res = kern_path_create(dfd, tmp->name, path, lookup_flags);
  2969. putname(tmp);
  2970. return res;
  2971. }
  2972. EXPORT_SYMBOL(user_path_create);
  2973. int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  2974. {
  2975. int error = may_create(dir, dentry);
  2976. if (error)
  2977. return error;
  2978. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  2979. return -EPERM;
  2980. if (!dir->i_op->mknod)
  2981. return -EPERM;
  2982. error = devcgroup_inode_mknod(mode, dev);
  2983. if (error)
  2984. return error;
  2985. error = security_inode_mknod(dir, dentry, mode, dev);
  2986. if (error)
  2987. return error;
  2988. error = dir->i_op->mknod(dir, dentry, mode, dev);
  2989. if (!error)
  2990. fsnotify_create(dir, dentry);
  2991. return error;
  2992. }
  2993. static int may_mknod(umode_t mode)
  2994. {
  2995. switch (mode & S_IFMT) {
  2996. case S_IFREG:
  2997. case S_IFCHR:
  2998. case S_IFBLK:
  2999. case S_IFIFO:
  3000. case S_IFSOCK:
  3001. case 0: /* zero mode translates to S_IFREG */
  3002. return 0;
  3003. case S_IFDIR:
  3004. return -EPERM;
  3005. default:
  3006. return -EINVAL;
  3007. }
  3008. }
  3009. SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
  3010. unsigned, dev)
  3011. {
  3012. struct dentry *dentry;
  3013. struct path path;
  3014. int error;
  3015. unsigned int lookup_flags = 0;
  3016. error = may_mknod(mode);
  3017. if (error)
  3018. return error;
  3019. retry:
  3020. dentry = user_path_create(dfd, filename, &path, lookup_flags);
  3021. if (IS_ERR(dentry))
  3022. return PTR_ERR(dentry);
  3023. if (!IS_POSIXACL(path.dentry->d_inode))
  3024. mode &= ~current_umask();
  3025. error = security_path_mknod(&path, dentry, mode, dev);
  3026. if (error)
  3027. goto out;
  3028. switch (mode & S_IFMT) {
  3029. case 0: case S_IFREG:
  3030. error = vfs_create(path.dentry->d_inode,dentry,mode,true);
  3031. break;
  3032. case S_IFCHR: case S_IFBLK:
  3033. error = vfs_mknod(path.dentry->d_inode,dentry,mode,
  3034. new_decode_dev(dev));
  3035. break;
  3036. case S_IFIFO: case S_IFSOCK:
  3037. error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
  3038. break;
  3039. }
  3040. out:
  3041. done_path_create(&path, dentry);
  3042. if (retry_estale(error, lookup_flags)) {
  3043. lookup_flags |= LOOKUP_REVAL;
  3044. goto retry;
  3045. }
  3046. return error;
  3047. }
  3048. SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
  3049. {
  3050. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  3051. }
  3052. int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  3053. {
  3054. int error = may_create(dir, dentry);
  3055. unsigned max_links = dir->i_sb->s_max_links;
  3056. if (error)
  3057. return error;
  3058. if (!dir->i_op->mkdir)
  3059. return -EPERM;
  3060. mode &= (S_IRWXUGO|S_ISVTX);
  3061. error = security_inode_mkdir(dir, dentry, mode);
  3062. if (error)
  3063. return error;
  3064. if (max_links && dir->i_nlink >= max_links)
  3065. return -EMLINK;
  3066. error = dir->i_op->mkdir(dir, dentry, mode);
  3067. if (!error)
  3068. fsnotify_mkdir(dir, dentry);
  3069. return error;
  3070. }
  3071. SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
  3072. {
  3073. struct dentry *dentry;
  3074. struct path path;
  3075. int error;
  3076. unsigned int lookup_flags = LOOKUP_DIRECTORY;
  3077. retry:
  3078. dentry = user_path_create(dfd, pathname, &path, lookup_flags);
  3079. if (IS_ERR(dentry))
  3080. return PTR_ERR(dentry);
  3081. if (!IS_POSIXACL(path.dentry->d_inode))
  3082. mode &= ~current_umask();
  3083. error = security_path_mkdir(&path, dentry, mode);
  3084. if (!error)
  3085. error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
  3086. done_path_create(&path, dentry);
  3087. if (retry_estale(error, lookup_flags)) {
  3088. lookup_flags |= LOOKUP_REVAL;
  3089. goto retry;
  3090. }
  3091. return error;
  3092. }
  3093. SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
  3094. {
  3095. return sys_mkdirat(AT_FDCWD, pathname, mode);
  3096. }
  3097. /*
  3098. * The dentry_unhash() helper will try to drop the dentry early: we
  3099. * should have a usage count of 1 if we're the only user of this
  3100. * dentry, and if that is true (possibly after pruning the dcache),
  3101. * then we drop the dentry now.
  3102. *
  3103. * A low-level filesystem can, if it choses, legally
  3104. * do a
  3105. *
  3106. * if (!d_unhashed(dentry))
  3107. * return -EBUSY;
  3108. *
  3109. * if it cannot handle the case of removing a directory
  3110. * that is still in use by something else..
  3111. */
  3112. void dentry_unhash(struct dentry *dentry)
  3113. {
  3114. shrink_dcache_parent(dentry);
  3115. spin_lock(&dentry->d_lock);
  3116. if (dentry->d_lockref.count == 1)
  3117. __d_drop(dentry);
  3118. spin_unlock(&dentry->d_lock);
  3119. }
  3120. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  3121. {
  3122. int error = may_delete(dir, dentry, 1);
  3123. if (error)
  3124. return error;
  3125. if (!dir->i_op->rmdir)
  3126. return -EPERM;
  3127. dget(dentry);
  3128. mutex_lock(&dentry->d_inode->i_mutex);
  3129. error = -EBUSY;
  3130. if (d_mountpoint(dentry))
  3131. goto out;
  3132. error = security_inode_rmdir(dir, dentry);
  3133. if (error)
  3134. goto out;
  3135. shrink_dcache_parent(dentry);
  3136. error = dir->i_op->rmdir(dir, dentry);
  3137. if (error)
  3138. goto out;
  3139. dentry->d_inode->i_flags |= S_DEAD;
  3140. dont_mount(dentry);
  3141. out:
  3142. mutex_unlock(&dentry->d_inode->i_mutex);
  3143. dput(dentry);
  3144. if (!error)
  3145. d_delete(dentry);
  3146. return error;
  3147. }
  3148. static long do_rmdir(int dfd, const char __user *pathname)
  3149. {
  3150. int error = 0;
  3151. struct filename *name;
  3152. struct dentry *dentry;
  3153. struct nameidata nd;
  3154. unsigned int lookup_flags = 0;
  3155. retry:
  3156. name = user_path_parent(dfd, pathname, &nd, lookup_flags);
  3157. if (IS_ERR(name))
  3158. return PTR_ERR(name);
  3159. switch(nd.last_type) {
  3160. case LAST_DOTDOT:
  3161. error = -ENOTEMPTY;
  3162. goto exit1;
  3163. case LAST_DOT:
  3164. error = -EINVAL;
  3165. goto exit1;
  3166. case LAST_ROOT:
  3167. error = -EBUSY;
  3168. goto exit1;
  3169. }
  3170. nd.flags &= ~LOOKUP_PARENT;
  3171. error = mnt_want_write(nd.path.mnt);
  3172. if (error)
  3173. goto exit1;
  3174. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  3175. dentry = lookup_hash(&nd);
  3176. error = PTR_ERR(dentry);
  3177. if (IS_ERR(dentry))
  3178. goto exit2;
  3179. if (!dentry->d_inode) {
  3180. error = -ENOENT;
  3181. goto exit3;
  3182. }
  3183. error = security_path_rmdir(&nd.path, dentry);
  3184. if (error)
  3185. goto exit3;
  3186. error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
  3187. exit3:
  3188. dput(dentry);
  3189. exit2:
  3190. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  3191. mnt_drop_write(nd.path.mnt);
  3192. exit1:
  3193. path_put(&nd.path);
  3194. putname(name);
  3195. if (retry_estale(error, lookup_flags)) {
  3196. lookup_flags |= LOOKUP_REVAL;
  3197. goto retry;
  3198. }
  3199. return error;
  3200. }
  3201. SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
  3202. {
  3203. return do_rmdir(AT_FDCWD, pathname);
  3204. }
  3205. /**
  3206. * vfs_unlink - unlink a filesystem object
  3207. * @dir: parent directory
  3208. * @dentry: victim
  3209. * @delegated_inode: returns victim inode, if the inode is delegated.
  3210. *
  3211. * The caller must hold dir->i_mutex.
  3212. *
  3213. * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
  3214. * return a reference to the inode in delegated_inode. The caller
  3215. * should then break the delegation on that inode and retry. Because
  3216. * breaking a delegation may take a long time, the caller should drop
  3217. * dir->i_mutex before doing so.
  3218. *
  3219. * Alternatively, a caller may pass NULL for delegated_inode. This may
  3220. * be appropriate for callers that expect the underlying filesystem not
  3221. * to be NFS exported.
  3222. */
  3223. int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
  3224. {
  3225. struct inode *target = dentry->d_inode;
  3226. int error = may_delete(dir, dentry, 0);
  3227. if (error)
  3228. return error;
  3229. if (!dir->i_op->unlink)
  3230. return -EPERM;
  3231. mutex_lock(&target->i_mutex);
  3232. if (d_mountpoint(dentry))
  3233. error = -EBUSY;
  3234. else {
  3235. error = security_inode_unlink(dir, dentry);
  3236. if (!error) {
  3237. error = try_break_deleg(target, delegated_inode);
  3238. if (error)
  3239. goto out;
  3240. error = dir->i_op->unlink(dir, dentry);
  3241. if (!error)
  3242. dont_mount(dentry);
  3243. }
  3244. }
  3245. out:
  3246. mutex_unlock(&target->i_mutex);
  3247. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  3248. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  3249. fsnotify_link_count(target);
  3250. d_delete(dentry);
  3251. }
  3252. return error;
  3253. }
  3254. /*
  3255. * Make sure that the actual truncation of the file will occur outside its
  3256. * directory's i_mutex. Truncate can take a long time if there is a lot of
  3257. * writeout happening, and we don't want to prevent access to the directory
  3258. * while waiting on the I/O.
  3259. */
  3260. static long do_unlinkat(int dfd, const char __user *pathname)
  3261. {
  3262. int error;
  3263. struct filename *name;
  3264. struct dentry *dentry;
  3265. struct nameidata nd;
  3266. struct inode *inode = NULL;
  3267. struct inode *delegated_inode = NULL;
  3268. unsigned int lookup_flags = 0;
  3269. retry:
  3270. name = user_path_parent(dfd, pathname, &nd, lookup_flags);
  3271. if (IS_ERR(name))
  3272. return PTR_ERR(name);
  3273. error = -EISDIR;
  3274. if (nd.last_type != LAST_NORM)
  3275. goto exit1;
  3276. nd.flags &= ~LOOKUP_PARENT;
  3277. error = mnt_want_write(nd.path.mnt);
  3278. if (error)
  3279. goto exit1;
  3280. retry_deleg:
  3281. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  3282. dentry = lookup_hash(&nd);
  3283. error = PTR_ERR(dentry);
  3284. if (!IS_ERR(dentry)) {
  3285. /* Why not before? Because we want correct error value */
  3286. if (nd.last.name[nd.last.len])
  3287. goto slashes;
  3288. inode = dentry->d_inode;
  3289. if (d_is_negative(dentry))
  3290. goto slashes;
  3291. ihold(inode);
  3292. error = security_path_unlink(&nd.path, dentry);
  3293. if (error)
  3294. goto exit2;
  3295. error = vfs_unlink(nd.path.dentry->d_inode, dentry, &delegated_inode);
  3296. exit2:
  3297. dput(dentry);
  3298. }
  3299. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  3300. if (inode)
  3301. iput(inode); /* truncate the inode here */
  3302. inode = NULL;
  3303. if (delegated_inode) {
  3304. error = break_deleg_wait(&delegated_inode);
  3305. if (!error)
  3306. goto retry_deleg;
  3307. }
  3308. mnt_drop_write(nd.path.mnt);
  3309. exit1:
  3310. path_put(&nd.path);
  3311. putname(name);
  3312. if (retry_estale(error, lookup_flags)) {
  3313. lookup_flags |= LOOKUP_REVAL;
  3314. inode = NULL;
  3315. goto retry;
  3316. }
  3317. return error;
  3318. slashes:
  3319. if (d_is_negative(dentry))
  3320. error = -ENOENT;
  3321. else if (d_is_directory(dentry) || d_is_autodir(dentry))
  3322. error = -EISDIR;
  3323. else
  3324. error = -ENOTDIR;
  3325. goto exit2;
  3326. }
  3327. SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
  3328. {
  3329. if ((flag & ~AT_REMOVEDIR) != 0)
  3330. return -EINVAL;
  3331. if (flag & AT_REMOVEDIR)
  3332. return do_rmdir(dfd, pathname);
  3333. return do_unlinkat(dfd, pathname);
  3334. }
  3335. SYSCALL_DEFINE1(unlink, const char __user *, pathname)
  3336. {
  3337. return do_unlinkat(AT_FDCWD, pathname);
  3338. }
  3339. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  3340. {
  3341. int error = may_create(dir, dentry);
  3342. if (error)
  3343. return error;
  3344. if (!dir->i_op->symlink)
  3345. return -EPERM;
  3346. error = security_inode_symlink(dir, dentry, oldname);
  3347. if (error)
  3348. return error;
  3349. error = dir->i_op->symlink(dir, dentry, oldname);
  3350. if (!error)
  3351. fsnotify_create(dir, dentry);
  3352. return error;
  3353. }
  3354. SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  3355. int, newdfd, const char __user *, newname)
  3356. {
  3357. int error;
  3358. struct filename *from;
  3359. struct dentry *dentry;
  3360. struct path path;
  3361. unsigned int lookup_flags = 0;
  3362. from = getname(oldname);
  3363. if (IS_ERR(from))
  3364. return PTR_ERR(from);
  3365. retry:
  3366. dentry = user_path_create(newdfd, newname, &path, lookup_flags);
  3367. error = PTR_ERR(dentry);
  3368. if (IS_ERR(dentry))
  3369. goto out_putname;
  3370. error = security_path_symlink(&path, dentry, from->name);
  3371. if (!error)
  3372. error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
  3373. done_path_create(&path, dentry);
  3374. if (retry_estale(error, lookup_flags)) {
  3375. lookup_flags |= LOOKUP_REVAL;
  3376. goto retry;
  3377. }
  3378. out_putname:
  3379. putname(from);
  3380. return error;
  3381. }
  3382. SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
  3383. {
  3384. return sys_symlinkat(oldname, AT_FDCWD, newname);
  3385. }
  3386. /**
  3387. * vfs_link - create a new link
  3388. * @old_dentry: object to be linked
  3389. * @dir: new parent
  3390. * @new_dentry: where to create the new link
  3391. * @delegated_inode: returns inode needing a delegation break
  3392. *
  3393. * The caller must hold dir->i_mutex
  3394. *
  3395. * If vfs_link discovers a delegation on the to-be-linked file in need
  3396. * of breaking, it will return -EWOULDBLOCK and return a reference to the
  3397. * inode in delegated_inode. The caller should then break the delegation
  3398. * and retry. Because breaking a delegation may take a long time, the
  3399. * caller should drop the i_mutex before doing so.
  3400. *
  3401. * Alternatively, a caller may pass NULL for delegated_inode. This may
  3402. * be appropriate for callers that expect the underlying filesystem not
  3403. * to be NFS exported.
  3404. */
  3405. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
  3406. {
  3407. struct inode *inode = old_dentry->d_inode;
  3408. unsigned max_links = dir->i_sb->s_max_links;
  3409. int error;
  3410. if (!inode)
  3411. return -ENOENT;
  3412. error = may_create(dir, new_dentry);
  3413. if (error)
  3414. return error;
  3415. if (dir->i_sb != inode->i_sb)
  3416. return -EXDEV;
  3417. /*
  3418. * A link to an append-only or immutable file cannot be created.
  3419. */
  3420. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  3421. return -EPERM;
  3422. if (!dir->i_op->link)
  3423. return -EPERM;
  3424. if (S_ISDIR(inode->i_mode))
  3425. return -EPERM;
  3426. error = security_inode_link(old_dentry, dir, new_dentry);
  3427. if (error)
  3428. return error;
  3429. mutex_lock(&inode->i_mutex);
  3430. /* Make sure we don't allow creating hardlink to an unlinked file */
  3431. if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
  3432. error = -ENOENT;
  3433. else if (max_links && inode->i_nlink >= max_links)
  3434. error = -EMLINK;
  3435. else {
  3436. error = try_break_deleg(inode, delegated_inode);
  3437. if (!error)
  3438. error = dir->i_op->link(old_dentry, dir, new_dentry);
  3439. }
  3440. if (!error && (inode->i_state & I_LINKABLE)) {
  3441. spin_lock(&inode->i_lock);
  3442. inode->i_state &= ~I_LINKABLE;
  3443. spin_unlock(&inode->i_lock);
  3444. }
  3445. mutex_unlock(&inode->i_mutex);
  3446. if (!error)
  3447. fsnotify_link(dir, inode, new_dentry);
  3448. return error;
  3449. }
  3450. /*
  3451. * Hardlinks are often used in delicate situations. We avoid
  3452. * security-related surprises by not following symlinks on the
  3453. * newname. --KAB
  3454. *
  3455. * We don't follow them on the oldname either to be compatible
  3456. * with linux 2.0, and to avoid hard-linking to directories
  3457. * and other special files. --ADM
  3458. */
  3459. SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  3460. int, newdfd, const char __user *, newname, int, flags)
  3461. {
  3462. struct dentry *new_dentry;
  3463. struct path old_path, new_path;
  3464. struct inode *delegated_inode = NULL;
  3465. int how = 0;
  3466. int error;
  3467. if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  3468. return -EINVAL;
  3469. /*
  3470. * To use null names we require CAP_DAC_READ_SEARCH
  3471. * This ensures that not everyone will be able to create
  3472. * handlink using the passed filedescriptor.
  3473. */
  3474. if (flags & AT_EMPTY_PATH) {
  3475. if (!capable(CAP_DAC_READ_SEARCH))
  3476. return -ENOENT;
  3477. how = LOOKUP_EMPTY;
  3478. }
  3479. if (flags & AT_SYMLINK_FOLLOW)
  3480. how |= LOOKUP_FOLLOW;
  3481. retry:
  3482. error = user_path_at(olddfd, oldname, how, &old_path);
  3483. if (error)
  3484. return error;
  3485. new_dentry = user_path_create(newdfd, newname, &new_path,
  3486. (how & LOOKUP_REVAL));
  3487. error = PTR_ERR(new_dentry);
  3488. if (IS_ERR(new_dentry))
  3489. goto out;
  3490. error = -EXDEV;
  3491. if (old_path.mnt != new_path.mnt)
  3492. goto out_dput;
  3493. error = may_linkat(&old_path);
  3494. if (unlikely(error))
  3495. goto out_dput;
  3496. error = security_path_link(old_path.dentry, &new_path, new_dentry);
  3497. if (error)
  3498. goto out_dput;
  3499. error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
  3500. out_dput:
  3501. done_path_create(&new_path, new_dentry);
  3502. if (delegated_inode) {
  3503. error = break_deleg_wait(&delegated_inode);
  3504. if (!error) {
  3505. path_put(&old_path);
  3506. goto retry;
  3507. }
  3508. }
  3509. if (retry_estale(error, how)) {
  3510. path_put(&old_path);
  3511. how |= LOOKUP_REVAL;
  3512. goto retry;
  3513. }
  3514. out:
  3515. path_put(&old_path);
  3516. return error;
  3517. }
  3518. SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
  3519. {
  3520. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  3521. }
  3522. /*
  3523. * The worst of all namespace operations - renaming directory. "Perverted"
  3524. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  3525. * Problems:
  3526. * a) we can get into loop creation. Check is done in is_subdir().
  3527. * b) race potential - two innocent renames can create a loop together.
  3528. * That's where 4.4 screws up. Current fix: serialization on
  3529. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  3530. * story.
  3531. * c) we have to lock _four_ objects - parents and victim (if it exists),
  3532. * and source (if it is not a directory).
  3533. * And that - after we got ->i_mutex on parents (until then we don't know
  3534. * whether the target exists). Solution: try to be smart with locking
  3535. * order for inodes. We rely on the fact that tree topology may change
  3536. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  3537. * move will be locked. Thus we can rank directories by the tree
  3538. * (ancestors first) and rank all non-directories after them.
  3539. * That works since everybody except rename does "lock parent, lookup,
  3540. * lock child" and rename is under ->s_vfs_rename_mutex.
  3541. * HOWEVER, it relies on the assumption that any object with ->lookup()
  3542. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  3543. * we'd better make sure that there's no link(2) for them.
  3544. * d) conversion from fhandle to dentry may come in the wrong moment - when
  3545. * we are removing the target. Solution: we will have to grab ->i_mutex
  3546. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  3547. * ->i_mutex on parents, which works but leads to some truly excessive
  3548. * locking].
  3549. */
  3550. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  3551. struct inode *new_dir, struct dentry *new_dentry)
  3552. {
  3553. int error = 0;
  3554. struct inode *target = new_dentry->d_inode;
  3555. unsigned max_links = new_dir->i_sb->s_max_links;
  3556. /*
  3557. * If we are going to change the parent - check write permissions,
  3558. * we'll need to flip '..'.
  3559. */
  3560. if (new_dir != old_dir) {
  3561. error = inode_permission(old_dentry->d_inode, MAY_WRITE);
  3562. if (error)
  3563. return error;
  3564. }
  3565. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  3566. if (error)
  3567. return error;
  3568. dget(new_dentry);
  3569. if (target)
  3570. mutex_lock(&target->i_mutex);
  3571. error = -EBUSY;
  3572. if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry))
  3573. goto out;
  3574. error = -EMLINK;
  3575. if (max_links && !target && new_dir != old_dir &&
  3576. new_dir->i_nlink >= max_links)
  3577. goto out;
  3578. if (target)
  3579. shrink_dcache_parent(new_dentry);
  3580. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  3581. if (error)
  3582. goto out;
  3583. if (target) {
  3584. target->i_flags |= S_DEAD;
  3585. dont_mount(new_dentry);
  3586. }
  3587. out:
  3588. if (target)
  3589. mutex_unlock(&target->i_mutex);
  3590. dput(new_dentry);
  3591. if (!error)
  3592. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  3593. d_move(old_dentry,new_dentry);
  3594. return error;
  3595. }
  3596. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  3597. struct inode *new_dir, struct dentry *new_dentry,
  3598. struct inode **delegated_inode)
  3599. {
  3600. struct inode *target = new_dentry->d_inode;
  3601. struct inode *source = old_dentry->d_inode;
  3602. int error;
  3603. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  3604. if (error)
  3605. return error;
  3606. dget(new_dentry);
  3607. lock_two_nondirectories(source, target);
  3608. error = -EBUSY;
  3609. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  3610. goto out;
  3611. error = try_break_deleg(source, delegated_inode);
  3612. if (error)
  3613. goto out;
  3614. if (target) {
  3615. error = try_break_deleg(target, delegated_inode);
  3616. if (error)
  3617. goto out;
  3618. }
  3619. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  3620. if (error)
  3621. goto out;
  3622. if (target)
  3623. dont_mount(new_dentry);
  3624. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  3625. d_move(old_dentry, new_dentry);
  3626. out:
  3627. unlock_two_nondirectories(source, target);
  3628. dput(new_dentry);
  3629. return error;
  3630. }
  3631. /**
  3632. * vfs_rename - rename a filesystem object
  3633. * @old_dir: parent of source
  3634. * @old_dentry: source
  3635. * @new_dir: parent of destination
  3636. * @new_dentry: destination
  3637. * @delegated_inode: returns an inode needing a delegation break
  3638. *
  3639. * The caller must hold multiple mutexes--see lock_rename()).
  3640. *
  3641. * If vfs_rename discovers a delegation in need of breaking at either
  3642. * the source or destination, it will return -EWOULDBLOCK and return a
  3643. * reference to the inode in delegated_inode. The caller should then
  3644. * break the delegation and retry. Because breaking a delegation may
  3645. * take a long time, the caller should drop all locks before doing
  3646. * so.
  3647. *
  3648. * Alternatively, a caller may pass NULL for delegated_inode. This may
  3649. * be appropriate for callers that expect the underlying filesystem not
  3650. * to be NFS exported.
  3651. */
  3652. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  3653. struct inode *new_dir, struct dentry *new_dentry,
  3654. struct inode **delegated_inode)
  3655. {
  3656. int error;
  3657. int is_dir = d_is_directory(old_dentry) || d_is_autodir(old_dentry);
  3658. const unsigned char *old_name;
  3659. if (old_dentry->d_inode == new_dentry->d_inode)
  3660. return 0;
  3661. error = may_delete(old_dir, old_dentry, is_dir);
  3662. if (error)
  3663. return error;
  3664. if (!new_dentry->d_inode)
  3665. error = may_create(new_dir, new_dentry);
  3666. else
  3667. error = may_delete(new_dir, new_dentry, is_dir);
  3668. if (error)
  3669. return error;
  3670. if (!old_dir->i_op->rename)
  3671. return -EPERM;
  3672. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  3673. if (is_dir)
  3674. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  3675. else
  3676. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry,delegated_inode);
  3677. if (!error)
  3678. fsnotify_move(old_dir, new_dir, old_name, is_dir,
  3679. new_dentry->d_inode, old_dentry);
  3680. fsnotify_oldname_free(old_name);
  3681. return error;
  3682. }
  3683. SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  3684. int, newdfd, const char __user *, newname)
  3685. {
  3686. struct dentry *old_dir, *new_dir;
  3687. struct dentry *old_dentry, *new_dentry;
  3688. struct dentry *trap;
  3689. struct nameidata oldnd, newnd;
  3690. struct inode *delegated_inode = NULL;
  3691. struct filename *from;
  3692. struct filename *to;
  3693. unsigned int lookup_flags = 0;
  3694. bool should_retry = false;
  3695. int error;
  3696. retry:
  3697. from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags);
  3698. if (IS_ERR(from)) {
  3699. error = PTR_ERR(from);
  3700. goto exit;
  3701. }
  3702. to = user_path_parent(newdfd, newname, &newnd, lookup_flags);
  3703. if (IS_ERR(to)) {
  3704. error = PTR_ERR(to);
  3705. goto exit1;
  3706. }
  3707. error = -EXDEV;
  3708. if (oldnd.path.mnt != newnd.path.mnt)
  3709. goto exit2;
  3710. old_dir = oldnd.path.dentry;
  3711. error = -EBUSY;
  3712. if (oldnd.last_type != LAST_NORM)
  3713. goto exit2;
  3714. new_dir = newnd.path.dentry;
  3715. if (newnd.last_type != LAST_NORM)
  3716. goto exit2;
  3717. error = mnt_want_write(oldnd.path.mnt);
  3718. if (error)
  3719. goto exit2;
  3720. oldnd.flags &= ~LOOKUP_PARENT;
  3721. newnd.flags &= ~LOOKUP_PARENT;
  3722. newnd.flags |= LOOKUP_RENAME_TARGET;
  3723. retry_deleg:
  3724. trap = lock_rename(new_dir, old_dir);
  3725. old_dentry = lookup_hash(&oldnd);
  3726. error = PTR_ERR(old_dentry);
  3727. if (IS_ERR(old_dentry))
  3728. goto exit3;
  3729. /* source must exist */
  3730. error = -ENOENT;
  3731. if (d_is_negative(old_dentry))
  3732. goto exit4;
  3733. /* unless the source is a directory trailing slashes give -ENOTDIR */
  3734. if (!d_is_directory(old_dentry) && !d_is_autodir(old_dentry)) {
  3735. error = -ENOTDIR;
  3736. if (oldnd.last.name[oldnd.last.len])
  3737. goto exit4;
  3738. if (newnd.last.name[newnd.last.len])
  3739. goto exit4;
  3740. }
  3741. /* source should not be ancestor of target */
  3742. error = -EINVAL;
  3743. if (old_dentry == trap)
  3744. goto exit4;
  3745. new_dentry = lookup_hash(&newnd);
  3746. error = PTR_ERR(new_dentry);
  3747. if (IS_ERR(new_dentry))
  3748. goto exit4;
  3749. /* target should not be an ancestor of source */
  3750. error = -ENOTEMPTY;
  3751. if (new_dentry == trap)
  3752. goto exit5;
  3753. error = security_path_rename(&oldnd.path, old_dentry,
  3754. &newnd.path, new_dentry);
  3755. if (error)
  3756. goto exit5;
  3757. error = vfs_rename(old_dir->d_inode, old_dentry,
  3758. new_dir->d_inode, new_dentry,
  3759. &delegated_inode);
  3760. exit5:
  3761. dput(new_dentry);
  3762. exit4:
  3763. dput(old_dentry);
  3764. exit3:
  3765. unlock_rename(new_dir, old_dir);
  3766. if (delegated_inode) {
  3767. error = break_deleg_wait(&delegated_inode);
  3768. if (!error)
  3769. goto retry_deleg;
  3770. }
  3771. mnt_drop_write(oldnd.path.mnt);
  3772. exit2:
  3773. if (retry_estale(error, lookup_flags))
  3774. should_retry = true;
  3775. path_put(&newnd.path);
  3776. putname(to);
  3777. exit1:
  3778. path_put(&oldnd.path);
  3779. putname(from);
  3780. if (should_retry) {
  3781. should_retry = false;
  3782. lookup_flags |= LOOKUP_REVAL;
  3783. goto retry;
  3784. }
  3785. exit:
  3786. return error;
  3787. }
  3788. SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
  3789. {
  3790. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  3791. }
  3792. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  3793. {
  3794. int len;
  3795. len = PTR_ERR(link);
  3796. if (IS_ERR(link))
  3797. goto out;
  3798. len = strlen(link);
  3799. if (len > (unsigned) buflen)
  3800. len = buflen;
  3801. if (copy_to_user(buffer, link, len))
  3802. len = -EFAULT;
  3803. out:
  3804. return len;
  3805. }
  3806. /*
  3807. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  3808. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  3809. * using) it for any given inode is up to filesystem.
  3810. */
  3811. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  3812. {
  3813. struct nameidata nd;
  3814. void *cookie;
  3815. int res;
  3816. nd.depth = 0;
  3817. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  3818. if (IS_ERR(cookie))
  3819. return PTR_ERR(cookie);
  3820. res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  3821. if (dentry->d_inode->i_op->put_link)
  3822. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  3823. return res;
  3824. }
  3825. /* get the link contents into pagecache */
  3826. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  3827. {
  3828. char *kaddr;
  3829. struct page *page;
  3830. struct address_space *mapping = dentry->d_inode->i_mapping;
  3831. page = read_mapping_page(mapping, 0, NULL);
  3832. if (IS_ERR(page))
  3833. return (char*)page;
  3834. *ppage = page;
  3835. kaddr = kmap(page);
  3836. nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
  3837. return kaddr;
  3838. }
  3839. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  3840. {
  3841. struct page *page = NULL;
  3842. char *s = page_getlink(dentry, &page);
  3843. int res = vfs_readlink(dentry,buffer,buflen,s);
  3844. if (page) {
  3845. kunmap(page);
  3846. page_cache_release(page);
  3847. }
  3848. return res;
  3849. }
  3850. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  3851. {
  3852. struct page *page = NULL;
  3853. nd_set_link(nd, page_getlink(dentry, &page));
  3854. return page;
  3855. }
  3856. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  3857. {
  3858. struct page *page = cookie;
  3859. if (page) {
  3860. kunmap(page);
  3861. page_cache_release(page);
  3862. }
  3863. }
  3864. /*
  3865. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  3866. */
  3867. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  3868. {
  3869. struct address_space *mapping = inode->i_mapping;
  3870. struct page *page;
  3871. void *fsdata;
  3872. int err;
  3873. char *kaddr;
  3874. unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  3875. if (nofs)
  3876. flags |= AOP_FLAG_NOFS;
  3877. retry:
  3878. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  3879. flags, &page, &fsdata);
  3880. if (err)
  3881. goto fail;
  3882. kaddr = kmap_atomic(page);
  3883. memcpy(kaddr, symname, len-1);
  3884. kunmap_atomic(kaddr);
  3885. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  3886. page, fsdata);
  3887. if (err < 0)
  3888. goto fail;
  3889. if (err < len-1)
  3890. goto retry;
  3891. mark_inode_dirty(inode);
  3892. return 0;
  3893. fail:
  3894. return err;
  3895. }
  3896. int page_symlink(struct inode *inode, const char *symname, int len)
  3897. {
  3898. return __page_symlink(inode, symname, len,
  3899. !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
  3900. }
  3901. const struct inode_operations page_symlink_inode_operations = {
  3902. .readlink = generic_readlink,
  3903. .follow_link = page_follow_link_light,
  3904. .put_link = page_put_link,
  3905. };
  3906. EXPORT_SYMBOL(user_path_at);
  3907. EXPORT_SYMBOL(follow_down_one);
  3908. EXPORT_SYMBOL(follow_down);
  3909. EXPORT_SYMBOL(follow_up);
  3910. EXPORT_SYMBOL(get_write_access); /* nfsd */
  3911. EXPORT_SYMBOL(lock_rename);
  3912. EXPORT_SYMBOL(lookup_one_len);
  3913. EXPORT_SYMBOL(page_follow_link_light);
  3914. EXPORT_SYMBOL(page_put_link);
  3915. EXPORT_SYMBOL(page_readlink);
  3916. EXPORT_SYMBOL(__page_symlink);
  3917. EXPORT_SYMBOL(page_symlink);
  3918. EXPORT_SYMBOL(page_symlink_inode_operations);
  3919. EXPORT_SYMBOL(kern_path);
  3920. EXPORT_SYMBOL(vfs_path_lookup);
  3921. EXPORT_SYMBOL(inode_permission);
  3922. EXPORT_SYMBOL(unlock_rename);
  3923. EXPORT_SYMBOL(vfs_create);
  3924. EXPORT_SYMBOL(vfs_link);
  3925. EXPORT_SYMBOL(vfs_mkdir);
  3926. EXPORT_SYMBOL(vfs_mknod);
  3927. EXPORT_SYMBOL(generic_permission);
  3928. EXPORT_SYMBOL(vfs_readlink);
  3929. EXPORT_SYMBOL(vfs_rename);
  3930. EXPORT_SYMBOL(vfs_rmdir);
  3931. EXPORT_SYMBOL(vfs_symlink);
  3932. EXPORT_SYMBOL(vfs_unlink);
  3933. EXPORT_SYMBOL(dentry_unhash);
  3934. EXPORT_SYMBOL(generic_readlink);