Blame SOURCES/0001-mu-Remove-use-of-VLAs-for-Marshalling-TPML-types.patch

e56592
From 58ee0fd916671942e62ac9930f18225761a6dd66 Mon Sep 17 00:00:00 2001
e56592
From: Joe Richey <joerichey@google.com>
e56592
Date: Tue, 21 Jan 2020 20:04:45 -0800
e56592
Subject: [PATCH] mu: Remove use of VLAs for Marshalling TPML types
e56592
e56592
All of the `Tss2_MU_*_Marshal()` functions have the property that
e56592
`buffer` can be NULL, `offset` can be NULL, but both cannot be
e56592
NULL. Some Marshal functions check this directly (returning
e56592
`TSS2_MU_RC_BAD_REFERENCE` on error), but most do this by composing
e56592
existing Marshalling functions together.
e56592
e56592
The TMPL Marshal functions does things differently, it creates a local
e56592
VLA `local_buffer[buffer_size]` and uses that as the buffer pointer if
e56592
a NULL buffer is given. This is unnecessary, as this pointer is only
e56592
used for debug logging and passed to other Marshalling functions, which
e56592
will correctly handle a NULL buffer.
e56592
e56592
Note that the VLA in the existing code is of length `buffer_size` (the
e56592
length of the _entire_ buffer, _not_ the length of the data being
e56592
unmarshaled). This can potentially result in a very large stack
e56592
allocation, or stack overflow.
e56592
e56592
Signed-off-by: Joe Richey <joerichey@google.com>
e56592
---
e56592
 src/tss2-mu/tpml-types.c | 11 +++--------
e56592
 1 file changed, 3 insertions(+), 8 deletions(-)
e56592
e56592
diff --git a/src/tss2-mu/tpml-types.c b/src/tss2-mu/tpml-types.c
e56592
index 9506a26efd14..ae1ed6177d75 100644
e56592
--- a/src/tss2-mu/tpml-types.c
e56592
+++ b/src/tss2-mu/tpml-types.c
e56592
@@ -29,8 +29,6 @@ TSS2_RC Tss2_MU_##type##_Marshal(type const *src, uint8_t buffer[], \
e56592
     size_t  local_offset = 0; \
e56592
     UINT32 i, count = 0; \
e56592
     TSS2_RC ret = TSS2_RC_SUCCESS; \
e56592
-    uint8_t *buf_ptr = buffer; \
e56592
-    uint8_t local_buffer[buffer_size]; \
e56592
 \
e56592
     if (offset != NULL) { \
e56592
         LOG_TRACE("offset non-NULL, initial value: %zu", *offset); \
e56592
@@ -60,24 +58,21 @@ TSS2_RC Tss2_MU_##type##_Marshal(type const *src, uint8_t buffer[], \
e56592
         LOG_WARNING("count too big"); \
e56592
         return TSS2_SYS_RC_BAD_VALUE; \
e56592
     } \
e56592
-\
e56592
-    if (buf_ptr == NULL) \
e56592
-        buf_ptr = local_buffer; \
e56592
 \
e56592
     LOG_DEBUG(\
e56592
          "Marshalling " #type " from 0x%" PRIxPTR " to buffer 0x%" PRIxPTR \
e56592
          " at index 0x%zx", \
e56592
          (uintptr_t)&src, \
e56592
-         (uintptr_t)buf_ptr, \
e56592
+         (uintptr_t)buffer, \
e56592
          local_offset); \
e56592
 \
e56592
-    ret = Tss2_MU_UINT32_Marshal(src->count, buf_ptr, buffer_size, &local_offset); \
e56592
+    ret = Tss2_MU_UINT32_Marshal(src->count, buffer, buffer_size, &local_offset); \
e56592
     if (ret) \
e56592
         return ret; \
e56592
 \
e56592
     for (i = 0; i < src->count; i++) \
e56592
     { \
e56592
-        ret = marshal_func(op src->buf_name[i], buf_ptr, buffer_size, &local_offset); \
e56592
+        ret = marshal_func(op src->buf_name[i], buffer, buffer_size, &local_offset); \
e56592
         if (ret) \
e56592
             return ret; \
e56592
     } \
e56592
-- 
e56592
2.27.0
e56592