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 "overlayfs.h"
15 static int ovl_copy_up_truncate(struct dentry
*dentry
)
18 struct dentry
*parent
;
20 struct path lowerpath
;
22 parent
= dget_parent(dentry
);
23 err
= ovl_copy_up(parent
);
27 ovl_path_lower(dentry
, &lowerpath
);
28 err
= vfs_getattr(&lowerpath
, &stat
);
33 err
= ovl_copy_up_one(parent
, dentry
, &lowerpath
, &stat
);
40 int ovl_setattr(struct dentry
*dentry
, struct iattr
*attr
)
43 struct dentry
*upperdentry
;
46 * Check for permissions before trying to copy-up. This is redundant
47 * since it will be rechecked later by ->setattr() on upper dentry. But
48 * without this, copy-up can be triggered by just about anybody.
50 * We don't initialize inode->size, which just means that
51 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
52 * check for a swapfile (which this won't be anyway).
54 err
= inode_change_ok(dentry
->d_inode
, attr
);
58 err
= ovl_want_write(dentry
);
62 err
= ovl_copy_up(dentry
);
64 upperdentry
= ovl_dentry_upper(dentry
);
66 if (attr
->ia_valid
& (ATTR_KILL_SUID
|ATTR_KILL_SGID
))
67 attr
->ia_valid
&= ~ATTR_MODE
;
69 mutex_lock(&upperdentry
->d_inode
->i_mutex
);
70 err
= notify_change(upperdentry
, attr
, NULL
);
72 ovl_copyattr(upperdentry
->d_inode
, dentry
->d_inode
);
73 mutex_unlock(&upperdentry
->d_inode
->i_mutex
);
75 ovl_drop_write(dentry
);
80 static int ovl_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
85 ovl_path_real(dentry
, &realpath
);
86 return vfs_getattr(&realpath
, stat
);
89 int ovl_permission(struct inode
*inode
, int mask
)
92 struct dentry
*alias
= NULL
;
93 struct inode
*realinode
;
94 struct dentry
*realdentry
;
98 if (S_ISDIR(inode
->i_mode
)) {
99 oe
= inode
->i_private
;
100 } else if (mask
& MAY_NOT_BLOCK
) {
104 * For non-directories find an alias and get the info
107 alias
= d_find_any_alias(inode
);
111 oe
= alias
->d_fsdata
;
114 realdentry
= ovl_entry_real(oe
, &is_upper
);
116 /* Careful in RCU walk mode */
117 realinode
= ACCESS_ONCE(realdentry
->d_inode
);
119 WARN_ON(!(mask
& MAY_NOT_BLOCK
));
124 if (mask
& MAY_WRITE
) {
125 umode_t mode
= realinode
->i_mode
;
128 * Writes will always be redirected to upper layer, so
129 * ignore lower layer being read-only.
131 * If the overlay itself is read-only then proceed
132 * with the permission check, don't return EROFS.
133 * This will only happen if this is the lower layer of
136 * If upper fs becomes read-only after the overlay was
137 * constructed return EROFS to prevent modification of
141 if (is_upper
&& !IS_RDONLY(inode
) && IS_RDONLY(realinode
) &&
142 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
)))
146 err
= __inode_permission(realinode
, mask
);
153 struct ovl_link_data
{
154 struct dentry
*realdentry
;
158 static const char *ovl_follow_link(struct dentry
*dentry
, void **cookie
)
160 struct dentry
*realdentry
;
161 struct inode
*realinode
;
162 struct ovl_link_data
*data
= NULL
;
165 realdentry
= ovl_dentry_real(dentry
);
166 realinode
= realdentry
->d_inode
;
168 if (WARN_ON(!realinode
->i_op
->follow_link
))
169 return ERR_PTR(-EPERM
);
171 if (realinode
->i_op
->put_link
) {
172 data
= kmalloc(sizeof(struct ovl_link_data
), GFP_KERNEL
);
174 return ERR_PTR(-ENOMEM
);
175 data
->realdentry
= realdentry
;
178 ret
= realinode
->i_op
->follow_link(realdentry
, cookie
);
179 if (IS_ERR_OR_NULL(ret
)) {
185 data
->cookie
= *cookie
;
192 static void ovl_put_link(struct inode
*unused
, void *c
)
194 struct inode
*realinode
;
195 struct ovl_link_data
*data
= c
;
200 realinode
= data
->realdentry
->d_inode
;
201 realinode
->i_op
->put_link(realinode
, data
->cookie
);
205 static int ovl_readlink(struct dentry
*dentry
, char __user
*buf
, int bufsiz
)
207 struct path realpath
;
208 struct inode
*realinode
;
210 ovl_path_real(dentry
, &realpath
);
211 realinode
= realpath
.dentry
->d_inode
;
213 if (!realinode
->i_op
->readlink
)
216 touch_atime(&realpath
);
218 return realinode
->i_op
->readlink(realpath
.dentry
, buf
, bufsiz
);
222 bool ovl_is_private_xattr(const char *name
)
224 return strncmp(name
, OVL_XATTR_PRE_NAME
, OVL_XATTR_PRE_LEN
) == 0;
227 int ovl_setxattr(struct dentry
*dentry
, const char *name
,
228 const void *value
, size_t size
, int flags
)
231 struct dentry
*upperdentry
;
233 err
= ovl_want_write(dentry
);
238 if (ovl_is_private_xattr(name
))
241 err
= ovl_copy_up(dentry
);
245 upperdentry
= ovl_dentry_upper(dentry
);
246 err
= vfs_setxattr(upperdentry
, name
, value
, size
, flags
);
249 ovl_drop_write(dentry
);
254 static bool ovl_need_xattr_filter(struct dentry
*dentry
,
255 enum ovl_path_type type
)
257 if ((type
& (__OVL_PATH_PURE
| __OVL_PATH_UPPER
)) == __OVL_PATH_UPPER
)
258 return S_ISDIR(dentry
->d_inode
->i_mode
);
263 ssize_t
ovl_getxattr(struct dentry
*dentry
, const char *name
,
264 void *value
, size_t size
)
266 struct path realpath
;
267 enum ovl_path_type type
= ovl_path_real(dentry
, &realpath
);
269 if (ovl_need_xattr_filter(dentry
, type
) && ovl_is_private_xattr(name
))
272 return vfs_getxattr(realpath
.dentry
, name
, value
, size
);
275 static bool ovl_can_list(const char *s
)
277 /* List all non-trusted xatts */
278 if (strncmp(s
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
) != 0)
281 /* Never list trusted.overlay, list other trusted for superuser only */
282 return !ovl_is_private_xattr(s
) &&
283 ns_capable_noaudit(&init_user_ns
, CAP_SYS_ADMIN
);
286 ssize_t
ovl_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
288 struct path realpath
;
289 enum ovl_path_type type
= ovl_path_real(dentry
, &realpath
);
294 res
= vfs_listxattr(realpath
.dentry
, list
, size
);
295 if (res
<= 0 || size
== 0)
298 if (!ovl_need_xattr_filter(dentry
, type
))
301 /* filter out private xattrs */
302 for (s
= list
, len
= res
; len
;) {
303 size_t slen
= strnlen(s
, len
) + 1;
305 /* underlying fs providing us with an broken xattr list? */
306 if (WARN_ON(slen
> len
))
310 if (!ovl_can_list(s
)) {
312 memmove(s
, s
+ slen
, len
);
321 int ovl_removexattr(struct dentry
*dentry
, const char *name
)
324 struct path realpath
;
325 enum ovl_path_type type
= ovl_path_real(dentry
, &realpath
);
327 err
= ovl_want_write(dentry
);
332 if (ovl_need_xattr_filter(dentry
, type
) && ovl_is_private_xattr(name
))
335 if (!OVL_TYPE_UPPER(type
)) {
336 err
= vfs_getxattr(realpath
.dentry
, name
, NULL
, 0);
340 err
= ovl_copy_up(dentry
);
344 ovl_path_upper(dentry
, &realpath
);
347 err
= vfs_removexattr(realpath
.dentry
, name
);
349 ovl_drop_write(dentry
);
354 static bool ovl_open_need_copy_up(int flags
, enum ovl_path_type type
,
355 struct dentry
*realdentry
)
357 if (OVL_TYPE_UPPER(type
))
360 if (special_file(realdentry
->d_inode
->i_mode
))
363 if (!(OPEN_FMODE(flags
) & FMODE_WRITE
) && !(flags
& O_TRUNC
))
369 struct inode
*ovl_d_select_inode(struct dentry
*dentry
, unsigned file_flags
)
372 struct path realpath
;
373 enum ovl_path_type type
;
375 if (d_is_dir(dentry
))
376 return d_backing_inode(dentry
);
378 type
= ovl_path_real(dentry
, &realpath
);
379 if (ovl_open_need_copy_up(file_flags
, type
, realpath
.dentry
)) {
380 err
= ovl_want_write(dentry
);
384 if (file_flags
& O_TRUNC
)
385 err
= ovl_copy_up_truncate(dentry
);
387 err
= ovl_copy_up(dentry
);
388 ovl_drop_write(dentry
);
392 ovl_path_upper(dentry
, &realpath
);
395 if (realpath
.dentry
->d_flags
& DCACHE_OP_SELECT_INODE
)
396 return realpath
.dentry
->d_op
->d_select_inode(realpath
.dentry
, file_flags
);
398 return d_backing_inode(realpath
.dentry
);
401 static const struct inode_operations ovl_file_inode_operations
= {
402 .setattr
= ovl_setattr
,
403 .permission
= ovl_permission
,
404 .getattr
= ovl_getattr
,
405 .setxattr
= ovl_setxattr
,
406 .getxattr
= ovl_getxattr
,
407 .listxattr
= ovl_listxattr
,
408 .removexattr
= ovl_removexattr
,
411 static const struct inode_operations ovl_symlink_inode_operations
= {
412 .setattr
= ovl_setattr
,
413 .follow_link
= ovl_follow_link
,
414 .put_link
= ovl_put_link
,
415 .readlink
= ovl_readlink
,
416 .getattr
= ovl_getattr
,
417 .setxattr
= ovl_setxattr
,
418 .getxattr
= ovl_getxattr
,
419 .listxattr
= ovl_listxattr
,
420 .removexattr
= ovl_removexattr
,
423 struct inode
*ovl_new_inode(struct super_block
*sb
, umode_t mode
,
424 struct ovl_entry
*oe
)
428 inode
= new_inode(sb
);
432 inode
->i_ino
= get_next_ino();
433 inode
->i_mode
= mode
;
434 inode
->i_flags
|= S_NOATIME
| S_NOCMTIME
;
439 inode
->i_private
= oe
;
440 inode
->i_op
= &ovl_dir_inode_operations
;
441 inode
->i_fop
= &ovl_dir_operations
;
445 inode
->i_op
= &ovl_symlink_inode_operations
;
453 inode
->i_op
= &ovl_file_inode_operations
;
457 WARN(1, "illegal file type: %i\n", mode
);