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. int vfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  32. {
  33. int retval = -ENODEV;
  34. if (dentry) {
  35. retval = -ENOSYS;
  36. if (dentry->d_sb->s_op->statfs) {
  37. memset(buf, 0, sizeof(*buf));
  38. retval = security_sb_statfs(dentry);
  39. if (retval)
  40. return retval;
  41. retval = dentry->d_sb->s_op->statfs(dentry, buf);
  42. if (retval == 0 && buf->f_frsize == 0)
  43. buf->f_frsize = buf->f_bsize;
  44. }
  45. }
  46. return retval;
  47. }
  48. EXPORT_SYMBOL(vfs_statfs);
  49. static int vfs_statfs_native(struct dentry *dentry, struct statfs *buf)
  50. {
  51. struct kstatfs st;
  52. int retval;
  53. retval = vfs_statfs(dentry, &st);
  54. if (retval)
  55. return retval;
  56. if (sizeof(*buf) == sizeof(st))
  57. memcpy(buf, &st, sizeof(st));
  58. else {
  59. if (sizeof buf->f_blocks == 4) {
  60. if ((st.f_blocks | st.f_bfree | st.f_bavail |
  61. st.f_bsize | st.f_frsize) &
  62. 0xffffffff00000000ULL)
  63. return -EOVERFLOW;
  64. /*
  65. * f_files and f_ffree may be -1; it's okay to stuff
  66. * that into 32 bits
  67. */
  68. if (st.f_files != -1 &&
  69. (st.f_files & 0xffffffff00000000ULL))
  70. return -EOVERFLOW;
  71. if (st.f_ffree != -1 &&
  72. (st.f_ffree & 0xffffffff00000000ULL))
  73. return -EOVERFLOW;
  74. }
  75. buf->f_type = st.f_type;
  76. buf->f_bsize = st.f_bsize;
  77. buf->f_blocks = st.f_blocks;
  78. buf->f_bfree = st.f_bfree;
  79. buf->f_bavail = st.f_bavail;
  80. buf->f_files = st.f_files;
  81. buf->f_ffree = st.f_ffree;
  82. buf->f_fsid = st.f_fsid;
  83. buf->f_namelen = st.f_namelen;
  84. buf->f_frsize = st.f_frsize;
  85. memset(buf->f_spare, 0, sizeof(buf->f_spare));
  86. }
  87. return 0;
  88. }
  89. static int vfs_statfs64(struct dentry *dentry, struct statfs64 *buf)
  90. {
  91. struct kstatfs st;
  92. int retval;
  93. retval = vfs_statfs(dentry, &st);
  94. if (retval)
  95. return retval;
  96. if (sizeof(*buf) == sizeof(st))
  97. memcpy(buf, &st, sizeof(st));
  98. else {
  99. buf->f_type = st.f_type;
  100. buf->f_bsize = st.f_bsize;
  101. buf->f_blocks = st.f_blocks;
  102. buf->f_bfree = st.f_bfree;
  103. buf->f_bavail = st.f_bavail;
  104. buf->f_files = st.f_files;
  105. buf->f_ffree = st.f_ffree;
  106. buf->f_fsid = st.f_fsid;
  107. buf->f_namelen = st.f_namelen;
  108. buf->f_frsize = st.f_frsize;
  109. memset(buf->f_spare, 0, sizeof(buf->f_spare));
  110. }
  111. return 0;
  112. }
  113. SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs __user *, buf)
  114. {
  115. struct path path;
  116. int error;
  117. error = user_path(pathname, &path);
  118. if (!error) {
  119. struct statfs tmp;
  120. error = vfs_statfs_native(path.dentry, &tmp);
  121. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  122. error = -EFAULT;
  123. path_put(&path);
  124. }
  125. return error;
  126. }
  127. SYSCALL_DEFINE3(statfs64, const char __user *, pathname, size_t, sz, struct statfs64 __user *, buf)
  128. {
  129. struct path path;
  130. long error;
  131. if (sz != sizeof(*buf))
  132. return -EINVAL;
  133. error = user_path(pathname, &path);
  134. if (!error) {
  135. struct statfs64 tmp;
  136. error = vfs_statfs64(path.dentry, &tmp);
  137. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  138. error = -EFAULT;
  139. path_put(&path);
  140. }
  141. return error;
  142. }
  143. SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct statfs __user *, buf)
  144. {
  145. struct file * file;
  146. struct statfs tmp;
  147. int error;
  148. error = -EBADF;
  149. file = fget(fd);
  150. if (!file)
  151. goto out;
  152. error = vfs_statfs_native(file->f_path.dentry, &tmp);
  153. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  154. error = -EFAULT;
  155. fput(file);
  156. out:
  157. return error;
  158. }
  159. SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, size_t, sz, struct statfs64 __user *, buf)
  160. {
  161. struct file * file;
  162. struct statfs64 tmp;
  163. int error;
  164. if (sz != sizeof(*buf))
  165. return -EINVAL;
  166. error = -EBADF;
  167. file = fget(fd);
  168. if (!file)
  169. goto out;
  170. error = vfs_statfs64(file->f_path.dentry, &tmp);
  171. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  172. error = -EFAULT;
  173. fput(file);
  174. out:
  175. return error;
  176. }
  177. int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
  178. struct file *filp)
  179. {
  180. int err;
  181. struct iattr newattrs;
  182. /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
  183. if (length < 0)
  184. return -EINVAL;
  185. newattrs.ia_size = length;
  186. newattrs.ia_valid = ATTR_SIZE | time_attrs;
  187. if (filp) {
  188. newattrs.ia_file = filp;
  189. newattrs.ia_valid |= ATTR_FILE;
  190. }
  191. /* Remove suid/sgid on truncate too */
  192. newattrs.ia_valid |= should_remove_suid(dentry);
  193. mutex_lock(&dentry->d_inode->i_mutex);
  194. err = notify_change(dentry, &newattrs);
  195. mutex_unlock(&dentry->d_inode->i_mutex);
  196. return err;
  197. }
  198. static long do_sys_truncate(const char __user *pathname, loff_t length)
  199. {
  200. struct path path;
  201. struct inode *inode;
  202. int error;
  203. error = -EINVAL;
  204. if (length < 0) /* sorry, but loff_t says... */
  205. goto out;
  206. error = user_path(pathname, &path);
  207. if (error)
  208. goto out;
  209. inode = path.dentry->d_inode;
  210. /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
  211. error = -EISDIR;
  212. if (S_ISDIR(inode->i_mode))
  213. goto dput_and_out;
  214. error = -EINVAL;
  215. if (!S_ISREG(inode->i_mode))
  216. goto dput_and_out;
  217. error = mnt_want_write(path.mnt);
  218. if (error)
  219. goto dput_and_out;
  220. error = inode_permission(inode, MAY_WRITE);
  221. if (error)
  222. goto mnt_drop_write_and_out;
  223. error = -EPERM;
  224. if (IS_APPEND(inode))
  225. goto mnt_drop_write_and_out;
  226. error = get_write_access(inode);
  227. if (error)
  228. goto mnt_drop_write_and_out;
  229. /*
  230. * Make sure that there are no leases. get_write_access() protects
  231. * against the truncate racing with a lease-granting setlease().
  232. */
  233. error = break_lease(inode, FMODE_WRITE);
  234. if (error)
  235. goto put_write_and_out;
  236. error = locks_verify_truncate(inode, NULL, length);
  237. if (!error)
  238. error = security_path_truncate(&path, length, 0);
  239. if (!error) {
  240. DQUOT_INIT(inode);
  241. error = do_truncate(path.dentry, length, 0, NULL);
  242. }
  243. put_write_and_out:
  244. put_write_access(inode);
  245. mnt_drop_write_and_out:
  246. mnt_drop_write(path.mnt);
  247. dput_and_out:
  248. path_put(&path);
  249. out:
  250. return error;
  251. }
  252. SYSCALL_DEFINE2(truncate, const char __user *, path, unsigned long, length)
  253. {
  254. /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
  255. return do_sys_truncate(path, (long)length);
  256. }
  257. static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
  258. {
  259. struct inode * inode;
  260. struct dentry *dentry;
  261. struct file * file;
  262. int error;
  263. error = -EINVAL;
  264. if (length < 0)
  265. goto out;
  266. error = -EBADF;
  267. file = fget(fd);
  268. if (!file)
  269. goto out;
  270. /* explicitly opened as large or we are on 64-bit box */
  271. if (file->f_flags & O_LARGEFILE)
  272. small = 0;
  273. dentry = file->f_path.dentry;
  274. inode = dentry->d_inode;
  275. error = -EINVAL;
  276. if (!S_ISREG(inode->i_mode) || !(file->f_mode & FMODE_WRITE))
  277. goto out_putf;
  278. error = -EINVAL;
  279. /* Cannot ftruncate over 2^31 bytes without large file support */
  280. if (small && length > MAX_NON_LFS)
  281. goto out_putf;
  282. error = -EPERM;
  283. if (IS_APPEND(inode))
  284. goto out_putf;
  285. error = locks_verify_truncate(inode, file, length);
  286. if (!error)
  287. error = security_path_truncate(&file->f_path, length,
  288. ATTR_MTIME|ATTR_CTIME);
  289. if (!error)
  290. error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, file);
  291. out_putf:
  292. fput(file);
  293. out:
  294. return error;
  295. }
  296. SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
  297. {
  298. long ret = do_sys_ftruncate(fd, length, 1);
  299. /* avoid REGPARM breakage on x86: */
  300. asmlinkage_protect(2, ret, fd, length);
  301. return ret;
  302. }
  303. /* LFS versions of truncate are only needed on 32 bit machines */
  304. #if BITS_PER_LONG == 32
  305. SYSCALL_DEFINE(truncate64)(const char __user * path, loff_t length)
  306. {
  307. return do_sys_truncate(path, length);
  308. }
  309. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  310. asmlinkage long SyS_truncate64(long path, loff_t length)
  311. {
  312. return SYSC_truncate64((const char __user *) path, length);
  313. }
  314. SYSCALL_ALIAS(sys_truncate64, SyS_truncate64);
  315. #endif
  316. SYSCALL_DEFINE(ftruncate64)(unsigned int fd, loff_t length)
  317. {
  318. long ret = do_sys_ftruncate(fd, length, 0);
  319. /* avoid REGPARM breakage on x86: */
  320. asmlinkage_protect(2, ret, fd, length);
  321. return ret;
  322. }
  323. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  324. asmlinkage long SyS_ftruncate64(long fd, loff_t length)
  325. {
  326. return SYSC_ftruncate64((unsigned int) fd, length);
  327. }
  328. SYSCALL_ALIAS(sys_ftruncate64, SyS_ftruncate64);
  329. #endif
  330. #endif /* BITS_PER_LONG == 32 */
  331. SYSCALL_DEFINE(fallocate)(int fd, int mode, loff_t offset, loff_t len)
  332. {
  333. struct file *file;
  334. struct inode *inode;
  335. long ret = -EINVAL;
  336. if (offset < 0 || len <= 0)
  337. goto out;
  338. /* Return error if mode is not supported */
  339. ret = -EOPNOTSUPP;
  340. if (mode && !(mode & FALLOC_FL_KEEP_SIZE))
  341. goto out;
  342. ret = -EBADF;
  343. file = fget(fd);
  344. if (!file)
  345. goto out;
  346. if (!(file->f_mode & FMODE_WRITE))
  347. goto out_fput;
  348. /*
  349. * Revalidate the write permissions, in case security policy has
  350. * changed since the files were opened.
  351. */
  352. ret = security_file_permission(file, MAY_WRITE);
  353. if (ret)
  354. goto out_fput;
  355. inode = file->f_path.dentry->d_inode;
  356. ret = -ESPIPE;
  357. if (S_ISFIFO(inode->i_mode))
  358. goto out_fput;
  359. ret = -ENODEV;
  360. /*
  361. * Let individual file system decide if it supports preallocation
  362. * for directories or not.
  363. */
  364. if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
  365. goto out_fput;
  366. ret = -EFBIG;
  367. /* Check for wrap through zero too */
  368. if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
  369. goto out_fput;
  370. if (inode->i_op->fallocate)
  371. ret = inode->i_op->fallocate(inode, mode, offset, len);
  372. else
  373. ret = -EOPNOTSUPP;
  374. out_fput:
  375. fput(file);
  376. out:
  377. return ret;
  378. }
  379. #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
  380. asmlinkage long SyS_fallocate(long fd, long mode, loff_t offset, loff_t len)
  381. {
  382. return SYSC_fallocate((int)fd, (int)mode, offset, len);
  383. }
  384. SYSCALL_ALIAS(sys_fallocate, SyS_fallocate);
  385. #endif
  386. /*
  387. * access() needs to use the real uid/gid, not the effective uid/gid.
  388. * We do this by temporarily clearing all FS-related capabilities and
  389. * switching the fsuid/fsgid around to the real ones.
  390. */
  391. asmlinkage long sys_faccessat(int dfd, const char __user *filename, int mode)
  392. {
  393. const struct cred *old_cred;
  394. struct cred *override_cred;
  395. struct path path;
  396. struct inode *inode;
  397. int res;
  398. if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
  399. return -EINVAL;
  400. override_cred = prepare_creds();
  401. if (!override_cred)
  402. return -ENOMEM;
  403. override_cred->fsuid = override_cred->uid;
  404. override_cred->fsgid = override_cred->gid;
  405. if (!issecure(SECURE_NO_SETUID_FIXUP)) {
  406. /* Clear the capabilities if we switch to a non-root user */
  407. if (override_cred->uid)
  408. cap_clear(override_cred->cap_effective);
  409. else
  410. override_cred->cap_effective =
  411. override_cred->cap_permitted;
  412. }
  413. old_cred = override_creds(override_cred);
  414. res = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
  415. if (res)
  416. goto out;
  417. inode = path.dentry->d_inode;
  418. if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
  419. /*
  420. * MAY_EXEC on regular files is denied if the fs is mounted
  421. * with the "noexec" flag.
  422. */
  423. res = -EACCES;
  424. if (path.mnt->mnt_flags & MNT_NOEXEC)
  425. goto out_path_release;
  426. }
  427. res = inode_permission(inode, mode | MAY_ACCESS);
  428. /* SuS v2 requires we report a read only fs too */
  429. if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
  430. goto out_path_release;
  431. /*
  432. * This is a rare case where using __mnt_is_readonly()
  433. * is OK without a mnt_want/drop_write() pair. Since
  434. * no actual write to the fs is performed here, we do
  435. * not need to telegraph to that to anyone.
  436. *
  437. * By doing this, we accept that this access is
  438. * inherently racy and know that the fs may change
  439. * state before we even see this result.
  440. */
  441. if (__mnt_is_readonly(path.mnt))
  442. res = -EROFS;
  443. out_path_release:
  444. path_put(&path);
  445. out:
  446. revert_creds(old_cred);
  447. put_cred(override_cred);
  448. return res;
  449. }
  450. SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
  451. {
  452. return sys_faccessat(AT_FDCWD, filename, mode);
  453. }
  454. SYSCALL_DEFINE1(chdir, const char __user *, filename)
  455. {
  456. struct path path;
  457. int error;
  458. error = user_path_dir(filename, &path);
  459. if (error)
  460. goto out;
  461. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_ACCESS);
  462. if (error)
  463. goto dput_and_out;
  464. set_fs_pwd(current->fs, &path);
  465. dput_and_out:
  466. path_put(&path);
  467. out:
  468. return error;
  469. }
  470. SYSCALL_DEFINE1(fchdir, unsigned int, fd)
  471. {
  472. struct file *file;
  473. struct inode *inode;
  474. int error;
  475. error = -EBADF;
  476. file = fget(fd);
  477. if (!file)
  478. goto out;
  479. inode = file->f_path.dentry->d_inode;
  480. error = -ENOTDIR;
  481. if (!S_ISDIR(inode->i_mode))
  482. goto out_putf;
  483. error = inode_permission(inode, MAY_EXEC | MAY_ACCESS);
  484. if (!error)
  485. set_fs_pwd(current->fs, &file->f_path);
  486. out_putf:
  487. fput(file);
  488. out:
  489. return error;
  490. }
  491. SYSCALL_DEFINE1(chroot, const char __user *, filename)
  492. {
  493. struct path path;
  494. int error;
  495. error = user_path_dir(filename, &path);
  496. if (error)
  497. goto out;
  498. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_ACCESS);
  499. if (error)
  500. goto dput_and_out;
  501. error = -EPERM;
  502. if (!capable(CAP_SYS_CHROOT))
  503. goto dput_and_out;
  504. set_fs_root(current->fs, &path);
  505. error = 0;
  506. dput_and_out:
  507. path_put(&path);
  508. out:
  509. return error;
  510. }
  511. SYSCALL_DEFINE2(fchmod, unsigned int, fd, mode_t, mode)
  512. {
  513. struct inode * inode;
  514. struct dentry * dentry;
  515. struct file * file;
  516. int err = -EBADF;
  517. struct iattr newattrs;
  518. file = fget(fd);
  519. if (!file)
  520. goto out;
  521. dentry = file->f_path.dentry;
  522. inode = dentry->d_inode;
  523. audit_inode(NULL, dentry);
  524. err = mnt_want_write(file->f_path.mnt);
  525. if (err)
  526. goto out_putf;
  527. mutex_lock(&inode->i_mutex);
  528. if (mode == (mode_t) -1)
  529. mode = inode->i_mode;
  530. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  531. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  532. err = notify_change(dentry, &newattrs);
  533. mutex_unlock(&inode->i_mutex);
  534. mnt_drop_write(file->f_path.mnt);
  535. out_putf:
  536. fput(file);
  537. out:
  538. return err;
  539. }
  540. asmlinkage long sys_fchmodat(int dfd, const char __user *filename,
  541. 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. asmlinkage long sys_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. asmlinkage long sys_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);