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