namei.c 107 KB

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