namei.c 120 KB

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