open.c 27 KB

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