Merge branch 'overlayfs-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszer...
authorLinus Torvalds <torvalds@linux-foundation.org>
Tue, 22 Mar 2016 20:11:15 +0000 (13:11 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Tue, 22 Mar 2016 20:11:15 +0000 (13:11 -0700)
Pull overlayfs updates from Miklos Szeredi:
 "Various fixes and tweaks"

* 'overlayfs-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs:
  ovl: cleanup unused var in rename2
  ovl: rename is_merge to is_lowest
  ovl: fixed coding style warning
  ovl: Ensure upper filesystem supports d_type
  ovl: Warn on copy up if a process has a R/O fd open to the lower file
  ovl: honor flag MS_SILENT at mount
  ovl: verify upper dentry before unlink and rename

fs/overlayfs/copy_up.c
fs/overlayfs/dir.c
fs/overlayfs/overlayfs.h
fs/overlayfs/readdir.c
fs/overlayfs/super.c

index d894e7cd9a864239ea19dc86d405239ee2405607..cc514da6f3e7bc973a5a8c6b2a739c57e8b51ebb 100644 (file)
@@ -7,6 +7,7 @@
  * the Free Software Foundation.
  */
 
+#include <linux/module.h>
 #include <linux/fs.h>
 #include <linux/slab.h>
 #include <linux/file.h>
 #include <linux/uaccess.h>
 #include <linux/sched.h>
 #include <linux/namei.h>
+#include <linux/fdtable.h>
+#include <linux/ratelimit.h>
 #include "overlayfs.h"
 
 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
 
+static bool __read_mostly ovl_check_copy_up;
+module_param_named(check_copy_up, ovl_check_copy_up, bool,
+                  S_IWUSR | S_IRUGO);
+MODULE_PARM_DESC(ovl_check_copy_up,
+                "Warn on copy-up when causing process also has a R/O fd open");
+
+static int ovl_check_fd(const void *data, struct file *f, unsigned int fd)
+{
+       const struct dentry *dentry = data;
+
+       if (f->f_inode == d_inode(dentry))
+               pr_warn_ratelimited("overlayfs: Warning: Copying up %pD, but open R/O on fd %u which will cease to be coherent [pid=%d %s]\n",
+                                   f, fd, current->pid, current->comm);
+       return 0;
+}
+
+/*
+ * Check the fds open by this process and warn if something like the following
+ * scenario is about to occur:
+ *
+ *     fd1 = open("foo", O_RDONLY);
+ *     fd2 = open("foo", O_RDWR);
+ */
+static void ovl_do_check_copy_up(struct dentry *dentry)
+{
+       if (ovl_check_copy_up)
+               iterate_fd(current->files, 0, ovl_check_fd, dentry);
+}
+
 int ovl_copy_xattr(struct dentry *old, struct dentry *new)
 {
        ssize_t list_size, size, value_size = 0;
@@ -235,6 +267,7 @@ static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
 
        if (S_ISREG(stat->mode)) {
                struct path upperpath;
+
                ovl_path_upper(dentry, &upperpath);
                BUG_ON(upperpath.dentry != NULL);
                upperpath.dentry = newdentry;
@@ -309,6 +342,8 @@ int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
        if (WARN_ON(!workdir))
                return -EROFS;
 
+       ovl_do_check_copy_up(lowerpath->dentry);
+
        ovl_path_upper(parent, &parentpath);
        upperdir = parentpath.dentry;
 
index 52f6de5d40a9211baa29a39fc2a178598a86a92f..b3fc0a35bf6242b4bc3cfc9144bcc2e3ae638713 100644 (file)
@@ -596,21 +596,25 @@ static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
 {
        struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
        struct inode *dir = upperdir->d_inode;
-       struct dentry *upper = ovl_dentry_upper(dentry);
+       struct dentry *upper;
        int err;
 
        inode_lock_nested(dir, I_MUTEX_PARENT);
+       upper = lookup_one_len(dentry->d_name.name, upperdir,
+                              dentry->d_name.len);
+       err = PTR_ERR(upper);
+       if (IS_ERR(upper))
+               goto out_unlock;
+
        err = -ESTALE;
-       if (upper->d_parent == upperdir) {
-               /* Don't let d_delete() think it can reset d_inode */
-               dget(upper);
+       if (upper == ovl_dentry_upper(dentry)) {
                if (is_dir)
                        err = vfs_rmdir(dir, upper);
                else
                        err = vfs_unlink(dir, upper, NULL);
-               dput(upper);
                ovl_dentry_version_inc(dentry->d_parent);
        }
+       dput(upper);
 
        /*
         * Keeping this dentry hashed would mean having to release
@@ -620,6 +624,7 @@ static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
         */
        if (!err)
                d_drop(dentry);
+out_unlock:
        inode_unlock(dir);
 
        return err;
@@ -714,7 +719,6 @@ static int ovl_rename2(struct inode *olddir, struct dentry *old,
        struct dentry *trap;
        bool old_opaque;
        bool new_opaque;
-       bool new_create = false;
        bool cleanup_whiteout = false;
        bool overwrite = !(flags & RENAME_EXCHANGE);
        bool is_dir = d_is_dir(old);
@@ -840,29 +844,38 @@ static int ovl_rename2(struct inode *olddir, struct dentry *old,
 
        trap = lock_rename(new_upperdir, old_upperdir);
 
-       olddentry = ovl_dentry_upper(old);
-       newdentry = ovl_dentry_upper(new);
-       if (newdentry) {
+
+       olddentry = lookup_one_len(old->d_name.name, old_upperdir,
+                                  old->d_name.len);
+       err = PTR_ERR(olddentry);
+       if (IS_ERR(olddentry))
+               goto out_unlock;
+
+       err = -ESTALE;
+       if (olddentry != ovl_dentry_upper(old))
+               goto out_dput_old;
+
+       newdentry = lookup_one_len(new->d_name.name, new_upperdir,
+                                  new->d_name.len);
+       err = PTR_ERR(newdentry);
+       if (IS_ERR(newdentry))
+               goto out_dput_old;
+
+       err = -ESTALE;
+       if (ovl_dentry_upper(new)) {
                if (opaquedir) {
-                       newdentry = opaquedir;
-                       opaquedir = NULL;
+                       if (newdentry != opaquedir)
+                               goto out_dput;
                } else {
-                       dget(newdentry);
+                       if (newdentry != ovl_dentry_upper(new))
+                               goto out_dput;
                }
        } else {
-               new_create = true;
-               newdentry = lookup_one_len(new->d_name.name, new_upperdir,
-                                          new->d_name.len);
-               err = PTR_ERR(newdentry);
-               if (IS_ERR(newdentry))
-                       goto out_unlock;
+               if (!d_is_negative(newdentry) &&
+                   (!new_opaque || !ovl_is_whiteout(newdentry)))
+                       goto out_dput;
        }
 
-       err = -ESTALE;
-       if (olddentry->d_parent != old_upperdir)
-               goto out_dput;
-       if (newdentry->d_parent != new_upperdir)
-               goto out_dput;
        if (olddentry == trap)
                goto out_dput;
        if (newdentry == trap)
@@ -925,6 +938,8 @@ static int ovl_rename2(struct inode *olddir, struct dentry *old,
 
 out_dput:
        dput(newdentry);
+out_dput_old:
+       dput(olddentry);
 out_unlock:
        unlock_rename(new_upperdir, old_upperdir);
 out_revert_creds:
index 99b4168c36ffb70bb37f81210ca1ce69883ae337..6a7090f4a4413010545bd808484b9a4cbb705c07 100644 (file)
@@ -166,6 +166,7 @@ extern const struct file_operations ovl_dir_operations;
 int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list);
 void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list);
 void ovl_cache_free(struct list_head *list);
+int ovl_check_d_type_supported(struct path *realpath);
 
 /* inode.c */
 int ovl_setattr(struct dentry *dentry, struct iattr *attr);
index fdaf28f75e12acf1f4dba023c31017c81747744b..6ec1e43a9a54af87d57edf8d1e5a228812f0bd61 100644 (file)
@@ -36,13 +36,14 @@ struct ovl_dir_cache {
 
 struct ovl_readdir_data {
        struct dir_context ctx;
-       bool is_merge;
+       bool is_lowest;
        struct rb_root root;
        struct list_head *list;
        struct list_head middle;
        struct ovl_cache_entry *first_maybe_whiteout;
        int count;
        int err;
+       bool d_type_supported;
 };
 
 struct ovl_dir_file {
@@ -139,9 +140,9 @@ static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
        return 0;
 }
 
-static int ovl_fill_lower(struct ovl_readdir_data *rdd,
-                         const char *name, int namelen,
-                         loff_t offset, u64 ino, unsigned int d_type)
+static int ovl_fill_lowest(struct ovl_readdir_data *rdd,
+                          const char *name, int namelen,
+                          loff_t offset, u64 ino, unsigned int d_type)
 {
        struct ovl_cache_entry *p;
 
@@ -193,10 +194,10 @@ static int ovl_fill_merge(struct dir_context *ctx, const char *name,
                container_of(ctx, struct ovl_readdir_data, ctx);
 
        rdd->count++;
-       if (!rdd->is_merge)
+       if (!rdd->is_lowest)
                return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
        else
-               return ovl_fill_lower(rdd, name, namelen, offset, ino, d_type);
+               return ovl_fill_lowest(rdd, name, namelen, offset, ino, d_type);
 }
 
 static int ovl_check_whiteouts(struct dentry *dir, struct ovl_readdir_data *rdd)
@@ -289,7 +290,7 @@ static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list)
                .ctx.actor = ovl_fill_merge,
                .list = list,
                .root = RB_ROOT,
-               .is_merge = false,
+               .is_lowest = false,
        };
        int idx, next;
 
@@ -306,7 +307,7 @@ static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list)
                         * allows offsets to be reasonably constant
                         */
                        list_add(&rdd.middle, rdd.list);
-                       rdd.is_merge = true;
+                       rdd.is_lowest = true;
                        err = ovl_dir_read(&realpath, &rdd);
                        list_del(&rdd.middle);
                }
@@ -577,3 +578,39 @@ void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list)
        }
        inode_unlock(upper->d_inode);
 }
+
+static int ovl_check_d_type(struct dir_context *ctx, const char *name,
+                         int namelen, loff_t offset, u64 ino,
+                         unsigned int d_type)
+{
+       struct ovl_readdir_data *rdd =
+               container_of(ctx, struct ovl_readdir_data, ctx);
+
+       /* Even if d_type is not supported, DT_DIR is returned for . and .. */
+       if (!strncmp(name, ".", namelen) || !strncmp(name, "..", namelen))
+               return 0;
+
+       if (d_type != DT_UNKNOWN)
+               rdd->d_type_supported = true;
+
+       return 0;
+}
+
+/*
+ * Returns 1 if d_type is supported, 0 not supported/unknown. Negative values
+ * if error is encountered.
+ */
+int ovl_check_d_type_supported(struct path *realpath)
+{
+       int err;
+       struct ovl_readdir_data rdd = {
+               .ctx.actor = ovl_check_d_type,
+               .d_type_supported = false,
+       };
+
+       err = ovl_dir_read(realpath, &rdd);
+       if (err)
+               return err;
+
+       return rdd.d_type_supported;
+}
index 619ad4b016d209adcb2b6649d6f654049c860b07..ef64984c9bbcec1f765ed0601dbfb9082bc44f42 100644 (file)
@@ -936,7 +936,8 @@ static int ovl_fill_super(struct super_block *sb, void *data, int silent)
 
        err = -EINVAL;
        if (!ufs->config.lowerdir) {
-               pr_err("overlayfs: missing 'lowerdir'\n");
+               if (!silent)
+                       pr_err("overlayfs: missing 'lowerdir'\n");
                goto out_free_config;
        }
 
@@ -1028,6 +1029,21 @@ static int ovl_fill_super(struct super_block *sb, void *data, int silent)
                        sb->s_flags |= MS_RDONLY;
                        ufs->workdir = NULL;
                }
+
+               /*
+                * Upper should support d_type, else whiteouts are visible.
+                * Given workdir and upper are on same fs, we can do
+                * iterate_dir() on workdir.
+                */
+               err = ovl_check_d_type_supported(&workpath);
+               if (err < 0)
+                       goto out_put_workdir;
+
+               if (!err) {
+                       pr_err("overlayfs: upper fs needs to support d_type.\n");
+                       err = -EINVAL;
+                       goto out_put_workdir;
+               }
        }
 
        err = -ENOMEM;