Skip to contentSkip to conversation

linux-user/gen-vdso: minor error handling improvements

PATCH Applied Peter Maydell 2 participants 8 messages Jul 10, 2025

lore
Share & tools
lore
#0 lore raw

cover linux-user/gen-vdso: minor error handling improvements

These two small patches improve the error handling in the gen-vdso program. Error handling isn't particularly critical here because the tool only gets run during the QEMU build process on input that we trust (because we generated it by calling a compiler for the guest architecture), but these were easy to do.

The main motivation here is that Coverity complained about not checking fseek()'s return value.

thanks
-- PMM

Peter Maydell (2):
linux-user/gen-vdso: Handle fseek() failure linux-user/gen-vdso: Don't write off the end of buf[]

 linux-user/gen-vdso.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 linux-user/gen-vdso: Handle fseek() failure

Coverity points out that we don't check for fseek() failure in gen-vdso.c, and so we might pass -1 to malloc(). Add the error checking.

(This is a standalone executable that doesn't link against glib, so we can't do the easy thing and use g_file_get_contents().)

Coverity: CID 1523742

---
 linux-user/gen-vdso.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
linux-user/gen-vdso.c +9 -2
index fce9d5cbc3c..1c406d1b10f 100644
@@ -113,9 +113,16 @@ int main(int argc, char **argv)
      * We expect the vdso to be small, on the order of one page,
      * therefore we do not expect a partial read.
      */
-    fseek(inf, 0, SEEK_END);
+    if (fseek(inf, 0, SEEK_END) < 0) {
+        goto perror_inf;
+    }
     total_len = ftell(inf);
-    fseek(inf, 0, SEEK_SET);
+    if (total_len < 0) {
+        goto perror_inf;
+    }
+    if (fseek(inf, 0, SEEK_SET) < 0) {
+        goto perror_inf;
+    }
 
     buf = malloc(total_len);
     if (buf == NULL) {
Signature · 1 line
2.43.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] linux-user/gen-vdso: Handle fseek() failure

Quoted 11 lines · Peter Maydell

Coverity points out that we don't check for fseek() failure in gen-vdso.c, and so we might pass -1 to malloc(). Add the error checking.

(This is a standalone executable that doesn't link against glib, so we can't do the easy thing and use g_file_get_contents().)

Coverity: CID 1523742

---
  linux-user/gen-vdso.c | 11 +++++++++--
  1 file changed, 9 insertions(+), 2 deletions(-)

r~

#3 lore raw

PATCH 2/2 linux-user/gen-vdso: Don't write off the end of buf[]

In reply to Peter Maydell

In gen-vdso we load in a file and assume it's a valid ELF file. In particular we assume it's big enough to be able to read the ELF information in e_ident in the ELF header.

Add a check that the total file length is at least big enough for all the e_ident bytes, which is good enough for the code in gen-vdso.c. This will catch the most obvious possible bad input file (truncated) and allow us to run the sanity checks like "not actually an ELF file" without potentially crashing.

The code in elf32_process() and elf64_process() still makes assumptions about the file being well-formed, but this is OK because we only run it on the vdso binaries that we create ourselves in the build process by running the compiler.

---
Hardening all of elf*_process() seems like overkill, but this is an easy check to add.

---
 linux-user/gen-vdso.c | 5 +++++
 1 file changed, 5 insertions(+)
linux-user/gen-vdso.c +5 -0
index 1c406d1b10f..aeaa927db8f 100644
@@ -124,6 +124,11 @@ int main(int argc, char **argv)
         goto perror_inf;
     }
 
+    if (total_len < EI_NIDENT) {
+        fprintf(stderr, "%s: file too small (truncated?)\n", inf_name);
+        return EXIT_FAILURE;
+    }
+
     buf = malloc(total_len);
     if (buf == NULL) {
         goto perror_inf;
Signature · 1 line
2.43.0
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] linux-user/gen-vdso: Don't write off the end of buf[]

Quoted 15 lines · Peter Maydell

In gen-vdso we load in a file and assume it's a valid ELF file. In particular we assume it's big enough to be able to read the ELF information in e_ident in the ELF header.

Add a check that the total file length is at least big enough for all the e_ident bytes, which is good enough for the code in gen-vdso.c. This will catch the most obvious possible bad input file (truncated) and allow us to run the sanity checks like "not actually an ELF file" without potentially crashing.

The code in elf32_process() and elf64_process() still makes assumptions about the file being well-formed, but this is OK because we only run it on the vdso binaries that we create ourselves in the build process by running the compiler.

Quoted 7 lines

---
Hardening all of elf*_process() seems like overkill, but this is an easy check to add.

---
  linux-user/gen-vdso.c | 5 +++++
  1 file changed, 5 insertions(+)

r~

#5 lore raw

Re: [PATCH 2/2] linux-user/gen-vdso: Don't write off the end of buf[]

In reply to Peter Maydell
Quoted 16 lines · Peter Maydell

In gen-vdso we load in a file and assume it's a valid ELF file. In particular we assume it's big enough to be able to read the ELF information in e_ident in the ELF header.

Add a check that the total file length is at least big enough for all the e_ident bytes, which is good enough for the code in gen-vdso.c. This will catch the most obvious possible bad input file (truncated) and allow us to run the sanity checks like "not actually an ELF file" without potentially crashing.

The code in elf32_process() and elf64_process() still makes assumptions about the file being well-formed, but this is OK because we only run it on the vdso binaries that we create ourselves in the build process by running the compiler.

Quoted 4 lines

---
Hardening all of elf*_process() seems like overkill, but this is an easy check to add.

Subject line should say "read off the end", of course.

-- PMM

#6 lore raw
In reply to Peter Maydell
Quoted 12 lines · Peter Maydell

These two small patches improve the error handling in the gen-vdso program. Error handling isn't particularly critical here because the tool only gets run during the QEMU build process on input that we trust (because we generated it by calling a compiler for the guest architecture), but these were easy to do.

The main motivation here is that Coverity complained about not checking fseek()'s return value.

thanks
-- PMM

Quoted 7 lines

Peter Maydell (2):
linux-user/gen-vdso: Handle fseek() failure linux-user/gen-vdso: Don't write off the end of buf[]

  linux-user/gen-vdso.c | 16 ++++++++++++++--
  1 file changed, 14 insertions(+), 2 deletions(-)

Queued, thanks.

r~

#7 lore raw
Quoted 18 lines · Richard Henderson
Quoted 16 lines · Peter Maydell

These two small patches improve the error handling in the gen-vdso program. Error handling isn't particularly critical here because the tool only gets run during the QEMU build process on input that we trust (because we generated it by calling a compiler for the guest architecture), but these were easy to do.

The main motivation here is that Coverity complained about not checking fseek()'s return value.

thanks
-- PMM

Peter Maydell (2):
linux-user/gen-vdso: Handle fseek() failure linux-user/gen-vdso: Don't write off the end of buf[]

Quoted 5 lines
Quoted 3 lines
  linux-user/gen-vdso.c | 16 ++++++++++++++--
  1 file changed, 14 insertions(+), 2 deletions(-)

Queued, thanks.

Would you mind fixing up the read/write thinko in the subject line of patch 2?

thanks
-- PMM