Commit e7b4f2d8 authored by Linus Torvalds's avatar Linus Torvalds

Merge branch 'overlayfs-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs

Pull overlayfs update from Miklos Szeredi:
 "First of all, this fixes a regression in overlayfs introduced by the
  dentry hash salting.  I've moved the patch fixing this to the front of
  the queue, so if (god forbid) something needs to be bisected in
  overlayfs this regression won't interfere with that.

  The biggest part is preparation for selinux support, done by Vivek
  Goyal.  Essentially this makes all operations on underlying
  filesystems be done with credentials of mounter.  This makes
  everything nicely consistent.

  There are also fixes for a number of known and recently discovered
  non-standard behavior (thanks to Eryu Guan for testing and improving
  the test suites)"

* 'overlayfs-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs: (23 commits)
  ovl: simplify empty checking
  qstr: constify instances in overlayfs
  ovl: clear nlink on rmdir
  ovl: disallow overlayfs as upperdir
  ovl: fix warning
  ovl: remove duplicated include from super.c
  ovl: append MAY_READ when diluting write checks
  ovl: dilute permission checks on lower only if not special file
  ovl: fix POSIX ACL setting
  ovl: share inode for hard link
  ovl: store real inode pointer in ->i_private
  ovl: permission: return ECHILD instead of ENOENT
  ovl: update atime on upper
  ovl: fix sgid on directory
  ovl: simplify permission checking
  ovl: do not require mounter to have MAY_WRITE on lower
  ovl: do operations on underlying file system in mounter's context
  ovl: modify ovl_permission() to do checks on two inodes
  ovl: define ->get_acl() for overlay inodes
  ovl: move some common code in a function
  ...
parents 0a7736d0 30c17ebf
...@@ -292,6 +292,7 @@ static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir, ...@@ -292,6 +292,7 @@ static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
goto out_cleanup; goto out_cleanup;
ovl_dentry_update(dentry, newdentry); ovl_dentry_update(dentry, newdentry);
ovl_inode_update(d_inode(dentry), d_inode(newdentry));
newdentry = NULL; newdentry = NULL;
/* /*
......
...@@ -138,9 +138,12 @@ static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry, ...@@ -138,9 +138,12 @@ static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
int err; int err;
enum ovl_path_type type; enum ovl_path_type type;
struct path realpath; struct path realpath;
const struct cred *old_cred;
type = ovl_path_real(dentry, &realpath); type = ovl_path_real(dentry, &realpath);
old_cred = ovl_override_creds(dentry->d_sb);
err = vfs_getattr(&realpath, stat); err = vfs_getattr(&realpath, stat);
revert_creds(old_cred);
if (err) if (err)
return err; return err;
...@@ -158,6 +161,22 @@ static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry, ...@@ -158,6 +161,22 @@ static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
return 0; return 0;
} }
/* Common operations required to be done after creation of file on upper */
static void ovl_instantiate(struct dentry *dentry, struct inode *inode,
struct dentry *newdentry, bool hardlink)
{
ovl_dentry_version_inc(dentry->d_parent);
ovl_dentry_update(dentry, newdentry);
if (!hardlink) {
ovl_inode_update(inode, d_inode(newdentry));
ovl_copyattr(newdentry->d_inode, inode);
} else {
WARN_ON(ovl_inode_real(inode, NULL) != d_inode(newdentry));
inc_nlink(inode);
}
d_instantiate(dentry, inode);
}
static int ovl_create_upper(struct dentry *dentry, struct inode *inode, static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
struct kstat *stat, const char *link, struct kstat *stat, const char *link,
struct dentry *hardlink) struct dentry *hardlink)
...@@ -177,10 +196,7 @@ static int ovl_create_upper(struct dentry *dentry, struct inode *inode, ...@@ -177,10 +196,7 @@ static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
if (err) if (err)
goto out_dput; goto out_dput;
ovl_dentry_version_inc(dentry->d_parent); ovl_instantiate(dentry, inode, newdentry, !!hardlink);
ovl_dentry_update(dentry, newdentry);
ovl_copyattr(newdentry->d_inode, inode);
d_instantiate(dentry, inode);
newdentry = NULL; newdentry = NULL;
out_dput: out_dput:
dput(newdentry); dput(newdentry);
...@@ -291,23 +307,29 @@ static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry) ...@@ -291,23 +307,29 @@ static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry)
{ {
int err; int err;
struct dentry *ret = NULL; struct dentry *ret = NULL;
enum ovl_path_type type = ovl_path_type(dentry);
LIST_HEAD(list); LIST_HEAD(list);
err = ovl_check_empty_dir(dentry, &list); err = ovl_check_empty_dir(dentry, &list);
if (err) if (err) {
ret = ERR_PTR(err); ret = ERR_PTR(err);
else { goto out_free;
/*
* If no upperdentry then skip clearing whiteouts.
*
* Can race with copy-up, since we don't hold the upperdir
* mutex. Doesn't matter, since copy-up can't create a
* non-empty directory from an empty one.
*/
if (ovl_dentry_upper(dentry))
ret = ovl_clear_empty(dentry, &list);
} }
/*
* When removing an empty opaque directory, then it makes no sense to
* replace it with an exact replica of itself.
*
* If no upperdentry then skip clearing whiteouts.
*
* Can race with copy-up, since we don't hold the upperdir mutex.
* Doesn't matter, since copy-up can't create a non-empty directory
* from an empty one.
*/
if (OVL_TYPE_UPPER(type) && OVL_TYPE_MERGE(type))
ret = ovl_clear_empty(dentry, &list);
out_free:
ovl_cache_free(&list); ovl_cache_free(&list);
return ret; return ret;
...@@ -347,7 +369,23 @@ static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode, ...@@ -347,7 +369,23 @@ static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
if (err) if (err)
goto out_dput2; goto out_dput2;
if (S_ISDIR(stat->mode)) { /*
* mode could have been mutilated due to umask (e.g. sgid directory)
*/
if (!hardlink &&
!S_ISLNK(stat->mode) && newdentry->d_inode->i_mode != stat->mode) {
struct iattr attr = {
.ia_valid = ATTR_MODE,
.ia_mode = stat->mode,
};
inode_lock(newdentry->d_inode);
err = notify_change(newdentry, &attr, NULL);
inode_unlock(newdentry->d_inode);
if (err)
goto out_cleanup;
}
if (!hardlink && S_ISDIR(stat->mode)) {
err = ovl_set_opaque(newdentry); err = ovl_set_opaque(newdentry);
if (err) if (err)
goto out_cleanup; goto out_cleanup;
...@@ -363,10 +401,7 @@ static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode, ...@@ -363,10 +401,7 @@ static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
if (err) if (err)
goto out_cleanup; goto out_cleanup;
} }
ovl_dentry_version_inc(dentry->d_parent); ovl_instantiate(dentry, inode, newdentry, !!hardlink);
ovl_dentry_update(dentry, newdentry);
ovl_copyattr(newdentry->d_inode, inode);
d_instantiate(dentry, inode);
newdentry = NULL; newdentry = NULL;
out_dput2: out_dput2:
dput(upper); dput(upper);
...@@ -382,52 +417,42 @@ static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode, ...@@ -382,52 +417,42 @@ static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
goto out_dput2; goto out_dput2;
} }
static int ovl_create_or_link(struct dentry *dentry, int mode, dev_t rdev, static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
const char *link, struct dentry *hardlink) struct kstat *stat, const char *link,
struct dentry *hardlink)
{ {
int err; int err;
struct inode *inode; const struct cred *old_cred;
struct kstat stat = { struct cred *override_cred;
.mode = mode,
.rdev = rdev,
};
err = -ENOMEM;
inode = ovl_new_inode(dentry->d_sb, mode, dentry->d_fsdata);
if (!inode)
goto out;
err = ovl_copy_up(dentry->d_parent); err = ovl_copy_up(dentry->d_parent);
if (err) if (err)
goto out_iput; return err;
if (!ovl_dentry_is_opaque(dentry)) {
err = ovl_create_upper(dentry, inode, &stat, link, hardlink);
} else {
const struct cred *old_cred;
struct cred *override_cred;
old_cred = ovl_override_creds(dentry->d_sb);
err = -ENOMEM;
override_cred = prepare_creds();
if (override_cred) {
override_cred->fsuid = old_cred->fsuid;
override_cred->fsgid = old_cred->fsgid;
put_cred(override_creds(override_cred));
put_cred(override_cred);
err = ovl_create_over_whiteout(dentry, inode, &stat, old_cred = ovl_override_creds(dentry->d_sb);
link, hardlink); err = -ENOMEM;
} override_cred = prepare_creds();
revert_creds(old_cred); if (override_cred) {
override_cred->fsuid = inode->i_uid;
override_cred->fsgid = inode->i_gid;
put_cred(override_creds(override_cred));
put_cred(override_cred);
if (!ovl_dentry_is_opaque(dentry))
err = ovl_create_upper(dentry, inode, stat, link,
hardlink);
else
err = ovl_create_over_whiteout(dentry, inode, stat,
link, hardlink);
} }
revert_creds(old_cred);
if (!err) {
struct inode *realinode = d_inode(ovl_dentry_upper(dentry));
if (!err) WARN_ON(inode->i_mode != realinode->i_mode);
inode = NULL; WARN_ON(!uid_eq(inode->i_uid, realinode->i_uid));
out_iput: WARN_ON(!gid_eq(inode->i_gid, realinode->i_gid));
iput(inode); }
out:
return err; return err;
} }
...@@ -435,13 +460,30 @@ static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev, ...@@ -435,13 +460,30 @@ static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
const char *link) const char *link)
{ {
int err; int err;
struct inode *inode;
struct kstat stat = {
.rdev = rdev,
};
err = ovl_want_write(dentry); err = ovl_want_write(dentry);
if (!err) { if (err)
err = ovl_create_or_link(dentry, mode, rdev, link, NULL); goto out;
ovl_drop_write(dentry);
} err = -ENOMEM;
inode = ovl_new_inode(dentry->d_sb, mode);
if (!inode)
goto out_drop_write;
inode_init_owner(inode, dentry->d_parent->d_inode, mode);
stat.mode = inode->i_mode;
err = ovl_create_or_link(dentry, inode, &stat, link, NULL);
if (err)
iput(inode);
out_drop_write:
ovl_drop_write(dentry);
out:
return err; return err;
} }
...@@ -476,7 +518,7 @@ static int ovl_link(struct dentry *old, struct inode *newdir, ...@@ -476,7 +518,7 @@ static int ovl_link(struct dentry *old, struct inode *newdir,
struct dentry *new) struct dentry *new)
{ {
int err; int err;
struct dentry *upper; struct inode *inode;
err = ovl_want_write(old); err = ovl_want_write(old);
if (err) if (err)
...@@ -486,8 +528,12 @@ static int ovl_link(struct dentry *old, struct inode *newdir, ...@@ -486,8 +528,12 @@ static int ovl_link(struct dentry *old, struct inode *newdir,
if (err) if (err)
goto out_drop_write; goto out_drop_write;
upper = ovl_dentry_upper(old); inode = d_inode(old);
err = ovl_create_or_link(new, upper->d_inode->i_mode, 0, NULL, upper); ihold(inode);
err = ovl_create_or_link(new, inode, NULL, NULL, ovl_dentry_upper(old));
if (err)
iput(inode);
out_drop_write: out_drop_write:
ovl_drop_write(old); ovl_drop_write(old);
...@@ -511,24 +557,10 @@ static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir) ...@@ -511,24 +557,10 @@ static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir)
return -EROFS; return -EROFS;
if (is_dir) { if (is_dir) {
if (OVL_TYPE_MERGE_OR_LOWER(ovl_path_type(dentry))) { opaquedir = ovl_check_empty_and_clear(dentry);
opaquedir = ovl_check_empty_and_clear(dentry); err = PTR_ERR(opaquedir);
err = PTR_ERR(opaquedir); if (IS_ERR(opaquedir))
if (IS_ERR(opaquedir)) goto out;
goto out;
} else {
LIST_HEAD(list);
/*
* When removing an empty opaque directory, then it
* makes no sense to replace it with an exact replica of
* itself. But emptiness still needs to be checked.
*/
err = ovl_check_empty_dir(dentry, &list);
ovl_cache_free(&list);
if (err)
goto out;
}
} }
err = ovl_lock_rename_workdir(workdir, upperdir); err = ovl_lock_rename_workdir(workdir, upperdir);
...@@ -633,6 +665,8 @@ static int ovl_do_remove(struct dentry *dentry, bool is_dir) ...@@ -633,6 +665,8 @@ static int ovl_do_remove(struct dentry *dentry, bool is_dir)
{ {
enum ovl_path_type type; enum ovl_path_type type;
int err; int err;
const struct cred *old_cred;
err = ovl_check_sticky(dentry); err = ovl_check_sticky(dentry);
if (err) if (err)
...@@ -647,14 +681,18 @@ static int ovl_do_remove(struct dentry *dentry, bool is_dir) ...@@ -647,14 +681,18 @@ static int ovl_do_remove(struct dentry *dentry, bool is_dir)
goto out_drop_write; goto out_drop_write;
type = ovl_path_type(dentry); type = ovl_path_type(dentry);
if (OVL_TYPE_PURE_UPPER(type)) {
err = ovl_remove_upper(dentry, is_dir);
} else {
const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
old_cred = ovl_override_creds(dentry->d_sb);
if (OVL_TYPE_PURE_UPPER(type))
err = ovl_remove_upper(dentry, is_dir);
else
err = ovl_remove_and_whiteout(dentry, is_dir); err = ovl_remove_and_whiteout(dentry, is_dir);
revert_creds(old_cred);
revert_creds(old_cred); if (!err) {
if (is_dir)
clear_nlink(dentry->d_inode);
else
drop_nlink(dentry->d_inode);
} }
out_drop_write: out_drop_write:
ovl_drop_write(dentry); ovl_drop_write(dentry);
...@@ -760,8 +798,7 @@ static int ovl_rename2(struct inode *olddir, struct dentry *old, ...@@ -760,8 +798,7 @@ static int ovl_rename2(struct inode *olddir, struct dentry *old,
old_opaque = !OVL_TYPE_PURE_UPPER(old_type); old_opaque = !OVL_TYPE_PURE_UPPER(old_type);
new_opaque = !OVL_TYPE_PURE_UPPER(new_type); new_opaque = !OVL_TYPE_PURE_UPPER(new_type);
if (old_opaque || new_opaque) old_cred = ovl_override_creds(old->d_sb);
old_cred = ovl_override_creds(old->d_sb);
if (overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir) { if (overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir) {
opaquedir = ovl_check_empty_and_clear(new); opaquedir = ovl_check_empty_and_clear(new);
...@@ -891,8 +928,7 @@ static int ovl_rename2(struct inode *olddir, struct dentry *old, ...@@ -891,8 +928,7 @@ static int ovl_rename2(struct inode *olddir, struct dentry *old,
out_unlock: out_unlock:
unlock_rename(new_upperdir, old_upperdir); unlock_rename(new_upperdir, old_upperdir);
out_revert_creds: out_revert_creds:
if (old_opaque || new_opaque) revert_creds(old_cred);
revert_creds(old_cred);
out_drop_write: out_drop_write:
ovl_drop_write(old); ovl_drop_write(old);
out: out:
...@@ -913,8 +949,10 @@ const struct inode_operations ovl_dir_inode_operations = { ...@@ -913,8 +949,10 @@ const struct inode_operations ovl_dir_inode_operations = {
.mknod = ovl_mknod, .mknod = ovl_mknod,
.permission = ovl_permission, .permission = ovl_permission,
.getattr = ovl_dir_getattr, .getattr = ovl_dir_getattr,
.setxattr = ovl_setxattr, .setxattr = generic_setxattr,
.getxattr = ovl_getxattr, .getxattr = ovl_getxattr,
.listxattr = ovl_listxattr, .listxattr = ovl_listxattr,
.removexattr = ovl_removexattr, .removexattr = ovl_removexattr,
.get_acl = ovl_get_acl,
.update_time = ovl_update_time,
}; };
This diff is collapsed.
...@@ -23,9 +23,11 @@ enum ovl_path_type { ...@@ -23,9 +23,11 @@ enum ovl_path_type {
#define OVL_TYPE_MERGE_OR_LOWER(type) \ #define OVL_TYPE_MERGE_OR_LOWER(type) \
(OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type)) (OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type))
#define OVL_XATTR_PRE_NAME "trusted.overlay."
#define OVL_XATTR_PRE_LEN 16 #define OVL_XATTR_PREFIX XATTR_TRUSTED_PREFIX "overlay"
#define OVL_XATTR_OPAQUE OVL_XATTR_PRE_NAME"opaque" #define OVL_XATTR_OPAQUE OVL_XATTR_PREFIX ".opaque"
#define OVL_ISUPPER_MASK 1UL
static inline int ovl_do_rmdir(struct inode *dir, struct dentry *dentry) static inline int ovl_do_rmdir(struct inode *dir, struct dentry *dentry)
{ {
...@@ -131,6 +133,16 @@ static inline int ovl_do_whiteout(struct inode *dir, struct dentry *dentry) ...@@ -131,6 +133,16 @@ static inline int ovl_do_whiteout(struct inode *dir, struct dentry *dentry)
return err; return err;
} }
static inline struct inode *ovl_inode_real(struct inode *inode, bool *is_upper)
{
unsigned long x = (unsigned long) READ_ONCE(inode->i_private);
if (is_upper)
*is_upper = x & OVL_ISUPPER_MASK;
return (struct inode *) (x & ~OVL_ISUPPER_MASK);
}
enum ovl_path_type ovl_path_type(struct dentry *dentry); enum ovl_path_type ovl_path_type(struct dentry *dentry);
u64 ovl_dentry_version_get(struct dentry *dentry); u64 ovl_dentry_version_get(struct dentry *dentry);
void ovl_dentry_version_inc(struct dentry *dentry); void ovl_dentry_version_inc(struct dentry *dentry);
...@@ -141,11 +153,9 @@ int ovl_path_next(int idx, struct dentry *dentry, struct path *path); ...@@ -141,11 +153,9 @@ int ovl_path_next(int idx, struct dentry *dentry, struct path *path);
struct dentry *ovl_dentry_upper(struct dentry *dentry); struct dentry *ovl_dentry_upper(struct dentry *dentry);
struct dentry *ovl_dentry_lower(struct dentry *dentry); struct dentry *ovl_dentry_lower(struct dentry *dentry);
struct dentry *ovl_dentry_real(struct dentry *dentry); struct dentry *ovl_dentry_real(struct dentry *dentry);
struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper);
struct vfsmount *ovl_entry_mnt_real(struct ovl_entry *oe, struct inode *inode, struct vfsmount *ovl_entry_mnt_real(struct ovl_entry *oe, struct inode *inode,
bool is_upper); bool is_upper);
struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry); struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry);
bool ovl_is_default_permissions(struct inode *inode);
void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache); void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache);
struct dentry *ovl_workdir(struct dentry *dentry); struct dentry *ovl_workdir(struct dentry *dentry);
int ovl_want_write(struct dentry *dentry); int ovl_want_write(struct dentry *dentry);
...@@ -155,6 +165,7 @@ void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque); ...@@ -155,6 +165,7 @@ void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque);
bool ovl_is_whiteout(struct dentry *dentry); bool ovl_is_whiteout(struct dentry *dentry);
const struct cred *ovl_override_creds(struct super_block *sb); const struct cred *ovl_override_creds(struct super_block *sb);
void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry); void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry);
void ovl_inode_update(struct inode *inode, struct inode *upperinode);
struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
unsigned int flags); unsigned int flags);
struct file *ovl_path_open(struct path *path, int flags); struct file *ovl_path_open(struct path *path, int flags);
...@@ -179,15 +190,20 @@ ssize_t ovl_getxattr(struct dentry *dentry, struct inode *inode, ...@@ -179,15 +190,20 @@ ssize_t ovl_getxattr(struct dentry *dentry, struct inode *inode,
const char *name, void *value, size_t size); const char *name, void *value, size_t size);
ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size); ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
int ovl_removexattr(struct dentry *dentry, const char *name); int ovl_removexattr(struct dentry *dentry, const char *name);
struct posix_acl *ovl_get_acl(struct inode *inode, int type);
int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags); int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags);
int ovl_update_time(struct inode *inode, struct timespec *ts, int flags);
struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, struct inode *ovl_new_inode(struct super_block *sb, umode_t mode);
struct ovl_entry *oe); struct inode *ovl_get_inode(struct super_block *sb, struct inode *realinode);
static inline void ovl_copyattr(struct inode *from, struct inode *to) static inline void ovl_copyattr(struct inode *from, struct inode *to)
{ {
to->i_uid = from->i_uid; to->i_uid = from->i_uid;
to->i_gid = from->i_gid; to->i_gid = from->i_gid;
to->i_mode = from->i_mode; to->i_mode = from->i_mode;
to->i_atime = from->i_atime;
to->i_mtime = from->i_mtime;
to->i_ctime = from->i_ctime;
} }
/* dir.c */ /* dir.c */
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment