Skip to contentSkip to conversation

tests/qtest/netdev-socket: Avoid variable-length array in inet_get_free_port_multiple()

PATCH In review Peter Maydell 4 participants 4 messages Aug 24, 2023 – Aug 25, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tests/qtest/netdev-socket: Avoid variable-length array in inet_get_free_port_multiple()

We use a variable-length array in inet_get_free_port_multiple(). This is only test code called at the start of a test, so switch to a heap allocation instead.

The codebase has very few VLAs, and if we can get rid of them all we can make the compiler error on new additions. This is a defensive measure against security bugs where an on-stack dynamic allocation isn't correctly size-checked (e.g. CVE-2021-3527).

---
 tests/qtest/netdev-socket.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/qtest/netdev-socket.c +1 -1
index 097abc0230b..8eed54801f2 100644
@@ -82,7 +82,7 @@ static int inet_get_free_port_socket_ipv6(int sock)
 
 static int inet_get_free_port_multiple(int nb, int *port, bool ipv6)
 {
-    int sock[nb];
+    g_autofree int *sock = g_new(int, nb);
     int i;
 
     for (i = 0; i < nb; i++) {
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 19 lines · Peter Maydell

We use a variable-length array in inet_get_free_port_multiple(). This is only test code called at the start of a test, so switch to a heap allocation instead.

The codebase has very few VLAs, and if we can get rid of them all we can make the compiler error on new additions. This is a defensive measure against security bugs where an on-stack dynamic allocation isn't correctly size-checked (e.g. CVE-2021-3527).

---
  tests/qtest/netdev-socket.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tests/qtest/netdev-socket.c +0 -0
index 097abc0230b..8eed54801f2 100644

tests/qtest/netdev-socket.c static int inet_get_free_port_socket_ipv6(int sock) · line 88

Quoted 8 lines
tests/qtest/netdev-socket.c +1 -1
@@ -82,7 +82,7 @@ static int inet_get_free_port_socket_ipv6(int sock)
 
  static int inet_get_free_port_multiple(int nb, int *port, bool ipv6)
  {
-    int sock[nb];
+    g_autofree int *sock = g_new(int, nb);
      int i;
 
      for (i = 0; i < nb; i++) {
Reviewed-by on v1 1/1
#2 lore raw
In reply to Peter Maydell
Quoted 9 lines · Peter Maydell

We use a variable-length array in inet_get_free_port_multiple(). This is only test code called at the start of a test, so switch to a heap allocation instead.

The codebase has very few VLAs, and if we can get rid of them all we can make the compiler error on new additions. This is a defensive measure against security bugs where an on-stack dynamic allocation isn't correctly size-checked (e.g. CVE-2021-3527).

Quoted 4 lines
---
  tests/qtest/netdev-socket.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Reviewed-by on v1 1/1
#3 lore raw
In reply to Peter Maydell
Quoted 19 lines · Peter Maydell

We use a variable-length array in inet_get_free_port_multiple(). This is only test code called at the start of a test, so switch to a heap allocation instead.

The codebase has very few VLAs, and if we can get rid of them all we can make the compiler error on new additions. This is a defensive measure against security bugs where an on-stack dynamic allocation isn't correctly size-checked (e.g. CVE-2021-3527).

---
  tests/qtest/netdev-socket.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tests/qtest/netdev-socket.c +0 -0
index 097abc0230b..8eed54801f2 100644

tests/qtest/netdev-socket.c static int inet_get_free_port_socket_ipv6(int sock) · line 88

Quoted 8 lines
tests/qtest/netdev-socket.c +1 -1
@@ -82,7 +82,7 @@ static int inet_get_free_port_socket_ipv6(int sock)
 
  static int inet_get_free_port_multiple(int nb, int *port, bool ipv6)
  {
-    int sock[nb];
+    g_autofree int *sock = g_new(int, nb);
      int i;
 
      for (i = 0; i < nb; i++) {