namei.c 121 KB

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