namei.c 83 KB

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