namei.c 110 KB

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