file.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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/filelock.h>
  8. #include <linux/mount.h>
  9. #include <linux/xattr.h>
  10. #include <linux/uio.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/security.h>
  13. #include <linux/fs.h>
  14. #include <linux/backing-file.h>
  15. #include "overlayfs.h"
  16. static char ovl_whatisit(struct inode *inode, struct inode *realinode)
  17. {
  18. if (realinode != ovl_inode_upper(inode))
  19. return 'l';
  20. if (ovl_has_upperdata(inode))
  21. return 'u';
  22. else
  23. return 'm';
  24. }
  25. static struct file *ovl_open_realfile(const struct file *file,
  26. const struct path *realpath)
  27. {
  28. struct inode *realinode = d_inode(realpath->dentry);
  29. struct inode *inode = file_inode(file);
  30. struct mnt_idmap *real_idmap;
  31. struct file *realfile;
  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. with_ovl_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(file),
  46. flags, realpath, current_cred());
  47. }
  48. }
  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. struct ovl_file {
  76. struct file *realfile;
  77. struct file *upperfile;
  78. };
  79. struct ovl_file *ovl_file_alloc(struct file *realfile)
  80. {
  81. struct ovl_file *of = kzalloc_obj(struct ovl_file);
  82. if (unlikely(!of))
  83. return NULL;
  84. of->realfile = realfile;
  85. return of;
  86. }
  87. void ovl_file_free(struct ovl_file *of)
  88. {
  89. fput(of->realfile);
  90. if (of->upperfile)
  91. fput(of->upperfile);
  92. kfree(of);
  93. }
  94. static bool ovl_is_real_file(const struct file *realfile,
  95. const struct path *realpath)
  96. {
  97. return file_inode(realfile) == d_inode(realpath->dentry);
  98. }
  99. static struct file *ovl_real_file_path(const struct file *file,
  100. const struct path *realpath)
  101. {
  102. struct ovl_file *of = file->private_data;
  103. struct file *realfile = of->realfile;
  104. if (WARN_ON_ONCE(!realpath->dentry))
  105. return ERR_PTR(-EIO);
  106. /*
  107. * If the realfile that we want is not where the data used to be at
  108. * open time, either we'd been copied up, or it's an fsync of a
  109. * metacopied file. We need the upperfile either way, so see if it
  110. * is already opened and if it is not then open and store it.
  111. */
  112. if (unlikely(!ovl_is_real_file(realfile, realpath))) {
  113. struct file *upperfile = READ_ONCE(of->upperfile);
  114. struct file *old;
  115. if (!upperfile) { /* Nobody opened upperfile yet */
  116. upperfile = ovl_open_realfile(file, realpath);
  117. if (IS_ERR(upperfile))
  118. return upperfile;
  119. /* Store the upperfile for later */
  120. old = cmpxchg_release(&of->upperfile, NULL, upperfile);
  121. if (old) { /* Someone opened upperfile before us */
  122. fput(upperfile);
  123. upperfile = old;
  124. }
  125. }
  126. /*
  127. * Stored file must be from the right inode, unless someone's
  128. * been corrupting the upper layer.
  129. */
  130. if (WARN_ON_ONCE(!ovl_is_real_file(upperfile, realpath)))
  131. return ERR_PTR(-EIO);
  132. realfile = upperfile;
  133. }
  134. /* Did the flags change since open? */
  135. if (unlikely((file->f_flags ^ realfile->f_flags) & ~OVL_OPEN_FLAGS)) {
  136. int err = ovl_change_flags(realfile, file->f_flags);
  137. if (err)
  138. return ERR_PTR(err);
  139. }
  140. return realfile;
  141. }
  142. static struct file *ovl_real_file(const struct file *file)
  143. {
  144. struct dentry *dentry = file_dentry(file);
  145. struct path realpath;
  146. int err;
  147. if (d_is_dir(dentry)) {
  148. struct file *f = ovl_dir_real_file(file, false);
  149. if (WARN_ON_ONCE(!f))
  150. return ERR_PTR(-EIO);
  151. return f;
  152. }
  153. /* lazy lookup and verify of lowerdata */
  154. err = ovl_verify_lowerdata(dentry);
  155. if (err)
  156. return ERR_PTR(err);
  157. ovl_path_realdata(dentry, &realpath);
  158. return ovl_real_file_path(file, &realpath);
  159. }
  160. static int ovl_open(struct inode *inode, struct file *file)
  161. {
  162. struct dentry *dentry = file_dentry(file);
  163. struct file *realfile;
  164. struct path realpath;
  165. struct ovl_file *of;
  166. int err;
  167. /* lazy lookup and verify lowerdata */
  168. err = ovl_verify_lowerdata(dentry);
  169. if (err)
  170. return err;
  171. err = ovl_maybe_copy_up(dentry, file->f_flags);
  172. if (err)
  173. return err;
  174. /* No longer need these flags, so don't pass them on to underlying fs */
  175. file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
  176. ovl_path_realdata(dentry, &realpath);
  177. if (!realpath.dentry)
  178. return -EIO;
  179. realfile = ovl_open_realfile(file, &realpath);
  180. if (IS_ERR(realfile))
  181. return PTR_ERR(realfile);
  182. of = ovl_file_alloc(realfile);
  183. if (!of) {
  184. fput(realfile);
  185. return -ENOMEM;
  186. }
  187. file->private_data = of;
  188. return 0;
  189. }
  190. static int ovl_release(struct inode *inode, struct file *file)
  191. {
  192. ovl_file_free(file->private_data);
  193. return 0;
  194. }
  195. static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
  196. {
  197. struct inode *inode = file_inode(file);
  198. struct file *realfile;
  199. loff_t ret;
  200. /*
  201. * The two special cases below do not need to involve real fs,
  202. * so we can optimizing concurrent callers.
  203. */
  204. if (offset == 0) {
  205. if (whence == SEEK_CUR)
  206. return file->f_pos;
  207. if (whence == SEEK_SET)
  208. return vfs_setpos(file, 0, 0);
  209. }
  210. realfile = ovl_real_file(file);
  211. if (IS_ERR(realfile))
  212. return PTR_ERR(realfile);
  213. /*
  214. * Overlay file f_pos is the master copy that is preserved
  215. * through copy up and modified on read/write, but only real
  216. * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose
  217. * limitations that are more strict than ->s_maxbytes for specific
  218. * files, so we use the real file to perform seeks.
  219. */
  220. ovl_inode_lock(inode);
  221. realfile->f_pos = file->f_pos;
  222. with_ovl_creds(inode->i_sb)
  223. ret = vfs_llseek(realfile, offset, whence);
  224. file->f_pos = realfile->f_pos;
  225. ovl_inode_unlock(inode);
  226. return ret;
  227. }
  228. static void ovl_file_modified(struct file *file)
  229. {
  230. /* Update size/mtime */
  231. ovl_copyattr(file_inode(file));
  232. }
  233. static void ovl_file_end_write(struct kiocb *iocb, ssize_t ret)
  234. {
  235. ovl_file_modified(iocb->ki_filp);
  236. }
  237. static void ovl_file_accessed(struct file *file)
  238. {
  239. struct inode *inode, *upperinode;
  240. struct timespec64 ctime, uctime;
  241. struct timespec64 mtime, umtime;
  242. if (file->f_flags & O_NOATIME)
  243. return;
  244. inode = file_inode(file);
  245. upperinode = ovl_inode_upper(inode);
  246. if (!upperinode)
  247. return;
  248. ctime = inode_get_ctime(inode);
  249. uctime = inode_get_ctime(upperinode);
  250. mtime = inode_get_mtime(inode);
  251. umtime = inode_get_mtime(upperinode);
  252. if ((!timespec64_equal(&mtime, &umtime)) ||
  253. !timespec64_equal(&ctime, &uctime)) {
  254. inode_set_mtime_to_ts(inode, inode_get_mtime(upperinode));
  255. inode_set_ctime_to_ts(inode, uctime);
  256. }
  257. touch_atime(&file->f_path);
  258. }
  259. static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
  260. {
  261. struct file *file = iocb->ki_filp;
  262. struct file *realfile;
  263. struct backing_file_ctx ctx = {
  264. .cred = ovl_creds(file_inode(file)->i_sb),
  265. .accessed = ovl_file_accessed,
  266. };
  267. if (!iov_iter_count(iter))
  268. return 0;
  269. realfile = ovl_real_file(file);
  270. if (IS_ERR(realfile))
  271. return PTR_ERR(realfile);
  272. return backing_file_read_iter(realfile, iter, iocb, iocb->ki_flags,
  273. &ctx);
  274. }
  275. static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
  276. {
  277. struct file *file = iocb->ki_filp;
  278. struct inode *inode = file_inode(file);
  279. struct file *realfile;
  280. ssize_t ret;
  281. int ifl = iocb->ki_flags;
  282. struct backing_file_ctx ctx = {
  283. .cred = ovl_creds(inode->i_sb),
  284. .end_write = ovl_file_end_write,
  285. };
  286. if (!iov_iter_count(iter))
  287. return 0;
  288. inode_lock(inode);
  289. /* Update mode */
  290. ovl_copyattr(inode);
  291. realfile = ovl_real_file(file);
  292. ret = PTR_ERR(realfile);
  293. if (IS_ERR(realfile))
  294. goto out_unlock;
  295. if (!ovl_should_sync(OVL_FS(inode->i_sb)))
  296. ifl &= ~(IOCB_DSYNC | IOCB_SYNC);
  297. ret = backing_file_write_iter(realfile, iter, iocb, ifl, &ctx);
  298. out_unlock:
  299. inode_unlock(inode);
  300. return ret;
  301. }
  302. static ssize_t ovl_splice_read(struct file *in, loff_t *ppos,
  303. struct pipe_inode_info *pipe, size_t len,
  304. unsigned int flags)
  305. {
  306. struct file *realfile;
  307. ssize_t ret;
  308. struct backing_file_ctx ctx = {
  309. .cred = ovl_creds(file_inode(in)->i_sb),
  310. .accessed = ovl_file_accessed,
  311. };
  312. struct kiocb iocb;
  313. realfile = ovl_real_file(in);
  314. if (IS_ERR(realfile))
  315. return PTR_ERR(realfile);
  316. init_sync_kiocb(&iocb, in);
  317. iocb.ki_pos = *ppos;
  318. ret = backing_file_splice_read(realfile, &iocb, pipe, len, flags, &ctx);
  319. *ppos = iocb.ki_pos;
  320. return ret;
  321. }
  322. /*
  323. * Calling iter_file_splice_write() directly from overlay's f_op may deadlock
  324. * due to lock order inversion between pipe->mutex in iter_file_splice_write()
  325. * and file_start_write(realfile) in ovl_write_iter().
  326. *
  327. * So do everything ovl_write_iter() does and call iter_file_splice_write() on
  328. * the real file.
  329. */
  330. static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out,
  331. loff_t *ppos, size_t len, unsigned int flags)
  332. {
  333. struct file *realfile;
  334. struct inode *inode = file_inode(out);
  335. ssize_t ret;
  336. struct backing_file_ctx ctx = {
  337. .cred = ovl_creds(inode->i_sb),
  338. .end_write = ovl_file_end_write,
  339. };
  340. struct kiocb iocb;
  341. inode_lock(inode);
  342. /* Update mode */
  343. ovl_copyattr(inode);
  344. realfile = ovl_real_file(out);
  345. ret = PTR_ERR(realfile);
  346. if (IS_ERR(realfile))
  347. goto out_unlock;
  348. init_sync_kiocb(&iocb, out);
  349. iocb.ki_pos = *ppos;
  350. ret = backing_file_splice_write(pipe, realfile, &iocb, len, flags, &ctx);
  351. *ppos = iocb.ki_pos;
  352. out_unlock:
  353. inode_unlock(inode);
  354. return ret;
  355. }
  356. static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  357. {
  358. struct dentry *dentry = file_dentry(file);
  359. enum ovl_path_type type;
  360. struct path upperpath;
  361. struct file *upperfile;
  362. int ret;
  363. ret = ovl_sync_status(OVL_FS(file_inode(file)->i_sb));
  364. if (ret <= 0)
  365. return ret;
  366. /* Don't sync lower file for fear of receiving EROFS error */
  367. type = ovl_path_type(dentry);
  368. if (!OVL_TYPE_UPPER(type) || (datasync && OVL_TYPE_MERGE(type)))
  369. return 0;
  370. ovl_path_upper(dentry, &upperpath);
  371. upperfile = ovl_real_file_path(file, &upperpath);
  372. if (IS_ERR(upperfile))
  373. return PTR_ERR(upperfile);
  374. with_ovl_creds(file_inode(file)->i_sb)
  375. return vfs_fsync_range(upperfile, start, end, datasync);
  376. }
  377. static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
  378. {
  379. struct ovl_file *of = file->private_data;
  380. struct backing_file_ctx ctx = {
  381. .cred = ovl_creds(file_inode(file)->i_sb),
  382. .accessed = ovl_file_accessed,
  383. };
  384. return backing_file_mmap(of->realfile, vma, &ctx);
  385. }
  386. static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  387. {
  388. struct inode *inode = file_inode(file);
  389. struct file *realfile;
  390. int ret;
  391. inode_lock(inode);
  392. /* Update mode */
  393. ovl_copyattr(inode);
  394. ret = file_remove_privs(file);
  395. if (ret)
  396. goto out_unlock;
  397. realfile = ovl_real_file(file);
  398. ret = PTR_ERR(realfile);
  399. if (IS_ERR(realfile))
  400. goto out_unlock;
  401. with_ovl_creds(inode->i_sb)
  402. ret = vfs_fallocate(realfile, mode, offset, len);
  403. /* Update size */
  404. ovl_file_modified(file);
  405. out_unlock:
  406. inode_unlock(inode);
  407. return ret;
  408. }
  409. static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
  410. {
  411. struct file *realfile;
  412. realfile = ovl_real_file(file);
  413. if (IS_ERR(realfile))
  414. return PTR_ERR(realfile);
  415. with_ovl_creds(file_inode(file)->i_sb)
  416. return vfs_fadvise(realfile, offset, len, advice);
  417. }
  418. enum ovl_copyop {
  419. OVL_COPY,
  420. OVL_CLONE,
  421. OVL_DEDUPE,
  422. };
  423. static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
  424. struct file *file_out, loff_t pos_out,
  425. loff_t len, unsigned int flags, enum ovl_copyop op)
  426. {
  427. struct inode *inode_out = file_inode(file_out);
  428. struct file *realfile_in, *realfile_out;
  429. loff_t ret;
  430. inode_lock(inode_out);
  431. if (op != OVL_DEDUPE) {
  432. /* Update mode */
  433. ovl_copyattr(inode_out);
  434. ret = file_remove_privs(file_out);
  435. if (ret)
  436. goto out_unlock;
  437. }
  438. realfile_out = ovl_real_file(file_out);
  439. ret = PTR_ERR(realfile_out);
  440. if (IS_ERR(realfile_out))
  441. goto out_unlock;
  442. realfile_in = ovl_real_file(file_in);
  443. ret = PTR_ERR(realfile_in);
  444. if (IS_ERR(realfile_in))
  445. goto out_unlock;
  446. with_ovl_creds(file_inode(file_out)->i_sb) {
  447. switch (op) {
  448. case OVL_COPY:
  449. ret = vfs_copy_file_range(realfile_in, pos_in,
  450. realfile_out, pos_out, len, flags);
  451. break;
  452. case OVL_CLONE:
  453. ret = vfs_clone_file_range(realfile_in, pos_in,
  454. realfile_out, pos_out, len, flags);
  455. break;
  456. case OVL_DEDUPE:
  457. ret = vfs_dedupe_file_range_one(realfile_in, pos_in,
  458. realfile_out, pos_out, len,
  459. flags);
  460. break;
  461. }
  462. }
  463. /* Update size */
  464. ovl_file_modified(file_out);
  465. out_unlock:
  466. inode_unlock(inode_out);
  467. return ret;
  468. }
  469. static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
  470. struct file *file_out, loff_t pos_out,
  471. size_t len, unsigned int flags)
  472. {
  473. return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
  474. OVL_COPY);
  475. }
  476. static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
  477. struct file *file_out, loff_t pos_out,
  478. loff_t len, unsigned int remap_flags)
  479. {
  480. enum ovl_copyop op;
  481. if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
  482. return -EINVAL;
  483. if (remap_flags & REMAP_FILE_DEDUP)
  484. op = OVL_DEDUPE;
  485. else
  486. op = OVL_CLONE;
  487. /*
  488. * Don't copy up because of a dedupe request, this wouldn't make sense
  489. * most of the time (data would be duplicated instead of deduplicated).
  490. */
  491. if (op == OVL_DEDUPE &&
  492. (!ovl_inode_upper(file_inode(file_in)) ||
  493. !ovl_inode_upper(file_inode(file_out))))
  494. return -EPERM;
  495. return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
  496. remap_flags, op);
  497. }
  498. static int ovl_flush(struct file *file, fl_owner_t id)
  499. {
  500. struct file *realfile;
  501. int err = 0;
  502. realfile = ovl_real_file(file);
  503. if (IS_ERR(realfile))
  504. return PTR_ERR(realfile);
  505. if (realfile->f_op->flush) {
  506. with_ovl_creds(file_inode(file)->i_sb)
  507. err = realfile->f_op->flush(realfile, id);
  508. }
  509. return err;
  510. }
  511. const struct file_operations ovl_file_operations = {
  512. .open = ovl_open,
  513. .release = ovl_release,
  514. .llseek = ovl_llseek,
  515. .read_iter = ovl_read_iter,
  516. .write_iter = ovl_write_iter,
  517. .fsync = ovl_fsync,
  518. .mmap = ovl_mmap,
  519. .fallocate = ovl_fallocate,
  520. .fadvise = ovl_fadvise,
  521. .flush = ovl_flush,
  522. .splice_read = ovl_splice_read,
  523. .splice_write = ovl_splice_write,
  524. .copy_file_range = ovl_copy_file_range,
  525. .remap_file_range = ovl_remap_file_range,
  526. .setlease = generic_setlease,
  527. };