open.c 29 KB

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