namei.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409
  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/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/fs.h>
  18. #include <linux/namei.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/fsnotify.h>
  21. #include <linux/personality.h>
  22. #include <linux/security.h>
  23. #include <linux/ima.h>
  24. #include <linux/syscalls.h>
  25. #include <linux/mount.h>
  26. #include <linux/audit.h>
  27. #include <linux/capability.h>
  28. #include <linux/file.h>
  29. #include <linux/fcntl.h>
  30. #include <linux/device_cgroup.h>
  31. #include <linux/fs_struct.h>
  32. #include <asm/uaccess.h>
  33. #include "internal.h"
  34. /* [Feb-1997 T. Schoebel-Theuer]
  35. * Fundamental changes in the pathname lookup mechanisms (namei)
  36. * were necessary because of omirr. The reason is that omirr needs
  37. * to know the _real_ pathname, not the user-supplied one, in case
  38. * of symlinks (and also when transname replacements occur).
  39. *
  40. * The new code replaces the old recursive symlink resolution with
  41. * an iterative one (in case of non-nested symlink chains). It does
  42. * this with calls to <fs>_follow_link().
  43. * As a side effect, dir_namei(), _namei() and follow_link() are now
  44. * replaced with a single function lookup_dentry() that can handle all
  45. * the special cases of the former code.
  46. *
  47. * With the new dcache, the pathname is stored at each inode, at least as
  48. * long as the refcount of the inode is positive. As a side effect, the
  49. * size of the dcache depends on the inode cache and thus is dynamic.
  50. *
  51. * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
  52. * resolution to correspond with current state of the code.
  53. *
  54. * Note that the symlink resolution is not *completely* iterative.
  55. * There is still a significant amount of tail- and mid- recursion in
  56. * the algorithm. Also, note that <fs>_readlink() is not used in
  57. * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
  58. * may return different results than <fs>_follow_link(). Many virtual
  59. * filesystems (including /proc) exhibit this behavior.
  60. */
  61. /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
  62. * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
  63. * and the name already exists in form of a symlink, try to create the new
  64. * name indicated by the symlink. The old code always complained that the
  65. * name already exists, due to not following the symlink even if its target
  66. * is nonexistent. The new semantics affects also mknod() and link() when
  67. * the name is a symlink pointing to a non-existant name.
  68. *
  69. * I don't know which semantics is the right one, since I have no access
  70. * to standards. But I found by trial that HP-UX 9.0 has the full "new"
  71. * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
  72. * "old" one. Personally, I think the new semantics is much more logical.
  73. * Note that "ln old new" where "new" is a symlink pointing to a non-existing
  74. * file does succeed in both HP-UX and SunOs, but not in Solaris
  75. * and in the old Linux semantics.
  76. */
  77. /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
  78. * semantics. See the comments in "open_namei" and "do_link" below.
  79. *
  80. * [10-Sep-98 Alan Modra] Another symlink change.
  81. */
  82. /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
  83. * inside the path - always follow.
  84. * in the last component in creation/removal/renaming - never follow.
  85. * if LOOKUP_FOLLOW passed - follow.
  86. * if the pathname has trailing slashes - follow.
  87. * otherwise - don't follow.
  88. * (applied in that order).
  89. *
  90. * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
  91. * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
  92. * During the 2.4 we need to fix the userland stuff depending on it -
  93. * hopefully we will be able to get rid of that wart in 2.5. So far only
  94. * XEmacs seems to be relying on it...
  95. */
  96. /*
  97. * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
  98. * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
  99. * any extra contention...
  100. */
  101. /* In order to reduce some races, while at the same time doing additional
  102. * checking and hopefully speeding things up, we copy filenames to the
  103. * kernel data space before using them..
  104. *
  105. * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
  106. * PATH_MAX includes the nul terminator --RR.
  107. */
  108. static int do_getname(const char __user *filename, char *page)
  109. {
  110. int retval;
  111. unsigned long len = PATH_MAX;
  112. if (!segment_eq(get_fs(), KERNEL_DS)) {
  113. if ((unsigned long) filename >= TASK_SIZE)
  114. return -EFAULT;
  115. if (TASK_SIZE - (unsigned long) filename < PATH_MAX)
  116. len = TASK_SIZE - (unsigned long) filename;
  117. }
  118. retval = strncpy_from_user(page, filename, len);
  119. if (retval > 0) {
  120. if (retval < len)
  121. return 0;
  122. return -ENAMETOOLONG;
  123. } else if (!retval)
  124. retval = -ENOENT;
  125. return retval;
  126. }
  127. static char *getname_flags(const char __user * filename, int flags)
  128. {
  129. char *tmp, *result;
  130. result = ERR_PTR(-ENOMEM);
  131. tmp = __getname();
  132. if (tmp) {
  133. int retval = do_getname(filename, tmp);
  134. result = tmp;
  135. if (retval < 0) {
  136. if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) {
  137. __putname(tmp);
  138. result = ERR_PTR(retval);
  139. }
  140. }
  141. }
  142. audit_getname(result);
  143. return result;
  144. }
  145. char *getname(const char __user * filename)
  146. {
  147. return getname_flags(filename, 0);
  148. }
  149. #ifdef CONFIG_AUDITSYSCALL
  150. void putname(const char *name)
  151. {
  152. if (unlikely(!audit_dummy_context()))
  153. audit_putname(name);
  154. else
  155. __putname(name);
  156. }
  157. EXPORT_SYMBOL(putname);
  158. #endif
  159. /*
  160. * This does basic POSIX ACL permission checking
  161. */
  162. static int acl_permission_check(struct inode *inode, int mask, unsigned int flags,
  163. int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
  164. {
  165. umode_t mode = inode->i_mode;
  166. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  167. if (current_fsuid() == inode->i_uid)
  168. mode >>= 6;
  169. else {
  170. if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
  171. int error = check_acl(inode, mask, flags);
  172. if (error != -EAGAIN)
  173. return error;
  174. }
  175. if (in_group_p(inode->i_gid))
  176. mode >>= 3;
  177. }
  178. /*
  179. * If the DACs are ok we don't need any capability check.
  180. */
  181. if ((mask & ~mode) == 0)
  182. return 0;
  183. return -EACCES;
  184. }
  185. /**
  186. * generic_permission - check for access rights on a Posix-like filesystem
  187. * @inode: inode to check access rights for
  188. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  189. * @check_acl: optional callback to check for Posix ACLs
  190. * @flags: IPERM_FLAG_ flags.
  191. *
  192. * Used to check for read/write/execute permissions on a file.
  193. * We use "fsuid" for this, letting us set arbitrary permissions
  194. * for filesystem access without changing the "normal" uids which
  195. * are used for other things.
  196. *
  197. * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
  198. * request cannot be satisfied (eg. requires blocking or too much complexity).
  199. * It would then be called again in ref-walk mode.
  200. */
  201. int generic_permission(struct inode *inode, int mask, unsigned int flags,
  202. int (*check_acl)(struct inode *inode, int mask, unsigned int flags))
  203. {
  204. int ret;
  205. /*
  206. * Do the basic POSIX ACL permission checks.
  207. */
  208. ret = acl_permission_check(inode, mask, flags, check_acl);
  209. if (ret != -EACCES)
  210. return ret;
  211. /*
  212. * Read/write DACs are always overridable.
  213. * Executable DACs are overridable if at least one exec bit is set.
  214. */
  215. if (!(mask & MAY_EXEC) || execute_ok(inode))
  216. if (capable(CAP_DAC_OVERRIDE))
  217. return 0;
  218. /*
  219. * Searching includes executable on directories, else just read.
  220. */
  221. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  222. if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
  223. if (capable(CAP_DAC_READ_SEARCH))
  224. return 0;
  225. return -EACCES;
  226. }
  227. /**
  228. * inode_permission - check for access rights to a given inode
  229. * @inode: inode to check permission on
  230. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  231. *
  232. * Used to check for read/write/execute permissions on an inode.
  233. * We use "fsuid" for this, letting us set arbitrary permissions
  234. * for filesystem access without changing the "normal" uids which
  235. * are used for other things.
  236. */
  237. int inode_permission(struct inode *inode, int mask)
  238. {
  239. int retval;
  240. if (mask & MAY_WRITE) {
  241. umode_t mode = inode->i_mode;
  242. /*
  243. * Nobody gets write access to a read-only fs.
  244. */
  245. if (IS_RDONLY(inode) &&
  246. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  247. return -EROFS;
  248. /*
  249. * Nobody gets write access to an immutable file.
  250. */
  251. if (IS_IMMUTABLE(inode))
  252. return -EACCES;
  253. }
  254. if (inode->i_op->permission)
  255. retval = inode->i_op->permission(inode, mask, 0);
  256. else
  257. retval = generic_permission(inode, mask, 0,
  258. inode->i_op->check_acl);
  259. if (retval)
  260. return retval;
  261. retval = devcgroup_inode_permission(inode, mask);
  262. if (retval)
  263. return retval;
  264. return security_inode_permission(inode, mask);
  265. }
  266. /**
  267. * file_permission - check for additional access rights to a given file
  268. * @file: file to check access rights for
  269. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  270. *
  271. * Used to check for read/write/execute permissions on an already opened
  272. * file.
  273. *
  274. * Note:
  275. * Do not use this function in new code. All access checks should
  276. * be done using inode_permission().
  277. */
  278. int file_permission(struct file *file, int mask)
  279. {
  280. return inode_permission(file->f_path.dentry->d_inode, mask);
  281. }
  282. /*
  283. * get_write_access() gets write permission for a file.
  284. * put_write_access() releases this write permission.
  285. * This is used for regular files.
  286. * We cannot support write (and maybe mmap read-write shared) accesses and
  287. * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
  288. * can have the following values:
  289. * 0: no writers, no VM_DENYWRITE mappings
  290. * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
  291. * > 0: (i_writecount) users are writing to the file.
  292. *
  293. * Normally we operate on that counter with atomic_{inc,dec} and it's safe
  294. * except for the cases where we don't hold i_writecount yet. Then we need to
  295. * use {get,deny}_write_access() - these functions check the sign and refuse
  296. * to do the change if sign is wrong. Exclusion between them is provided by
  297. * the inode->i_lock spinlock.
  298. */
  299. int get_write_access(struct inode * inode)
  300. {
  301. spin_lock(&inode->i_lock);
  302. if (atomic_read(&inode->i_writecount) < 0) {
  303. spin_unlock(&inode->i_lock);
  304. return -ETXTBSY;
  305. }
  306. atomic_inc(&inode->i_writecount);
  307. spin_unlock(&inode->i_lock);
  308. return 0;
  309. }
  310. int deny_write_access(struct file * file)
  311. {
  312. struct inode *inode = file->f_path.dentry->d_inode;
  313. spin_lock(&inode->i_lock);
  314. if (atomic_read(&inode->i_writecount) > 0) {
  315. spin_unlock(&inode->i_lock);
  316. return -ETXTBSY;
  317. }
  318. atomic_dec(&inode->i_writecount);
  319. spin_unlock(&inode->i_lock);
  320. return 0;
  321. }
  322. /**
  323. * path_get - get a reference to a path
  324. * @path: path to get the reference to
  325. *
  326. * Given a path increment the reference count to the dentry and the vfsmount.
  327. */
  328. void path_get(struct path *path)
  329. {
  330. mntget(path->mnt);
  331. dget(path->dentry);
  332. }
  333. EXPORT_SYMBOL(path_get);
  334. /**
  335. * path_put - put a reference to a path
  336. * @path: path to put the reference to
  337. *
  338. * Given a path decrement the reference count to the dentry and the vfsmount.
  339. */
  340. void path_put(struct path *path)
  341. {
  342. dput(path->dentry);
  343. mntput(path->mnt);
  344. }
  345. EXPORT_SYMBOL(path_put);
  346. /**
  347. * nameidata_drop_rcu - drop this nameidata out of rcu-walk
  348. * @nd: nameidata pathwalk data to drop
  349. * Returns: 0 on success, -ECHILD on failure
  350. *
  351. * Path walking has 2 modes, rcu-walk and ref-walk (see
  352. * Documentation/filesystems/path-lookup.txt). __drop_rcu* functions attempt
  353. * to drop out of rcu-walk mode and take normal reference counts on dentries
  354. * and vfsmounts to transition to rcu-walk mode. __drop_rcu* functions take
  355. * refcounts at the last known good point before rcu-walk got stuck, so
  356. * ref-walk may continue from there. If this is not successful (eg. a seqcount
  357. * has changed), then failure is returned and path walk restarts from the
  358. * beginning in ref-walk mode.
  359. *
  360. * nameidata_drop_rcu attempts to drop the current nd->path and nd->root into
  361. * ref-walk. Must be called from rcu-walk context.
  362. */
  363. static int nameidata_drop_rcu(struct nameidata *nd)
  364. {
  365. struct fs_struct *fs = current->fs;
  366. struct dentry *dentry = nd->path.dentry;
  367. int want_root = 0;
  368. BUG_ON(!(nd->flags & LOOKUP_RCU));
  369. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  370. want_root = 1;
  371. spin_lock(&fs->lock);
  372. if (nd->root.mnt != fs->root.mnt ||
  373. nd->root.dentry != fs->root.dentry)
  374. goto err_root;
  375. }
  376. spin_lock(&dentry->d_lock);
  377. if (!__d_rcu_to_refcount(dentry, nd->seq))
  378. goto err;
  379. BUG_ON(nd->inode != dentry->d_inode);
  380. spin_unlock(&dentry->d_lock);
  381. if (want_root) {
  382. path_get(&nd->root);
  383. spin_unlock(&fs->lock);
  384. }
  385. mntget(nd->path.mnt);
  386. rcu_read_unlock();
  387. br_read_unlock(vfsmount_lock);
  388. nd->flags &= ~LOOKUP_RCU;
  389. return 0;
  390. err:
  391. spin_unlock(&dentry->d_lock);
  392. err_root:
  393. if (want_root)
  394. spin_unlock(&fs->lock);
  395. return -ECHILD;
  396. }
  397. /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
  398. static inline int nameidata_drop_rcu_maybe(struct nameidata *nd)
  399. {
  400. if (nd->flags & LOOKUP_RCU)
  401. return nameidata_drop_rcu(nd);
  402. return 0;
  403. }
  404. /**
  405. * nameidata_dentry_drop_rcu - drop nameidata and dentry out of rcu-walk
  406. * @nd: nameidata pathwalk data to drop
  407. * @dentry: dentry to drop
  408. * Returns: 0 on success, -ECHILD on failure
  409. *
  410. * nameidata_dentry_drop_rcu attempts to drop the current nd->path and nd->root,
  411. * and dentry into ref-walk. @dentry must be a path found by a do_lookup call on
  412. * @nd. Must be called from rcu-walk context.
  413. */
  414. static int nameidata_dentry_drop_rcu(struct nameidata *nd, struct dentry *dentry)
  415. {
  416. struct fs_struct *fs = current->fs;
  417. struct dentry *parent = nd->path.dentry;
  418. int want_root = 0;
  419. BUG_ON(!(nd->flags & LOOKUP_RCU));
  420. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  421. want_root = 1;
  422. spin_lock(&fs->lock);
  423. if (nd->root.mnt != fs->root.mnt ||
  424. nd->root.dentry != fs->root.dentry)
  425. goto err_root;
  426. }
  427. spin_lock(&parent->d_lock);
  428. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  429. if (!__d_rcu_to_refcount(dentry, nd->seq))
  430. goto err;
  431. /*
  432. * If the sequence check on the child dentry passed, then the child has
  433. * not been removed from its parent. This means the parent dentry must
  434. * be valid and able to take a reference at this point.
  435. */
  436. BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent);
  437. BUG_ON(!parent->d_count);
  438. parent->d_count++;
  439. spin_unlock(&dentry->d_lock);
  440. spin_unlock(&parent->d_lock);
  441. if (want_root) {
  442. path_get(&nd->root);
  443. spin_unlock(&fs->lock);
  444. }
  445. mntget(nd->path.mnt);
  446. rcu_read_unlock();
  447. br_read_unlock(vfsmount_lock);
  448. nd->flags &= ~LOOKUP_RCU;
  449. return 0;
  450. err:
  451. spin_unlock(&dentry->d_lock);
  452. spin_unlock(&parent->d_lock);
  453. err_root:
  454. if (want_root)
  455. spin_unlock(&fs->lock);
  456. return -ECHILD;
  457. }
  458. /* Try to drop out of rcu-walk mode if we were in it, otherwise do nothing. */
  459. static inline int nameidata_dentry_drop_rcu_maybe(struct nameidata *nd, struct dentry *dentry)
  460. {
  461. if (nd->flags & LOOKUP_RCU) {
  462. if (unlikely(nameidata_dentry_drop_rcu(nd, dentry))) {
  463. nd->flags &= ~LOOKUP_RCU;
  464. if (!(nd->flags & LOOKUP_ROOT))
  465. nd->root.mnt = NULL;
  466. rcu_read_unlock();
  467. br_read_unlock(vfsmount_lock);
  468. return -ECHILD;
  469. }
  470. }
  471. return 0;
  472. }
  473. /**
  474. * nameidata_drop_rcu_last - drop nameidata ending path walk out of rcu-walk
  475. * @nd: nameidata pathwalk data to drop
  476. * Returns: 0 on success, -ECHILD on failure
  477. *
  478. * nameidata_drop_rcu_last attempts to drop the current nd->path into ref-walk.
  479. * nd->path should be the final element of the lookup, so nd->root is discarded.
  480. * Must be called from rcu-walk context.
  481. */
  482. static int nameidata_drop_rcu_last(struct nameidata *nd)
  483. {
  484. struct dentry *dentry = nd->path.dentry;
  485. BUG_ON(!(nd->flags & LOOKUP_RCU));
  486. nd->flags &= ~LOOKUP_RCU;
  487. if (!(nd->flags & LOOKUP_ROOT))
  488. nd->root.mnt = NULL;
  489. spin_lock(&dentry->d_lock);
  490. if (!__d_rcu_to_refcount(dentry, nd->seq))
  491. goto err_unlock;
  492. BUG_ON(nd->inode != dentry->d_inode);
  493. spin_unlock(&dentry->d_lock);
  494. mntget(nd->path.mnt);
  495. rcu_read_unlock();
  496. br_read_unlock(vfsmount_lock);
  497. return 0;
  498. err_unlock:
  499. spin_unlock(&dentry->d_lock);
  500. rcu_read_unlock();
  501. br_read_unlock(vfsmount_lock);
  502. return -ECHILD;
  503. }
  504. /**
  505. * release_open_intent - free up open intent resources
  506. * @nd: pointer to nameidata
  507. */
  508. void release_open_intent(struct nameidata *nd)
  509. {
  510. struct file *file = nd->intent.open.file;
  511. if (file && !IS_ERR(file)) {
  512. if (file->f_path.dentry == NULL)
  513. put_filp(file);
  514. else
  515. fput(file);
  516. }
  517. }
  518. static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd)
  519. {
  520. return dentry->d_op->d_revalidate(dentry, nd);
  521. }
  522. static struct dentry *
  523. do_revalidate(struct dentry *dentry, struct nameidata *nd)
  524. {
  525. int status = d_revalidate(dentry, nd);
  526. if (unlikely(status <= 0)) {
  527. /*
  528. * The dentry failed validation.
  529. * If d_revalidate returned 0 attempt to invalidate
  530. * the dentry otherwise d_revalidate is asking us
  531. * to return a fail status.
  532. */
  533. if (status < 0) {
  534. dput(dentry);
  535. dentry = ERR_PTR(status);
  536. } else if (!d_invalidate(dentry)) {
  537. dput(dentry);
  538. dentry = NULL;
  539. }
  540. }
  541. return dentry;
  542. }
  543. /*
  544. * handle_reval_path - force revalidation of a dentry
  545. *
  546. * In some situations the path walking code will trust dentries without
  547. * revalidating them. This causes problems for filesystems that depend on
  548. * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
  549. * (which indicates that it's possible for the dentry to go stale), force
  550. * a d_revalidate call before proceeding.
  551. *
  552. * Returns 0 if the revalidation was successful. If the revalidation fails,
  553. * either return the error returned by d_revalidate or -ESTALE if the
  554. * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
  555. * invalidate the dentry. It's up to the caller to handle putting references
  556. * to the path if necessary.
  557. */
  558. static inline int handle_reval_path(struct nameidata *nd)
  559. {
  560. struct dentry *dentry = nd->path.dentry;
  561. int status;
  562. if (likely(!(nd->flags & LOOKUP_JUMPED)))
  563. return 0;
  564. if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE)))
  565. return 0;
  566. if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)))
  567. return 0;
  568. /* Note: we do not d_invalidate() */
  569. status = d_revalidate(dentry, nd);
  570. if (status > 0)
  571. return 0;
  572. if (!status)
  573. status = -ESTALE;
  574. return status;
  575. }
  576. /*
  577. * Short-cut version of permission(), for calling on directories
  578. * during pathname resolution. Combines parts of permission()
  579. * and generic_permission(), and tests ONLY for MAY_EXEC permission.
  580. *
  581. * If appropriate, check DAC only. If not appropriate, or
  582. * short-cut DAC fails, then call ->permission() to do more
  583. * complete permission check.
  584. */
  585. static inline int exec_permission(struct inode *inode, unsigned int flags)
  586. {
  587. int ret;
  588. if (inode->i_op->permission) {
  589. ret = inode->i_op->permission(inode, MAY_EXEC, flags);
  590. } else {
  591. ret = acl_permission_check(inode, MAY_EXEC, flags,
  592. inode->i_op->check_acl);
  593. }
  594. if (likely(!ret))
  595. goto ok;
  596. if (ret == -ECHILD)
  597. return ret;
  598. if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
  599. goto ok;
  600. return ret;
  601. ok:
  602. return security_inode_exec_permission(inode, flags);
  603. }
  604. static __always_inline void set_root(struct nameidata *nd)
  605. {
  606. if (!nd->root.mnt)
  607. get_fs_root(current->fs, &nd->root);
  608. }
  609. static int link_path_walk(const char *, struct nameidata *);
  610. static __always_inline void set_root_rcu(struct nameidata *nd)
  611. {
  612. if (!nd->root.mnt) {
  613. struct fs_struct *fs = current->fs;
  614. unsigned seq;
  615. do {
  616. seq = read_seqcount_begin(&fs->seq);
  617. nd->root = fs->root;
  618. } while (read_seqcount_retry(&fs->seq, seq));
  619. }
  620. }
  621. static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
  622. {
  623. int ret;
  624. if (IS_ERR(link))
  625. goto fail;
  626. if (*link == '/') {
  627. set_root(nd);
  628. path_put(&nd->path);
  629. nd->path = nd->root;
  630. path_get(&nd->root);
  631. nd->flags |= LOOKUP_JUMPED;
  632. }
  633. nd->inode = nd->path.dentry->d_inode;
  634. ret = link_path_walk(link, nd);
  635. return ret;
  636. fail:
  637. path_put(&nd->path);
  638. return PTR_ERR(link);
  639. }
  640. static void path_put_conditional(struct path *path, struct nameidata *nd)
  641. {
  642. dput(path->dentry);
  643. if (path->mnt != nd->path.mnt)
  644. mntput(path->mnt);
  645. }
  646. static inline void path_to_nameidata(const struct path *path,
  647. struct nameidata *nd)
  648. {
  649. if (!(nd->flags & LOOKUP_RCU)) {
  650. dput(nd->path.dentry);
  651. if (nd->path.mnt != path->mnt)
  652. mntput(nd->path.mnt);
  653. }
  654. nd->path.mnt = path->mnt;
  655. nd->path.dentry = path->dentry;
  656. }
  657. static __always_inline int
  658. __do_follow_link(const struct path *link, struct nameidata *nd, void **p)
  659. {
  660. int error;
  661. struct dentry *dentry = link->dentry;
  662. BUG_ON(nd->flags & LOOKUP_RCU);
  663. touch_atime(link->mnt, dentry);
  664. nd_set_link(nd, NULL);
  665. if (link->mnt == nd->path.mnt)
  666. mntget(link->mnt);
  667. error = security_inode_follow_link(link->dentry, nd);
  668. if (error) {
  669. *p = ERR_PTR(error); /* no ->put_link(), please */
  670. path_put(&nd->path);
  671. return error;
  672. }
  673. nd->last_type = LAST_BIND;
  674. *p = dentry->d_inode->i_op->follow_link(dentry, nd);
  675. error = PTR_ERR(*p);
  676. if (!IS_ERR(*p)) {
  677. char *s = nd_get_link(nd);
  678. error = 0;
  679. if (s)
  680. error = __vfs_follow_link(nd, s);
  681. else if (nd->last_type == LAST_BIND) {
  682. nd->flags |= LOOKUP_JUMPED;
  683. nd->inode = nd->path.dentry->d_inode;
  684. if (nd->inode->i_op->follow_link) {
  685. /* stepped on a _really_ weird one */
  686. path_put(&nd->path);
  687. error = -ELOOP;
  688. }
  689. }
  690. }
  691. return error;
  692. }
  693. /*
  694. * This limits recursive symlink follows to 8, while
  695. * limiting consecutive symlinks to 40.
  696. *
  697. * Without that kind of total limit, nasty chains of consecutive
  698. * symlinks can cause almost arbitrarily long lookups.
  699. */
  700. static inline int do_follow_link(struct path *path, struct nameidata *nd)
  701. {
  702. void *cookie;
  703. int err = -ELOOP;
  704. if (current->link_count >= MAX_NESTED_LINKS)
  705. goto loop;
  706. if (current->total_link_count >= 40)
  707. goto loop;
  708. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  709. cond_resched();
  710. current->link_count++;
  711. current->total_link_count++;
  712. nd->depth++;
  713. err = __do_follow_link(path, nd, &cookie);
  714. if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link)
  715. path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie);
  716. path_put(path);
  717. current->link_count--;
  718. nd->depth--;
  719. return err;
  720. loop:
  721. path_put_conditional(path, nd);
  722. path_put(&nd->path);
  723. return err;
  724. }
  725. static int follow_up_rcu(struct path *path)
  726. {
  727. struct vfsmount *parent;
  728. struct dentry *mountpoint;
  729. parent = path->mnt->mnt_parent;
  730. if (parent == path->mnt)
  731. return 0;
  732. mountpoint = path->mnt->mnt_mountpoint;
  733. path->dentry = mountpoint;
  734. path->mnt = parent;
  735. return 1;
  736. }
  737. int follow_up(struct path *path)
  738. {
  739. struct vfsmount *parent;
  740. struct dentry *mountpoint;
  741. br_read_lock(vfsmount_lock);
  742. parent = path->mnt->mnt_parent;
  743. if (parent == path->mnt) {
  744. br_read_unlock(vfsmount_lock);
  745. return 0;
  746. }
  747. mntget(parent);
  748. mountpoint = dget(path->mnt->mnt_mountpoint);
  749. br_read_unlock(vfsmount_lock);
  750. dput(path->dentry);
  751. path->dentry = mountpoint;
  752. mntput(path->mnt);
  753. path->mnt = parent;
  754. return 1;
  755. }
  756. /*
  757. * Perform an automount
  758. * - return -EISDIR to tell follow_managed() to stop and return the path we
  759. * were called with.
  760. */
  761. static int follow_automount(struct path *path, unsigned flags,
  762. bool *need_mntput)
  763. {
  764. struct vfsmount *mnt;
  765. int err;
  766. if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
  767. return -EREMOTE;
  768. /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT
  769. * and this is the terminal part of the path.
  770. */
  771. if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_CONTINUE))
  772. return -EISDIR; /* we actually want to stop here */
  773. /* We want to mount if someone is trying to open/create a file of any
  774. * type under the mountpoint, wants to traverse through the mountpoint
  775. * or wants to open the mounted directory.
  776. *
  777. * We don't want to mount if someone's just doing a stat and they've
  778. * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and
  779. * appended a '/' to the name.
  780. */
  781. if (!(flags & LOOKUP_FOLLOW) &&
  782. !(flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY |
  783. LOOKUP_OPEN | LOOKUP_CREATE)))
  784. return -EISDIR;
  785. current->total_link_count++;
  786. if (current->total_link_count >= 40)
  787. return -ELOOP;
  788. mnt = path->dentry->d_op->d_automount(path);
  789. if (IS_ERR(mnt)) {
  790. /*
  791. * The filesystem is allowed to return -EISDIR here to indicate
  792. * it doesn't want to automount. For instance, autofs would do
  793. * this so that its userspace daemon can mount on this dentry.
  794. *
  795. * However, we can only permit this if it's a terminal point in
  796. * the path being looked up; if it wasn't then the remainder of
  797. * the path is inaccessible and we should say so.
  798. */
  799. if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_CONTINUE))
  800. return -EREMOTE;
  801. return PTR_ERR(mnt);
  802. }
  803. if (!mnt) /* mount collision */
  804. return 0;
  805. err = finish_automount(mnt, path);
  806. switch (err) {
  807. case -EBUSY:
  808. /* Someone else made a mount here whilst we were busy */
  809. return 0;
  810. case 0:
  811. dput(path->dentry);
  812. if (*need_mntput)
  813. mntput(path->mnt);
  814. path->mnt = mnt;
  815. path->dentry = dget(mnt->mnt_root);
  816. *need_mntput = true;
  817. return 0;
  818. default:
  819. return err;
  820. }
  821. }
  822. /*
  823. * Handle a dentry that is managed in some way.
  824. * - Flagged for transit management (autofs)
  825. * - Flagged as mountpoint
  826. * - Flagged as automount point
  827. *
  828. * This may only be called in refwalk mode.
  829. *
  830. * Serialization is taken care of in namespace.c
  831. */
  832. static int follow_managed(struct path *path, unsigned flags)
  833. {
  834. unsigned managed;
  835. bool need_mntput = false;
  836. int ret;
  837. /* Given that we're not holding a lock here, we retain the value in a
  838. * local variable for each dentry as we look at it so that we don't see
  839. * the components of that value change under us */
  840. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  841. managed &= DCACHE_MANAGED_DENTRY,
  842. unlikely(managed != 0)) {
  843. /* Allow the filesystem to manage the transit without i_mutex
  844. * being held. */
  845. if (managed & DCACHE_MANAGE_TRANSIT) {
  846. BUG_ON(!path->dentry->d_op);
  847. BUG_ON(!path->dentry->d_op->d_manage);
  848. ret = path->dentry->d_op->d_manage(path->dentry,
  849. false, false);
  850. if (ret < 0)
  851. return ret == -EISDIR ? 0 : ret;
  852. }
  853. /* Transit to a mounted filesystem. */
  854. if (managed & DCACHE_MOUNTED) {
  855. struct vfsmount *mounted = lookup_mnt(path);
  856. if (mounted) {
  857. dput(path->dentry);
  858. if (need_mntput)
  859. mntput(path->mnt);
  860. path->mnt = mounted;
  861. path->dentry = dget(mounted->mnt_root);
  862. need_mntput = true;
  863. continue;
  864. }
  865. /* Something is mounted on this dentry in another
  866. * namespace and/or whatever was mounted there in this
  867. * namespace got unmounted before we managed to get the
  868. * vfsmount_lock */
  869. }
  870. /* Handle an automount point */
  871. if (managed & DCACHE_NEED_AUTOMOUNT) {
  872. ret = follow_automount(path, flags, &need_mntput);
  873. if (ret < 0)
  874. return ret == -EISDIR ? 0 : ret;
  875. continue;
  876. }
  877. /* We didn't change the current path point */
  878. break;
  879. }
  880. return 0;
  881. }
  882. int follow_down_one(struct path *path)
  883. {
  884. struct vfsmount *mounted;
  885. mounted = lookup_mnt(path);
  886. if (mounted) {
  887. dput(path->dentry);
  888. mntput(path->mnt);
  889. path->mnt = mounted;
  890. path->dentry = dget(mounted->mnt_root);
  891. return 1;
  892. }
  893. return 0;
  894. }
  895. /*
  896. * Skip to top of mountpoint pile in rcuwalk mode. We abort the rcu-walk if we
  897. * meet a managed dentry and we're not walking to "..". True is returned to
  898. * continue, false to abort.
  899. */
  900. static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
  901. struct inode **inode, bool reverse_transit)
  902. {
  903. while (d_mountpoint(path->dentry)) {
  904. struct vfsmount *mounted;
  905. if (unlikely(path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) &&
  906. !reverse_transit &&
  907. path->dentry->d_op->d_manage(path->dentry, false, true) < 0)
  908. return false;
  909. mounted = __lookup_mnt(path->mnt, path->dentry, 1);
  910. if (!mounted)
  911. break;
  912. path->mnt = mounted;
  913. path->dentry = mounted->mnt_root;
  914. nd->seq = read_seqcount_begin(&path->dentry->d_seq);
  915. *inode = path->dentry->d_inode;
  916. }
  917. if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
  918. return reverse_transit;
  919. return true;
  920. }
  921. static int follow_dotdot_rcu(struct nameidata *nd)
  922. {
  923. struct inode *inode = nd->inode;
  924. set_root_rcu(nd);
  925. while (1) {
  926. if (nd->path.dentry == nd->root.dentry &&
  927. nd->path.mnt == nd->root.mnt) {
  928. break;
  929. }
  930. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  931. struct dentry *old = nd->path.dentry;
  932. struct dentry *parent = old->d_parent;
  933. unsigned seq;
  934. seq = read_seqcount_begin(&parent->d_seq);
  935. if (read_seqcount_retry(&old->d_seq, nd->seq))
  936. goto failed;
  937. inode = parent->d_inode;
  938. nd->path.dentry = parent;
  939. nd->seq = seq;
  940. break;
  941. }
  942. if (!follow_up_rcu(&nd->path))
  943. break;
  944. nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
  945. inode = nd->path.dentry->d_inode;
  946. }
  947. __follow_mount_rcu(nd, &nd->path, &inode, true);
  948. nd->inode = inode;
  949. return 0;
  950. failed:
  951. nd->flags &= ~LOOKUP_RCU;
  952. if (!(nd->flags & LOOKUP_ROOT))
  953. nd->root.mnt = NULL;
  954. rcu_read_unlock();
  955. br_read_unlock(vfsmount_lock);
  956. return -ECHILD;
  957. }
  958. /*
  959. * Follow down to the covering mount currently visible to userspace. At each
  960. * point, the filesystem owning that dentry may be queried as to whether the
  961. * caller is permitted to proceed or not.
  962. *
  963. * Care must be taken as namespace_sem may be held (indicated by mounting_here
  964. * being true).
  965. */
  966. int follow_down(struct path *path, bool mounting_here)
  967. {
  968. unsigned managed;
  969. int ret;
  970. while (managed = ACCESS_ONCE(path->dentry->d_flags),
  971. unlikely(managed & DCACHE_MANAGED_DENTRY)) {
  972. /* Allow the filesystem to manage the transit without i_mutex
  973. * being held.
  974. *
  975. * We indicate to the filesystem if someone is trying to mount
  976. * something here. This gives autofs the chance to deny anyone
  977. * other than its daemon the right to mount on its
  978. * superstructure.
  979. *
  980. * The filesystem may sleep at this point.
  981. */
  982. if (managed & DCACHE_MANAGE_TRANSIT) {
  983. BUG_ON(!path->dentry->d_op);
  984. BUG_ON(!path->dentry->d_op->d_manage);
  985. ret = path->dentry->d_op->d_manage(
  986. path->dentry, mounting_here, false);
  987. if (ret < 0)
  988. return ret == -EISDIR ? 0 : ret;
  989. }
  990. /* Transit to a mounted filesystem. */
  991. if (managed & DCACHE_MOUNTED) {
  992. struct vfsmount *mounted = lookup_mnt(path);
  993. if (!mounted)
  994. break;
  995. dput(path->dentry);
  996. mntput(path->mnt);
  997. path->mnt = mounted;
  998. path->dentry = dget(mounted->mnt_root);
  999. continue;
  1000. }
  1001. /* Don't handle automount points here */
  1002. break;
  1003. }
  1004. return 0;
  1005. }
  1006. /*
  1007. * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
  1008. */
  1009. static void follow_mount(struct path *path)
  1010. {
  1011. while (d_mountpoint(path->dentry)) {
  1012. struct vfsmount *mounted = lookup_mnt(path);
  1013. if (!mounted)
  1014. break;
  1015. dput(path->dentry);
  1016. mntput(path->mnt);
  1017. path->mnt = mounted;
  1018. path->dentry = dget(mounted->mnt_root);
  1019. }
  1020. }
  1021. static void follow_dotdot(struct nameidata *nd)
  1022. {
  1023. set_root(nd);
  1024. while(1) {
  1025. struct dentry *old = nd->path.dentry;
  1026. if (nd->path.dentry == nd->root.dentry &&
  1027. nd->path.mnt == nd->root.mnt) {
  1028. break;
  1029. }
  1030. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  1031. /* rare case of legitimate dget_parent()... */
  1032. nd->path.dentry = dget_parent(nd->path.dentry);
  1033. dput(old);
  1034. break;
  1035. }
  1036. if (!follow_up(&nd->path))
  1037. break;
  1038. }
  1039. follow_mount(&nd->path);
  1040. nd->inode = nd->path.dentry->d_inode;
  1041. }
  1042. /*
  1043. * Allocate a dentry with name and parent, and perform a parent
  1044. * directory ->lookup on it. Returns the new dentry, or ERR_PTR
  1045. * on error. parent->d_inode->i_mutex must be held. d_lookup must
  1046. * have verified that no child exists while under i_mutex.
  1047. */
  1048. static struct dentry *d_alloc_and_lookup(struct dentry *parent,
  1049. struct qstr *name, struct nameidata *nd)
  1050. {
  1051. struct inode *inode = parent->d_inode;
  1052. struct dentry *dentry;
  1053. struct dentry *old;
  1054. /* Don't create child dentry for a dead directory. */
  1055. if (unlikely(IS_DEADDIR(inode)))
  1056. return ERR_PTR(-ENOENT);
  1057. dentry = d_alloc(parent, name);
  1058. if (unlikely(!dentry))
  1059. return ERR_PTR(-ENOMEM);
  1060. old = inode->i_op->lookup(inode, dentry, nd);
  1061. if (unlikely(old)) {
  1062. dput(dentry);
  1063. dentry = old;
  1064. }
  1065. return dentry;
  1066. }
  1067. /*
  1068. * It's more convoluted than I'd like it to be, but... it's still fairly
  1069. * small and for now I'd prefer to have fast path as straight as possible.
  1070. * It _is_ time-critical.
  1071. */
  1072. static int do_lookup(struct nameidata *nd, struct qstr *name,
  1073. struct path *path, struct inode **inode)
  1074. {
  1075. struct vfsmount *mnt = nd->path.mnt;
  1076. struct dentry *dentry, *parent = nd->path.dentry;
  1077. int need_reval = 1;
  1078. int status = 1;
  1079. int err;
  1080. /*
  1081. * Rename seqlock is not required here because in the off chance
  1082. * of a false negative due to a concurrent rename, we're going to
  1083. * do the non-racy lookup, below.
  1084. */
  1085. if (nd->flags & LOOKUP_RCU) {
  1086. unsigned seq;
  1087. *inode = nd->inode;
  1088. dentry = __d_lookup_rcu(parent, name, &seq, inode);
  1089. if (!dentry)
  1090. goto unlazy;
  1091. /* Memory barrier in read_seqcount_begin of child is enough */
  1092. if (__read_seqcount_retry(&parent->d_seq, nd->seq))
  1093. return -ECHILD;
  1094. nd->seq = seq;
  1095. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) {
  1096. status = d_revalidate(dentry, nd);
  1097. if (unlikely(status <= 0)) {
  1098. if (status != -ECHILD)
  1099. need_reval = 0;
  1100. goto unlazy;
  1101. }
  1102. }
  1103. path->mnt = mnt;
  1104. path->dentry = dentry;
  1105. if (likely(__follow_mount_rcu(nd, path, inode, false)))
  1106. return 0;
  1107. unlazy:
  1108. if (dentry) {
  1109. if (nameidata_dentry_drop_rcu(nd, dentry))
  1110. return -ECHILD;
  1111. } else {
  1112. if (nameidata_drop_rcu(nd))
  1113. return -ECHILD;
  1114. }
  1115. } else {
  1116. dentry = __d_lookup(parent, name);
  1117. }
  1118. retry:
  1119. if (unlikely(!dentry)) {
  1120. struct inode *dir = parent->d_inode;
  1121. BUG_ON(nd->inode != dir);
  1122. mutex_lock(&dir->i_mutex);
  1123. dentry = d_lookup(parent, name);
  1124. if (likely(!dentry)) {
  1125. dentry = d_alloc_and_lookup(parent, name, nd);
  1126. if (IS_ERR(dentry)) {
  1127. mutex_unlock(&dir->i_mutex);
  1128. return PTR_ERR(dentry);
  1129. }
  1130. /* known good */
  1131. need_reval = 0;
  1132. status = 1;
  1133. }
  1134. mutex_unlock(&dir->i_mutex);
  1135. }
  1136. if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval)
  1137. status = d_revalidate(dentry, nd);
  1138. if (unlikely(status <= 0)) {
  1139. if (status < 0) {
  1140. dput(dentry);
  1141. return status;
  1142. }
  1143. if (!d_invalidate(dentry)) {
  1144. dput(dentry);
  1145. dentry = NULL;
  1146. need_reval = 1;
  1147. goto retry;
  1148. }
  1149. }
  1150. path->mnt = mnt;
  1151. path->dentry = dentry;
  1152. err = follow_managed(path, nd->flags);
  1153. if (unlikely(err < 0)) {
  1154. path_put_conditional(path, nd);
  1155. return err;
  1156. }
  1157. *inode = path->dentry->d_inode;
  1158. return 0;
  1159. }
  1160. static inline int may_lookup(struct nameidata *nd)
  1161. {
  1162. if (nd->flags & LOOKUP_RCU) {
  1163. int err = exec_permission(nd->inode, IPERM_FLAG_RCU);
  1164. if (err != -ECHILD)
  1165. return err;
  1166. if (nameidata_drop_rcu(nd))
  1167. return -ECHILD;
  1168. }
  1169. return exec_permission(nd->inode, 0);
  1170. }
  1171. static inline int handle_dots(struct nameidata *nd, int type)
  1172. {
  1173. if (type == LAST_DOTDOT) {
  1174. if (nd->flags & LOOKUP_RCU) {
  1175. if (follow_dotdot_rcu(nd))
  1176. return -ECHILD;
  1177. } else
  1178. follow_dotdot(nd);
  1179. }
  1180. return 0;
  1181. }
  1182. static void terminate_walk(struct nameidata *nd)
  1183. {
  1184. if (!(nd->flags & LOOKUP_RCU)) {
  1185. path_put(&nd->path);
  1186. } else {
  1187. nd->flags &= ~LOOKUP_RCU;
  1188. if (!(nd->flags & LOOKUP_ROOT))
  1189. nd->root.mnt = NULL;
  1190. rcu_read_unlock();
  1191. br_read_unlock(vfsmount_lock);
  1192. }
  1193. }
  1194. static inline int walk_component(struct nameidata *nd, struct path *path,
  1195. struct qstr *name, int type, int follow)
  1196. {
  1197. struct inode *inode;
  1198. int err;
  1199. /*
  1200. * "." and ".." are special - ".." especially so because it has
  1201. * to be able to know about the current root directory and
  1202. * parent relationships.
  1203. */
  1204. if (unlikely(type != LAST_NORM))
  1205. return handle_dots(nd, type);
  1206. err = do_lookup(nd, name, path, &inode);
  1207. if (unlikely(err)) {
  1208. terminate_walk(nd);
  1209. return err;
  1210. }
  1211. if (!inode) {
  1212. path_to_nameidata(path, nd);
  1213. terminate_walk(nd);
  1214. return -ENOENT;
  1215. }
  1216. if (unlikely(inode->i_op->follow_link) && follow) {
  1217. if (nameidata_dentry_drop_rcu_maybe(nd, path->dentry))
  1218. return -ECHILD;
  1219. BUG_ON(inode != path->dentry->d_inode);
  1220. return 1;
  1221. }
  1222. path_to_nameidata(path, nd);
  1223. nd->inode = inode;
  1224. return 0;
  1225. }
  1226. /*
  1227. * Name resolution.
  1228. * This is the basic name resolution function, turning a pathname into
  1229. * the final dentry. We expect 'base' to be positive and a directory.
  1230. *
  1231. * Returns 0 and nd will have valid dentry and mnt on success.
  1232. * Returns error and drops reference to input namei data on failure.
  1233. */
  1234. static int link_path_walk(const char *name, struct nameidata *nd)
  1235. {
  1236. struct path next;
  1237. int err;
  1238. unsigned int lookup_flags = nd->flags;
  1239. while (*name=='/')
  1240. name++;
  1241. if (!*name)
  1242. return 0;
  1243. if (nd->depth)
  1244. lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
  1245. /* At this point we know we have a real path component. */
  1246. for(;;) {
  1247. unsigned long hash;
  1248. struct qstr this;
  1249. unsigned int c;
  1250. int type;
  1251. nd->flags |= LOOKUP_CONTINUE;
  1252. err = may_lookup(nd);
  1253. if (err)
  1254. break;
  1255. this.name = name;
  1256. c = *(const unsigned char *)name;
  1257. hash = init_name_hash();
  1258. do {
  1259. name++;
  1260. hash = partial_name_hash(c, hash);
  1261. c = *(const unsigned char *)name;
  1262. } while (c && (c != '/'));
  1263. this.len = name - (const char *) this.name;
  1264. this.hash = end_name_hash(hash);
  1265. type = LAST_NORM;
  1266. if (this.name[0] == '.') switch (this.len) {
  1267. case 2:
  1268. if (this.name[1] == '.') {
  1269. type = LAST_DOTDOT;
  1270. nd->flags |= LOOKUP_JUMPED;
  1271. }
  1272. break;
  1273. case 1:
  1274. type = LAST_DOT;
  1275. }
  1276. if (likely(type == LAST_NORM)) {
  1277. struct dentry *parent = nd->path.dentry;
  1278. nd->flags &= ~LOOKUP_JUMPED;
  1279. if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
  1280. err = parent->d_op->d_hash(parent, nd->inode,
  1281. &this);
  1282. if (err < 0)
  1283. break;
  1284. }
  1285. }
  1286. /* remove trailing slashes? */
  1287. if (!c)
  1288. goto last_component;
  1289. while (*++name == '/');
  1290. if (!*name)
  1291. goto last_with_slashes;
  1292. err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW);
  1293. if (err < 0)
  1294. return err;
  1295. if (err) {
  1296. err = do_follow_link(&next, nd);
  1297. if (err)
  1298. return err;
  1299. }
  1300. err = -ENOTDIR;
  1301. if (!nd->inode->i_op->lookup)
  1302. break;
  1303. continue;
  1304. /* here ends the main loop */
  1305. last_with_slashes:
  1306. lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1307. last_component:
  1308. /* Clear LOOKUP_CONTINUE iff it was previously unset */
  1309. nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
  1310. if (lookup_flags & LOOKUP_PARENT) {
  1311. nd->last = this;
  1312. nd->last_type = type;
  1313. return 0;
  1314. }
  1315. err = walk_component(nd, &next, &this, type,
  1316. lookup_flags & LOOKUP_FOLLOW);
  1317. if (err < 0)
  1318. return err;
  1319. if (err) {
  1320. err = do_follow_link(&next, nd);
  1321. if (err)
  1322. return err;
  1323. }
  1324. if (lookup_flags & LOOKUP_DIRECTORY) {
  1325. err = -ENOTDIR;
  1326. if (!nd->inode->i_op->lookup)
  1327. break;
  1328. }
  1329. return 0;
  1330. }
  1331. terminate_walk(nd);
  1332. return err;
  1333. }
  1334. static int path_init(int dfd, const char *name, unsigned int flags,
  1335. struct nameidata *nd, struct file **fp)
  1336. {
  1337. int retval = 0;
  1338. int fput_needed;
  1339. struct file *file;
  1340. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  1341. nd->flags = flags | LOOKUP_JUMPED;
  1342. nd->depth = 0;
  1343. if (flags & LOOKUP_ROOT) {
  1344. struct inode *inode = nd->root.dentry->d_inode;
  1345. if (*name) {
  1346. if (!inode->i_op->lookup)
  1347. return -ENOTDIR;
  1348. retval = inode_permission(inode, MAY_EXEC);
  1349. if (retval)
  1350. return retval;
  1351. }
  1352. nd->path = nd->root;
  1353. nd->inode = inode;
  1354. if (flags & LOOKUP_RCU) {
  1355. br_read_lock(vfsmount_lock);
  1356. rcu_read_lock();
  1357. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1358. } else {
  1359. path_get(&nd->path);
  1360. }
  1361. return 0;
  1362. }
  1363. nd->root.mnt = NULL;
  1364. if (*name=='/') {
  1365. if (flags & LOOKUP_RCU) {
  1366. br_read_lock(vfsmount_lock);
  1367. rcu_read_lock();
  1368. set_root_rcu(nd);
  1369. } else {
  1370. set_root(nd);
  1371. path_get(&nd->root);
  1372. }
  1373. nd->path = nd->root;
  1374. } else if (dfd == AT_FDCWD) {
  1375. if (flags & LOOKUP_RCU) {
  1376. struct fs_struct *fs = current->fs;
  1377. unsigned seq;
  1378. br_read_lock(vfsmount_lock);
  1379. rcu_read_lock();
  1380. do {
  1381. seq = read_seqcount_begin(&fs->seq);
  1382. nd->path = fs->pwd;
  1383. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1384. } while (read_seqcount_retry(&fs->seq, seq));
  1385. } else {
  1386. get_fs_pwd(current->fs, &nd->path);
  1387. }
  1388. } else {
  1389. struct dentry *dentry;
  1390. file = fget_raw_light(dfd, &fput_needed);
  1391. retval = -EBADF;
  1392. if (!file)
  1393. goto out_fail;
  1394. dentry = file->f_path.dentry;
  1395. if (*name) {
  1396. retval = -ENOTDIR;
  1397. if (!S_ISDIR(dentry->d_inode->i_mode))
  1398. goto fput_fail;
  1399. retval = file_permission(file, MAY_EXEC);
  1400. if (retval)
  1401. goto fput_fail;
  1402. }
  1403. nd->path = file->f_path;
  1404. if (flags & LOOKUP_RCU) {
  1405. if (fput_needed)
  1406. *fp = file;
  1407. nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
  1408. br_read_lock(vfsmount_lock);
  1409. rcu_read_lock();
  1410. } else {
  1411. path_get(&file->f_path);
  1412. fput_light(file, fput_needed);
  1413. }
  1414. }
  1415. nd->inode = nd->path.dentry->d_inode;
  1416. return 0;
  1417. fput_fail:
  1418. fput_light(file, fput_needed);
  1419. out_fail:
  1420. return retval;
  1421. }
  1422. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  1423. static int path_lookupat(int dfd, const char *name,
  1424. unsigned int flags, struct nameidata *nd)
  1425. {
  1426. struct file *base = NULL;
  1427. int retval;
  1428. /*
  1429. * Path walking is largely split up into 2 different synchronisation
  1430. * schemes, rcu-walk and ref-walk (explained in
  1431. * Documentation/filesystems/path-lookup.txt). These share much of the
  1432. * path walk code, but some things particularly setup, cleanup, and
  1433. * following mounts are sufficiently divergent that functions are
  1434. * duplicated. Typically there is a function foo(), and its RCU
  1435. * analogue, foo_rcu().
  1436. *
  1437. * -ECHILD is the error number of choice (just to avoid clashes) that
  1438. * is returned if some aspect of an rcu-walk fails. Such an error must
  1439. * be handled by restarting a traditional ref-walk (which will always
  1440. * be able to complete).
  1441. */
  1442. retval = path_init(dfd, name, flags, nd, &base);
  1443. if (unlikely(retval))
  1444. return retval;
  1445. current->total_link_count = 0;
  1446. retval = link_path_walk(name, nd);
  1447. if (nd->flags & LOOKUP_RCU) {
  1448. /* went all way through without dropping RCU */
  1449. BUG_ON(retval);
  1450. if (nameidata_drop_rcu_last(nd))
  1451. retval = -ECHILD;
  1452. }
  1453. if (!retval)
  1454. retval = handle_reval_path(nd);
  1455. if (base)
  1456. fput(base);
  1457. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
  1458. path_put(&nd->root);
  1459. nd->root.mnt = NULL;
  1460. }
  1461. return retval;
  1462. }
  1463. static int do_path_lookup(int dfd, const char *name,
  1464. unsigned int flags, struct nameidata *nd)
  1465. {
  1466. int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd);
  1467. if (unlikely(retval == -ECHILD))
  1468. retval = path_lookupat(dfd, name, flags, nd);
  1469. if (unlikely(retval == -ESTALE))
  1470. retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd);
  1471. if (likely(!retval)) {
  1472. if (unlikely(!audit_dummy_context())) {
  1473. if (nd->path.dentry && nd->inode)
  1474. audit_inode(name, nd->path.dentry);
  1475. }
  1476. }
  1477. return retval;
  1478. }
  1479. int kern_path_parent(const char *name, struct nameidata *nd)
  1480. {
  1481. return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd);
  1482. }
  1483. int kern_path(const char *name, unsigned int flags, struct path *path)
  1484. {
  1485. struct nameidata nd;
  1486. int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
  1487. if (!res)
  1488. *path = nd.path;
  1489. return res;
  1490. }
  1491. /**
  1492. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  1493. * @dentry: pointer to dentry of the base directory
  1494. * @mnt: pointer to vfs mount of the base directory
  1495. * @name: pointer to file name
  1496. * @flags: lookup flags
  1497. * @nd: pointer to nameidata
  1498. */
  1499. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  1500. const char *name, unsigned int flags,
  1501. struct nameidata *nd)
  1502. {
  1503. nd->root.dentry = dentry;
  1504. nd->root.mnt = mnt;
  1505. /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */
  1506. return do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, nd);
  1507. }
  1508. static struct dentry *__lookup_hash(struct qstr *name,
  1509. struct dentry *base, struct nameidata *nd)
  1510. {
  1511. struct inode *inode = base->d_inode;
  1512. struct dentry *dentry;
  1513. int err;
  1514. err = exec_permission(inode, 0);
  1515. if (err)
  1516. return ERR_PTR(err);
  1517. /*
  1518. * Don't bother with __d_lookup: callers are for creat as
  1519. * well as unlink, so a lot of the time it would cost
  1520. * a double lookup.
  1521. */
  1522. dentry = d_lookup(base, name);
  1523. if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE))
  1524. dentry = do_revalidate(dentry, nd);
  1525. if (!dentry)
  1526. dentry = d_alloc_and_lookup(base, name, nd);
  1527. return dentry;
  1528. }
  1529. /*
  1530. * Restricted form of lookup. Doesn't follow links, single-component only,
  1531. * needs parent already locked. Doesn't follow mounts.
  1532. * SMP-safe.
  1533. */
  1534. static struct dentry *lookup_hash(struct nameidata *nd)
  1535. {
  1536. return __lookup_hash(&nd->last, nd->path.dentry, nd);
  1537. }
  1538. /**
  1539. * lookup_one_len - filesystem helper to lookup single pathname component
  1540. * @name: pathname component to lookup
  1541. * @base: base directory to lookup from
  1542. * @len: maximum length @len should be interpreted to
  1543. *
  1544. * Note that this routine is purely a helper for filesystem usage and should
  1545. * not be called by generic code. Also note that by using this function the
  1546. * nameidata argument is passed to the filesystem methods and a filesystem
  1547. * using this helper needs to be prepared for that.
  1548. */
  1549. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  1550. {
  1551. struct qstr this;
  1552. unsigned long hash;
  1553. unsigned int c;
  1554. WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
  1555. this.name = name;
  1556. this.len = len;
  1557. if (!len)
  1558. return ERR_PTR(-EACCES);
  1559. hash = init_name_hash();
  1560. while (len--) {
  1561. c = *(const unsigned char *)name++;
  1562. if (c == '/' || c == '\0')
  1563. return ERR_PTR(-EACCES);
  1564. hash = partial_name_hash(c, hash);
  1565. }
  1566. this.hash = end_name_hash(hash);
  1567. /*
  1568. * See if the low-level filesystem might want
  1569. * to use its own hash..
  1570. */
  1571. if (base->d_flags & DCACHE_OP_HASH) {
  1572. int err = base->d_op->d_hash(base, base->d_inode, &this);
  1573. if (err < 0)
  1574. return ERR_PTR(err);
  1575. }
  1576. return __lookup_hash(&this, base, NULL);
  1577. }
  1578. int user_path_at(int dfd, const char __user *name, unsigned flags,
  1579. struct path *path)
  1580. {
  1581. struct nameidata nd;
  1582. char *tmp = getname_flags(name, flags);
  1583. int err = PTR_ERR(tmp);
  1584. if (!IS_ERR(tmp)) {
  1585. BUG_ON(flags & LOOKUP_PARENT);
  1586. err = do_path_lookup(dfd, tmp, flags, &nd);
  1587. putname(tmp);
  1588. if (!err)
  1589. *path = nd.path;
  1590. }
  1591. return err;
  1592. }
  1593. static int user_path_parent(int dfd, const char __user *path,
  1594. struct nameidata *nd, char **name)
  1595. {
  1596. char *s = getname(path);
  1597. int error;
  1598. if (IS_ERR(s))
  1599. return PTR_ERR(s);
  1600. error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
  1601. if (error)
  1602. putname(s);
  1603. else
  1604. *name = s;
  1605. return error;
  1606. }
  1607. /*
  1608. * It's inline, so penalty for filesystems that don't use sticky bit is
  1609. * minimal.
  1610. */
  1611. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1612. {
  1613. uid_t fsuid = current_fsuid();
  1614. if (!(dir->i_mode & S_ISVTX))
  1615. return 0;
  1616. if (inode->i_uid == fsuid)
  1617. return 0;
  1618. if (dir->i_uid == fsuid)
  1619. return 0;
  1620. return !capable(CAP_FOWNER);
  1621. }
  1622. /*
  1623. * Check whether we can remove a link victim from directory dir, check
  1624. * whether the type of victim is right.
  1625. * 1. We can't do it if dir is read-only (done in permission())
  1626. * 2. We should have write and exec permissions on dir
  1627. * 3. We can't remove anything from append-only dir
  1628. * 4. We can't do anything with immutable dir (done in permission())
  1629. * 5. If the sticky bit on dir is set we should either
  1630. * a. be owner of dir, or
  1631. * b. be owner of victim, or
  1632. * c. have CAP_FOWNER capability
  1633. * 6. If the victim is append-only or immutable we can't do antyhing with
  1634. * links pointing to it.
  1635. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1636. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1637. * 9. We can't remove a root or mountpoint.
  1638. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1639. * nfs_async_unlink().
  1640. */
  1641. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1642. {
  1643. int error;
  1644. if (!victim->d_inode)
  1645. return -ENOENT;
  1646. BUG_ON(victim->d_parent->d_inode != dir);
  1647. audit_inode_child(victim, dir);
  1648. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1649. if (error)
  1650. return error;
  1651. if (IS_APPEND(dir))
  1652. return -EPERM;
  1653. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1654. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  1655. return -EPERM;
  1656. if (isdir) {
  1657. if (!S_ISDIR(victim->d_inode->i_mode))
  1658. return -ENOTDIR;
  1659. if (IS_ROOT(victim))
  1660. return -EBUSY;
  1661. } else if (S_ISDIR(victim->d_inode->i_mode))
  1662. return -EISDIR;
  1663. if (IS_DEADDIR(dir))
  1664. return -ENOENT;
  1665. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1666. return -EBUSY;
  1667. return 0;
  1668. }
  1669. /* Check whether we can create an object with dentry child in directory
  1670. * dir.
  1671. * 1. We can't do it if child already exists (open has special treatment for
  1672. * this case, but since we are inlined it's OK)
  1673. * 2. We can't do it if dir is read-only (done in permission())
  1674. * 3. We should have write and exec permissions on dir
  1675. * 4. We can't do it if dir is immutable (done in permission())
  1676. */
  1677. static inline int may_create(struct inode *dir, struct dentry *child)
  1678. {
  1679. if (child->d_inode)
  1680. return -EEXIST;
  1681. if (IS_DEADDIR(dir))
  1682. return -ENOENT;
  1683. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1684. }
  1685. /*
  1686. * p1 and p2 should be directories on the same fs.
  1687. */
  1688. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1689. {
  1690. struct dentry *p;
  1691. if (p1 == p2) {
  1692. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1693. return NULL;
  1694. }
  1695. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1696. p = d_ancestor(p2, p1);
  1697. if (p) {
  1698. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  1699. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  1700. return p;
  1701. }
  1702. p = d_ancestor(p1, p2);
  1703. if (p) {
  1704. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1705. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1706. return p;
  1707. }
  1708. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1709. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1710. return NULL;
  1711. }
  1712. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1713. {
  1714. mutex_unlock(&p1->d_inode->i_mutex);
  1715. if (p1 != p2) {
  1716. mutex_unlock(&p2->d_inode->i_mutex);
  1717. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1718. }
  1719. }
  1720. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1721. struct nameidata *nd)
  1722. {
  1723. int error = may_create(dir, dentry);
  1724. if (error)
  1725. return error;
  1726. if (!dir->i_op->create)
  1727. return -EACCES; /* shouldn't it be ENOSYS? */
  1728. mode &= S_IALLUGO;
  1729. mode |= S_IFREG;
  1730. error = security_inode_create(dir, dentry, mode);
  1731. if (error)
  1732. return error;
  1733. error = dir->i_op->create(dir, dentry, mode, nd);
  1734. if (!error)
  1735. fsnotify_create(dir, dentry);
  1736. return error;
  1737. }
  1738. static int may_open(struct path *path, int acc_mode, int flag)
  1739. {
  1740. struct dentry *dentry = path->dentry;
  1741. struct inode *inode = dentry->d_inode;
  1742. int error;
  1743. /* O_PATH? */
  1744. if (!acc_mode)
  1745. return 0;
  1746. if (!inode)
  1747. return -ENOENT;
  1748. switch (inode->i_mode & S_IFMT) {
  1749. case S_IFLNK:
  1750. return -ELOOP;
  1751. case S_IFDIR:
  1752. if (acc_mode & MAY_WRITE)
  1753. return -EISDIR;
  1754. break;
  1755. case S_IFBLK:
  1756. case S_IFCHR:
  1757. if (path->mnt->mnt_flags & MNT_NODEV)
  1758. return -EACCES;
  1759. /*FALLTHRU*/
  1760. case S_IFIFO:
  1761. case S_IFSOCK:
  1762. flag &= ~O_TRUNC;
  1763. break;
  1764. }
  1765. error = inode_permission(inode, acc_mode);
  1766. if (error)
  1767. return error;
  1768. /*
  1769. * An append-only file must be opened in append mode for writing.
  1770. */
  1771. if (IS_APPEND(inode)) {
  1772. if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
  1773. return -EPERM;
  1774. if (flag & O_TRUNC)
  1775. return -EPERM;
  1776. }
  1777. /* O_NOATIME can only be set by the owner or superuser */
  1778. if (flag & O_NOATIME && !is_owner_or_cap(inode))
  1779. return -EPERM;
  1780. /*
  1781. * Ensure there are no outstanding leases on the file.
  1782. */
  1783. return break_lease(inode, flag);
  1784. }
  1785. static int handle_truncate(struct file *filp)
  1786. {
  1787. struct path *path = &filp->f_path;
  1788. struct inode *inode = path->dentry->d_inode;
  1789. int error = get_write_access(inode);
  1790. if (error)
  1791. return error;
  1792. /*
  1793. * Refuse to truncate files with mandatory locks held on them.
  1794. */
  1795. error = locks_verify_locked(inode);
  1796. if (!error)
  1797. error = security_path_truncate(path);
  1798. if (!error) {
  1799. error = do_truncate(path->dentry, 0,
  1800. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  1801. filp);
  1802. }
  1803. put_write_access(inode);
  1804. return error;
  1805. }
  1806. /*
  1807. * Note that while the flag value (low two bits) for sys_open means:
  1808. * 00 - read-only
  1809. * 01 - write-only
  1810. * 10 - read-write
  1811. * 11 - special
  1812. * it is changed into
  1813. * 00 - no permissions needed
  1814. * 01 - read-permission
  1815. * 10 - write-permission
  1816. * 11 - read-write
  1817. * for the internal routines (ie open_namei()/follow_link() etc)
  1818. * This is more logical, and also allows the 00 "no perm needed"
  1819. * to be used for symlinks (where the permissions are checked
  1820. * later).
  1821. *
  1822. */
  1823. static inline int open_to_namei_flags(int flag)
  1824. {
  1825. if ((flag+1) & O_ACCMODE)
  1826. flag++;
  1827. return flag;
  1828. }
  1829. /*
  1830. * Handle the last step of open()
  1831. */
  1832. static struct file *do_last(struct nameidata *nd, struct path *path,
  1833. const struct open_flags *op, const char *pathname)
  1834. {
  1835. struct dentry *dir = nd->path.dentry;
  1836. struct dentry *dentry;
  1837. int open_flag = op->open_flag;
  1838. int will_truncate = open_flag & O_TRUNC;
  1839. int want_write = 0;
  1840. int acc_mode = op->acc_mode;
  1841. struct file *filp;
  1842. int error;
  1843. nd->flags &= ~LOOKUP_PARENT;
  1844. nd->flags |= op->intent;
  1845. switch (nd->last_type) {
  1846. case LAST_DOTDOT:
  1847. case LAST_DOT:
  1848. error = handle_dots(nd, nd->last_type);
  1849. if (error)
  1850. return ERR_PTR(error);
  1851. /* fallthrough */
  1852. case LAST_ROOT:
  1853. if (nd->flags & LOOKUP_RCU) {
  1854. if (nameidata_drop_rcu_last(nd))
  1855. return ERR_PTR(-ECHILD);
  1856. }
  1857. error = handle_reval_path(nd);
  1858. if (error)
  1859. goto exit;
  1860. audit_inode(pathname, nd->path.dentry);
  1861. if (open_flag & O_CREAT) {
  1862. error = -EISDIR;
  1863. goto exit;
  1864. }
  1865. goto ok;
  1866. case LAST_BIND:
  1867. /* can't be RCU mode here */
  1868. error = handle_reval_path(nd);
  1869. if (error)
  1870. goto exit;
  1871. audit_inode(pathname, dir);
  1872. goto ok;
  1873. }
  1874. if (!(open_flag & O_CREAT)) {
  1875. int symlink_ok = 0;
  1876. if (nd->last.name[nd->last.len])
  1877. nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  1878. if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW))
  1879. symlink_ok = 1;
  1880. /* we _can_ be in RCU mode here */
  1881. error = walk_component(nd, path, &nd->last, LAST_NORM,
  1882. !symlink_ok);
  1883. if (error < 0)
  1884. return ERR_PTR(error);
  1885. if (error) /* symlink */
  1886. return NULL;
  1887. /* sayonara */
  1888. if (nd->flags & LOOKUP_RCU) {
  1889. if (nameidata_drop_rcu_last(nd))
  1890. return ERR_PTR(-ECHILD);
  1891. }
  1892. error = -ENOTDIR;
  1893. if (nd->flags & LOOKUP_DIRECTORY) {
  1894. if (!nd->inode->i_op->lookup)
  1895. goto exit;
  1896. }
  1897. audit_inode(pathname, nd->path.dentry);
  1898. goto ok;
  1899. }
  1900. /* create side of things */
  1901. if (nd->flags & LOOKUP_RCU) {
  1902. if (nameidata_drop_rcu_last(nd))
  1903. return ERR_PTR(-ECHILD);
  1904. }
  1905. audit_inode(pathname, dir);
  1906. error = -EISDIR;
  1907. /* trailing slashes? */
  1908. if (nd->last.name[nd->last.len])
  1909. goto exit;
  1910. mutex_lock(&dir->d_inode->i_mutex);
  1911. dentry = lookup_hash(nd);
  1912. error = PTR_ERR(dentry);
  1913. if (IS_ERR(dentry)) {
  1914. mutex_unlock(&dir->d_inode->i_mutex);
  1915. goto exit;
  1916. }
  1917. path->dentry = dentry;
  1918. path->mnt = nd->path.mnt;
  1919. /* Negative dentry, just create the file */
  1920. if (!dentry->d_inode) {
  1921. int mode = op->mode;
  1922. if (!IS_POSIXACL(dir->d_inode))
  1923. mode &= ~current_umask();
  1924. /*
  1925. * This write is needed to ensure that a
  1926. * rw->ro transition does not occur between
  1927. * the time when the file is created and when
  1928. * a permanent write count is taken through
  1929. * the 'struct file' in nameidata_to_filp().
  1930. */
  1931. error = mnt_want_write(nd->path.mnt);
  1932. if (error)
  1933. goto exit_mutex_unlock;
  1934. want_write = 1;
  1935. /* Don't check for write permission, don't truncate */
  1936. open_flag &= ~O_TRUNC;
  1937. will_truncate = 0;
  1938. acc_mode = MAY_OPEN;
  1939. error = security_path_mknod(&nd->path, dentry, mode, 0);
  1940. if (error)
  1941. goto exit_mutex_unlock;
  1942. error = vfs_create(dir->d_inode, dentry, mode, nd);
  1943. if (error)
  1944. goto exit_mutex_unlock;
  1945. mutex_unlock(&dir->d_inode->i_mutex);
  1946. dput(nd->path.dentry);
  1947. nd->path.dentry = dentry;
  1948. goto common;
  1949. }
  1950. /*
  1951. * It already exists.
  1952. */
  1953. mutex_unlock(&dir->d_inode->i_mutex);
  1954. audit_inode(pathname, path->dentry);
  1955. error = -EEXIST;
  1956. if (open_flag & O_EXCL)
  1957. goto exit_dput;
  1958. error = follow_managed(path, nd->flags);
  1959. if (error < 0)
  1960. goto exit_dput;
  1961. error = -ENOENT;
  1962. if (!path->dentry->d_inode)
  1963. goto exit_dput;
  1964. if (path->dentry->d_inode->i_op->follow_link)
  1965. return NULL;
  1966. path_to_nameidata(path, nd);
  1967. nd->inode = path->dentry->d_inode;
  1968. error = -EISDIR;
  1969. if (S_ISDIR(nd->inode->i_mode))
  1970. goto exit;
  1971. ok:
  1972. if (!S_ISREG(nd->inode->i_mode))
  1973. will_truncate = 0;
  1974. if (will_truncate) {
  1975. error = mnt_want_write(nd->path.mnt);
  1976. if (error)
  1977. goto exit;
  1978. want_write = 1;
  1979. }
  1980. common:
  1981. error = may_open(&nd->path, acc_mode, open_flag);
  1982. if (error)
  1983. goto exit;
  1984. filp = nameidata_to_filp(nd);
  1985. if (!IS_ERR(filp)) {
  1986. error = ima_file_check(filp, op->acc_mode);
  1987. if (error) {
  1988. fput(filp);
  1989. filp = ERR_PTR(error);
  1990. }
  1991. }
  1992. if (!IS_ERR(filp)) {
  1993. if (will_truncate) {
  1994. error = handle_truncate(filp);
  1995. if (error) {
  1996. fput(filp);
  1997. filp = ERR_PTR(error);
  1998. }
  1999. }
  2000. }
  2001. out:
  2002. if (want_write)
  2003. mnt_drop_write(nd->path.mnt);
  2004. path_put(&nd->path);
  2005. return filp;
  2006. exit_mutex_unlock:
  2007. mutex_unlock(&dir->d_inode->i_mutex);
  2008. exit_dput:
  2009. path_put_conditional(path, nd);
  2010. exit:
  2011. filp = ERR_PTR(error);
  2012. goto out;
  2013. }
  2014. static struct file *path_openat(int dfd, const char *pathname,
  2015. struct nameidata *nd, const struct open_flags *op, int flags)
  2016. {
  2017. struct file *base = NULL;
  2018. struct file *filp;
  2019. struct path path;
  2020. int count = 0;
  2021. int error;
  2022. filp = get_empty_filp();
  2023. if (!filp)
  2024. return ERR_PTR(-ENFILE);
  2025. filp->f_flags = op->open_flag;
  2026. nd->intent.open.file = filp;
  2027. nd->intent.open.flags = open_to_namei_flags(op->open_flag);
  2028. nd->intent.open.create_mode = op->mode;
  2029. error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base);
  2030. if (unlikely(error))
  2031. goto out_filp;
  2032. current->total_link_count = 0;
  2033. error = link_path_walk(pathname, nd);
  2034. if (unlikely(error))
  2035. goto out_filp;
  2036. filp = do_last(nd, &path, op, pathname);
  2037. while (unlikely(!filp)) { /* trailing symlink */
  2038. struct path link = path;
  2039. struct inode *linki = link.dentry->d_inode;
  2040. void *cookie;
  2041. if (!(nd->flags & LOOKUP_FOLLOW) || count++ == 32) {
  2042. path_put_conditional(&path, nd);
  2043. path_put(&nd->path);
  2044. filp = ERR_PTR(-ELOOP);
  2045. break;
  2046. }
  2047. /*
  2048. * This is subtle. Instead of calling do_follow_link() we do
  2049. * the thing by hands. The reason is that this way we have zero
  2050. * link_count and path_walk() (called from ->follow_link)
  2051. * honoring LOOKUP_PARENT. After that we have the parent and
  2052. * last component, i.e. we are in the same situation as after
  2053. * the first path_walk(). Well, almost - if the last component
  2054. * is normal we get its copy stored in nd->last.name and we will
  2055. * have to putname() it when we are done. Procfs-like symlinks
  2056. * just set LAST_BIND.
  2057. */
  2058. nd->flags |= LOOKUP_PARENT;
  2059. nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
  2060. error = __do_follow_link(&link, nd, &cookie);
  2061. if (unlikely(error))
  2062. filp = ERR_PTR(error);
  2063. else
  2064. filp = do_last(nd, &path, op, pathname);
  2065. if (!IS_ERR(cookie) && linki->i_op->put_link)
  2066. linki->i_op->put_link(link.dentry, nd, cookie);
  2067. path_put(&link);
  2068. }
  2069. out:
  2070. if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT))
  2071. path_put(&nd->root);
  2072. if (base)
  2073. fput(base);
  2074. release_open_intent(nd);
  2075. return filp;
  2076. out_filp:
  2077. filp = ERR_PTR(error);
  2078. goto out;
  2079. }
  2080. struct file *do_filp_open(int dfd, const char *pathname,
  2081. const struct open_flags *op, int flags)
  2082. {
  2083. struct nameidata nd;
  2084. struct file *filp;
  2085. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU);
  2086. if (unlikely(filp == ERR_PTR(-ECHILD)))
  2087. filp = path_openat(dfd, pathname, &nd, op, flags);
  2088. if (unlikely(filp == ERR_PTR(-ESTALE)))
  2089. filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL);
  2090. return filp;
  2091. }
  2092. struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  2093. const char *name, const struct open_flags *op, int flags)
  2094. {
  2095. struct nameidata nd;
  2096. struct file *file;
  2097. nd.root.mnt = mnt;
  2098. nd.root.dentry = dentry;
  2099. flags |= LOOKUP_ROOT;
  2100. if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN)
  2101. return ERR_PTR(-ELOOP);
  2102. file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU);
  2103. if (unlikely(file == ERR_PTR(-ECHILD)))
  2104. file = path_openat(-1, name, &nd, op, flags);
  2105. if (unlikely(file == ERR_PTR(-ESTALE)))
  2106. file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL);
  2107. return file;
  2108. }
  2109. /**
  2110. * lookup_create - lookup a dentry, creating it if it doesn't exist
  2111. * @nd: nameidata info
  2112. * @is_dir: directory flag
  2113. *
  2114. * Simple function to lookup and return a dentry and create it
  2115. * if it doesn't exist. Is SMP-safe.
  2116. *
  2117. * Returns with nd->path.dentry->d_inode->i_mutex locked.
  2118. */
  2119. struct dentry *lookup_create(struct nameidata *nd, int is_dir)
  2120. {
  2121. struct dentry *dentry = ERR_PTR(-EEXIST);
  2122. mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2123. /*
  2124. * Yucky last component or no last component at all?
  2125. * (foo/., foo/.., /////)
  2126. */
  2127. if (nd->last_type != LAST_NORM)
  2128. goto fail;
  2129. nd->flags &= ~LOOKUP_PARENT;
  2130. nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  2131. nd->intent.open.flags = O_EXCL;
  2132. /*
  2133. * Do the final lookup.
  2134. */
  2135. dentry = lookup_hash(nd);
  2136. if (IS_ERR(dentry))
  2137. goto fail;
  2138. if (dentry->d_inode)
  2139. goto eexist;
  2140. /*
  2141. * Special case - lookup gave negative, but... we had foo/bar/
  2142. * From the vfs_mknod() POV we just have a negative dentry -
  2143. * all is fine. Let's be bastards - you had / on the end, you've
  2144. * been asking for (non-existent) directory. -ENOENT for you.
  2145. */
  2146. if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
  2147. dput(dentry);
  2148. dentry = ERR_PTR(-ENOENT);
  2149. }
  2150. return dentry;
  2151. eexist:
  2152. dput(dentry);
  2153. dentry = ERR_PTR(-EEXIST);
  2154. fail:
  2155. return dentry;
  2156. }
  2157. EXPORT_SYMBOL_GPL(lookup_create);
  2158. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  2159. {
  2160. int error = may_create(dir, dentry);
  2161. if (error)
  2162. return error;
  2163. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  2164. return -EPERM;
  2165. if (!dir->i_op->mknod)
  2166. return -EPERM;
  2167. error = devcgroup_inode_mknod(mode, dev);
  2168. if (error)
  2169. return error;
  2170. error = security_inode_mknod(dir, dentry, mode, dev);
  2171. if (error)
  2172. return error;
  2173. error = dir->i_op->mknod(dir, dentry, mode, dev);
  2174. if (!error)
  2175. fsnotify_create(dir, dentry);
  2176. return error;
  2177. }
  2178. static int may_mknod(mode_t mode)
  2179. {
  2180. switch (mode & S_IFMT) {
  2181. case S_IFREG:
  2182. case S_IFCHR:
  2183. case S_IFBLK:
  2184. case S_IFIFO:
  2185. case S_IFSOCK:
  2186. case 0: /* zero mode translates to S_IFREG */
  2187. return 0;
  2188. case S_IFDIR:
  2189. return -EPERM;
  2190. default:
  2191. return -EINVAL;
  2192. }
  2193. }
  2194. SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
  2195. unsigned, dev)
  2196. {
  2197. int error;
  2198. char *tmp;
  2199. struct dentry *dentry;
  2200. struct nameidata nd;
  2201. if (S_ISDIR(mode))
  2202. return -EPERM;
  2203. error = user_path_parent(dfd, filename, &nd, &tmp);
  2204. if (error)
  2205. return error;
  2206. dentry = lookup_create(&nd, 0);
  2207. if (IS_ERR(dentry)) {
  2208. error = PTR_ERR(dentry);
  2209. goto out_unlock;
  2210. }
  2211. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  2212. mode &= ~current_umask();
  2213. error = may_mknod(mode);
  2214. if (error)
  2215. goto out_dput;
  2216. error = mnt_want_write(nd.path.mnt);
  2217. if (error)
  2218. goto out_dput;
  2219. error = security_path_mknod(&nd.path, dentry, mode, dev);
  2220. if (error)
  2221. goto out_drop_write;
  2222. switch (mode & S_IFMT) {
  2223. case 0: case S_IFREG:
  2224. error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
  2225. break;
  2226. case S_IFCHR: case S_IFBLK:
  2227. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
  2228. new_decode_dev(dev));
  2229. break;
  2230. case S_IFIFO: case S_IFSOCK:
  2231. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
  2232. break;
  2233. }
  2234. out_drop_write:
  2235. mnt_drop_write(nd.path.mnt);
  2236. out_dput:
  2237. dput(dentry);
  2238. out_unlock:
  2239. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2240. path_put(&nd.path);
  2241. putname(tmp);
  2242. return error;
  2243. }
  2244. SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
  2245. {
  2246. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  2247. }
  2248. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  2249. {
  2250. int error = may_create(dir, dentry);
  2251. if (error)
  2252. return error;
  2253. if (!dir->i_op->mkdir)
  2254. return -EPERM;
  2255. mode &= (S_IRWXUGO|S_ISVTX);
  2256. error = security_inode_mkdir(dir, dentry, mode);
  2257. if (error)
  2258. return error;
  2259. error = dir->i_op->mkdir(dir, dentry, mode);
  2260. if (!error)
  2261. fsnotify_mkdir(dir, dentry);
  2262. return error;
  2263. }
  2264. SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
  2265. {
  2266. int error = 0;
  2267. char * tmp;
  2268. struct dentry *dentry;
  2269. struct nameidata nd;
  2270. error = user_path_parent(dfd, pathname, &nd, &tmp);
  2271. if (error)
  2272. goto out_err;
  2273. dentry = lookup_create(&nd, 1);
  2274. error = PTR_ERR(dentry);
  2275. if (IS_ERR(dentry))
  2276. goto out_unlock;
  2277. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  2278. mode &= ~current_umask();
  2279. error = mnt_want_write(nd.path.mnt);
  2280. if (error)
  2281. goto out_dput;
  2282. error = security_path_mkdir(&nd.path, dentry, mode);
  2283. if (error)
  2284. goto out_drop_write;
  2285. error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
  2286. out_drop_write:
  2287. mnt_drop_write(nd.path.mnt);
  2288. out_dput:
  2289. dput(dentry);
  2290. out_unlock:
  2291. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2292. path_put(&nd.path);
  2293. putname(tmp);
  2294. out_err:
  2295. return error;
  2296. }
  2297. SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
  2298. {
  2299. return sys_mkdirat(AT_FDCWD, pathname, mode);
  2300. }
  2301. /*
  2302. * We try to drop the dentry early: we should have
  2303. * a usage count of 2 if we're the only user of this
  2304. * dentry, and if that is true (possibly after pruning
  2305. * the dcache), then we drop the dentry now.
  2306. *
  2307. * A low-level filesystem can, if it choses, legally
  2308. * do a
  2309. *
  2310. * if (!d_unhashed(dentry))
  2311. * return -EBUSY;
  2312. *
  2313. * if it cannot handle the case of removing a directory
  2314. * that is still in use by something else..
  2315. */
  2316. void dentry_unhash(struct dentry *dentry)
  2317. {
  2318. dget(dentry);
  2319. shrink_dcache_parent(dentry);
  2320. spin_lock(&dentry->d_lock);
  2321. if (dentry->d_count == 2)
  2322. __d_drop(dentry);
  2323. spin_unlock(&dentry->d_lock);
  2324. }
  2325. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  2326. {
  2327. int error = may_delete(dir, dentry, 1);
  2328. if (error)
  2329. return error;
  2330. if (!dir->i_op->rmdir)
  2331. return -EPERM;
  2332. mutex_lock(&dentry->d_inode->i_mutex);
  2333. dentry_unhash(dentry);
  2334. if (d_mountpoint(dentry))
  2335. error = -EBUSY;
  2336. else {
  2337. error = security_inode_rmdir(dir, dentry);
  2338. if (!error) {
  2339. error = dir->i_op->rmdir(dir, dentry);
  2340. if (!error) {
  2341. dentry->d_inode->i_flags |= S_DEAD;
  2342. dont_mount(dentry);
  2343. }
  2344. }
  2345. }
  2346. mutex_unlock(&dentry->d_inode->i_mutex);
  2347. if (!error) {
  2348. d_delete(dentry);
  2349. }
  2350. dput(dentry);
  2351. return error;
  2352. }
  2353. static long do_rmdir(int dfd, const char __user *pathname)
  2354. {
  2355. int error = 0;
  2356. char * name;
  2357. struct dentry *dentry;
  2358. struct nameidata nd;
  2359. error = user_path_parent(dfd, pathname, &nd, &name);
  2360. if (error)
  2361. return error;
  2362. switch(nd.last_type) {
  2363. case LAST_DOTDOT:
  2364. error = -ENOTEMPTY;
  2365. goto exit1;
  2366. case LAST_DOT:
  2367. error = -EINVAL;
  2368. goto exit1;
  2369. case LAST_ROOT:
  2370. error = -EBUSY;
  2371. goto exit1;
  2372. }
  2373. nd.flags &= ~LOOKUP_PARENT;
  2374. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2375. dentry = lookup_hash(&nd);
  2376. error = PTR_ERR(dentry);
  2377. if (IS_ERR(dentry))
  2378. goto exit2;
  2379. error = mnt_want_write(nd.path.mnt);
  2380. if (error)
  2381. goto exit3;
  2382. error = security_path_rmdir(&nd.path, dentry);
  2383. if (error)
  2384. goto exit4;
  2385. error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
  2386. exit4:
  2387. mnt_drop_write(nd.path.mnt);
  2388. exit3:
  2389. dput(dentry);
  2390. exit2:
  2391. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2392. exit1:
  2393. path_put(&nd.path);
  2394. putname(name);
  2395. return error;
  2396. }
  2397. SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
  2398. {
  2399. return do_rmdir(AT_FDCWD, pathname);
  2400. }
  2401. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  2402. {
  2403. int error = may_delete(dir, dentry, 0);
  2404. if (error)
  2405. return error;
  2406. if (!dir->i_op->unlink)
  2407. return -EPERM;
  2408. mutex_lock(&dentry->d_inode->i_mutex);
  2409. if (d_mountpoint(dentry))
  2410. error = -EBUSY;
  2411. else {
  2412. error = security_inode_unlink(dir, dentry);
  2413. if (!error) {
  2414. error = dir->i_op->unlink(dir, dentry);
  2415. if (!error)
  2416. dont_mount(dentry);
  2417. }
  2418. }
  2419. mutex_unlock(&dentry->d_inode->i_mutex);
  2420. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  2421. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  2422. fsnotify_link_count(dentry->d_inode);
  2423. d_delete(dentry);
  2424. }
  2425. return error;
  2426. }
  2427. /*
  2428. * Make sure that the actual truncation of the file will occur outside its
  2429. * directory's i_mutex. Truncate can take a long time if there is a lot of
  2430. * writeout happening, and we don't want to prevent access to the directory
  2431. * while waiting on the I/O.
  2432. */
  2433. static long do_unlinkat(int dfd, const char __user *pathname)
  2434. {
  2435. int error;
  2436. char *name;
  2437. struct dentry *dentry;
  2438. struct nameidata nd;
  2439. struct inode *inode = NULL;
  2440. error = user_path_parent(dfd, pathname, &nd, &name);
  2441. if (error)
  2442. return error;
  2443. error = -EISDIR;
  2444. if (nd.last_type != LAST_NORM)
  2445. goto exit1;
  2446. nd.flags &= ~LOOKUP_PARENT;
  2447. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2448. dentry = lookup_hash(&nd);
  2449. error = PTR_ERR(dentry);
  2450. if (!IS_ERR(dentry)) {
  2451. /* Why not before? Because we want correct error value */
  2452. if (nd.last.name[nd.last.len])
  2453. goto slashes;
  2454. inode = dentry->d_inode;
  2455. if (inode)
  2456. ihold(inode);
  2457. error = mnt_want_write(nd.path.mnt);
  2458. if (error)
  2459. goto exit2;
  2460. error = security_path_unlink(&nd.path, dentry);
  2461. if (error)
  2462. goto exit3;
  2463. error = vfs_unlink(nd.path.dentry->d_inode, dentry);
  2464. exit3:
  2465. mnt_drop_write(nd.path.mnt);
  2466. exit2:
  2467. dput(dentry);
  2468. }
  2469. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2470. if (inode)
  2471. iput(inode); /* truncate the inode here */
  2472. exit1:
  2473. path_put(&nd.path);
  2474. putname(name);
  2475. return error;
  2476. slashes:
  2477. error = !dentry->d_inode ? -ENOENT :
  2478. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  2479. goto exit2;
  2480. }
  2481. SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
  2482. {
  2483. if ((flag & ~AT_REMOVEDIR) != 0)
  2484. return -EINVAL;
  2485. if (flag & AT_REMOVEDIR)
  2486. return do_rmdir(dfd, pathname);
  2487. return do_unlinkat(dfd, pathname);
  2488. }
  2489. SYSCALL_DEFINE1(unlink, const char __user *, pathname)
  2490. {
  2491. return do_unlinkat(AT_FDCWD, pathname);
  2492. }
  2493. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  2494. {
  2495. int error = may_create(dir, dentry);
  2496. if (error)
  2497. return error;
  2498. if (!dir->i_op->symlink)
  2499. return -EPERM;
  2500. error = security_inode_symlink(dir, dentry, oldname);
  2501. if (error)
  2502. return error;
  2503. error = dir->i_op->symlink(dir, dentry, oldname);
  2504. if (!error)
  2505. fsnotify_create(dir, dentry);
  2506. return error;
  2507. }
  2508. SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  2509. int, newdfd, const char __user *, newname)
  2510. {
  2511. int error;
  2512. char *from;
  2513. char *to;
  2514. struct dentry *dentry;
  2515. struct nameidata nd;
  2516. from = getname(oldname);
  2517. if (IS_ERR(from))
  2518. return PTR_ERR(from);
  2519. error = user_path_parent(newdfd, newname, &nd, &to);
  2520. if (error)
  2521. goto out_putname;
  2522. dentry = lookup_create(&nd, 0);
  2523. error = PTR_ERR(dentry);
  2524. if (IS_ERR(dentry))
  2525. goto out_unlock;
  2526. error = mnt_want_write(nd.path.mnt);
  2527. if (error)
  2528. goto out_dput;
  2529. error = security_path_symlink(&nd.path, dentry, from);
  2530. if (error)
  2531. goto out_drop_write;
  2532. error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
  2533. out_drop_write:
  2534. mnt_drop_write(nd.path.mnt);
  2535. out_dput:
  2536. dput(dentry);
  2537. out_unlock:
  2538. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2539. path_put(&nd.path);
  2540. putname(to);
  2541. out_putname:
  2542. putname(from);
  2543. return error;
  2544. }
  2545. SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
  2546. {
  2547. return sys_symlinkat(oldname, AT_FDCWD, newname);
  2548. }
  2549. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  2550. {
  2551. struct inode *inode = old_dentry->d_inode;
  2552. int error;
  2553. if (!inode)
  2554. return -ENOENT;
  2555. error = may_create(dir, new_dentry);
  2556. if (error)
  2557. return error;
  2558. if (dir->i_sb != inode->i_sb)
  2559. return -EXDEV;
  2560. /*
  2561. * A link to an append-only or immutable file cannot be created.
  2562. */
  2563. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2564. return -EPERM;
  2565. if (!dir->i_op->link)
  2566. return -EPERM;
  2567. if (S_ISDIR(inode->i_mode))
  2568. return -EPERM;
  2569. error = security_inode_link(old_dentry, dir, new_dentry);
  2570. if (error)
  2571. return error;
  2572. mutex_lock(&inode->i_mutex);
  2573. /* Make sure we don't allow creating hardlink to an unlinked file */
  2574. if (inode->i_nlink == 0)
  2575. error = -ENOENT;
  2576. else
  2577. error = dir->i_op->link(old_dentry, dir, new_dentry);
  2578. mutex_unlock(&inode->i_mutex);
  2579. if (!error)
  2580. fsnotify_link(dir, inode, new_dentry);
  2581. return error;
  2582. }
  2583. /*
  2584. * Hardlinks are often used in delicate situations. We avoid
  2585. * security-related surprises by not following symlinks on the
  2586. * newname. --KAB
  2587. *
  2588. * We don't follow them on the oldname either to be compatible
  2589. * with linux 2.0, and to avoid hard-linking to directories
  2590. * and other special files. --ADM
  2591. */
  2592. SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  2593. int, newdfd, const char __user *, newname, int, flags)
  2594. {
  2595. struct dentry *new_dentry;
  2596. struct nameidata nd;
  2597. struct path old_path;
  2598. int how = 0;
  2599. int error;
  2600. char *to;
  2601. if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  2602. return -EINVAL;
  2603. /*
  2604. * To use null names we require CAP_DAC_READ_SEARCH
  2605. * This ensures that not everyone will be able to create
  2606. * handlink using the passed filedescriptor.
  2607. */
  2608. if (flags & AT_EMPTY_PATH) {
  2609. if (!capable(CAP_DAC_READ_SEARCH))
  2610. return -ENOENT;
  2611. how = LOOKUP_EMPTY;
  2612. }
  2613. if (flags & AT_SYMLINK_FOLLOW)
  2614. how |= LOOKUP_FOLLOW;
  2615. error = user_path_at(olddfd, oldname, how, &old_path);
  2616. if (error)
  2617. return error;
  2618. error = user_path_parent(newdfd, newname, &nd, &to);
  2619. if (error)
  2620. goto out;
  2621. error = -EXDEV;
  2622. if (old_path.mnt != nd.path.mnt)
  2623. goto out_release;
  2624. new_dentry = lookup_create(&nd, 0);
  2625. error = PTR_ERR(new_dentry);
  2626. if (IS_ERR(new_dentry))
  2627. goto out_unlock;
  2628. error = mnt_want_write(nd.path.mnt);
  2629. if (error)
  2630. goto out_dput;
  2631. error = security_path_link(old_path.dentry, &nd.path, new_dentry);
  2632. if (error)
  2633. goto out_drop_write;
  2634. error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
  2635. out_drop_write:
  2636. mnt_drop_write(nd.path.mnt);
  2637. out_dput:
  2638. dput(new_dentry);
  2639. out_unlock:
  2640. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2641. out_release:
  2642. path_put(&nd.path);
  2643. putname(to);
  2644. out:
  2645. path_put(&old_path);
  2646. return error;
  2647. }
  2648. SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
  2649. {
  2650. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  2651. }
  2652. /*
  2653. * The worst of all namespace operations - renaming directory. "Perverted"
  2654. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  2655. * Problems:
  2656. * a) we can get into loop creation. Check is done in is_subdir().
  2657. * b) race potential - two innocent renames can create a loop together.
  2658. * That's where 4.4 screws up. Current fix: serialization on
  2659. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  2660. * story.
  2661. * c) we have to lock _three_ objects - parents and victim (if it exists).
  2662. * And that - after we got ->i_mutex on parents (until then we don't know
  2663. * whether the target exists). Solution: try to be smart with locking
  2664. * order for inodes. We rely on the fact that tree topology may change
  2665. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  2666. * move will be locked. Thus we can rank directories by the tree
  2667. * (ancestors first) and rank all non-directories after them.
  2668. * That works since everybody except rename does "lock parent, lookup,
  2669. * lock child" and rename is under ->s_vfs_rename_mutex.
  2670. * HOWEVER, it relies on the assumption that any object with ->lookup()
  2671. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  2672. * we'd better make sure that there's no link(2) for them.
  2673. * d) some filesystems don't support opened-but-unlinked directories,
  2674. * either because of layout or because they are not ready to deal with
  2675. * all cases correctly. The latter will be fixed (taking this sort of
  2676. * stuff into VFS), but the former is not going away. Solution: the same
  2677. * trick as in rmdir().
  2678. * e) conversion from fhandle to dentry may come in the wrong moment - when
  2679. * we are removing the target. Solution: we will have to grab ->i_mutex
  2680. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  2681. * ->i_mutex on parents, which works but leads to some truly excessive
  2682. * locking].
  2683. */
  2684. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  2685. struct inode *new_dir, struct dentry *new_dentry)
  2686. {
  2687. int error = 0;
  2688. struct inode *target;
  2689. /*
  2690. * If we are going to change the parent - check write permissions,
  2691. * we'll need to flip '..'.
  2692. */
  2693. if (new_dir != old_dir) {
  2694. error = inode_permission(old_dentry->d_inode, MAY_WRITE);
  2695. if (error)
  2696. return error;
  2697. }
  2698. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2699. if (error)
  2700. return error;
  2701. target = new_dentry->d_inode;
  2702. if (target)
  2703. mutex_lock(&target->i_mutex);
  2704. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2705. error = -EBUSY;
  2706. else {
  2707. if (target)
  2708. dentry_unhash(new_dentry);
  2709. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2710. }
  2711. if (target) {
  2712. if (!error) {
  2713. target->i_flags |= S_DEAD;
  2714. dont_mount(new_dentry);
  2715. }
  2716. mutex_unlock(&target->i_mutex);
  2717. if (d_unhashed(new_dentry))
  2718. d_rehash(new_dentry);
  2719. dput(new_dentry);
  2720. }
  2721. if (!error)
  2722. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2723. d_move(old_dentry,new_dentry);
  2724. return error;
  2725. }
  2726. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2727. struct inode *new_dir, struct dentry *new_dentry)
  2728. {
  2729. struct inode *target;
  2730. int error;
  2731. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2732. if (error)
  2733. return error;
  2734. dget(new_dentry);
  2735. target = new_dentry->d_inode;
  2736. if (target)
  2737. mutex_lock(&target->i_mutex);
  2738. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2739. error = -EBUSY;
  2740. else
  2741. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2742. if (!error) {
  2743. if (target)
  2744. dont_mount(new_dentry);
  2745. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2746. d_move(old_dentry, new_dentry);
  2747. }
  2748. if (target)
  2749. mutex_unlock(&target->i_mutex);
  2750. dput(new_dentry);
  2751. return error;
  2752. }
  2753. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2754. struct inode *new_dir, struct dentry *new_dentry)
  2755. {
  2756. int error;
  2757. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2758. const unsigned char *old_name;
  2759. if (old_dentry->d_inode == new_dentry->d_inode)
  2760. return 0;
  2761. error = may_delete(old_dir, old_dentry, is_dir);
  2762. if (error)
  2763. return error;
  2764. if (!new_dentry->d_inode)
  2765. error = may_create(new_dir, new_dentry);
  2766. else
  2767. error = may_delete(new_dir, new_dentry, is_dir);
  2768. if (error)
  2769. return error;
  2770. if (!old_dir->i_op->rename)
  2771. return -EPERM;
  2772. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2773. if (is_dir)
  2774. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2775. else
  2776. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2777. if (!error)
  2778. fsnotify_move(old_dir, new_dir, old_name, is_dir,
  2779. new_dentry->d_inode, old_dentry);
  2780. fsnotify_oldname_free(old_name);
  2781. return error;
  2782. }
  2783. SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  2784. int, newdfd, const char __user *, newname)
  2785. {
  2786. struct dentry *old_dir, *new_dir;
  2787. struct dentry *old_dentry, *new_dentry;
  2788. struct dentry *trap;
  2789. struct nameidata oldnd, newnd;
  2790. char *from;
  2791. char *to;
  2792. int error;
  2793. error = user_path_parent(olddfd, oldname, &oldnd, &from);
  2794. if (error)
  2795. goto exit;
  2796. error = user_path_parent(newdfd, newname, &newnd, &to);
  2797. if (error)
  2798. goto exit1;
  2799. error = -EXDEV;
  2800. if (oldnd.path.mnt != newnd.path.mnt)
  2801. goto exit2;
  2802. old_dir = oldnd.path.dentry;
  2803. error = -EBUSY;
  2804. if (oldnd.last_type != LAST_NORM)
  2805. goto exit2;
  2806. new_dir = newnd.path.dentry;
  2807. if (newnd.last_type != LAST_NORM)
  2808. goto exit2;
  2809. oldnd.flags &= ~LOOKUP_PARENT;
  2810. newnd.flags &= ~LOOKUP_PARENT;
  2811. newnd.flags |= LOOKUP_RENAME_TARGET;
  2812. trap = lock_rename(new_dir, old_dir);
  2813. old_dentry = lookup_hash(&oldnd);
  2814. error = PTR_ERR(old_dentry);
  2815. if (IS_ERR(old_dentry))
  2816. goto exit3;
  2817. /* source must exist */
  2818. error = -ENOENT;
  2819. if (!old_dentry->d_inode)
  2820. goto exit4;
  2821. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2822. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2823. error = -ENOTDIR;
  2824. if (oldnd.last.name[oldnd.last.len])
  2825. goto exit4;
  2826. if (newnd.last.name[newnd.last.len])
  2827. goto exit4;
  2828. }
  2829. /* source should not be ancestor of target */
  2830. error = -EINVAL;
  2831. if (old_dentry == trap)
  2832. goto exit4;
  2833. new_dentry = lookup_hash(&newnd);
  2834. error = PTR_ERR(new_dentry);
  2835. if (IS_ERR(new_dentry))
  2836. goto exit4;
  2837. /* target should not be an ancestor of source */
  2838. error = -ENOTEMPTY;
  2839. if (new_dentry == trap)
  2840. goto exit5;
  2841. error = mnt_want_write(oldnd.path.mnt);
  2842. if (error)
  2843. goto exit5;
  2844. error = security_path_rename(&oldnd.path, old_dentry,
  2845. &newnd.path, new_dentry);
  2846. if (error)
  2847. goto exit6;
  2848. error = vfs_rename(old_dir->d_inode, old_dentry,
  2849. new_dir->d_inode, new_dentry);
  2850. exit6:
  2851. mnt_drop_write(oldnd.path.mnt);
  2852. exit5:
  2853. dput(new_dentry);
  2854. exit4:
  2855. dput(old_dentry);
  2856. exit3:
  2857. unlock_rename(new_dir, old_dir);
  2858. exit2:
  2859. path_put(&newnd.path);
  2860. putname(to);
  2861. exit1:
  2862. path_put(&oldnd.path);
  2863. putname(from);
  2864. exit:
  2865. return error;
  2866. }
  2867. SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
  2868. {
  2869. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  2870. }
  2871. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2872. {
  2873. int len;
  2874. len = PTR_ERR(link);
  2875. if (IS_ERR(link))
  2876. goto out;
  2877. len = strlen(link);
  2878. if (len > (unsigned) buflen)
  2879. len = buflen;
  2880. if (copy_to_user(buffer, link, len))
  2881. len = -EFAULT;
  2882. out:
  2883. return len;
  2884. }
  2885. /*
  2886. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2887. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2888. * using) it for any given inode is up to filesystem.
  2889. */
  2890. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2891. {
  2892. struct nameidata nd;
  2893. void *cookie;
  2894. int res;
  2895. nd.depth = 0;
  2896. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2897. if (IS_ERR(cookie))
  2898. return PTR_ERR(cookie);
  2899. res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2900. if (dentry->d_inode->i_op->put_link)
  2901. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  2902. return res;
  2903. }
  2904. int vfs_follow_link(struct nameidata *nd, const char *link)
  2905. {
  2906. return __vfs_follow_link(nd, link);
  2907. }
  2908. /* get the link contents into pagecache */
  2909. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2910. {
  2911. char *kaddr;
  2912. struct page *page;
  2913. struct address_space *mapping = dentry->d_inode->i_mapping;
  2914. page = read_mapping_page(mapping, 0, NULL);
  2915. if (IS_ERR(page))
  2916. return (char*)page;
  2917. *ppage = page;
  2918. kaddr = kmap(page);
  2919. nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
  2920. return kaddr;
  2921. }
  2922. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2923. {
  2924. struct page *page = NULL;
  2925. char *s = page_getlink(dentry, &page);
  2926. int res = vfs_readlink(dentry,buffer,buflen,s);
  2927. if (page) {
  2928. kunmap(page);
  2929. page_cache_release(page);
  2930. }
  2931. return res;
  2932. }
  2933. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2934. {
  2935. struct page *page = NULL;
  2936. nd_set_link(nd, page_getlink(dentry, &page));
  2937. return page;
  2938. }
  2939. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2940. {
  2941. struct page *page = cookie;
  2942. if (page) {
  2943. kunmap(page);
  2944. page_cache_release(page);
  2945. }
  2946. }
  2947. /*
  2948. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  2949. */
  2950. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  2951. {
  2952. struct address_space *mapping = inode->i_mapping;
  2953. struct page *page;
  2954. void *fsdata;
  2955. int err;
  2956. char *kaddr;
  2957. unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  2958. if (nofs)
  2959. flags |= AOP_FLAG_NOFS;
  2960. retry:
  2961. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  2962. flags, &page, &fsdata);
  2963. if (err)
  2964. goto fail;
  2965. kaddr = kmap_atomic(page, KM_USER0);
  2966. memcpy(kaddr, symname, len-1);
  2967. kunmap_atomic(kaddr, KM_USER0);
  2968. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  2969. page, fsdata);
  2970. if (err < 0)
  2971. goto fail;
  2972. if (err < len-1)
  2973. goto retry;
  2974. mark_inode_dirty(inode);
  2975. return 0;
  2976. fail:
  2977. return err;
  2978. }
  2979. int page_symlink(struct inode *inode, const char *symname, int len)
  2980. {
  2981. return __page_symlink(inode, symname, len,
  2982. !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
  2983. }
  2984. const struct inode_operations page_symlink_inode_operations = {
  2985. .readlink = generic_readlink,
  2986. .follow_link = page_follow_link_light,
  2987. .put_link = page_put_link,
  2988. };
  2989. EXPORT_SYMBOL(user_path_at);
  2990. EXPORT_SYMBOL(follow_down_one);
  2991. EXPORT_SYMBOL(follow_down);
  2992. EXPORT_SYMBOL(follow_up);
  2993. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  2994. EXPORT_SYMBOL(getname);
  2995. EXPORT_SYMBOL(lock_rename);
  2996. EXPORT_SYMBOL(lookup_one_len);
  2997. EXPORT_SYMBOL(page_follow_link_light);
  2998. EXPORT_SYMBOL(page_put_link);
  2999. EXPORT_SYMBOL(page_readlink);
  3000. EXPORT_SYMBOL(__page_symlink);
  3001. EXPORT_SYMBOL(page_symlink);
  3002. EXPORT_SYMBOL(page_symlink_inode_operations);
  3003. EXPORT_SYMBOL(kern_path_parent);
  3004. EXPORT_SYMBOL(kern_path);
  3005. EXPORT_SYMBOL(vfs_path_lookup);
  3006. EXPORT_SYMBOL(inode_permission);
  3007. EXPORT_SYMBOL(file_permission);
  3008. EXPORT_SYMBOL(unlock_rename);
  3009. EXPORT_SYMBOL(vfs_create);
  3010. EXPORT_SYMBOL(vfs_follow_link);
  3011. EXPORT_SYMBOL(vfs_link);
  3012. EXPORT_SYMBOL(vfs_mkdir);
  3013. EXPORT_SYMBOL(vfs_mknod);
  3014. EXPORT_SYMBOL(generic_permission);
  3015. EXPORT_SYMBOL(vfs_readlink);
  3016. EXPORT_SYMBOL(vfs_rename);
  3017. EXPORT_SYMBOL(vfs_rmdir);
  3018. EXPORT_SYMBOL(vfs_symlink);
  3019. EXPORT_SYMBOL(vfs_unlink);
  3020. EXPORT_SYMBOL(dentry_unhash);
  3021. EXPORT_SYMBOL(generic_readlink);