[PATCH v4 09/12] efi/cper: Pass buffer length to DIMM location helpers
From: Pierre Gondois
Date: Fri Nov 13 2026 - 13:52:29 EST
Add a length parameter to:
- cper_dimm_err_location()
- cper_dimm_err2_location()
to prevent potential buffer overflows/truncated messages.
Signed-off-by: Pierre Gondois <pierre.gondois@xxxxxxx>
---
drivers/edac/ghes_edac.c | 15 ++++++++++-----
drivers/firmware/efi/cper.c | 20 +++++++++-----------
include/linux/cper.h | 4 ++--
3 files changed, 21 insertions(+), 18 deletions(-)
diff --git a/drivers/edac/ghes_edac.c b/drivers/edac/ghes_edac.c
index 1a963e554ff8..075328d1bb7d 100644
--- a/drivers/edac/ghes_edac.c
+++ b/drivers/edac/ghes_edac.c
@@ -308,6 +308,7 @@ static int ghes_edac_report_mem_error(struct notifier_block *nb,
struct edac_raw_error_desc *e;
struct mem_ctl_info *mci;
struct ghes_pvt *pvt;
+ size_t len;
char *p;
/*
@@ -352,21 +353,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)
@@ -422,6 +425,7 @@ static int ghes_edac_report_mem_error2(struct notifier_block *nb,
struct edac_raw_error_desc *e;
struct mem_ctl_info *mci;
struct ghes_pvt *pvt;
+ size_t len;
char *p;
/*
@@ -466,13 +470,14 @@ static int ghes_edac_report_mem_error2(struct notifier_block *nb,
e->grain = ~mem_err->physical_addr_mask + 1;
/* Memory error location, mapped on e->location */
+ len = sizeof(e->location);
p = e->location;
p += cper_mem_err2_location(mem_err, p);
if (mem_err->validation_bits & CPER_MEM2_VALID_MODULE_HANDLE) {
struct dimm_info *dimm = NULL;
- p += cper_dimm_err2_location(mem_err, p);
+ p += cper_dimm_err2_location(mem_err, p, len - (p - e->location));
if (mem_err->mem_dev_handle <= U16_MAX) {
dimm = find_dimm_by_handle(mci, mem_err->mem_dev_handle);
if (dimm) {
@@ -481,7 +486,7 @@ static int ghes_edac_report_mem_error2(struct notifier_block *nb,
}
}
}
- 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 d8587141f8b2..0c38ff65ee0b 100644
--- a/drivers/firmware/efi/cper.c
+++ b/drivers/firmware/efi/cper.c
@@ -353,15 +353,14 @@ 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 = scnprintf(msg, len, "DIMM location: %s %s ", bank, device);
@@ -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);
}
@@ -514,12 +513,12 @@ int cper_mem_err2_location(const struct cper_sec_mem_err2 *mem, char *msg)
}
EXPORT_SYMBOL_GPL(cper_mem_err2_location);
-int cper_dimm_err2_location(const struct cper_sec_mem_err2 *mem, char *msg)
+int cper_dimm_err2_location(const struct cper_sec_mem_err2 *mem, char *msg, size_t len)
{
- u32 len, n;
+ u32 n;
const char *bank = NULL, *device = NULL;
- if (!msg || !(mem->validation_bits & CPER_MEM2_VALID_MODULE_HANDLE))
+ if (!msg || !len || !(mem->validation_bits & CPER_MEM2_VALID_MODULE_HANDLE))
return 0;
/* SMBIOS handles are 16 bits; never truncate a Section 2 handle. */
@@ -529,7 +528,6 @@ int cper_dimm_err2_location(const struct cper_sec_mem_err2 *mem, char *msg)
pr_warn_ratelimited(FW_WARN "Memory Error Section 2 module handle 0x%x exceeds SMBIOS range\n",
mem->mem_dev_handle);
- len = CPER_REC_LEN;
if (bank && device)
n = scnprintf(msg, len, "DIMM location: %s %s ", bank, device);
else
@@ -560,7 +558,7 @@ static void cper_print_mem2(const char *pfx, const struct cper_sec_mem_err2 *mem
cper_mem_err_type_str(mem->error_type));
if (mem->validation_bits & CPER_MEM2_VALID_STATUS)
printk("%sstatus: 0x%02x\n", pfx, mem->status);
- if (cper_dimm_err2_location(mem, rcd_decode_str))
+ if (cper_dimm_err2_location(mem, 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 afbfb4e77f29..4338c216b1dc 100644
--- a/include/linux/cper.h
+++ b/include/linux/cper.h
@@ -655,10 +655,10 @@ 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);
int cper_mem_err2_location(const struct cper_sec_mem_err2 *mem, char *msg);
-int cper_dimm_err2_location(const struct cper_sec_mem_err2 *mem, char *msg);
+int cper_dimm_err2_location(const struct cper_sec_mem_err2 *mem, char *msg, size_t len);
struct acpi_hest_generic_status;
void cper_estatus_print(const char *pfx,
--
2.43.0