hw/nvme: Avoid dynamic stack allocation
authorPeter Maydell <peter.maydell@linaro.org>
Fri, 11 Aug 2023 17:47:51 +0000 (18:47 +0100)
committerKlaus Jensen <k.jensen@samsung.com>
Tue, 12 Sep 2023 14:17:05 +0000 (16:17 +0200)
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).

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Klaus Jensen <k.jensen@samsung.com>
hw/nvme/ctrl.c

index d99a6f5c9a2ef9e5ac06e47c010a8c86a76d75cf..90687b168ae151f912a5f4aec0a46630c2b24a1f 100644 (file)
@@ -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;