Skip to contentSkip to conversation

ovl: detect overlapping layers

PATCH Superseded Amir Goldstein 2 participants 4 messages Apr 2, 2019 – Apr 4, 2019 Overlay filesystem

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Amir Goldstein

author maintainer
#0 lore raw

PATCH 1/1 ovl: detect overlapping layers

Overlapping overlay layers are not supported and can cause unexpected behavior, but overlayfs does not currently check or warn about these configurations.

User is not supposed to specify the same directory for upper and lower dirs or for different lower layers and user is not supposed to specify directories that are descendants of each other for overlay layers, but that is exactly what this zysbot repro did:

    https://syzkaller.appspot.com/x/repro.syz?x=12c7a94f400000

Moving layer root directories into other layers while overlayfs is mounted could also result in unexpected behavior.

This commit places "traps" in the overlay inode hash table. Those traps are dummy overlay inodes that are hashed by the layers root inodes. On mount, the hash table trap entries are used to verify that overlay layers are not overlapping.
On lookup, the trap entries are used to verify that overlay layers root inodes have not been moved into other layers after mount.

Some examples:

$ ./run --ov --samefs -s
...
( mkdir -p base/upper/0/u base/upper/0/w base/lower lower upper mnt
  mount -o bind base/lower lower
  mount -o bind base/upper upper
  mount -t overlay none mnt ...
        -o lowerdir=lower,upperdir=upper/0/u,workdir=upper/0/w)
$ umount mnt
$ mount -t overlay none mnt ...
        -o lowerdir=base,upperdir=upper/0/u,workdir=upper/0/w
  [   94.434900] overlayfs: overlapping upperdir path
  mount: mount overlay on mnt failed: Too many levels of symbolic links
$ mount -t overlay none mnt ...
        -o lowerdir=upper/0/u,upperdir=upper/0/u,workdir=upper/0/w
  [  151.350132] overlayfs: conflicting lowerdir path (-17)
  mount: mount overlay on mnt failed: File exists
$ mount -t overlay none mnt ...
        -o lowerdir=lower:lower/a,upperdir=upper/0/u,workdir=upper/0/w
  [  201.205045] overlayfs: overlapping lowerdir path
  mount: mount overlay on mnt failed: Too many levels of symbolic links
$ mount -t overlay none mnt ...
        -o lowerdir=lower,upperdir=upper/0/u,workdir=upper/0/w
$ mv base/upper/0/ base/lower/
$ find mnt/0
  mnt/0
  mnt/0/w
  find: 'mnt/0/w/work': Too many levels of symbolic links
  find: 'mnt/0/u': Too many levels of symbolic links

---

Miklos,

As you and Vivek proposed, this patch eliminates some overlapping layers use cases, both at mount time and lookup time.

Thanks,
Amir.

 fs/overlayfs/inode.c     |  48 ++++++++++++++
 fs/overlayfs/namei.c     |   8 +++
 fs/overlayfs/overlayfs.h |   2 +
 fs/overlayfs/ovl_entry.h |   6 ++
 fs/overlayfs/super.c     | 135 ++++++++++++++++++++++++++++++++++-----
 5 files changed, 182 insertions(+), 17 deletions(-)
fs/overlayfs/inode.c +48 -0
index b48273e846ad..6cf80cad40b2 100644
@@ -777,6 +777,54 @@ struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
 	return inode;
 }
 
+bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir)
+{
+	struct inode *key = d_inode(dir);
+	struct inode *trap;
+	bool res;
+
+	trap = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
+	if (!trap)
+		return false;
+
+	res = IS_DEADDIR(trap) && !ovl_inode_upper(trap) &&
+				  !ovl_inode_lower(trap);
+
+	iput(trap);
+	return res;
+}
+
+/*
+ * Create an inode cache entry for layer root dir, that will intentionally
+ * fail ovl_verify_inode(), so any lookup that will find some layer root
+ * will fail.
+ */
+struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir)
+{
+	struct inode *key = d_inode(dir);
+	struct inode *trap;
+
+	if (!d_is_dir(dir))
+		return ERR_PTR(-ENOTDIR);
+
+	trap = iget5_locked(sb, (unsigned long) key, ovl_inode_test,
+			    ovl_inode_set, key);
+	if (!trap)
+		return ERR_PTR(-ENOMEM);
+
+	if (!(trap->i_state & I_NEW)) {
+		/* Conflicting layer roots? */
+		iput(trap);
+		return ERR_PTR(-EEXIST);
+	}
+
+	trap->i_mode = S_IFDIR;
+	trap->i_flags = S_DEAD;
+	unlock_new_inode(trap);
+
+	return trap;
+}
+
 /*
  * Does overlay inode need to be hashed by lower inode?
  */
fs/overlayfs/namei.c +8 -0
index efd372312ef1..badf039267a2 100644
@@ -18,6 +18,7 @@
 #include "overlayfs.h"
 
 struct ovl_lookup_data {
+	struct super_block *sb;
 	struct qstr name;
 	bool is_dir;
 	bool opaque;
@@ -244,6 +245,12 @@ static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
 		if (!d->metacopy || d->last)
 			goto out;
 	} else {
+		if (ovl_lookup_trap_inode(d->sb, this)) {
+			/* Caught in a trap of overlapping layers */
+			err = -ELOOP;
+			goto out_err;
+		}
+
 		if (last_element)
 			d->is_dir = true;
 		if (d->last)
@@ -819,6 +826,7 @@ struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
 	int err;
 	bool metacopy = false;
 	struct ovl_lookup_data d = {
+		.sb = dentry->d_sb,
 		.name = dentry->d_name,
 		.is_dir = false,
 		.opaque = false,
fs/overlayfs/overlayfs.h +2 -0
index d26efed9f80a..a1eaac913097 100644
@@ -376,6 +376,8 @@ struct ovl_inode_params {
 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev);
 struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
 			       bool is_upper);
+bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir);
+struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir);
 struct inode *ovl_get_inode(struct super_block *sb,
 			    struct ovl_inode_params *oip);
 static inline void ovl_copyattr(struct inode *from, struct inode *to)
fs/overlayfs/ovl_entry.h +6 -0
index ec237035333a..6ed1ace8f8b3 100644
@@ -29,6 +29,8 @@ struct ovl_sb {
 
 struct ovl_layer {
 	struct vfsmount *mnt;
+	/* Trap in ovl inode cache */
+	struct inode *trap;
 	struct ovl_sb *fs;
 	/* Index of this layer in fs root (upper idx == 0) */
 	int idx;
@@ -65,6 +67,10 @@ struct ovl_fs {
 	/* Did we take the inuse lock? */
 	bool upperdir_locked;
 	bool workdir_locked;
+	/* Traps in ovl inode cache */
+	struct inode *upperdir_trap;
+	struct inode *workdir_trap;
+	struct inode *indexdir_trap;
 	/* Inode numbers in all layers do not use the high xino_bits */
 	unsigned int xino_bits;
 };
fs/overlayfs/super.c +118 -17

270 lines not on this page · raw message

Signature · 1 line
2.17.1
#1 lore raw

On Tue, Apr 02, 2019 at 10:31:55PM +0300, Amir Goldstein wrote:
[..]

Quoted 16 lines

/*
* Does overlay inode need to be hashed by lower inode? */

fs/overlayfs/namei.c +1 -0
index efd372312ef1..badf039267a2 100644
@@ -18,6 +18,7 @@
 #include "overlayfs.h"
 
 struct ovl_lookup_data {
+	struct super_block *sb;
 	struct qstr name;
 	bool is_dir;
 	bool opaque;

fs/overlayfs/namei.c static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d, · line 253

Quoted 9 lines
fs/overlayfs/namei.c +6 -0
@@ -244,6 +245,12 @@ static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
 		if (!d->metacopy || d->last)
 			goto out;
 	} else {
+		if (ovl_lookup_trap_inode(d->sb, this)) {
+			/* Caught in a trap of overlapping layers */
+			err = -ELOOP;
+			goto out_err;
+		}
+

Hi Amir,

This idea of putting dummy inodes in overlay inode cache hashed by inode pointer of root inode of underlying layers seems very clever. Cost of checking overlapping layer also seems to be O(N) and that's good.

So if we want to detect this case of overlapping layers after the mount, then we have to pay the cost of this trap inode lookup for every dentry found in upper/lower layer

[..]

fs/overlayfs/namei.c static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,

Quoted 13 lines
fs/overlayfs/namei.c +13 -0
+static int ovl_setup_trap(struct super_block *sb, struct dentry *dir,
+			  struct inode **ptrap, const char *name)
+{
+	struct inode *trap;
+	int err;
+
+	trap = ovl_get_trap_inode(sb, dir);
+	err = PTR_ERR(trap);
+	if (IS_ERR(trap)) {
+		pr_err("overlayfs: conflicting %s path (%i)\n", name, err);
+		return err;
+	}
+

ovl_get_trap_inode() can fail for other reasons like -ENOMEM. Should we warn about conflicting path only on -EEXIST.

[..]

fs/overlayfs/namei.c static int ovl_fill_super(struct super_block *sb, void *data, int silent) · line 1557

Quoted 10 lines

static int ovl_fill_super(struct super_block *sb, void *data, int silent) {

fs/overlayfs/namei.c +3 -0
 	struct path upperpath = { };
@@ -1457,17 +1552,20 @@ static int ovl_fill_super(struct super_block *sb, void *data, int silent)
 	if (ofs->config.xino != OVL_XINO_OFF)
 		ofs->xino_bits = BITS_PER_LONG - 32;
 
+	/* alloc/destroy_inode needed for setting up traps in inode cache */
+	sb->s_op = &ovl_super_operations;
+

Passing super block pointer to routines outside overlay before sb initializaiton is complete, is it safe?

Thanks
Vivek

author maintainer
#2 lore raw
Quoted 31 lines · Vivek Goyal

On Tue, Apr 02, 2019 at 10:31:55PM +0300, Amir Goldstein wrote:
[..]

Quoted 16 lines

/*
* Does overlay inode need to be hashed by lower inode? */

fs/overlayfs/namei.c +1 -0
index efd372312ef1..badf039267a2 100644
@@ -18,6 +18,7 @@
 #include "overlayfs.h"
 
 struct ovl_lookup_data {
+     struct super_block *sb;
      struct qstr name;
      bool is_dir;
      bool opaque;

fs/overlayfs/namei.c static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d, · line 253

Quoted 9 lines
fs/overlayfs/namei.c +6 -0
@@ -244,6 +245,12 @@ static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
              if (!d->metacopy || d->last)
                      goto out;
      } else {
+             if (ovl_lookup_trap_inode(d->sb, this)) {
+                     /* Caught in a trap of overlapping layers */
+                     err = -ELOOP;
+                     goto out_err;
+             }
+

Hi Amir,

Quoted 8 lines

This idea of putting dummy inodes in overlay inode cache hashed by inode pointer of root inode of underlying layers seems very clever. Cost of checking overlapping layer also seems to be O(N) and that's good.

So if we want to detect this case of overlapping layers after the mount, then we have to pay the cost of this trap inode lookup for every dentry found in upper/lower layer

Doesn't seem so expensive compared to overlay directory lookup, even in the best case of upper/lower layer dentry in dcache.

Quoted 18 lines

[..]

fs/overlayfs/namei.c

Quoted 13 lines
fs/overlayfs/namei.c +13 -0
+static int ovl_setup_trap(struct super_block *sb, struct dentry *dir,
+                       struct inode **ptrap, const char *name)
+{
+     struct inode *trap;
+     int err;
+
+     trap = ovl_get_trap_inode(sb, dir);
+     err = PTR_ERR(trap);
+     if (IS_ERR(trap)) {
+             pr_err("overlayfs: conflicting %s path (%i)\n", name, err);
+             return err;
+     }
+

ovl_get_trap_inode() can fail for other reasons like -ENOMEM. Should we warn about conflicting path only on -EEXIST.

Agreed.

Quoted 8 lines

[..]

Quoted 7 lines

static int ovl_fill_super(struct super_block *sb, void *data, int silent)

 {
      struct path upperpath = { };
fs/overlayfs/namei.c +0 -0
@@ -1457,17 +1552,20 @@ static int ovl_fill_super(struct super_block *sb, void *data, int silent)
      if (ofs->config.xino != OVL_XINO_OFF)
              ofs->xino_bits = BITS_PER_LONG - 32;
 
Quoted 7 lines

fs/overlayfs/namei.c static int ovl_fill_super(struct super_block *sb, void *data, int silent) · line 1557

Quoted 3 lines
fs/overlayfs/namei.c +3 -0
+     /* alloc/destroy_inode needed for setting up traps in inode cache */
+     sb->s_op = &ovl_super_operations;
+

Passing super block pointer to routines outside overlay before sb initializaiton is complete, is it safe?

It ain't pretty, but as far as calling vfs inode helpers, d_make_root(ovl_new_inode(sb, ...
below does the same with only "slightly more" initialized sb.

I could do more of the sb initialization at this point.
In fact, setting sb->s_fs_info = ofs, at this point could save passing both sb and ofs to ovl_fill_super() helpers.

One more thing about order of sanity tests -
It would have been nice if ovl_verify_origin() was done after ovl_check_overlapping_layers(). As it is, when mounting with overlapping layers, we first store fh of overlapping lower in upper root xattr and only then fail the mount, leaving the upper dir unusable for another mount try.
I had to take special care of this case in the xfstest:
https://github.com/amir73il/xfstests/blob/overlayfs-devel/tests/overlay/065#L60

However, I've decided that this is a corner case not worth complicating the code over. Something that could be left for fsck.overlay.

Thanks,
Amir.

#3 lore raw
Quoted 89 lines · Amir Goldstein
Quoted 31 lines · Vivek Goyal

On Tue, Apr 02, 2019 at 10:31:55PM +0300, Amir Goldstein wrote:
[..]

Quoted 16 lines

/*
* Does overlay inode need to be hashed by lower inode? */

fs/overlayfs/namei.c +1 -0
index efd372312ef1..badf039267a2 100644
@@ -18,6 +18,7 @@
 #include "overlayfs.h"
 
 struct ovl_lookup_data {
+     struct super_block *sb;
      struct qstr name;
      bool is_dir;
      bool opaque;

fs/overlayfs/namei.c static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d, · line 253

Quoted 9 lines
fs/overlayfs/namei.c +6 -0
@@ -244,6 +245,12 @@ static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
              if (!d->metacopy || d->last)
                      goto out;
      } else {
+             if (ovl_lookup_trap_inode(d->sb, this)) {
+                     /* Caught in a trap of overlapping layers */
+                     err = -ELOOP;
+                     goto out_err;
+             }
+

Hi Amir,

Quoted 8 lines

This idea of putting dummy inodes in overlay inode cache hashed by inode pointer of root inode of underlying layers seems very clever. Cost of checking overlapping layer also seems to be O(N) and that's good.

So if we want to detect this case of overlapping layers after the mount, then we have to pay the cost of this trap inode lookup for every dentry found in upper/lower layer

Doesn't seem so expensive compared to overlay directory lookup, even in the best case of upper/lower layer dentry in dcache.

Quoted 18 lines

[..]

fs/overlayfs/namei.c

Quoted 13 lines
fs/overlayfs/namei.c +13 -0
+static int ovl_setup_trap(struct super_block *sb, struct dentry *dir,
+                       struct inode **ptrap, const char *name)
+{
+     struct inode *trap;
+     int err;
+
+     trap = ovl_get_trap_inode(sb, dir);
+     err = PTR_ERR(trap);
+     if (IS_ERR(trap)) {
+             pr_err("overlayfs: conflicting %s path (%i)\n", name, err);
+             return err;
+     }
+

ovl_get_trap_inode() can fail for other reasons like -ENOMEM. Should we warn about conflicting path only on -EEXIST.

Agreed.

Quoted 8 lines

[..]

Quoted 7 lines

static int ovl_fill_super(struct super_block *sb, void *data, int silent)

 {
      struct path upperpath = { };
fs/overlayfs/namei.c +0 -0
@@ -1457,17 +1552,20 @@ static int ovl_fill_super(struct super_block *sb, void *data, int silent)
      if (ofs->config.xino != OVL_XINO_OFF)
              ofs->xino_bits = BITS_PER_LONG - 32;
 
Quoted 7 lines

fs/overlayfs/namei.c static int ovl_fill_super(struct super_block *sb, void *data, int silent) · line 1557

Quoted 3 lines
fs/overlayfs/namei.c +3 -0
+     /* alloc/destroy_inode needed for setting up traps in inode cache */
+     sb->s_op = &ovl_super_operations;
+

Passing super block pointer to routines outside overlay before sb initializaiton is complete, is it safe?

It ain't pretty, but as far as calling vfs inode helpers, d_make_root(ovl_new_inode(sb, ...
below does the same with only "slightly more" initialized sb.

I could do more of the sb initialization at this point.
In fact, setting sb->s_fs_info = ofs, at this point could save passing both sb and ofs to ovl_fill_super() helpers.

Quoted 8 lines

One more thing about order of sanity tests -
It would have been nice if ovl_verify_origin() was done after ovl_check_overlapping_layers(). As it is, when mounting with overlapping layers, we first store fh of overlapping lower in upper root xattr and only then fail the mount, leaving the upper dir unusable for another mount try.
I had to take special care of this case in the xfstest:
https://github.com/amir73il/xfstests/blob/overlayfs-devel/tests/overlay/065#L60

Ok, so we set orgin on upper and if we later error out, then origin xattr on upper will be left intact. If user tries to mount again with different lower it will fail (as origin verification failed).

Why not introduce a boolean to keep track if orgin was set. And if it was cleanup it up in error path in out_free_oe.

Anyway, its fine either way. Also agreed it is a corner case.

Vivek