[PATCH] fs/squashfs: Use kcalloc when relevant
authorMiquel Raynal <miquel.raynal@bootlin.com>
Mon, 27 Jun 2022 10:20:03 +0000 (12:20 +0200)
committerDaniel Leidert <dleidert@debian.org>
Wed, 30 Apr 2025 23:19:02 +0000 (01:19 +0200)
commitc7312cdbdc3ebf8bf44a7b78f20532113cbdd0bb
tree0374d8c9bdf3951a45e4142d70c32dd5d1255ec4
parent02d4c2334a437d04bb5d1df0e7a39d20cdc1935c
[PATCH] fs/squashfs: Use kcalloc when relevant

A crafted squashfs image could embed a huge number of empty metadata
blocks in order to make the amount of malloc()'d memory overflow and be
much smaller than expected. Because of this flaw, any random code
positioned at the right location in the squashfs image could be memcpy'd
from the squashfs structures into U-Boot code location while trying to
access the rearmost blocks, before being executed.

In order to prevent this vulnerability from being exploited in eg. a
secure boot environment, let's add a check over the amount of data
that is going to be allocated. Such a check could look like:

if (!elem_size || n > SIZE_MAX / elem_size)
        return NULL;

The right way to do it would be to enhance the calloc() implementation
but this is quite an impacting change for such a small fix. Another
solution would be to add the check before the malloc call in the
squashfs implementation, but this does not look right. So for now, let's
use the kcalloc() compatibility function from Linux, which has this
check.

Fixes: c5100613037 ("fs/squashfs: new filesystem")
Reported-by: Tatsuhiko Yasumatsu <Tatsuhiko.Yasumatsu@sony.com>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Tested-by: Tatsuhiko Yasumatsu <Tatsuhiko.Yasumatsu@sony.com>
Reviewed-By: Daniel Leidert <dleidert@debian.org>
Origin: https://github.com/u-boot/u-boot/commit/b6f4c757959f8850e1299a77c8e5713da78e8ec0
Bug: https://lists.denx.de/pipermail/u-boot/2022-June/487467.html
Bug-Debian-Security: https://security-tracker.debian.org/tracker/CVE-2022-33967
Bug-Freexian-Security: https://deb.freexian.com/extended-lts/tracker/CVE-2022-33967

Gbp-Pq: Name CVE-2022-33967.patch
fs/squashfs/sqfs.c