Skip to contentSkip to conversation

ovl: explicitly initialize error in ovl_copy_xattr()

PATCH Applied [email protected] 3 participants 6 messages Jun 4, 2020 – Jun 5, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ovl: explicitly initialize error in ovl_copy_xattr()

Under certain circumstances (we found this out running Docker on a Clang-built kernel with CONFIG_INIT_STACK_ALL) ovl_copy_xattr() may return uninitialized value of |error| from ovl_copy_xattr(). It is then returned by ovl_create() to lookup_open(), which casts it to an invalid dentry pointer, that can be further read or written by the lookup_open() callers.

The uninitialized value is returned when all the xattr on the file are ovl_is_private_xattr(), which is actually a successful case, therefore we initialize |error| with 0.

---

The bug seem to date back to at least v4.1 where the annotation has been introduced (i.e. the compilers started noticing error could be used before being initialized). I hovever didn't try to prove that the problem is actually reproducible on such ancient kernels. We've seen it on a real machine running v4.4 as well.

v2:
-- Per Vivek Goyal's suggestion, changed |error| to be 0

---
 fs/overlayfs/copy_up.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/overlayfs/copy_up.c +1 -1
index 9709cf22cab3..07e0d1961e96 100644
@@ -47,7 +47,7 @@ int ovl_copy_xattr(struct dentry *old, struct dentry *new)
 {
 	ssize_t list_size, size, value_size = 0;
 	char *buf, *name, *value = NULL;
-	int uninitialized_var(error);
+	int error = 0;
 	size_t slen;
 
 	if (!(old->d_inode->i_opflags & IOP_XATTR) ||
Signature · 1 line
2.27.0.278.ge193c7cf3a9-goog
maintainer Applied
#1 lore raw
Quoted 19 lines · On Thu, Jun 4, 2020 at 10:43 AM <[email protected]> wrote:

Under certain circumstances (we found this out running Docker on a Clang-built kernel with CONFIG_INIT_STACK_ALL) ovl_copy_xattr() may return uninitialized value of |error| from ovl_copy_xattr(). It is then returned by ovl_create() to lookup_open(), which casts it to an invalid dentry pointer, that can be further read or written by the lookup_open() callers.

The uninitialized value is returned when all the xattr on the file are ovl_is_private_xattr(), which is actually a successful case, therefore we initialize |error| with 0.

---

Quoted 8 lines

The bug seem to date back to at least v4.1 where the annotation has been introduced (i.e. the compilers started noticing error could be used before being initialized). I hovever didn't try to prove that the problem is actually reproducible on such ancient kernels. We've seen it on a real machine running v4.4 as well.

v2:
-- Per Vivek Goyal's suggestion, changed |error| to be 0

Thanks, applied patch posted here (with your signed-off as well, since the patch is the same...):

https://lore.kernel.org/linux-unionfs/874ks212uj.fsf@m5Zedd9JOGzJrf0/

Miklos

Reviewed-by on v1 1/1
#3 lore raw
In reply to [email protected]
Quoted 7 lines · On Thu, Jun 04, 2020 at 10:42:45AM +0200, [email protected] wrote:

Under certain circumstances (we found this out running Docker on a Clang-built kernel with CONFIG_INIT_STACK_ALL) ovl_copy_xattr() may return uninitialized value of |error| from ovl_copy_xattr(). It is then returned by ovl_create() to lookup_open(), which casts it to an invalid dentry pointer, that can be further read or written by the lookup_open() callers.

Quoted 8 lines

The uninitialized value is returned when all the xattr on the file are ovl_is_private_xattr(), which is actually a successful case, therefore we initialize |error| with 0.

Please include a Fixes (more below) and Link tags for details to help guide backporting, then you don't need to bother with with "# 4.1", the -stable tools will figure it out with a "Fixes" tag.

Thanks for the v2!

Quoted 5 lines

The bug seem to date back to at least v4.1 where the annotation has been introduced (i.e. the compilers started noticing error could be used before being initialized). I hovever didn't try to prove that the problem is actually reproducible on such ancient kernels. We've seen it on a real machine running v4.4 as well.

It seems like it came from this, but that's v4.5:

Fixes e4ad29fa0d22 ("ovl: use a minimal buffer in ovl_copy_xattr")

What did you find in v4.1? It looks like error isn't uninitialized in v4.1:

int ovl_copy_xattr(struct dentry *old, struct dentry *new)

{
        ssize_t list_size, size;
        char *buf, *name, *value;
        int error;

        if (!old->d_inode->i_op->getxattr ||
            !new->d_inode->i_op->getxattr)
                return 0;

        list_size = vfs_listxattr(old, NULL, 0);
        if (list_size <= 0) {
                if (list_size == -EOPNOTSUPP)
                        return 0;
                return list_size;
        }

        buf = kzalloc(list_size, GFP_KERNEL);
        if (!buf)
                return -ENOMEM;

        error = -ENOMEM;

...

But v4.1.52 backported the above patch (e4ad29fa0d22), which is why I don't try to figure these things out manually. Once we find the commit, the tools will figure it out. I think you just need:

Fixes e4ad29fa0d22 ("ovl: use a minimal buffer in ovl_copy_xattr")

and things like v4.1.52 will get fixed (if anyone is actually doing updates for v4.1.z any more...)

Signature · 1 line
Kees Cook
Reviewed-by
#2 lore raw
In reply to Miklos Szeredi
Quoted 26 lines · Miklos Szeredi
Quoted 25 lines · On Thu, Jun 4, 2020 at 10:43 AM <[email protected]> wrote:

Under certain circumstances (we found this out running Docker on a Clang-built kernel with CONFIG_INIT_STACK_ALL) ovl_copy_xattr() may return uninitialized value of |error| from ovl_copy_xattr(). It is then returned by ovl_create() to lookup_open(), which casts it to an invalid dentry pointer, that can be further read or written by the lookup_open() callers.

The uninitialized value is returned when all the xattr on the file are ovl_is_private_xattr(), which is actually a successful case, therefore we initialize |error| with 0.

---

The bug seem to date back to at least v4.1 where the annotation has been introduced (i.e. the compilers started noticing error could be used before being initialized). I hovever didn't try to prove that the problem is actually reproducible on such ancient kernels. We've seen it on a real machine running v4.4 as well.

Quoted 7 lines
Quoted 2 lines

v2:
-- Per Vivek Goyal's suggestion, changed |error| to be 0

Thanks, applied patch posted here (with your signed-off as well, since the patch is the same...):

https://lore.kernel.org/linux-unionfs/874ks212uj.fsf@m5Zedd9JOGzJrf0/

Can you please add:

Fixes e4ad29fa0d22 ("ovl: use a minimal buffer in ovl_copy_xattr")

(and adjust the CC field to drop the "# 4.1" so tools can figure it out?)

Thanks!

Signature · 1 line
Kees Cook
#4 lore raw
In reply to Kees Cook
Quoted 33 lines · Kees Cook
Quoted 7 lines · On Thu, Jun 04, 2020 at 10:42:45AM +0200, [email protected] wrote:

Under certain circumstances (we found this out running Docker on a Clang-built kernel with CONFIG_INIT_STACK_ALL) ovl_copy_xattr() may return uninitialized value of |error| from ovl_copy_xattr(). It is then returned by ovl_create() to lookup_open(), which casts it to an invalid dentry pointer, that can be further read or written by the lookup_open() callers.

Quoted 8 lines

The uninitialized value is returned when all the xattr on the file are ovl_is_private_xattr(), which is actually a successful case, therefore we initialize |error| with 0.

Please include a Fixes (more below) and Link tags for details to help guide backporting, then you don't need to bother with with "# 4.1", the -stable tools will figure it out with a "Fixes" tag.

Thanks for the v2!

Quoted 5 lines

The bug seem to date back to at least v4.1 where the annotation has been introduced (i.e. the compilers started noticing error could be used before being initialized). I hovever didn't try to prove that the problem is actually reproducible on such ancient kernels. We've seen it on a real machine running v4.4 as well.

Quoted 3 lines

It seems like it came from this, but that's v4.5:

Fixes e4ad29fa0d22 ("ovl: use a minimal buffer in ovl_copy_xattr")

https://bugs.chromium.org/p/chromium/issues/detail?id=1050405 mentions v4.4.212. Your patch could've been slipped into that kernel as well.

Quoted 2 lines

What did you find in v4.1? It looks like error isn't uninitialized in v4.1:

The annotation appeared first in
https://elixir.bootlin.com/linux/v4.1.18/source/fs/overlayfs/copy_up.c#L27,
does that count as 4.1 or 4.2?

Quoted 6 lines

But v4.1.52 backported the above patch (e4ad29fa0d22), which is why I don't try to figure these things out manually. Once we find the commit, the tools will figure it out. I think you just need:

Fixes e4ad29fa0d22 ("ovl: use a minimal buffer in ovl_copy_xattr")

Sounds good!

maintainer
#5 lore raw
In reply to Kees Cook
Quoted 33 lines · Kees Cook
Quoted 7 lines · On Thu, Jun 04, 2020 at 10:42:45AM +0200, [email protected] wrote:

Under certain circumstances (we found this out running Docker on a Clang-built kernel with CONFIG_INIT_STACK_ALL) ovl_copy_xattr() may return uninitialized value of |error| from ovl_copy_xattr(). It is then returned by ovl_create() to lookup_open(), which casts it to an invalid dentry pointer, that can be further read or written by the lookup_open() callers.

Quoted 8 lines

The uninitialized value is returned when all the xattr on the file are ovl_is_private_xattr(), which is actually a successful case, therefore we initialize |error| with 0.

Please include a Fixes (more below) and Link tags for details to help guide backporting, then you don't need to bother with with "# 4.1", the -stable tools will figure it out with a "Fixes" tag.

Thanks for the v2!

Quoted 5 lines

The bug seem to date back to at least v4.1 where the annotation has been introduced (i.e. the compilers started noticing error could be used before being initialized). I hovever didn't try to prove that the problem is actually reproducible on such ancient kernels. We've seen it on a real machine running v4.4 as well.

Quoted 3 lines

It seems like it came from this, but that's v4.5:

Fixes e4ad29fa0d22 ("ovl: use a minimal buffer in ovl_copy_xattr")

I believe it's actually:

Fixes 0956254a2d5b ("ovl: don't copy up opaqueness")

That patch added the ovl_is_private_xattr() check in ovl_copy_xattr(), without which 'error' was always initilalized.

Thanks,
Miklos