namei.c 120 KB

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