namei.c 107 KB

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