copy_up.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 (f->f_inode == 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. /* FIXME: copy up sparse files efficiently */
  134. while (len) {
  135. size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
  136. long bytes;
  137. if (len < this_len)
  138. this_len = len;
  139. if (signal_pending_state(TASK_KILLABLE, current)) {
  140. error = -EINTR;
  141. break;
  142. }
  143. bytes = do_splice_direct(old_file, &old_pos,
  144. new_file, &new_pos,
  145. this_len, SPLICE_F_MOVE);
  146. if (bytes <= 0) {
  147. error = bytes;
  148. break;
  149. }
  150. WARN_ON(old_pos != new_pos);
  151. len -= bytes;
  152. }
  153. if (!error)
  154. error = vfs_fsync(new_file, 0);
  155. fput(new_file);
  156. out_fput:
  157. fput(old_file);
  158. return error;
  159. }
  160. static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
  161. {
  162. struct iattr attr = {
  163. .ia_valid =
  164. ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
  165. .ia_atime = stat->atime,
  166. .ia_mtime = stat->mtime,
  167. };
  168. return notify_change(upperdentry, &attr, NULL);
  169. }
  170. int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
  171. {
  172. int err = 0;
  173. if (!S_ISLNK(stat->mode)) {
  174. struct iattr attr = {
  175. .ia_valid = ATTR_MODE,
  176. .ia_mode = stat->mode,
  177. };
  178. err = notify_change(upperdentry, &attr, NULL);
  179. }
  180. if (!err) {
  181. struct iattr attr = {
  182. .ia_valid = ATTR_UID | ATTR_GID,
  183. .ia_uid = stat->uid,
  184. .ia_gid = stat->gid,
  185. };
  186. err = notify_change(upperdentry, &attr, NULL);
  187. }
  188. if (!err)
  189. ovl_set_timestamps(upperdentry, stat);
  190. return err;
  191. }
  192. static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
  193. struct dentry *dentry, struct path *lowerpath,
  194. struct kstat *stat, const char *link)
  195. {
  196. struct inode *wdir = workdir->d_inode;
  197. struct inode *udir = upperdir->d_inode;
  198. struct dentry *newdentry = NULL;
  199. struct dentry *upper = NULL;
  200. umode_t mode = stat->mode;
  201. int err;
  202. const struct cred *old_creds = NULL;
  203. struct cred *new_creds = NULL;
  204. newdentry = ovl_lookup_temp(workdir, dentry);
  205. err = PTR_ERR(newdentry);
  206. if (IS_ERR(newdentry))
  207. goto out;
  208. upper = lookup_one_len(dentry->d_name.name, upperdir,
  209. dentry->d_name.len);
  210. err = PTR_ERR(upper);
  211. if (IS_ERR(upper))
  212. goto out1;
  213. err = security_inode_copy_up(dentry, &new_creds);
  214. if (err < 0)
  215. goto out2;
  216. if (new_creds)
  217. old_creds = override_creds(new_creds);
  218. /* Can't properly set mode on creation because of the umask */
  219. stat->mode &= S_IFMT;
  220. err = ovl_create_real(wdir, newdentry, stat, link, NULL, true);
  221. stat->mode = mode;
  222. if (new_creds) {
  223. revert_creds(old_creds);
  224. put_cred(new_creds);
  225. }
  226. if (err)
  227. goto out2;
  228. if (S_ISREG(stat->mode)) {
  229. struct path upperpath;
  230. ovl_path_upper(dentry, &upperpath);
  231. BUG_ON(upperpath.dentry != NULL);
  232. upperpath.dentry = newdentry;
  233. err = ovl_copy_up_data(lowerpath, &upperpath, stat->size);
  234. if (err)
  235. goto out_cleanup;
  236. }
  237. err = ovl_copy_xattr(lowerpath->dentry, newdentry);
  238. if (err)
  239. goto out_cleanup;
  240. inode_lock(newdentry->d_inode);
  241. err = ovl_set_attr(newdentry, stat);
  242. inode_unlock(newdentry->d_inode);
  243. if (err)
  244. goto out_cleanup;
  245. err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
  246. if (err)
  247. goto out_cleanup;
  248. ovl_dentry_update(dentry, newdentry);
  249. ovl_inode_update(d_inode(dentry), d_inode(newdentry));
  250. newdentry = NULL;
  251. /*
  252. * Non-directores become opaque when copied up.
  253. */
  254. if (!S_ISDIR(stat->mode))
  255. ovl_dentry_set_opaque(dentry, true);
  256. out2:
  257. dput(upper);
  258. out1:
  259. dput(newdentry);
  260. out:
  261. return err;
  262. out_cleanup:
  263. ovl_cleanup(wdir, newdentry);
  264. goto out2;
  265. }
  266. /*
  267. * Copy up a single dentry
  268. *
  269. * Directory renames only allowed on "pure upper" (already created on
  270. * upper filesystem, never copied up). Directories which are on lower or
  271. * are merged may not be renamed. For these -EXDEV is returned and
  272. * userspace has to deal with it. This means, when copying up a
  273. * directory we can rely on it and ancestors being stable.
  274. *
  275. * Non-directory renames start with copy up of source if necessary. The
  276. * actual rename will only proceed once the copy up was successful. Copy
  277. * up uses upper parent i_mutex for exclusion. Since rename can change
  278. * d_parent it is possible that the copy up will lock the old parent. At
  279. * that point the file will have already been copied up anyway.
  280. */
  281. int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
  282. struct path *lowerpath, struct kstat *stat)
  283. {
  284. DEFINE_DELAYED_CALL(done);
  285. struct dentry *workdir = ovl_workdir(dentry);
  286. int err;
  287. struct kstat pstat;
  288. struct path parentpath;
  289. struct dentry *lowerdentry = lowerpath->dentry;
  290. struct dentry *upperdir;
  291. struct dentry *upperdentry;
  292. const char *link = NULL;
  293. if (WARN_ON(!workdir))
  294. return -EROFS;
  295. ovl_do_check_copy_up(lowerdentry);
  296. ovl_path_upper(parent, &parentpath);
  297. upperdir = parentpath.dentry;
  298. err = vfs_getattr(&parentpath, &pstat);
  299. if (err)
  300. return err;
  301. if (S_ISLNK(stat->mode)) {
  302. link = vfs_get_link(lowerdentry, &done);
  303. if (IS_ERR(link))
  304. return PTR_ERR(link);
  305. }
  306. err = -EIO;
  307. if (lock_rename(workdir, upperdir) != NULL) {
  308. pr_err("overlayfs: failed to lock workdir+upperdir\n");
  309. goto out_unlock;
  310. }
  311. upperdentry = ovl_dentry_upper(dentry);
  312. if (upperdentry) {
  313. /* Raced with another copy-up? Nothing to do, then... */
  314. err = 0;
  315. goto out_unlock;
  316. }
  317. err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
  318. stat, link);
  319. if (!err) {
  320. /* Restore timestamps on parent (best effort) */
  321. ovl_set_timestamps(upperdir, &pstat);
  322. }
  323. out_unlock:
  324. unlock_rename(workdir, upperdir);
  325. do_delayed_call(&done);
  326. return err;
  327. }
  328. int ovl_copy_up(struct dentry *dentry)
  329. {
  330. int err = 0;
  331. const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
  332. while (!err) {
  333. struct dentry *next;
  334. struct dentry *parent;
  335. struct path lowerpath;
  336. struct kstat stat;
  337. enum ovl_path_type type = ovl_path_type(dentry);
  338. if (OVL_TYPE_UPPER(type))
  339. break;
  340. next = dget(dentry);
  341. /* find the topmost dentry not yet copied up */
  342. for (;;) {
  343. parent = dget_parent(next);
  344. type = ovl_path_type(parent);
  345. if (OVL_TYPE_UPPER(type))
  346. break;
  347. dput(next);
  348. next = parent;
  349. }
  350. ovl_path_lower(next, &lowerpath);
  351. err = vfs_getattr(&lowerpath, &stat);
  352. if (!err)
  353. err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
  354. dput(parent);
  355. dput(next);
  356. }
  357. revert_creds(old_cred);
  358. return err;
  359. }