namei.c 121 KB

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