open.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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/fsnotify.h>
  11. #include <linux/module.h>
  12. #include <linux/tty.h>
  13. #include <linux/namei.h>
  14. #include <linux/backing-dev.h>
  15. #include <linux/capability.h>
  16. #include <linux/securebits.h>
  17. #include <linux/security.h>
  18. #include <linux/mount.h>
  19. #include <linux/fcntl.h>
  20. #include <linux/slab.h>
  21. #include <asm/uaccess.h>
  22. #include <linux/fs.h>
  23. #include <linux/personality.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/rcupdate.h>
  27. #include <linux/audit.h>
  28. #include <linux/falloc.h>
  29. #include <linux/fs_struct.h>
  30. #include <linux/ima.h>
  31. #include <linux/dnotify.h>
  32. #include <linux/compat.h>
  33. #include "internal.h"
  34. int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
  35. struct file *filp)
  36. {
  37. int ret;
  38. struct iattr newattrs;
  39. /* Not pretty: "inode->i_size" shouldn't really be signed. But it is. */
  40. if (length < 0)
  41. return -EINVAL;
  42. newattrs.ia_size = length;
  43. newattrs.ia_valid = ATTR_SIZE | time_attrs;
  44. if (filp) {
  45. newattrs.ia_file = filp;
  46. newattrs.ia_valid |= ATTR_FILE;
  47. }
  48. /* Remove suid/sgid on truncate too */
  49. ret = should_remove_suid(dentry);
  50. if (ret)
  51. newattrs.ia_valid |= ret | ATTR_FORCE;
  52. mutex_lock(&dentry->d_inode->i_mutex);
  53. /* Note any delegations or leases have already been broken: */
  54. ret = notify_change(dentry, &newattrs, NULL);
  55. mutex_unlock(&dentry->d_inode->i_mutex);
  56. return ret;
  57. }
  58. long vfs_truncate(struct path *path, loff_t length)
  59. {
  60. struct inode *inode;
  61. long error;
  62. inode = path->dentry->d_inode;
  63. /* For directories it's -EISDIR, for other non-regulars - -EINVAL */
  64. if (S_ISDIR(inode->i_mode))
  65. return -EISDIR;
  66. if (!S_ISREG(inode->i_mode))
  67. return -EINVAL;
  68. error = mnt_want_write(path->mnt);
  69. if (error)
  70. goto out;
  71. error = inode_permission(inode, MAY_WRITE);
  72. if (error)
  73. goto mnt_drop_write_and_out;
  74. error = -EPERM;
  75. if (IS_APPEND(inode))
  76. goto mnt_drop_write_and_out;
  77. error = get_write_access(inode);
  78. if (error)
  79. goto mnt_drop_write_and_out;
  80. /*
  81. * Make sure that there are no leases. get_write_access() protects
  82. * against the truncate racing with a lease-granting setlease().
  83. */
  84. error = break_lease(inode, O_WRONLY);
  85. if (error)
  86. goto put_write_and_out;
  87. error = locks_verify_truncate(inode, NULL, length);
  88. if (!error)
  89. error = security_path_truncate(path);
  90. if (!error)
  91. error = do_truncate(path->dentry, length, 0, NULL);
  92. put_write_and_out:
  93. put_write_access(inode);
  94. mnt_drop_write_and_out:
  95. mnt_drop_write(path->mnt);
  96. out:
  97. return error;
  98. }
  99. EXPORT_SYMBOL_GPL(vfs_truncate);
  100. static long do_sys_truncate(const char __user *pathname, loff_t length)
  101. {
  102. unsigned int lookup_flags = LOOKUP_FOLLOW;
  103. struct path path;
  104. int error;
  105. if (length < 0) /* sorry, but loff_t says... */
  106. return -EINVAL;
  107. retry:
  108. error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
  109. if (!error) {
  110. error = vfs_truncate(&path, length);
  111. path_put(&path);
  112. }
  113. if (retry_estale(error, lookup_flags)) {
  114. lookup_flags |= LOOKUP_REVAL;
  115. goto retry;
  116. }
  117. return error;
  118. }
  119. SYSCALL_DEFINE2(truncate, const char __user *, path, long, length)
  120. {
  121. return do_sys_truncate(path, length);
  122. }
  123. #ifdef CONFIG_COMPAT
  124. COMPAT_SYSCALL_DEFINE2(truncate, const char __user *, path, compat_off_t, length)
  125. {
  126. return do_sys_truncate(path, length);
  127. }
  128. #endif
  129. static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)
  130. {
  131. struct inode *inode;
  132. struct dentry *dentry;
  133. struct fd f;
  134. int error;
  135. error = -EINVAL;
  136. if (length < 0)
  137. goto out;
  138. error = -EBADF;
  139. f = fdget(fd);
  140. if (!f.file)
  141. goto out;
  142. /* explicitly opened as large or we are on 64-bit box */
  143. if (f.file->f_flags & O_LARGEFILE)
  144. small = 0;
  145. dentry = f.file->f_path.dentry;
  146. inode = dentry->d_inode;
  147. error = -EINVAL;
  148. if (!S_ISREG(inode->i_mode) || !(f.file->f_mode & FMODE_WRITE))
  149. goto out_putf;
  150. error = -EINVAL;
  151. /* Cannot ftruncate over 2^31 bytes without large file support */
  152. if (small && length > MAX_NON_LFS)
  153. goto out_putf;
  154. error = -EPERM;
  155. if (IS_APPEND(inode))
  156. goto out_putf;
  157. sb_start_write(inode->i_sb);
  158. error = locks_verify_truncate(inode, f.file, length);
  159. if (!error)
  160. error = security_path_truncate(&f.file->f_path);
  161. if (!error)
  162. error = do_truncate(dentry, length, ATTR_MTIME|ATTR_CTIME, f.file);
  163. sb_end_write(inode->i_sb);
  164. out_putf:
  165. fdput(f);
  166. out:
  167. return error;
  168. }
  169. SYSCALL_DEFINE2(ftruncate, unsigned int, fd, unsigned long, length)
  170. {
  171. return do_sys_ftruncate(fd, length, 1);
  172. }
  173. #ifdef CONFIG_COMPAT
  174. COMPAT_SYSCALL_DEFINE2(ftruncate, unsigned int, fd, compat_ulong_t, length)
  175. {
  176. return do_sys_ftruncate(fd, length, 1);
  177. }
  178. #endif
  179. /* LFS versions of truncate are only needed on 32 bit machines */
  180. #if BITS_PER_LONG == 32
  181. SYSCALL_DEFINE2(truncate64, const char __user *, path, loff_t, length)
  182. {
  183. return do_sys_truncate(path, length);
  184. }
  185. SYSCALL_DEFINE2(ftruncate64, unsigned int, fd, loff_t, length)
  186. {
  187. return do_sys_ftruncate(fd, length, 0);
  188. }
  189. #endif /* BITS_PER_LONG == 32 */
  190. int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  191. {
  192. struct inode *inode = file_inode(file);
  193. long ret;
  194. if (offset < 0 || len <= 0)
  195. return -EINVAL;
  196. /* Return error if mode is not supported */
  197. if (mode & ~FALLOC_FL_SUPPORTED_MASK)
  198. return -EOPNOTSUPP;
  199. /* Punch hole and zero range are mutually exclusive */
  200. if ((mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) ==
  201. (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE))
  202. return -EOPNOTSUPP;
  203. /* Punch hole must have keep size set */
  204. if ((mode & FALLOC_FL_PUNCH_HOLE) &&
  205. !(mode & FALLOC_FL_KEEP_SIZE))
  206. return -EOPNOTSUPP;
  207. /* Collapse range should only be used exclusively. */
  208. if ((mode & FALLOC_FL_COLLAPSE_RANGE) &&
  209. (mode & ~FALLOC_FL_COLLAPSE_RANGE))
  210. return -EINVAL;
  211. /* Insert range should only be used exclusively. */
  212. if ((mode & FALLOC_FL_INSERT_RANGE) &&
  213. (mode & ~FALLOC_FL_INSERT_RANGE))
  214. return -EINVAL;
  215. if (!(file->f_mode & FMODE_WRITE))
  216. return -EBADF;
  217. /*
  218. * We can only allow pure fallocate on append only files
  219. */
  220. if ((mode & ~FALLOC_FL_KEEP_SIZE) && IS_APPEND(inode))
  221. return -EPERM;
  222. if (IS_IMMUTABLE(inode))
  223. return -EPERM;
  224. /*
  225. * We cannot allow any fallocate operation on an active swapfile
  226. */
  227. if (IS_SWAPFILE(inode))
  228. return -ETXTBSY;
  229. /*
  230. * Revalidate the write permissions, in case security policy has
  231. * changed since the files were opened.
  232. */
  233. ret = security_file_permission(file, MAY_WRITE);
  234. if (ret)
  235. return ret;
  236. if (S_ISFIFO(inode->i_mode))
  237. return -ESPIPE;
  238. /*
  239. * Let individual file system decide if it supports preallocation
  240. * for directories or not.
  241. */
  242. if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
  243. return -ENODEV;
  244. /* Check for wrap through zero too */
  245. if (((offset + len) > inode->i_sb->s_maxbytes) || ((offset + len) < 0))
  246. return -EFBIG;
  247. if (!file->f_op->fallocate)
  248. return -EOPNOTSUPP;
  249. sb_start_write(inode->i_sb);
  250. ret = file->f_op->fallocate(file, mode, offset, len);
  251. /*
  252. * Create inotify and fanotify events.
  253. *
  254. * To keep the logic simple always create events if fallocate succeeds.
  255. * This implies that events are even created if the file size remains
  256. * unchanged, e.g. when using flag FALLOC_FL_KEEP_SIZE.
  257. */
  258. if (ret == 0)
  259. fsnotify_modify(file);
  260. sb_end_write(inode->i_sb);
  261. return ret;
  262. }
  263. EXPORT_SYMBOL_GPL(vfs_fallocate);
  264. SYSCALL_DEFINE4(fallocate, int, fd, int, mode, loff_t, offset, loff_t, len)
  265. {
  266. struct fd f = fdget(fd);
  267. int error = -EBADF;
  268. if (f.file) {
  269. error = vfs_fallocate(f.file, mode, offset, len);
  270. fdput(f);
  271. }
  272. return error;
  273. }
  274. /*
  275. * access() needs to use the real uid/gid, not the effective uid/gid.
  276. * We do this by temporarily clearing all FS-related capabilities and
  277. * switching the fsuid/fsgid around to the real ones.
  278. */
  279. SYSCALL_DEFINE3(faccessat, int, dfd, const char __user *, filename, int, mode)
  280. {
  281. const struct cred *old_cred;
  282. struct cred *override_cred;
  283. struct path path;
  284. struct inode *inode;
  285. int res;
  286. unsigned int lookup_flags = LOOKUP_FOLLOW;
  287. if (mode & ~S_IRWXO) /* where's F_OK, X_OK, W_OK, R_OK? */
  288. return -EINVAL;
  289. override_cred = prepare_creds();
  290. if (!override_cred)
  291. return -ENOMEM;
  292. override_cred->fsuid = override_cred->uid;
  293. override_cred->fsgid = override_cred->gid;
  294. if (!issecure(SECURE_NO_SETUID_FIXUP)) {
  295. /* Clear the capabilities if we switch to a non-root user */
  296. kuid_t root_uid = make_kuid(override_cred->user_ns, 0);
  297. if (!uid_eq(override_cred->uid, root_uid))
  298. cap_clear(override_cred->cap_effective);
  299. else
  300. override_cred->cap_effective =
  301. override_cred->cap_permitted;
  302. }
  303. old_cred = override_creds(override_cred);
  304. retry:
  305. res = user_path_at(dfd, filename, lookup_flags, &path);
  306. if (res)
  307. goto out;
  308. inode = path.dentry->d_inode;
  309. if ((mode & MAY_EXEC) && S_ISREG(inode->i_mode)) {
  310. /*
  311. * MAY_EXEC on regular files is denied if the fs is mounted
  312. * with the "noexec" flag.
  313. */
  314. res = -EACCES;
  315. if (path.mnt->mnt_flags & MNT_NOEXEC)
  316. goto out_path_release;
  317. }
  318. res = inode_permission(inode, mode | MAY_ACCESS);
  319. /* SuS v2 requires we report a read only fs too */
  320. if (res || !(mode & S_IWOTH) || special_file(inode->i_mode))
  321. goto out_path_release;
  322. /*
  323. * This is a rare case where using __mnt_is_readonly()
  324. * is OK without a mnt_want/drop_write() pair. Since
  325. * no actual write to the fs is performed here, we do
  326. * not need to telegraph to that to anyone.
  327. *
  328. * By doing this, we accept that this access is
  329. * inherently racy and know that the fs may change
  330. * state before we even see this result.
  331. */
  332. if (__mnt_is_readonly(path.mnt))
  333. res = -EROFS;
  334. out_path_release:
  335. path_put(&path);
  336. if (retry_estale(res, lookup_flags)) {
  337. lookup_flags |= LOOKUP_REVAL;
  338. goto retry;
  339. }
  340. out:
  341. revert_creds(old_cred);
  342. put_cred(override_cred);
  343. return res;
  344. }
  345. SYSCALL_DEFINE2(access, const char __user *, filename, int, mode)
  346. {
  347. return sys_faccessat(AT_FDCWD, filename, mode);
  348. }
  349. SYSCALL_DEFINE1(chdir, const char __user *, filename)
  350. {
  351. struct path path;
  352. int error;
  353. unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  354. retry:
  355. error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
  356. if (error)
  357. goto out;
  358. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
  359. if (error)
  360. goto dput_and_out;
  361. set_fs_pwd(current->fs, &path);
  362. dput_and_out:
  363. path_put(&path);
  364. if (retry_estale(error, lookup_flags)) {
  365. lookup_flags |= LOOKUP_REVAL;
  366. goto retry;
  367. }
  368. out:
  369. return error;
  370. }
  371. SYSCALL_DEFINE1(fchdir, unsigned int, fd)
  372. {
  373. struct fd f = fdget_raw(fd);
  374. struct inode *inode;
  375. int error = -EBADF;
  376. error = -EBADF;
  377. if (!f.file)
  378. goto out;
  379. inode = file_inode(f.file);
  380. error = -ENOTDIR;
  381. if (!S_ISDIR(inode->i_mode))
  382. goto out_putf;
  383. error = inode_permission(inode, MAY_EXEC | MAY_CHDIR);
  384. if (!error)
  385. set_fs_pwd(current->fs, &f.file->f_path);
  386. out_putf:
  387. fdput(f);
  388. out:
  389. return error;
  390. }
  391. SYSCALL_DEFINE1(chroot, const char __user *, filename)
  392. {
  393. struct path path;
  394. int error;
  395. unsigned int lookup_flags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  396. retry:
  397. error = user_path_at(AT_FDCWD, filename, lookup_flags, &path);
  398. if (error)
  399. goto out;
  400. error = inode_permission(path.dentry->d_inode, MAY_EXEC | MAY_CHDIR);
  401. if (error)
  402. goto dput_and_out;
  403. error = -EPERM;
  404. if (!ns_capable(current_user_ns(), CAP_SYS_CHROOT))
  405. goto dput_and_out;
  406. error = security_path_chroot(&path);
  407. if (error)
  408. goto dput_and_out;
  409. set_fs_root(current->fs, &path);
  410. error = 0;
  411. dput_and_out:
  412. path_put(&path);
  413. if (retry_estale(error, lookup_flags)) {
  414. lookup_flags |= LOOKUP_REVAL;
  415. goto retry;
  416. }
  417. out:
  418. return error;
  419. }
  420. static int chmod_common(struct path *path, umode_t mode)
  421. {
  422. struct inode *inode = path->dentry->d_inode;
  423. struct inode *delegated_inode = NULL;
  424. struct iattr newattrs;
  425. int error;
  426. error = mnt_want_write(path->mnt);
  427. if (error)
  428. return error;
  429. retry_deleg:
  430. mutex_lock(&inode->i_mutex);
  431. error = security_path_chmod(path, mode);
  432. if (error)
  433. goto out_unlock;
  434. newattrs.ia_mode = (mode & S_IALLUGO) | (inode->i_mode & ~S_IALLUGO);
  435. newattrs.ia_valid = ATTR_MODE | ATTR_CTIME;
  436. error = notify_change(path->dentry, &newattrs, &delegated_inode);
  437. out_unlock:
  438. mutex_unlock(&inode->i_mutex);
  439. if (delegated_inode) {
  440. error = break_deleg_wait(&delegated_inode);
  441. if (!error)
  442. goto retry_deleg;
  443. }
  444. mnt_drop_write(path->mnt);
  445. return error;
  446. }
  447. SYSCALL_DEFINE2(fchmod, unsigned int, fd, umode_t, mode)
  448. {
  449. struct fd f = fdget(fd);
  450. int err = -EBADF;
  451. if (f.file) {
  452. audit_file(f.file);
  453. err = chmod_common(&f.file->f_path, mode);
  454. fdput(f);
  455. }
  456. return err;
  457. }
  458. SYSCALL_DEFINE3(fchmodat, int, dfd, const char __user *, filename, umode_t, mode)
  459. {
  460. struct path path;
  461. int error;
  462. unsigned int lookup_flags = LOOKUP_FOLLOW;
  463. retry:
  464. error = user_path_at(dfd, filename, lookup_flags, &path);
  465. if (!error) {
  466. error = chmod_common(&path, mode);
  467. path_put(&path);
  468. if (retry_estale(error, lookup_flags)) {
  469. lookup_flags |= LOOKUP_REVAL;
  470. goto retry;
  471. }
  472. }
  473. return error;
  474. }
  475. SYSCALL_DEFINE2(chmod, const char __user *, filename, umode_t, mode)
  476. {
  477. return sys_fchmodat(AT_FDCWD, filename, mode);
  478. }
  479. static int chown_common(struct path *path, uid_t user, gid_t group)
  480. {
  481. struct inode *inode = path->dentry->d_inode;
  482. struct inode *delegated_inode = NULL;
  483. int error;
  484. struct iattr newattrs;
  485. kuid_t uid;
  486. kgid_t gid;
  487. uid = make_kuid(current_user_ns(), user);
  488. gid = make_kgid(current_user_ns(), group);
  489. retry_deleg:
  490. newattrs.ia_valid = ATTR_CTIME;
  491. if (user != (uid_t) -1) {
  492. if (!uid_valid(uid))
  493. return -EINVAL;
  494. newattrs.ia_valid |= ATTR_UID;
  495. newattrs.ia_uid = uid;
  496. }
  497. if (group != (gid_t) -1) {
  498. if (!gid_valid(gid))
  499. return -EINVAL;
  500. newattrs.ia_valid |= ATTR_GID;
  501. newattrs.ia_gid = gid;
  502. }
  503. if (!S_ISDIR(inode->i_mode))
  504. newattrs.ia_valid |=
  505. ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
  506. mutex_lock(&inode->i_mutex);
  507. error = security_path_chown(path, uid, gid);
  508. if (!error)
  509. error = notify_change(path->dentry, &newattrs, &delegated_inode);
  510. mutex_unlock(&inode->i_mutex);
  511. if (delegated_inode) {
  512. error = break_deleg_wait(&delegated_inode);
  513. if (!error)
  514. goto retry_deleg;
  515. }
  516. return error;
  517. }
  518. SYSCALL_DEFINE5(fchownat, int, dfd, const char __user *, filename, uid_t, user,
  519. gid_t, group, int, flag)
  520. {
  521. struct path path;
  522. int error = -EINVAL;
  523. int lookup_flags;
  524. if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH)) != 0)
  525. goto out;
  526. lookup_flags = (flag & AT_SYMLINK_NOFOLLOW) ? 0 : LOOKUP_FOLLOW;
  527. if (flag & AT_EMPTY_PATH)
  528. lookup_flags |= LOOKUP_EMPTY;
  529. retry:
  530. error = user_path_at(dfd, filename, lookup_flags, &path);
  531. if (error)
  532. goto out;
  533. error = mnt_want_write(path.mnt);
  534. if (error)
  535. goto out_release;
  536. error = chown_common(&path, user, group);
  537. mnt_drop_write(path.mnt);
  538. out_release:
  539. path_put(&path);
  540. if (retry_estale(error, lookup_flags)) {
  541. lookup_flags |= LOOKUP_REVAL;
  542. goto retry;
  543. }
  544. out:
  545. return error;
  546. }
  547. SYSCALL_DEFINE3(chown, const char __user *, filename, uid_t, user, gid_t, group)
  548. {
  549. return sys_fchownat(AT_FDCWD, filename, user, group, 0);
  550. }
  551. SYSCALL_DEFINE3(lchown, const char __user *, filename, uid_t, user, gid_t, group)
  552. {
  553. return sys_fchownat(AT_FDCWD, filename, user, group,
  554. AT_SYMLINK_NOFOLLOW);
  555. }
  556. SYSCALL_DEFINE3(fchown, unsigned int, fd, uid_t, user, gid_t, group)
  557. {
  558. struct fd f = fdget(fd);
  559. int error = -EBADF;
  560. if (!f.file)
  561. goto out;
  562. error = mnt_want_write_file(f.file);
  563. if (error)
  564. goto out_fput;
  565. audit_file(f.file);
  566. error = chown_common(&f.file->f_path, user, group);
  567. mnt_drop_write_file(f.file);
  568. out_fput:
  569. fdput(f);
  570. out:
  571. return error;
  572. }
  573. int open_check_o_direct(struct file *f)
  574. {
  575. /* NB: we're sure to have correct a_ops only after f_op->open */
  576. if (f->f_flags & O_DIRECT) {
  577. if (!f->f_mapping->a_ops || !f->f_mapping->a_ops->direct_IO)
  578. return -EINVAL;
  579. }
  580. return 0;
  581. }
  582. static int do_dentry_open(struct file *f,
  583. int (*open)(struct inode *, struct file *),
  584. const struct cred *cred)
  585. {
  586. static const struct file_operations empty_fops = {};
  587. struct inode *inode;
  588. int error;
  589. f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
  590. FMODE_PREAD | FMODE_PWRITE;
  591. path_get(&f->f_path);
  592. inode = f->f_inode = f->f_path.dentry->d_inode;
  593. f->f_mapping = inode->i_mapping;
  594. if (unlikely(f->f_flags & O_PATH)) {
  595. f->f_mode = FMODE_PATH;
  596. f->f_op = &empty_fops;
  597. return 0;
  598. }
  599. if (f->f_mode & FMODE_WRITE && !special_file(inode->i_mode)) {
  600. error = get_write_access(inode);
  601. if (unlikely(error))
  602. goto cleanup_file;
  603. error = __mnt_want_write(f->f_path.mnt);
  604. if (unlikely(error)) {
  605. put_write_access(inode);
  606. goto cleanup_file;
  607. }
  608. f->f_mode |= FMODE_WRITER;
  609. }
  610. /* POSIX.1-2008/SUSv4 Section XSI 2.9.7 */
  611. if (S_ISREG(inode->i_mode))
  612. f->f_mode |= FMODE_ATOMIC_POS;
  613. f->f_op = fops_get(inode->i_fop);
  614. if (unlikely(WARN_ON(!f->f_op))) {
  615. error = -ENODEV;
  616. goto cleanup_all;
  617. }
  618. error = security_file_open(f, cred);
  619. if (error)
  620. goto cleanup_all;
  621. error = break_lease(inode, f->f_flags);
  622. if (error)
  623. goto cleanup_all;
  624. if (!open)
  625. open = f->f_op->open;
  626. if (open) {
  627. error = open(inode, f);
  628. if (error)
  629. goto cleanup_all;
  630. }
  631. if ((f->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  632. i_readcount_inc(inode);
  633. if ((f->f_mode & FMODE_READ) &&
  634. likely(f->f_op->read || f->f_op->read_iter))
  635. f->f_mode |= FMODE_CAN_READ;
  636. if ((f->f_mode & FMODE_WRITE) &&
  637. likely(f->f_op->write || f->f_op->write_iter))
  638. f->f_mode |= FMODE_CAN_WRITE;
  639. f->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  640. file_ra_state_init(&f->f_ra, f->f_mapping->host->i_mapping);
  641. return 0;
  642. cleanup_all:
  643. fops_put(f->f_op);
  644. if (f->f_mode & FMODE_WRITER) {
  645. put_write_access(inode);
  646. __mnt_drop_write(f->f_path.mnt);
  647. }
  648. cleanup_file:
  649. path_put(&f->f_path);
  650. f->f_path.mnt = NULL;
  651. f->f_path.dentry = NULL;
  652. f->f_inode = NULL;
  653. return error;
  654. }
  655. /**
  656. * finish_open - finish opening a file
  657. * @file: file pointer
  658. * @dentry: pointer to dentry
  659. * @open: open callback
  660. * @opened: state of open
  661. *
  662. * This can be used to finish opening a file passed to i_op->atomic_open().
  663. *
  664. * If the open callback is set to NULL, then the standard f_op->open()
  665. * filesystem callback is substituted.
  666. *
  667. * NB: the dentry reference is _not_ consumed. If, for example, the dentry is
  668. * the return value of d_splice_alias(), then the caller needs to perform dput()
  669. * on it after finish_open().
  670. *
  671. * On successful return @file is a fully instantiated open file. After this, if
  672. * an error occurs in ->atomic_open(), it needs to clean up with fput().
  673. *
  674. * Returns zero on success or -errno if the open failed.
  675. */
  676. int finish_open(struct file *file, struct dentry *dentry,
  677. int (*open)(struct inode *, struct file *),
  678. int *opened)
  679. {
  680. int error;
  681. BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
  682. file->f_path.dentry = dentry;
  683. error = do_dentry_open(file, open, current_cred());
  684. if (!error)
  685. *opened |= FILE_OPENED;
  686. return error;
  687. }
  688. EXPORT_SYMBOL(finish_open);
  689. /**
  690. * finish_no_open - finish ->atomic_open() without opening the file
  691. *
  692. * @file: file pointer
  693. * @dentry: dentry or NULL (as returned from ->lookup())
  694. *
  695. * This can be used to set the result of a successful lookup in ->atomic_open().
  696. *
  697. * NB: unlike finish_open() this function does consume the dentry reference and
  698. * the caller need not dput() it.
  699. *
  700. * Returns "1" which must be the return value of ->atomic_open() after having
  701. * called this function.
  702. */
  703. int finish_no_open(struct file *file, struct dentry *dentry)
  704. {
  705. file->f_path.dentry = dentry;
  706. return 1;
  707. }
  708. EXPORT_SYMBOL(finish_no_open);
  709. struct file *dentry_open(const struct path *path, int flags,
  710. const struct cred *cred)
  711. {
  712. int error;
  713. struct file *f;
  714. validate_creds(cred);
  715. /* We must always pass in a valid mount pointer. */
  716. BUG_ON(!path->mnt);
  717. f = get_empty_filp();
  718. if (!IS_ERR(f)) {
  719. f->f_flags = flags;
  720. error = vfs_open(path, f, cred);
  721. if (!error) {
  722. /* from now on we need fput() to dispose of f */
  723. error = open_check_o_direct(f);
  724. if (error) {
  725. fput(f);
  726. f = ERR_PTR(error);
  727. }
  728. } else {
  729. put_filp(f);
  730. f = ERR_PTR(error);
  731. }
  732. }
  733. return f;
  734. }
  735. EXPORT_SYMBOL(dentry_open);
  736. /**
  737. * vfs_open - open the file at the given path
  738. * @path: path to open
  739. * @filp: newly allocated file with f_flag initialized
  740. * @cred: credentials to use
  741. */
  742. int vfs_open(const struct path *path, struct file *filp,
  743. const struct cred *cred)
  744. {
  745. struct inode *inode = path->dentry->d_inode;
  746. if (inode->i_op->dentry_open)
  747. return inode->i_op->dentry_open(path->dentry, filp, cred);
  748. else {
  749. filp->f_path = *path;
  750. return do_dentry_open(filp, NULL, cred);
  751. }
  752. }
  753. EXPORT_SYMBOL(vfs_open);
  754. static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
  755. {
  756. int lookup_flags = 0;
  757. int acc_mode;
  758. if (flags & (O_CREAT | __O_TMPFILE))
  759. op->mode = (mode & S_IALLUGO) | S_IFREG;
  760. else
  761. op->mode = 0;
  762. /* Must never be set by userspace */
  763. flags &= ~FMODE_NONOTIFY & ~O_CLOEXEC;
  764. /*
  765. * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
  766. * check for O_DSYNC if the need any syncing at all we enforce it's
  767. * always set instead of having to deal with possibly weird behaviour
  768. * for malicious applications setting only __O_SYNC.
  769. */
  770. if (flags & __O_SYNC)
  771. flags |= O_DSYNC;
  772. if (flags & __O_TMPFILE) {
  773. if ((flags & O_TMPFILE_MASK) != O_TMPFILE)
  774. return -EINVAL;
  775. acc_mode = MAY_OPEN | ACC_MODE(flags);
  776. if (!(acc_mode & MAY_WRITE))
  777. return -EINVAL;
  778. } else if (flags & O_PATH) {
  779. /*
  780. * If we have O_PATH in the open flag. Then we
  781. * cannot have anything other than the below set of flags
  782. */
  783. flags &= O_DIRECTORY | O_NOFOLLOW | O_PATH;
  784. acc_mode = 0;
  785. } else {
  786. acc_mode = MAY_OPEN | ACC_MODE(flags);
  787. }
  788. op->open_flag = flags;
  789. /* O_TRUNC implies we need access checks for write permissions */
  790. if (flags & O_TRUNC)
  791. acc_mode |= MAY_WRITE;
  792. /* Allow the LSM permission hook to distinguish append
  793. access from general write access. */
  794. if (flags & O_APPEND)
  795. acc_mode |= MAY_APPEND;
  796. op->acc_mode = acc_mode;
  797. op->intent = flags & O_PATH ? 0 : LOOKUP_OPEN;
  798. if (flags & O_CREAT) {
  799. op->intent |= LOOKUP_CREATE;
  800. if (flags & O_EXCL)
  801. op->intent |= LOOKUP_EXCL;
  802. }
  803. if (flags & O_DIRECTORY)
  804. lookup_flags |= LOOKUP_DIRECTORY;
  805. if (!(flags & O_NOFOLLOW))
  806. lookup_flags |= LOOKUP_FOLLOW;
  807. op->lookup_flags = lookup_flags;
  808. return 0;
  809. }
  810. /**
  811. * file_open_name - open file and return file pointer
  812. *
  813. * @name: struct filename containing path to open
  814. * @flags: open flags as per the open(2) second argument
  815. * @mode: mode for the new file if O_CREAT is set, else ignored
  816. *
  817. * This is the helper to open a file from kernelspace if you really
  818. * have to. But in generally you should not do this, so please move
  819. * along, nothing to see here..
  820. */
  821. struct file *file_open_name(struct filename *name, int flags, umode_t mode)
  822. {
  823. struct open_flags op;
  824. int err = build_open_flags(flags, mode, &op);
  825. return err ? ERR_PTR(err) : do_filp_open(AT_FDCWD, name, &op);
  826. }
  827. /**
  828. * filp_open - open file and return file pointer
  829. *
  830. * @filename: path to open
  831. * @flags: open flags as per the open(2) second argument
  832. * @mode: mode for the new file if O_CREAT is set, else ignored
  833. *
  834. * This is the helper to open a file from kernelspace if you really
  835. * have to. But in generally you should not do this, so please move
  836. * along, nothing to see here..
  837. */
  838. struct file *filp_open(const char *filename, int flags, umode_t mode)
  839. {
  840. struct filename *name = getname_kernel(filename);
  841. struct file *file = ERR_CAST(name);
  842. if (!IS_ERR(name)) {
  843. file = file_open_name(name, flags, mode);
  844. putname(name);
  845. }
  846. return file;
  847. }
  848. EXPORT_SYMBOL(filp_open);
  849. struct file *file_open_root(struct dentry *dentry, struct vfsmount *mnt,
  850. const char *filename, int flags)
  851. {
  852. struct open_flags op;
  853. int err = build_open_flags(flags, 0, &op);
  854. if (err)
  855. return ERR_PTR(err);
  856. if (flags & O_CREAT)
  857. return ERR_PTR(-EINVAL);
  858. return do_file_open_root(dentry, mnt, filename, &op);
  859. }
  860. EXPORT_SYMBOL(file_open_root);
  861. long do_sys_open(int dfd, const char __user *filename, int flags, umode_t mode)
  862. {
  863. struct open_flags op;
  864. int fd = build_open_flags(flags, mode, &op);
  865. struct filename *tmp;
  866. if (fd)
  867. return fd;
  868. tmp = getname(filename);
  869. if (IS_ERR(tmp))
  870. return PTR_ERR(tmp);
  871. fd = get_unused_fd_flags(flags);
  872. if (fd >= 0) {
  873. struct file *f = do_filp_open(dfd, tmp, &op);
  874. if (IS_ERR(f)) {
  875. put_unused_fd(fd);
  876. fd = PTR_ERR(f);
  877. } else {
  878. fsnotify_open(f);
  879. fd_install(fd, f);
  880. }
  881. }
  882. putname(tmp);
  883. return fd;
  884. }
  885. SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
  886. {
  887. if (force_o_largefile())
  888. flags |= O_LARGEFILE;
  889. return do_sys_open(AT_FDCWD, filename, flags, mode);
  890. }
  891. SYSCALL_DEFINE4(openat, int, dfd, const char __user *, filename, int, flags,
  892. umode_t, mode)
  893. {
  894. if (force_o_largefile())
  895. flags |= O_LARGEFILE;
  896. return do_sys_open(dfd, filename, flags, mode);
  897. }
  898. #ifndef __alpha__
  899. /*
  900. * For backward compatibility? Maybe this should be moved
  901. * into arch/i386 instead?
  902. */
  903. SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
  904. {
  905. return sys_open(pathname, O_CREAT | O_WRONLY | O_TRUNC, mode);
  906. }
  907. #endif
  908. /*
  909. * "id" is the POSIX thread ID. We use the
  910. * files pointer for this..
  911. */
  912. int filp_close(struct file *filp, fl_owner_t id)
  913. {
  914. int retval = 0;
  915. if (!file_count(filp)) {
  916. printk(KERN_ERR "VFS: Close: file count is 0\n");
  917. return 0;
  918. }
  919. if (filp->f_op->flush)
  920. retval = filp->f_op->flush(filp, id);
  921. if (likely(!(filp->f_mode & FMODE_PATH))) {
  922. dnotify_flush(filp, id);
  923. locks_remove_posix(filp, id);
  924. }
  925. fput(filp);
  926. return retval;
  927. }
  928. EXPORT_SYMBOL(filp_close);
  929. /*
  930. * Careful here! We test whether the file pointer is NULL before
  931. * releasing the fd. This ensures that one clone task can't release
  932. * an fd while another clone is opening it.
  933. */
  934. SYSCALL_DEFINE1(close, unsigned int, fd)
  935. {
  936. int retval = __close_fd(current->files, fd);
  937. /* can't restart close syscall because file table entry was cleared */
  938. if (unlikely(retval == -ERESTARTSYS ||
  939. retval == -ERESTARTNOINTR ||
  940. retval == -ERESTARTNOHAND ||
  941. retval == -ERESTART_RESTARTBLOCK))
  942. retval = -EINTR;
  943. return retval;
  944. }
  945. EXPORT_SYMBOL(sys_close);
  946. /*
  947. * This routine simulates a hangup on the tty, to arrange that users
  948. * are given clean terminals at login time.
  949. */
  950. SYSCALL_DEFINE0(vhangup)
  951. {
  952. if (capable(CAP_SYS_TTY_CONFIG)) {
  953. tty_vhangup_self();
  954. return 0;
  955. }
  956. return -EPERM;
  957. }
  958. /*
  959. * Called when an inode is about to be open.
  960. * We use this to disallow opening large files on 32bit systems if
  961. * the caller didn't specify O_LARGEFILE. On 64bit systems we force
  962. * on this flag in sys_open.
  963. */
  964. int generic_file_open(struct inode * inode, struct file * filp)
  965. {
  966. if (!(filp->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  967. return -EOVERFLOW;
  968. return 0;
  969. }
  970. EXPORT_SYMBOL(generic_file_open);
  971. /*
  972. * This is used by subsystems that don't want seekable
  973. * file descriptors. The function is not supposed to ever fail, the only
  974. * reason it returns an 'int' and not 'void' is so that it can be plugged
  975. * directly into file_operations structure.
  976. */
  977. int nonseekable_open(struct inode *inode, struct file *filp)
  978. {
  979. filp->f_mode &= ~(FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE);
  980. return 0;
  981. }
  982. EXPORT_SYMBOL(nonseekable_open);