namei.c 71 KB

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