Skip to contentSkip to conversation

i386/sev: Ensure attestation report length is valid before retrieving

PATCH In review Tyler Fanelli 3 participants 3 messages Mar 4, 2022 – Mar 7, 2022 KVM

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 i386/sev: Ensure attestation report length is valid before retrieving

The length of the attestation report buffer is never checked to be valid before allocation is made. If the length of the report is returned to be 0, the buffer to retrieve the attestation buffer is allocated with length 0 and passed to the kernel to fill with contents of the attestation report. Leaving this unchecked is dangerous and could lead to undefined behavior.

---
 target/i386/sev.c | 7 +++++++
 1 file changed, 7 insertions(+)
target/i386/sev.c +7 -0
index 025ff7a6f8..e82be3e350 100644
@@ -616,6 +616,8 @@ static SevAttestationReport *sev_get_attestation_report(const char *mnonce,
         return NULL;
     }
 
+    input.len = 0;
+
     /* Query the report length */
     ret = sev_ioctl(sev->sev_fd, KVM_SEV_GET_ATTESTATION_REPORT,
             &input, &err);
@@ -626,6 +628,11 @@ static SevAttestationReport *sev_get_attestation_report(const char *mnonce,
                        ret, err, fw_error_to_str(err));
             return NULL;
         }
+    } else if (input.len == 0) {
+        error_setg(errp, "SEV: Failed to query attestation report:"
+                         " length returned=%u",
+                   input.len);
+        return NULL;
     }
 
     data = g_malloc(input.len);
Signature · 1 line
2.31.1
#1 lore raw
Quoted 17 lines · Tyler Fanelli

The length of the attestation report buffer is never checked to be valid before allocation is made. If the length of the report is returned to be 0, the buffer to retrieve the attestation buffer is allocated with length 0 and passed to the kernel to fill with contents of the attestation report. Leaving this unchecked is dangerous and could lead to undefined behavior.

---
  target/i386/sev.c | 7 +++++++
  1 file changed, 7 insertions(+)
target/i386/sev.c +0 -0
index 025ff7a6f8..e82be3e350 100644

target/i386/sev.c static SevAttestationReport *sev_get_attestation_report(const char *mnonce, · line 619

Quoted 4 lines
target/i386/sev.c +1 -0
@@ -616,6 +616,8 @@ static SevAttestationReport *sev_get_attestation_report(const char *mnonce,
          return NULL;
      }
 
+    input.len = 0;

I agree with Daniel's review of your v1:

"The declaration of 'input' already zero initializes."

https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 15 lines
target/i386/sev.c +5 -0
      /* Query the report length */
      ret = sev_ioctl(sev->sev_fd, KVM_SEV_GET_ATTESTATION_REPORT,
              &input, &err);
@@ -626,6 +628,11 @@ static SevAttestationReport *sev_get_attestation_report(const char *mnonce,
                         ret, err, fw_error_to_str(err));
              return NULL;
          }
+    } else if (input.len == 0) {
+        error_setg(errp, "SEV: Failed to query attestation report:"
+                         " length returned=%u",
+                   input.len);
+        return NULL;
      }
 
      data = g_malloc(input.len);
#2 lore raw
In reply to Tyler Fanelli
Quoted 26 lines · Tyler Fanelli

The length of the attestation report buffer is never checked to be valid before allocation is made. If the length of the report is returned to be 0, the buffer to retrieve the attestation buffer is allocated with length 0 and passed to the kernel to fill with contents of the attestation report. Leaving this unchecked is dangerous and could lead to undefined behavior.

---
 target/i386/sev.c | 7 +++++++
 1 file changed, 7 insertions(+)
target/i386/sev.c +2 -0
index 025ff7a6f8..e82be3e350 100644
@@ -616,6 +616,8 @@ static SevAttestationReport *sev_get_attestation_report(const char *mnonce,
         return NULL;
     }
 
+    input.len = 0;
+
     /* Query the report length */
     ret = sev_ioctl(sev->sev_fd, KVM_SEV_GET_ATTESTATION_REPORT,
             &input, &err);

target/i386/sev.c static SevAttestationReport *sev_get_attestation_report(const char *mnonce, · line 635

Quoted 8 lines
target/i386/sev.c +5 -0
@@ -626,6 +628,11 @@ static SevAttestationReport *sev_get_attestation_report(const char *mnonce,
                        ret, err, fw_error_to_str(err));
             return NULL;
         }
+    } else if (input.len == 0) {
+        error_setg(errp, "SEV: Failed to query attestation report:"
+                         " length returned=%u",
+                   input.len);
+        return NULL;

I still feel the described scenario is a kernel bug, as QEMU handles len == 0 safely already AFAICT. I can't see how the upstream kernel would end up in the problem state. Can you show the buggy kernel code upstream.

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|