namei.c 174 KB

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