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;
23 parent = dget_parent(dentry);
24 err = ovl_copy_up(parent);
28 ovl_path_lower(dentry, &lowerpath);
29 err = vfs_getattr(&lowerpath, &stat);
34 err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
41 int ovl_setattr(struct dentry *dentry, struct iattr *attr)
44 struct dentry *upperdentry;
45 const struct cred *old_cred;
48 * Check for permissions before trying to copy-up. This is redundant
49 * since it will be rechecked later by ->setattr() on upper dentry. But
50 * without this, copy-up can be triggered by just about anybody.
52 * We don't initialize inode->size, which just means that
53 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
54 * check for a swapfile (which this won't be anyway).
56 err = inode_change_ok(dentry->d_inode, attr);
60 err = ovl_want_write(dentry);
64 if (attr->ia_valid & ATTR_SIZE) {
65 struct inode *realinode = d_inode(ovl_dentry_real(dentry));
68 if (atomic_read(&realinode->i_writecount) < 0)
72 err = ovl_copy_up(dentry);
74 struct inode *winode = NULL;
76 upperdentry = ovl_dentry_upper(dentry);
78 if (attr->ia_valid & ATTR_SIZE) {
79 winode = d_inode(upperdentry);
80 err = get_write_access(winode);
85 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
86 attr->ia_valid &= ~ATTR_MODE;
88 inode_lock(upperdentry->d_inode);
89 old_cred = ovl_override_creds(dentry->d_sb);
90 err = notify_change(upperdentry, attr, NULL);
91 revert_creds(old_cred);
93 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
94 inode_unlock(upperdentry->d_inode);
97 put_write_access(winode);
100 ovl_drop_write(dentry);
105 static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
108 struct path realpath;
109 const struct cred *old_cred;
112 ovl_path_real(dentry, &realpath);
113 old_cred = ovl_override_creds(dentry->d_sb);
114 err = vfs_getattr(&realpath, stat);
115 revert_creds(old_cred);
119 int ovl_permission(struct inode *inode, int mask)
122 struct inode *realinode = ovl_inode_real(inode, &is_upper);
123 const struct cred *old_cred;
126 /* Careful in RCU walk mode */
128 WARN_ON(!(mask & MAY_NOT_BLOCK));
133 * Check overlay inode with the creds of task and underlying inode
134 * with creds of mounter
136 err = generic_permission(inode, mask);
140 old_cred = ovl_override_creds(inode->i_sb);
141 if (!is_upper && !special_file(realinode->i_mode) && mask & MAY_WRITE) {
142 mask &= ~(MAY_WRITE | MAY_APPEND);
143 /* Make sure mounter can read file for copy up later */
146 err = inode_permission(realinode, mask);
147 revert_creds(old_cred);
152 static const char *ovl_get_link(struct dentry *dentry,
154 struct delayed_call *done)
156 struct dentry *realdentry;
157 struct inode *realinode;
158 const struct cred *old_cred;
162 return ERR_PTR(-ECHILD);
164 realdentry = ovl_dentry_real(dentry);
165 realinode = realdentry->d_inode;
167 if (WARN_ON(!realinode->i_op->get_link))
168 return ERR_PTR(-EPERM);
170 old_cred = ovl_override_creds(dentry->d_sb);
171 p = realinode->i_op->get_link(realdentry, realinode, done);
172 revert_creds(old_cred);
176 static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
178 struct path realpath;
179 struct inode *realinode;
180 const struct cred *old_cred;
183 ovl_path_real(dentry, &realpath);
184 realinode = realpath.dentry->d_inode;
186 if (!realinode->i_op->readlink)
189 old_cred = ovl_override_creds(dentry->d_sb);
190 err = realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
191 revert_creds(old_cred);
195 bool ovl_is_private_xattr(const char *name)
197 return strncmp(name, OVL_XATTR_PREFIX,
198 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
201 int ovl_xattr_set(struct dentry *dentry, const char *name, const void *value,
202 size_t size, int flags)
205 struct path realpath;
206 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
207 const struct cred *old_cred;
209 err = ovl_want_write(dentry);
213 if (!value && !OVL_TYPE_UPPER(type)) {
214 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
219 err = ovl_copy_up(dentry);
223 if (!OVL_TYPE_UPPER(type))
224 ovl_path_upper(dentry, &realpath);
226 old_cred = ovl_override_creds(dentry->d_sb);
228 err = vfs_setxattr(realpath.dentry, name, value, size, flags);
230 WARN_ON(flags != XATTR_REPLACE);
231 err = vfs_removexattr(realpath.dentry, name);
233 revert_creds(old_cred);
236 ovl_drop_write(dentry);
241 int ovl_xattr_get(struct dentry *dentry, const char *name,
242 void *value, size_t size)
244 struct dentry *realdentry = ovl_dentry_real(dentry);
246 const struct cred *old_cred;
248 old_cred = ovl_override_creds(dentry->d_sb);
249 res = vfs_getxattr(realdentry, name, value, size);
250 revert_creds(old_cred);
254 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
256 struct dentry *realdentry = ovl_dentry_real(dentry);
260 const struct cred *old_cred;
262 old_cred = ovl_override_creds(dentry->d_sb);
263 res = vfs_listxattr(realdentry, list, size);
264 revert_creds(old_cred);
265 if (res <= 0 || size == 0)
268 /* filter out private xattrs */
269 for (s = list, len = res; len;) {
270 size_t slen = strnlen(s, len) + 1;
272 /* underlying fs providing us with an broken xattr list? */
273 if (WARN_ON(slen > len))
277 if (ovl_is_private_xattr(s)) {
279 memmove(s, s + slen, len);
288 struct posix_acl *ovl_get_acl(struct inode *inode, int type)
290 struct inode *realinode = ovl_inode_real(inode, NULL);
291 const struct cred *old_cred;
292 struct posix_acl *acl;
294 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
297 if (!realinode->i_op->get_acl)
300 old_cred = ovl_override_creds(inode->i_sb);
301 acl = get_acl(realinode, type);
302 revert_creds(old_cred);
307 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
308 struct dentry *realdentry)
310 if (OVL_TYPE_UPPER(type))
313 if (special_file(realdentry->d_inode->i_mode))
316 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
322 int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
325 struct path realpath;
326 enum ovl_path_type type;
328 type = ovl_path_real(dentry, &realpath);
329 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
330 err = ovl_want_write(dentry);
332 if (file_flags & O_TRUNC)
333 err = ovl_copy_up_truncate(dentry);
335 err = ovl_copy_up(dentry);
336 ovl_drop_write(dentry);
343 int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
345 struct dentry *alias;
346 struct path upperpath;
348 if (!(flags & S_ATIME))
351 alias = d_find_any_alias(inode);
355 ovl_path_upper(alias, &upperpath);
356 if (upperpath.dentry) {
357 touch_atime(&upperpath);
358 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
366 static const struct inode_operations ovl_file_inode_operations = {
367 .setattr = ovl_setattr,
368 .permission = ovl_permission,
369 .getattr = ovl_getattr,
370 .setxattr = generic_setxattr,
371 .getxattr = generic_getxattr,
372 .listxattr = ovl_listxattr,
373 .removexattr = generic_removexattr,
374 .get_acl = ovl_get_acl,
375 .update_time = ovl_update_time,
378 static const struct inode_operations ovl_symlink_inode_operations = {
379 .setattr = ovl_setattr,
380 .get_link = ovl_get_link,
381 .readlink = ovl_readlink,
382 .getattr = ovl_getattr,
383 .setxattr = generic_setxattr,
384 .getxattr = generic_getxattr,
385 .listxattr = ovl_listxattr,
386 .removexattr = generic_removexattr,
387 .update_time = ovl_update_time,
390 static void ovl_fill_inode(struct inode *inode, umode_t mode)
392 inode->i_ino = get_next_ino();
393 inode->i_mode = mode;
394 inode->i_flags |= S_NOCMTIME;
395 #ifdef CONFIG_FS_POSIX_ACL
396 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
402 inode->i_op = &ovl_dir_inode_operations;
403 inode->i_fop = &ovl_dir_operations;
407 inode->i_op = &ovl_symlink_inode_operations;
411 WARN(1, "illegal file type: %i\n", mode);
419 inode->i_op = &ovl_file_inode_operations;
424 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode)
428 inode = new_inode(sb);
430 ovl_fill_inode(inode, mode);
435 static int ovl_inode_test(struct inode *inode, void *data)
437 return ovl_inode_real(inode, NULL) == data;
440 static int ovl_inode_set(struct inode *inode, void *data)
442 inode->i_private = (void *) (((unsigned long) data) | OVL_ISUPPER_MASK);
446 struct inode *ovl_get_inode(struct super_block *sb, struct inode *realinode)
451 inode = iget5_locked(sb, (unsigned long) realinode,
452 ovl_inode_test, ovl_inode_set, realinode);
453 if (inode && inode->i_state & I_NEW) {
454 ovl_fill_inode(inode, realinode->i_mode);
455 set_nlink(inode, realinode->i_nlink);
456 unlock_new_inode(inode);