Skip to contentSkip to conversation

tests/tcg: fix buffer overflow in multiarch/linux/linux-sigrtminmax

PATCH In review Max Filippov 7 participants 9 messages Jul 8 – Sep 30

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tests/tcg: fix buffer overflow in multiarch/linux/linux-sigrtminmax

Fix new_argv allocation size to be (argc + 2) * sizeof(char *) bytes, which matches the way it's used to store argc + 2 pointers.

This fixes the test for me, which otherwise fails on xtensa with the following message

linux-sigrtminmax.c: 59: main: Assertion `execve(new_argv[0], new_argv, environ) == 0' failed.'.

---
 tests/tcg/multiarch/linux/linux-sigrtminmax.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/tcg/multiarch/linux/linux-sigrtminmax.c +1 -1
index a7059aacd9cb..b5ea65f3d393 100644
@@ -39,7 +39,7 @@ int main(int argc, char **argv)
     assert(qemu);
 
     if (!getenv("QEMU_RTSIG_MAP")) {
-        char **new_argv = malloc((argc + 2) + sizeof(char *));
+        char **new_argv = malloc((argc + 2) * sizeof(char *));
         int tsig1, hsig1, count1, tsig2, hsig2, count2;
         char rt_sigmap[64];
 
Signature · 1 line
2.47.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Max Filippov

Fix new_argv allocation size to be (argc + 2) * sizeof(char *) bytes, which matches the way it's used to store argc + 2 pointers.

This fixes the test for me, which otherwise fails on xtensa with the following message

linux-sigrtminmax.c: 59: main: Assertion `execve(new_argv[0], new_argv, environ) == 0' failed.'.

Quoted 16 lines
---
 tests/tcg/multiarch/linux/linux-sigrtminmax.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/tcg/multiarch/linux/linux-sigrtminmax.c +1 -1
index a7059aacd9cb..b5ea65f3d393 100644
@@ -39,7 +39,7 @@ int main(int argc, char **argv)
     assert(qemu);
 
     if (!getenv("QEMU_RTSIG_MAP")) {
-        char **new_argv = malloc((argc + 2) + sizeof(char *));
+        char **new_argv = malloc((argc + 2) * sizeof(char *));
         int tsig1, hsig1, count1, tsig2, hsig2, count2;
         char rt_sigmap[64];
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
Reviewed-by on v1 1/1
#2 lore raw
In reply to Max Filippov
Quoted 6 lines · Max Filippov

Fix new_argv allocation size to be (argc + 2) * sizeof(char *) bytes, which matches the way it's used to store argc + 2 pointers.

This fixes the test for me, which otherwise fails on xtensa with the following message

Quoted 6 lines
   linux-sigrtminmax.c: 59: main: Assertion `execve(new_argv[0], new_argv, environ) == 0' failed.'.
---
  tests/tcg/multiarch/linux/linux-sigrtminmax.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

r~

Quoted 14 lines
tests/tcg/multiarch/linux/linux-sigrtminmax.c +1 -1
index a7059aacd9cb..b5ea65f3d393 100644
@@ -39,7 +39,7 @@ int main(int argc, char **argv)
      assert(qemu);
 
      if (!getenv("QEMU_RTSIG_MAP")) {
-        char **new_argv = malloc((argc + 2) + sizeof(char *));
+        char **new_argv = malloc((argc + 2) * sizeof(char *));
          int tsig1, hsig1, count1, tsig2, hsig2, count2;
          char rt_sigmap[64];
 
Reviewed-by on v1 1/1
#3 lore raw
In reply to Max Filippov
Quoted 6 lines · Max Filippov

Fix new_argv allocation size to be (argc + 2) * sizeof(char *) bytes, which matches the way it's used to store argc + 2 pointers.

This fixes the test for me, which otherwise fails on xtensa with the following message

Quoted 6 lines
   linux-sigrtminmax.c: 59: main: Assertion `execve(new_argv[0], new_argv, environ) == 0' failed.'.
---
  tests/tcg/multiarch/linux/linux-sigrtminmax.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Fixes 6f58b090328 ("tests/tcg: Add SIGRTMIN/SIGRTMAX test")
Reviewed-by on v1 1/1
#4 lore raw
In reply to Max Filippov
Quoted 18 lines · Max Filippov

Fix new_argv allocation size to be (argc + 2) * sizeof(char *) bytes, which matches the way it's used to store argc + 2 pointers.

This fixes the test for me, which otherwise fails on xtensa with the following message

   linux-sigrtminmax.c: 59: main: Assertion `execve(new_argv[0], new_argv, environ) == 0' failed.'.
---
  tests/tcg/multiarch/linux/linux-sigrtminmax.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tests/tcg/multiarch/linux/linux-sigrtminmax.c +0 -0
index a7059aacd9cb..b5ea65f3d393 100644

tests/tcg/multiarch/linux/linux-sigrtminmax.c int main(int argc, char **argv) · line 45

Quoted 8 lines
tests/tcg/multiarch/linux/linux-sigrtminmax.c +1 -1
@@ -39,7 +39,7 @@ int main(int argc, char **argv)
      assert(qemu);
 
      if (!getenv("QEMU_RTSIG_MAP")) {
-        char **new_argv = malloc((argc + 2) + sizeof(char *));
+        char **new_argv = malloc((argc + 2) * sizeof(char *));
          int tsig1, hsig1, count1, tsig2, hsig2, count2;
          char rt_sigmap[64];
 

Whoops, a silly typo on my part.
Thanks for taking the time to fix it!

#5 lore raw
In reply to Max Filippov
Quoted 18 lines · Max Filippov

Fix new_argv allocation size to be (argc + 2) * sizeof(char *) bytes, which matches the way it's used to store argc + 2 pointers.

This fixes the test for me, which otherwise fails on xtensa with the following message

linux-sigrtminmax.c: 59: main: Assertion `execve(new_argv[0], new_argv, environ) == 0' failed.'.

---
 tests/tcg/multiarch/linux/linux-sigrtminmax.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/tcg/multiarch/linux/linux-sigrtminmax.c +0 -0
index a7059aacd9cb..b5ea65f3d393 100644

tests/tcg/multiarch/linux/linux-sigrtminmax.c int main(int argc, char **argv) · line 42

Quoted 5 lines
tests/tcg/multiarch/linux/linux-sigrtminmax.c +1 -1
@@ -39,7 +39,7 @@ int main(int argc, char **argv)
     assert(qemu);
 
     if (!getenv("QEMU_RTSIG_MAP")) {
-        char **new_argv = malloc((argc + 2) + sizeof(char *));
+        char **new_argv = malloc((argc + 2) * sizeof(char *));

If you can switch to glib's malloc, then g_new(argc + 2, sizeof(char *)) is better. Yes, @argc can't become big enough for the multiplication to overflow, but g_new() removes the need for reasoning.

Quoted 2 lines
tests/tcg/multiarch/linux/linux-sigrtminmax.c +0 -0
         int tsig1, hsig1, count1, tsig2, hsig2, count2;
         char rt_sigmap[64];
#6 lore raw
Quoted 22 lines · Markus Armbruster
Quoted 20 lines · Max Filippov

Fix new_argv allocation size to be (argc + 2) * sizeof(char *) bytes, which matches the way it's used to store argc + 2 pointers.

This fixes the test for me, which otherwise fails on xtensa with the following message

linux-sigrtminmax.c: 59: main: Assertion `execve(new_argv[0], new_argv, environ) == 0' failed.'.

---
 tests/tcg/multiarch/linux/linux-sigrtminmax.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/tcg/multiarch/linux/linux-sigrtminmax.c +0 -0
index a7059aacd9cb..b5ea65f3d393 100644
@@ -39,7 +39,7 @@ int main(int argc, char **argv)
     assert(qemu);
 
Quoted 7 lines

tests/tcg/multiarch/linux/linux-sigrtminmax.c int main(int argc, char **argv) · line 42

Quoted 3 lines
tests/tcg/multiarch/linux/linux-sigrtminmax.c +1 -1
     if (!getenv("QEMU_RTSIG_MAP")) {
-        char **new_argv = malloc((argc + 2) + sizeof(char *));
+        char **new_argv = malloc((argc + 2) * sizeof(char *));

If you can switch to glib's malloc, then g_new(argc + 2, sizeof(char *)) is better. Yes, @argc can't become big enough for the multiplication to overflow, but g_new() removes the need for reasoning.

The TCG unit tests don't link to GLib, only basic libc.

Fortunately unit tests have a trusted caller (the QEMU meson build system), so we don't have to worry about malicious usage with overflow in this context.

Quoted 5 lines

tests/tcg/multiarch/linux/linux-sigrtminmax.c int main(int argc, char **argv) · line 44

Quoted 3 lines
tests/tcg/multiarch/linux/linux-sigrtminmax.c +0 -0
         int tsig1, hsig1, count1, tsig2, hsig2, count2;
         char rt_sigmap[64];

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#7 lore raw
In reply to Markus Armbruster
Quoted 22 lines · Markus Armbruster
Quoted 20 lines · Max Filippov

Fix new_argv allocation size to be (argc + 2) * sizeof(char *) bytes, which matches the way it's used to store argc + 2 pointers.

This fixes the test for me, which otherwise fails on xtensa with the following message

linux-sigrtminmax.c: 59: main: Assertion `execve(new_argv[0], new_argv, environ) == 0' failed.'.

---
 tests/tcg/multiarch/linux/linux-sigrtminmax.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/tcg/multiarch/linux/linux-sigrtminmax.c +0 -0
index a7059aacd9cb..b5ea65f3d393 100644
@@ -39,7 +39,7 @@ int main(int argc, char **argv)
     assert(qemu);
 
Quoted 7 lines

tests/tcg/multiarch/linux/linux-sigrtminmax.c int main(int argc, char **argv) · line 42

Quoted 3 lines
tests/tcg/multiarch/linux/linux-sigrtminmax.c +1 -1
     if (!getenv("QEMU_RTSIG_MAP")) {
-        char **new_argv = malloc((argc + 2) + sizeof(char *));
+        char **new_argv = malloc((argc + 2) * sizeof(char *));

If you can switch to glib's malloc, then g_new(argc + 2, sizeof(char *)) is better. Yes, @argc can't become big enough for the multiplication to overflow, but g_new() removes the need for reasoning.

Not for TCG test cases - we are limited to plain old glibc.

Quoted 3 lines
Quoted 3 lines
tests/tcg/multiarch/linux/linux-sigrtminmax.c +0 -0
         int tsig1, hsig1, count1, tsig2, hsig2, count2;
         char rt_sigmap[64];
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#8 lore raw
In reply to Max Filippov
Quoted 9 lines · Max Filippov

Fix new_argv allocation size to be (argc + 2) * sizeof(char *) bytes, which matches the way it's used to store argc + 2 pointers.

This fixes the test for me, which otherwise fails on xtensa with the following message

linux-sigrtminmax.c: 59: main: Assertion `execve(new_argv[0], new_argv, environ) == 0' failed.'.

Queued to testing/next, thanks.
<snip>

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro