namei.c 121 KB

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