blob: f18c1a616e9e3ce729c09fff28501fd5146dca29 [file] [log] [blame]
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001/*
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
David Howellsfb5bb2c32015-07-07 15:04:44 +010010#include <linux/module.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020011#include <linux/fs.h>
12#include <linux/slab.h>
13#include <linux/file.h>
14#include <linux/splice.h>
15#include <linux/xattr.h>
16#include <linux/security.h>
17#include <linux/uaccess.h>
18#include <linux/sched.h>
19#include <linux/namei.h>
David Howellsfb5bb2c32015-07-07 15:04:44 +010020#include <linux/fdtable.h>
21#include <linux/ratelimit.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020022#include "overlayfs.h"
23
24#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
25
David Howellsfb5bb2c32015-07-07 15:04:44 +010026static bool __read_mostly ovl_check_copy_up;
27module_param_named(check_copy_up, ovl_check_copy_up, bool,
28 S_IWUSR | S_IRUGO);
29MODULE_PARM_DESC(ovl_check_copy_up,
30 "Warn on copy-up when causing process also has a R/O fd open");
31
32static int ovl_check_fd(const void *data, struct file *f, unsigned int fd)
33{
34 const struct dentry *dentry = data;
35
36 if (f->f_inode == d_inode(dentry))
37 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",
38 f, fd, current->pid, current->comm);
39 return 0;
40}
41
42/*
43 * Check the fds open by this process and warn if something like the following
44 * scenario is about to occur:
45 *
46 * fd1 = open("foo", O_RDONLY);
47 * fd2 = open("foo", O_RDWR);
48 */
49static void ovl_do_check_copy_up(struct dentry *dentry)
50{
51 if (ovl_check_copy_up)
52 iterate_fd(current->files, 0, ovl_check_fd, dentry);
53}
54
Miklos Szeredie9be9d52014-10-24 00:14:38 +020055int ovl_copy_xattr(struct dentry *old, struct dentry *new)
56{
Vito Caputoe4ad29f2015-10-24 07:19:46 -050057 ssize_t list_size, size, value_size = 0;
58 char *buf, *name, *value = NULL;
59 int uninitialized_var(error);
Miklos Szeredi8b326c62016-09-16 14:12:11 +020060 size_t slen;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020061
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +020062 if (!(old->d_inode->i_opflags & IOP_XATTR) ||
63 !(new->d_inode->i_opflags & IOP_XATTR))
Miklos Szeredie9be9d52014-10-24 00:14:38 +020064 return 0;
65
66 list_size = vfs_listxattr(old, NULL, 0);
67 if (list_size <= 0) {
68 if (list_size == -EOPNOTSUPP)
69 return 0;
70 return list_size;
71 }
72
73 buf = kzalloc(list_size, GFP_KERNEL);
74 if (!buf)
75 return -ENOMEM;
76
Miklos Szeredie9be9d52014-10-24 00:14:38 +020077 list_size = vfs_listxattr(old, buf, list_size);
78 if (list_size <= 0) {
79 error = list_size;
Vito Caputoe4ad29f2015-10-24 07:19:46 -050080 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020081 }
82
Miklos Szeredi8b326c62016-09-16 14:12:11 +020083 for (name = buf; list_size; name += slen) {
84 slen = strnlen(name, list_size) + 1;
85
86 /* underlying fs providing us with an broken xattr list? */
87 if (WARN_ON(slen > list_size)) {
88 error = -EIO;
89 break;
90 }
91 list_size -= slen;
92
Miklos Szeredi09562542016-08-08 15:08:49 +020093 if (ovl_is_private_xattr(name))
94 continue;
Vito Caputoe4ad29f2015-10-24 07:19:46 -050095retry:
96 size = vfs_getxattr(old, name, value, value_size);
97 if (size == -ERANGE)
98 size = vfs_getxattr(old, name, NULL, 0);
99
Miklos Szeredi97daf8b2015-11-10 17:08:41 +0100100 if (size < 0) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200101 error = size;
Vito Caputoe4ad29f2015-10-24 07:19:46 -0500102 break;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200103 }
Vito Caputoe4ad29f2015-10-24 07:19:46 -0500104
105 if (size > value_size) {
106 void *new;
107
108 new = krealloc(value, size, GFP_KERNEL);
109 if (!new) {
110 error = -ENOMEM;
111 break;
112 }
113 value = new;
114 value_size = size;
115 goto retry;
116 }
117
Vivek Goyal121ab822016-07-13 10:44:49 -0400118 error = security_inode_copy_up_xattr(name);
119 if (error < 0 && error != -EOPNOTSUPP)
120 break;
121 if (error == 1) {
122 error = 0;
123 continue; /* Discard */
124 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200125 error = vfs_setxattr(new, name, value, size, 0);
126 if (error)
Vito Caputoe4ad29f2015-10-24 07:19:46 -0500127 break;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200128 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200129 kfree(value);
130out:
131 kfree(buf);
132 return error;
133}
134
135static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
136{
137 struct file *old_file;
138 struct file *new_file;
139 loff_t old_pos = 0;
140 loff_t new_pos = 0;
141 int error = 0;
142
143 if (len == 0)
144 return 0;
145
David Howells04803342015-09-18 11:45:12 +0100146 old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200147 if (IS_ERR(old_file))
148 return PTR_ERR(old_file);
149
David Howells04803342015-09-18 11:45:12 +0100150 new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200151 if (IS_ERR(new_file)) {
152 error = PTR_ERR(new_file);
153 goto out_fput;
154 }
155
Amir Goldstein2ea98462016-09-23 11:38:12 +0300156 /* Try to use clone_file_range to clone up within the same fs */
157 error = vfs_clone_file_range(old_file, 0, new_file, 0, len);
158 if (!error)
159 goto out;
160 /* Couldn't clone, so now we try to copy the data */
161 error = 0;
162
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200163 /* FIXME: copy up sparse files efficiently */
164 while (len) {
165 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
166 long bytes;
167
168 if (len < this_len)
169 this_len = len;
170
171 if (signal_pending_state(TASK_KILLABLE, current)) {
172 error = -EINTR;
173 break;
174 }
175
176 bytes = do_splice_direct(old_file, &old_pos,
177 new_file, &new_pos,
178 this_len, SPLICE_F_MOVE);
179 if (bytes <= 0) {
180 error = bytes;
181 break;
182 }
183 WARN_ON(old_pos != new_pos);
184
185 len -= bytes;
186 }
Amir Goldstein2ea98462016-09-23 11:38:12 +0300187out:
Miklos Szeredi641089c2016-10-31 14:42:14 +0100188 if (!error)
189 error = vfs_fsync(new_file, 0);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200190 fput(new_file);
191out_fput:
192 fput(old_file);
193 return error;
194}
195
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200196static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
197{
198 struct iattr attr = {
199 .ia_valid =
200 ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
201 .ia_atime = stat->atime,
202 .ia_mtime = stat->mtime,
203 };
204
205 return notify_change(upperdentry, &attr, NULL);
206}
207
208int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
209{
210 int err = 0;
211
212 if (!S_ISLNK(stat->mode)) {
213 struct iattr attr = {
214 .ia_valid = ATTR_MODE,
215 .ia_mode = stat->mode,
216 };
217 err = notify_change(upperdentry, &attr, NULL);
218 }
219 if (!err) {
220 struct iattr attr = {
221 .ia_valid = ATTR_UID | ATTR_GID,
222 .ia_uid = stat->uid,
223 .ia_gid = stat->gid,
224 };
225 err = notify_change(upperdentry, &attr, NULL);
226 }
227 if (!err)
228 ovl_set_timestamps(upperdentry, stat);
229
230 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200231}
232
233static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
234 struct dentry *dentry, struct path *lowerpath,
Al Viro0f7ff2d2015-12-06 12:31:07 -0500235 struct kstat *stat, const char *link)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200236{
237 struct inode *wdir = workdir->d_inode;
238 struct inode *udir = upperdir->d_inode;
239 struct dentry *newdentry = NULL;
240 struct dentry *upper = NULL;
241 umode_t mode = stat->mode;
242 int err;
Vivek Goyald8ad8b42016-07-13 11:13:56 -0400243 const struct cred *old_creds = NULL;
244 struct cred *new_creds = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200245
246 newdentry = ovl_lookup_temp(workdir, dentry);
247 err = PTR_ERR(newdentry);
248 if (IS_ERR(newdentry))
249 goto out;
250
251 upper = lookup_one_len(dentry->d_name.name, upperdir,
252 dentry->d_name.len);
253 err = PTR_ERR(upper);
254 if (IS_ERR(upper))
255 goto out1;
256
Vivek Goyald8ad8b42016-07-13 11:13:56 -0400257 err = security_inode_copy_up(dentry, &new_creds);
258 if (err < 0)
259 goto out2;
260
261 if (new_creds)
262 old_creds = override_creds(new_creds);
263
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200264 /* Can't properly set mode on creation because of the umask */
265 stat->mode &= S_IFMT;
266 err = ovl_create_real(wdir, newdentry, stat, link, NULL, true);
267 stat->mode = mode;
Vivek Goyald8ad8b42016-07-13 11:13:56 -0400268
269 if (new_creds) {
270 revert_creds(old_creds);
271 put_cred(new_creds);
272 }
273
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200274 if (err)
275 goto out2;
276
277 if (S_ISREG(stat->mode)) {
278 struct path upperpath;
Sohom Bhattacharjeef134f242016-03-15 20:57:59 +0530279
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200280 ovl_path_upper(dentry, &upperpath);
281 BUG_ON(upperpath.dentry != NULL);
282 upperpath.dentry = newdentry;
283
284 err = ovl_copy_up_data(lowerpath, &upperpath, stat->size);
285 if (err)
286 goto out_cleanup;
287 }
288
289 err = ovl_copy_xattr(lowerpath->dentry, newdentry);
290 if (err)
291 goto out_cleanup;
292
Al Viro59551022016-01-22 15:40:57 -0500293 inode_lock(newdentry->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200294 err = ovl_set_attr(newdentry, stat);
Al Viro59551022016-01-22 15:40:57 -0500295 inode_unlock(newdentry->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200296 if (err)
297 goto out_cleanup;
298
299 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
300 if (err)
301 goto out_cleanup;
302
303 ovl_dentry_update(dentry, newdentry);
Miklos Szeredi39b681f2016-07-29 12:05:24 +0200304 ovl_inode_update(d_inode(dentry), d_inode(newdentry));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200305 newdentry = NULL;
306
307 /*
308 * Non-directores become opaque when copied up.
309 */
310 if (!S_ISDIR(stat->mode))
311 ovl_dentry_set_opaque(dentry, true);
312out2:
313 dput(upper);
314out1:
315 dput(newdentry);
316out:
317 return err;
318
319out_cleanup:
320 ovl_cleanup(wdir, newdentry);
David Howellsab79efa2015-09-18 11:45:22 +0100321 goto out2;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200322}
323
324/*
325 * Copy up a single dentry
326 *
327 * Directory renames only allowed on "pure upper" (already created on
328 * upper filesystem, never copied up). Directories which are on lower or
329 * are merged may not be renamed. For these -EXDEV is returned and
330 * userspace has to deal with it. This means, when copying up a
331 * directory we can rely on it and ancestors being stable.
332 *
333 * Non-directory renames start with copy up of source if necessary. The
334 * actual rename will only proceed once the copy up was successful. Copy
335 * up uses upper parent i_mutex for exclusion. Since rename can change
336 * d_parent it is possible that the copy up will lock the old parent. At
337 * that point the file will have already been copied up anyway.
338 */
339int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
Al Viro0f7ff2d2015-12-06 12:31:07 -0500340 struct path *lowerpath, struct kstat *stat)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200341{
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200342 DEFINE_DELAYED_CALL(done);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200343 struct dentry *workdir = ovl_workdir(dentry);
344 int err;
345 struct kstat pstat;
346 struct path parentpath;
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200347 struct dentry *lowerdentry = lowerpath->dentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200348 struct dentry *upperdir;
349 struct dentry *upperdentry;
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200350 const char *link = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200351
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200352 if (WARN_ON(!workdir))
353 return -EROFS;
354
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200355 ovl_do_check_copy_up(lowerdentry);
David Howellsfb5bb2c32015-07-07 15:04:44 +0100356
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200357 ovl_path_upper(parent, &parentpath);
358 upperdir = parentpath.dentry;
359
360 err = vfs_getattr(&parentpath, &pstat);
361 if (err)
362 return err;
363
364 if (S_ISLNK(stat->mode)) {
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200365 link = vfs_get_link(lowerdentry, &done);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200366 if (IS_ERR(link))
367 return PTR_ERR(link);
368 }
369
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200370 err = -EIO;
371 if (lock_rename(workdir, upperdir) != NULL) {
372 pr_err("overlayfs: failed to lock workdir+upperdir\n");
373 goto out_unlock;
374 }
375 upperdentry = ovl_dentry_upper(dentry);
376 if (upperdentry) {
Al Viro0f7ff2d2015-12-06 12:31:07 -0500377 /* Raced with another copy-up? Nothing to do, then... */
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200378 err = 0;
Al Viro0f7ff2d2015-12-06 12:31:07 -0500379 goto out_unlock;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200380 }
381
382 err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
Al Viro0f7ff2d2015-12-06 12:31:07 -0500383 stat, link);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200384 if (!err) {
385 /* Restore timestamps on parent (best effort) */
386 ovl_set_timestamps(upperdir, &pstat);
387 }
388out_unlock:
389 unlock_rename(workdir, upperdir);
Miklos Szeredi7764235b2016-10-04 14:40:45 +0200390 do_delayed_call(&done);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200391
392 return err;
393}
394
395int ovl_copy_up(struct dentry *dentry)
396{
Vivek Goyal8eac98b2016-09-06 13:40:32 -0400397 int err = 0;
398 const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200399
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200400 while (!err) {
401 struct dentry *next;
402 struct dentry *parent;
403 struct path lowerpath;
404 struct kstat stat;
405 enum ovl_path_type type = ovl_path_type(dentry);
406
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100407 if (OVL_TYPE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200408 break;
409
410 next = dget(dentry);
411 /* find the topmost dentry not yet copied up */
412 for (;;) {
413 parent = dget_parent(next);
414
415 type = ovl_path_type(parent);
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100416 if (OVL_TYPE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200417 break;
418
419 dput(next);
420 next = parent;
421 }
422
423 ovl_path_lower(next, &lowerpath);
424 err = vfs_getattr(&lowerpath, &stat);
425 if (!err)
Al Viro0f7ff2d2015-12-06 12:31:07 -0500426 err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200427
428 dput(parent);
429 dput(next);
430 }
Vivek Goyal8eac98b2016-09-06 13:40:32 -0400431 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200432
433 return err;
434}