open.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  1. /*
  2. * linux/fs/open.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/string.h>
  7. #include <linux/mm.h>
  8. #include <linux/file.h>
  9. #include <linux/fdtable.h>
  10. #include <linux/quotaops.h>
  11. #include <linux/fsnotify.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/tty.h>
  15. #include <linux/namei.h>
  16. #include <linux/backing-dev.h>
  17. #include <linux/capability.h>
  18. #include <linux/securebits.h>
  19. #include <linux/security.h>
  20. #include <linux/mount.h>
  21. #include <linux/vfs.h>
  22. #include <linux/fcntl.h>
  23. #include <asm/uaccess.h>
  24. #include <linux/fs.h>
  25. #include <linux/personality.h>
  26. #include <linux/pagemap.h>
  27. #include <linux/syscalls.h>
  28. #include <linux/rcupdate.h>
  29. #include <linux/audit.h>
  30. #include <linux/falloc.h>
  31. #include <linux/fs_struct.h>
  32. int vfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  33. {
  34. int retval = -ENODEV;
  35. if (dentry) {
  36. retval = -ENOSYS;
  37. if (dentry->d_sb->s_op->statfs) {
  38. memset(buf, 0, sizeof(*buf));
  39. retval = security_sb_statfs(dentry);
  40. if (retval)
  41. return retval;
  42. retval = dentry->d_sb->s_op->statfs(dentry, buf);
  43. if (retval == 0 && buf->f_frsize == 0)
  44. buf->f_frsize = buf->f_bsize;
  45. }
  46. }
  47. return retval;
  48. }
  49. EXPORT_SYMBOL(vfs_statfs);
  50. static int vfs_statfs_native(struct dentry *dentry, struct statfs *buf)
  51. {
  52. struct kstatfs st;
  53. int retval;
  54. retval = vfs_statfs(dentry, &st);
  55. if (retval)
  56. return retval;
  57. if (sizeof(*buf) == sizeof(st))
  58. memcpy(buf, &st, sizeof(st));
  59. else {
  60. if (sizeof buf->f_blocks == 4) {
  61. if ((st.f_blocks | st.f_bfree | st.f_bavail |
  62. st.f_bsize | st.f_frsize) &
  63. 0xffffffff00000000ULL)
  64. return -EOVERFLOW;
  65. /*
  66. * f_files and f_ffree may be -1; it's okay to stuff
  67. * that into 32 bits
  68. */
  69. if (st.f_files != -1 &&
  70. (st.f_files & 0xffffffff00000000ULL))
  71. return -EOVERFLOW;
  72. if (st.f_ffree != -1 &&
  73. (st.f_ffree & 0xffffffff00000000ULL))
  74. return -EOVERFLOW;
  75. }
  76. buf->f_type = st.f_type;
  77. buf->f_bsize = st.f_bsize;
  78. buf->f_blocks = st.f_blocks;
  79. buf->f_bfree = st.f_bfree;
  80. buf->f_bavail = st.f_bavail;
  81. buf->f_files = st.f_files;
  82. buf->f_ffree = st.f_ffree;
  83. buf->f_fsid = st.f_fsid;
  84. buf->f_namelen = st.f_namelen;
  85. buf->f_frsize = st.f_frsize;
  86. memset(buf->f_spare, 0, sizeof(buf->f_spare));
  87. }
  88. return 0;
  89. }
  90. static int vfs_statfs64(struct dentry *dentry, struct statfs64 *buf)
  91. {
  92. struct kstatfs st;
  93. int retval;
  94. retval = vfs_statfs(dentry, &st);
  95. if (retval)
  96. return retval;
  97. if (sizeof(*buf) == sizeof(st))
  98. memcpy(buf, &st, sizeof(st));
  99. else {
  100. buf->f_type = st.f_type;
  101. buf->f_bsize = st.f_bsize;
  102. buf->f_blocks = st.f_blocks;
  103. buf->f_bfree = st.f_bfree;
  104. buf->f_bavail = st.f_bavail;
  105. buf->f_files = st.f_files;
  106. buf->f_ffree = st.f_ffree;
  107. buf->f_fsid = st.f_fsid;
  108. buf->f_namelen = st.f_namelen;
  109. buf->f_frsize = st.f_frsize;
  110. memset(buf->f_spare, 0, sizeof(buf->f_spare));
  111. }
  112. return 0;
  113. }
  114. SYSCALL_DEFINE2(statfs, const char __user *, pathname, struct statfs __user *, buf)
  115. {
  116. struct path path;
  117. int error;
  118. error = user_path(pathname, &path);
  119. if (!error) {
  120. struct statfs tmp;
  121. error = vfs_statfs_native(path.dentry, &tmp);
  122. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  123. error = -EFAULT;
  124. path_put(&path);
  125. }
  126. return error;
  127. }
  128. SYSCALL_DEFINE3(statfs64, const char __user *, pathname, size_t, sz, struct statfs64 __user *, buf)
  129. {
  130. struct path path;
  131. long error;
  132. if (sz != sizeof(*buf))
  133. return -EINVAL;
  134. error = user_path(pathname, &path);
  135. if (!error) {
  136. struct statfs64 tmp;
  137. error = vfs_statfs64(path.dentry, &tmp);
  138. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  139. error = -EFAULT;
  140. path_put(&path);
  141. }
  142. return error;
  143. }
  144. SYSCALL_DEFINE2(fstatfs, unsigned int, fd, struct statfs __user *, buf)
  145. {
  146. struct file * file;
  147. struct statfs tmp;
  148. int error;
  149. error = -EBADF;
  150. file = fget(fd);
  151. if (!file)
  152. goto out;
  153. error = vfs_statfs_native(file->f_path.dentry, &tmp);
  154. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  155. error = -EFAULT;
  156. fput(file);
  157. out:
  158. return error;
  159. }
  160. SYSCALL_DEFINE3(fstatfs64, unsigned int, fd, size_t, sz, struct statfs64 __user *, buf)
  161. {
  162. struct file * file;
  163. struct statfs64 tmp;
  164. int error;
  165. if (sz != sizeof(*buf))
  166. return -EINVAL;
  167. error = -EBADF;
  168. file = fget(fd);
  169. if (!file)
  170. goto out;
  171. error = vfs_statfs64(file->f_path.dentry, &tmp);
  172. if (!error && copy_to_user(buf, &tmp, sizeof(tmp)))
  173. error = -EFAULT;
  174. fput(file);
  175. out:
  176. return error;
  177. }
  178. int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
  179. struct file *filp)
  180. {
  181. int ret;
  182. struct iattr newattrs;
  183. /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
  184. if (length < 0)
  185. return -EINVAL;
  186. newattrs.ia_size = length;
  187. newattrs.ia_valid = ATTR_SIZE | time_attrs;
  188. if (filp) {
  189. newattrs.ia_file = filp;
  190. newattrs.ia_valid |= ATTR_FILE;
  191. }
  192. /* Remove suid/sgid on truncate too */
  193. ret = should_remove_suid(dentry);
  194. if (ret)
  195. newattrs.ia_valid |= ret | ATTR_FORCE;
  196. mutex_lock(&dentry->d_inode->i_mutex);
  197. ret = notify_change(dentry, &newattrs);
  198. mutex_unlock(&dentry->d_inode->i_mutex);
  199. return ret;
  200. }
  201. static long do_sys_truncate(const char __user *pathname, loff_t length)
  202. {
  203. struct path path;
  204. struct inode *inode;
  205. int error;
  206. error = -EINVAL;
  207. if (length < 0) /* sorry, but loff_t says... */
  208. goto out;
  209. error = user_path(pathname, &path);
  210. if (error)
  211. goto out;
  212. inode = path.dentry->d_inode;
  213. /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
  214. error = -EISDIR;
  215. if (S_ISDIR(inode->i_mode))
  216. goto dput_and_out;
  217. error = -EINVAL;
  218. if (!S_ISREG(inode->i_mode))
  219. goto dput_and_out;
  220. error = mnt_want_write(path.mnt);
  221. if (error)
  222. goto dput_and_out;
  223. error = inode_permission(inode, MAY_WRITE);
  224. if (error)
  225. goto mnt_drop_write_and_out;
  226. error = -EPERM;
  227. if (IS_APPEND(inode))
  228. goto mnt_drop_write_and_out;
  229. error = get_write_access(inode);
  230. if (error)
  231. goto mnt_drop_write_and_out;
  232. /*
  233. * Make sure that there are no leases. get_write_access() protects
  234. * against the truncate racing with a lease-granting setlease().
  235. */
  236. error = break_lease(inode, FMODE_WRITE);
  237. if (error)
  238. goto put_write_and_out;
  239. error = locks_verify_truncate(inode, NULL, length);
  240. if (!error)
  241. error = security_path_truncate(&path, length, 0);
  242. if (!error) {
  243. vfs_dq_init(inode);
  244. error = do_truncate(path.dentry, length, 0, NULL);
  245. }
  246. put_write_and_out:
  247. put_write_access(inode);
  248. mnt_drop_write_and_out:
  249. mnt_drop_write(path.mnt);
  250. dput_and_out:
  251. path_put(&path);
  252. out:
  253. return error;
  254. }
  255. SYSCALL_DEFINE2(truncate, const char __user *, path, unsigned long, length)
  256. {
  257. /* on 32-bit boxen it will cut the range 2^31--2^32-1 off */
  258. return do_sys_truncate(path, (long)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. set_fs_root(current->fs, &path);
  504. error = 0;
  505. dput_and_out:
  506. path_put(&path);
  507. out:
  508. return error;
  509. }
  510. SYSCALL_DEFINE2(fchmod, unsigned int, fd, mode_t, mode)
  511. {
  512. struct inode * inode;
  513. struct dentry * dentry;
  514. struct file * file;
  515. int err = -EBADF;
  516. struct iattr newattrs;
  517. file = fget(fd);
  518. if (!file)
  519. goto out;
  520. dentry = file->f_path.dentry;
  521. inode = dentry->d_inode;
  522. audit_inode(NULL, dentry);
  523. err = mnt_want_write_file(file);
  524. if (err)
  525. goto out_putf;
  526. mutex_lock(&inode->i_mutex);
  527. if (mode == (mode_t) -1)
  528. mode = inode->i_mode;
  529. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  530. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  531. err = notify_change(dentry, &newattrs);
  532. mutex_unlock(&inode->i_mutex);
  533. mnt_drop_write(file->f_path.mnt);
  534. out_putf:
  535. fput(file);
  536. out:
  537. return err;
  538. }
  539. SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, mode_t, mode)
  540. {
  541. struct path path;
  542. struct inode *inode;
  543. int error;
  544. struct iattr newattrs;
  545. error = user_path_at(dfd, filename, LOOKUP_FOLLOW, &path);
  546. if (error)
  547. goto out;
  548. inode = path.dentry->d_inode;
  549. error = mnt_want_write(path.mnt);
  550. if (error)
  551. goto dput_and_out;
  552. mutex_lock(&inode->i_mutex);
  553. if (mode == (mode_t) -1)
  554. mode = inode->i_mode;
  555. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  556. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  557. error = notify_change(path.dentry, &newattrs);
  558. mutex_unlock(&inode->i_mutex);
  559. mnt_drop_write(path.mnt);
  560. dput_and_out:
  561. path_put(&path);
  562. out:
  563. return error;
  564. }
  565. SYSCALL_DEFINE2(chmod, const char __user *, filename, mode_t, mode)
  566. {
  567. return sys_fchmodat(AT_FDCWD, filename, mode);
  568. }
  569. static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
  570. {
  571. struct inode *inode = dentry->d_inode;
  572. int error;
  573. struct iattr newattrs;
  574. newattrs.ia_valid = ATTR_CTIME;
  575. if (user != (uid_t) -1) {
  576. newattrs.ia_valid |= ATTR_UID;
  577. newattrs.ia_uid = user;
  578. }
  579. if (group != (gid_t) -1) {
  580. newattrs.ia_valid |= ATTR_GID;
  581. newattrs.ia_gid = group;
  582. }
  583. if (!S_ISDIR(inode->i_mode))
  584. newattrs.ia_valid |=
  585. ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
  586. mutex_lock(&inode->i_mutex);
  587. error = notify_change(dentry, &newattrs);
  588. mutex_unlock(&inode->i_mutex);
  589. return error;
  590. }
  591. SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
  592. {
  593. struct path path;
  594. int error;
  595. error = user_path(filename, &path);
  596. if (error)
  597. goto out;
  598. error = mnt_want_write(path.mnt);
  599. if (error)
  600. goto out_release;
  601. error = chown_common(path.dentry, user, group);
  602. mnt_drop_write(path.mnt);
  603. out_release:
  604. path_put(&path);
  605. out:
  606. return error;
  607. }
  608. SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
  609. gid_t, group, int, flag)
  610. {
  611. struct path path;
  612. int error = -EINVAL;
  613. int follow;
  614. if ((flag & ~AT_SYMLINK_NOFOLLOW) != 0)
  615. goto out;
  616. follow = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  617. error = user_path_at(dfd, filename, follow, &path);
  618. if (error)
  619. goto out;
  620. error = mnt_want_write(path.mnt);
  621. if (error)
  622. goto out_release;
  623. error = chown_common(path.dentry, user, group);
  624. mnt_drop_write(path.mnt);
  625. out_release:
  626. path_put(&path);
  627. out:
  628. return error;
  629. }
  630. SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
  631. {
  632. struct path path;
  633. int error;
  634. error = user_lpath(filename, &path);
  635. if (error)
  636. goto out;
  637. error = mnt_want_write(path.mnt);
  638. if (error)
  639. goto out_release;
  640. error = chown_common(path.dentry, user, group);
  641. mnt_drop_write(path.mnt);
  642. out_release:
  643. path_put(&path);
  644. out:
  645. return error;
  646. }
  647. SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
  648. {
  649. struct file * file;
  650. int error = -EBADF;
  651. struct dentry * dentry;
  652. file = fget(fd);
  653. if (!file)
  654. goto out;
  655. error = mnt_want_write_file(file);
  656. if (error)
  657. goto out_fput;
  658. dentry = file->f_path.dentry;
  659. audit_inode(NULL, dentry);
  660. error = chown_common(dentry, user, group);
  661. mnt_drop_write(file->f_path.mnt);
  662. out_fput:
  663. fput(file);
  664. out:
  665. return error;
  666. }
  667. /*
  668. * You have to be very careful that these write
  669. * counts get cleaned up in error cases and
  670. * upon __fput(). This should probably never
  671. * be called outside of __dentry_open().
  672. */
  673. static inline int __get_file_write_access(struct inode *inode,
  674. struct vfsmount *mnt)
  675. {
  676. int error;
  677. error = get_write_access(inode);
  678. if (error)
  679. return error;
  680. /*
  681. * Do not take mount writer counts on
  682. * special files since no writes to
  683. * the mount itself will occur.
  684. */
  685. if (!special_file(inode->i_mode)) {
  686. /*
  687. * Balanced in __fput()
  688. */
  689. error = mnt_want_write(mnt);
  690. if (error)
  691. put_write_access(inode);
  692. }
  693. return error;
  694. }
  695. static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
  696. int flags, struct file *f,
  697. int (*open)(struct inode *, struct file *),
  698. const struct cred *cred)
  699. {
  700. struct inode *inode;
  701. int error;
  702. f->f_flags = flags;
  703. f->f_mode = (__force fmode_t)((flags+1) & O_ACCMODE) | FMODE_LSEEK |
  704. FMODE_PREAD | FMODE_PWRITE;
  705. inode = dentry->d_inode;
  706. if (f->f_mode & FMODE_WRITE) {
  707. error = __get_file_write_access(inode, mnt);
  708. if (error)
  709. goto cleanup_file;
  710. if (!special_file(inode->i_mode))
  711. file_take_write(f);
  712. }
  713. f->f_mapping = inode->i_mapping;
  714. f->f_path.dentry = dentry;
  715. f->f_path.mnt = mnt;
  716. f->f_pos = 0;
  717. f->f_op = fops_get(inode->i_fop);
  718. file_move(f, &inode->i_sb->s_files);
  719. error = security_dentry_open(f, cred);
  720. if (error)
  721. goto cleanup_all;
  722. if (!open && f->f_op)
  723. open = f->f_op->open;
  724. if (open) {
  725. error = open(inode, f);
  726. if (error)
  727. goto cleanup_all;
  728. }
  729. f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  730. file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
  731. /* NB: we're sure to have correct a_ops only after f_op->open */
  732. if (f->f_flags & O_DIRECT) {
  733. if (!f->f_mapping->a_ops ||
  734. ((!f->f_mapping->a_ops->direct_IO) &&
  735. (!f->f_mapping->a_ops->get_xip_mem))) {
  736. fput(f);
  737. f = ERR_PTR(-EINVAL);
  738. }
  739. }
  740. return f;
  741. cleanup_all:
  742. fops_put(f->f_op);
  743. if (f->f_mode & FMODE_WRITE) {
  744. put_write_access(inode);
  745. if (!special_file(inode->i_mode)) {
  746. /*
  747. * We don't consider this a real
  748. * mnt_want/drop_write() pair
  749. * because it all happenend right
  750. * here, so just reset the state.
  751. */
  752. file_reset_write(f);
  753. mnt_drop_write(mnt);
  754. }
  755. }
  756. file_kill(f);
  757. f->f_path.dentry = NULL;
  758. f->f_path.mnt = NULL;
  759. cleanup_file:
  760. put_filp(f);
  761. dput(dentry);
  762. mntput(mnt);
  763. return ERR_PTR(error);
  764. }
  765. /**
  766. * lookup_instantiate_filp - instantiates the open intent filp
  767. * @nd: pointer to nameidata
  768. * @dentry: pointer to dentry
  769. * @open: open callback
  770. *
  771. * Helper for filesystems that want to use lookup open intents and pass back
  772. * a fully instantiated struct file to the caller.
  773. * This function is meant to be called from within a filesystem's
  774. * lookup method.
  775. * Beware of calling it for non-regular files! Those ->open methods might block
  776. * (e.g. in fifo_open), leaving you with parent locked (and in case of fifo,
  777. * leading to a deadlock, as nobody can open that fifo anymore, because
  778. * another process to open fifo will block on locked parent when doing lookup).
  779. * Note that in case of error, nd->intent.open.file is destroyed, but the
  780. * path information remains valid.
  781. * If the open callback is set to NULL, then the standard f_op->open()
  782. * filesystem callback is substituted.
  783. */
  784. struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
  785. int (*open)(struct inode *, struct file *))
  786. {
  787. const struct cred *cred = current_cred();
  788. if (IS_ERR(nd->intent.open.file))
  789. goto out;
  790. if (IS_ERR(dentry))
  791. goto out_err;
  792. nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->path.mnt),
  793. nd->intent.open.flags - 1,
  794. nd->intent.open.file,
  795. open, cred);
  796. out:
  797. return nd->intent.open.file;
  798. out_err:
  799. release_open_intent(nd);
  800. nd->intent.open.file = (struct file *)dentry;
  801. goto out;
  802. }
  803. EXPORT_SYMBOL_GPL(lookup_instantiate_filp);
  804. /**
  805. * nameidata_to_filp - convert a nameidata to an open filp.
  806. * @nd: pointer to nameidata
  807. * @flags: open flags
  808. *
  809. * Note that this function destroys the original nameidata
  810. */
  811. struct file *nameidata_to_filp(struct nameidata *nd, int flags)
  812. {
  813. const struct cred *cred = current_cred();
  814. struct file *filp;
  815. /* Pick up the filp from the open intent */
  816. filp = nd->intent.open.file;
  817. /* Has the filesystem initialised the file for us? */
  818. if (filp->f_path.dentry == NULL)
  819. filp = __dentry_open(nd->path.dentry, nd->path.mnt, flags, filp,
  820. NULL, cred);
  821. else
  822. path_put(&nd->path);
  823. return filp;
  824. }
  825. /*
  826. * dentry_open() will have done dput(dentry) and mntput(mnt) if it returns an
  827. * error.
  828. */
  829. struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags,
  830. const struct cred *cred)
  831. {
  832. int error;
  833. struct file *f;
  834. /*
  835. * We must always pass in a valid mount pointer. Historically
  836. * callers got away with not passing it, but we must enforce this at
  837. * the earliest possible point now to avoid strange problems deep in the
  838. * filesystem stack.
  839. */
  840. if (!mnt) {
  841. printk(KERN_WARNING "%s called with NULL vfsmount\n", __func__);
  842. dump_stack();
  843. return ERR_PTR(-EINVAL);
  844. }
  845. error = -ENFILE;
  846. f = get_empty_filp();
  847. if (f == NULL) {
  848. dput(dentry);
  849. mntput(mnt);
  850. return ERR_PTR(error);
  851. }
  852. return __dentry_open(dentry, mnt, flags, f, NULL, cred);
  853. }
  854. EXPORT_SYMBOL(dentry_open);
  855. static void __put_unused_fd(struct files_struct *files, unsigned int fd)
  856. {
  857. struct fdtable *fdt = files_fdtable(files);
  858. __FD_CLR(fd, fdt->open_fds);
  859. if (fd < files->next_fd)
  860. files->next_fd = fd;
  861. }
  862. void put_unused_fd(unsigned int fd)
  863. {
  864. struct files_struct *files = current->files;
  865. spin_lock(&files->file_lock);
  866. __put_unused_fd(files, fd);
  867. spin_unlock(&files->file_lock);
  868. }
  869. EXPORT_SYMBOL(put_unused_fd);
  870. /*
  871. * Install a file pointer in the fd array.
  872. *
  873. * The VFS is full of places where we drop the files lock between
  874. * setting the open_fds bitmap and installing the file in the file
  875. * array. At any such point, we are vulnerable to a dup2() race
  876. * installing a file in the array before us. We need to detect this and
  877. * fput() the struct file we are about to overwrite in this case.
  878. *
  879. * It should never happen - if we allow dup2() do it, _really_ bad things
  880. * will follow.
  881. */
  882. void fd_install(unsigned int fd, struct file *file)
  883. {
  884. struct files_struct *files = current->files;
  885. struct fdtable *fdt;
  886. spin_lock(&files->file_lock);
  887. fdt = files_fdtable(files);
  888. BUG_ON(fdt->fd[fd] != NULL);
  889. rcu_assign_pointer(fdt->fd[fd], file);
  890. spin_unlock(&files->file_lock);
  891. }
  892. EXPORT_SYMBOL(fd_install);
  893. long do_sys_open(int dfd, const char __user *filename, int flags, int mode)
  894. {
  895. char *tmp = getname(filename);
  896. int fd = PTR_ERR(tmp);
  897. if (!IS_ERR(tmp)) {
  898. fd = get_unused_fd_flags(flags);
  899. if (fd >= 0) {
  900. struct file *f = do_filp_open(dfd, tmp, flags, mode, 0);
  901. if (IS_ERR(f)) {
  902. put_unused_fd(fd);
  903. fd = PTR_ERR(f);
  904. } else {
  905. fsnotify_open(f->f_path.dentry);
  906. fd_install(fd, f);
  907. }
  908. }
  909. putname(tmp);
  910. }
  911. return fd;
  912. }
  913. SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, int, mode)
  914. {
  915. long ret;
  916. if (force_o_largefile())
  917. flags |= O_LARGEFILE;
  918. ret = do_sys_open(AT_FDCWD, filename, flags, mode);
  919. /* avoid REGPARM breakage on x86: */
  920. asmlinkage_protect(3, ret, filename, flags, mode);
  921. return ret;
  922. }
  923. SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
  924. int, mode)
  925. {
  926. long ret;
  927. if (force_o_largefile())
  928. flags |= O_LARGEFILE;
  929. ret = do_sys_open(dfd, filename, flags, mode);
  930. /* avoid REGPARM breakage on x86: */
  931. asmlinkage_protect(4, ret, dfd, filename, flags, mode);
  932. return ret;
  933. }
  934. #ifndef __alpha__
  935. /*
  936. * For backward compatibility? Maybe this should be moved
  937. * into arch/i386 instead?
  938. */
  939. SYSCALL_DEFINE2(creat, const char __user *, pathname, int, mode)
  940. {
  941. return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
  942. }
  943. #endif
  944. /*
  945. * "id" is the POSIX thread ID. We use the
  946. * files pointer for this..
  947. */
  948. int filp_close(struct file *filp, fl_owner_t id)
  949. {
  950. int retval = 0;
  951. if (!file_count(filp)) {
  952. printk(KERN_ERR "VFS: Close: file count is 0\n");
  953. return 0;
  954. }
  955. if (filp->f_op && filp->f_op->flush)
  956. retval = filp->f_op->flush(filp, id);
  957. dnotify_flush(filp, id);
  958. locks_remove_posix(filp, id);
  959. fput(filp);
  960. return retval;
  961. }
  962. EXPORT_SYMBOL(filp_close);
  963. /*
  964. * Careful here! We test whether the file pointer is NULL before
  965. * releasing the fd. This ensures that one clone task can't release
  966. * an fd while another clone is opening it.
  967. */
  968. SYSCALL_DEFINE1(close, unsigned int, fd)
  969. {
  970. struct file * filp;
  971. struct files_struct *files = current->files;
  972. struct fdtable *fdt;
  973. int retval;
  974. spin_lock(&files->file_lock);
  975. fdt = files_fdtable(files);
  976. if (fd >= fdt->max_fds)
  977. goto out_unlock;
  978. filp = fdt->fd[fd];
  979. if (!filp)
  980. goto out_unlock;
  981. rcu_assign_pointer(fdt->fd[fd], NULL);
  982. FD_CLR(fd, fdt->close_on_exec);
  983. __put_unused_fd(files, fd);
  984. spin_unlock(&files->file_lock);
  985. retval = filp_close(filp, files);
  986. /* can't restart close syscall because file table entry was cleared */
  987. if (unlikely(retval == -ERESTARTSYS ||
  988. retval == -ERESTARTNOINTR ||
  989. retval == -ERESTARTNOHAND ||
  990. retval == -ERESTART_RESTARTBLOCK))
  991. retval = -EINTR;
  992. return retval;
  993. out_unlock:
  994. spin_unlock(&files->file_lock);
  995. return -EBADF;
  996. }
  997. EXPORT_SYMBOL(sys_close);
  998. /*
  999. * This routine simulates a hangup on the tty, to arrange that users
  1000. * are given clean terminals at login time.
  1001. */
  1002. SYSCALL_DEFINE0(vhangup)
  1003. {
  1004. if (capable(CAP_SYS_TTY_CONFIG)) {
  1005. tty_vhangup_self();
  1006. return 0;
  1007. }
  1008. return -EPERM;
  1009. }
  1010. /*
  1011. * Called when an inode is about to be open.
  1012. * We use this to disallow opening large files on 32bit systems if
  1013. * the caller didn't specify O_LARGEFILE. On 64bit systems we force
  1014. * on this flag in sys_open.
  1015. */
  1016. int generic_file_open(struct inode * inode, struct file * filp)
  1017. {
  1018. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  1019. return -EOVERFLOW;
  1020. return 0;
  1021. }
  1022. EXPORT_SYMBOL(generic_file_open);
  1023. /*
  1024. * This is used by subsystems that don't want seekable
  1025. * file descriptors
  1026. */
  1027. int nonseekable_open(struct inode *inode, struct file *filp)
  1028. {
  1029. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  1030. return 0;
  1031. }
  1032. EXPORT_SYMBOL(nonseekable_open);