namei.c 120 KB

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