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