[PATCH] mtd: parsers: tplink_safeloader: prevent out-of-bounds table reads

From: shao.mingyin

Date: Tue Dec 01 2026 - 09:51:44 EST


From: Shao Mingyin <shao.mingyin@xxxxxxxxxx>

The SafeLoader parser starts scanning the partition table at offset 4.
The read helper allocates size + 1 bytes and writes a NUL at buf[size],
but the scan does not check its starting offset against the table length.

If size is less than 4, the first sscanf() starts past the buffer. If
the last entry is followed directly by the NUL instead of a newline, the
bytes + 1 step skips the NUL and can put the next sscanf() past the
buffer. Both cases can cause a KASAN slab-out-of-bounds report in
vsscanf().

Return the table length from the read helper and check offset < size
before calling sscanf(). The parser still accepts a final entry without
a trailing newline.

Fixes: 00a3588084be ("mtd: parsers: add TP-Link SafeLoader partitions table parser")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Shao Mingyin <shao.mingyin@xxxxxxxxxx>
---
Tested on next-20261130 (QEMU/TCG aarch64 virt guest) with a test-only
hook (not part of this patch) that registers a RAM-backed MTD device whose
image holds a crafted safeloader table; the hook only feeds data to the
parser. The parser is architecture-independent and was built on arm64 by
relaxing its Kconfig dependency in the test tree only.

A table whose last entry has no trailing newline advances the scan to
offset == size + 1, and a table shorter than
TPLINK_SAFELOADER_DATA_OFFSET starts the scan past the allocation. With
KASAN the guest reports a slab-out-of-bounds read of size 1 in vsscanf()
called from mtd_parser_tplink_safeloader_parse():

table unpatched patched
no final newline (85) 0 bytes past the 86-byte allocation 2 partitions
two-byte table (2) 1 byte past the 3-byte allocation 0 partitions

Kernel next-20261130 (253f86660c1f); arm64 defconfig with CONFIG_MTD=y,
CONFIG_MTD_PARSER_TPLINK_SAFELOADER=y and CONFIG_KASAN=y; built with
aarch64-linux-gnu-gcc 13.3.0 and run on QEMU 8.2.2 (TCG).

drivers/mtd/parsers/tplink_safeloader.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/drivers/mtd/parsers/tplink_safeloader.c b/drivers/mtd/parsers/tplink_safeloader.c
index b770f41ef874..cd4c66623d9b 100644
--- a/drivers/mtd/parsers/tplink_safeloader.c
+++ b/drivers/mtd/parsers/tplink_safeloader.c
@@ -18,7 +18,8 @@ struct safeloader_cmn_header {
uint32_t unused;
} __packed;

-static void *mtd_parser_tplink_safeloader_read_table(struct mtd_info *mtd)
+static void *mtd_parser_tplink_safeloader_read_table(struct mtd_info *mtd,
+ size_t *table_size)
{
struct safeloader_cmn_header hdr;
struct device_node *np;
@@ -61,6 +62,8 @@ static void *mtd_parser_tplink_safeloader_read_table(struct mtd_info *mtd)

of_node_put(np);

+ *table_size = size;
+
return buf;

err_kfree:
@@ -78,6 +81,7 @@ static int mtd_parser_tplink_safeloader_parse(struct mtd_info *mtd,
char name[65];
size_t offset;
size_t bytes;
+ size_t size;
char *buf;
int idx;
int err;
@@ -88,14 +92,14 @@ static int mtd_parser_tplink_safeloader_parse(struct mtd_info *mtd,
goto err_out;
}

- buf = mtd_parser_tplink_safeloader_read_table(mtd);
+ buf = mtd_parser_tplink_safeloader_read_table(mtd, &size);
if (!buf) {
err = -ENOENT;
goto err_free_parts;
}

for (idx = 0, offset = TPLINK_SAFELOADER_DATA_OFFSET;
- idx < TPLINK_SAFELOADER_MAX_PARTS &&
+ idx < TPLINK_SAFELOADER_MAX_PARTS && offset < size &&
sscanf(buf + offset, "partition %64s base 0x%llx size 0x%llx%zn\n",
name, &parts[idx].offset, &parts[idx].size, &bytes) == 3;
idx++, offset += bytes + 1) {

base-commit: 253f86660c1f882fbf554db5eaa34bc5d66dc7e8
--
2.43.0