[PATCH 5/9] modpost: fix stack out-of-bounds read for unterminated PNP ids

From: lzhan011

Date: Mon Oct 05 2026 - 06:42:39 EST


From: lzhan011 <zhangleizhen645@xxxxxxxxx>

do_pnp_device_entry() and do_pnp_card_entry() copy the PNP id into a
local acpi_id[PNP_ID_LEN] buffer to upper-case it, and then print it with
"%s". A PNP id that uses all PNP_ID_LEN characters, e.g. "PNP0C0F1", is
accepted by the compiler without a terminating NUL (the kernel is built
with -Wno-unterminated-string-initialization), so the local copy is not
NUL-terminated and printing it reads past the end of the stack buffer:

AddressSanitizer: stack-buffer-overflow ... in printf_common
... do_pnp_device_entry scripts/mod/file2alias.c

Limit the printed length to the size of the id buffers.

Found by fuzzing modpost with ASan/UBSan.

Fixes: 72638f598ec9 ("PNP: fix broken pnp lowercasing for acpi module aliases")
Assisted-by: Claude:claude-opus-5-5 ASan UBSan
Signed-off-by: lzhan011 <zhangleizhen645@xxxxxxxxx>
---
scripts/mod/file2alias.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/scripts/mod/file2alias.c b/scripts/mod/file2alias.c
index 61a831c18..7685fb513 100644
--- a/scripts/mod/file2alias.c
+++ b/scripts/mod/file2alias.c
@@ -573,8 +573,9 @@ static void do_pnp_device_entry(struct module *mod, void *symval)
/* fix broken pnp bus lowercasing */
for (unsigned int i = 0; i < sizeof(acpi_id); i++)
acpi_id[i] = toupper((*id)[i]);
- module_alias_printf(mod, false, "pnp:d%s*", *id);
- module_alias_printf(mod, false, "acpi*:%s:*", acpi_id);
+ module_alias_printf(mod, false, "pnp:d%.*s*", (int)sizeof(*id), *id);
+ module_alias_printf(mod, false, "acpi*:%.*s:*",
+ (int)sizeof(acpi_id), acpi_id);
}

/* looks like: "pnp:dD" for every device of the card */
@@ -594,8 +595,9 @@ static void do_pnp_card_entry(struct module *mod, void *symval)
acpi_id[j] = toupper(id[j]);

/* add an individual alias for every device entry */
- module_alias_printf(mod, false, "pnp:d%s*", id);
- module_alias_printf(mod, false, "acpi*:%s:*", acpi_id);
+ module_alias_printf(mod, false, "pnp:d%.*s*", PNP_ID_LEN, id);
+ module_alias_printf(mod, false, "acpi*:%.*s:*",
+ PNP_ID_LEN, acpi_id);
}
}

--
2.34.1