copy_up.c 9.3 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. if (!old->d_inode->i_op->getxattr ||
  54. !new->d_inode->i_op->getxattr)
  55. return 0;
  56. list_size = vfs_listxattr(old, NULL, 0);
  57. if (list_size <= 0) {
  58. if (list_size == -EOPNOTSUPP)
  59. return 0;
  60. return list_size;
  61. }
  62. buf = kzalloc(list_size, GFP_KERNEL);
  63. if (!buf)
  64. return -ENOMEM;
  65. list_size = vfs_listxattr(old, buf, list_size);
  66. if (list_size <= 0) {
  67. error = list_size;
  68. goto out;
  69. }
  70. for (name = buf; name < (buf + list_size); name += strlen(name) + 1) {
  71. retry:
  72. size = vfs_getxattr(old, name, value, value_size);
  73. if (size == -ERANGE)
  74. size = vfs_getxattr(old, name, NULL, 0);
  75. if (size < 0) {
  76. error = size;
  77. break;
  78. }
  79. if (size > value_size) {
  80. void *new;
  81. new = krealloc(value, size, GFP_KERNEL);
  82. if (!new) {
  83. error = -ENOMEM;
  84. break;
  85. }
  86. value = new;
  87. value_size = size;
  88. goto retry;
  89. }
  90. error = vfs_setxattr(new, name, value, size, 0);
  91. if (error)
  92. break;
  93. }
  94. kfree(value);
  95. out:
  96. kfree(buf);
  97. return error;
  98. }
  99. static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
  100. {
  101. struct file *old_file;
  102. struct file *new_file;
  103. loff_t old_pos = 0;
  104. loff_t new_pos = 0;
  105. int error = 0;
  106. if (len == 0)
  107. return 0;
  108. old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
  109. if (IS_ERR(old_file))
  110. return PTR_ERR(old_file);
  111. new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
  112. if (IS_ERR(new_file)) {
  113. error = PTR_ERR(new_file);
  114. goto out_fput;
  115. }
  116. /* FIXME: copy up sparse files efficiently */
  117. while (len) {
  118. size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
  119. long bytes;
  120. if (len < this_len)
  121. this_len = len;
  122. if (signal_pending_state(TASK_KILLABLE, current)) {
  123. error = -EINTR;
  124. break;
  125. }
  126. bytes = do_splice_direct(old_file, &old_pos,
  127. new_file, &new_pos,
  128. this_len, SPLICE_F_MOVE);
  129. if (bytes <= 0) {
  130. error = bytes;
  131. break;
  132. }
  133. WARN_ON(old_pos != new_pos);
  134. len -= bytes;
  135. }
  136. fput(new_file);
  137. out_fput:
  138. fput(old_file);
  139. return error;
  140. }
  141. static char *ovl_read_symlink(struct dentry *realdentry)
  142. {
  143. int res;
  144. char *buf;
  145. struct inode *inode = realdentry->d_inode;
  146. mm_segment_t old_fs;
  147. res = -EINVAL;
  148. if (!inode->i_op->readlink)
  149. goto err;
  150. res = -ENOMEM;
  151. buf = (char *) __get_free_page(GFP_KERNEL);
  152. if (!buf)
  153. goto err;
  154. old_fs = get_fs();
  155. set_fs(get_ds());
  156. /* The cast to a user pointer is valid due to the set_fs() */
  157. res = inode->i_op->readlink(realdentry,
  158. (char __user *)buf, PAGE_SIZE - 1);
  159. set_fs(old_fs);
  160. if (res < 0) {
  161. free_page((unsigned long) buf);
  162. goto err;
  163. }
  164. buf[res] = '\0';
  165. return buf;
  166. err:
  167. return ERR_PTR(res);
  168. }
  169. static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
  170. {
  171. struct iattr attr = {
  172. .ia_valid =
  173. ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
  174. .ia_atime = stat->atime,
  175. .ia_mtime = stat->mtime,
  176. };
  177. return notify_change(upperdentry, &attr, NULL);
  178. }
  179. int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
  180. {
  181. int err = 0;
  182. if (!S_ISLNK(stat->mode)) {
  183. struct iattr attr = {
  184. .ia_valid = ATTR_MODE,
  185. .ia_mode = stat->mode,
  186. };
  187. err = notify_change(upperdentry, &attr, NULL);
  188. }
  189. if (!err) {
  190. struct iattr attr = {
  191. .ia_valid = ATTR_UID | ATTR_GID,
  192. .ia_uid = stat->uid,
  193. .ia_gid = stat->gid,
  194. };
  195. err = notify_change(upperdentry, &attr, NULL);
  196. }
  197. if (!err)
  198. ovl_set_timestamps(upperdentry, stat);
  199. return err;
  200. }
  201. static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
  202. struct dentry *dentry, struct path *lowerpath,
  203. struct kstat *stat, const char *link)
  204. {
  205. struct inode *wdir = workdir->d_inode;
  206. struct inode *udir = upperdir->d_inode;
  207. struct dentry *newdentry = NULL;
  208. struct dentry *upper = NULL;
  209. umode_t mode = stat->mode;
  210. int err;
  211. newdentry = ovl_lookup_temp(workdir, dentry);
  212. err = PTR_ERR(newdentry);
  213. if (IS_ERR(newdentry))
  214. goto out;
  215. upper = lookup_one_len(dentry->d_name.name, upperdir,
  216. dentry->d_name.len);
  217. err = PTR_ERR(upper);
  218. if (IS_ERR(upper))
  219. goto out1;
  220. /* Can't properly set mode on creation because of the umask */
  221. stat->mode &= S_IFMT;
  222. err = ovl_create_real(wdir, newdentry, stat, link, NULL, true);
  223. stat->mode = mode;
  224. if (err)
  225. goto out2;
  226. if (S_ISREG(stat->mode)) {
  227. struct path upperpath;
  228. ovl_path_upper(dentry, &upperpath);
  229. BUG_ON(upperpath.dentry != NULL);
  230. upperpath.dentry = newdentry;
  231. err = ovl_copy_up_data(lowerpath, &upperpath, stat->size);
  232. if (err)
  233. goto out_cleanup;
  234. }
  235. err = ovl_copy_xattr(lowerpath->dentry, newdentry);
  236. if (err)
  237. goto out_cleanup;
  238. inode_lock(newdentry->d_inode);
  239. err = ovl_set_attr(newdentry, stat);
  240. inode_unlock(newdentry->d_inode);
  241. if (err)
  242. goto out_cleanup;
  243. err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
  244. if (err)
  245. goto out_cleanup;
  246. ovl_dentry_update(dentry, newdentry);
  247. ovl_inode_update(d_inode(dentry), d_inode(newdentry));
  248. newdentry = NULL;
  249. /*
  250. * Non-directores become opaque when copied up.
  251. */
  252. if (!S_ISDIR(stat->mode))
  253. ovl_dentry_set_opaque(dentry, true);
  254. out2:
  255. dput(upper);
  256. out1:
  257. dput(newdentry);
  258. out:
  259. return err;
  260. out_cleanup:
  261. ovl_cleanup(wdir, newdentry);
  262. goto out2;
  263. }
  264. /*
  265. * Copy up a single dentry
  266. *
  267. * Directory renames only allowed on "pure upper" (already created on
  268. * upper filesystem, never copied up). Directories which are on lower or
  269. * are merged may not be renamed. For these -EXDEV is returned and
  270. * userspace has to deal with it. This means, when copying up a
  271. * directory we can rely on it and ancestors being stable.
  272. *
  273. * Non-directory renames start with copy up of source if necessary. The
  274. * actual rename will only proceed once the copy up was successful. Copy
  275. * up uses upper parent i_mutex for exclusion. Since rename can change
  276. * d_parent it is possible that the copy up will lock the old parent. At
  277. * that point the file will have already been copied up anyway.
  278. */
  279. int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
  280. struct path *lowerpath, struct kstat *stat)
  281. {
  282. struct dentry *workdir = ovl_workdir(dentry);
  283. int err;
  284. struct kstat pstat;
  285. struct path parentpath;
  286. struct dentry *upperdir;
  287. struct dentry *upperdentry;
  288. const struct cred *old_cred;
  289. char *link = NULL;
  290. if (WARN_ON(!workdir))
  291. return -EROFS;
  292. ovl_do_check_copy_up(lowerpath->dentry);
  293. ovl_path_upper(parent, &parentpath);
  294. upperdir = parentpath.dentry;
  295. err = vfs_getattr(&parentpath, &pstat);
  296. if (err)
  297. return err;
  298. if (S_ISLNK(stat->mode)) {
  299. link = ovl_read_symlink(lowerpath->dentry);
  300. if (IS_ERR(link))
  301. return PTR_ERR(link);
  302. }
  303. old_cred = ovl_override_creds(dentry->d_sb);
  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. upperdentry = ovl_dentry_upper(dentry);
  310. if (upperdentry) {
  311. /* Raced with another copy-up? Nothing to do, then... */
  312. err = 0;
  313. goto out_unlock;
  314. }
  315. err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
  316. stat, link);
  317. if (!err) {
  318. /* Restore timestamps on parent (best effort) */
  319. ovl_set_timestamps(upperdir, &pstat);
  320. }
  321. out_unlock:
  322. unlock_rename(workdir, upperdir);
  323. revert_creds(old_cred);
  324. if (link)
  325. free_page((unsigned long) link);
  326. return err;
  327. }
  328. int ovl_copy_up(struct dentry *dentry)
  329. {
  330. int err;
  331. err = 0;
  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. return err;
  358. }