namei.c 71 KB

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