Skip to contentSkip to conversation

remoteproc: core: Format firmware carveout rsc->name with %.*s

PATCH Applied Ian Rogers 3 participants 3 messages Nov 19 – Nov 26 Remote processor (remoteproc)

lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

PATCH 1/1 remoteproc: core: Format firmware carveout rsc->name with %.*s

In rproc_handle_carveout(), rsc->name is a 32-byte u8 array inside the firmware resource table. It is passed directly as the printf format string to both rproc_find_carveout_by_name() and rproc_mem_entry_init() (which call vsnprintf() with zero varargs), and printed via "%s" in dev_dbg() without verifying NUL termination within sizeof(rsc->name). A malformed or corrupt firmware resource table containing '%' specifiers or lacking a NUL byte in rsc->name can trigger out-of-bounds reads or invalid vararg stack accesses.

Pass "%.*s" bounded by (int)sizeof(rsc->name) at all three sites in rproc_handle_carveout().

Found via an x86_64 LLVM=1 (Clang 21.1.8) allyesconfig audit with -Wformat-security enabled; fix and changelog drafted with an AI coding assistant. Verified with an ASan/UBSan C test and build-tested under LLVM=1 W=1 and -Wformat-security; not runtime-tested with remoteproc firmware images.

Fixes 72029c901a02 ("remoteproc: add helper function to allocate and init rproc_mem_entry struct")
Fixes ffa5f9c84810 ("remoteproc: modify rproc_handle_carveout to support pre-registered region")
---
 drivers/remoteproc/remoteproc_core.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/remoteproc/remoteproc_core.c +8 -4
index 23f8539a99bd..1b1743093edc 100644
@@ -837,14 +837,17 @@ static int rproc_handle_carveout(struct rproc *rproc,
 		return -EINVAL;
 	}
 
-	dev_dbg(dev, "carveout rsc: name: %s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n",
-		rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags);
+	dev_dbg(dev, "carveout rsc: name: %.*s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n",
+		(int)sizeof(rsc->name), rsc->name, rsc->da, rsc->pa, rsc->len,
+		rsc->flags);
 
 	/*
 	 * Check carveout rsc already part of a registered carveout,
 	 * Search by name, then check the da and length
 	 */
-	carveout = rproc_find_carveout_by_name(rproc, rsc->name);
+	carveout = rproc_find_carveout_by_name(rproc, "%.*s",
+					       (int)sizeof(rsc->name),
+					       rsc->name);
 
 	if (carveout) {
 		if (carveout->rsc_offset != FW_RSC_ADDR_ANY) {
@@ -866,7 +869,8 @@ static int rproc_handle_carveout(struct rproc *rproc,
 	/* Register carveout in list */
 	carveout = rproc_mem_entry_init(dev, NULL, 0, rsc->len, rsc->da,
 					rproc_alloc_carveout,
-					rproc_release_carveout, rsc->name);
+					rproc_release_carveout, "%.*s",
+					(int)sizeof(rsc->name), rsc->name);
 	if (!carveout) {
 		dev_err(dev, "Can't allocate memory entry structure\n");
 		return -ENOMEM;
Signature · 1 line
2.56.1.708.g77570a6a1d-goog
Reviewed-by Tested-by on v1 1/1
#1 lore raw

Hello Ian,

Quoted 9 lines · Ian Rogers

In rproc_handle_carveout(), rsc->name is a 32-byte u8 array inside the firmware resource table. It is passed directly as the printf format string to both rproc_find_carveout_by_name() and rproc_mem_entry_init() (which call vsnprintf() with zero varargs), and printed via "%s" in dev_dbg() without verifying NUL termination within sizeof(rsc->name). A malformed or corrupt firmware resource table containing '%' specifiers or lacking a NUL byte in rsc->name can trigger out-of-bounds reads or invalid vararg stack accesses.

Quoted 9 lines

Pass "%.*s" bounded by (int)sizeof(rsc->name) at all three sites in rproc_handle_carveout().

Found via an x86_64 LLVM=1 (Clang 21.1.8) allyesconfig audit with -Wformat-security enabled; fix and changelog drafted with an AI coding assistant. Verified with an ASan/UBSan C test and build-tested under LLVM=1 W=1 and -Wformat-security; not runtime-tested with remoteproc firmware images.

I tested this on a AM67A with a rogue firmware (with name not limited to 32 bytes) and can confirm both the out-of-bounds read and that this patch fixes it.

A note on a behavior that I observed and might be worth considering:

with a name of exaclty 32 bytes, the dmesg reports:

remoteproc remoteproc2: carveout rsc: name: BAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB, da 0x95000000, pa 0x95000000, len 0x10000, flags 0x0

while the debugfs prints the same carveout as:

Carveout memory entry:

        Name: BAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
        Virtual address: 000000005cc9fc76
        DMA address: 0x00000000a1000000
        Device address: 0x95000000
        Length: 0x10000 Bytes

i.e., with the last character truncated. This is because the name is forcibly zero-terminated inside rproc_mem_entry_init().

This obviously predates this patch and is basically harmless, since rproc_find_carveout_by_name() does the same truncation, but might be confusing while debugging (and limits the usable space for the carveout name to 31 bytes, not 32).

Quoted 46 lines
Fixes 72029c901a02 ("remoteproc: add helper function to allocate and init rproc_mem_entry struct")
Fixes ffa5f9c84810 ("remoteproc: modify rproc_handle_carveout to support pre-registered region")
---
 drivers/remoteproc/remoteproc_core.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/remoteproc/remoteproc_core.c +8 -4
index 23f8539a99bd..1b1743093edc 100644
@@ -837,14 +837,17 @@ static int rproc_handle_carveout(struct rproc *rproc,
 		return -EINVAL;
 	}
 
-	dev_dbg(dev, "carveout rsc: name: %s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n",
-		rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags);
+	dev_dbg(dev, "carveout rsc: name: %.*s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n",
+		(int)sizeof(rsc->name), rsc->name, rsc->da, rsc->pa, rsc->len,
+		rsc->flags);
 
 	/*
 	 * Check carveout rsc already part of a registered carveout,
 	 * Search by name, then check the da and length
 	 */
-	carveout = rproc_find_carveout_by_name(rproc, rsc->name);
+	carveout = rproc_find_carveout_by_name(rproc, "%.*s",
+					       (int)sizeof(rsc->name),
+					       rsc->name);
 
 	if (carveout) {
 		if (carveout->rsc_offset != FW_RSC_ADDR_ANY) {
@@ -866,7 +869,8 @@ static int rproc_handle_carveout(struct rproc *rproc,
 	/* Register carveout in list */
 	carveout = rproc_mem_entry_init(dev, NULL, 0, rsc->len, rsc->da,
 					rproc_alloc_carveout,
-					rproc_release_carveout, rsc->name);
+					rproc_release_carveout, "%.*s",
+					(int)sizeof(rsc->name), rsc->name);
 	if (!carveout) {
 		dev_err(dev, "Can't allocate memory entry structure\n");
 		return -ENOMEM;
Signature · 1 line
2.56.1.708.g77570a6a1d-goog

With the note above (that can be considered later):

Regards,
Francesco

maintainer Applied
#2 lore raw
In reply to Ian Rogers
Quoted 18 lines · Ian Rogers

In rproc_handle_carveout(), rsc->name is a 32-byte u8 array inside the firmware resource table. It is passed directly as the printf format string to both rproc_find_carveout_by_name() and rproc_mem_entry_init() (which call vsnprintf() with zero varargs), and printed via "%s" in dev_dbg() without verifying NUL termination within sizeof(rsc->name). A malformed or corrupt firmware resource table containing '%' specifiers or lacking a NUL byte in rsc->name can trigger out-of-bounds reads or invalid vararg stack accesses.

Pass "%.*s" bounded by (int)sizeof(rsc->name) at all three sites in rproc_handle_carveout().

Found via an x86_64 LLVM=1 (Clang 21.1.8) allyesconfig audit with -Wformat-security enabled; fix and changelog drafted with an AI coding assistant. Verified with an ASan/UBSan C test and build-tested under LLVM=1 W=1 and -Wformat-security; not runtime-tested with remoteproc firmware images.

Quoted 7 lines
Fixes 72029c901a02 ("remoteproc: add helper function to allocate and init rproc_mem_entry struct")
Fixes ffa5f9c84810 ("remoteproc: modify rproc_handle_carveout to support pre-registered region")
---
 drivers/remoteproc/remoteproc_core.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

Applied.

Thanks,
Mathieu

Quoted 38 lines
drivers/remoteproc/remoteproc_core.c +8 -4
index 23f8539a99bd..1b1743093edc 100644
@@ -837,14 +837,17 @@ static int rproc_handle_carveout(struct rproc *rproc,
 		return -EINVAL;
 	}
 
-	dev_dbg(dev, "carveout rsc: name: %s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n",
-		rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags);
+	dev_dbg(dev, "carveout rsc: name: %.*s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n",
+		(int)sizeof(rsc->name), rsc->name, rsc->da, rsc->pa, rsc->len,
+		rsc->flags);
 
 	/*
 	 * Check carveout rsc already part of a registered carveout,
 	 * Search by name, then check the da and length
 	 */
-	carveout = rproc_find_carveout_by_name(rproc, rsc->name);
+	carveout = rproc_find_carveout_by_name(rproc, "%.*s",
+					       (int)sizeof(rsc->name),
+					       rsc->name);
 
 	if (carveout) {
 		if (carveout->rsc_offset != FW_RSC_ADDR_ANY) {
@@ -866,7 +869,8 @@ static int rproc_handle_carveout(struct rproc *rproc,
 	/* Register carveout in list */
 	carveout = rproc_mem_entry_init(dev, NULL, 0, rsc->len, rsc->da,
 					rproc_alloc_carveout,
-					rproc_release_carveout, rsc->name);
+					rproc_release_carveout, "%.*s",
+					(int)sizeof(rsc->name), rsc->name);
 	if (!carveout) {
 		dev_err(dev, "Can't allocate memory entry structure\n");
 		return -ENOMEM;
Signature · 1 line
2.56.1.708.g77570a6a1d-goog