namei.c 107 KB

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