namei.c 71 KB

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