namei.c 116 KB

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