Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ovl: redirect_dir=nofollow should not follow redirect for opaque lower

redirect_dir=nofollow should not follow a redirect. But in a specific
configuration it can still follow it. For example try this.

$ mkdir -p lower0 lower1/foo upper work merged
$ touch lower1/foo/lower-file.txt
$ setfattr -n "trusted.overlay.opaque" -v "y" lower1/foo
$ mount -t overlay -o lowerdir=lower1:lower0,workdir=work,upperdir=upper,redirect_dir=on none merged
$ cd merged
$ mv foo foo-renamed
$ umount merged

# mount again. This time with redirect_dir=nofollow
$ mount -t overlay -o lowerdir=lower1:lower0,workdir=work,upperdir=upper,redirect_dir=nofollow none merged
$ ls merged/foo-renamed/
# This lists lower-file.txt, while it should not have.

Basically, we are doing redirect check after we check for d.stop. And
if this is not last lower, and we find an opaque lower, d.stop will be
set.

ovl_lookup_single()
if (!d->last && ovl_is_opaquedir(this)) {
d->stop = d->opaque = true;
goto out;
}

To fix this, first check redirect is allowed. And after that check if
d.stop has been set or not.

Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
Fixes: 438c84c2f0c7 ("ovl: don't follow redirects if redirect_dir=off")
Cc: <stable@vger.kernel.org> #v4.15
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>

authored by

Vivek Goyal and committed by
Miklos Szeredi
d1fe96c0 b5095f24

+3 -3
+3 -3
fs/overlayfs/namei.c
··· 913 913 stack[ctr].layer = lower.layer; 914 914 ctr++; 915 915 916 - if (d.stop) 917 - break; 918 - 919 916 /* 920 917 * Following redirects can have security consequences: it's like 921 918 * a symlink into the lower layer without the permission checks. ··· 929 932 dentry); 930 933 goto out_put; 931 934 } 935 + 936 + if (d.stop) 937 + break; 932 938 933 939 if (d.redirect && d.redirect[0] == '/' && poe != roe) { 934 940 poe = roe;