PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/fs/open.c

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