namei.c 121 KB

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