namei.c 123 KB

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