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 static int ovl_copy_up_truncate(struct dentry
*dentry
)
19 struct dentry
*parent
;
21 struct path lowerpath
;
22 const struct cred
*old_cred
;
24 parent
= dget_parent(dentry
);
25 err
= ovl_copy_up(parent
);
29 ovl_path_lower(dentry
, &lowerpath
);
31 old_cred
= ovl_override_creds(dentry
->d_sb
);
32 err
= vfs_getattr(&lowerpath
, &stat
);
35 err
= ovl_copy_up_one(parent
, dentry
, &lowerpath
, &stat
);
37 revert_creds(old_cred
);
44 int ovl_setattr(struct dentry
*dentry
, struct iattr
*attr
)
47 struct dentry
*upperdentry
;
48 const struct cred
*old_cred
;
51 * Check for permissions before trying to copy-up. This is redundant
52 * since it will be rechecked later by ->setattr() on upper dentry. But
53 * without this, copy-up can be triggered by just about anybody.
55 * We don't initialize inode->size, which just means that
56 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
57 * check for a swapfile (which this won't be anyway).
59 err
= setattr_prepare(dentry
, attr
);
63 err
= ovl_want_write(dentry
);
67 if (attr
->ia_valid
& ATTR_SIZE
) {
68 struct inode
*realinode
= d_inode(ovl_dentry_real(dentry
));
71 if (atomic_read(&realinode
->i_writecount
) < 0)
75 err
= ovl_copy_up(dentry
);
77 struct inode
*winode
= NULL
;
79 upperdentry
= ovl_dentry_upper(dentry
);
81 if (attr
->ia_valid
& ATTR_SIZE
) {
82 winode
= d_inode(upperdentry
);
83 err
= get_write_access(winode
);
88 if (attr
->ia_valid
& (ATTR_KILL_SUID
|ATTR_KILL_SGID
))
89 attr
->ia_valid
&= ~ATTR_MODE
;
91 inode_lock(upperdentry
->d_inode
);
92 old_cred
= ovl_override_creds(dentry
->d_sb
);
93 err
= notify_change(upperdentry
, attr
, NULL
);
94 revert_creds(old_cred
);
96 ovl_copyattr(upperdentry
->d_inode
, dentry
->d_inode
);
97 inode_unlock(upperdentry
->d_inode
);
100 put_write_access(winode
);
103 ovl_drop_write(dentry
);
108 static int ovl_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
111 struct path realpath
;
112 const struct cred
*old_cred
;
115 ovl_path_real(dentry
, &realpath
);
116 old_cred
= ovl_override_creds(dentry
->d_sb
);
117 err
= vfs_getattr(&realpath
, stat
);
118 revert_creds(old_cred
);
122 int ovl_permission(struct inode
*inode
, int mask
)
125 struct inode
*realinode
= ovl_inode_real(inode
, &is_upper
);
126 const struct cred
*old_cred
;
129 /* Careful in RCU walk mode */
131 WARN_ON(!(mask
& MAY_NOT_BLOCK
));
136 * Check overlay inode with the creds of task and underlying inode
137 * with creds of mounter
139 err
= generic_permission(inode
, mask
);
143 old_cred
= ovl_override_creds(inode
->i_sb
);
144 if (!is_upper
&& !special_file(realinode
->i_mode
) && mask
& MAY_WRITE
) {
145 mask
&= ~(MAY_WRITE
| MAY_APPEND
);
146 /* Make sure mounter can read file for copy up later */
149 err
= inode_permission(realinode
, mask
);
150 revert_creds(old_cred
);
155 static const char *ovl_get_link(struct dentry
*dentry
,
157 struct delayed_call
*done
)
159 const struct cred
*old_cred
;
163 return ERR_PTR(-ECHILD
);
165 old_cred
= ovl_override_creds(dentry
->d_sb
);
166 p
= vfs_get_link(ovl_dentry_real(dentry
), done
);
167 revert_creds(old_cred
);
171 bool ovl_is_private_xattr(const char *name
)
173 return strncmp(name
, OVL_XATTR_PREFIX
,
174 sizeof(OVL_XATTR_PREFIX
) - 1) == 0;
177 int ovl_xattr_set(struct dentry
*dentry
, const char *name
, const void *value
,
178 size_t size
, int flags
)
181 struct path realpath
;
182 enum ovl_path_type type
= ovl_path_real(dentry
, &realpath
);
183 const struct cred
*old_cred
;
185 err
= ovl_want_write(dentry
);
189 if (!value
&& !OVL_TYPE_UPPER(type
)) {
190 err
= vfs_getxattr(realpath
.dentry
, name
, NULL
, 0);
195 err
= ovl_copy_up(dentry
);
199 if (!OVL_TYPE_UPPER(type
))
200 ovl_path_upper(dentry
, &realpath
);
202 old_cred
= ovl_override_creds(dentry
->d_sb
);
204 err
= vfs_setxattr(realpath
.dentry
, name
, value
, size
, flags
);
206 WARN_ON(flags
!= XATTR_REPLACE
);
207 err
= vfs_removexattr(realpath
.dentry
, name
);
209 revert_creds(old_cred
);
212 ovl_drop_write(dentry
);
217 int ovl_xattr_get(struct dentry
*dentry
, const char *name
,
218 void *value
, size_t size
)
220 struct dentry
*realdentry
= ovl_dentry_real(dentry
);
222 const struct cred
*old_cred
;
224 old_cred
= ovl_override_creds(dentry
->d_sb
);
225 res
= vfs_getxattr(realdentry
, name
, value
, size
);
226 revert_creds(old_cred
);
230 static bool ovl_can_list(const char *s
)
232 /* List all non-trusted xatts */
233 if (strncmp(s
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) != 0)
236 /* Never list trusted.overlay, list other trusted for superuser only */
237 return !ovl_is_private_xattr(s
) &&
238 ns_capable_noaudit(&init_user_ns
, CAP_SYS_ADMIN
);
241 ssize_t
ovl_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
243 struct dentry
*realdentry
= ovl_dentry_real(dentry
);
247 const struct cred
*old_cred
;
249 old_cred
= ovl_override_creds(dentry
->d_sb
);
250 res
= vfs_listxattr(realdentry
, list
, size
);
251 revert_creds(old_cred
);
252 if (res
<= 0 || size
== 0)
255 /* filter out private xattrs */
256 for (s
= list
, len
= res
; len
;) {
257 size_t slen
= strnlen(s
, len
) + 1;
259 /* underlying fs providing us with an broken xattr list? */
260 if (WARN_ON(slen
> len
))
264 if (!ovl_can_list(s
)) {
266 memmove(s
, s
+ slen
, len
);
275 struct posix_acl
*ovl_get_acl(struct inode
*inode
, int type
)
277 struct inode
*realinode
= ovl_inode_real(inode
, NULL
);
278 const struct cred
*old_cred
;
279 struct posix_acl
*acl
;
281 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL
) || !IS_POSIXACL(realinode
))
284 old_cred
= ovl_override_creds(inode
->i_sb
);
285 acl
= get_acl(realinode
, type
);
286 revert_creds(old_cred
);
291 static bool ovl_open_need_copy_up(int flags
, enum ovl_path_type type
,
292 struct dentry
*realdentry
)
294 if (OVL_TYPE_UPPER(type
))
297 if (special_file(realdentry
->d_inode
->i_mode
))
300 if (!(OPEN_FMODE(flags
) & FMODE_WRITE
) && !(flags
& O_TRUNC
))
306 int ovl_open_maybe_copy_up(struct dentry
*dentry
, unsigned int file_flags
)
309 struct path realpath
;
310 enum ovl_path_type type
;
312 type
= ovl_path_real(dentry
, &realpath
);
313 if (ovl_open_need_copy_up(file_flags
, type
, realpath
.dentry
)) {
314 err
= ovl_want_write(dentry
);
316 if (file_flags
& O_TRUNC
)
317 err
= ovl_copy_up_truncate(dentry
);
319 err
= ovl_copy_up(dentry
);
320 ovl_drop_write(dentry
);
327 int ovl_update_time(struct inode
*inode
, struct timespec
*ts
, int flags
)
329 struct dentry
*alias
;
330 struct path upperpath
;
332 if (!(flags
& S_ATIME
))
335 alias
= d_find_any_alias(inode
);
339 ovl_path_upper(alias
, &upperpath
);
340 if (upperpath
.dentry
) {
341 touch_atime(&upperpath
);
342 inode
->i_atime
= d_inode(upperpath
.dentry
)->i_atime
;
350 static const struct inode_operations ovl_file_inode_operations
= {
351 .setattr
= ovl_setattr
,
352 .permission
= ovl_permission
,
353 .getattr
= ovl_getattr
,
354 .listxattr
= ovl_listxattr
,
355 .get_acl
= ovl_get_acl
,
356 .update_time
= ovl_update_time
,
359 static const struct inode_operations ovl_symlink_inode_operations
= {
360 .setattr
= ovl_setattr
,
361 .get_link
= ovl_get_link
,
362 .readlink
= generic_readlink
,
363 .getattr
= ovl_getattr
,
364 .listxattr
= ovl_listxattr
,
365 .update_time
= ovl_update_time
,
368 static void ovl_fill_inode(struct inode
*inode
, umode_t mode
)
370 inode
->i_ino
= get_next_ino();
371 inode
->i_mode
= mode
;
372 inode
->i_flags
|= S_NOCMTIME
;
373 #ifdef CONFIG_FS_POSIX_ACL
374 inode
->i_acl
= inode
->i_default_acl
= ACL_DONT_CACHE
;
380 inode
->i_op
= &ovl_dir_inode_operations
;
381 inode
->i_fop
= &ovl_dir_operations
;
385 inode
->i_op
= &ovl_symlink_inode_operations
;
389 WARN(1, "illegal file type: %i\n", mode
);
397 inode
->i_op
= &ovl_file_inode_operations
;
402 struct inode
*ovl_new_inode(struct super_block
*sb
, umode_t mode
)
406 inode
= new_inode(sb
);
408 ovl_fill_inode(inode
, mode
);
413 static int ovl_inode_test(struct inode
*inode
, void *data
)
415 return ovl_inode_real(inode
, NULL
) == data
;
418 static int ovl_inode_set(struct inode
*inode
, void *data
)
420 inode
->i_private
= (void *) (((unsigned long) data
) | OVL_ISUPPER_MASK
);
424 struct inode
*ovl_get_inode(struct super_block
*sb
, struct inode
*realinode
)
429 inode
= iget5_locked(sb
, (unsigned long) realinode
,
430 ovl_inode_test
, ovl_inode_set
, realinode
);
431 if (inode
&& inode
->i_state
& I_NEW
) {
432 ovl_fill_inode(inode
, realinode
->i_mode
);
433 set_nlink(inode
, realinode
->i_nlink
);
434 unlock_new_inode(inode
);