namei.c 143 KB

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