file.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2017 Red Hat, Inc.
  4. */
  5. #include <linux/cred.h>
  6. #include <linux/file.h>
  7. #include <linux/mount.h>
  8. #include <linux/xattr.h>
  9. #include <linux/uio.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/security.h>
  12. #include <linux/fs.h>
  13. #include <linux/backing-file.h>
  14. #include "overlayfs.h"
  15. static char ovl_whatisit(struct inode *inode, struct inode *realinode)
  16. {
  17. if (realinode != ovl_inode_upper(inode))
  18. return 'l';
  19. if (ovl_has_upperdata(inode))
  20. return 'u';
  21. else
  22. return 'm';
  23. }
  24. static struct file *ovl_open_realfile(const struct file *file,
  25. const struct path *realpath)
  26. {
  27. struct inode *realinode = d_inode(realpath->dentry);
  28. struct inode *inode = file_inode(file);
  29. struct mnt_idmap *real_idmap;
  30. struct file *realfile;
  31. const struct cred *old_cred;
  32. int flags = file->f_flags | OVL_OPEN_FLAGS;
  33. int acc_mode = ACC_MODE(flags);
  34. int err;
  35. if (flags & O_APPEND)
  36. acc_mode |= MAY_APPEND;
  37. old_cred = ovl_override_creds(inode->i_sb);
  38. real_idmap = mnt_idmap(realpath->mnt);
  39. err = inode_permission(real_idmap, realinode, MAY_OPEN | acc_mode);
  40. if (err) {
  41. realfile = ERR_PTR(err);
  42. } else {
  43. if (!inode_owner_or_capable(real_idmap, realinode))
  44. flags &= ~O_NOATIME;
  45. realfile = backing_file_open(file_user_path((struct file *) file),
  46. flags, realpath, current_cred());
  47. }
  48. revert_creds(old_cred);
  49. pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
  50. file, file, ovl_whatisit(inode, realinode), file->f_flags,
  51. realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
  52. return realfile;
  53. }
  54. #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
  55. static int ovl_change_flags(struct file *file, unsigned int flags)
  56. {
  57. struct inode *inode = file_inode(file);
  58. int err;
  59. flags &= OVL_SETFL_MASK;
  60. if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
  61. return -EPERM;
  62. if ((flags & O_DIRECT) && !(file->f_mode & FMODE_CAN_ODIRECT))
  63. return -EINVAL;
  64. if (file->f_op->check_flags) {
  65. err = file->f_op->check_flags(flags);
  66. if (err)
  67. return err;
  68. }
  69. spin_lock(&file->f_lock);
  70. file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
  71. file->f_iocb_flags = iocb_flags(file);
  72. spin_unlock(&file->f_lock);
  73. return 0;
  74. }
  75. static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
  76. bool allow_meta)
  77. {
  78. struct dentry *dentry = file_dentry(file);
  79. struct file *realfile = file->private_data;
  80. struct path realpath;
  81. int err;
  82. real->word = (unsigned long)realfile;
  83. if (allow_meta) {
  84. ovl_path_real(dentry, &realpath);
  85. } else {
  86. /* lazy lookup and verify of lowerdata */
  87. err = ovl_verify_lowerdata(dentry);
  88. if (err)
  89. return err;
  90. ovl_path_realdata(dentry, &realpath);
  91. }
  92. if (!realpath.dentry)
  93. return -EIO;
  94. /* Has it been copied up since we'd opened it? */
  95. if (unlikely(file_inode(realfile) != d_inode(realpath.dentry))) {
  96. struct file *f = ovl_open_realfile(file, &realpath);
  97. if (IS_ERR(f))
  98. return PTR_ERR(f);
  99. real->word = (unsigned long)f | FDPUT_FPUT;
  100. return 0;
  101. }
  102. /* Did the flags change since open? */
  103. if (unlikely((file->f_flags ^ realfile->f_flags) & ~OVL_OPEN_FLAGS))
  104. return ovl_change_flags(realfile, file->f_flags);
  105. return 0;
  106. }
  107. static int ovl_real_fdget(const struct file *file, struct fd *real)
  108. {
  109. if (d_is_dir(file_dentry(file))) {
  110. struct file *f = ovl_dir_real_file(file, false);
  111. if (IS_ERR(f))
  112. return PTR_ERR(f);
  113. real->word = (unsigned long)f;
  114. return 0;
  115. }
  116. return ovl_real_fdget_meta(file, real, false);
  117. }
  118. static int ovl_open(struct inode *inode, struct file *file)
  119. {
  120. struct dentry *dentry = file_dentry(file);
  121. struct file *realfile;
  122. struct path realpath;
  123. int err;
  124. /* lazy lookup and verify lowerdata */
  125. err = ovl_verify_lowerdata(dentry);
  126. if (err)
  127. return err;
  128. err = ovl_maybe_copy_up(dentry, file->f_flags);
  129. if (err)
  130. return err;
  131. /* No longer need these flags, so don't pass them on to underlying fs */
  132. file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  133. ovl_path_realdata(dentry, &realpath);
  134. if (!realpath.dentry)
  135. return -EIO;
  136. realfile = ovl_open_realfile(file, &realpath);
  137. if (IS_ERR(realfile))
  138. return PTR_ERR(realfile);
  139. file->private_data = realfile;
  140. return 0;
  141. }
  142. static int ovl_release(struct inode *inode, struct file *file)
  143. {
  144. fput(file->private_data);
  145. return 0;
  146. }
  147. static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
  148. {
  149. struct inode *inode = file_inode(file);
  150. struct fd real;
  151. const struct cred *old_cred;
  152. loff_t ret;
  153. /*
  154. * The two special cases below do not need to involve real fs,
  155. * so we can optimizing concurrent callers.
  156. */
  157. if (offset == 0) {
  158. if (whence == SEEK_CUR)
  159. return file->f_pos;
  160. if (whence == SEEK_SET)
  161. return vfs_setpos(file, 0, 0);
  162. }
  163. ret = ovl_real_fdget(file, &real);
  164. if (ret)
  165. return ret;
  166. /*
  167. * Overlay file f_pos is the master copy that is preserved
  168. * through copy up and modified on read/write, but only real
  169. * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose
  170. * limitations that are more strict than ->s_maxbytes for specific
  171. * files, so we use the real file to perform seeks.
  172. */
  173. ovl_inode_lock(inode);
  174. fd_file(real)->f_pos = file->f_pos;
  175. old_cred = ovl_override_creds(inode->i_sb);
  176. ret = vfs_llseek(fd_file(real), offset, whence);
  177. revert_creds(old_cred);
  178. file->f_pos = fd_file(real)->f_pos;
  179. ovl_inode_unlock(inode);
  180. fdput(real);
  181. return ret;
  182. }
  183. static void ovl_file_modified(struct file *file)
  184. {
  185. /* Update size/mtime */
  186. ovl_copyattr(file_inode(file));
  187. }
  188. static void ovl_file_end_write(struct file *file, loff_t pos, ssize_t ret)
  189. {
  190. ovl_file_modified(file);
  191. }
  192. static void ovl_file_accessed(struct file *file)
  193. {
  194. struct inode *inode, *upperinode;
  195. struct timespec64 ctime, uctime;
  196. struct timespec64 mtime, umtime;
  197. if (file->f_flags & O_NOATIME)
  198. return;
  199. inode = file_inode(file);
  200. upperinode = ovl_inode_upper(inode);
  201. if (!upperinode)
  202. return;
  203. ctime = inode_get_ctime(inode);
  204. uctime = inode_get_ctime(upperinode);
  205. mtime = inode_get_mtime(inode);
  206. umtime = inode_get_mtime(upperinode);
  207. if ((!timespec64_equal(&mtime, &umtime)) ||
  208. !timespec64_equal(&ctime, &uctime)) {
  209. inode_set_mtime_to_ts(inode, inode_get_mtime(upperinode));
  210. inode_set_ctime_to_ts(inode, uctime);
  211. }
  212. touch_atime(&file->f_path);
  213. }
  214. static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
  215. {
  216. struct file *file = iocb->ki_filp;
  217. struct fd real;
  218. ssize_t ret;
  219. struct backing_file_ctx ctx = {
  220. .cred = ovl_creds(file_inode(file)->i_sb),
  221. .user_file = file,
  222. .accessed = ovl_file_accessed,
  223. };
  224. if (!iov_iter_count(iter))
  225. return 0;
  226. ret = ovl_real_fdget(file, &real);
  227. if (ret)
  228. return ret;
  229. ret = backing_file_read_iter(fd_file(real), iter, iocb, iocb->ki_flags,
  230. &ctx);
  231. fdput(real);
  232. return ret;
  233. }
  234. static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
  235. {
  236. struct file *file = iocb->ki_filp;
  237. struct inode *inode = file_inode(file);
  238. struct fd real;
  239. ssize_t ret;
  240. int ifl = iocb->ki_flags;
  241. struct backing_file_ctx ctx = {
  242. .cred = ovl_creds(inode->i_sb),
  243. .user_file = file,
  244. .end_write = ovl_file_end_write,
  245. };
  246. if (!iov_iter_count(iter))
  247. return 0;
  248. inode_lock(inode);
  249. /* Update mode */
  250. ovl_copyattr(inode);
  251. ret = ovl_real_fdget(file, &real);
  252. if (ret)
  253. goto out_unlock;
  254. if (!ovl_should_sync(OVL_FS(inode->i_sb)))
  255. ifl &= ~(IOCB_DSYNC | IOCB_SYNC);
  256. /*
  257. * Overlayfs doesn't support deferred completions, don't copy
  258. * this property in case it is set by the issuer.
  259. */
  260. ifl &= ~IOCB_DIO_CALLER_COMP;
  261. ret = backing_file_write_iter(fd_file(real), iter, iocb, ifl, &ctx);
  262. fdput(real);
  263. out_unlock:
  264. inode_unlock(inode);
  265. return ret;
  266. }
  267. static ssize_t ovl_splice_read(struct file *in, loff_t *ppos,
  268. struct pipe_inode_info *pipe, size_t len,
  269. unsigned int flags)
  270. {
  271. struct fd real;
  272. ssize_t ret;
  273. struct backing_file_ctx ctx = {
  274. .cred = ovl_creds(file_inode(in)->i_sb),
  275. .user_file = in,
  276. .accessed = ovl_file_accessed,
  277. };
  278. ret = ovl_real_fdget(in, &real);
  279. if (ret)
  280. return ret;
  281. ret = backing_file_splice_read(fd_file(real), ppos, pipe, len, flags, &ctx);
  282. fdput(real);
  283. return ret;
  284. }
  285. /*
  286. * Calling iter_file_splice_write() directly from overlay's f_op may deadlock
  287. * due to lock order inversion between pipe->mutex in iter_file_splice_write()
  288. * and file_start_write(fd_file(real)) in ovl_write_iter().
  289. *
  290. * So do everything ovl_write_iter() does and call iter_file_splice_write() on
  291. * the real file.
  292. */
  293. static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out,
  294. loff_t *ppos, size_t len, unsigned int flags)
  295. {
  296. struct fd real;
  297. struct inode *inode = file_inode(out);
  298. ssize_t ret;
  299. struct backing_file_ctx ctx = {
  300. .cred = ovl_creds(inode->i_sb),
  301. .user_file = out,
  302. .end_write = ovl_file_end_write,
  303. };
  304. inode_lock(inode);
  305. /* Update mode */
  306. ovl_copyattr(inode);
  307. ret = ovl_real_fdget(out, &real);
  308. if (ret)
  309. goto out_unlock;
  310. ret = backing_file_splice_write(pipe, fd_file(real), ppos, len, flags, &ctx);
  311. fdput(real);
  312. out_unlock:
  313. inode_unlock(inode);
  314. return ret;
  315. }
  316. static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  317. {
  318. struct fd real;
  319. const struct cred *old_cred;
  320. int ret;
  321. ret = ovl_sync_status(OVL_FS(file_inode(file)->i_sb));
  322. if (ret <= 0)
  323. return ret;
  324. ret = ovl_real_fdget_meta(file, &real, !datasync);
  325. if (ret)
  326. return ret;
  327. /* Don't sync lower file for fear of receiving EROFS error */
  328. if (file_inode(fd_file(real)) == ovl_inode_upper(file_inode(file))) {
  329. old_cred = ovl_override_creds(file_inode(file)->i_sb);
  330. ret = vfs_fsync_range(fd_file(real), start, end, datasync);
  331. revert_creds(old_cred);
  332. }
  333. fdput(real);
  334. return ret;
  335. }
  336. static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
  337. {
  338. struct file *realfile = file->private_data;
  339. struct backing_file_ctx ctx = {
  340. .cred = ovl_creds(file_inode(file)->i_sb),
  341. .user_file = file,
  342. .accessed = ovl_file_accessed,
  343. };
  344. return backing_file_mmap(realfile, vma, &ctx);
  345. }
  346. static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  347. {
  348. struct inode *inode = file_inode(file);
  349. struct fd real;
  350. const struct cred *old_cred;
  351. int ret;
  352. inode_lock(inode);
  353. /* Update mode */
  354. ovl_copyattr(inode);
  355. ret = file_remove_privs(file);
  356. if (ret)
  357. goto out_unlock;
  358. ret = ovl_real_fdget(file, &real);
  359. if (ret)
  360. goto out_unlock;
  361. old_cred = ovl_override_creds(file_inode(file)->i_sb);
  362. ret = vfs_fallocate(fd_file(real), mode, offset, len);
  363. revert_creds(old_cred);
  364. /* Update size */
  365. ovl_file_modified(file);
  366. fdput(real);
  367. out_unlock:
  368. inode_unlock(inode);
  369. return ret;
  370. }
  371. static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
  372. {
  373. struct fd real;
  374. const struct cred *old_cred;
  375. int ret;
  376. ret = ovl_real_fdget(file, &real);
  377. if (ret)
  378. return ret;
  379. old_cred = ovl_override_creds(file_inode(file)->i_sb);
  380. ret = vfs_fadvise(fd_file(real), offset, len, advice);
  381. revert_creds(old_cred);
  382. fdput(real);
  383. return ret;
  384. }
  385. enum ovl_copyop {
  386. OVL_COPY,
  387. OVL_CLONE,
  388. OVL_DEDUPE,
  389. };
  390. static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
  391. struct file *file_out, loff_t pos_out,
  392. loff_t len, unsigned int flags, enum ovl_copyop op)
  393. {
  394. struct inode *inode_out = file_inode(file_out);
  395. struct fd real_in, real_out;
  396. const struct cred *old_cred;
  397. loff_t ret;
  398. inode_lock(inode_out);
  399. if (op != OVL_DEDUPE) {
  400. /* Update mode */
  401. ovl_copyattr(inode_out);
  402. ret = file_remove_privs(file_out);
  403. if (ret)
  404. goto out_unlock;
  405. }
  406. ret = ovl_real_fdget(file_out, &real_out);
  407. if (ret)
  408. goto out_unlock;
  409. ret = ovl_real_fdget(file_in, &real_in);
  410. if (ret) {
  411. fdput(real_out);
  412. goto out_unlock;
  413. }
  414. old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
  415. switch (op) {
  416. case OVL_COPY:
  417. ret = vfs_copy_file_range(fd_file(real_in), pos_in,
  418. fd_file(real_out), pos_out, len, flags);
  419. break;
  420. case OVL_CLONE:
  421. ret = vfs_clone_file_range(fd_file(real_in), pos_in,
  422. fd_file(real_out), pos_out, len, flags);
  423. break;
  424. case OVL_DEDUPE:
  425. ret = vfs_dedupe_file_range_one(fd_file(real_in), pos_in,
  426. fd_file(real_out), pos_out, len,
  427. flags);
  428. break;
  429. }
  430. revert_creds(old_cred);
  431. /* Update size */
  432. ovl_file_modified(file_out);
  433. fdput(real_in);
  434. fdput(real_out);
  435. out_unlock:
  436. inode_unlock(inode_out);
  437. return ret;
  438. }
  439. static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
  440. struct file *file_out, loff_t pos_out,
  441. size_t len, unsigned int flags)
  442. {
  443. return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
  444. OVL_COPY);
  445. }
  446. static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
  447. struct file *file_out, loff_t pos_out,
  448. loff_t len, unsigned int remap_flags)
  449. {
  450. enum ovl_copyop op;
  451. if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
  452. return -EINVAL;
  453. if (remap_flags & REMAP_FILE_DEDUP)
  454. op = OVL_DEDUPE;
  455. else
  456. op = OVL_CLONE;
  457. /*
  458. * Don't copy up because of a dedupe request, this wouldn't make sense
  459. * most of the time (data would be duplicated instead of deduplicated).
  460. */
  461. if (op == OVL_DEDUPE &&
  462. (!ovl_inode_upper(file_inode(file_in)) ||
  463. !ovl_inode_upper(file_inode(file_out))))
  464. return -EPERM;
  465. return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
  466. remap_flags, op);
  467. }
  468. static int ovl_flush(struct file *file, fl_owner_t id)
  469. {
  470. struct fd real;
  471. const struct cred *old_cred;
  472. int err;
  473. err = ovl_real_fdget(file, &real);
  474. if (err)
  475. return err;
  476. if (fd_file(real)->f_op->flush) {
  477. old_cred = ovl_override_creds(file_inode(file)->i_sb);
  478. err = fd_file(real)->f_op->flush(fd_file(real), id);
  479. revert_creds(old_cred);
  480. }
  481. fdput(real);
  482. return err;
  483. }
  484. const struct file_operations ovl_file_operations = {
  485. .open = ovl_open,
  486. .release = ovl_release,
  487. .llseek = ovl_llseek,
  488. .read_iter = ovl_read_iter,
  489. .write_iter = ovl_write_iter,
  490. .fsync = ovl_fsync,
  491. .mmap = ovl_mmap,
  492. .fallocate = ovl_fallocate,
  493. .fadvise = ovl_fadvise,
  494. .flush = ovl_flush,
  495. .splice_read = ovl_splice_read,
  496. .splice_write = ovl_splice_write,
  497. .copy_file_range = ovl_copy_file_range,
  498. .remap_file_range = ovl_remap_file_range,
  499. };