open.c 27 KB

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