Skip to contentSkip to conversation

load_elf: fix iterator type in glue

PATCH In review Anastasia Belova 3 participants 7 messages Dec 21, 2023 – Jan 15, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 load_elf: fix iterator type in glue

file_size is uint32_t, so j < file_size should be
uint32_t too.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 7ef295ea5b ("loader: Add data swap option to load-elf")
---
 include/hw/elf_ops.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/hw/elf_ops.h +1 -1
index 0a5c258fe6..1defccaa71 100644
@@ -500,7 +500,7 @@ static ssize_t glue(load_elf, SZ)(const char *name, int fd,
             }
 
             if (data_swab) {
-                int j;
+                uint32_t j;
                 for (j = 0; j < file_size; j += (1 << data_swab)) {
                     uint8_t *dp = data + j;
                     switch (data_swab) {
Signature · 1 line
2.30.2
#1 lore raw

Hi,

Quoted 2 lines · Anastasia Belova

file_size is uint32_t, so j < file_size should be
uint32_t too.

file_size is of elf_word type, which is either uint32_t or uint64_t.

Your explanation is not very clear... Maybe you want an unsigned type? In that case, does the following makes your sanitizer happier?

-- >8 --

include/hw/elf_ops.h +1 -2
index 0a5c258fe6..03eba78c6e 100644
@@ -502,4 +502,3 @@ static ssize_t glue(load_elf, SZ)(const char *name, int fd,
             if (data_swab) {
-                int j;
-                for (j = 0; j < file_size; j += (1 << data_swab)) {
+                for (unsigned j = 0; j < file_size; j += (1 << data_swab)) {
                     uint8_t *dp = data + j;

---

Quoted 21 lines

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 7ef295ea5b ("loader: Add data swap option to load-elf")
---
  include/hw/elf_ops.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
include/hw/elf_ops.h +1 -1
index 0a5c258fe6..1defccaa71 100644
@@ -500,7 +500,7 @@ static ssize_t glue(load_elf, SZ)(const char *name, int fd,
              }
 
              if (data_swab) {
-                int j;
+                uint32_t j;
                  for (j = 0; j < file_size; j += (1 << data_swab)) {
                      uint8_t *dp = data + j;
                      switch (data_swab) {
#2 lore raw
Quoted 4 lines · Philippe Mathieu-Daudé

Hi,

On 21/12/23 09:08, Anastasia Belova wrote:

Quoted 8 lines
Quoted 2 lines

file_size is uint32_t, so j < file_size should be
uint32_t too.

file_size is of elf_word type, which is either uint32_t or uint64_t.

Your explanation is not very clear... Maybe you want an unsigned type? In that case, does the following makes your sanitizer happier?

Since file_size is type 'elf_word', the iterator 'j' should be the matching type. In practice nobody is loading 2GB ELF files, so we don't really run into this, but it is a bug.

I agree with Philippe that it would be helpful if the commit message:
* is clear about the problem it is fixing
* describes whether there are any real-world consequences of the issue and how severe (or not) they are

thanks
-- PMM

#3 lore raw

PATCH v2 1/1 load_elf: fix iterators' types for elf file processing

i and size should be the same type as ehdr.e_phnum (Elf32_Half or Elf64_Half) to avoid overflows. So the bigger one is chosen.

j should be the same type as file_size for the same reasons.

This commit fixes a minor bug, maybe even a typo.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 7ef295ea5b ("loader: Add data swap option to load-elf")
---
 include/hw/elf_ops.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
include/hw/elf_ops.h +3 -3
index 0a5c258fe6..6e807708f3 100644
@@ -325,7 +325,7 @@ static ssize_t glue(load_elf, SZ)(const char *name, int fd,
 {
     struct elfhdr ehdr;
     struct elf_phdr *phdr = NULL, *ph;
-    int size, i;
+    Elf64_Half size, i;
     ssize_t total_size;
     elf_word mem_size, file_size, data_offset;
     uint64_t addr, low = (uint64_t)-1, high = 0;
@@ -464,7 +464,7 @@ static ssize_t glue(load_elf, SZ)(const char *name, int fd,
                  * the ROM overlap check in loader.c, so we don't try to
                  * explicitly detect those here.
                  */
-                int j;
+                Elf64_Half j;
                 elf_word zero_start = ph->p_paddr + file_size;
                 elf_word zero_end = ph->p_paddr + mem_size;
 
@@ -500,7 +500,7 @@ static ssize_t glue(load_elf, SZ)(const char *name, int fd,
             }
 
             if (data_swab) {
-                int j;
+                elf_word j;
                 for (j = 0; j < file_size; j += (1 << data_swab)) {
                     uint8_t *dp = data + j;
                     switch (data_swab) {
Signature · 1 line
2.30.2
#4 lore raw

Re: [PATCH v2] load_elf: fix iterators' types for elf file processing

Quoted 21 lines · Anastasia Belova

i and size should be the same type as ehdr.e_phnum (Elf32_Half or Elf64_Half) to avoid overflows. So the bigger one is chosen.

j should be the same type as file_size for the same reasons.

This commit fixes a minor bug, maybe even a typo.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 7ef295ea5b ("loader: Add data swap option to load-elf")
---
 include/hw/elf_ops.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
include/hw/elf_ops.h +0 -0
index 0a5c258fe6..6e807708f3 100644

include/hw/elf_ops.h static ssize_t glue(load_elf, SZ)(const char *name, int fd, · line 328

Quoted 5 lines
include/hw/elf_ops.h +1 -1
@@ -325,7 +325,7 @@ static ssize_t glue(load_elf, SZ)(const char *name, int fd,
 {
     struct elfhdr ehdr;
     struct elf_phdr *phdr = NULL, *ph;
-    int size, i;
+    Elf64_Half size, i;

Elf64_Half is a 16 bit type -- this doesn't seem right.

In particular we use 'size' to store "ehdr.e_phnum * sizeof(phdr[0])" so even if we know e_phnum fits in a 16 bit type the multiplication is not guaranteed to; so this change actually introduces a bug. The type we need for what we're doing needs to be big enough to store the result of that multiplication, so anything bigger than 16 bits will be fine, including 'int' (since we know the RHS is a small number). We could change this to 'size_t' or 'ssize_t', I suppose, but it doesn't really seem necessary.

(Side note: whatever your static analyser is could presumbaly be doing better about identifying overflows here -- it ought to be able to figure out that "unsigned 16 bit value * constant 64" is not going to overflow a signed 32 bit type.)

i is only used as the iterator through the program headers, so it wouldn't be wrong to make it a 16 bit type, but there's really no need to constrain it -- an 'int' is fine.

Also, these functions have macro magic so we create both the 32-bit and 64-bit elf versions from one bit of source, so it is not in general right for them to have a type in them that is specific to one or the other, as Elf64_Half is.

include/hw/elf_ops.h static ssize_t glue(load_elf, SZ)(const char *name, int fd, · line 467

Quoted 9 lines
include/hw/elf_ops.h +1 -1
     ssize_t total_size;
     elf_word mem_size, file_size, data_offset;
     uint64_t addr, low = (uint64_t)-1, high = 0;
@@ -464,7 +464,7 @@ static ssize_t glue(load_elf, SZ)(const char *name, int fd,
                  * the ROM overlap check in loader.c, so we don't try to
                  * explicitly detect those here.
                  */
-                int j;
+                Elf64_Half j;

This case is like 'i' above -- it's not a bug to use a 16 bit type for the iterator variable, but it isn't necessary, and we shouldn't use an Elf64* type.

include/hw/elf_ops.h static ssize_t glue(load_elf, SZ)(const char *name, int fd, · line 506

Quoted 12 lines
include/hw/elf_ops.h +1 -1
                 elf_word zero_start = ph->p_paddr + file_size;
                 elf_word zero_end = ph->p_paddr + mem_size;
 
@@ -500,7 +500,7 @@ static ssize_t glue(load_elf, SZ)(const char *name, int fd,
             }
 
             if (data_swab) {
-                int j;
+                elf_word j;
                 for (j = 0; j < file_size; j += (1 << data_swab)) {
                     uint8_t *dp = data + j;
                     switch (data_swab) {

This change is OK and necessary. It fixes a minor bug:
if we are loading an ELF file that contains a segment whose data in the ELF file is larger than 2GB and we need to byteswap that data, we would previously get this wrong. (We should mention this in the commit message.)

thanks
-- PMM

#5 lore raw

PATCH v3 1/1 load_elf: fix iterator's type for elf file processing

j is used while loading an ELF file to byteswap segments' data. If data is larger than 2GB an overflow may happen. So j should be elf_word.

This commit fixes a minor bug, maybe even a typo.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes 7ef295ea5b ("loader: Add data swap option to load-elf")

---
v2: fix type of j
v3: remove changes for i, size and another j
Thanks for your patience.

 include/hw/elf_ops.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/hw/elf_ops.h +1 -1
index 0a5c258fe6..9c35d1b9da 100644
@@ -500,7 +500,7 @@ static ssize_t glue(load_elf, SZ)(const char *name, int fd,
             }
 
             if (data_swab) {
-                int j;
+                elf_word j;
                 for (j = 0; j < file_size; j += (1 << data_swab)) {
                     uint8_t *dp = data + j;
                     switch (data_swab) {
Signature · 1 line
2.30.2
#6 lore raw

Re: [PATCH v3] load_elf: fix iterator's type for elf file processing

Quoted 9 lines · Anastasia Belova

j is used while loading an ELF file to byteswap segments' data. If data is larger than 2GB an overflow may happen. So j should be elf_word.

This commit fixes a minor bug, maybe even a typo.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Quoted 8 lines
Fixes 7ef295ea5b ("loader: Add data swap option to load-elf")

---
v2: fix type of j
v3: remove changes for i, size and another j
Thanks for your patience.

 include/hw/elf_ops.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Applied to target-arm.next, with a minor tweak to the commit message. Thanks for your contribution!

-- PMM