open.c 27 KB

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