3 * Copyright (C) 2011 Novell Inc.
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.
10 #include <linux/module.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"
24 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
26 static bool __read_mostly ovl_check_copy_up
;
27 module_param_named(check_copy_up
, ovl_check_copy_up
, bool,
29 MODULE_PARM_DESC(ovl_check_copy_up
,
30 "Warn on copy-up when causing process also has a R/O fd open");
32 static int ovl_check_fd(const void *data
, struct file
*f
, unsigned int fd
)
34 const struct dentry
*dentry
= data
;
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
);
43 * Check the fds open by this process and warn if something like the following
44 * scenario is about to occur:
46 * fd1 = open("foo", O_RDONLY);
47 * fd2 = open("foo", O_RDWR);
49 static void ovl_do_check_copy_up(struct dentry
*dentry
)
51 if (ovl_check_copy_up
)
52 iterate_fd(current
->files
, 0, ovl_check_fd
, dentry
);
55 int ovl_copy_xattr(struct dentry
*old
, struct dentry
*new)
57 ssize_t list_size
, size
, value_size
= 0;
58 char *buf
, *name
, *value
= NULL
;
59 int uninitialized_var(error
);
61 if (!old
->d_inode
->i_op
->getxattr
||
62 !new->d_inode
->i_op
->getxattr
)
65 list_size
= vfs_listxattr(old
, NULL
, 0);
67 if (list_size
== -EOPNOTSUPP
)
72 buf
= kzalloc(list_size
, GFP_KERNEL
);
76 list_size
= vfs_listxattr(old
, buf
, list_size
);
82 for (name
= buf
; name
< (buf
+ list_size
); name
+= strlen(name
) + 1) {
84 size
= vfs_getxattr(old
, name
, value
, value_size
);
86 size
= vfs_getxattr(old
, name
, NULL
, 0);
93 if (size
> value_size
) {
96 new = krealloc(value
, size
, GFP_KERNEL
);
106 error
= vfs_setxattr(new, name
, value
, size
, 0);
116 static int ovl_copy_up_data(struct path
*old
, struct path
*new, loff_t len
)
118 struct file
*old_file
;
119 struct file
*new_file
;
127 old_file
= ovl_path_open(old
, O_LARGEFILE
| O_RDONLY
);
128 if (IS_ERR(old_file
))
129 return PTR_ERR(old_file
);
131 new_file
= ovl_path_open(new, O_LARGEFILE
| O_WRONLY
);
132 if (IS_ERR(new_file
)) {
133 error
= PTR_ERR(new_file
);
137 /* FIXME: copy up sparse files efficiently */
139 size_t this_len
= OVL_COPY_UP_CHUNK_SIZE
;
145 if (signal_pending_state(TASK_KILLABLE
, current
)) {
150 bytes
= do_splice_direct(old_file
, &old_pos
,
152 this_len
, SPLICE_F_MOVE
);
157 WARN_ON(old_pos
!= new_pos
);
168 static char *ovl_read_symlink(struct dentry
*realdentry
)
172 struct inode
*inode
= realdentry
->d_inode
;
176 if (!inode
->i_op
->readlink
)
180 buf
= (char *) __get_free_page(GFP_KERNEL
);
186 /* The cast to a user pointer is valid due to the set_fs() */
187 res
= inode
->i_op
->readlink(realdentry
,
188 (char __user
*)buf
, PAGE_SIZE
- 1);
191 free_page((unsigned long) buf
);
202 static int ovl_set_timestamps(struct dentry
*upperdentry
, struct kstat
*stat
)
204 struct iattr attr
= {
206 ATTR_ATIME
| ATTR_MTIME
| ATTR_ATIME_SET
| ATTR_MTIME_SET
,
207 .ia_atime
= stat
->atime
,
208 .ia_mtime
= stat
->mtime
,
211 return notify_change(upperdentry
, &attr
, NULL
);
214 int ovl_set_attr(struct dentry
*upperdentry
, struct kstat
*stat
)
218 if (!S_ISLNK(stat
->mode
)) {
219 struct iattr attr
= {
220 .ia_valid
= ATTR_MODE
,
221 .ia_mode
= stat
->mode
,
223 err
= notify_change(upperdentry
, &attr
, NULL
);
226 struct iattr attr
= {
227 .ia_valid
= ATTR_UID
| ATTR_GID
,
231 err
= notify_change(upperdentry
, &attr
, NULL
);
234 ovl_set_timestamps(upperdentry
, stat
);
239 static int ovl_copy_up_locked(struct dentry
*workdir
, struct dentry
*upperdir
,
240 struct dentry
*dentry
, struct path
*lowerpath
,
241 struct kstat
*stat
, const char *link
)
243 struct inode
*wdir
= workdir
->d_inode
;
244 struct inode
*udir
= upperdir
->d_inode
;
245 struct dentry
*newdentry
= NULL
;
246 struct dentry
*upper
= NULL
;
247 umode_t mode
= stat
->mode
;
250 newdentry
= ovl_lookup_temp(workdir
, dentry
);
251 err
= PTR_ERR(newdentry
);
252 if (IS_ERR(newdentry
))
255 upper
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
257 err
= PTR_ERR(upper
);
261 /* Can't properly set mode on creation because of the umask */
262 stat
->mode
&= S_IFMT
;
263 err
= ovl_create_real(wdir
, newdentry
, stat
, link
, NULL
, true);
268 if (S_ISREG(stat
->mode
)) {
269 struct path upperpath
;
271 ovl_path_upper(dentry
, &upperpath
);
272 BUG_ON(upperpath
.dentry
!= NULL
);
273 upperpath
.dentry
= newdentry
;
275 err
= ovl_copy_up_data(lowerpath
, &upperpath
, stat
->size
);
280 err
= ovl_copy_xattr(lowerpath
->dentry
, newdentry
);
284 inode_lock(newdentry
->d_inode
);
285 err
= ovl_set_attr(newdentry
, stat
);
286 inode_unlock(newdentry
->d_inode
);
290 err
= ovl_do_rename(wdir
, newdentry
, udir
, upper
, 0);
294 ovl_dentry_update(dentry
, newdentry
);
298 * Non-directores become opaque when copied up.
300 if (!S_ISDIR(stat
->mode
))
301 ovl_dentry_set_opaque(dentry
, true);
310 ovl_cleanup(wdir
, newdentry
);
315 * Copy up a single dentry
317 * Directory renames only allowed on "pure upper" (already created on
318 * upper filesystem, never copied up). Directories which are on lower or
319 * are merged may not be renamed. For these -EXDEV is returned and
320 * userspace has to deal with it. This means, when copying up a
321 * directory we can rely on it and ancestors being stable.
323 * Non-directory renames start with copy up of source if necessary. The
324 * actual rename will only proceed once the copy up was successful. Copy
325 * up uses upper parent i_mutex for exclusion. Since rename can change
326 * d_parent it is possible that the copy up will lock the old parent. At
327 * that point the file will have already been copied up anyway.
329 int ovl_copy_up_one(struct dentry
*parent
, struct dentry
*dentry
,
330 struct path
*lowerpath
, struct kstat
*stat
)
332 struct dentry
*workdir
= ovl_workdir(dentry
);
335 struct path parentpath
;
336 struct dentry
*upperdir
;
337 struct dentry
*upperdentry
;
338 const struct cred
*old_cred
;
339 struct cred
*override_cred
;
342 if (WARN_ON(!workdir
))
345 ovl_do_check_copy_up(lowerpath
->dentry
);
347 ovl_path_upper(parent
, &parentpath
);
348 upperdir
= parentpath
.dentry
;
350 err
= vfs_getattr(&parentpath
, &pstat
);
354 if (S_ISLNK(stat
->mode
)) {
355 link
= ovl_read_symlink(lowerpath
->dentry
);
357 return PTR_ERR(link
);
361 override_cred
= prepare_creds();
365 override_cred
->fsuid
= stat
->uid
;
366 override_cred
->fsgid
= stat
->gid
;
368 * CAP_SYS_ADMIN for copying up extended attributes
369 * CAP_DAC_OVERRIDE for create
370 * CAP_FOWNER for chmod, timestamp update
371 * CAP_FSETID for chmod
372 * CAP_CHOWN for chown
373 * CAP_MKNOD for mknod
375 cap_raise(override_cred
->cap_effective
, CAP_SYS_ADMIN
);
376 cap_raise(override_cred
->cap_effective
, CAP_DAC_OVERRIDE
);
377 cap_raise(override_cred
->cap_effective
, CAP_FOWNER
);
378 cap_raise(override_cred
->cap_effective
, CAP_FSETID
);
379 cap_raise(override_cred
->cap_effective
, CAP_CHOWN
);
380 cap_raise(override_cred
->cap_effective
, CAP_MKNOD
);
381 old_cred
= override_creds(override_cred
);
384 if (lock_rename(workdir
, upperdir
) != NULL
) {
385 pr_err("overlayfs: failed to lock workdir+upperdir\n");
388 upperdentry
= ovl_dentry_upper(dentry
);
390 /* Raced with another copy-up? Nothing to do, then... */
395 err
= ovl_copy_up_locked(workdir
, upperdir
, dentry
, lowerpath
,
398 /* Restore timestamps on parent (best effort) */
399 ovl_set_timestamps(upperdir
, &pstat
);
402 unlock_rename(workdir
, upperdir
);
403 revert_creds(old_cred
);
404 put_cred(override_cred
);
408 free_page((unsigned long) link
);
413 int ovl_copy_up(struct dentry
*dentry
)
420 struct dentry
*parent
;
421 struct path lowerpath
;
423 enum ovl_path_type type
= ovl_path_type(dentry
);
425 if (OVL_TYPE_UPPER(type
))
429 /* find the topmost dentry not yet copied up */
431 parent
= dget_parent(next
);
433 type
= ovl_path_type(parent
);
434 if (OVL_TYPE_UPPER(type
))
441 ovl_path_lower(next
, &lowerpath
);
442 err
= vfs_getattr(&lowerpath
, &stat
);
444 err
= ovl_copy_up_one(parent
, next
, &lowerpath
, &stat
);