namei.c 70 KB

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