open.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  1. /*
  2. * linux/fs/open.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/string.h>
  7. #include <linux/mm.h>
  8. #include <linux/file.h>
  9. #include <linux/fdtable.h>
  10. #include <linux/quotaops.h>
  11. #include <linux/fsnotify.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/tty.h>
  15. #include <linux/namei.h>
  16. #include <linux/backing-dev.h>
  17. #include <linux/capability.h>
  18. #include <linux/securebits.h>
  19. #include <linux/security.h>
  20. #include <linux/mount.h>
  21. #include <linux/vfs.h>
  22. #include <linux/fcntl.h>
  23. #include <asm/uaccess.h>
  24. #include <linux/fs.h>
  25. #include <linux/personality.h>
  26. #include <linux/pagemap.h>
  27. #include <linux/syscalls.h>
  28. #include <linux/rcupdate.h>
  29. #include <linux/audit.h>
  30. #include <linux/falloc.h>
  31. int vfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  32. {
  33. int retval = -ENODEV;
  34. if (dentry) {
  35. retval = -ENOSYS;
  36. if (dentry->d_sb->s_op->statfs) {
  37. memset(buf, 0, sizeof(*buf));
  38. retval = security_sb_statfs(dentry);
  39. if (retval)
  40. return retval;
  41. retval = dentry->d_sb->s_op->statfs(dentry, buf);
  42. if (retval == 0 && buf->f_frsize == 0)
  43. buf->f_frsize = buf->f_bsize;
  44. }
  45. }
  46. return retval;
  47. }
  48. EXPORT_SYMBOL(vfs_statfs);
  49. static int vfs_statfs_native(struct dentry *dentry, struct statfs *buf)
  50. {
  51. struct kstatfs st;
  52. int retval;
  53. retval = vfs_statfs(dentry, &st);
  54. if (retval)
  55. return retval;
  56. if (sizeof(*buf) == sizeof(st))
  57. memcpy(buf, &st, sizeof(st));
  58. else {
  59. if (sizeof buf->f_blocks == 4) {
  60. if ((st.f_blocks | st.f_bfree | st.f_bavail |
  61. st.f_bsize | st.f_frsize) &
  62. 0xffffffff00000000ULL)
  63. return -EOVERFLOW;
  64. /*
  65. * f_files and f_ffree may be -1; it's okay to stuff
  66. * that into 32 bits
  67. */
  68. if (st.f_files != -1 &&
  69. (st.f_files & 0xffffffff00000000ULL))
  70. return -EOVERFLOW;
  71. if (st.f_ffree != -1 &&
  72. (st.f_ffree & 0xffffffff00000000ULL))
  73. return -EOVERFLOW;
  74. }
  75. buf->f_type = st.f_type;
  76. buf->f_bsize = st.f_bsize;
  77. buf->f_blocks = st.f_blocks;
  78. buf->f_bfree = st.f_bfree;
  79. buf->f_bavail = st.f_bavail;
  80. buf->f_files = st.f_files;
  81. buf->f_ffree = st.f_ffree;
  82. buf->f_fsid = st.f_fsid;
  83. buf->f_namelen = st.f_namelen;
  84. buf->f_frsize = st.f_frsize;
  85. memset(buf->f_spare, 0, sizeof(buf->f_spare));
  86. }
  87. return 0;
  88. }
  89. static int vfs_statfs64(struct dentry *dentry, struct statfs64 *buf)
  90. {
  91. struct kstatfs st;
  92. int retval;
  93. retval = vfs_statfs(dentry, &st);
  94. if (retval)
  95. return retval;
  96. if (sizeof(*buf) == sizeof(st))
  97. memcpy(buf, &st, sizeof(st));
  98. else {
  99. buf->f_type = st.f_type;
  100. buf->f_bsize = st.f_bsize;
  101. buf->f_blocks = st.f_blocks;
  102. buf->f_bfree = st.f_bfree;
  103. buf->f_bavail = st.f_bavail;
  104. buf->f_files = st.f_files;
  105. buf->f_ffree = st.f_ffree;
  106. buf->f_fsid = st.f_fsid;
  107. buf->f_namelen = st.f_namelen;
  108. buf->f_frsize = st.f_frsize;
  109. memset(buf->f_spare, 0, sizeof(buf->f_spare));
  110. }
  111. return 0;
  112. }
  113. asmlinkage long sys_statfs(const char __user * path, struct statfs __user * buf)
  114. {
  115. struct nameidata nd;
  116. int error;
  117. error = user_path_walk(path, &nd);
  118. if (!error) {
  119. struct statfs tmp;
  120. error = vfs_statfs_native(nd.path.dentry, &tmp);
  121. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  122. error = -EFAULT;
  123. path_put(&nd.path);
  124. }
  125. return error;
  126. }
  127. asmlinkage long sys_statfs64(const char __user *path, size_t sz, struct statfs64 __user *buf)
  128. {
  129. struct nameidata nd;
  130. long error;
  131. if (sz != sizeof(*buf))
  132. return -EINVAL;
  133. error = user_path_walk(path, &nd);
  134. if (!error) {
  135. struct statfs64 tmp;
  136. error = vfs_statfs64(nd.path.dentry, &tmp);
  137. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  138. error = -EFAULT;
  139. path_put(&nd.path);
  140. }
  141. return error;
  142. }
  143. asmlinkage long sys_fstatfs(unsigned int fd, struct statfs __user * buf)
  144. {
  145. struct file * file;
  146. struct statfs tmp;
  147. int error;
  148. error = -EBADF;
  149. file = fget(fd);
  150. if (!file)
  151. goto out;
  152. error = vfs_statfs_native(file->f_path.dentry, &tmp);
  153. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  154. error = -EFAULT;
  155. fput(file);
  156. out:
  157. return error;
  158. }
  159. asmlinkage long sys_fstatfs64(unsigned int fd, size_t sz, struct statfs64 __user *buf)
  160. {
  161. struct file * file;
  162. struct statfs64 tmp;
  163. int error;
  164. if (sz != sizeof(*buf))
  165. return -EINVAL;
  166. error = -EBADF;
  167. file = fget(fd);
  168. if (!file)
  169. goto out;
  170. error = vfs_statfs64(file->f_path.dentry, &tmp);
  171. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  172. error = -EFAULT;
  173. fput(file);
  174. out:
  175. return error;
  176. }
  177. int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
  178. struct file *filp)
  179. {
  180. int err;
  181. struct iattr newattrs;
  182. /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
  183. if (length < 0)
  184. return -EINVAL;
  185. newattrs.ia_size = length;
  186. newattrs.ia_valid = ATTR_SIZE | time_attrs;
  187. if (filp) {
  188. newattrs.ia_file = filp;
  189. newattrs.ia_valid |= ATTR_FILE;
  190. }
  191. /* Remove suid/sgid on truncate too */
  192. newattrs.ia_valid |= should_remove_suid(dentry);
  193. mutex_lock(&dentry->d_inode->i_mutex);
  194. err = notify_change(dentry, &newattrs);
  195. mutex_unlock(&dentry->d_inode->i_mutex);
  196. return err;
  197. }
  198. static long do_sys_truncate(const char __user * path, loff_t length)
  199. {
  200. struct nameidata nd;
  201. struct inode * inode;
  202. int error;
  203. error = -EINVAL;
  204. if (length < 0) /* sorry, but loff_t says... */
  205. goto out;
  206. error = user_path_walk(path, &nd);
  207. if (error)
  208. goto out;
  209. inode = nd.path.dentry->d_inode;
  210. /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
  211. error = -EISDIR;
  212. if (S_ISDIR(inode->i_mode))
  213. goto dput_and_out;
  214. error = -EINVAL;
  215. if (!S_ISREG(inode->i_mode))
  216. goto dput_and_out;
  217. error = mnt_want_write(nd.path.mnt);
  218. if (error)
  219. goto dput_and_out;
  220. error = vfs_permission(&nd, MAY_WRITE);
  221. if (error)
  222. goto mnt_drop_write_and_out;
  223. error = -EPERM;
  224. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  225. goto mnt_drop_write_and_out;
  226. error = get_write_access(inode);
  227. if (error)
  228. goto mnt_drop_write_and_out;
  229. /*
  230. * Make sure that there are no leases. get_write_access() protects
  231. * against the truncate racing with a lease-granting setlease().
  232. */
  233. error = break_lease(inode, FMODE_WRITE);
  234. if (error)
  235. goto put_write_and_out;
  236. error = locks_verify_truncate(inode, NULL, length);
  237. if (!error) {
  238. DQUOT_INIT(inode);
  239. error = do_truncate(nd.path.dentry, length, 0, NULL);
  240. }
  241. put_write_and_out:
  242. put_write_access(inode);
  243. mnt_drop_write_and_out:
  244. mnt_drop_write(nd.path.mnt);
  245. dput_and_out:
  246. path_put(&nd.path);
  247. out:
  248. return error;
  249. }
  250. asmlinkage long sys_truncate(const char __user * path, unsigned long length)
  251. {
  252. /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
  253. return do_sys_truncate(path, (long)length);
  254. }
  255. static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
  256. {
  257. struct inode * inode;
  258. struct dentry *dentry;
  259. struct file * file;
  260. int error;
  261. error = -EINVAL;
  262. if (length < 0)
  263. goto out;
  264. error = -EBADF;
  265. file = fget(fd);
  266. if (!file)
  267. goto out;
  268. /* explicitly opened as large or we are on 64-bit box */
  269. if (file->f_flags & O_LARGEFILE)
  270. small = 0;
  271. dentry = file->f_path.dentry;
  272. inode = dentry->d_inode;
  273. error = -EINVAL;
  274. if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
  275. goto out_putf;
  276. error = -EINVAL;
  277. /* Cannot ftruncate over 2^31 bytes without large file support */
  278. if (small && length > MAX_NON_LFS)
  279. goto out_putf;
  280. error = -EPERM;
  281. if (IS_APPEND(inode))
  282. goto out_putf;
  283. error = locks_verify_truncate(inode, file, length);
  284. if (!error)
  285. error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, file);
  286. out_putf:
  287. fput(file);
  288. out:
  289. return error;
  290. }
  291. asmlinkage long sys_ftruncate(unsigned int fd, unsigned long length)
  292. {
  293. long ret = do_sys_ftruncate(fd, length, 1);
  294. /* avoid REGPARM breakage on x86: */
  295. asmlinkage_protect(2, ret, fd, length);
  296. return ret;
  297. }
  298. /* LFS versions of truncate are only needed on 32 bit machines */
  299. #if BITS_PER_LONG == 32
  300. asmlinkage long sys_truncate64(const char __user * path, loff_t length)
  301. {
  302. return do_sys_truncate(path, length);
  303. }
  304. asmlinkage long sys_ftruncate64(unsigned int fd, loff_t length)
  305. {
  306. long ret = do_sys_ftruncate(fd, length, 0);
  307. /* avoid REGPARM breakage on x86: */
  308. asmlinkage_protect(2, ret, fd, length);
  309. return ret;
  310. }
  311. #endif
  312. asmlinkage long sys_fallocate(int fd, int mode, loff_t offset, loff_t len)
  313. {
  314. struct file *file;
  315. struct inode *inode;
  316. long ret = -EINVAL;
  317. if (offset < 0 || len <= 0)
  318. goto out;
  319. /* Return error if mode is not supported */
  320. ret = -EOPNOTSUPP;
  321. if (mode && !(mode & FALLOC_FL_KEEP_SIZE))
  322. goto out;
  323. ret = -EBADF;
  324. file = fget(fd);
  325. if (!file)
  326. goto out;
  327. if (!(file->f_mode & FMODE_WRITE))
  328. goto out_fput;
  329. /*
  330. * Revalidate the write permissions, in case security policy has
  331. * changed since the files were opened.
  332. */
  333. ret = security_file_permission(file, MAY_WRITE);
  334. if (ret)
  335. goto out_fput;
  336. inode = file->f_path.dentry->d_inode;
  337. ret = -ESPIPE;
  338. if (S_ISFIFO(inode->i_mode))
  339. goto out_fput;
  340. ret = -ENODEV;
  341. /*
  342. * Let individual file system decide if it supports preallocation
  343. * for directories or not.
  344. */
  345. if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
  346. goto out_fput;
  347. ret = -EFBIG;
  348. /* Check for wrap through zero too */
  349. if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
  350. goto out_fput;
  351. if (inode->i_op && inode->i_op->fallocate)
  352. ret = inode->i_op->fallocate(inode, mode, offset, len);
  353. else
  354. ret = -EOPNOTSUPP;
  355. out_fput:
  356. fput(file);
  357. out:
  358. return ret;
  359. }
  360. /*
  361. * access() needs to use the real uid/gid, not the effective uid/gid.
  362. * We do this by temporarily clearing all FS-related capabilities and
  363. * switching the fsuid/fsgid around to the real ones.
  364. */
  365. asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode)
  366. {
  367. struct nameidata nd;
  368. int old_fsuid, old_fsgid;
  369. kernel_cap_t uninitialized_var(old_cap); /* !SECURE_NO_SETUID_FIXUP */
  370. int res;
  371. if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
  372. return -EINVAL;
  373. old_fsuid = current->fsuid;
  374. old_fsgid = current->fsgid;
  375. current->fsuid = current->uid;
  376. current->fsgid = current->gid;
  377. if (!issecure(SECURE_NO_SETUID_FIXUP)) {
  378. /*
  379. * Clear the capabilities if we switch to a non-root user
  380. */
  381. #ifndef CONFIG_SECURITY_FILE_CAPABILITIES
  382. /*
  383. * FIXME: There is a race here against sys_capset. The
  384. * capabilities can change yet we will restore the old
  385. * value below. We should hold task_capabilities_lock,
  386. * but we cannot because user_path_walk can sleep.
  387. */
  388. #endif /* ndef CONFIG_SECURITY_FILE_CAPABILITIES */
  389. if (current->uid)
  390. old_cap = cap_set_effective(__cap_empty_set);
  391. else
  392. old_cap = cap_set_effective(current->cap_permitted);
  393. }
  394. res = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW|LOOKUP_ACCESS, &nd);
  395. if (res)
  396. goto out;
  397. res = vfs_permission(&nd, mode);
  398. /* SuS v2 requires we report a read only fs too */
  399. if(res || !(mode & S_IWOTH) ||
  400. special_file(nd.path.dentry->d_inode->i_mode))
  401. goto out_path_release;
  402. /*
  403. * This is a rare case where using __mnt_is_readonly()
  404. * is OK without a mnt_want/drop_write() pair. Since
  405. * no actual write to the fs is performed here, we do
  406. * not need to telegraph to that to anyone.
  407. *
  408. * By doing this, we accept that this access is
  409. * inherently racy and know that the fs may change
  410. * state before we even see this result.
  411. */
  412. if (__mnt_is_readonly(nd.path.mnt))
  413. res = -EROFS;
  414. out_path_release:
  415. path_put(&nd.path);
  416. out:
  417. current->fsuid = old_fsuid;
  418. current->fsgid = old_fsgid;
  419. if (!issecure(SECURE_NO_SETUID_FIXUP))
  420. cap_set_effective(old_cap);
  421. return res;
  422. }
  423. asmlinkage long sys_access(const char __user *filename, int mode)
  424. {
  425. return sys_faccessat(AT_FDCWD, filename, mode);
  426. }
  427. asmlinkage long sys_chdir(const char __user * filename)
  428. {
  429. struct nameidata nd;
  430. int error;
  431. error = __user_walk(filename,
  432. LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_CHDIR, &nd);
  433. if (error)
  434. goto out;
  435. error = vfs_permission(&nd, MAY_EXEC);
  436. if (error)
  437. goto dput_and_out;
  438. set_fs_pwd(current->fs, &nd.path);
  439. dput_and_out:
  440. path_put(&nd.path);
  441. out:
  442. return error;
  443. }
  444. asmlinkage long sys_fchdir(unsigned int fd)
  445. {
  446. struct file *file;
  447. struct inode *inode;
  448. int error;
  449. error = -EBADF;
  450. file = fget(fd);
  451. if (!file)
  452. goto out;
  453. inode = file->f_path.dentry->d_inode;
  454. error = -ENOTDIR;
  455. if (!S_ISDIR(inode->i_mode))
  456. goto out_putf;
  457. error = file_permission(file, MAY_EXEC);
  458. if (!error)
  459. set_fs_pwd(current->fs, &file->f_path);
  460. out_putf:
  461. fput(file);
  462. out:
  463. return error;
  464. }
  465. asmlinkage long sys_chroot(const char __user * filename)
  466. {
  467. struct nameidata nd;
  468. int error;
  469. error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
  470. if (error)
  471. goto out;
  472. error = vfs_permission(&nd, MAY_EXEC);
  473. if (error)
  474. goto dput_and_out;
  475. error = -EPERM;
  476. if (!capable(CAP_SYS_CHROOT))
  477. goto dput_and_out;
  478. set_fs_root(current->fs, &nd.path);
  479. set_fs_altroot();
  480. error = 0;
  481. dput_and_out:
  482. path_put(&nd.path);
  483. out:
  484. return error;
  485. }
  486. asmlinkage long sys_fchmod(unsigned int fd, mode_t mode)
  487. {
  488. struct inode * inode;
  489. struct dentry * dentry;
  490. struct file * file;
  491. int err = -EBADF;
  492. struct iattr newattrs;
  493. file = fget(fd);
  494. if (!file)
  495. goto out;
  496. dentry = file->f_path.dentry;
  497. inode = dentry->d_inode;
  498. audit_inode(NULL, dentry);
  499. err = mnt_want_write(file->f_path.mnt);
  500. if (err)
  501. goto out_putf;
  502. err = -EPERM;
  503. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  504. goto out_drop_write;
  505. mutex_lock(&inode->i_mutex);
  506. if (mode == (mode_t) -1)
  507. mode = inode->i_mode;
  508. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  509. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  510. err = notify_change(dentry, &newattrs);
  511. mutex_unlock(&inode->i_mutex);
  512. out_drop_write:
  513. mnt_drop_write(file->f_path.mnt);
  514. out_putf:
  515. fput(file);
  516. out:
  517. return err;
  518. }
  519. asmlinkage long sys_fchmodat(int dfd, const char __user *filename,
  520. mode_t mode)
  521. {
  522. struct nameidata nd;
  523. struct inode * inode;
  524. int error;
  525. struct iattr newattrs;
  526. error = __user_walk_fd(dfd, filename, LOOKUP_FOLLOW, &nd);
  527. if (error)
  528. goto out;
  529. inode = nd.path.dentry->d_inode;
  530. error = mnt_want_write(nd.path.mnt);
  531. if (error)
  532. goto dput_and_out;
  533. error = -EPERM;
  534. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  535. goto out_drop_write;
  536. mutex_lock(&inode->i_mutex);
  537. if (mode == (mode_t) -1)
  538. mode = inode->i_mode;
  539. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  540. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  541. error = notify_change(nd.path.dentry, &newattrs);
  542. mutex_unlock(&inode->i_mutex);
  543. out_drop_write:
  544. mnt_drop_write(nd.path.mnt);
  545. dput_and_out:
  546. path_put(&nd.path);
  547. out:
  548. return error;
  549. }
  550. asmlinkage long sys_chmod(const char __user *filename, mode_t mode)
  551. {
  552. return sys_fchmodat(AT_FDCWD, filename, mode);
  553. }
  554. static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
  555. {
  556. struct inode * inode;
  557. int error;
  558. struct iattr newattrs;
  559. error = -ENOENT;
  560. if (!(inode = dentry->d_inode)) {
  561. printk(KERN_ERR "chown_common: NULL inode\n");
  562. goto out;
  563. }
  564. error = -EPERM;
  565. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  566. goto out;
  567. newattrs.ia_valid = ATTR_CTIME;
  568. if (user != (uid_t) -1) {
  569. newattrs.ia_valid |= ATTR_UID;
  570. newattrs.ia_uid = user;
  571. }
  572. if (group != (gid_t) -1) {
  573. newattrs.ia_valid |= ATTR_GID;
  574. newattrs.ia_gid = group;
  575. }
  576. if (!S_ISDIR(inode->i_mode))
  577. newattrs.ia_valid |=
  578. ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
  579. mutex_lock(&inode->i_mutex);
  580. error = notify_change(dentry, &newattrs);
  581. mutex_unlock(&inode->i_mutex);
  582. out:
  583. return error;
  584. }
  585. asmlinkage long sys_chown(const char __user * filename, uid_t user, gid_t group)
  586. {
  587. struct nameidata nd;
  588. int error;
  589. error = user_path_walk(filename, &nd);
  590. if (error)
  591. goto out;
  592. error = mnt_want_write(nd.path.mnt);
  593. if (error)
  594. goto out_release;
  595. error = chown_common(nd.path.dentry, user, group);
  596. mnt_drop_write(nd.path.mnt);
  597. out_release:
  598. path_put(&nd.path);
  599. out:
  600. return error;
  601. }
  602. asmlinkage long sys_fchownat(int dfd, const char __user *filename, uid_t user,
  603. gid_t group, int flag)
  604. {
  605. struct nameidata nd;
  606. int error = -EINVAL;
  607. int follow;
  608. if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
  609. goto out;
  610. follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  611. error = __user_walk_fd(dfd, filename, follow, &nd);
  612. if (error)
  613. goto out;
  614. error = mnt_want_write(nd.path.mnt);
  615. if (error)
  616. goto out_release;
  617. error = chown_common(nd.path.dentry, user, group);
  618. mnt_drop_write(nd.path.mnt);
  619. out_release:
  620. path_put(&nd.path);
  621. out:
  622. return error;
  623. }
  624. asmlinkage long sys_lchown(const char __user * filename, uid_t user, gid_t group)
  625. {
  626. struct nameidata nd;
  627. int error;
  628. error = user_path_walk_link(filename, &nd);
  629. if (error)
  630. goto out;
  631. error = mnt_want_write(nd.path.mnt);
  632. if (error)
  633. goto out_release;
  634. error = chown_common(nd.path.dentry, user, group);
  635. mnt_drop_write(nd.path.mnt);
  636. out_release:
  637. path_put(&nd.path);
  638. out:
  639. return error;
  640. }
  641. asmlinkage long sys_fchown(unsigned int fd, uid_t user, gid_t group)
  642. {
  643. struct file * file;
  644. int error = -EBADF;
  645. struct dentry * dentry;
  646. file = fget(fd);
  647. if (!file)
  648. goto out;
  649. error = mnt_want_write(file->f_path.mnt);
  650. if (error)
  651. goto out_fput;
  652. dentry = file->f_path.dentry;
  653. audit_inode(NULL, dentry);
  654. error = chown_common(dentry, user, group);
  655. mnt_drop_write(file->f_path.mnt);
  656. out_fput:
  657. fput(file);
  658. out:
  659. return error;
  660. }
  661. /*
  662. * You have to be very careful that these write
  663. * counts get cleaned up in error cases and
  664. * upon __fput(). This should probably never
  665. * be called outside of __dentry_open().
  666. */
  667. static inline int __get_file_write_access(struct inode *inode,
  668. struct vfsmount *mnt)
  669. {
  670. int error;
  671. error = get_write_access(inode);
  672. if (error)
  673. return error;
  674. /*
  675. * Do not take mount writer counts on
  676. * special files since no writes to
  677. * the mount itself will occur.
  678. */
  679. if (!special_file(inode->i_mode)) {
  680. /*
  681. * Balanced in __fput()
  682. */
  683. error = mnt_want_write(mnt);
  684. if (error)
  685. put_write_access(inode);
  686. }
  687. return error;
  688. }
  689. static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
  690. int flags, struct file *f,
  691. int (*open)(struct inode *, struct file *))
  692. {
  693. struct inode *inode;
  694. int error;
  695. f->f_flags = flags;
  696. f->f_mode = ((flags+1) & O_ACCMODE) | FMODE_LSEEK |
  697. FMODE_PREAD | FMODE_PWRITE;
  698. inode = dentry->d_inode;
  699. if (f->f_mode & FMODE_WRITE) {
  700. error = __get_file_write_access(inode, mnt);
  701. if (error)
  702. goto cleanup_file;
  703. if (!special_file(inode->i_mode))
  704. file_take_write(f);
  705. }
  706. f->f_mapping = inode->i_mapping;
  707. f->f_path.dentry = dentry;
  708. f->f_path.mnt = mnt;
  709. f->f_pos = 0;
  710. f->f_op = fops_get(inode->i_fop);
  711. file_move(f, &inode->i_sb->s_files);
  712. error = security_dentry_open(f);
  713. if (error)
  714. goto cleanup_all;
  715. if (!open && f->f_op)
  716. open = f->f_op->open;
  717. if (open) {
  718. error = open(inode, f);
  719. if (error)
  720. goto cleanup_all;
  721. }
  722. f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  723. file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
  724. /* NB: we're sure to have correct a_ops only after f_op->open */
  725. if (f->f_flags & O_DIRECT) {
  726. if (!f->f_mapping->a_ops ||
  727. ((!f->f_mapping->a_ops->direct_IO) &&
  728. (!f->f_mapping->a_ops->get_xip_mem))) {
  729. fput(f);
  730. f = ERR_PTR(-EINVAL);
  731. }
  732. }
  733. return f;
  734. cleanup_all:
  735. fops_put(f->f_op);
  736. if (f->f_mode & FMODE_WRITE) {
  737. put_write_access(inode);
  738. if (!special_file(inode->i_mode)) {
  739. /*
  740. * We don't consider this a real
  741. * mnt_want/drop_write() pair
  742. * because it all happenend right
  743. * here, so just reset the state.
  744. */
  745. file_reset_write(f);
  746. mnt_drop_write(mnt);
  747. }
  748. }
  749. file_kill(f);
  750. f->f_path.dentry = NULL;
  751. f->f_path.mnt = NULL;
  752. cleanup_file:
  753. put_filp(f);
  754. dput(dentry);
  755. mntput(mnt);
  756. return ERR_PTR(error);
  757. }
  758. /**
  759. * lookup_instantiate_filp - instantiates the open intent filp
  760. * @nd: pointer to nameidata
  761. * @dentry: pointer to dentry
  762. * @open: open callback
  763. *
  764. * Helper for filesystems that want to use lookup open intents and pass back
  765. * a fully instantiated struct file to the caller.
  766. * This function is meant to be called from within a filesystem's
  767. * lookup method.
  768. * Beware of calling it for non-regular files! Those ->open methods might block
  769. * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
  770. * leading to a deadlock, as nobody can open that fifo anymore, because
  771. * another process to open fifo will block on locked parent when doing lookup).
  772. * Note that in case of error, nd->intent.open.file is destroyed, but the
  773. * path information remains valid.
  774. * If the open callback is set to NULL, then the standard f_op->open()
  775. * filesystem callback is substituted.
  776. */
  777. struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
  778. int (*open)(struct inode *, struct file *))
  779. {
  780. if (IS_ERR(nd->intent.open.file))
  781. goto out;
  782. if (IS_ERR(dentry))
  783. goto out_err;
  784. nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->path.mnt),
  785. nd->intent.open.flags - 1,
  786. nd->intent.open.file,
  787. open);
  788. out:
  789. return nd->intent.open.file;
  790. out_err:
  791. release_open_intent(nd);
  792. nd->intent.open.file = (struct file *)dentry;
  793. goto out;
  794. }
  795. EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
  796. /**
  797. * nameidata_to_filp - convert a nameidata to an open filp.
  798. * @nd: pointer to nameidata
  799. * @flags: open flags
  800. *
  801. * Note that this function destroys the original nameidata
  802. */
  803. struct file *nameidata_to_filp(struct nameidata *nd, int flags)
  804. {
  805. struct file *filp;
  806. /* Pick up the filp from the open intent */
  807. filp = nd->intent.open.file;
  808. /* Has the filesystem initialised the file for us? */
  809. if (filp->f_path.dentry == NULL)
  810. filp = __dentry_open(nd->path.dentry, nd->path.mnt, flags, filp,
  811. NULL);
  812. else
  813. path_put(&nd->path);
  814. return filp;
  815. }
  816. /*
  817. * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
  818. * error.
  819. */
  820. struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags)
  821. {
  822. int error;
  823. struct file *f;
  824. /*
  825. * We must always pass in a valid mount pointer. Historically
  826. * callers got away with not passing it, but we must enforce this at
  827. * the earliest possible point now to avoid strange problems deep in the
  828. * filesystem stack.
  829. */
  830. if (!mnt) {
  831. printk(KERN_WARNING "%s called with NULL vfsmount\n", __func__);
  832. dump_stack();
  833. return ERR_PTR(-EINVAL);
  834. }
  835. error = -ENFILE;
  836. f = get_empty_filp();
  837. if (f == NULL) {
  838. dput(dentry);
  839. mntput(mnt);
  840. return ERR_PTR(error);
  841. }
  842. return __dentry_open(dentry, mnt, flags, f, NULL);
  843. }
  844. EXPORT_SYMBOL(dentry_open);
  845. /*
  846. * Find an empty file descriptor entry, and mark it busy.
  847. */
  848. int get_unused_fd_flags(int flags)
  849. {
  850. struct files_struct * files = current->files;
  851. int fd, error;
  852. struct fdtable *fdt;
  853. error = -EMFILE;
  854. spin_lock(&files->file_lock);
  855. repeat:
  856. fdt = files_fdtable(files);
  857. fd = find_next_zero_bit(fdt->open_fds->fds_bits, fdt->max_fds,
  858. files->next_fd);
  859. /*
  860. * N.B. For clone tasks sharing a files structure, this test
  861. * will limit the total number of files that can be opened.
  862. */
  863. if (fd >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
  864. goto out;
  865. /* Do we need to expand the fd array or fd set? */
  866. error = expand_files(files, fd);
  867. if (error < 0)
  868. goto out;
  869. if (error) {
  870. /*
  871. * If we needed to expand the fs array we
  872. * might have blocked - try again.
  873. */
  874. error = -EMFILE;
  875. goto repeat;
  876. }
  877. FD_SET(fd, fdt->open_fds);
  878. if (flags & O_CLOEXEC)
  879. FD_SET(fd, fdt->close_on_exec);
  880. else
  881. FD_CLR(fd, fdt->close_on_exec);
  882. files->next_fd = fd + 1;
  883. #if 1
  884. /* Sanity check */
  885. if (fdt->fd[fd] != NULL) {
  886. printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
  887. fdt->fd[fd] = NULL;
  888. }
  889. #endif
  890. error = fd;
  891. out:
  892. spin_unlock(&files->file_lock);
  893. return error;
  894. }
  895. int get_unused_fd(void)
  896. {
  897. return get_unused_fd_flags(0);
  898. }
  899. EXPORT_SYMBOL(get_unused_fd);
  900. static void __put_unused_fd(struct files_struct *files, unsigned int fd)
  901. {
  902. struct fdtable *fdt = files_fdtable(files);
  903. __FD_CLR(fd, fdt->open_fds);
  904. if (fd < files->next_fd)
  905. files->next_fd = fd;
  906. }
  907. void put_unused_fd(unsigned int fd)
  908. {
  909. struct files_struct *files = current->files;
  910. spin_lock(&files->file_lock);
  911. __put_unused_fd(files, fd);
  912. spin_unlock(&files->file_lock);
  913. }
  914. EXPORT_SYMBOL(put_unused_fd);
  915. /*
  916. * Install a file pointer in the fd array.
  917. *
  918. * The VFS is full of places where we drop the files lock between
  919. * setting the open_fds bitmap and installing the file in the file
  920. * array. At any such point, we are vulnerable to a dup2() race
  921. * installing a file in the array before us. We need to detect this and
  922. * fput() the struct file we are about to overwrite in this case.
  923. *
  924. * It should never happen - if we allow dup2() do it, _really_ bad things
  925. * will follow.
  926. */
  927. void fd_install(unsigned int fd, struct file *file)
  928. {
  929. struct files_struct *files = current->files;
  930. struct fdtable *fdt;
  931. spin_lock(&files->file_lock);
  932. fdt = files_fdtable(files);
  933. BUG_ON(fdt->fd[fd] != NULL);
  934. rcu_assign_pointer(fdt->fd[fd], file);
  935. spin_unlock(&files->file_lock);
  936. }
  937. EXPORT_SYMBOL(fd_install);
  938. long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
  939. {
  940. char *tmp = getname(filename);
  941. int fd = PTR_ERR(tmp);
  942. if (!IS_ERR(tmp)) {
  943. fd = get_unused_fd_flags(flags);
  944. if (fd >= 0) {
  945. struct file *f = do_filp_open(dfd, tmp, flags, mode);
  946. if (IS_ERR(f)) {
  947. put_unused_fd(fd);
  948. fd = PTR_ERR(f);
  949. } else {
  950. fsnotify_open(f->f_path.dentry);
  951. fd_install(fd, f);
  952. }
  953. }
  954. putname(tmp);
  955. }
  956. return fd;
  957. }
  958. asmlinkage long sys_open(const char __user *filename, int flags, int mode)
  959. {
  960. long ret;
  961. if (force_o_largefile())
  962. flags |= O_LARGEFILE;
  963. ret = do_sys_open(AT_FDCWD, filename, flags, mode);
  964. /* avoid REGPARM breakage on x86: */
  965. asmlinkage_protect(3, ret, filename, flags, mode);
  966. return ret;
  967. }
  968. asmlinkage long sys_openat(int dfd, const char __user *filename, int flags,
  969. int mode)
  970. {
  971. long ret;
  972. if (force_o_largefile())
  973. flags |= O_LARGEFILE;
  974. ret = do_sys_open(dfd, filename, flags, mode);
  975. /* avoid REGPARM breakage on x86: */
  976. asmlinkage_protect(4, ret, dfd, filename, flags, mode);
  977. return ret;
  978. }
  979. #ifndef __alpha__
  980. /*
  981. * For backward compatibility? Maybe this should be moved
  982. * into arch/i386 instead?
  983. */
  984. asmlinkage long sys_creat(const char __user * pathname, int mode)
  985. {
  986. return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
  987. }
  988. #endif
  989. /*
  990. * "id" is the POSIX thread ID. We use the
  991. * files pointer for this..
  992. */
  993. int filp_close(struct file *filp, fl_owner_t id)
  994. {
  995. int retval = 0;
  996. if (!file_count(filp)) {
  997. printk(KERN_ERR "VFS: Close: file count is 0\n");
  998. return 0;
  999. }
  1000. if (filp->f_op && filp->f_op->flush)
  1001. retval = filp->f_op->flush(filp, id);
  1002. dnotify_flush(filp, id);
  1003. locks_remove_posix(filp, id);
  1004. fput(filp);
  1005. return retval;
  1006. }
  1007. EXPORT_SYMBOL(filp_close);
  1008. /*
  1009. * Careful here! We test whether the file pointer is NULL before
  1010. * releasing the fd. This ensures that one clone task can't release
  1011. * an fd while another clone is opening it.
  1012. */
  1013. asmlinkage long sys_close(unsigned int fd)
  1014. {
  1015. struct file * filp;
  1016. struct files_struct *files = current->files;
  1017. struct fdtable *fdt;
  1018. int retval;
  1019. spin_lock(&files->file_lock);
  1020. fdt = files_fdtable(files);
  1021. if (fd >= fdt->max_fds)
  1022. goto out_unlock;
  1023. filp = fdt->fd[fd];
  1024. if (!filp)
  1025. goto out_unlock;
  1026. rcu_assign_pointer(fdt->fd[fd], NULL);
  1027. FD_CLR(fd, fdt->close_on_exec);
  1028. __put_unused_fd(files, fd);
  1029. spin_unlock(&files->file_lock);
  1030. retval = filp_close(filp, files);
  1031. /* can't restart close syscall because file table entry was cleared */
  1032. if (unlikely(retval == -ERESTARTSYS ||
  1033. retval == -ERESTARTNOINTR ||
  1034. retval == -ERESTARTNOHAND ||
  1035. retval == -ERESTART_RESTARTBLOCK))
  1036. retval = -EINTR;
  1037. return retval;
  1038. out_unlock:
  1039. spin_unlock(&files->file_lock);
  1040. return -EBADF;
  1041. }
  1042. EXPORT_SYMBOL(sys_close);
  1043. /*
  1044. * This routine simulates a hangup on the tty, to arrange that users
  1045. * are given clean terminals at login time.
  1046. */
  1047. asmlinkage long sys_vhangup(void)
  1048. {
  1049. if (capable(CAP_SYS_TTY_CONFIG)) {
  1050. /* XXX: this needs locking */
  1051. tty_vhangup(current->signal->tty);
  1052. return 0;
  1053. }
  1054. return -EPERM;
  1055. }
  1056. /*
  1057. * Called when an inode is about to be open.
  1058. * We use this to disallow opening large files on 32bit systems if
  1059. * the caller didn't specify O_LARGEFILE. On 64bit systems we force
  1060. * on this flag in sys_open.
  1061. */
  1062. int generic_file_open(struct inode * inode, struct file * filp)
  1063. {
  1064. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  1065. return -EOVERFLOW;
  1066. return 0;
  1067. }
  1068. EXPORT_SYMBOL(generic_file_open);
  1069. /*
  1070. * This is used by subsystems that don't want seekable
  1071. * file descriptors
  1072. */
  1073. int nonseekable_open(struct inode *inode, struct file *filp)
  1074. {
  1075. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  1076. return 0;
  1077. }
  1078. EXPORT_SYMBOL(nonseekable_open);