[PATCH v5 01/13] efi/cper: Prevent buffer overflows in cper_dimm_err_location()
From: Pierre Gondois
Date: Mon Nov 30 2026 - 12:08:26 EST
Buffer overflows/truncated messages might happen in
cper_dimm_err_location(). To prevent them, add a length parameter to
cper_dimm_err_location() and use scnprintf() instead of snprintf()
to return the actual chars written to the buffer.
Fixes: ed27b5df3877 ("EDAC/ghes: Unify CPER memory error location reporting")
Signed-off-by: Pierre Gondois <pierre.gondois@xxxxxxx>
---
drivers/edac/ghes_edac.c | 9 ++++++---
drivers/firmware/efi/cper.c | 19 +++++++++----------
include/linux/cper.h | 2 +-
3 files changed, 16 insertions(+), 14 deletions(-)
diff --git a/drivers/edac/ghes_edac.c b/drivers/edac/ghes_edac.c
index d80c88818691..78eb9dccbbbe 100644
--- a/drivers/edac/ghes_edac.c
+++ b/drivers/edac/ghes_edac.c
@@ -276,6 +276,7 @@ static int ghes_edac_report_mem_error(struct notifier_block *nb,
struct mem_ctl_info *mci;
unsigned long sev = val;
struct ghes_pvt *pvt;
+ size_t len;
unsigned long flags;
char *p;
@@ -347,21 +348,23 @@ static int ghes_edac_report_mem_error(struct notifier_block *nb,
e->grain = ~mem_err->physical_addr_mask + 1;
/* Memory error location, mapped on e->location */
- p = e->location;
cper_mem_err_pack(mem_err, &cmem);
+
+ len = sizeof(e->location);
+ p = e->location;
p += cper_mem_err_location(&cmem, p);
if (mem_err->validation_bits & CPER_MEM_VALID_MODULE_HANDLE) {
struct dimm_info *dimm;
- p += cper_dimm_err_location(&cmem, p);
+ p += cper_dimm_err_location(&cmem, p, len - (p - e->location));
dimm = find_dimm_by_handle(mci, mem_err->mem_dev_handle);
if (dimm) {
e->top_layer = dimm->idx;
strscpy(e->label, dimm->label);
}
}
- if (p > e->location)
+ if (p > e->location && p[-1] == ' ')
*(p - 1) = '\0';
if (!*e->label)
diff --git a/drivers/firmware/efi/cper.c b/drivers/firmware/efi/cper.c
index 13b3e72c3b62..70df07a1c37d 100644
--- a/drivers/firmware/efi/cper.c
+++ b/drivers/firmware/efi/cper.c
@@ -353,22 +353,21 @@ int cper_mem_err_location(struct cper_mem_err_compact *mem, char *msg)
}
EXPORT_SYMBOL_GPL(cper_mem_err_location);
-int cper_dimm_err_location(struct cper_mem_err_compact *mem, char *msg)
+int cper_dimm_err_location(struct cper_mem_err_compact *mem, char *msg, size_t len)
{
- u32 len, n;
+ u32 n;
const char *bank = NULL, *device = NULL;
- if (!msg || !(mem->validation_bits & CPER_MEM_VALID_MODULE_HANDLE))
+ if (!msg || !len || !(mem->validation_bits & CPER_MEM_VALID_MODULE_HANDLE))
return 0;
- len = CPER_REC_LEN;
dmi_memdev_name(mem->mem_dev_handle, &bank, &device);
if (bank && device)
- n = snprintf(msg, len, "DIMM location: %s %s ", bank, device);
+ n = scnprintf(msg, len, "DIMM location: %s %s ", bank, device);
else
- n = snprintf(msg, len,
- "DIMM location: not present. DMI handle: 0x%.4x ",
- mem->mem_dev_handle);
+ n = scnprintf(msg, len,
+ "DIMM location: not present. DMI handle: 0x%.4x ",
+ mem->mem_dev_handle);
return n;
}
@@ -422,7 +421,7 @@ const char *cper_mem_err_unpack(struct trace_seq *p,
if (cper_mem_err_location(cmem, rcd_decode_str))
trace_seq_printf(p, "%s", rcd_decode_str);
- if (cper_dimm_err_location(cmem, rcd_decode_str))
+ if (cper_dimm_err_location(cmem, rcd_decode_str, sizeof(rcd_decode_str)))
trace_seq_printf(p, "%s", rcd_decode_str);
trace_seq_putc(p, '\0');
@@ -459,7 +458,7 @@ static void cper_print_mem(const char *pfx, const struct cper_sec_mem_err *mem,
printk("%s""error_type: %d, %s\n", pfx, etype,
cper_mem_err_type_str(etype));
}
- if (cper_dimm_err_location(&cmem, rcd_decode_str))
+ if (cper_dimm_err_location(&cmem, rcd_decode_str, sizeof(rcd_decode_str)))
printk("%s%s\n", pfx, rcd_decode_str);
}
diff --git a/include/linux/cper.h b/include/linux/cper.h
index 440b35e459e5..c0b2c98ead45 100644
--- a/include/linux/cper.h
+++ b/include/linux/cper.h
@@ -600,7 +600,7 @@ void cper_print_proc_arm(const char *pfx,
void cper_print_proc_ia(const char *pfx,
const struct cper_sec_proc_ia *proc);
int cper_mem_err_location(struct cper_mem_err_compact *mem, char *msg);
-int cper_dimm_err_location(struct cper_mem_err_compact *mem, char *msg);
+int cper_dimm_err_location(struct cper_mem_err_compact *mem, char *msg, size_t len);
struct acpi_hest_generic_status;
void cper_estatus_print(const char *pfx,
--
2.43.0