namei.c 109 KB

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