namei.c 125 KB

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