NFS: Don't drop CB requests with invalid principals
[firefly-linux-kernel-4.4.55.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/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 "overlayfs.h"
20
21 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
22
23 int ovl_copy_xattr(struct dentry *old, struct dentry *new)
24 {
25         ssize_t list_size, size, value_size = 0;
26         char *buf, *name, *value = NULL;
27         int uninitialized_var(error);
28
29         if (!old->d_inode->i_op->getxattr ||
30             !new->d_inode->i_op->getxattr)
31                 return 0;
32
33         list_size = vfs_listxattr(old, NULL, 0);
34         if (list_size <= 0) {
35                 if (list_size == -EOPNOTSUPP)
36                         return 0;
37                 return list_size;
38         }
39
40         buf = kzalloc(list_size, GFP_KERNEL);
41         if (!buf)
42                 return -ENOMEM;
43
44         list_size = vfs_listxattr(old, buf, list_size);
45         if (list_size <= 0) {
46                 error = list_size;
47                 goto out;
48         }
49
50         for (name = buf; name < (buf + list_size); name += strlen(name) + 1) {
51                 if (ovl_is_private_xattr(name))
52                         continue;
53 retry:
54                 size = vfs_getxattr(old, name, value, value_size);
55                 if (size == -ERANGE)
56                         size = vfs_getxattr(old, name, NULL, 0);
57
58                 if (size < 0) {
59                         error = size;
60                         break;
61                 }
62
63                 if (size > value_size) {
64                         void *new;
65
66                         new = krealloc(value, size, GFP_KERNEL);
67                         if (!new) {
68                                 error = -ENOMEM;
69                                 break;
70                         }
71                         value = new;
72                         value_size = size;
73                         goto retry;
74                 }
75
76                 error = vfs_setxattr(new, name, value, size, 0);
77                 if (error)
78                         break;
79         }
80         kfree(value);
81 out:
82         kfree(buf);
83         return error;
84 }
85
86 static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
87 {
88         struct file *old_file;
89         struct file *new_file;
90         loff_t old_pos = 0;
91         loff_t new_pos = 0;
92         int error = 0;
93
94         if (len == 0)
95                 return 0;
96
97         old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
98         if (IS_ERR(old_file))
99                 return PTR_ERR(old_file);
100
101         new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
102         if (IS_ERR(new_file)) {
103                 error = PTR_ERR(new_file);
104                 goto out_fput;
105         }
106
107         /* FIXME: copy up sparse files efficiently */
108         while (len) {
109                 size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
110                 long bytes;
111
112                 if (len < this_len)
113                         this_len = len;
114
115                 if (signal_pending_state(TASK_KILLABLE, current)) {
116                         error = -EINTR;
117                         break;
118                 }
119
120                 bytes = do_splice_direct(old_file, &old_pos,
121                                          new_file, &new_pos,
122                                          this_len, SPLICE_F_MOVE);
123                 if (bytes <= 0) {
124                         error = bytes;
125                         break;
126                 }
127                 WARN_ON(old_pos != new_pos);
128
129                 len -= bytes;
130         }
131
132         fput(new_file);
133 out_fput:
134         fput(old_file);
135         return error;
136 }
137
138 static char *ovl_read_symlink(struct dentry *realdentry)
139 {
140         int res;
141         char *buf;
142         struct inode *inode = realdentry->d_inode;
143         mm_segment_t old_fs;
144
145         res = -EINVAL;
146         if (!inode->i_op->readlink)
147                 goto err;
148
149         res = -ENOMEM;
150         buf = (char *) __get_free_page(GFP_KERNEL);
151         if (!buf)
152                 goto err;
153
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
166         return buf;
167
168 err:
169         return ERR_PTR(res);
170 }
171
172 static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
173 {
174         struct iattr attr = {
175                 .ia_valid =
176                      ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
177                 .ia_atime = stat->atime,
178                 .ia_mtime = stat->mtime,
179         };
180
181         return notify_change(upperdentry, &attr, NULL);
182 }
183
184 int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
185 {
186         int err = 0;
187
188         if (!S_ISLNK(stat->mode)) {
189                 struct iattr attr = {
190                         .ia_valid = ATTR_MODE,
191                         .ia_mode = stat->mode,
192                 };
193                 err = notify_change(upperdentry, &attr, NULL);
194         }
195         if (!err) {
196                 struct iattr attr = {
197                         .ia_valid = ATTR_UID | ATTR_GID,
198                         .ia_uid = stat->uid,
199                         .ia_gid = stat->gid,
200                 };
201                 err = notify_change(upperdentry, &attr, NULL);
202         }
203         if (!err)
204                 ovl_set_timestamps(upperdentry, stat);
205
206         return err;
207 }
208
209 static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
210                               struct dentry *dentry, struct path *lowerpath,
211                               struct kstat *stat, const char *link)
212 {
213         struct inode *wdir = workdir->d_inode;
214         struct inode *udir = upperdir->d_inode;
215         struct dentry *newdentry = NULL;
216         struct dentry *upper = NULL;
217         umode_t mode = stat->mode;
218         int err;
219
220         newdentry = ovl_lookup_temp(workdir, dentry);
221         err = PTR_ERR(newdentry);
222         if (IS_ERR(newdentry))
223                 goto out;
224
225         upper = lookup_one_len(dentry->d_name.name, upperdir,
226                                dentry->d_name.len);
227         err = PTR_ERR(upper);
228         if (IS_ERR(upper))
229                 goto out1;
230
231         /* Can't properly set mode on creation because of the umask */
232         stat->mode &= S_IFMT;
233         err = ovl_create_real(wdir, newdentry, stat, link, NULL, true);
234         stat->mode = mode;
235         if (err)
236                 goto out2;
237
238         if (S_ISREG(stat->mode)) {
239                 struct path upperpath;
240                 ovl_path_upper(dentry, &upperpath);
241                 BUG_ON(upperpath.dentry != NULL);
242                 upperpath.dentry = newdentry;
243
244                 err = ovl_copy_up_data(lowerpath, &upperpath, stat->size);
245                 if (err)
246                         goto out_cleanup;
247         }
248
249         err = ovl_copy_xattr(lowerpath->dentry, newdentry);
250         if (err)
251                 goto out_cleanup;
252
253         mutex_lock(&newdentry->d_inode->i_mutex);
254         err = ovl_set_attr(newdentry, stat);
255         mutex_unlock(&newdentry->d_inode->i_mutex);
256         if (err)
257                 goto out_cleanup;
258
259         err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
260         if (err)
261                 goto out_cleanup;
262
263         ovl_dentry_update(dentry, newdentry);
264         newdentry = NULL;
265
266         /*
267          * Non-directores become opaque when copied up.
268          */
269         if (!S_ISDIR(stat->mode))
270                 ovl_dentry_set_opaque(dentry, true);
271 out2:
272         dput(upper);
273 out1:
274         dput(newdentry);
275 out:
276         return err;
277
278 out_cleanup:
279         ovl_cleanup(wdir, newdentry);
280         goto out2;
281 }
282
283 /*
284  * Copy up a single dentry
285  *
286  * Directory renames only allowed on "pure upper" (already created on
287  * upper filesystem, never copied up).  Directories which are on lower or
288  * are merged may not be renamed.  For these -EXDEV is returned and
289  * userspace has to deal with it.  This means, when copying up a
290  * directory we can rely on it and ancestors being stable.
291  *
292  * Non-directory renames start with copy up of source if necessary.  The
293  * actual rename will only proceed once the copy up was successful.  Copy
294  * up uses upper parent i_mutex for exclusion.  Since rename can change
295  * d_parent it is possible that the copy up will lock the old parent.  At
296  * that point the file will have already been copied up anyway.
297  */
298 int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
299                     struct path *lowerpath, struct kstat *stat)
300 {
301         struct dentry *workdir = ovl_workdir(dentry);
302         int err;
303         struct kstat pstat;
304         struct path parentpath;
305         struct dentry *upperdir;
306         struct dentry *upperdentry;
307         const struct cred *old_cred;
308         struct cred *override_cred;
309         char *link = NULL;
310
311         if (WARN_ON(!workdir))
312                 return -EROFS;
313
314         ovl_path_upper(parent, &parentpath);
315         upperdir = parentpath.dentry;
316
317         err = vfs_getattr(&parentpath, &pstat);
318         if (err)
319                 return err;
320
321         if (S_ISLNK(stat->mode)) {
322                 link = ovl_read_symlink(lowerpath->dentry);
323                 if (IS_ERR(link))
324                         return PTR_ERR(link);
325         }
326
327         err = -ENOMEM;
328         override_cred = prepare_creds();
329         if (!override_cred)
330                 goto out_free_link;
331
332         override_cred->fsuid = stat->uid;
333         override_cred->fsgid = stat->gid;
334         /*
335          * CAP_SYS_ADMIN for copying up extended attributes
336          * CAP_DAC_OVERRIDE for create
337          * CAP_FOWNER for chmod, timestamp update
338          * CAP_FSETID for chmod
339          * CAP_CHOWN for chown
340          * CAP_MKNOD for mknod
341          */
342         cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
343         cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
344         cap_raise(override_cred->cap_effective, CAP_FOWNER);
345         cap_raise(override_cred->cap_effective, CAP_FSETID);
346         cap_raise(override_cred->cap_effective, CAP_CHOWN);
347         cap_raise(override_cred->cap_effective, CAP_MKNOD);
348         old_cred = override_creds(override_cred);
349
350         err = -EIO;
351         if (lock_rename(workdir, upperdir) != NULL) {
352                 pr_err("overlayfs: failed to lock workdir+upperdir\n");
353                 goto out_unlock;
354         }
355         upperdentry = ovl_dentry_upper(dentry);
356         if (upperdentry) {
357                 /* Raced with another copy-up?  Nothing to do, then... */
358                 err = 0;
359                 goto out_unlock;
360         }
361
362         err = ovl_copy_up_locked(workdir, upperdir, dentry, lowerpath,
363                                  stat, link);
364         if (!err) {
365                 /* Restore timestamps on parent (best effort) */
366                 ovl_set_timestamps(upperdir, &pstat);
367         }
368 out_unlock:
369         unlock_rename(workdir, upperdir);
370         revert_creds(old_cred);
371         put_cred(override_cred);
372
373 out_free_link:
374         if (link)
375                 free_page((unsigned long) link);
376
377         return err;
378 }
379
380 int ovl_copy_up(struct dentry *dentry)
381 {
382         int err;
383
384         err = 0;
385         while (!err) {
386                 struct dentry *next;
387                 struct dentry *parent;
388                 struct path lowerpath;
389                 struct kstat stat;
390                 enum ovl_path_type type = ovl_path_type(dentry);
391
392                 if (OVL_TYPE_UPPER(type))
393                         break;
394
395                 next = dget(dentry);
396                 /* find the topmost dentry not yet copied up */
397                 for (;;) {
398                         parent = dget_parent(next);
399
400                         type = ovl_path_type(parent);
401                         if (OVL_TYPE_UPPER(type))
402                                 break;
403
404                         dput(next);
405                         next = parent;
406                 }
407
408                 ovl_path_lower(next, &lowerpath);
409                 err = vfs_getattr(&lowerpath, &stat);
410                 if (!err)
411                         err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
412
413                 dput(parent);
414                 dput(next);
415         }
416
417         return err;
418 }