[PATCH 04/10] remoteproc: elf_loader: Fix overflow in rproc_elf_load_segments()

From: Lee Jones

Date: Mon Nov 23 2026 - 12:22:20 EST


For 64-bit ELF firmware images, rproc_elf_load_segments() reads 64-bit
p_offset and p_filesz fields and checks offset + filesz > fw->size. A
malformed segment header where offset + filesz wraps around 64 bits
bypasses this truncation check, causing the subsequent segment copy
from elf_data + offset to read out of bounds outside the firmware
buffer.

Replace the addition with an overflow-safe subtraction check and log
offset and filesz individually when rejecting a truncated image.

Fixes: f31e339f1b9b ("remoteproc: Add elf64 support in elf loader")
Closes: https://sashiko.dev/#/bug/linux-754a0c40-c150-42df-9c0d-bbb2c3846fa7
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
---
drivers/remoteproc/remoteproc_elf_loader.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/remoteproc/remoteproc_elf_loader.c b/drivers/remoteproc/remoteproc_elf_loader.c
index da3cddbe7d4c..6b9de0627768 100644
--- a/drivers/remoteproc/remoteproc_elf_loader.c
+++ b/drivers/remoteproc/remoteproc_elf_loader.c
@@ -213,9 +213,9 @@ int rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
break;
}

- if (offset + filesz > fw->size) {
- dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n",
- offset + filesz, fw->size);
+ if (offset > fw->size || filesz > fw->size - offset) {
+ dev_err(dev, "truncated fw: offset 0x%llx filesz 0x%llx avail 0x%zx\n",
+ offset, filesz, fw->size);
ret = -EINVAL;
break;
}
--
2.56.1.750.gf70dc682cf-goog