Skip to contentSkip to conversation

nvme: avoid dynamic stack allocations

PATCH Applied Peter Maydell 3 participants 7 messages Aug 11, 2023 – Sep 12, 2023

lore
Share & tools
lore
#0 lore raw

cover nvme: avoid dynamic stack allocations

The QEMU codebase has very few C variable length arrays, 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).

We last had a go at this a few years ago, when Philippe wrote patches for this:
https://patchew.org/QEMU/[email protected]/
Some of the fixes made it into the tree, but some didn't (either because of lack of review or because review found some changes that needed to be made). I'm going through the remainder as a non-urgent Friday afternoon task...

This patchset deals with two VLAs in the NVME code.

thanks
-- PMM

Peter Maydell (1):
hw/nvme: Avoid dynamic stack allocation

Philippe Mathieu-Daudé (1):
hw/nvme: Use #define to avoid variable length array

 hw/nvme/ctrl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 hw/nvme: Use #define to avoid variable length array

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

In nvme_map_sgl() we create an array segment[] whose size is the 'const int SEG_CHUNK_SIZE'. Since this is C, rather than C++, a "const int foo" is not a true constant, it's merely a variable with a constant value, and so semantically segment[] is a variable-length array. Switch SEG_CHUNK_SIZE to a #define so that we can make the segment[] array truly fixed-size, in the sense that it doesn't trigger the -Wvla warning.

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

[PMM: rebased (function has moved file), expand commit message
based on discussion from previous version of patch]

---
 hw/nvme/ctrl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/nvme/ctrl.c +1 -1
index 539d2735531..d99a6f5c9a2 100644
@@ -1045,7 +1045,7 @@ static uint16_t nvme_map_sgl(NvmeCtrl *n, NvmeSg *sg, NvmeSglDescriptor sgl,
      * descriptors and segment chain) than the command transfer size, so it is
      * not bounded by MDTS.
      */
-    const int SEG_CHUNK_SIZE = 256;
+#define SEG_CHUNK_SIZE 256
 
     NvmeSglDescriptor segment[SEG_CHUNK_SIZE], *sgld, *last_sgld;
     uint64_t nsgld;
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/2 hw/nvme: Avoid dynamic stack allocation

In reply to Peter Maydell

Instead of using a variable-length array in nvme_map_prp(), allocate on the stack with a g_autofree pointer.

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

---
Using the approach suggested by RTH in review of Philippe's original patch:
https://patchew.org/QEMU/[email protected]/[email protected]/

---
 hw/nvme/ctrl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/nvme/ctrl.c +1 -1
index d99a6f5c9a2..90687b168ae 100644
@@ -894,7 +894,7 @@ static uint16_t nvme_map_prp(NvmeCtrl *n, NvmeSg *sg, uint64_t prp1,
     len -= trans_len;
     if (len) {
         if (len > n->page_size) {
-            uint64_t prp_list[n->max_prp_ents];
+            g_autofree uint64_t *prp_list = g_new(uint64_t, n->max_prp_ents);
             uint32_t nents, prp_trans;
             int i = 0;
 
Signature · 1 line
2.34.1
Applied
#3 lore raw
In reply to Peter Maydell
Quoted 18 lines · Peter Maydell

The QEMU codebase has very few C variable length arrays, 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).

We last had a go at this a few years ago, when Philippe wrote patches for this:
https://patchew.org/QEMU/[email protected]/
Some of the fixes made it into the tree, but some didn't (either because of lack of review or because review found some changes that needed to be made). I'm going through the remainder as a non-urgent Friday afternoon task...

This patchset deals with two VLAs in the NVME code.

thanks
-- PMM

Quoted 12 lines

Peter Maydell (1):
hw/nvme: Avoid dynamic stack allocation

Philippe Mathieu-Daudé (1):
hw/nvme: Use #define to avoid variable length array

 hw/nvme/ctrl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Signature · 1 line
2.34.1

Thanks Peter,

Applied to nvme-next!

#4 lore raw
Quoted 29 lines · Klaus Jensen
Quoted 27 lines · Peter Maydell

The QEMU codebase has very few C variable length arrays, 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).

We last had a go at this a few years ago, when Philippe wrote patches for this:
https://patchew.org/QEMU/[email protected]/
Some of the fixes made it into the tree, but some didn't (either because of lack of review or because review found some changes that needed to be made). I'm going through the remainder as a non-urgent Friday afternoon task...

This patchset deals with two VLAs in the NVME code.

thanks
-- PMM

Peter Maydell (1):
hw/nvme: Avoid dynamic stack allocation

Philippe Mathieu-Daudé (1):
hw/nvme: Use #define to avoid variable length array

 hw/nvme/ctrl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 7 lines
Quoted 3 lines

--
2.34.1

Thanks Peter,

Applied to nvme-next!

Hi Klaus -- did these patches get lost? They don't seem to have appeared in master yet.

thanks
-- PMM

#5 lore raw
Quoted 36 lines · Peter Maydell
Quoted 35 lines · Klaus Jensen
Quoted 21 lines · Peter Maydell

The QEMU codebase has very few C variable length arrays, 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).

We last had a go at this a few years ago, when Philippe wrote patches for this:
https://patchew.org/QEMU/[email protected]/
Some of the fixes made it into the tree, but some didn't (either because of lack of review or because review found some changes that needed to be made). I'm going through the remainder as a non-urgent Friday afternoon task...

This patchset deals with two VLAs in the NVME code.

thanks
-- PMM

Peter Maydell (1):
hw/nvme: Avoid dynamic stack allocation

Quoted 9 lines

Philippe Mathieu-Daudé (1):
hw/nvme: Use #define to avoid variable length array

 hw/nvme/ctrl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

--
2.34.1

Thanks Peter,

Quoted 8 lines
Quoted 1 line

Applied to nvme-next!

Hi Klaus -- did these patches get lost? They don't seem to have appeared in master yet.

thanks
-- PMM

Oh. I never sent the pull - I'll do that right away! Thanks for the reminder!

Reviewed-by on the v1 series
#6 lore raw
In reply to Peter Maydell
Quoted 5 lines · Peter Maydell

The QEMU codebase has very few C variable length arrays, 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

We last had a go at this a few years ago, when Philippe wrote patches for this:
https://patchew.org/QEMU/[email protected]/
Some of the fixes made it into the tree, but some didn't (either because of lack of review or because review found some changes that needed to be made). I'm going through the remainder as a non-urgent Friday afternoon task...

Thanks for refreshing this, I totally forgot about it :/

Quoted 14 lines

This patchset deals with two VLAs in the NVME code.

thanks
-- PMM

Peter Maydell (1):
hw/nvme: Avoid dynamic stack allocation

Philippe Mathieu-Daudé (1):
hw/nvme: Use #define to avoid variable length array

  hw/nvme/ctrl.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)