namei.c 143 KB

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