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;
44 const struct cred *old_cred;
47 * Check for permissions before trying to copy-up. This is redundant
48 * since it will be rechecked later by ->setattr() on upper dentry. But
49 * without this, copy-up can be triggered by just about anybody.
51 * We don't initialize inode->size, which just means that
52 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
53 * check for a swapfile (which this won't be anyway).
55 err = inode_change_ok(dentry->d_inode, attr);
59 err = ovl_want_write(dentry);
63 if (attr->ia_valid & ATTR_SIZE) {
64 struct inode *realinode = d_inode(ovl_dentry_real(dentry));
67 if (atomic_read(&realinode->i_writecount) < 0)
71 err = ovl_copy_up(dentry);
73 struct inode *winode = NULL;
75 upperdentry = ovl_dentry_upper(dentry);
77 if (attr->ia_valid & ATTR_SIZE) {
78 winode = d_inode(upperdentry);
79 err = get_write_access(winode);
84 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
85 attr->ia_valid &= ~ATTR_MODE;
87 inode_lock(upperdentry->d_inode);
88 old_cred = ovl_override_creds(dentry->d_sb);
89 err = notify_change(upperdentry, attr, NULL);
90 revert_creds(old_cred);
92 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
93 inode_unlock(upperdentry->d_inode);
96 put_write_access(winode);
99 ovl_drop_write(dentry);
104 static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
107 struct path realpath;
108 const struct cred *old_cred;
111 ovl_path_real(dentry, &realpath);
112 old_cred = ovl_override_creds(dentry->d_sb);
113 err = vfs_getattr(&realpath, stat);
114 revert_creds(old_cred);
118 int ovl_permission(struct inode *inode, int mask)
120 struct ovl_entry *oe = inode->i_private;
122 struct dentry *realdentry = ovl_entry_real(oe, &is_upper);
123 struct inode *realinode;
124 const struct cred *old_cred;
127 if (ovl_is_default_permissions(inode)) {
129 struct path realpath = { .dentry = realdentry };
131 if (mask & MAY_NOT_BLOCK)
134 realpath.mnt = ovl_entry_mnt_real(oe, inode, is_upper);
136 err = vfs_getattr(&realpath, &stat);
140 if ((stat.mode ^ inode->i_mode) & S_IFMT)
143 inode->i_mode = stat.mode;
144 inode->i_uid = stat.uid;
145 inode->i_gid = stat.gid;
147 return generic_permission(inode, mask);
150 /* Careful in RCU walk mode */
151 realinode = d_inode_rcu(realdentry);
153 WARN_ON(!(mask & MAY_NOT_BLOCK));
157 if (mask & MAY_WRITE) {
158 umode_t mode = realinode->i_mode;
161 * Writes will always be redirected to upper layer, so
162 * ignore lower layer being read-only.
164 * If the overlay itself is read-only then proceed
165 * with the permission check, don't return EROFS.
166 * This will only happen if this is the lower layer of
169 * If upper fs becomes read-only after the overlay was
170 * constructed return EROFS to prevent modification of
173 if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
174 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
179 * Check overlay inode with the creds of task and underlying inode
180 * with creds of mounter
182 err = generic_permission(inode, mask);
186 old_cred = ovl_override_creds(inode->i_sb);
187 err = __inode_permission(realinode, mask);
188 revert_creds(old_cred);
193 static const char *ovl_get_link(struct dentry *dentry,
195 struct delayed_call *done)
197 struct dentry *realdentry;
198 struct inode *realinode;
199 const struct cred *old_cred;
203 return ERR_PTR(-ECHILD);
205 realdentry = ovl_dentry_real(dentry);
206 realinode = realdentry->d_inode;
208 if (WARN_ON(!realinode->i_op->get_link))
209 return ERR_PTR(-EPERM);
211 old_cred = ovl_override_creds(dentry->d_sb);
212 p = realinode->i_op->get_link(realdentry, realinode, done);
213 revert_creds(old_cred);
217 static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
219 struct path realpath;
220 struct inode *realinode;
221 const struct cred *old_cred;
224 ovl_path_real(dentry, &realpath);
225 realinode = realpath.dentry->d_inode;
227 if (!realinode->i_op->readlink)
230 touch_atime(&realpath);
232 old_cred = ovl_override_creds(dentry->d_sb);
233 err = realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
234 revert_creds(old_cred);
238 static bool ovl_is_private_xattr(const char *name)
240 return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
243 int ovl_setxattr(struct dentry *dentry, struct inode *inode,
244 const char *name, const void *value,
245 size_t size, int flags)
248 struct dentry *upperdentry;
249 const struct cred *old_cred;
251 err = ovl_want_write(dentry);
256 if (ovl_is_private_xattr(name))
259 err = ovl_copy_up(dentry);
263 upperdentry = ovl_dentry_upper(dentry);
264 old_cred = ovl_override_creds(dentry->d_sb);
265 err = vfs_setxattr(upperdentry, name, value, size, flags);
266 revert_creds(old_cred);
269 ovl_drop_write(dentry);
274 ssize_t ovl_getxattr(struct dentry *dentry, struct inode *inode,
275 const char *name, void *value, size_t size)
277 struct dentry *realdentry = ovl_dentry_real(dentry);
279 const struct cred *old_cred;
281 if (ovl_is_private_xattr(name))
284 old_cred = ovl_override_creds(dentry->d_sb);
285 res = vfs_getxattr(realdentry, name, value, size);
286 revert_creds(old_cred);
290 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
292 struct dentry *realdentry = ovl_dentry_real(dentry);
295 const struct cred *old_cred;
297 old_cred = ovl_override_creds(dentry->d_sb);
298 res = vfs_listxattr(realdentry, list, size);
299 revert_creds(old_cred);
300 if (res <= 0 || size == 0)
303 /* filter out private xattrs */
304 for (off = 0; off < res;) {
305 char *s = list + off;
306 size_t slen = strlen(s) + 1;
308 BUG_ON(off + slen > res);
310 if (ovl_is_private_xattr(s)) {
312 memmove(s, s + slen, res - off);
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);
326 const struct cred *old_cred;
328 err = ovl_want_write(dentry);
333 if (ovl_is_private_xattr(name))
336 if (!OVL_TYPE_UPPER(type)) {
337 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
341 err = ovl_copy_up(dentry);
345 ovl_path_upper(dentry, &realpath);
348 old_cred = ovl_override_creds(dentry->d_sb);
349 err = vfs_removexattr(realpath.dentry, name);
350 revert_creds(old_cred);
352 ovl_drop_write(dentry);
357 struct posix_acl *ovl_get_acl(struct inode *inode, int type)
359 struct inode *realinode = ovl_inode_real(inode);
360 const struct cred *old_cred;
361 struct posix_acl *acl;
363 if (!IS_POSIXACL(realinode))
366 if (!realinode->i_op->get_acl)
369 old_cred = ovl_override_creds(inode->i_sb);
370 acl = realinode->i_op->get_acl(realinode, type);
371 revert_creds(old_cred);
376 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
377 struct dentry *realdentry)
379 if (OVL_TYPE_UPPER(type))
382 if (special_file(realdentry->d_inode->i_mode))
385 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
391 int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
394 struct path realpath;
395 enum ovl_path_type type;
397 type = ovl_path_real(dentry, &realpath);
398 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
399 err = ovl_want_write(dentry);
401 if (file_flags & O_TRUNC)
402 err = ovl_copy_up_truncate(dentry);
404 err = ovl_copy_up(dentry);
405 ovl_drop_write(dentry);
412 static const struct inode_operations ovl_file_inode_operations = {
413 .setattr = ovl_setattr,
414 .permission = ovl_permission,
415 .getattr = ovl_getattr,
416 .setxattr = ovl_setxattr,
417 .getxattr = ovl_getxattr,
418 .listxattr = ovl_listxattr,
419 .removexattr = ovl_removexattr,
420 .get_acl = ovl_get_acl,
423 static const struct inode_operations ovl_symlink_inode_operations = {
424 .setattr = ovl_setattr,
425 .get_link = ovl_get_link,
426 .readlink = ovl_readlink,
427 .getattr = ovl_getattr,
428 .setxattr = ovl_setxattr,
429 .getxattr = ovl_getxattr,
430 .listxattr = ovl_listxattr,
431 .removexattr = ovl_removexattr,
434 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
435 struct ovl_entry *oe)
439 inode = new_inode(sb);
443 inode->i_ino = get_next_ino();
444 inode->i_mode = mode;
445 inode->i_flags |= S_NOATIME | S_NOCMTIME;
446 inode->i_private = oe;
451 inode->i_op = &ovl_dir_inode_operations;
452 inode->i_fop = &ovl_dir_operations;
456 inode->i_op = &ovl_symlink_inode_operations;
464 inode->i_op = &ovl_file_inode_operations;
468 WARN(1, "illegal file type: %i\n", mode);