GNU Linux-libre 4.9.318-gnu1
[releases.git] / fs / overlayfs / copy_up.c
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
10 #include <linux/module.h>
11 #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>
20 #include <linux/fdtable.h>
21 #include <linux/ratelimit.h>
22 #include "overlayfs.h"
23
24 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
25
26 static bool __read_mostly ovl_check_copy_up;
27 module_param_named(check_copy_up, ovl_check_copy_up, bool,
28                    S_IWUSR | S_IRUGO);
29 MODULE_PARM_DESC(ovl_check_copy_up,
30                  "Warn on copy-up when causing process also has a R/O fd open");
31
32 static 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  */
49 static 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
55 int ovl_copy_xattr(struct dentry *old, struct dentry *new)
56 {
57         ssize_t list_size, size, value_size = 0;
58         char *buf, *name, *value = NULL;
59         int error = 0;
60         size_t slen;
61
62         if (!(old->d_inode->i_opflags & IOP_XATTR) ||
63             !(new->d_inode->i_opflags & IOP_XATTR))
64                 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
77         list_size = vfs_listxattr(old, buf, list_size);
78         if (list_size <= 0) {
79                 error = list_size;
80                 goto out;
81         }
82
83         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
93                 if (ovl_is_private_xattr(name))
94                         continue;
95
96                 error = security_inode_copy_up_xattr(name);
97                 if (error < 0 && error != -EOPNOTSUPP)
98                         break;
99                 if (error == 1) {
100                         error = 0;
101                         continue; /* Discard */
102                 }
103 retry:
104                 size = vfs_getxattr(old, name, value, value_size);
105                 if (size == -ERANGE)
106                         size = vfs_getxattr(old, name, NULL, 0);
107
108                 if (size < 0) {
109                         error = size;
110                         break;
111                 }
112
113                 if (size > value_size) {
114                         void *new;
115
116                         new = krealloc(value, size, GFP_KERNEL);
117                         if (!new) {
118                                 error = -ENOMEM;
119                                 break;
120                         }
121                         value = new;
122                         value_size = size;
123                         goto retry;
124                 }
125
126                 error = vfs_setxattr(new, name, value, size, 0);
127                 if (error)
128                         break;
129         }
130         kfree(value);
131 out:
132         kfree(buf);
133         return error;
134 }
135
136 static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
137 {
138         struct file *old_file;
139         struct file *new_file;
140         loff_t old_pos = 0;
141         loff_t new_pos = 0;
142         int error = 0;
143
144         if (len == 0)
145                 return 0;
146
147         old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
148         if (IS_ERR(old_file))
149                 return PTR_ERR(old_file);
150
151         new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
152         if (IS_ERR(new_file)) {
153                 error = PTR_ERR(new_file);
154                 goto out_fput;
155         }
156
157         /* FIXME: copy up sparse files efficiently */
158         while (len) {
159                 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
160                 long bytes;
161
162                 if (len < this_len)
163                         this_len = len;
164
165                 if (signal_pending_state(TASK_KILLABLE, current)) {
166                         error = -EINTR;
167                         break;
168                 }
169
170                 bytes = do_splice_direct(old_file, &old_pos,
171                                          new_file, &new_pos,
172                                          this_len, SPLICE_F_MOVE);
173                 if (bytes <= 0) {
174                         error = bytes;
175                         break;
176                 }
177                 WARN_ON(old_pos != new_pos);
178
179                 len -= bytes;
180         }
181
182         if (!error)
183                 error = vfs_fsync(new_file, 0);
184         fput(new_file);
185 out_fput:
186         fput(old_file);
187         return error;
188 }
189
190 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
191 {
192         struct iattr attr = {
193                 .ia_valid =
194                      ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
195                 .ia_atime = stat->atime,
196                 .ia_mtime = stat->mtime,
197         };
198
199         return notify_change(upperdentry, &attr, NULL);
200 }
201
202 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
203 {
204         int err = 0;
205
206         if (!S_ISLNK(stat->mode)) {
207                 struct iattr attr = {
208                         .ia_valid = ATTR_MODE,
209                         .ia_mode = stat->mode,
210                 };
211                 err = notify_change(upperdentry, &attr, NULL);
212         }
213         if (!err) {
214                 struct iattr attr = {
215                         .ia_valid = ATTR_UID | ATTR_GID,
216                         .ia_uid = stat->uid,
217                         .ia_gid = stat->gid,
218                 };
219                 err = notify_change(upperdentry, &attr, NULL);
220         }
221         if (!err)
222                 ovl_set_timestamps(upperdentry, stat);
223
224         return err;
225 }
226
227 static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
228                               struct dentry *dentry, struct path *lowerpath,
229                               struct kstat *stat, const char *link)
230 {
231         struct inode *wdir = workdir->d_inode;
232         struct inode *udir = upperdir->d_inode;
233         struct dentry *newdentry = NULL;
234         struct dentry *upper = NULL;
235         umode_t mode = stat->mode;
236         int err;
237         const struct cred *old_creds = NULL;
238         struct cred *new_creds = NULL;
239
240         newdentry = ovl_lookup_temp(workdir, dentry);
241         err = PTR_ERR(newdentry);
242         if (IS_ERR(newdentry))
243                 goto out;
244
245         upper = lookup_one_len(dentry->d_name.name, upperdir,
246                                dentry->d_name.len);
247         err = PTR_ERR(upper);
248         if (IS_ERR(upper))
249                 goto out1;
250
251         err = security_inode_copy_up(dentry, &new_creds);
252         if (err < 0)
253                 goto out2;
254
255         if (new_creds)
256                 old_creds = override_creds(new_creds);
257
258         /* Can't properly set mode on creation because of the umask */
259         stat->mode &= S_IFMT;
260         err = ovl_create_real(wdir, newdentry, stat, link, NULL, true);
261         stat->mode = mode;
262
263         if (new_creds) {
264                 revert_creds(old_creds);
265                 put_cred(new_creds);
266         }
267
268         if (err)
269                 goto out2;
270
271         if (S_ISREG(stat->mode)) {
272                 struct path upperpath;
273
274                 ovl_path_upper(dentry, &upperpath);
275                 BUG_ON(upperpath.dentry != NULL);
276                 upperpath.dentry = newdentry;
277
278                 err = ovl_copy_up_data(lowerpath, &upperpath, stat->size);
279                 if (err)
280                         goto out_cleanup;
281         }
282
283         err = ovl_copy_xattr(lowerpath->dentry, newdentry);
284         if (err)
285                 goto out_cleanup;
286
287         inode_lock(newdentry->d_inode);
288         err = ovl_set_attr(newdentry, stat);
289         inode_unlock(newdentry->d_inode);
290         if (err)
291                 goto out_cleanup;
292
293         err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
294         if (err)
295                 goto out_cleanup;
296
297         ovl_dentry_update(dentry, newdentry);
298         ovl_inode_update(d_inode(dentry), d_inode(newdentry));
299         newdentry = NULL;
300
301         /*
302          * Non-directores become opaque when copied up.
303          */
304         if (!S_ISDIR(stat->mode))
305                 ovl_dentry_set_opaque(dentry, true);
306 out2:
307         dput(upper);
308 out1:
309         dput(newdentry);
310 out:
311         return err;
312
313 out_cleanup:
314         ovl_cleanup(wdir, newdentry);
315         goto out2;
316 }
317
318 /*
319  * Copy up a single dentry
320  *
321  * Directory renames only allowed on "pure upper" (already created on
322  * upper filesystem, never copied up).  Directories which are on lower or
323  * are merged may not be renamed.  For these -EXDEV is returned and
324  * userspace has to deal with it.  This means, when copying up a
325  * directory we can rely on it and ancestors being stable.
326  *
327  * Non-directory renames start with copy up of source if necessary.  The
328  * actual rename will only proceed once the copy up was successful.  Copy
329  * up uses upper parent i_mutex for exclusion.  Since rename can change
330  * d_parent it is possible that the copy up will lock the old parent.  At
331  * that point the file will have already been copied up anyway.
332  */
333 int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
334                     struct path *lowerpath, struct kstat *stat)
335 {
336         DEFINE_DELAYED_CALL(done);
337         struct dentry *workdir = ovl_workdir(dentry);
338         int err;
339         struct kstat pstat;
340         struct path parentpath;
341         struct dentry *lowerdentry = lowerpath->dentry;
342         struct dentry *upperdir;
343         struct dentry *upperdentry;
344         const char *link = NULL;
345
346         if (WARN_ON(!workdir))
347                 return -EROFS;
348
349         ovl_do_check_copy_up(lowerdentry);
350
351         ovl_path_upper(parent, &parentpath);
352         upperdir = parentpath.dentry;
353
354         err = vfs_getattr(&parentpath, &pstat);
355         if (err)
356                 return err;
357
358         if (S_ISLNK(stat->mode)) {
359                 link = vfs_get_link(lowerdentry, &done);
360                 if (IS_ERR(link))
361                         return PTR_ERR(link);
362         }
363
364         err = -EIO;
365         if (lock_rename(workdir, upperdir) != NULL) {
366                 pr_err("overlayfs: failed to lock workdir+upperdir\n");
367                 goto out_unlock;
368         }
369         upperdentry = ovl_dentry_upper(dentry);
370         if (upperdentry) {
371                 /* Raced with another copy-up?  Nothing to do, then... */
372                 err = 0;
373                 goto out_unlock;
374         }
375
376         err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
377                                  stat, link);
378         if (!err) {
379                 /* Restore timestamps on parent (best effort) */
380                 ovl_set_timestamps(upperdir, &pstat);
381         }
382 out_unlock:
383         unlock_rename(workdir, upperdir);
384         do_delayed_call(&done);
385
386         return err;
387 }
388
389 int ovl_copy_up(struct dentry *dentry)
390 {
391         int err = 0;
392         const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
393
394         while (!err) {
395                 struct dentry *next;
396                 struct dentry *parent;
397                 struct path lowerpath;
398                 struct kstat stat;
399                 enum ovl_path_type type = ovl_path_type(dentry);
400
401                 if (OVL_TYPE_UPPER(type))
402                         break;
403
404                 next = dget(dentry);
405                 /* find the topmost dentry not yet copied up */
406                 for (;;) {
407                         parent = dget_parent(next);
408
409                         type = ovl_path_type(parent);
410                         if (OVL_TYPE_UPPER(type))
411                                 break;
412
413                         dput(next);
414                         next = parent;
415                 }
416
417                 ovl_path_lower(next, &lowerpath);
418                 err = vfs_getattr(&lowerpath, &stat);
419                 if (!err)
420                         err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
421
422                 dput(parent);
423                 dput(next);
424         }
425         revert_creds(old_cred);
426
427         return err;
428 }