namei.c 123 KB

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