open.c 27 KB

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