open.c 27 KB

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