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.
11 #include <linux/slab.h>
12 #include <linux/cred.h>
13 #include <linux/xattr.h>
14 #include <linux/posix_acl.h>
15 #include "overlayfs.h"
17 int ovl_setattr(struct dentry
*dentry
, struct iattr
*attr
)
20 struct dentry
*upperdentry
;
21 const struct cred
*old_cred
;
24 * Check for permissions before trying to copy-up. This is redundant
25 * since it will be rechecked later by ->setattr() on upper dentry. But
26 * without this, copy-up can be triggered by just about anybody.
28 * We don't initialize inode->size, which just means that
29 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
30 * check for a swapfile (which this won't be anyway).
32 err
= setattr_prepare(dentry
, attr
);
36 err
= ovl_want_write(dentry
);
40 err
= ovl_copy_up(dentry
);
42 upperdentry
= ovl_dentry_upper(dentry
);
44 if (attr
->ia_valid
& (ATTR_KILL_SUID
|ATTR_KILL_SGID
))
45 attr
->ia_valid
&= ~ATTR_MODE
;
47 inode_lock(upperdentry
->d_inode
);
48 old_cred
= ovl_override_creds(dentry
->d_sb
);
49 err
= notify_change(upperdentry
, attr
, NULL
);
50 revert_creds(old_cred
);
52 ovl_copyattr(upperdentry
->d_inode
, dentry
->d_inode
);
53 inode_unlock(upperdentry
->d_inode
);
55 ovl_drop_write(dentry
);
60 static int ovl_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
64 const struct cred
*old_cred
;
67 ovl_path_real(dentry
, &realpath
);
68 old_cred
= ovl_override_creds(dentry
->d_sb
);
69 err
= vfs_getattr(&realpath
, stat
);
70 revert_creds(old_cred
);
74 int ovl_permission(struct inode
*inode
, int mask
)
77 struct inode
*realinode
= ovl_inode_real(inode
, &is_upper
);
78 const struct cred
*old_cred
;
81 /* Careful in RCU walk mode */
83 WARN_ON(!(mask
& MAY_NOT_BLOCK
));
88 * Check overlay inode with the creds of task and underlying inode
89 * with creds of mounter
91 err
= generic_permission(inode
, mask
);
95 old_cred
= ovl_override_creds(inode
->i_sb
);
96 if (!is_upper
&& !special_file(realinode
->i_mode
) && mask
& MAY_WRITE
) {
97 mask
&= ~(MAY_WRITE
| MAY_APPEND
);
98 /* Make sure mounter can read file for copy up later */
101 err
= inode_permission(realinode
, mask
);
102 revert_creds(old_cred
);
107 static const char *ovl_get_link(struct dentry
*dentry
,
109 struct delayed_call
*done
)
111 const struct cred
*old_cred
;
115 return ERR_PTR(-ECHILD
);
117 old_cred
= ovl_override_creds(dentry
->d_sb
);
118 p
= vfs_get_link(ovl_dentry_real(dentry
), done
);
119 revert_creds(old_cred
);
123 bool ovl_is_private_xattr(const char *name
)
125 return strncmp(name
, OVL_XATTR_PREFIX
,
126 sizeof(OVL_XATTR_PREFIX
) - 1) == 0;
129 int ovl_xattr_set(struct dentry
*dentry
, const char *name
, const void *value
,
130 size_t size
, int flags
)
133 struct path realpath
;
134 enum ovl_path_type type
= ovl_path_real(dentry
, &realpath
);
135 const struct cred
*old_cred
;
137 err
= ovl_want_write(dentry
);
141 if (!value
&& !OVL_TYPE_UPPER(type
)) {
142 err
= vfs_getxattr(realpath
.dentry
, name
, NULL
, 0);
147 err
= ovl_copy_up(dentry
);
151 if (!OVL_TYPE_UPPER(type
))
152 ovl_path_upper(dentry
, &realpath
);
154 old_cred
= ovl_override_creds(dentry
->d_sb
);
156 err
= vfs_setxattr(realpath
.dentry
, name
, value
, size
, flags
);
158 WARN_ON(flags
!= XATTR_REPLACE
);
159 err
= vfs_removexattr(realpath
.dentry
, name
);
161 revert_creds(old_cred
);
164 ovl_drop_write(dentry
);
169 int ovl_xattr_get(struct dentry
*dentry
, const char *name
,
170 void *value
, size_t size
)
172 struct dentry
*realdentry
= ovl_dentry_real(dentry
);
174 const struct cred
*old_cred
;
176 old_cred
= ovl_override_creds(dentry
->d_sb
);
177 res
= vfs_getxattr(realdentry
, name
, value
, size
);
178 revert_creds(old_cred
);
182 ssize_t
ovl_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
184 struct dentry
*realdentry
= ovl_dentry_real(dentry
);
188 const struct cred
*old_cred
;
190 old_cred
= ovl_override_creds(dentry
->d_sb
);
191 res
= vfs_listxattr(realdentry
, list
, size
);
192 revert_creds(old_cred
);
193 if (res
<= 0 || size
== 0)
196 /* filter out private xattrs */
197 for (s
= list
, len
= res
; len
;) {
198 size_t slen
= strnlen(s
, len
) + 1;
200 /* underlying fs providing us with an broken xattr list? */
201 if (WARN_ON(slen
> len
))
205 if (ovl_is_private_xattr(s
)) {
207 memmove(s
, s
+ slen
, len
);
216 struct posix_acl
*ovl_get_acl(struct inode
*inode
, int type
)
218 struct inode
*realinode
= ovl_inode_real(inode
, NULL
);
219 const struct cred
*old_cred
;
220 struct posix_acl
*acl
;
222 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL
) || !IS_POSIXACL(realinode
))
225 old_cred
= ovl_override_creds(inode
->i_sb
);
226 acl
= get_acl(realinode
, type
);
227 revert_creds(old_cred
);
232 static bool ovl_open_need_copy_up(int flags
, enum ovl_path_type type
,
233 struct dentry
*realdentry
)
235 if (OVL_TYPE_UPPER(type
))
238 if (special_file(realdentry
->d_inode
->i_mode
))
241 if (!(OPEN_FMODE(flags
) & FMODE_WRITE
) && !(flags
& O_TRUNC
))
247 int ovl_open_maybe_copy_up(struct dentry
*dentry
, unsigned int file_flags
)
250 struct path realpath
;
251 enum ovl_path_type type
;
253 type
= ovl_path_real(dentry
, &realpath
);
254 if (ovl_open_need_copy_up(file_flags
, type
, realpath
.dentry
)) {
255 err
= ovl_want_write(dentry
);
257 err
= ovl_copy_up_flags(dentry
, file_flags
);
258 ovl_drop_write(dentry
);
265 int ovl_update_time(struct inode
*inode
, struct timespec
*ts
, int flags
)
267 struct dentry
*alias
;
268 struct path upperpath
;
270 if (!(flags
& S_ATIME
))
273 alias
= d_find_any_alias(inode
);
277 ovl_path_upper(alias
, &upperpath
);
278 if (upperpath
.dentry
) {
279 touch_atime(&upperpath
);
280 inode
->i_atime
= d_inode(upperpath
.dentry
)->i_atime
;
288 static const struct inode_operations ovl_file_inode_operations
= {
289 .setattr
= ovl_setattr
,
290 .permission
= ovl_permission
,
291 .getattr
= ovl_getattr
,
292 .listxattr
= ovl_listxattr
,
293 .get_acl
= ovl_get_acl
,
294 .update_time
= ovl_update_time
,
297 static const struct inode_operations ovl_symlink_inode_operations
= {
298 .setattr
= ovl_setattr
,
299 .get_link
= ovl_get_link
,
300 .getattr
= ovl_getattr
,
301 .listxattr
= ovl_listxattr
,
302 .update_time
= ovl_update_time
,
305 static void ovl_fill_inode(struct inode
*inode
, umode_t mode
, dev_t rdev
)
307 inode
->i_ino
= get_next_ino();
308 inode
->i_mode
= mode
;
309 inode
->i_flags
|= S_NOCMTIME
;
310 #ifdef CONFIG_FS_POSIX_ACL
311 inode
->i_acl
= inode
->i_default_acl
= ACL_DONT_CACHE
;
314 switch (mode
& S_IFMT
) {
316 inode
->i_op
= &ovl_file_inode_operations
;
320 inode
->i_op
= &ovl_dir_inode_operations
;
321 inode
->i_fop
= &ovl_dir_operations
;
325 inode
->i_op
= &ovl_symlink_inode_operations
;
329 inode
->i_op
= &ovl_file_inode_operations
;
330 init_special_inode(inode
, mode
, rdev
);
335 struct inode
*ovl_new_inode(struct super_block
*sb
, umode_t mode
, dev_t rdev
)
339 inode
= new_inode(sb
);
341 ovl_fill_inode(inode
, mode
, rdev
);
346 static int ovl_inode_test(struct inode
*inode
, void *data
)
348 return ovl_inode_real(inode
, NULL
) == data
;
351 static int ovl_inode_set(struct inode
*inode
, void *data
)
353 inode
->i_private
= (void *) (((unsigned long) data
) | OVL_ISUPPER_MASK
);
357 struct inode
*ovl_get_inode(struct super_block
*sb
, struct inode
*realinode
)
362 inode
= iget5_locked(sb
, (unsigned long) realinode
,
363 ovl_inode_test
, ovl_inode_set
, realinode
);
364 if (inode
&& inode
->i_state
& I_NEW
) {
365 ovl_fill_inode(inode
, realinode
->i_mode
, realinode
->i_rdev
);
366 set_nlink(inode
, realinode
->i_nlink
);
367 unlock_new_inode(inode
);