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 = inode_change_ok(dentry->d_inode, 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 struct dentry *realdentry;
160 struct inode *realinode;
161 const struct cred *old_cred;
165 return ERR_PTR(-ECHILD);
167 realdentry = ovl_dentry_real(dentry);
168 realinode = realdentry->d_inode;
170 if (WARN_ON(!realinode->i_op->get_link))
171 return ERR_PTR(-EPERM);
173 old_cred = ovl_override_creds(dentry->d_sb);
174 p = realinode->i_op->get_link(realdentry, realinode, done);
175 revert_creds(old_cred);
179 static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
181 struct path realpath;
182 struct inode *realinode;
183 const struct cred *old_cred;
186 ovl_path_real(dentry, &realpath);
187 realinode = realpath.dentry->d_inode;
189 if (!realinode->i_op->readlink)
192 old_cred = ovl_override_creds(dentry->d_sb);
193 err = realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
194 revert_creds(old_cred);
198 bool ovl_is_private_xattr(const char *name)
200 return strncmp(name, OVL_XATTR_PREFIX,
201 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
204 int ovl_xattr_set(struct dentry *dentry, const char *name, const void *value,
205 size_t size, int flags)
208 struct path realpath;
209 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
210 const struct cred *old_cred;
212 err = ovl_want_write(dentry);
216 if (!value && !OVL_TYPE_UPPER(type)) {
217 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
222 err = ovl_copy_up(dentry);
226 if (!OVL_TYPE_UPPER(type))
227 ovl_path_upper(dentry, &realpath);
229 old_cred = ovl_override_creds(dentry->d_sb);
231 err = vfs_setxattr(realpath.dentry, name, value, size, flags);
233 WARN_ON(flags != XATTR_REPLACE);
234 err = vfs_removexattr(realpath.dentry, name);
236 revert_creds(old_cred);
239 ovl_drop_write(dentry);
244 int ovl_xattr_get(struct dentry *dentry, const char *name,
245 void *value, size_t size)
247 struct dentry *realdentry = ovl_dentry_real(dentry);
249 const struct cred *old_cred;
251 old_cred = ovl_override_creds(dentry->d_sb);
252 res = vfs_getxattr(realdentry, name, value, size);
253 revert_creds(old_cred);
257 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
259 struct dentry *realdentry = ovl_dentry_real(dentry);
263 const struct cred *old_cred;
265 old_cred = ovl_override_creds(dentry->d_sb);
266 res = vfs_listxattr(realdentry, list, size);
267 revert_creds(old_cred);
268 if (res <= 0 || size == 0)
271 /* filter out private xattrs */
272 for (s = list, len = res; len;) {
273 size_t slen = strnlen(s, len) + 1;
275 /* underlying fs providing us with an broken xattr list? */
276 if (WARN_ON(slen > len))
280 if (ovl_is_private_xattr(s)) {
282 memmove(s, s + slen, len);
291 struct posix_acl *ovl_get_acl(struct inode *inode, int type)
293 struct inode *realinode = ovl_inode_real(inode, NULL);
294 const struct cred *old_cred;
295 struct posix_acl *acl;
297 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
300 if (!realinode->i_op->get_acl)
303 old_cred = ovl_override_creds(inode->i_sb);
304 acl = get_acl(realinode, type);
305 revert_creds(old_cred);
310 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
311 struct dentry *realdentry)
313 if (OVL_TYPE_UPPER(type))
316 if (special_file(realdentry->d_inode->i_mode))
319 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
325 int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
328 struct path realpath;
329 enum ovl_path_type type;
331 type = ovl_path_real(dentry, &realpath);
332 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
333 err = ovl_want_write(dentry);
335 if (file_flags & O_TRUNC)
336 err = ovl_copy_up_truncate(dentry);
338 err = ovl_copy_up(dentry);
339 ovl_drop_write(dentry);
346 int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
348 struct dentry *alias;
349 struct path upperpath;
351 if (!(flags & S_ATIME))
354 alias = d_find_any_alias(inode);
358 ovl_path_upper(alias, &upperpath);
359 if (upperpath.dentry) {
360 touch_atime(&upperpath);
361 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
369 static const struct inode_operations ovl_file_inode_operations = {
370 .setattr = ovl_setattr,
371 .permission = ovl_permission,
372 .getattr = ovl_getattr,
373 .setxattr = generic_setxattr,
374 .getxattr = generic_getxattr,
375 .listxattr = ovl_listxattr,
376 .removexattr = generic_removexattr,
377 .get_acl = ovl_get_acl,
378 .update_time = ovl_update_time,
381 static const struct inode_operations ovl_symlink_inode_operations = {
382 .setattr = ovl_setattr,
383 .get_link = ovl_get_link,
384 .readlink = ovl_readlink,
385 .getattr = ovl_getattr,
386 .setxattr = generic_setxattr,
387 .getxattr = generic_getxattr,
388 .listxattr = ovl_listxattr,
389 .removexattr = generic_removexattr,
390 .update_time = ovl_update_time,
393 static void ovl_fill_inode(struct inode *inode, umode_t mode)
395 inode->i_ino = get_next_ino();
396 inode->i_mode = mode;
397 inode->i_flags |= S_NOCMTIME;
398 #ifdef CONFIG_FS_POSIX_ACL
399 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
405 inode->i_op = &ovl_dir_inode_operations;
406 inode->i_fop = &ovl_dir_operations;
410 inode->i_op = &ovl_symlink_inode_operations;
414 WARN(1, "illegal file type: %i\n", mode);
422 inode->i_op = &ovl_file_inode_operations;
427 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode)
431 inode = new_inode(sb);
433 ovl_fill_inode(inode, mode);
438 static int ovl_inode_test(struct inode *inode, void *data)
440 return ovl_inode_real(inode, NULL) == data;
443 static int ovl_inode_set(struct inode *inode, void *data)
445 inode->i_private = (void *) (((unsigned long) data) | OVL_ISUPPER_MASK);
449 struct inode *ovl_get_inode(struct super_block *sb, struct inode *realinode)
454 inode = iget5_locked(sb, (unsigned long) realinode,
455 ovl_inode_test, ovl_inode_set, realinode);
456 if (inode && inode->i_state & I_NEW) {
457 ovl_fill_inode(inode, realinode->i_mode);
458 set_nlink(inode, realinode->i_nlink);
459 unlock_new_inode(inode);