copy_up.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. *
  3. * Copyright (C) 2011 Novell Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/fs.h>
  11. #include <linux/slab.h>
  12. #include <linux/file.h>
  13. #include <linux/splice.h>
  14. #include <linux/xattr.h>
  15. #include <linux/security.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/sched.h>
  18. #include <linux/namei.h>
  19. #include <linux/fdtable.h>
  20. #include <linux/ratelimit.h>
  21. #include "overlayfs.h"
  22. #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
  23. static bool __read_mostly ovl_check_copy_up;
  24. module_param_named(check_copy_up, ovl_check_copy_up, bool,
  25. S_IWUSR | S_IRUGO);
  26. MODULE_PARM_DESC(ovl_check_copy_up,
  27. "Warn on copy-up when causing process also has a R/O fd open");
  28. static int ovl_check_fd(const void *data, struct file *f, unsigned int fd)
  29. {
  30. const struct dentry *dentry = data;
  31. if (file_inode(f) == d_inode(dentry))
  32. pr_warn_ratelimited("overlayfs: Warning: Copying up %pD, but open R/O on fd %u which will cease to be coherent [pid=%d %s]\n",
  33. f, fd, current->pid, current->comm);
  34. return 0;
  35. }
  36. /*
  37. * Check the fds open by this process and warn if something like the following
  38. * scenario is about to occur:
  39. *
  40. * fd1 = open("foo", O_RDONLY);
  41. * fd2 = open("foo", O_RDWR);
  42. */
  43. static void ovl_do_check_copy_up(struct dentry *dentry)
  44. {
  45. if (ovl_check_copy_up)
  46. iterate_fd(current->files, 0, ovl_check_fd, dentry);
  47. }
  48. int ovl_copy_xattr(struct dentry *old, struct dentry *new)
  49. {
  50. ssize_t list_size, size, value_size = 0;
  51. char *buf, *name, *value = NULL;
  52. int uninitialized_var(error);
  53. size_t slen;
  54. if (!(old->d_inode->i_opflags & IOP_XATTR) ||
  55. !(new->d_inode->i_opflags & IOP_XATTR))
  56. return 0;
  57. list_size = vfs_listxattr(old, NULL, 0);
  58. if (list_size <= 0) {
  59. if (list_size == -EOPNOTSUPP)
  60. return 0;
  61. return list_size;
  62. }
  63. buf = kzalloc(list_size, GFP_KERNEL);
  64. if (!buf)
  65. return -ENOMEM;
  66. list_size = vfs_listxattr(old, buf, list_size);
  67. if (list_size <= 0) {
  68. error = list_size;
  69. goto out;
  70. }
  71. for (name = buf; list_size; name += slen) {
  72. slen = strnlen(name, list_size) + 1;
  73. /* underlying fs providing us with an broken xattr list? */
  74. if (WARN_ON(slen > list_size)) {
  75. error = -EIO;
  76. break;
  77. }
  78. list_size -= slen;
  79. if (ovl_is_private_xattr(name))
  80. continue;
  81. retry:
  82. size = vfs_getxattr(old, name, value, value_size);
  83. if (size == -ERANGE)
  84. size = vfs_getxattr(old, name, NULL, 0);
  85. if (size < 0) {
  86. error = size;
  87. break;
  88. }
  89. if (size > value_size) {
  90. void *new;
  91. new = krealloc(value, size, GFP_KERNEL);
  92. if (!new) {
  93. error = -ENOMEM;
  94. break;
  95. }
  96. value = new;
  97. value_size = size;
  98. goto retry;
  99. }
  100. error = security_inode_copy_up_xattr(name);
  101. if (error < 0 && error != -EOPNOTSUPP)
  102. break;
  103. if (error == 1) {
  104. error = 0;
  105. continue; /* Discard */
  106. }
  107. error = vfs_setxattr(new, name, value, size, 0);
  108. if (error)
  109. break;
  110. }
  111. kfree(value);
  112. out:
  113. kfree(buf);
  114. return error;
  115. }
  116. static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
  117. {
  118. struct file *old_file;
  119. struct file *new_file;
  120. loff_t old_pos = 0;
  121. loff_t new_pos = 0;
  122. int error = 0;
  123. if (len == 0)
  124. return 0;
  125. old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
  126. if (IS_ERR(old_file))
  127. return PTR_ERR(old_file);
  128. new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
  129. if (IS_ERR(new_file)) {
  130. error = PTR_ERR(new_file);
  131. goto out_fput;
  132. }
  133. /* Try to use clone_file_range to clone up within the same fs */
  134. error = vfs_clone_file_range(old_file, 0, new_file, 0, len);
  135. if (!error)
  136. goto out;
  137. /* Couldn't clone, so now we try to copy the data */
  138. error = 0;
  139. /* FIXME: copy up sparse files efficiently */
  140. while (len) {
  141. size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
  142. long bytes;
  143. if (len < this_len)
  144. this_len = len;
  145. if (signal_pending_state(TASK_KILLABLE, current)) {
  146. error = -EINTR;
  147. break;
  148. }
  149. bytes = do_splice_direct(old_file, &old_pos,
  150. new_file, &new_pos,
  151. this_len, SPLICE_F_MOVE);
  152. if (bytes <= 0) {
  153. error = bytes;
  154. break;
  155. }
  156. WARN_ON(old_pos != new_pos);
  157. len -= bytes;
  158. }
  159. out:
  160. if (!error)
  161. error = vfs_fsync(new_file, 0);
  162. fput(new_file);
  163. out_fput:
  164. fput(old_file);
  165. return error;
  166. }
  167. static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
  168. {
  169. struct iattr attr = {
  170. .ia_valid =
  171. ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
  172. .ia_atime = stat->atime,
  173. .ia_mtime = stat->mtime,
  174. };
  175. return notify_change(upperdentry, &attr, NULL);
  176. }
  177. int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
  178. {
  179. int err = 0;
  180. if (!S_ISLNK(stat->mode)) {
  181. struct iattr attr = {
  182. .ia_valid = ATTR_MODE,
  183. .ia_mode = stat->mode,
  184. };
  185. err = notify_change(upperdentry, &attr, NULL);
  186. }
  187. if (!err) {
  188. struct iattr attr = {
  189. .ia_valid = ATTR_UID | ATTR_GID,
  190. .ia_uid = stat->uid,
  191. .ia_gid = stat->gid,
  192. };
  193. err = notify_change(upperdentry, &attr, NULL);
  194. }
  195. if (!err)
  196. ovl_set_timestamps(upperdentry, stat);
  197. return err;
  198. }
  199. static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
  200. struct dentry *dentry, struct path *lowerpath,
  201. struct kstat *stat, const char *link)
  202. {
  203. struct inode *wdir = workdir->d_inode;
  204. struct inode *udir = upperdir->d_inode;
  205. struct dentry *newdentry = NULL;
  206. struct dentry *upper = NULL;
  207. int err;
  208. const struct cred *old_creds = NULL;
  209. struct cred *new_creds = NULL;
  210. struct cattr cattr = {
  211. /* Can't properly set mode on creation because of the umask */
  212. .mode = stat->mode & S_IFMT,
  213. .rdev = stat->rdev,
  214. .link = link
  215. };
  216. newdentry = ovl_lookup_temp(workdir, dentry);
  217. err = PTR_ERR(newdentry);
  218. if (IS_ERR(newdentry))
  219. goto out;
  220. upper = lookup_one_len(dentry->d_name.name, upperdir,
  221. dentry->d_name.len);
  222. err = PTR_ERR(upper);
  223. if (IS_ERR(upper))
  224. goto out1;
  225. err = security_inode_copy_up(dentry, &new_creds);
  226. if (err < 0)
  227. goto out2;
  228. if (new_creds)
  229. old_creds = override_creds(new_creds);
  230. err = ovl_create_real(wdir, newdentry, &cattr, NULL, true);
  231. if (new_creds) {
  232. revert_creds(old_creds);
  233. put_cred(new_creds);
  234. }
  235. if (err)
  236. goto out2;
  237. if (S_ISREG(stat->mode)) {
  238. struct path upperpath;
  239. ovl_path_upper(dentry, &upperpath);
  240. BUG_ON(upperpath.dentry != NULL);
  241. upperpath.dentry = newdentry;
  242. err = ovl_copy_up_data(lowerpath, &upperpath, stat->size);
  243. if (err)
  244. goto out_cleanup;
  245. }
  246. err = ovl_copy_xattr(lowerpath->dentry, newdentry);
  247. if (err)
  248. goto out_cleanup;
  249. inode_lock(newdentry->d_inode);
  250. err = ovl_set_attr(newdentry, stat);
  251. inode_unlock(newdentry->d_inode);
  252. if (err)
  253. goto out_cleanup;
  254. err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
  255. if (err)
  256. goto out_cleanup;
  257. ovl_dentry_update(dentry, newdentry);
  258. ovl_inode_update(d_inode(dentry), d_inode(newdentry));
  259. newdentry = NULL;
  260. out2:
  261. dput(upper);
  262. out1:
  263. dput(newdentry);
  264. out:
  265. return err;
  266. out_cleanup:
  267. ovl_cleanup(wdir, newdentry);
  268. goto out2;
  269. }
  270. /*
  271. * Copy up a single dentry
  272. *
  273. * All renames start with copy up of source if necessary. The actual
  274. * rename will only proceed once the copy up was successful. Copy up uses
  275. * upper parent i_mutex for exclusion. Since rename can change d_parent it
  276. * is possible that the copy up will lock the old parent. At that point
  277. * the file will have already been copied up anyway.
  278. */
  279. static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
  280. struct path *lowerpath, struct kstat *stat)
  281. {
  282. DEFINE_DELAYED_CALL(done);
  283. struct dentry *workdir = ovl_workdir(dentry);
  284. int err;
  285. struct kstat pstat;
  286. struct path parentpath;
  287. struct dentry *lowerdentry = lowerpath->dentry;
  288. struct dentry *upperdir;
  289. const char *link = NULL;
  290. if (WARN_ON(!workdir))
  291. return -EROFS;
  292. ovl_do_check_copy_up(lowerdentry);
  293. ovl_path_upper(parent, &parentpath);
  294. upperdir = parentpath.dentry;
  295. err = vfs_getattr(&parentpath, &pstat,
  296. STATX_ATIME | STATX_MTIME, AT_STATX_SYNC_AS_STAT);
  297. if (err)
  298. return err;
  299. if (S_ISLNK(stat->mode)) {
  300. link = vfs_get_link(lowerdentry, &done);
  301. if (IS_ERR(link))
  302. return PTR_ERR(link);
  303. }
  304. err = -EIO;
  305. if (lock_rename(workdir, upperdir) != NULL) {
  306. pr_err("overlayfs: failed to lock workdir+upperdir\n");
  307. goto out_unlock;
  308. }
  309. if (ovl_dentry_upper(dentry)) {
  310. /* Raced with another copy-up? Nothing to do, then... */
  311. err = 0;
  312. goto out_unlock;
  313. }
  314. err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
  315. stat, link);
  316. if (!err) {
  317. /* Restore timestamps on parent (best effort) */
  318. ovl_set_timestamps(upperdir, &pstat);
  319. }
  320. out_unlock:
  321. unlock_rename(workdir, upperdir);
  322. do_delayed_call(&done);
  323. return err;
  324. }
  325. int ovl_copy_up_flags(struct dentry *dentry, int flags)
  326. {
  327. int err = 0;
  328. const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
  329. while (!err) {
  330. struct dentry *next;
  331. struct dentry *parent;
  332. struct path lowerpath;
  333. struct kstat stat;
  334. enum ovl_path_type type = ovl_path_type(dentry);
  335. if (OVL_TYPE_UPPER(type))
  336. break;
  337. next = dget(dentry);
  338. /* find the topmost dentry not yet copied up */
  339. for (;;) {
  340. parent = dget_parent(next);
  341. type = ovl_path_type(parent);
  342. if (OVL_TYPE_UPPER(type))
  343. break;
  344. dput(next);
  345. next = parent;
  346. }
  347. ovl_path_lower(next, &lowerpath);
  348. err = vfs_getattr(&lowerpath, &stat,
  349. STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
  350. /* maybe truncate regular file. this has no effect on dirs */
  351. if (flags & O_TRUNC)
  352. stat.size = 0;
  353. if (!err)
  354. err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
  355. dput(parent);
  356. dput(next);
  357. }
  358. revert_creds(old_cred);
  359. return err;
  360. }
  361. int ovl_copy_up(struct dentry *dentry)
  362. {
  363. return ovl_copy_up_flags(dentry, 0);
  364. }