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(const struct path
*path
, struct kstat
*stat
,
61 u32 request_mask
, unsigned int flags
)
63 struct dentry
*dentry
= path
->dentry
;
65 const struct cred
*old_cred
;
68 ovl_path_real(dentry
, &realpath
);
69 old_cred
= ovl_override_creds(dentry
->d_sb
);
70 err
= vfs_getattr(&realpath
, stat
, request_mask
, flags
);
71 revert_creds(old_cred
);
75 int ovl_permission(struct inode
*inode
, int mask
)
78 struct inode
*realinode
= ovl_inode_real(inode
, &is_upper
);
79 const struct cred
*old_cred
;
82 /* Careful in RCU walk mode */
84 WARN_ON(!(mask
& MAY_NOT_BLOCK
));
89 * Check overlay inode with the creds of task and underlying inode
90 * with creds of mounter
92 err
= generic_permission(inode
, mask
);
96 old_cred
= ovl_override_creds(inode
->i_sb
);
97 if (!is_upper
&& !special_file(realinode
->i_mode
) && mask
& MAY_WRITE
) {
98 mask
&= ~(MAY_WRITE
| MAY_APPEND
);
99 /* Make sure mounter can read file for copy up later */
102 err
= inode_permission(realinode
, mask
);
103 revert_creds(old_cred
);
108 static const char *ovl_get_link(struct dentry
*dentry
,
110 struct delayed_call
*done
)
112 const struct cred
*old_cred
;
116 return ERR_PTR(-ECHILD
);
118 old_cred
= ovl_override_creds(dentry
->d_sb
);
119 p
= vfs_get_link(ovl_dentry_real(dentry
), done
);
120 revert_creds(old_cred
);
124 bool ovl_is_private_xattr(const char *name
)
126 return strncmp(name
, OVL_XATTR_PREFIX
,
127 sizeof(OVL_XATTR_PREFIX
) - 1) == 0;
130 int ovl_xattr_set(struct dentry
*dentry
, const char *name
, const void *value
,
131 size_t size
, int flags
)
134 struct path realpath
;
135 enum ovl_path_type type
= ovl_path_real(dentry
, &realpath
);
136 const struct cred
*old_cred
;
138 err
= ovl_want_write(dentry
);
142 if (!value
&& !OVL_TYPE_UPPER(type
)) {
143 err
= vfs_getxattr(realpath
.dentry
, name
, NULL
, 0);
148 err
= ovl_copy_up(dentry
);
152 if (!OVL_TYPE_UPPER(type
))
153 ovl_path_upper(dentry
, &realpath
);
155 old_cred
= ovl_override_creds(dentry
->d_sb
);
157 err
= vfs_setxattr(realpath
.dentry
, name
, value
, size
, flags
);
159 WARN_ON(flags
!= XATTR_REPLACE
);
160 err
= vfs_removexattr(realpath
.dentry
, name
);
162 revert_creds(old_cred
);
165 ovl_drop_write(dentry
);
170 int ovl_xattr_get(struct dentry
*dentry
, const char *name
,
171 void *value
, size_t size
)
173 struct dentry
*realdentry
= ovl_dentry_real(dentry
);
175 const struct cred
*old_cred
;
177 old_cred
= ovl_override_creds(dentry
->d_sb
);
178 res
= vfs_getxattr(realdentry
, name
, value
, size
);
179 revert_creds(old_cred
);
183 ssize_t
ovl_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
185 struct dentry
*realdentry
= ovl_dentry_real(dentry
);
189 const struct cred
*old_cred
;
191 old_cred
= ovl_override_creds(dentry
->d_sb
);
192 res
= vfs_listxattr(realdentry
, list
, size
);
193 revert_creds(old_cred
);
194 if (res
<= 0 || size
== 0)
197 /* filter out private xattrs */
198 for (s
= list
, len
= res
; len
;) {
199 size_t slen
= strnlen(s
, len
) + 1;
201 /* underlying fs providing us with an broken xattr list? */
202 if (WARN_ON(slen
> len
))
206 if (ovl_is_private_xattr(s
)) {
208 memmove(s
, s
+ slen
, len
);
217 struct posix_acl
*ovl_get_acl(struct inode
*inode
, int type
)
219 struct inode
*realinode
= ovl_inode_real(inode
, NULL
);
220 const struct cred
*old_cred
;
221 struct posix_acl
*acl
;
223 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL
) || !IS_POSIXACL(realinode
))
226 old_cred
= ovl_override_creds(inode
->i_sb
);
227 acl
= get_acl(realinode
, type
);
228 revert_creds(old_cred
);
233 static bool ovl_open_need_copy_up(int flags
, enum ovl_path_type type
,
234 struct dentry
*realdentry
)
236 if (OVL_TYPE_UPPER(type
))
239 if (special_file(realdentry
->d_inode
->i_mode
))
242 if (!(OPEN_FMODE(flags
) & FMODE_WRITE
) && !(flags
& O_TRUNC
))
248 int ovl_open_maybe_copy_up(struct dentry
*dentry
, unsigned int file_flags
)
251 struct path realpath
;
252 enum ovl_path_type type
;
254 type
= ovl_path_real(dentry
, &realpath
);
255 if (ovl_open_need_copy_up(file_flags
, type
, realpath
.dentry
)) {
256 err
= ovl_want_write(dentry
);
258 err
= ovl_copy_up_flags(dentry
, file_flags
);
259 ovl_drop_write(dentry
);
266 int ovl_update_time(struct inode
*inode
, struct timespec
*ts
, int flags
)
268 struct dentry
*alias
;
269 struct path upperpath
;
271 if (!(flags
& S_ATIME
))
274 alias
= d_find_any_alias(inode
);
278 ovl_path_upper(alias
, &upperpath
);
279 if (upperpath
.dentry
) {
280 touch_atime(&upperpath
);
281 inode
->i_atime
= d_inode(upperpath
.dentry
)->i_atime
;
289 static const struct inode_operations ovl_file_inode_operations
= {
290 .setattr
= ovl_setattr
,
291 .permission
= ovl_permission
,
292 .getattr
= ovl_getattr
,
293 .listxattr
= ovl_listxattr
,
294 .get_acl
= ovl_get_acl
,
295 .update_time
= ovl_update_time
,
298 static const struct inode_operations ovl_symlink_inode_operations
= {
299 .setattr
= ovl_setattr
,
300 .get_link
= ovl_get_link
,
301 .getattr
= ovl_getattr
,
302 .listxattr
= ovl_listxattr
,
303 .update_time
= ovl_update_time
,
306 static void ovl_fill_inode(struct inode
*inode
, umode_t mode
, dev_t rdev
)
308 inode
->i_ino
= get_next_ino();
309 inode
->i_mode
= mode
;
310 inode
->i_flags
|= S_NOCMTIME
;
311 #ifdef CONFIG_FS_POSIX_ACL
312 inode
->i_acl
= inode
->i_default_acl
= ACL_DONT_CACHE
;
315 switch (mode
& S_IFMT
) {
317 inode
->i_op
= &ovl_file_inode_operations
;
321 inode
->i_op
= &ovl_dir_inode_operations
;
322 inode
->i_fop
= &ovl_dir_operations
;
326 inode
->i_op
= &ovl_symlink_inode_operations
;
330 inode
->i_op
= &ovl_file_inode_operations
;
331 init_special_inode(inode
, mode
, rdev
);
336 struct inode
*ovl_new_inode(struct super_block
*sb
, umode_t mode
, dev_t rdev
)
340 inode
= new_inode(sb
);
342 ovl_fill_inode(inode
, mode
, rdev
);
347 static int ovl_inode_test(struct inode
*inode
, void *data
)
349 return ovl_inode_real(inode
, NULL
) == data
;
352 static int ovl_inode_set(struct inode
*inode
, void *data
)
354 inode
->i_private
= (void *) (((unsigned long) data
) | OVL_ISUPPER_MASK
);
358 struct inode
*ovl_get_inode(struct super_block
*sb
, struct inode
*realinode
)
363 inode
= iget5_locked(sb
, (unsigned long) realinode
,
364 ovl_inode_test
, ovl_inode_set
, realinode
);
365 if (inode
&& inode
->i_state
& I_NEW
) {
366 ovl_fill_inode(inode
, realinode
->i_mode
, realinode
->i_rdev
);
367 set_nlink(inode
, realinode
->i_nlink
);
368 unlock_new_inode(inode
);