namei.c 100 KB

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