namei.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914
  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. char * getname(const char __user * filename)
  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. __putname(tmp);
  137. result = ERR_PTR(retval);
  138. }
  139. }
  140. audit_getname(result);
  141. return result;
  142. }
  143. #ifdef CONFIG_AUDITSYSCALL
  144. void putname(const char *name)
  145. {
  146. if (unlikely(!audit_dummy_context()))
  147. audit_putname(name);
  148. else
  149. __putname(name);
  150. }
  151. EXPORT_SYMBOL(putname);
  152. #endif
  153. /*
  154. * This does basic POSIX ACL permission checking
  155. */
  156. static int acl_permission_check(struct inode *inode, int mask,
  157. int (*check_acl)(struct inode *inode, int mask))
  158. {
  159. umode_t mode = inode->i_mode;
  160. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  161. if (current_fsuid() == inode->i_uid)
  162. mode >>= 6;
  163. else {
  164. if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
  165. int error = check_acl(inode, mask);
  166. if (error != -EAGAIN)
  167. return error;
  168. }
  169. if (in_group_p(inode->i_gid))
  170. mode >>= 3;
  171. }
  172. /*
  173. * If the DACs are ok we don't need any capability check.
  174. */
  175. if ((mask & ~mode) == 0)
  176. return 0;
  177. return -EACCES;
  178. }
  179. /**
  180. * generic_permission - check for access rights on a Posix-like filesystem
  181. * @inode: inode to check access rights for
  182. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  183. * @check_acl: optional callback to check for Posix ACLs
  184. *
  185. * Used to check for read/write/execute permissions on a file.
  186. * We use "fsuid" for this, letting us set arbitrary permissions
  187. * for filesystem access without changing the "normal" uids which
  188. * are used for other things..
  189. */
  190. int generic_permission(struct inode *inode, int mask,
  191. int (*check_acl)(struct inode *inode, int mask))
  192. {
  193. int ret;
  194. /*
  195. * Do the basic POSIX ACL permission checks.
  196. */
  197. ret = acl_permission_check(inode, mask, check_acl);
  198. if (ret != -EACCES)
  199. return ret;
  200. /*
  201. * Read/write DACs are always overridable.
  202. * Executable DACs are overridable if at least one exec bit is set.
  203. */
  204. if (!(mask & MAY_EXEC) || execute_ok(inode))
  205. if (capable(CAP_DAC_OVERRIDE))
  206. return 0;
  207. /*
  208. * Searching includes executable on directories, else just read.
  209. */
  210. mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
  211. if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
  212. if (capable(CAP_DAC_READ_SEARCH))
  213. return 0;
  214. return -EACCES;
  215. }
  216. /**
  217. * inode_permission - check for access rights to a given inode
  218. * @inode: inode to check permission on
  219. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  220. *
  221. * Used to check for read/write/execute permissions on an inode.
  222. * We use "fsuid" for this, letting us set arbitrary permissions
  223. * for filesystem access without changing the "normal" uids which
  224. * are used for other things.
  225. */
  226. int inode_permission(struct inode *inode, int mask)
  227. {
  228. int retval;
  229. if (mask & MAY_WRITE) {
  230. umode_t mode = inode->i_mode;
  231. /*
  232. * Nobody gets write access to a read-only fs.
  233. */
  234. if (IS_RDONLY(inode) &&
  235. (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
  236. return -EROFS;
  237. /*
  238. * Nobody gets write access to an immutable file.
  239. */
  240. if (IS_IMMUTABLE(inode))
  241. return -EACCES;
  242. }
  243. if (inode->i_op->permission)
  244. retval = inode->i_op->permission(inode, mask);
  245. else
  246. retval = generic_permission(inode, mask, inode->i_op->check_acl);
  247. if (retval)
  248. return retval;
  249. retval = devcgroup_inode_permission(inode, mask);
  250. if (retval)
  251. return retval;
  252. return security_inode_permission(inode, mask);
  253. }
  254. /**
  255. * file_permission - check for additional access rights to a given file
  256. * @file: file to check access rights for
  257. * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
  258. *
  259. * Used to check for read/write/execute permissions on an already opened
  260. * file.
  261. *
  262. * Note:
  263. * Do not use this function in new code. All access checks should
  264. * be done using inode_permission().
  265. */
  266. int file_permission(struct file *file, int mask)
  267. {
  268. return inode_permission(file->f_path.dentry->d_inode, mask);
  269. }
  270. /*
  271. * get_write_access() gets write permission for a file.
  272. * put_write_access() releases this write permission.
  273. * This is used for regular files.
  274. * We cannot support write (and maybe mmap read-write shared) accesses and
  275. * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
  276. * can have the following values:
  277. * 0: no writers, no VM_DENYWRITE mappings
  278. * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
  279. * > 0: (i_writecount) users are writing to the file.
  280. *
  281. * Normally we operate on that counter with atomic_{inc,dec} and it's safe
  282. * except for the cases where we don't hold i_writecount yet. Then we need to
  283. * use {get,deny}_write_access() - these functions check the sign and refuse
  284. * to do the change if sign is wrong. Exclusion between them is provided by
  285. * the inode->i_lock spinlock.
  286. */
  287. int get_write_access(struct inode * inode)
  288. {
  289. spin_lock(&inode->i_lock);
  290. if (atomic_read(&inode->i_writecount) < 0) {
  291. spin_unlock(&inode->i_lock);
  292. return -ETXTBSY;
  293. }
  294. atomic_inc(&inode->i_writecount);
  295. spin_unlock(&inode->i_lock);
  296. return 0;
  297. }
  298. int deny_write_access(struct file * file)
  299. {
  300. struct inode *inode = file->f_path.dentry->d_inode;
  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_dec(&inode->i_writecount);
  307. spin_unlock(&inode->i_lock);
  308. return 0;
  309. }
  310. /**
  311. * path_get - get a reference to a path
  312. * @path: path to get the reference to
  313. *
  314. * Given a path increment the reference count to the dentry and the vfsmount.
  315. */
  316. void path_get(struct path *path)
  317. {
  318. mntget(path->mnt);
  319. dget(path->dentry);
  320. }
  321. EXPORT_SYMBOL(path_get);
  322. /**
  323. * path_put - put a reference to a path
  324. * @path: path to put the reference to
  325. *
  326. * Given a path decrement the reference count to the dentry and the vfsmount.
  327. */
  328. void path_put(struct path *path)
  329. {
  330. dput(path->dentry);
  331. mntput(path->mnt);
  332. }
  333. EXPORT_SYMBOL(path_put);
  334. /**
  335. * release_open_intent - free up open intent resources
  336. * @nd: pointer to nameidata
  337. */
  338. void release_open_intent(struct nameidata *nd)
  339. {
  340. if (nd->intent.open.file->f_path.dentry == NULL)
  341. put_filp(nd->intent.open.file);
  342. else
  343. fput(nd->intent.open.file);
  344. }
  345. static inline struct dentry *
  346. do_revalidate(struct dentry *dentry, struct nameidata *nd)
  347. {
  348. int status = dentry->d_op->d_revalidate(dentry, nd);
  349. if (unlikely(status <= 0)) {
  350. /*
  351. * The dentry failed validation.
  352. * If d_revalidate returned 0 attempt to invalidate
  353. * the dentry otherwise d_revalidate is asking us
  354. * to return a fail status.
  355. */
  356. if (!status) {
  357. if (!d_invalidate(dentry)) {
  358. dput(dentry);
  359. dentry = NULL;
  360. }
  361. } else {
  362. dput(dentry);
  363. dentry = ERR_PTR(status);
  364. }
  365. }
  366. return dentry;
  367. }
  368. /*
  369. * force_reval_path - force revalidation of a dentry
  370. *
  371. * In some situations the path walking code will trust dentries without
  372. * revalidating them. This causes problems for filesystems that depend on
  373. * d_revalidate to handle file opens (e.g. NFSv4). When FS_REVAL_DOT is set
  374. * (which indicates that it's possible for the dentry to go stale), force
  375. * a d_revalidate call before proceeding.
  376. *
  377. * Returns 0 if the revalidation was successful. If the revalidation fails,
  378. * either return the error returned by d_revalidate or -ESTALE if the
  379. * revalidation it just returned 0. If d_revalidate returns 0, we attempt to
  380. * invalidate the dentry. It's up to the caller to handle putting references
  381. * to the path if necessary.
  382. */
  383. static int
  384. force_reval_path(struct path *path, struct nameidata *nd)
  385. {
  386. int status;
  387. struct dentry *dentry = path->dentry;
  388. /*
  389. * only check on filesystems where it's possible for the dentry to
  390. * become stale. It's assumed that if this flag is set then the
  391. * d_revalidate op will also be defined.
  392. */
  393. if (!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))
  394. return 0;
  395. status = dentry->d_op->d_revalidate(dentry, nd);
  396. if (status > 0)
  397. return 0;
  398. if (!status) {
  399. d_invalidate(dentry);
  400. status = -ESTALE;
  401. }
  402. return status;
  403. }
  404. /*
  405. * Short-cut version of permission(), for calling on directories
  406. * during pathname resolution. Combines parts of permission()
  407. * and generic_permission(), and tests ONLY for MAY_EXEC permission.
  408. *
  409. * If appropriate, check DAC only. If not appropriate, or
  410. * short-cut DAC fails, then call ->permission() to do more
  411. * complete permission check.
  412. */
  413. static int exec_permission(struct inode *inode)
  414. {
  415. int ret;
  416. if (inode->i_op->permission) {
  417. ret = inode->i_op->permission(inode, MAY_EXEC);
  418. if (!ret)
  419. goto ok;
  420. return ret;
  421. }
  422. ret = acl_permission_check(inode, MAY_EXEC, inode->i_op->check_acl);
  423. if (!ret)
  424. goto ok;
  425. if (capable(CAP_DAC_OVERRIDE) || capable(CAP_DAC_READ_SEARCH))
  426. goto ok;
  427. return ret;
  428. ok:
  429. return security_inode_permission(inode, MAY_EXEC);
  430. }
  431. static __always_inline void set_root(struct nameidata *nd)
  432. {
  433. if (!nd->root.mnt)
  434. get_fs_root(current->fs, &nd->root);
  435. }
  436. static int link_path_walk(const char *, struct nameidata *);
  437. static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
  438. {
  439. if (IS_ERR(link))
  440. goto fail;
  441. if (*link == '/') {
  442. set_root(nd);
  443. path_put(&nd->path);
  444. nd->path = nd->root;
  445. path_get(&nd->root);
  446. }
  447. return link_path_walk(link, nd);
  448. fail:
  449. path_put(&nd->path);
  450. return PTR_ERR(link);
  451. }
  452. static void path_put_conditional(struct path *path, struct nameidata *nd)
  453. {
  454. dput(path->dentry);
  455. if (path->mnt != nd->path.mnt)
  456. mntput(path->mnt);
  457. }
  458. static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
  459. {
  460. dput(nd->path.dentry);
  461. if (nd->path.mnt != path->mnt) {
  462. mntput(nd->path.mnt);
  463. nd->path.mnt = path->mnt;
  464. }
  465. nd->path.dentry = path->dentry;
  466. }
  467. static __always_inline int
  468. __do_follow_link(struct path *path, struct nameidata *nd, void **p)
  469. {
  470. int error;
  471. struct dentry *dentry = path->dentry;
  472. touch_atime(path->mnt, dentry);
  473. nd_set_link(nd, NULL);
  474. if (path->mnt != nd->path.mnt) {
  475. path_to_nameidata(path, nd);
  476. dget(dentry);
  477. }
  478. mntget(path->mnt);
  479. nd->last_type = LAST_BIND;
  480. *p = dentry->d_inode->i_op->follow_link(dentry, nd);
  481. error = PTR_ERR(*p);
  482. if (!IS_ERR(*p)) {
  483. char *s = nd_get_link(nd);
  484. error = 0;
  485. if (s)
  486. error = __vfs_follow_link(nd, s);
  487. else if (nd->last_type == LAST_BIND) {
  488. error = force_reval_path(&nd->path, nd);
  489. if (error)
  490. path_put(&nd->path);
  491. }
  492. }
  493. return error;
  494. }
  495. /*
  496. * This limits recursive symlink follows to 8, while
  497. * limiting consecutive symlinks to 40.
  498. *
  499. * Without that kind of total limit, nasty chains of consecutive
  500. * symlinks can cause almost arbitrarily long lookups.
  501. */
  502. static inline int do_follow_link(struct path *path, struct nameidata *nd)
  503. {
  504. void *cookie;
  505. int err = -ELOOP;
  506. if (current->link_count >= MAX_NESTED_LINKS)
  507. goto loop;
  508. if (current->total_link_count >= 40)
  509. goto loop;
  510. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  511. cond_resched();
  512. err = security_inode_follow_link(path->dentry, nd);
  513. if (err)
  514. goto loop;
  515. current->link_count++;
  516. current->total_link_count++;
  517. nd->depth++;
  518. err = __do_follow_link(path, nd, &cookie);
  519. if (!IS_ERR(cookie) && path->dentry->d_inode->i_op->put_link)
  520. path->dentry->d_inode->i_op->put_link(path->dentry, nd, cookie);
  521. path_put(path);
  522. current->link_count--;
  523. nd->depth--;
  524. return err;
  525. loop:
  526. path_put_conditional(path, nd);
  527. path_put(&nd->path);
  528. return err;
  529. }
  530. int follow_up(struct path *path)
  531. {
  532. struct vfsmount *parent;
  533. struct dentry *mountpoint;
  534. br_read_lock(vfsmount_lock);
  535. parent = path->mnt->mnt_parent;
  536. if (parent == path->mnt) {
  537. br_read_unlock(vfsmount_lock);
  538. return 0;
  539. }
  540. mntget(parent);
  541. mountpoint = dget(path->mnt->mnt_mountpoint);
  542. br_read_unlock(vfsmount_lock);
  543. dput(path->dentry);
  544. path->dentry = mountpoint;
  545. mntput(path->mnt);
  546. path->mnt = parent;
  547. return 1;
  548. }
  549. /* no need for dcache_lock, as serialization is taken care in
  550. * namespace.c
  551. */
  552. static int __follow_mount(struct path *path)
  553. {
  554. int res = 0;
  555. while (d_mountpoint(path->dentry)) {
  556. struct vfsmount *mounted = lookup_mnt(path);
  557. if (!mounted)
  558. break;
  559. dput(path->dentry);
  560. if (res)
  561. mntput(path->mnt);
  562. path->mnt = mounted;
  563. path->dentry = dget(mounted->mnt_root);
  564. res = 1;
  565. }
  566. return res;
  567. }
  568. static void follow_mount(struct path *path)
  569. {
  570. while (d_mountpoint(path->dentry)) {
  571. struct vfsmount *mounted = lookup_mnt(path);
  572. if (!mounted)
  573. break;
  574. dput(path->dentry);
  575. mntput(path->mnt);
  576. path->mnt = mounted;
  577. path->dentry = dget(mounted->mnt_root);
  578. }
  579. }
  580. /* no need for dcache_lock, as serialization is taken care in
  581. * namespace.c
  582. */
  583. int follow_down(struct path *path)
  584. {
  585. struct vfsmount *mounted;
  586. mounted = lookup_mnt(path);
  587. if (mounted) {
  588. dput(path->dentry);
  589. mntput(path->mnt);
  590. path->mnt = mounted;
  591. path->dentry = dget(mounted->mnt_root);
  592. return 1;
  593. }
  594. return 0;
  595. }
  596. static __always_inline void follow_dotdot(struct nameidata *nd)
  597. {
  598. set_root(nd);
  599. while(1) {
  600. struct dentry *old = nd->path.dentry;
  601. if (nd->path.dentry == nd->root.dentry &&
  602. nd->path.mnt == nd->root.mnt) {
  603. break;
  604. }
  605. if (nd->path.dentry != nd->path.mnt->mnt_root) {
  606. /* rare case of legitimate dget_parent()... */
  607. nd->path.dentry = dget_parent(nd->path.dentry);
  608. dput(old);
  609. break;
  610. }
  611. if (!follow_up(&nd->path))
  612. break;
  613. }
  614. follow_mount(&nd->path);
  615. }
  616. /*
  617. * Allocate a dentry with name and parent, and perform a parent
  618. * directory ->lookup on it. Returns the new dentry, or ERR_PTR
  619. * on error. parent->d_inode->i_mutex must be held. d_lookup must
  620. * have verified that no child exists while under i_mutex.
  621. */
  622. static struct dentry *d_alloc_and_lookup(struct dentry *parent,
  623. struct qstr *name, struct nameidata *nd)
  624. {
  625. struct inode *inode = parent->d_inode;
  626. struct dentry *dentry;
  627. struct dentry *old;
  628. /* Don't create child dentry for a dead directory. */
  629. if (unlikely(IS_DEADDIR(inode)))
  630. return ERR_PTR(-ENOENT);
  631. dentry = d_alloc(parent, name);
  632. if (unlikely(!dentry))
  633. return ERR_PTR(-ENOMEM);
  634. old = inode->i_op->lookup(inode, dentry, nd);
  635. if (unlikely(old)) {
  636. dput(dentry);
  637. dentry = old;
  638. }
  639. return dentry;
  640. }
  641. /*
  642. * It's more convoluted than I'd like it to be, but... it's still fairly
  643. * small and for now I'd prefer to have fast path as straight as possible.
  644. * It _is_ time-critical.
  645. */
  646. static int do_lookup(struct nameidata *nd, struct qstr *name,
  647. struct path *path)
  648. {
  649. struct vfsmount *mnt = nd->path.mnt;
  650. struct dentry *dentry, *parent;
  651. struct inode *dir;
  652. /*
  653. * See if the low-level filesystem might want
  654. * to use its own hash..
  655. */
  656. if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
  657. int err = nd->path.dentry->d_op->d_hash(nd->path.dentry, name);
  658. if (err < 0)
  659. return err;
  660. }
  661. /*
  662. * Rename seqlock is not required here because in the off chance
  663. * of a false negative due to a concurrent rename, we're going to
  664. * do the non-racy lookup, below.
  665. */
  666. dentry = __d_lookup(nd->path.dentry, name);
  667. if (!dentry)
  668. goto need_lookup;
  669. found:
  670. if (dentry->d_op && dentry->d_op->d_revalidate)
  671. goto need_revalidate;
  672. done:
  673. path->mnt = mnt;
  674. path->dentry = dentry;
  675. __follow_mount(path);
  676. return 0;
  677. need_lookup:
  678. parent = nd->path.dentry;
  679. dir = parent->d_inode;
  680. mutex_lock(&dir->i_mutex);
  681. /*
  682. * First re-do the cached lookup just in case it was created
  683. * while we waited for the directory semaphore, or the first
  684. * lookup failed due to an unrelated rename.
  685. *
  686. * This could use version numbering or similar to avoid unnecessary
  687. * cache lookups, but then we'd have to do the first lookup in the
  688. * non-racy way. However in the common case here, everything should
  689. * be hot in cache, so would it be a big win?
  690. */
  691. dentry = d_lookup(parent, name);
  692. if (likely(!dentry)) {
  693. dentry = d_alloc_and_lookup(parent, name, nd);
  694. mutex_unlock(&dir->i_mutex);
  695. if (IS_ERR(dentry))
  696. goto fail;
  697. goto done;
  698. }
  699. /*
  700. * Uhhuh! Nasty case: the cache was re-populated while
  701. * we waited on the semaphore. Need to revalidate.
  702. */
  703. mutex_unlock(&dir->i_mutex);
  704. goto found;
  705. need_revalidate:
  706. dentry = do_revalidate(dentry, nd);
  707. if (!dentry)
  708. goto need_lookup;
  709. if (IS_ERR(dentry))
  710. goto fail;
  711. goto done;
  712. fail:
  713. return PTR_ERR(dentry);
  714. }
  715. /*
  716. * This is a temporary kludge to deal with "automount" symlinks; proper
  717. * solution is to trigger them on follow_mount(), so that do_lookup()
  718. * would DTRT. To be killed before 2.6.34-final.
  719. */
  720. static inline int follow_on_final(struct inode *inode, unsigned lookup_flags)
  721. {
  722. return inode && unlikely(inode->i_op->follow_link) &&
  723. ((lookup_flags & LOOKUP_FOLLOW) || S_ISDIR(inode->i_mode));
  724. }
  725. /*
  726. * Name resolution.
  727. * This is the basic name resolution function, turning a pathname into
  728. * the final dentry. We expect 'base' to be positive and a directory.
  729. *
  730. * Returns 0 and nd will have valid dentry and mnt on success.
  731. * Returns error and drops reference to input namei data on failure.
  732. */
  733. static int link_path_walk(const char *name, struct nameidata *nd)
  734. {
  735. struct path next;
  736. struct inode *inode;
  737. int err;
  738. unsigned int lookup_flags = nd->flags;
  739. while (*name=='/')
  740. name++;
  741. if (!*name)
  742. goto return_reval;
  743. inode = nd->path.dentry->d_inode;
  744. if (nd->depth)
  745. lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
  746. /* At this point we know we have a real path component. */
  747. for(;;) {
  748. unsigned long hash;
  749. struct qstr this;
  750. unsigned int c;
  751. nd->flags |= LOOKUP_CONTINUE;
  752. err = exec_permission(inode);
  753. if (err)
  754. break;
  755. this.name = name;
  756. c = *(const unsigned char *)name;
  757. hash = init_name_hash();
  758. do {
  759. name++;
  760. hash = partial_name_hash(c, hash);
  761. c = *(const unsigned char *)name;
  762. } while (c && (c != '/'));
  763. this.len = name - (const char *) this.name;
  764. this.hash = end_name_hash(hash);
  765. /* remove trailing slashes? */
  766. if (!c)
  767. goto last_component;
  768. while (*++name == '/');
  769. if (!*name)
  770. goto last_with_slashes;
  771. /*
  772. * "." and ".." are special - ".." especially so because it has
  773. * to be able to know about the current root directory and
  774. * parent relationships.
  775. */
  776. if (this.name[0] == '.') switch (this.len) {
  777. default:
  778. break;
  779. case 2:
  780. if (this.name[1] != '.')
  781. break;
  782. follow_dotdot(nd);
  783. inode = nd->path.dentry->d_inode;
  784. /* fallthrough */
  785. case 1:
  786. continue;
  787. }
  788. /* This does the actual lookups.. */
  789. err = do_lookup(nd, &this, &next);
  790. if (err)
  791. break;
  792. err = -ENOENT;
  793. inode = next.dentry->d_inode;
  794. if (!inode)
  795. goto out_dput;
  796. if (inode->i_op->follow_link) {
  797. err = do_follow_link(&next, nd);
  798. if (err)
  799. goto return_err;
  800. err = -ENOENT;
  801. inode = nd->path.dentry->d_inode;
  802. if (!inode)
  803. break;
  804. } else
  805. path_to_nameidata(&next, nd);
  806. err = -ENOTDIR;
  807. if (!inode->i_op->lookup)
  808. break;
  809. continue;
  810. /* here ends the main loop */
  811. last_with_slashes:
  812. lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  813. last_component:
  814. /* Clear LOOKUP_CONTINUE iff it was previously unset */
  815. nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
  816. if (lookup_flags & LOOKUP_PARENT)
  817. goto lookup_parent;
  818. if (this.name[0] == '.') switch (this.len) {
  819. default:
  820. break;
  821. case 2:
  822. if (this.name[1] != '.')
  823. break;
  824. follow_dotdot(nd);
  825. inode = nd->path.dentry->d_inode;
  826. /* fallthrough */
  827. case 1:
  828. goto return_reval;
  829. }
  830. err = do_lookup(nd, &this, &next);
  831. if (err)
  832. break;
  833. inode = next.dentry->d_inode;
  834. if (follow_on_final(inode, lookup_flags)) {
  835. err = do_follow_link(&next, nd);
  836. if (err)
  837. goto return_err;
  838. inode = nd->path.dentry->d_inode;
  839. } else
  840. path_to_nameidata(&next, nd);
  841. err = -ENOENT;
  842. if (!inode)
  843. break;
  844. if (lookup_flags & LOOKUP_DIRECTORY) {
  845. err = -ENOTDIR;
  846. if (!inode->i_op->lookup)
  847. break;
  848. }
  849. goto return_base;
  850. lookup_parent:
  851. nd->last = this;
  852. nd->last_type = LAST_NORM;
  853. if (this.name[0] != '.')
  854. goto return_base;
  855. if (this.len == 1)
  856. nd->last_type = LAST_DOT;
  857. else if (this.len == 2 && this.name[1] == '.')
  858. nd->last_type = LAST_DOTDOT;
  859. else
  860. goto return_base;
  861. return_reval:
  862. /*
  863. * We bypassed the ordinary revalidation routines.
  864. * We may need to check the cached dentry for staleness.
  865. */
  866. if (nd->path.dentry && nd->path.dentry->d_sb &&
  867. (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
  868. err = -ESTALE;
  869. /* Note: we do not d_invalidate() */
  870. if (!nd->path.dentry->d_op->d_revalidate(
  871. nd->path.dentry, nd))
  872. break;
  873. }
  874. return_base:
  875. return 0;
  876. out_dput:
  877. path_put_conditional(&next, nd);
  878. break;
  879. }
  880. path_put(&nd->path);
  881. return_err:
  882. return err;
  883. }
  884. static int path_walk(const char *name, struct nameidata *nd)
  885. {
  886. struct path save = nd->path;
  887. int result;
  888. current->total_link_count = 0;
  889. /* make sure the stuff we saved doesn't go away */
  890. path_get(&save);
  891. result = link_path_walk(name, nd);
  892. if (result == -ESTALE) {
  893. /* nd->path had been dropped */
  894. current->total_link_count = 0;
  895. nd->path = save;
  896. path_get(&nd->path);
  897. nd->flags |= LOOKUP_REVAL;
  898. result = link_path_walk(name, nd);
  899. }
  900. path_put(&save);
  901. return result;
  902. }
  903. static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
  904. {
  905. int retval = 0;
  906. int fput_needed;
  907. struct file *file;
  908. nd->last_type = LAST_ROOT; /* if there are only slashes... */
  909. nd->flags = flags;
  910. nd->depth = 0;
  911. nd->root.mnt = NULL;
  912. if (*name=='/') {
  913. set_root(nd);
  914. nd->path = nd->root;
  915. path_get(&nd->root);
  916. } else if (dfd == AT_FDCWD) {
  917. get_fs_pwd(current->fs, &nd->path);
  918. } else {
  919. struct dentry *dentry;
  920. file = fget_light(dfd, &fput_needed);
  921. retval = -EBADF;
  922. if (!file)
  923. goto out_fail;
  924. dentry = file->f_path.dentry;
  925. retval = -ENOTDIR;
  926. if (!S_ISDIR(dentry->d_inode->i_mode))
  927. goto fput_fail;
  928. retval = file_permission(file, MAY_EXEC);
  929. if (retval)
  930. goto fput_fail;
  931. nd->path = file->f_path;
  932. path_get(&file->f_path);
  933. fput_light(file, fput_needed);
  934. }
  935. return 0;
  936. fput_fail:
  937. fput_light(file, fput_needed);
  938. out_fail:
  939. return retval;
  940. }
  941. /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
  942. static int do_path_lookup(int dfd, const char *name,
  943. unsigned int flags, struct nameidata *nd)
  944. {
  945. int retval = path_init(dfd, name, flags, nd);
  946. if (!retval)
  947. retval = path_walk(name, nd);
  948. if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
  949. nd->path.dentry->d_inode))
  950. audit_inode(name, nd->path.dentry);
  951. if (nd->root.mnt) {
  952. path_put(&nd->root);
  953. nd->root.mnt = NULL;
  954. }
  955. return retval;
  956. }
  957. int path_lookup(const char *name, unsigned int flags,
  958. struct nameidata *nd)
  959. {
  960. return do_path_lookup(AT_FDCWD, name, flags, nd);
  961. }
  962. int kern_path(const char *name, unsigned int flags, struct path *path)
  963. {
  964. struct nameidata nd;
  965. int res = do_path_lookup(AT_FDCWD, name, flags, &nd);
  966. if (!res)
  967. *path = nd.path;
  968. return res;
  969. }
  970. /**
  971. * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
  972. * @dentry: pointer to dentry of the base directory
  973. * @mnt: pointer to vfs mount of the base directory
  974. * @name: pointer to file name
  975. * @flags: lookup flags
  976. * @nd: pointer to nameidata
  977. */
  978. int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
  979. const char *name, unsigned int flags,
  980. struct nameidata *nd)
  981. {
  982. int retval;
  983. /* same as do_path_lookup */
  984. nd->last_type = LAST_ROOT;
  985. nd->flags = flags;
  986. nd->depth = 0;
  987. nd->path.dentry = dentry;
  988. nd->path.mnt = mnt;
  989. path_get(&nd->path);
  990. nd->root = nd->path;
  991. path_get(&nd->root);
  992. retval = path_walk(name, nd);
  993. if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
  994. nd->path.dentry->d_inode))
  995. audit_inode(name, nd->path.dentry);
  996. path_put(&nd->root);
  997. nd->root.mnt = NULL;
  998. return retval;
  999. }
  1000. static struct dentry *__lookup_hash(struct qstr *name,
  1001. struct dentry *base, struct nameidata *nd)
  1002. {
  1003. struct inode *inode = base->d_inode;
  1004. struct dentry *dentry;
  1005. int err;
  1006. err = exec_permission(inode);
  1007. if (err)
  1008. return ERR_PTR(err);
  1009. /*
  1010. * See if the low-level filesystem might want
  1011. * to use its own hash..
  1012. */
  1013. if (base->d_op && base->d_op->d_hash) {
  1014. err = base->d_op->d_hash(base, name);
  1015. dentry = ERR_PTR(err);
  1016. if (err < 0)
  1017. goto out;
  1018. }
  1019. /*
  1020. * Don't bother with __d_lookup: callers are for creat as
  1021. * well as unlink, so a lot of the time it would cost
  1022. * a double lookup.
  1023. */
  1024. dentry = d_lookup(base, name);
  1025. if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
  1026. dentry = do_revalidate(dentry, nd);
  1027. if (!dentry)
  1028. dentry = d_alloc_and_lookup(base, name, nd);
  1029. out:
  1030. return dentry;
  1031. }
  1032. /*
  1033. * Restricted form of lookup. Doesn't follow links, single-component only,
  1034. * needs parent already locked. Doesn't follow mounts.
  1035. * SMP-safe.
  1036. */
  1037. static struct dentry *lookup_hash(struct nameidata *nd)
  1038. {
  1039. return __lookup_hash(&nd->last, nd->path.dentry, nd);
  1040. }
  1041. static int __lookup_one_len(const char *name, struct qstr *this,
  1042. struct dentry *base, int len)
  1043. {
  1044. unsigned long hash;
  1045. unsigned int c;
  1046. this->name = name;
  1047. this->len = len;
  1048. if (!len)
  1049. return -EACCES;
  1050. hash = init_name_hash();
  1051. while (len--) {
  1052. c = *(const unsigned char *)name++;
  1053. if (c == '/' || c == '\0')
  1054. return -EACCES;
  1055. hash = partial_name_hash(c, hash);
  1056. }
  1057. this->hash = end_name_hash(hash);
  1058. return 0;
  1059. }
  1060. /**
  1061. * lookup_one_len - filesystem helper to lookup single pathname component
  1062. * @name: pathname component to lookup
  1063. * @base: base directory to lookup from
  1064. * @len: maximum length @len should be interpreted to
  1065. *
  1066. * Note that this routine is purely a helper for filesystem usage and should
  1067. * not be called by generic code. Also note that by using this function the
  1068. * nameidata argument is passed to the filesystem methods and a filesystem
  1069. * using this helper needs to be prepared for that.
  1070. */
  1071. struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
  1072. {
  1073. int err;
  1074. struct qstr this;
  1075. WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex));
  1076. err = __lookup_one_len(name, &this, base, len);
  1077. if (err)
  1078. return ERR_PTR(err);
  1079. return __lookup_hash(&this, base, NULL);
  1080. }
  1081. int user_path_at(int dfd, const char __user *name, unsigned flags,
  1082. struct path *path)
  1083. {
  1084. struct nameidata nd;
  1085. char *tmp = getname(name);
  1086. int err = PTR_ERR(tmp);
  1087. if (!IS_ERR(tmp)) {
  1088. BUG_ON(flags & LOOKUP_PARENT);
  1089. err = do_path_lookup(dfd, tmp, flags, &nd);
  1090. putname(tmp);
  1091. if (!err)
  1092. *path = nd.path;
  1093. }
  1094. return err;
  1095. }
  1096. static int user_path_parent(int dfd, const char __user *path,
  1097. struct nameidata *nd, char **name)
  1098. {
  1099. char *s = getname(path);
  1100. int error;
  1101. if (IS_ERR(s))
  1102. return PTR_ERR(s);
  1103. error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd);
  1104. if (error)
  1105. putname(s);
  1106. else
  1107. *name = s;
  1108. return error;
  1109. }
  1110. /*
  1111. * It's inline, so penalty for filesystems that don't use sticky bit is
  1112. * minimal.
  1113. */
  1114. static inline int check_sticky(struct inode *dir, struct inode *inode)
  1115. {
  1116. uid_t fsuid = current_fsuid();
  1117. if (!(dir->i_mode & S_ISVTX))
  1118. return 0;
  1119. if (inode->i_uid == fsuid)
  1120. return 0;
  1121. if (dir->i_uid == fsuid)
  1122. return 0;
  1123. return !capable(CAP_FOWNER);
  1124. }
  1125. /*
  1126. * Check whether we can remove a link victim from directory dir, check
  1127. * whether the type of victim is right.
  1128. * 1. We can't do it if dir is read-only (done in permission())
  1129. * 2. We should have write and exec permissions on dir
  1130. * 3. We can't remove anything from append-only dir
  1131. * 4. We can't do anything with immutable dir (done in permission())
  1132. * 5. If the sticky bit on dir is set we should either
  1133. * a. be owner of dir, or
  1134. * b. be owner of victim, or
  1135. * c. have CAP_FOWNER capability
  1136. * 6. If the victim is append-only or immutable we can't do antyhing with
  1137. * links pointing to it.
  1138. * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
  1139. * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
  1140. * 9. We can't remove a root or mountpoint.
  1141. * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
  1142. * nfs_async_unlink().
  1143. */
  1144. static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
  1145. {
  1146. int error;
  1147. if (!victim->d_inode)
  1148. return -ENOENT;
  1149. BUG_ON(victim->d_parent->d_inode != dir);
  1150. audit_inode_child(victim, dir);
  1151. error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1152. if (error)
  1153. return error;
  1154. if (IS_APPEND(dir))
  1155. return -EPERM;
  1156. if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
  1157. IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
  1158. return -EPERM;
  1159. if (isdir) {
  1160. if (!S_ISDIR(victim->d_inode->i_mode))
  1161. return -ENOTDIR;
  1162. if (IS_ROOT(victim))
  1163. return -EBUSY;
  1164. } else if (S_ISDIR(victim->d_inode->i_mode))
  1165. return -EISDIR;
  1166. if (IS_DEADDIR(dir))
  1167. return -ENOENT;
  1168. if (victim->d_flags & DCACHE_NFSFS_RENAMED)
  1169. return -EBUSY;
  1170. return 0;
  1171. }
  1172. /* Check whether we can create an object with dentry child in directory
  1173. * dir.
  1174. * 1. We can't do it if child already exists (open has special treatment for
  1175. * this case, but since we are inlined it's OK)
  1176. * 2. We can't do it if dir is read-only (done in permission())
  1177. * 3. We should have write and exec permissions on dir
  1178. * 4. We can't do it if dir is immutable (done in permission())
  1179. */
  1180. static inline int may_create(struct inode *dir, struct dentry *child)
  1181. {
  1182. if (child->d_inode)
  1183. return -EEXIST;
  1184. if (IS_DEADDIR(dir))
  1185. return -ENOENT;
  1186. return inode_permission(dir, MAY_WRITE | MAY_EXEC);
  1187. }
  1188. /*
  1189. * p1 and p2 should be directories on the same fs.
  1190. */
  1191. struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
  1192. {
  1193. struct dentry *p;
  1194. if (p1 == p2) {
  1195. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1196. return NULL;
  1197. }
  1198. mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1199. p = d_ancestor(p2, p1);
  1200. if (p) {
  1201. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
  1202. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
  1203. return p;
  1204. }
  1205. p = d_ancestor(p1, p2);
  1206. if (p) {
  1207. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1208. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1209. return p;
  1210. }
  1211. mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
  1212. mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
  1213. return NULL;
  1214. }
  1215. void unlock_rename(struct dentry *p1, struct dentry *p2)
  1216. {
  1217. mutex_unlock(&p1->d_inode->i_mutex);
  1218. if (p1 != p2) {
  1219. mutex_unlock(&p2->d_inode->i_mutex);
  1220. mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
  1221. }
  1222. }
  1223. int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1224. struct nameidata *nd)
  1225. {
  1226. int error = may_create(dir, dentry);
  1227. if (error)
  1228. return error;
  1229. if (!dir->i_op->create)
  1230. return -EACCES; /* shouldn't it be ENOSYS? */
  1231. mode &= S_IALLUGO;
  1232. mode |= S_IFREG;
  1233. error = security_inode_create(dir, dentry, mode);
  1234. if (error)
  1235. return error;
  1236. error = dir->i_op->create(dir, dentry, mode, nd);
  1237. if (!error)
  1238. fsnotify_create(dir, dentry);
  1239. return error;
  1240. }
  1241. int may_open(struct path *path, int acc_mode, int flag)
  1242. {
  1243. struct dentry *dentry = path->dentry;
  1244. struct inode *inode = dentry->d_inode;
  1245. int error;
  1246. if (!inode)
  1247. return -ENOENT;
  1248. switch (inode->i_mode & S_IFMT) {
  1249. case S_IFLNK:
  1250. return -ELOOP;
  1251. case S_IFDIR:
  1252. if (acc_mode & MAY_WRITE)
  1253. return -EISDIR;
  1254. break;
  1255. case S_IFBLK:
  1256. case S_IFCHR:
  1257. if (path->mnt->mnt_flags & MNT_NODEV)
  1258. return -EACCES;
  1259. /*FALLTHRU*/
  1260. case S_IFIFO:
  1261. case S_IFSOCK:
  1262. flag &= ~O_TRUNC;
  1263. break;
  1264. }
  1265. error = inode_permission(inode, acc_mode);
  1266. if (error)
  1267. return error;
  1268. /*
  1269. * An append-only file must be opened in append mode for writing.
  1270. */
  1271. if (IS_APPEND(inode)) {
  1272. if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
  1273. return -EPERM;
  1274. if (flag & O_TRUNC)
  1275. return -EPERM;
  1276. }
  1277. /* O_NOATIME can only be set by the owner or superuser */
  1278. if (flag & O_NOATIME && !is_owner_or_cap(inode))
  1279. return -EPERM;
  1280. /*
  1281. * Ensure there are no outstanding leases on the file.
  1282. */
  1283. return break_lease(inode, flag);
  1284. }
  1285. static int handle_truncate(struct path *path)
  1286. {
  1287. struct inode *inode = path->dentry->d_inode;
  1288. int error = get_write_access(inode);
  1289. if (error)
  1290. return error;
  1291. /*
  1292. * Refuse to truncate files with mandatory locks held on them.
  1293. */
  1294. error = locks_verify_locked(inode);
  1295. if (!error)
  1296. error = security_path_truncate(path);
  1297. if (!error) {
  1298. error = do_truncate(path->dentry, 0,
  1299. ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
  1300. NULL);
  1301. }
  1302. put_write_access(inode);
  1303. return error;
  1304. }
  1305. /*
  1306. * Be careful about ever adding any more callers of this
  1307. * function. Its flags must be in the namei format, not
  1308. * what get passed to sys_open().
  1309. */
  1310. static int __open_namei_create(struct nameidata *nd, struct path *path,
  1311. int open_flag, int mode)
  1312. {
  1313. int error;
  1314. struct dentry *dir = nd->path.dentry;
  1315. if (!IS_POSIXACL(dir->d_inode))
  1316. mode &= ~current_umask();
  1317. error = security_path_mknod(&nd->path, path->dentry, mode, 0);
  1318. if (error)
  1319. goto out_unlock;
  1320. error = vfs_create(dir->d_inode, path->dentry, mode, nd);
  1321. out_unlock:
  1322. mutex_unlock(&dir->d_inode->i_mutex);
  1323. dput(nd->path.dentry);
  1324. nd->path.dentry = path->dentry;
  1325. if (error)
  1326. return error;
  1327. /* Don't check for write permission, don't truncate */
  1328. return may_open(&nd->path, 0, open_flag & ~O_TRUNC);
  1329. }
  1330. /*
  1331. * Note that while the flag value (low two bits) for sys_open means:
  1332. * 00 - read-only
  1333. * 01 - write-only
  1334. * 10 - read-write
  1335. * 11 - special
  1336. * it is changed into
  1337. * 00 - no permissions needed
  1338. * 01 - read-permission
  1339. * 10 - write-permission
  1340. * 11 - read-write
  1341. * for the internal routines (ie open_namei()/follow_link() etc)
  1342. * This is more logical, and also allows the 00 "no perm needed"
  1343. * to be used for symlinks (where the permissions are checked
  1344. * later).
  1345. *
  1346. */
  1347. static inline int open_to_namei_flags(int flag)
  1348. {
  1349. if ((flag+1) & O_ACCMODE)
  1350. flag++;
  1351. return flag;
  1352. }
  1353. static int open_will_truncate(int flag, struct inode *inode)
  1354. {
  1355. /*
  1356. * We'll never write to the fs underlying
  1357. * a device file.
  1358. */
  1359. if (special_file(inode->i_mode))
  1360. return 0;
  1361. return (flag & O_TRUNC);
  1362. }
  1363. static struct file *finish_open(struct nameidata *nd,
  1364. int open_flag, int acc_mode)
  1365. {
  1366. struct file *filp;
  1367. int will_truncate;
  1368. int error;
  1369. will_truncate = open_will_truncate(open_flag, nd->path.dentry->d_inode);
  1370. if (will_truncate) {
  1371. error = mnt_want_write(nd->path.mnt);
  1372. if (error)
  1373. goto exit;
  1374. }
  1375. error = may_open(&nd->path, acc_mode, open_flag);
  1376. if (error) {
  1377. if (will_truncate)
  1378. mnt_drop_write(nd->path.mnt);
  1379. goto exit;
  1380. }
  1381. filp = nameidata_to_filp(nd);
  1382. if (!IS_ERR(filp)) {
  1383. error = ima_file_check(filp, acc_mode);
  1384. if (error) {
  1385. fput(filp);
  1386. filp = ERR_PTR(error);
  1387. }
  1388. }
  1389. if (!IS_ERR(filp)) {
  1390. if (will_truncate) {
  1391. error = handle_truncate(&nd->path);
  1392. if (error) {
  1393. fput(filp);
  1394. filp = ERR_PTR(error);
  1395. }
  1396. }
  1397. }
  1398. /*
  1399. * It is now safe to drop the mnt write
  1400. * because the filp has had a write taken
  1401. * on its behalf.
  1402. */
  1403. if (will_truncate)
  1404. mnt_drop_write(nd->path.mnt);
  1405. path_put(&nd->path);
  1406. return filp;
  1407. exit:
  1408. if (!IS_ERR(nd->intent.open.file))
  1409. release_open_intent(nd);
  1410. path_put(&nd->path);
  1411. return ERR_PTR(error);
  1412. }
  1413. static struct file *do_last(struct nameidata *nd, struct path *path,
  1414. int open_flag, int acc_mode,
  1415. int mode, const char *pathname)
  1416. {
  1417. struct dentry *dir = nd->path.dentry;
  1418. struct file *filp;
  1419. int error = -EISDIR;
  1420. switch (nd->last_type) {
  1421. case LAST_DOTDOT:
  1422. follow_dotdot(nd);
  1423. dir = nd->path.dentry;
  1424. case LAST_DOT:
  1425. if (nd->path.mnt->mnt_sb->s_type->fs_flags & FS_REVAL_DOT) {
  1426. if (!dir->d_op->d_revalidate(dir, nd)) {
  1427. error = -ESTALE;
  1428. goto exit;
  1429. }
  1430. }
  1431. /* fallthrough */
  1432. case LAST_ROOT:
  1433. if (open_flag & O_CREAT)
  1434. goto exit;
  1435. /* fallthrough */
  1436. case LAST_BIND:
  1437. audit_inode(pathname, dir);
  1438. goto ok;
  1439. }
  1440. /* trailing slashes? */
  1441. if (nd->last.name[nd->last.len]) {
  1442. if (open_flag & O_CREAT)
  1443. goto exit;
  1444. nd->flags |= LOOKUP_DIRECTORY | LOOKUP_FOLLOW;
  1445. }
  1446. /* just plain open? */
  1447. if (!(open_flag & O_CREAT)) {
  1448. error = do_lookup(nd, &nd->last, path);
  1449. if (error)
  1450. goto exit;
  1451. error = -ENOENT;
  1452. if (!path->dentry->d_inode)
  1453. goto exit_dput;
  1454. if (path->dentry->d_inode->i_op->follow_link)
  1455. return NULL;
  1456. error = -ENOTDIR;
  1457. if (nd->flags & LOOKUP_DIRECTORY) {
  1458. if (!path->dentry->d_inode->i_op->lookup)
  1459. goto exit_dput;
  1460. }
  1461. path_to_nameidata(path, nd);
  1462. audit_inode(pathname, nd->path.dentry);
  1463. goto ok;
  1464. }
  1465. /* OK, it's O_CREAT */
  1466. mutex_lock(&dir->d_inode->i_mutex);
  1467. path->dentry = lookup_hash(nd);
  1468. path->mnt = nd->path.mnt;
  1469. error = PTR_ERR(path->dentry);
  1470. if (IS_ERR(path->dentry)) {
  1471. mutex_unlock(&dir->d_inode->i_mutex);
  1472. goto exit;
  1473. }
  1474. if (IS_ERR(nd->intent.open.file)) {
  1475. error = PTR_ERR(nd->intent.open.file);
  1476. goto exit_mutex_unlock;
  1477. }
  1478. /* Negative dentry, just create the file */
  1479. if (!path->dentry->d_inode) {
  1480. /*
  1481. * This write is needed to ensure that a
  1482. * ro->rw transition does not occur between
  1483. * the time when the file is created and when
  1484. * a permanent write count is taken through
  1485. * the 'struct file' in nameidata_to_filp().
  1486. */
  1487. error = mnt_want_write(nd->path.mnt);
  1488. if (error)
  1489. goto exit_mutex_unlock;
  1490. error = __open_namei_create(nd, path, open_flag, mode);
  1491. if (error) {
  1492. mnt_drop_write(nd->path.mnt);
  1493. goto exit;
  1494. }
  1495. filp = nameidata_to_filp(nd);
  1496. mnt_drop_write(nd->path.mnt);
  1497. path_put(&nd->path);
  1498. if (!IS_ERR(filp)) {
  1499. error = ima_file_check(filp, acc_mode);
  1500. if (error) {
  1501. fput(filp);
  1502. filp = ERR_PTR(error);
  1503. }
  1504. }
  1505. return filp;
  1506. }
  1507. /*
  1508. * It already exists.
  1509. */
  1510. mutex_unlock(&dir->d_inode->i_mutex);
  1511. audit_inode(pathname, path->dentry);
  1512. error = -EEXIST;
  1513. if (open_flag & O_EXCL)
  1514. goto exit_dput;
  1515. if (__follow_mount(path)) {
  1516. error = -ELOOP;
  1517. if (open_flag & O_NOFOLLOW)
  1518. goto exit_dput;
  1519. }
  1520. error = -ENOENT;
  1521. if (!path->dentry->d_inode)
  1522. goto exit_dput;
  1523. if (path->dentry->d_inode->i_op->follow_link)
  1524. return NULL;
  1525. path_to_nameidata(path, nd);
  1526. error = -EISDIR;
  1527. if (S_ISDIR(path->dentry->d_inode->i_mode))
  1528. goto exit;
  1529. ok:
  1530. filp = finish_open(nd, open_flag, acc_mode);
  1531. return filp;
  1532. exit_mutex_unlock:
  1533. mutex_unlock(&dir->d_inode->i_mutex);
  1534. exit_dput:
  1535. path_put_conditional(path, nd);
  1536. exit:
  1537. if (!IS_ERR(nd->intent.open.file))
  1538. release_open_intent(nd);
  1539. path_put(&nd->path);
  1540. return ERR_PTR(error);
  1541. }
  1542. /*
  1543. * Note that the low bits of the passed in "open_flag"
  1544. * are not the same as in the local variable "flag". See
  1545. * open_to_namei_flags() for more details.
  1546. */
  1547. struct file *do_filp_open(int dfd, const char *pathname,
  1548. int open_flag, int mode, int acc_mode)
  1549. {
  1550. struct file *filp;
  1551. struct nameidata nd;
  1552. int error;
  1553. struct path path;
  1554. int count = 0;
  1555. int flag = open_to_namei_flags(open_flag);
  1556. int force_reval = 0;
  1557. if (!(open_flag & O_CREAT))
  1558. mode = 0;
  1559. /*
  1560. * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
  1561. * check for O_DSYNC if the need any syncing at all we enforce it's
  1562. * always set instead of having to deal with possibly weird behaviour
  1563. * for malicious applications setting only __O_SYNC.
  1564. */
  1565. if (open_flag & __O_SYNC)
  1566. open_flag |= O_DSYNC;
  1567. if (!acc_mode)
  1568. acc_mode = MAY_OPEN | ACC_MODE(open_flag);
  1569. /* O_TRUNC implies we need access checks for write permissions */
  1570. if (open_flag & O_TRUNC)
  1571. acc_mode |= MAY_WRITE;
  1572. /* Allow the LSM permission hook to distinguish append
  1573. access from general write access. */
  1574. if (open_flag & O_APPEND)
  1575. acc_mode |= MAY_APPEND;
  1576. /* find the parent */
  1577. reval:
  1578. error = path_init(dfd, pathname, LOOKUP_PARENT, &nd);
  1579. if (error)
  1580. return ERR_PTR(error);
  1581. if (force_reval)
  1582. nd.flags |= LOOKUP_REVAL;
  1583. current->total_link_count = 0;
  1584. error = link_path_walk(pathname, &nd);
  1585. if (error) {
  1586. filp = ERR_PTR(error);
  1587. goto out;
  1588. }
  1589. if (unlikely(!audit_dummy_context()) && (open_flag & O_CREAT))
  1590. audit_inode(pathname, nd.path.dentry);
  1591. /*
  1592. * We have the parent and last component.
  1593. */
  1594. error = -ENFILE;
  1595. filp = get_empty_filp();
  1596. if (filp == NULL)
  1597. goto exit_parent;
  1598. nd.intent.open.file = filp;
  1599. filp->f_flags = open_flag;
  1600. nd.intent.open.flags = flag;
  1601. nd.intent.open.create_mode = mode;
  1602. nd.flags &= ~LOOKUP_PARENT;
  1603. nd.flags |= LOOKUP_OPEN;
  1604. if (open_flag & O_CREAT) {
  1605. nd.flags |= LOOKUP_CREATE;
  1606. if (open_flag & O_EXCL)
  1607. nd.flags |= LOOKUP_EXCL;
  1608. }
  1609. if (open_flag & O_DIRECTORY)
  1610. nd.flags |= LOOKUP_DIRECTORY;
  1611. if (!(open_flag & O_NOFOLLOW))
  1612. nd.flags |= LOOKUP_FOLLOW;
  1613. filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
  1614. while (unlikely(!filp)) { /* trailing symlink */
  1615. struct path holder;
  1616. struct inode *inode = path.dentry->d_inode;
  1617. void *cookie;
  1618. error = -ELOOP;
  1619. /* S_ISDIR part is a temporary automount kludge */
  1620. if (!(nd.flags & LOOKUP_FOLLOW) && !S_ISDIR(inode->i_mode))
  1621. goto exit_dput;
  1622. if (count++ == 32)
  1623. goto exit_dput;
  1624. /*
  1625. * This is subtle. Instead of calling do_follow_link() we do
  1626. * the thing by hands. The reason is that this way we have zero
  1627. * link_count and path_walk() (called from ->follow_link)
  1628. * honoring LOOKUP_PARENT. After that we have the parent and
  1629. * last component, i.e. we are in the same situation as after
  1630. * the first path_walk(). Well, almost - if the last component
  1631. * is normal we get its copy stored in nd->last.name and we will
  1632. * have to putname() it when we are done. Procfs-like symlinks
  1633. * just set LAST_BIND.
  1634. */
  1635. nd.flags |= LOOKUP_PARENT;
  1636. error = security_inode_follow_link(path.dentry, &nd);
  1637. if (error)
  1638. goto exit_dput;
  1639. error = __do_follow_link(&path, &nd, &cookie);
  1640. if (unlikely(error)) {
  1641. /* nd.path had been dropped */
  1642. if (!IS_ERR(cookie) && inode->i_op->put_link)
  1643. inode->i_op->put_link(path.dentry, &nd, cookie);
  1644. path_put(&path);
  1645. release_open_intent(&nd);
  1646. filp = ERR_PTR(error);
  1647. goto out;
  1648. }
  1649. holder = path;
  1650. nd.flags &= ~LOOKUP_PARENT;
  1651. filp = do_last(&nd, &path, open_flag, acc_mode, mode, pathname);
  1652. if (inode->i_op->put_link)
  1653. inode->i_op->put_link(holder.dentry, &nd, cookie);
  1654. path_put(&holder);
  1655. }
  1656. out:
  1657. if (nd.root.mnt)
  1658. path_put(&nd.root);
  1659. if (filp == ERR_PTR(-ESTALE) && !force_reval) {
  1660. force_reval = 1;
  1661. goto reval;
  1662. }
  1663. return filp;
  1664. exit_dput:
  1665. path_put_conditional(&path, &nd);
  1666. if (!IS_ERR(nd.intent.open.file))
  1667. release_open_intent(&nd);
  1668. exit_parent:
  1669. path_put(&nd.path);
  1670. filp = ERR_PTR(error);
  1671. goto out;
  1672. }
  1673. /**
  1674. * filp_open - open file and return file pointer
  1675. *
  1676. * @filename: path to open
  1677. * @flags: open flags as per the open(2) second argument
  1678. * @mode: mode for the new file if O_CREAT is set, else ignored
  1679. *
  1680. * This is the helper to open a file from kernelspace if you really
  1681. * have to. But in generally you should not do this, so please move
  1682. * along, nothing to see here..
  1683. */
  1684. struct file *filp_open(const char *filename, int flags, int mode)
  1685. {
  1686. return do_filp_open(AT_FDCWD, filename, flags, mode, 0);
  1687. }
  1688. EXPORT_SYMBOL(filp_open);
  1689. /**
  1690. * lookup_create - lookup a dentry, creating it if it doesn't exist
  1691. * @nd: nameidata info
  1692. * @is_dir: directory flag
  1693. *
  1694. * Simple function to lookup and return a dentry and create it
  1695. * if it doesn't exist. Is SMP-safe.
  1696. *
  1697. * Returns with nd->path.dentry->d_inode->i_mutex locked.
  1698. */
  1699. struct dentry *lookup_create(struct nameidata *nd, int is_dir)
  1700. {
  1701. struct dentry *dentry = ERR_PTR(-EEXIST);
  1702. mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  1703. /*
  1704. * Yucky last component or no last component at all?
  1705. * (foo/., foo/.., /////)
  1706. */
  1707. if (nd->last_type != LAST_NORM)
  1708. goto fail;
  1709. nd->flags &= ~LOOKUP_PARENT;
  1710. nd->flags |= LOOKUP_CREATE | LOOKUP_EXCL;
  1711. nd->intent.open.flags = O_EXCL;
  1712. /*
  1713. * Do the final lookup.
  1714. */
  1715. dentry = lookup_hash(nd);
  1716. if (IS_ERR(dentry))
  1717. goto fail;
  1718. if (dentry->d_inode)
  1719. goto eexist;
  1720. /*
  1721. * Special case - lookup gave negative, but... we had foo/bar/
  1722. * From the vfs_mknod() POV we just have a negative dentry -
  1723. * all is fine. Let's be bastards - you had / on the end, you've
  1724. * been asking for (non-existent) directory. -ENOENT for you.
  1725. */
  1726. if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
  1727. dput(dentry);
  1728. dentry = ERR_PTR(-ENOENT);
  1729. }
  1730. return dentry;
  1731. eexist:
  1732. dput(dentry);
  1733. dentry = ERR_PTR(-EEXIST);
  1734. fail:
  1735. return dentry;
  1736. }
  1737. EXPORT_SYMBOL_GPL(lookup_create);
  1738. int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  1739. {
  1740. int error = may_create(dir, dentry);
  1741. if (error)
  1742. return error;
  1743. if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
  1744. return -EPERM;
  1745. if (!dir->i_op->mknod)
  1746. return -EPERM;
  1747. error = devcgroup_inode_mknod(mode, dev);
  1748. if (error)
  1749. return error;
  1750. error = security_inode_mknod(dir, dentry, mode, dev);
  1751. if (error)
  1752. return error;
  1753. error = dir->i_op->mknod(dir, dentry, mode, dev);
  1754. if (!error)
  1755. fsnotify_create(dir, dentry);
  1756. return error;
  1757. }
  1758. static int may_mknod(mode_t mode)
  1759. {
  1760. switch (mode & S_IFMT) {
  1761. case S_IFREG:
  1762. case S_IFCHR:
  1763. case S_IFBLK:
  1764. case S_IFIFO:
  1765. case S_IFSOCK:
  1766. case 0: /* zero mode translates to S_IFREG */
  1767. return 0;
  1768. case S_IFDIR:
  1769. return -EPERM;
  1770. default:
  1771. return -EINVAL;
  1772. }
  1773. }
  1774. SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode,
  1775. unsigned, dev)
  1776. {
  1777. int error;
  1778. char *tmp;
  1779. struct dentry *dentry;
  1780. struct nameidata nd;
  1781. if (S_ISDIR(mode))
  1782. return -EPERM;
  1783. error = user_path_parent(dfd, filename, &nd, &tmp);
  1784. if (error)
  1785. return error;
  1786. dentry = lookup_create(&nd, 0);
  1787. if (IS_ERR(dentry)) {
  1788. error = PTR_ERR(dentry);
  1789. goto out_unlock;
  1790. }
  1791. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  1792. mode &= ~current_umask();
  1793. error = may_mknod(mode);
  1794. if (error)
  1795. goto out_dput;
  1796. error = mnt_want_write(nd.path.mnt);
  1797. if (error)
  1798. goto out_dput;
  1799. error = security_path_mknod(&nd.path, dentry, mode, dev);
  1800. if (error)
  1801. goto out_drop_write;
  1802. switch (mode & S_IFMT) {
  1803. case 0: case S_IFREG:
  1804. error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
  1805. break;
  1806. case S_IFCHR: case S_IFBLK:
  1807. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
  1808. new_decode_dev(dev));
  1809. break;
  1810. case S_IFIFO: case S_IFSOCK:
  1811. error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
  1812. break;
  1813. }
  1814. out_drop_write:
  1815. mnt_drop_write(nd.path.mnt);
  1816. out_dput:
  1817. dput(dentry);
  1818. out_unlock:
  1819. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  1820. path_put(&nd.path);
  1821. putname(tmp);
  1822. return error;
  1823. }
  1824. SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev)
  1825. {
  1826. return sys_mknodat(AT_FDCWD, filename, mode, dev);
  1827. }
  1828. int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1829. {
  1830. int error = may_create(dir, dentry);
  1831. if (error)
  1832. return error;
  1833. if (!dir->i_op->mkdir)
  1834. return -EPERM;
  1835. mode &= (S_IRWXUGO|S_ISVTX);
  1836. error = security_inode_mkdir(dir, dentry, mode);
  1837. if (error)
  1838. return error;
  1839. error = dir->i_op->mkdir(dir, dentry, mode);
  1840. if (!error)
  1841. fsnotify_mkdir(dir, dentry);
  1842. return error;
  1843. }
  1844. SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode)
  1845. {
  1846. int error = 0;
  1847. char * tmp;
  1848. struct dentry *dentry;
  1849. struct nameidata nd;
  1850. error = user_path_parent(dfd, pathname, &nd, &tmp);
  1851. if (error)
  1852. goto out_err;
  1853. dentry = lookup_create(&nd, 1);
  1854. error = PTR_ERR(dentry);
  1855. if (IS_ERR(dentry))
  1856. goto out_unlock;
  1857. if (!IS_POSIXACL(nd.path.dentry->d_inode))
  1858. mode &= ~current_umask();
  1859. error = mnt_want_write(nd.path.mnt);
  1860. if (error)
  1861. goto out_dput;
  1862. error = security_path_mkdir(&nd.path, dentry, mode);
  1863. if (error)
  1864. goto out_drop_write;
  1865. error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
  1866. out_drop_write:
  1867. mnt_drop_write(nd.path.mnt);
  1868. out_dput:
  1869. dput(dentry);
  1870. out_unlock:
  1871. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  1872. path_put(&nd.path);
  1873. putname(tmp);
  1874. out_err:
  1875. return error;
  1876. }
  1877. SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode)
  1878. {
  1879. return sys_mkdirat(AT_FDCWD, pathname, mode);
  1880. }
  1881. /*
  1882. * We try to drop the dentry early: we should have
  1883. * a usage count of 2 if we're the only user of this
  1884. * dentry, and if that is true (possibly after pruning
  1885. * the dcache), then we drop the dentry now.
  1886. *
  1887. * A low-level filesystem can, if it choses, legally
  1888. * do a
  1889. *
  1890. * if (!d_unhashed(dentry))
  1891. * return -EBUSY;
  1892. *
  1893. * if it cannot handle the case of removing a directory
  1894. * that is still in use by something else..
  1895. */
  1896. void dentry_unhash(struct dentry *dentry)
  1897. {
  1898. dget(dentry);
  1899. shrink_dcache_parent(dentry);
  1900. spin_lock(&dcache_lock);
  1901. spin_lock(&dentry->d_lock);
  1902. if (atomic_read(&dentry->d_count) == 2)
  1903. __d_drop(dentry);
  1904. spin_unlock(&dentry->d_lock);
  1905. spin_unlock(&dcache_lock);
  1906. }
  1907. int vfs_rmdir(struct inode *dir, struct dentry *dentry)
  1908. {
  1909. int error = may_delete(dir, dentry, 1);
  1910. if (error)
  1911. return error;
  1912. if (!dir->i_op->rmdir)
  1913. return -EPERM;
  1914. mutex_lock(&dentry->d_inode->i_mutex);
  1915. dentry_unhash(dentry);
  1916. if (d_mountpoint(dentry))
  1917. error = -EBUSY;
  1918. else {
  1919. error = security_inode_rmdir(dir, dentry);
  1920. if (!error) {
  1921. error = dir->i_op->rmdir(dir, dentry);
  1922. if (!error) {
  1923. dentry->d_inode->i_flags |= S_DEAD;
  1924. dont_mount(dentry);
  1925. }
  1926. }
  1927. }
  1928. mutex_unlock(&dentry->d_inode->i_mutex);
  1929. if (!error) {
  1930. d_delete(dentry);
  1931. }
  1932. dput(dentry);
  1933. return error;
  1934. }
  1935. static long do_rmdir(int dfd, const char __user *pathname)
  1936. {
  1937. int error = 0;
  1938. char * name;
  1939. struct dentry *dentry;
  1940. struct nameidata nd;
  1941. error = user_path_parent(dfd, pathname, &nd, &name);
  1942. if (error)
  1943. return error;
  1944. switch(nd.last_type) {
  1945. case LAST_DOTDOT:
  1946. error = -ENOTEMPTY;
  1947. goto exit1;
  1948. case LAST_DOT:
  1949. error = -EINVAL;
  1950. goto exit1;
  1951. case LAST_ROOT:
  1952. error = -EBUSY;
  1953. goto exit1;
  1954. }
  1955. nd.flags &= ~LOOKUP_PARENT;
  1956. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  1957. dentry = lookup_hash(&nd);
  1958. error = PTR_ERR(dentry);
  1959. if (IS_ERR(dentry))
  1960. goto exit2;
  1961. error = mnt_want_write(nd.path.mnt);
  1962. if (error)
  1963. goto exit3;
  1964. error = security_path_rmdir(&nd.path, dentry);
  1965. if (error)
  1966. goto exit4;
  1967. error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
  1968. exit4:
  1969. mnt_drop_write(nd.path.mnt);
  1970. exit3:
  1971. dput(dentry);
  1972. exit2:
  1973. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  1974. exit1:
  1975. path_put(&nd.path);
  1976. putname(name);
  1977. return error;
  1978. }
  1979. SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
  1980. {
  1981. return do_rmdir(AT_FDCWD, pathname);
  1982. }
  1983. int vfs_unlink(struct inode *dir, struct dentry *dentry)
  1984. {
  1985. int error = may_delete(dir, dentry, 0);
  1986. if (error)
  1987. return error;
  1988. if (!dir->i_op->unlink)
  1989. return -EPERM;
  1990. mutex_lock(&dentry->d_inode->i_mutex);
  1991. if (d_mountpoint(dentry))
  1992. error = -EBUSY;
  1993. else {
  1994. error = security_inode_unlink(dir, dentry);
  1995. if (!error) {
  1996. error = dir->i_op->unlink(dir, dentry);
  1997. if (!error)
  1998. dont_mount(dentry);
  1999. }
  2000. }
  2001. mutex_unlock(&dentry->d_inode->i_mutex);
  2002. /* We don't d_delete() NFS sillyrenamed files--they still exist. */
  2003. if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
  2004. fsnotify_link_count(dentry->d_inode);
  2005. d_delete(dentry);
  2006. }
  2007. return error;
  2008. }
  2009. /*
  2010. * Make sure that the actual truncation of the file will occur outside its
  2011. * directory's i_mutex. Truncate can take a long time if there is a lot of
  2012. * writeout happening, and we don't want to prevent access to the directory
  2013. * while waiting on the I/O.
  2014. */
  2015. static long do_unlinkat(int dfd, const char __user *pathname)
  2016. {
  2017. int error;
  2018. char *name;
  2019. struct dentry *dentry;
  2020. struct nameidata nd;
  2021. struct inode *inode = NULL;
  2022. error = user_path_parent(dfd, pathname, &nd, &name);
  2023. if (error)
  2024. return error;
  2025. error = -EISDIR;
  2026. if (nd.last_type != LAST_NORM)
  2027. goto exit1;
  2028. nd.flags &= ~LOOKUP_PARENT;
  2029. mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
  2030. dentry = lookup_hash(&nd);
  2031. error = PTR_ERR(dentry);
  2032. if (!IS_ERR(dentry)) {
  2033. /* Why not before? Because we want correct error value */
  2034. if (nd.last.name[nd.last.len])
  2035. goto slashes;
  2036. inode = dentry->d_inode;
  2037. if (inode)
  2038. ihold(inode);
  2039. error = mnt_want_write(nd.path.mnt);
  2040. if (error)
  2041. goto exit2;
  2042. error = security_path_unlink(&nd.path, dentry);
  2043. if (error)
  2044. goto exit3;
  2045. error = vfs_unlink(nd.path.dentry->d_inode, dentry);
  2046. exit3:
  2047. mnt_drop_write(nd.path.mnt);
  2048. exit2:
  2049. dput(dentry);
  2050. }
  2051. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2052. if (inode)
  2053. iput(inode); /* truncate the inode here */
  2054. exit1:
  2055. path_put(&nd.path);
  2056. putname(name);
  2057. return error;
  2058. slashes:
  2059. error = !dentry->d_inode ? -ENOENT :
  2060. S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
  2061. goto exit2;
  2062. }
  2063. SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
  2064. {
  2065. if ((flag & ~AT_REMOVEDIR) != 0)
  2066. return -EINVAL;
  2067. if (flag & AT_REMOVEDIR)
  2068. return do_rmdir(dfd, pathname);
  2069. return do_unlinkat(dfd, pathname);
  2070. }
  2071. SYSCALL_DEFINE1(unlink, const char __user *, pathname)
  2072. {
  2073. return do_unlinkat(AT_FDCWD, pathname);
  2074. }
  2075. int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
  2076. {
  2077. int error = may_create(dir, dentry);
  2078. if (error)
  2079. return error;
  2080. if (!dir->i_op->symlink)
  2081. return -EPERM;
  2082. error = security_inode_symlink(dir, dentry, oldname);
  2083. if (error)
  2084. return error;
  2085. error = dir->i_op->symlink(dir, dentry, oldname);
  2086. if (!error)
  2087. fsnotify_create(dir, dentry);
  2088. return error;
  2089. }
  2090. SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
  2091. int, newdfd, const char __user *, newname)
  2092. {
  2093. int error;
  2094. char *from;
  2095. char *to;
  2096. struct dentry *dentry;
  2097. struct nameidata nd;
  2098. from = getname(oldname);
  2099. if (IS_ERR(from))
  2100. return PTR_ERR(from);
  2101. error = user_path_parent(newdfd, newname, &nd, &to);
  2102. if (error)
  2103. goto out_putname;
  2104. dentry = lookup_create(&nd, 0);
  2105. error = PTR_ERR(dentry);
  2106. if (IS_ERR(dentry))
  2107. goto out_unlock;
  2108. error = mnt_want_write(nd.path.mnt);
  2109. if (error)
  2110. goto out_dput;
  2111. error = security_path_symlink(&nd.path, dentry, from);
  2112. if (error)
  2113. goto out_drop_write;
  2114. error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
  2115. out_drop_write:
  2116. mnt_drop_write(nd.path.mnt);
  2117. out_dput:
  2118. dput(dentry);
  2119. out_unlock:
  2120. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2121. path_put(&nd.path);
  2122. putname(to);
  2123. out_putname:
  2124. putname(from);
  2125. return error;
  2126. }
  2127. SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
  2128. {
  2129. return sys_symlinkat(oldname, AT_FDCWD, newname);
  2130. }
  2131. int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
  2132. {
  2133. struct inode *inode = old_dentry->d_inode;
  2134. int error;
  2135. if (!inode)
  2136. return -ENOENT;
  2137. error = may_create(dir, new_dentry);
  2138. if (error)
  2139. return error;
  2140. if (dir->i_sb != inode->i_sb)
  2141. return -EXDEV;
  2142. /*
  2143. * A link to an append-only or immutable file cannot be created.
  2144. */
  2145. if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
  2146. return -EPERM;
  2147. if (!dir->i_op->link)
  2148. return -EPERM;
  2149. if (S_ISDIR(inode->i_mode))
  2150. return -EPERM;
  2151. error = security_inode_link(old_dentry, dir, new_dentry);
  2152. if (error)
  2153. return error;
  2154. mutex_lock(&inode->i_mutex);
  2155. error = dir->i_op->link(old_dentry, dir, new_dentry);
  2156. mutex_unlock(&inode->i_mutex);
  2157. if (!error)
  2158. fsnotify_link(dir, inode, new_dentry);
  2159. return error;
  2160. }
  2161. /*
  2162. * Hardlinks are often used in delicate situations. We avoid
  2163. * security-related surprises by not following symlinks on the
  2164. * newname. --KAB
  2165. *
  2166. * We don't follow them on the oldname either to be compatible
  2167. * with linux 2.0, and to avoid hard-linking to directories
  2168. * and other special files. --ADM
  2169. */
  2170. SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
  2171. int, newdfd, const char __user *, newname, int, flags)
  2172. {
  2173. struct dentry *new_dentry;
  2174. struct nameidata nd;
  2175. struct path old_path;
  2176. int error;
  2177. char *to;
  2178. if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
  2179. return -EINVAL;
  2180. error = user_path_at(olddfd, oldname,
  2181. flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
  2182. &old_path);
  2183. if (error)
  2184. return error;
  2185. error = user_path_parent(newdfd, newname, &nd, &to);
  2186. if (error)
  2187. goto out;
  2188. error = -EXDEV;
  2189. if (old_path.mnt != nd.path.mnt)
  2190. goto out_release;
  2191. new_dentry = lookup_create(&nd, 0);
  2192. error = PTR_ERR(new_dentry);
  2193. if (IS_ERR(new_dentry))
  2194. goto out_unlock;
  2195. error = mnt_want_write(nd.path.mnt);
  2196. if (error)
  2197. goto out_dput;
  2198. error = security_path_link(old_path.dentry, &nd.path, new_dentry);
  2199. if (error)
  2200. goto out_drop_write;
  2201. error = vfs_link(old_path.dentry, nd.path.dentry->d_inode, new_dentry);
  2202. out_drop_write:
  2203. mnt_drop_write(nd.path.mnt);
  2204. out_dput:
  2205. dput(new_dentry);
  2206. out_unlock:
  2207. mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
  2208. out_release:
  2209. path_put(&nd.path);
  2210. putname(to);
  2211. out:
  2212. path_put(&old_path);
  2213. return error;
  2214. }
  2215. SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
  2216. {
  2217. return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
  2218. }
  2219. /*
  2220. * The worst of all namespace operations - renaming directory. "Perverted"
  2221. * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
  2222. * Problems:
  2223. * a) we can get into loop creation. Check is done in is_subdir().
  2224. * b) race potential - two innocent renames can create a loop together.
  2225. * That's where 4.4 screws up. Current fix: serialization on
  2226. * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
  2227. * story.
  2228. * c) we have to lock _three_ objects - parents and victim (if it exists).
  2229. * And that - after we got ->i_mutex on parents (until then we don't know
  2230. * whether the target exists). Solution: try to be smart with locking
  2231. * order for inodes. We rely on the fact that tree topology may change
  2232. * only under ->s_vfs_rename_mutex _and_ that parent of the object we
  2233. * move will be locked. Thus we can rank directories by the tree
  2234. * (ancestors first) and rank all non-directories after them.
  2235. * That works since everybody except rename does "lock parent, lookup,
  2236. * lock child" and rename is under ->s_vfs_rename_mutex.
  2237. * HOWEVER, it relies on the assumption that any object with ->lookup()
  2238. * has no more than 1 dentry. If "hybrid" objects will ever appear,
  2239. * we'd better make sure that there's no link(2) for them.
  2240. * d) some filesystems don't support opened-but-unlinked directories,
  2241. * either because of layout or because they are not ready to deal with
  2242. * all cases correctly. The latter will be fixed (taking this sort of
  2243. * stuff into VFS), but the former is not going away. Solution: the same
  2244. * trick as in rmdir().
  2245. * e) conversion from fhandle to dentry may come in the wrong moment - when
  2246. * we are removing the target. Solution: we will have to grab ->i_mutex
  2247. * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
  2248. * ->i_mutex on parents, which works but leads to some truly excessive
  2249. * locking].
  2250. */
  2251. static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
  2252. struct inode *new_dir, struct dentry *new_dentry)
  2253. {
  2254. int error = 0;
  2255. struct inode *target;
  2256. /*
  2257. * If we are going to change the parent - check write permissions,
  2258. * we'll need to flip '..'.
  2259. */
  2260. if (new_dir != old_dir) {
  2261. error = inode_permission(old_dentry->d_inode, MAY_WRITE);
  2262. if (error)
  2263. return error;
  2264. }
  2265. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2266. if (error)
  2267. return error;
  2268. target = new_dentry->d_inode;
  2269. if (target)
  2270. mutex_lock(&target->i_mutex);
  2271. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2272. error = -EBUSY;
  2273. else {
  2274. if (target)
  2275. dentry_unhash(new_dentry);
  2276. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2277. }
  2278. if (target) {
  2279. if (!error) {
  2280. target->i_flags |= S_DEAD;
  2281. dont_mount(new_dentry);
  2282. }
  2283. mutex_unlock(&target->i_mutex);
  2284. if (d_unhashed(new_dentry))
  2285. d_rehash(new_dentry);
  2286. dput(new_dentry);
  2287. }
  2288. if (!error)
  2289. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2290. d_move(old_dentry,new_dentry);
  2291. return error;
  2292. }
  2293. static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
  2294. struct inode *new_dir, struct dentry *new_dentry)
  2295. {
  2296. struct inode *target;
  2297. int error;
  2298. error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
  2299. if (error)
  2300. return error;
  2301. dget(new_dentry);
  2302. target = new_dentry->d_inode;
  2303. if (target)
  2304. mutex_lock(&target->i_mutex);
  2305. if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
  2306. error = -EBUSY;
  2307. else
  2308. error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
  2309. if (!error) {
  2310. if (target)
  2311. dont_mount(new_dentry);
  2312. if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
  2313. d_move(old_dentry, new_dentry);
  2314. }
  2315. if (target)
  2316. mutex_unlock(&target->i_mutex);
  2317. dput(new_dentry);
  2318. return error;
  2319. }
  2320. int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  2321. struct inode *new_dir, struct dentry *new_dentry)
  2322. {
  2323. int error;
  2324. int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
  2325. const unsigned char *old_name;
  2326. if (old_dentry->d_inode == new_dentry->d_inode)
  2327. return 0;
  2328. error = may_delete(old_dir, old_dentry, is_dir);
  2329. if (error)
  2330. return error;
  2331. if (!new_dentry->d_inode)
  2332. error = may_create(new_dir, new_dentry);
  2333. else
  2334. error = may_delete(new_dir, new_dentry, is_dir);
  2335. if (error)
  2336. return error;
  2337. if (!old_dir->i_op->rename)
  2338. return -EPERM;
  2339. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  2340. if (is_dir)
  2341. error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
  2342. else
  2343. error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
  2344. if (!error)
  2345. fsnotify_move(old_dir, new_dir, old_name, is_dir,
  2346. new_dentry->d_inode, old_dentry);
  2347. fsnotify_oldname_free(old_name);
  2348. return error;
  2349. }
  2350. SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
  2351. int, newdfd, const char __user *, newname)
  2352. {
  2353. struct dentry *old_dir, *new_dir;
  2354. struct dentry *old_dentry, *new_dentry;
  2355. struct dentry *trap;
  2356. struct nameidata oldnd, newnd;
  2357. char *from;
  2358. char *to;
  2359. int error;
  2360. error = user_path_parent(olddfd, oldname, &oldnd, &from);
  2361. if (error)
  2362. goto exit;
  2363. error = user_path_parent(newdfd, newname, &newnd, &to);
  2364. if (error)
  2365. goto exit1;
  2366. error = -EXDEV;
  2367. if (oldnd.path.mnt != newnd.path.mnt)
  2368. goto exit2;
  2369. old_dir = oldnd.path.dentry;
  2370. error = -EBUSY;
  2371. if (oldnd.last_type != LAST_NORM)
  2372. goto exit2;
  2373. new_dir = newnd.path.dentry;
  2374. if (newnd.last_type != LAST_NORM)
  2375. goto exit2;
  2376. oldnd.flags &= ~LOOKUP_PARENT;
  2377. newnd.flags &= ~LOOKUP_PARENT;
  2378. newnd.flags |= LOOKUP_RENAME_TARGET;
  2379. trap = lock_rename(new_dir, old_dir);
  2380. old_dentry = lookup_hash(&oldnd);
  2381. error = PTR_ERR(old_dentry);
  2382. if (IS_ERR(old_dentry))
  2383. goto exit3;
  2384. /* source must exist */
  2385. error = -ENOENT;
  2386. if (!old_dentry->d_inode)
  2387. goto exit4;
  2388. /* unless the source is a directory trailing slashes give -ENOTDIR */
  2389. if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
  2390. error = -ENOTDIR;
  2391. if (oldnd.last.name[oldnd.last.len])
  2392. goto exit4;
  2393. if (newnd.last.name[newnd.last.len])
  2394. goto exit4;
  2395. }
  2396. /* source should not be ancestor of target */
  2397. error = -EINVAL;
  2398. if (old_dentry == trap)
  2399. goto exit4;
  2400. new_dentry = lookup_hash(&newnd);
  2401. error = PTR_ERR(new_dentry);
  2402. if (IS_ERR(new_dentry))
  2403. goto exit4;
  2404. /* target should not be an ancestor of source */
  2405. error = -ENOTEMPTY;
  2406. if (new_dentry == trap)
  2407. goto exit5;
  2408. error = mnt_want_write(oldnd.path.mnt);
  2409. if (error)
  2410. goto exit5;
  2411. error = security_path_rename(&oldnd.path, old_dentry,
  2412. &newnd.path, new_dentry);
  2413. if (error)
  2414. goto exit6;
  2415. error = vfs_rename(old_dir->d_inode, old_dentry,
  2416. new_dir->d_inode, new_dentry);
  2417. exit6:
  2418. mnt_drop_write(oldnd.path.mnt);
  2419. exit5:
  2420. dput(new_dentry);
  2421. exit4:
  2422. dput(old_dentry);
  2423. exit3:
  2424. unlock_rename(new_dir, old_dir);
  2425. exit2:
  2426. path_put(&newnd.path);
  2427. putname(to);
  2428. exit1:
  2429. path_put(&oldnd.path);
  2430. putname(from);
  2431. exit:
  2432. return error;
  2433. }
  2434. SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
  2435. {
  2436. return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
  2437. }
  2438. int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
  2439. {
  2440. int len;
  2441. len = PTR_ERR(link);
  2442. if (IS_ERR(link))
  2443. goto out;
  2444. len = strlen(link);
  2445. if (len > (unsigned) buflen)
  2446. len = buflen;
  2447. if (copy_to_user(buffer, link, len))
  2448. len = -EFAULT;
  2449. out:
  2450. return len;
  2451. }
  2452. /*
  2453. * A helper for ->readlink(). This should be used *ONLY* for symlinks that
  2454. * have ->follow_link() touching nd only in nd_set_link(). Using (or not
  2455. * using) it for any given inode is up to filesystem.
  2456. */
  2457. int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2458. {
  2459. struct nameidata nd;
  2460. void *cookie;
  2461. int res;
  2462. nd.depth = 0;
  2463. cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
  2464. if (IS_ERR(cookie))
  2465. return PTR_ERR(cookie);
  2466. res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
  2467. if (dentry->d_inode->i_op->put_link)
  2468. dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
  2469. return res;
  2470. }
  2471. int vfs_follow_link(struct nameidata *nd, const char *link)
  2472. {
  2473. return __vfs_follow_link(nd, link);
  2474. }
  2475. /* get the link contents into pagecache */
  2476. static char *page_getlink(struct dentry * dentry, struct page **ppage)
  2477. {
  2478. char *kaddr;
  2479. struct page *page;
  2480. struct address_space *mapping = dentry->d_inode->i_mapping;
  2481. page = read_mapping_page(mapping, 0, NULL);
  2482. if (IS_ERR(page))
  2483. return (char*)page;
  2484. *ppage = page;
  2485. kaddr = kmap(page);
  2486. nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1);
  2487. return kaddr;
  2488. }
  2489. int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
  2490. {
  2491. struct page *page = NULL;
  2492. char *s = page_getlink(dentry, &page);
  2493. int res = vfs_readlink(dentry,buffer,buflen,s);
  2494. if (page) {
  2495. kunmap(page);
  2496. page_cache_release(page);
  2497. }
  2498. return res;
  2499. }
  2500. void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
  2501. {
  2502. struct page *page = NULL;
  2503. nd_set_link(nd, page_getlink(dentry, &page));
  2504. return page;
  2505. }
  2506. void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  2507. {
  2508. struct page *page = cookie;
  2509. if (page) {
  2510. kunmap(page);
  2511. page_cache_release(page);
  2512. }
  2513. }
  2514. /*
  2515. * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
  2516. */
  2517. int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
  2518. {
  2519. struct address_space *mapping = inode->i_mapping;
  2520. struct page *page;
  2521. void *fsdata;
  2522. int err;
  2523. char *kaddr;
  2524. unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
  2525. if (nofs)
  2526. flags |= AOP_FLAG_NOFS;
  2527. retry:
  2528. err = pagecache_write_begin(NULL, mapping, 0, len-1,
  2529. flags, &page, &fsdata);
  2530. if (err)
  2531. goto fail;
  2532. kaddr = kmap_atomic(page, KM_USER0);
  2533. memcpy(kaddr, symname, len-1);
  2534. kunmap_atomic(kaddr, KM_USER0);
  2535. err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
  2536. page, fsdata);
  2537. if (err < 0)
  2538. goto fail;
  2539. if (err < len-1)
  2540. goto retry;
  2541. mark_inode_dirty(inode);
  2542. return 0;
  2543. fail:
  2544. return err;
  2545. }
  2546. int page_symlink(struct inode *inode, const char *symname, int len)
  2547. {
  2548. return __page_symlink(inode, symname, len,
  2549. !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS));
  2550. }
  2551. const struct inode_operations page_symlink_inode_operations = {
  2552. .readlink = generic_readlink,
  2553. .follow_link = page_follow_link_light,
  2554. .put_link = page_put_link,
  2555. };
  2556. EXPORT_SYMBOL(user_path_at);
  2557. EXPORT_SYMBOL(follow_down);
  2558. EXPORT_SYMBOL(follow_up);
  2559. EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
  2560. EXPORT_SYMBOL(getname);
  2561. EXPORT_SYMBOL(lock_rename);
  2562. EXPORT_SYMBOL(lookup_one_len);
  2563. EXPORT_SYMBOL(page_follow_link_light);
  2564. EXPORT_SYMBOL(page_put_link);
  2565. EXPORT_SYMBOL(page_readlink);
  2566. EXPORT_SYMBOL(__page_symlink);
  2567. EXPORT_SYMBOL(page_symlink);
  2568. EXPORT_SYMBOL(page_symlink_inode_operations);
  2569. EXPORT_SYMBOL(path_lookup);
  2570. EXPORT_SYMBOL(kern_path);
  2571. EXPORT_SYMBOL(vfs_path_lookup);
  2572. EXPORT_SYMBOL(inode_permission);
  2573. EXPORT_SYMBOL(file_permission);
  2574. EXPORT_SYMBOL(unlock_rename);
  2575. EXPORT_SYMBOL(vfs_create);
  2576. EXPORT_SYMBOL(vfs_follow_link);
  2577. EXPORT_SYMBOL(vfs_link);
  2578. EXPORT_SYMBOL(vfs_mkdir);
  2579. EXPORT_SYMBOL(vfs_mknod);
  2580. EXPORT_SYMBOL(generic_permission);
  2581. EXPORT_SYMBOL(vfs_readlink);
  2582. EXPORT_SYMBOL(vfs_rename);
  2583. EXPORT_SYMBOL(vfs_rmdir);
  2584. EXPORT_SYMBOL(vfs_symlink);
  2585. EXPORT_SYMBOL(vfs_unlink);
  2586. EXPORT_SYMBOL(dentry_unhash);
  2587. EXPORT_SYMBOL(generic_readlink);