namei.c 70 KB

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