namei.c 71 KB

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