Skip to contentSkip to conversation

target/s390x/arch_dump: Fix memory corruption in s390x_write_elf64_notes()

PATCH In review Thomas Huth 3 participants 5 messages Feb 14, 2023 – Feb 15, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/s390x/arch_dump: Fix memory corruption in s390x_write_elf64_notes()

"note_size" can be smaller than sizeof(note), so unconditionally calling memset(notep, 0, sizeof(note)) could cause a memory corruption here in case notep has been allocated dynamically, thus let's use note_size as length argument for memset() instead.

Fixes 113d8f4e95 ("s390x: pv: Add dump support")
---
 target/s390x/arch_dump.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/s390x/arch_dump.c +1 -1
index a2329141e8..a7c44ba49d 100644
@@ -248,7 +248,7 @@ static int s390x_write_elf64_notes(const char *note_name,
             notep = g_malloc(note_size);
         }
 
-        memset(notep, 0, sizeof(note));
+        memset(notep, 0, note_size);
 
         /* Setup note header data */
         notep->hdr.n_descsz = cpu_to_be32(content_size);
Signature · 1 line
2.31.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Thomas Huth

"note_size" can be smaller than sizeof(note), so unconditionally calling memset(notep, 0, sizeof(note)) could cause a memory corruption here in case notep has been allocated dynamically, thus let's use note_size as length argument for memset() instead.

Correct.

I wonder why use one notep* pointing to a stack allocated or a heap allocated buffer. This isn't hot path, one heap use could simplify this code complexity IMO.

Quoted 5 lines
Fixes 113d8f4e95 ("s390x: pv: Add dump support")
---
  target/s390x/arch_dump.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
#2 lore raw
Quoted 11 lines · Philippe Mathieu-Daudé
Quoted 4 lines · Thomas Huth

"note_size" can be smaller than sizeof(note), so unconditionally calling memset(notep, 0, sizeof(note)) could cause a memory corruption here in case notep has been allocated dynamically, thus let's use note_size as length argument for memset() instead.

Correct.

I wonder why use one notep* pointing to a stack allocated or a heap allocated buffer. This isn't hot path, one heap use could simplify this code complexity IMO.

You've got a point. I'll give it a try and send a v2.

Thanks,
Thomas

#3 lore raw
Quoted 7 lines · Thomas Huth
Quoted 6 lines · Philippe Mathieu-Daudé
Quoted 4 lines · Thomas Huth

"note_size" can be smaller than sizeof(note), so unconditionally calling memset(notep, 0, sizeof(note)) could cause a memory corruption here in case notep has been allocated dynamically, thus let's use note_size as length argument for memset() instead.

Quoted 7 lines
Quoted 5 lines

Correct.

I wonder why use one notep* pointing to a stack allocated or a heap allocated buffer. This isn't hot path, one heap use could simplify this code complexity IMO.

You've got a point. I'll give it a try and send a v2.

Actually, it looked better as a separate, independent patch, so I sent it as "Simplify memory allocation in s390x_write_elf64_notes()" (based on this one here).

Thomas

Reviewed-by on v1 1/1
#4 lore raw
In reply to Thomas Huth
Quoted 7 lines · Thomas Huth

"note_size" can be smaller than sizeof(note), so unconditionally calling memset(notep, 0, sizeof(note)) could cause a memory corruption here in case notep has been allocated dynamically, thus let's use note_size as length argument for memset() instead.

Fixes 113d8f4e95 ("s390x: pv: Add dump support")

This was found because a machine was used that has PV support but doesn't have the PV dump support (I currently don't have access to a previous generation machine). In that case the size of the PV cpu note is reported as 0 but it's still being written / processed.

I added proper checks for dump support on my todo list so we can avoid writing empty notes. However it's easier said than done since "dump support" is actually a combination of KVM, QEMU, the machine AND a bit in the SE header that allows dumping. Additionally we need to report the size of the notes way before we start the PV dump process where we get told if the machine is allowed to dump.

Thanks for helping with the debug effort and creating a patch Thomas!

Also:

Quoted 17 lines
---
  target/s390x/arch_dump.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
target/s390x/arch_dump.c +1 -1
index a2329141e8..a7c44ba49d 100644
@@ -248,7 +248,7 @@ static int s390x_write_elf64_notes(const char *note_name,
              notep = g_malloc(note_size);
          }
 
-        memset(notep, 0, sizeof(note));
+        memset(notep, 0, note_size);
 
          /* Setup note header data */
          notep->hdr.n_descsz = cpu_to_be32(content_size);