namei.c 70 KB

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