namei.c 138 KB

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