namei.c 71 KB

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