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/xattr.h>
13 #include <linux/posix_acl.h>
14 #include "overlayfs.h"
16 int ovl_setattr(struct dentry
*dentry
, struct iattr
*attr
)
19 struct dentry
*upperdentry
;
20 const struct cred
*old_cred
;
23 * Check for permissions before trying to copy-up. This is redundant
24 * since it will be rechecked later by ->setattr() on upper dentry. But
25 * without this, copy-up can be triggered by just about anybody.
27 * We don't initialize inode->size, which just means that
28 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
29 * check for a swapfile (which this won't be anyway).
31 err
= setattr_prepare(dentry
, attr
);
35 err
= ovl_want_write(dentry
);
39 err
= ovl_copy_up(dentry
);
41 upperdentry
= ovl_dentry_upper(dentry
);
43 if (attr
->ia_valid
& (ATTR_KILL_SUID
|ATTR_KILL_SGID
))
44 attr
->ia_valid
&= ~ATTR_MODE
;
46 inode_lock(upperdentry
->d_inode
);
47 old_cred
= ovl_override_creds(dentry
->d_sb
);
48 err
= notify_change(upperdentry
, attr
, NULL
);
49 revert_creds(old_cred
);
51 ovl_copyattr(upperdentry
->d_inode
, dentry
->d_inode
);
52 inode_unlock(upperdentry
->d_inode
);
54 ovl_drop_write(dentry
);
59 static int ovl_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
63 const struct cred
*old_cred
;
66 ovl_path_real(dentry
, &realpath
);
67 old_cred
= ovl_override_creds(dentry
->d_sb
);
68 err
= vfs_getattr(&realpath
, stat
);
69 revert_creds(old_cred
);
73 int ovl_permission(struct inode
*inode
, int mask
)
76 struct inode
*realinode
= ovl_inode_real(inode
, &is_upper
);
77 const struct cred
*old_cred
;
80 /* Careful in RCU walk mode */
82 WARN_ON(!(mask
& MAY_NOT_BLOCK
));
87 * Check overlay inode with the creds of task and underlying inode
88 * with creds of mounter
90 err
= generic_permission(inode
, mask
);
94 old_cred
= ovl_override_creds(inode
->i_sb
);
95 if (!is_upper
&& !special_file(realinode
->i_mode
) && mask
& MAY_WRITE
) {
96 mask
&= ~(MAY_WRITE
| MAY_APPEND
);
97 /* Make sure mounter can read file for copy up later */
100 err
= inode_permission(realinode
, mask
);
101 revert_creds(old_cred
);
106 static const char *ovl_get_link(struct dentry
*dentry
,
108 struct delayed_call
*done
)
110 const struct cred
*old_cred
;
114 return ERR_PTR(-ECHILD
);
116 old_cred
= ovl_override_creds(dentry
->d_sb
);
117 p
= vfs_get_link(ovl_dentry_real(dentry
), done
);
118 revert_creds(old_cred
);
122 bool ovl_is_private_xattr(const char *name
)
124 return strncmp(name
, OVL_XATTR_PREFIX
,
125 sizeof(OVL_XATTR_PREFIX
) - 1) == 0;
128 int ovl_xattr_set(struct dentry
*dentry
, const char *name
, const void *value
,
129 size_t size
, int flags
)
132 struct path realpath
;
133 enum ovl_path_type type
= ovl_path_real(dentry
, &realpath
);
134 const struct cred
*old_cred
;
136 err
= ovl_want_write(dentry
);
140 if (!value
&& !OVL_TYPE_UPPER(type
)) {
141 err
= vfs_getxattr(realpath
.dentry
, name
, NULL
, 0);
146 err
= ovl_copy_up(dentry
);
150 if (!OVL_TYPE_UPPER(type
))
151 ovl_path_upper(dentry
, &realpath
);
153 old_cred
= ovl_override_creds(dentry
->d_sb
);
155 err
= vfs_setxattr(realpath
.dentry
, name
, value
, size
, flags
);
157 WARN_ON(flags
!= XATTR_REPLACE
);
158 err
= vfs_removexattr(realpath
.dentry
, name
);
160 revert_creds(old_cred
);
163 ovl_drop_write(dentry
);
168 int ovl_xattr_get(struct dentry
*dentry
, const char *name
,
169 void *value
, size_t size
)
171 struct dentry
*realdentry
= ovl_dentry_real(dentry
);
173 const struct cred
*old_cred
;
175 old_cred
= ovl_override_creds(dentry
->d_sb
);
176 res
= vfs_getxattr(realdentry
, name
, value
, size
);
177 revert_creds(old_cred
);
181 ssize_t
ovl_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
183 struct dentry
*realdentry
= ovl_dentry_real(dentry
);
187 const struct cred
*old_cred
;
189 old_cred
= ovl_override_creds(dentry
->d_sb
);
190 res
= vfs_listxattr(realdentry
, list
, size
);
191 revert_creds(old_cred
);
192 if (res
<= 0 || size
== 0)
195 /* filter out private xattrs */
196 for (s
= list
, len
= res
; len
;) {
197 size_t slen
= strnlen(s
, len
) + 1;
199 /* underlying fs providing us with an broken xattr list? */
200 if (WARN_ON(slen
> len
))
204 if (ovl_is_private_xattr(s
)) {
206 memmove(s
, s
+ slen
, len
);
215 struct posix_acl
*ovl_get_acl(struct inode
*inode
, int type
)
217 struct inode
*realinode
= ovl_inode_real(inode
, NULL
);
218 const struct cred
*old_cred
;
219 struct posix_acl
*acl
;
221 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL
) || !IS_POSIXACL(realinode
))
224 old_cred
= ovl_override_creds(inode
->i_sb
);
225 acl
= get_acl(realinode
, type
);
226 revert_creds(old_cred
);
231 static bool ovl_open_need_copy_up(int flags
, enum ovl_path_type type
,
232 struct dentry
*realdentry
)
234 if (OVL_TYPE_UPPER(type
))
237 if (special_file(realdentry
->d_inode
->i_mode
))
240 if (!(OPEN_FMODE(flags
) & FMODE_WRITE
) && !(flags
& O_TRUNC
))
246 int ovl_open_maybe_copy_up(struct dentry
*dentry
, unsigned int file_flags
)
249 struct path realpath
;
250 enum ovl_path_type type
;
252 type
= ovl_path_real(dentry
, &realpath
);
253 if (ovl_open_need_copy_up(file_flags
, type
, realpath
.dentry
)) {
254 err
= ovl_want_write(dentry
);
256 err
= ovl_copy_up_flags(dentry
, file_flags
);
257 ovl_drop_write(dentry
);
264 int ovl_update_time(struct inode
*inode
, struct timespec
*ts
, int flags
)
266 struct dentry
*alias
;
267 struct path upperpath
;
269 if (!(flags
& S_ATIME
))
272 alias
= d_find_any_alias(inode
);
276 ovl_path_upper(alias
, &upperpath
);
277 if (upperpath
.dentry
) {
278 touch_atime(&upperpath
);
279 inode
->i_atime
= d_inode(upperpath
.dentry
)->i_atime
;
287 static const struct inode_operations ovl_file_inode_operations
= {
288 .setattr
= ovl_setattr
,
289 .permission
= ovl_permission
,
290 .getattr
= ovl_getattr
,
291 .listxattr
= ovl_listxattr
,
292 .get_acl
= ovl_get_acl
,
293 .update_time
= ovl_update_time
,
296 static const struct inode_operations ovl_symlink_inode_operations
= {
297 .setattr
= ovl_setattr
,
298 .get_link
= ovl_get_link
,
299 .getattr
= ovl_getattr
,
300 .listxattr
= ovl_listxattr
,
301 .update_time
= ovl_update_time
,
304 static void ovl_fill_inode(struct inode
*inode
, umode_t mode
, dev_t rdev
)
306 inode
->i_ino
= get_next_ino();
307 inode
->i_mode
= mode
;
308 inode
->i_flags
|= S_NOCMTIME
;
309 #ifdef CONFIG_FS_POSIX_ACL
310 inode
->i_acl
= inode
->i_default_acl
= ACL_DONT_CACHE
;
313 switch (mode
& S_IFMT
) {
315 inode
->i_op
= &ovl_file_inode_operations
;
319 inode
->i_op
= &ovl_dir_inode_operations
;
320 inode
->i_fop
= &ovl_dir_operations
;
324 inode
->i_op
= &ovl_symlink_inode_operations
;
328 inode
->i_op
= &ovl_file_inode_operations
;
329 init_special_inode(inode
, mode
, rdev
);
334 struct inode
*ovl_new_inode(struct super_block
*sb
, umode_t mode
, dev_t rdev
)
338 inode
= new_inode(sb
);
340 ovl_fill_inode(inode
, mode
, rdev
);
345 static int ovl_inode_test(struct inode
*inode
, void *data
)
347 return ovl_inode_real(inode
, NULL
) == data
;
350 static int ovl_inode_set(struct inode
*inode
, void *data
)
352 inode
->i_private
= (void *) (((unsigned long) data
) | OVL_ISUPPER_MASK
);
356 struct inode
*ovl_get_inode(struct super_block
*sb
, struct inode
*realinode
)
361 inode
= iget5_locked(sb
, (unsigned long) realinode
,
362 ovl_inode_test
, ovl_inode_set
, realinode
);
363 if (inode
&& inode
->i_state
& I_NEW
) {
364 ovl_fill_inode(inode
, realinode
->i_mode
, realinode
->i_rdev
);
365 set_nlink(inode
, realinode
->i_nlink
);
366 unlock_new_inode(inode
);