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 (file_inode(f
) == 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
);
62 if (!(old
->d_inode
->i_opflags
& IOP_XATTR
) ||
63 !(new->d_inode
->i_opflags
& IOP_XATTR
))
66 list_size
= vfs_listxattr(old
, NULL
, 0);
68 if (list_size
== -EOPNOTSUPP
)
73 buf
= kzalloc(list_size
, GFP_KERNEL
);
77 list_size
= vfs_listxattr(old
, buf
, list_size
);
83 for (name
= buf
; list_size
; name
+= slen
) {
84 slen
= strnlen(name
, list_size
) + 1;
86 /* underlying fs providing us with an broken xattr list? */
87 if (WARN_ON(slen
> list_size
)) {
93 if (ovl_is_private_xattr(name
))
96 size
= vfs_getxattr(old
, name
, value
, value_size
);
98 size
= vfs_getxattr(old
, name
, NULL
, 0);
105 if (size
> value_size
) {
108 new = krealloc(value
, size
, GFP_KERNEL
);
118 error
= security_inode_copy_up_xattr(name
);
119 if (error
< 0 && error
!= -EOPNOTSUPP
)
123 continue; /* Discard */
125 error
= vfs_setxattr(new, name
, value
, size
, 0);
135 static int ovl_copy_up_data(struct path
*old
, struct path
*new, loff_t len
)
137 struct file
*old_file
;
138 struct file
*new_file
;
146 old_file
= ovl_path_open(old
, O_LARGEFILE
| O_RDONLY
);
147 if (IS_ERR(old_file
))
148 return PTR_ERR(old_file
);
150 new_file
= ovl_path_open(new, O_LARGEFILE
| O_WRONLY
);
151 if (IS_ERR(new_file
)) {
152 error
= PTR_ERR(new_file
);
156 /* Try to use clone_file_range to clone up within the same fs */
157 error
= vfs_clone_file_range(old_file
, 0, new_file
, 0, len
);
160 /* Couldn't clone, so now we try to copy the data */
163 /* FIXME: copy up sparse files efficiently */
165 size_t this_len
= OVL_COPY_UP_CHUNK_SIZE
;
171 if (signal_pending_state(TASK_KILLABLE
, current
)) {
176 bytes
= do_splice_direct(old_file
, &old_pos
,
178 this_len
, SPLICE_F_MOVE
);
183 WARN_ON(old_pos
!= new_pos
);
189 error
= vfs_fsync(new_file
, 0);
196 static int ovl_set_timestamps(struct dentry
*upperdentry
, struct kstat
*stat
)
198 struct iattr attr
= {
200 ATTR_ATIME
| ATTR_MTIME
| ATTR_ATIME_SET
| ATTR_MTIME_SET
,
201 .ia_atime
= stat
->atime
,
202 .ia_mtime
= stat
->mtime
,
205 return notify_change(upperdentry
, &attr
, NULL
);
208 int ovl_set_attr(struct dentry
*upperdentry
, struct kstat
*stat
)
213 * For the most part we want to set the mode bits before setting
214 * the user, otherwise the current context might lack permission
215 * for setting the mode. However for sxid/sticky bits we want
216 * the operation to fail if the current user isn't privileged
217 * towards the resulting inode. So we first set the mode but
218 * exclude the sxid/sticky bits, then set the user, then set the
219 * mode again if any of the sxid/sticky bits are set.
221 if (!S_ISLNK(stat
->mode
)) {
222 struct iattr attr
= {
223 .ia_valid
= ATTR_MODE
,
224 .ia_mode
= stat
->mode
& ~(S_ISUID
|S_ISGID
|S_ISVTX
),
226 err
= notify_change(upperdentry
, &attr
, NULL
);
229 struct iattr attr
= {
230 .ia_valid
= ATTR_UID
| ATTR_GID
,
234 err
= notify_change(upperdentry
, &attr
, NULL
);
236 if (!err
&& !S_ISLNK(stat
->mode
) &&
237 (stat
->mode
& (S_ISUID
|S_ISGID
|S_ISVTX
))) {
238 struct iattr attr
= {
239 .ia_valid
= ATTR_MODE
,
240 .ia_mode
= stat
->mode
,
242 err
= notify_change(upperdentry
, &attr
, NULL
);
245 ovl_set_timestamps(upperdentry
, stat
);
250 static int ovl_copy_up_locked(struct dentry
*workdir
, struct dentry
*upperdir
,
251 struct dentry
*dentry
, struct path
*lowerpath
,
252 struct kstat
*stat
, const char *link
)
254 struct inode
*wdir
= workdir
->d_inode
;
255 struct inode
*udir
= upperdir
->d_inode
;
256 struct dentry
*newdentry
= NULL
;
257 struct dentry
*upper
= NULL
;
259 const struct cred
*old_creds
= NULL
;
260 struct cred
*new_creds
= NULL
;
261 struct cattr cattr
= {
262 /* Can't properly set mode on creation because of the umask */
263 .mode
= stat
->mode
& S_IFMT
,
268 newdentry
= ovl_lookup_temp(workdir
, dentry
);
269 err
= PTR_ERR(newdentry
);
270 if (IS_ERR(newdentry
))
273 upper
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
275 err
= PTR_ERR(upper
);
279 err
= security_inode_copy_up(dentry
, &new_creds
);
284 old_creds
= override_creds(new_creds
);
286 err
= ovl_create_real(wdir
, newdentry
, &cattr
, NULL
, true);
289 revert_creds(old_creds
);
296 if (S_ISREG(stat
->mode
)) {
297 struct path upperpath
;
299 ovl_path_upper(dentry
, &upperpath
);
300 BUG_ON(upperpath
.dentry
!= NULL
);
301 upperpath
.dentry
= newdentry
;
303 err
= ovl_copy_up_data(lowerpath
, &upperpath
, stat
->size
);
308 err
= ovl_copy_xattr(lowerpath
->dentry
, newdentry
);
312 inode_lock(newdentry
->d_inode
);
313 err
= ovl_set_attr(newdentry
, stat
);
314 inode_unlock(newdentry
->d_inode
);
318 err
= ovl_do_rename(wdir
, newdentry
, udir
, upper
, 0);
322 ovl_dentry_update(dentry
, newdentry
);
323 ovl_inode_update(d_inode(dentry
), d_inode(newdentry
));
333 ovl_cleanup(wdir
, newdentry
);
338 * Copy up a single dentry
340 * All renames start with copy up of source if necessary. The actual
341 * rename will only proceed once the copy up was successful. Copy up uses
342 * upper parent i_mutex for exclusion. Since rename can change d_parent it
343 * is possible that the copy up will lock the old parent. At that point
344 * the file will have already been copied up anyway.
346 static int ovl_copy_up_one(struct dentry
*parent
, struct dentry
*dentry
,
347 struct path
*lowerpath
, struct kstat
*stat
)
349 DEFINE_DELAYED_CALL(done
);
350 struct dentry
*workdir
= ovl_workdir(dentry
);
353 struct path parentpath
;
354 struct dentry
*lowerdentry
= lowerpath
->dentry
;
355 struct dentry
*upperdir
;
356 const char *link
= NULL
;
358 if (WARN_ON(!workdir
))
361 ovl_do_check_copy_up(lowerdentry
);
363 ovl_path_upper(parent
, &parentpath
);
364 upperdir
= parentpath
.dentry
;
366 err
= vfs_getattr(&parentpath
, &pstat
);
370 if (S_ISLNK(stat
->mode
)) {
371 link
= vfs_get_link(lowerdentry
, &done
);
373 return PTR_ERR(link
);
377 if (lock_rename(workdir
, upperdir
) != NULL
) {
378 pr_err("overlayfs: failed to lock workdir+upperdir\n");
381 if (ovl_dentry_upper(dentry
)) {
382 /* Raced with another copy-up? Nothing to do, then... */
387 err
= ovl_copy_up_locked(workdir
, upperdir
, dentry
, lowerpath
,
390 /* Restore timestamps on parent (best effort) */
391 ovl_set_timestamps(upperdir
, &pstat
);
394 unlock_rename(workdir
, upperdir
);
395 do_delayed_call(&done
);
400 int ovl_copy_up_flags(struct dentry
*dentry
, int flags
)
403 const struct cred
*old_cred
= ovl_override_creds(dentry
->d_sb
);
407 struct dentry
*parent
;
408 struct path lowerpath
;
410 enum ovl_path_type type
= ovl_path_type(dentry
);
412 if (OVL_TYPE_UPPER(type
))
416 /* find the topmost dentry not yet copied up */
418 parent
= dget_parent(next
);
420 type
= ovl_path_type(parent
);
421 if (OVL_TYPE_UPPER(type
))
428 ovl_path_lower(next
, &lowerpath
);
429 err
= vfs_getattr(&lowerpath
, &stat
);
430 /* maybe truncate regular file. this has no effect on dirs */
434 err
= ovl_copy_up_one(parent
, next
, &lowerpath
, &stat
);
439 revert_creds(old_cred
);
444 int ovl_copy_up(struct dentry
*dentry
)
446 return ovl_copy_up_flags(dentry
, 0);