Skip to contentSkip to conversation

remoteproc: elf_loader: bound the section header table

PATCH In review HyeongJun An 2 participants 5 messages Jul 24 – Aug 20

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 remoteproc: elf_loader: bound the section header table

The rproc_elf_sanity_check() only checks the image is big enough to hold one section header. But find_table() walks e_shnum of them, and first dereferences the header at e_shstrndx to locate the section name table. Both fields are u16 and both come from the image, so an e_shstrndx of 65535 reads about 4 MB past the buffer.

The commit 9f9967fed9d0 ("soc: qcom: mdt_loader: Ensure we don't read past the ELF header") added the same check to the MDT loader, and says the header "is sanitized beforehand" under remoteproc. That is what this patch makes true.

Check e_shoff against the image size first, so the two bounds can use size_add() without an e_shoff above SIZE_MAX wrapping on 32-bit. The bounds are separate because e_shnum may be zero while find_table() still reads the e_shstrndx header.

Fixes 400e64df6b23 ("remoteproc: add framework for controlling remote processors")
Assisted-by Claude:claude-opus-4-8
---
 drivers/remoteproc/remoteproc_elf_loader.c | 23 ++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
drivers/remoteproc/remoteproc_elf_loader.c +21 -2
index 94177e416047..da3cddbe7d4c 100644
@@ -46,8 +46,9 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
 	struct elf32_hdr *ehdr;
 	u32 elf_shdr_get_size;
 	u64 phoff, shoff;
+	size_t shend;
 	char class;
-	u16 phnum;
+	u16 phnum, shnum, shstrndx;
 
 	if (!fw) {
 		dev_err(dev, "failed to load %s\n", name);
@@ -90,9 +91,27 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
 	phoff = elf_hdr_get_e_phoff(class, fw->data);
 	shoff = elf_hdr_get_e_shoff(class, fw->data);
 	phnum =  elf_hdr_get_e_phnum(class, fw->data);
+	shnum = elf_hdr_get_e_shnum(class, fw->data);
+	shstrndx = elf_hdr_get_e_shstrndx(class, fw->data);
 	elf_shdr_get_size = elf_size_of_shdr(class);
 
-	if (fw->size < shoff + elf_shdr_get_size) {
+	/* keeps shoff in size_t range for the two bounds below */
+	if (shoff > fw->size) {
+		dev_err(dev, "Section header table is out of bounds\n");
+		return -EINVAL;
+	}
+
+	if (shnum) {
+		shend = size_add(size_mul(elf_shdr_get_size, shnum), shoff);
+		if (shend > fw->size) {
+			dev_err(dev, "Section headers are out of bounds\n");
+			return -EINVAL;
+		}
+	}
+
+	/* find_table() reads the header at shstrndx even with no sections */
+	shend = size_add(size_mul(elf_shdr_get_size, (size_t)shstrndx + 1), shoff);
+	if (shend > fw->size) {
 		dev_err(dev, "Image is too small\n");
 		return -EINVAL;
 	}
Signature · 1 line
2.43.0
maintainer
#1 lore raw

Good day,

Quoted 54 lines · HyeongJun An

The rproc_elf_sanity_check() only checks the image is big enough to hold one section header. But find_table() walks e_shnum of them, and first dereferences the header at e_shstrndx to locate the section name table. Both fields are u16 and both come from the image, so an e_shstrndx of 65535 reads about 4 MB past the buffer.

The commit 9f9967fed9d0 ("soc: qcom: mdt_loader: Ensure we don't read past the ELF header") added the same check to the MDT loader, and says the header "is sanitized beforehand" under remoteproc. That is what this patch makes true.

Check e_shoff against the image size first, so the two bounds can use size_add() without an e_shoff above SIZE_MAX wrapping on 32-bit. The bounds are separate because e_shnum may be zero while find_table() still reads the e_shstrndx header.

Fixes 400e64df6b23 ("remoteproc: add framework for controlling remote processors")
Assisted-by Claude:claude-opus-4-8
---
 drivers/remoteproc/remoteproc_elf_loader.c | 23 ++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
drivers/remoteproc/remoteproc_elf_loader.c +9 -2
index 94177e416047..da3cddbe7d4c 100644
@@ -46,8 +46,9 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
        struct elf32_hdr *ehdr;
        u32 elf_shdr_get_size;
        u64 phoff, shoff;
+       size_t shend;
        char class;
-       u16 phnum;
+       u16 phnum, shnum, shstrndx;
 
        if (!fw) {
                dev_err(dev, "failed to load %s\n", name);
@@ -90,9 +91,27 @@ int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw)
        phoff = elf_hdr_get_e_phoff(class, fw->data);
        shoff = elf_hdr_get_e_shoff(class, fw->data);
        phnum =  elf_hdr_get_e_phnum(class, fw->data);
+       shnum = elf_hdr_get_e_shnum(class, fw->data);
+       shstrndx = elf_hdr_get_e_shstrndx(class, fw->data);
        elf_shdr_get_size = elf_size_of_shdr(class);
 
-       if (fw->size < shoff + elf_shdr_get_size) {
+       /* keeps shoff in size_t range for the two bounds below */
+       if (shoff > fw->size) {
+               dev_err(dev, "Section header table is out of bounds\n");
+               return -EINVAL;
+       }

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw) · line 112

Quoted 10 lines
drivers/remoteproc/remoteproc_elf_loader.c +10 -0
+
+       if (shnum) {
+               shend = size_add(size_mul(elf_shdr_get_size, shnum), shoff);
+               if (shend > fw->size) {
+                       dev_err(dev, "Section headers are out of bounds\n");
+                       return -EINVAL;
+               }
+       }
+
+       /* find_table() reads the header at shstrndx even with no sections */

Right, but if there is no sections, @shnum in find_tables is 0 and not arm is done.

drivers/remoteproc/remoteproc_elf_loader.c int rproc_elf_sanity_check(struct rproc *rproc, const struct firmware *fw) · line 113

Quoted 1 line
drivers/remoteproc/remoteproc_elf_loader.c +1 -0
+       shend = size_add(size_mul(elf_shdr_get_size, (size_t)shstrndx + 1), shoff);

Why the shstrndx + 1?

Also, there is no point in doing this check if @shnum is 0. Please move this block in the "if (shnum)".

Thanks,
Mathieu

Quoted 7 lines
drivers/remoteproc/remoteproc_elf_loader.c +1 -1
+       if (shend > fw->size) {
                dev_err(dev, "Image is too small\n");
                return -EINVAL;
        }
--

2.43.0

#2 lore raw

Thanks for taking a look!

Quoted 5 lines · Mathieu Poirier
Quoted 1 line · HyeongJun An

+ /* find_table() reads the header at shstrndx even with no sections */

Right, but if there is no sections, @shnum in find_tables is 0 and not arm is done.

The loop is skipped, but there is one load before it:

  :266  name_table_shdr = shdr + (shstrndx * elf_shdr_get_size);
  :268  name_table = elf_data + elf_shdr_get_sh_offset(class, name_table_shdr);
  :270  for (i = 0; i < shnum; i++, shdr += elf_shdr_get_size) {

:268 reads sh_offset out of the header at index shstrndx, before shnum is tested at :270. Both callers, :336 and :380, reach find_table() without checking shnum. The value is unused when shnum is 0, so it is a 4 or 8 byte read past the buffer and nothing worse.

Quoted 3 lines
Quoted 1 line

+ shend = size_add(size_mul(elf_shdr_get_size, (size_t)shstrndx + 1), shoff);

Why the shstrndx + 1?

To hold the header at index shstrndx the table needs shstrndx + 1 entries. It is keyed on shstrndx and not shnum because nothing here requires e_shstrndx < e_shnum.

Quoted 2 lines

Also, there is no point in doing this check if @shnum is 0. Please move this block in the "if (shnum)".

The check it replaces, :95, sits outside any shnum test today, so moving it in loses what master already has.

If you want it inside, the way there is to drop the read:

if (!shnum)
return NULL;

at the top of find_table(). The loop already falls through to return NULL at :313, so it changes nothing today, and the bound can then go inside "if (shnum)".

I am happy to do either. Let me know which you would prefer for v2.

Thanks a lot!

maintainer
#3 lore raw
Quoted 21 lines · HyeongJun An

Thanks for taking a look!

Quoted 5 lines · Mathieu Poirier
Quoted 1 line · HyeongJun An

+ /* find_table() reads the header at shstrndx even with no sections */

Right, but if there is no sections, @shnum in find_tables is 0 and not arm is done.

The loop is skipped, but there is one load before it:

  :266  name_table_shdr = shdr + (shstrndx * elf_shdr_get_size);
  :268  name_table = elf_data + elf_shdr_get_sh_offset(class, name_table_shdr);
  :270  for (i = 0; i < shnum; i++, shdr += elf_shdr_get_size) {

:268 reads sh_offset out of the header at index shstrndx, before shnum is tested at :270. Both callers, :336 and :380, reach find_table() without checking shnum. The value is unused when shnum is 0, so it is a 4 or 8 byte read past the buffer and nothing worse.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+ shend = size_add(size_mul(elf_shdr_get_size, (size_t)shstrndx + 1), shoff);

Why the shstrndx + 1?

To hold the header at index shstrndx the table needs shstrndx + 1 entries.

You are correct.

I would assume that if e_shnum is 0, then e_shstrndx would also be 0. If it isn't the case then your patch is valid. I will queue it when rc1 comes out.

Thanks,
Mathieu

Quoted 21 lines

It is keyed on shstrndx and not shnum because nothing here requires e_shstrndx < e_shnum.

Quoted 2 lines

Also, there is no point in doing this check if @shnum is 0. Please move this block in the "if (shnum)".

The check it replaces, :95, sits outside any shnum test today, so moving it in loses what master already has.

If you want it inside, the way there is to drop the read:

if (!shnum)
return NULL;

at the top of find_table(). The loop already falls through to return NULL at :313, so it changes nothing today, and the bound can then go inside "if (shnum)".

I am happy to do either. Let me know which you would prefer for v2.

Thanks a lot!

#4 lore raw
Quoted 1 line · Mathieu Poirier

I would assume that if e_shnum is 0, then e_shstrndx would also be 0.

They are independent. e_shnum goes to 0 when the section count reaches SHN_LORESERVE. e_shstrndx goes to SHN_XINDEX when the string table's own index reaches SHN_LORESERVE. One keys off the total, the other off one section's index, so a conforming image with many sections and its name string table at a low index has e_shnum 0 and a small e_shstrndx.

scripts/recordmcount.h:219 set_shnum() zeroes e_shnum from the count alone and never touches e_shstrndx.

Thanks.