namei.c 122 KB

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