namei.c 110 KB

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