namei.c 123 KB

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