Skip to contentSkip to conversation

util/iov: Avoid dynamic stack allocation

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

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 util/iov: Avoid dynamic stack allocation

From: Philippe Mathieu-Daudé <[email protected]>

Use autofree heap allocation instead of variable-length array on the stack.

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).

---
Usual "only tested with make check/make check-avocado" caveat.

 util/iov.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/iov.c +1 -1
index 866fb577f30..7e73948f5e3 100644
@@ -571,7 +571,7 @@ static int sortelem_cmp_src_index(const void *a, const void *b)
  */
 void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf)
 {
-    IOVectorSortElem sortelems[src->niov];
+    g_autofree IOVectorSortElem *sortelems = g_new(IOVectorSortElem, src->niov);
     void *last_end;
     int i;
 
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Peter Maydell

From: Philippe Mathieu-Daudé <[email protected]>

Use autofree heap allocation instead of variable-length array on the stack.

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 7 lines

---
Usual "only tested with make check/make check-avocado" caveat.

 util/iov.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 17 lines
util/iov.c +1 -1
index 866fb577f30..7e73948f5e3 100644
@@ -571,7 +571,7 @@ static int sortelem_cmp_src_index(const void *a, const void *b)
  */
 void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf)
 {
-    IOVectorSortElem sortelems[src->niov];
+    g_autofree IOVectorSortElem *sortelems = g_new(IOVectorSortElem, src->niov);
     void *last_end;
     int i;
 
Signature · 1 line
2.34.1
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org
#2 lore raw
In reply to Peter Maydell

Cc'ing qemu-block@ (I suppose this will go via a block tree)

Quoted 31 lines · Peter Maydell

From: Philippe Mathieu-Daudé <[email protected]>

Use autofree heap allocation instead of variable-length array on the stack.

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).

---
Usual "only tested with make check/make check-avocado" caveat.

  util/iov.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
util/iov.c +1 -1
index 866fb577f30..7e73948f5e3 100644
@@ -571,7 +571,7 @@ static int sortelem_cmp_src_index(const void *a, const void *b)
   */
  void qemu_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src, void *buf)
  {
-    IOVectorSortElem sortelems[src->niov];
+    g_autofree IOVectorSortElem *sortelems = g_new(IOVectorSortElem, src->niov);
      void *last_end;
      int i;
 
#3 lore raw
Quoted 1 line · Philippe Mathieu-Daudé

Cc'ing qemu-block@ (I suppose this will go via a block tree)

Makes sense.

I can queue it through my NBD tree if no one else beats me (since the nbd code is a heavy user of iovs).

Quoted 12 lines · Peter Maydell
Quoted 10 lines

From: Philippe Mathieu-Daudé <[email protected]>

Use autofree heap allocation instead of variable-length array on the stack.

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).

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org