Skip to contentSkip to conversation

remoteproc: validate resource table offset count

PATCH In review Pengpeng Hou 2 participants 2 messages Mar 23 – Mar 25 Remote processor (remoteproc)

lore
Share & tools
lore
#0 lore raw

PATCH 6/7 remoteproc: validate resource table offset count

rproc_handle_resources() trusts table_ptr->num when it walks the offset[] array, but it does not first verify that the loaded resource table is large enough to actually contain that many offsets. A malformed firmware image can therefore force the loop to read past the end of the table header before any per-entry bounds checks run.

Reject resource tables whose offset array does not fit inside the loaded section.

---
 drivers/remoteproc/remoteproc_core.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/remoteproc/remoteproc_core.c +6 -0
index b087ed21858a..47fdfe5c2124 100644
@@ -1022,6 +1022,12 @@ static int rproc_handle_resources(struct rproc *rproc,
 	if (!rproc->table_ptr)
 		return 0;
 
+	if (struct_size(rproc->table_ptr, offset,
+			rproc->table_ptr->num) > rproc->table_sz) {
+		dev_err(dev, "resource table is truncated\n");
+		return -EINVAL;
+	}
+
 	for (i = 0; i < rproc->table_ptr->num; i++) {
 		int offset = rproc->table_ptr->offset[i];
 		struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset;
Signature · 1 line
2.50.1 (Apple Git-155)
maintainer
#1 lore raw

Good morning,

Quoted 20 lines · Pengpeng Hou

rproc_handle_resources() trusts table_ptr->num when it walks the offset[] array, but it does not first verify that the loaded resource table is large enough to actually contain that many offsets. A malformed firmware image can therefore force the loop to read past the end of the table header before any per-entry bounds checks run.

Reject resource tables whose offset array does not fit inside the loaded section.

---
 drivers/remoteproc/remoteproc_core.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/remoteproc/remoteproc_core.c +0 -0
index b087ed21858a..47fdfe5c2124 100644

drivers/remoteproc/remoteproc_core.c static int rproc_handle_resources(struct rproc *rproc, · line 1026

Quoted 5 lines
drivers/remoteproc/remoteproc_core.c +2 -0
@@ -1022,6 +1022,12 @@ static int rproc_handle_resources(struct rproc *rproc,
        if (!rproc->table_ptr)
                return 0;
 
+       if (struct_size(rproc->table_ptr, offset,
+                       rproc->table_ptr->num) > rproc->table_sz) {

This check is already present in here:
https://elixir.bootlin.com/linux/v7.0-rc5/source/drivers/remoteproc/remoteproc_elf_loader.c#L305

Thanks,
Mathieu

Quoted 10 lines
drivers/remoteproc/remoteproc_core.c +4 -1
+               dev_err(dev, "resource table is truncated\n");
+               return -EINVAL;
+       }
+
        for (i = 0; i < rproc->table_ptr->num; i++) {
                int offset = rproc->table_ptr->offset[i];
                struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset;
--

2.50.1 (Apple Git-155)