namei.c 107 KB

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