Skip to contentSkip to conversation

accel/kvm: coalesced mmio: check lenght of the MMIO write entry

PATCH Applied Maxim Levitsky 3 participants 6 messages Sep 23 – Sep 24

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 accel/kvm: coalesced mmio: check lenght of the MMIO write entry

KVM's coalesced mmio ring buffer has entries that contain deffered MMIO writes.

Each such entry contains the length, an 8 byte value and a guest address.

While the length value comes from the kernel, it is still possible for an attacker to corrupt the length field using another exploit.

The attacker can then invoke kvm_flush_coalesced_mmio_buffer, which blindly trusts the length and can be used to inflict further damage.

Add a sanity check on the length field to prevent this.

This patch was only compile tested.

Reported by: "Labs, STAR" <[email protected]>
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3863

---
 accel/kvm/kvm-all.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
accel/kvm/kvm-all.c +9 -2
index 83cbd120a847..82898fd89148 100644
@@ -3174,8 +3174,15 @@ void kvm_flush_coalesced_mmio_buffer(void)
 
             ent = &ring->coalesced_mmio[ring->first];
             as = ent->pio == 1 ? &address_space_io : &address_space_memory;
-            address_space_write(as, ent->phys_addr, MEMTXATTRS_UNSPECIFIED,
-                                ent->data, ent->len);
+
+            if (ent->len > sizeof(ent->data)) {
+                warn_report("coalesced MMIO entry has invalid len %u",
+                            ent->len);
+            } else {
+                address_space_write(as, ent->phys_addr, MEMTXATTRS_UNSPECIFIED,
+                                    ent->data, ent->len);
+            }
+
             smp_wmb();
             ring->first = (ring->first + 1) % KVM_COALESCED_MMIO_MAX;
         }
Signature · 1 line
2.54.0
#1 lore raw

Hi Maxim,

Quoted 27 lines · Maxim Levitsky

KVM's coalesced mmio ring buffer has entries that contain deffered MMIO writes.

Each such entry contains the length, an 8 byte value and a guest address.

While the length value comes from the kernel, it is still possible for an attacker to corrupt the length field using another exploit.

The attacker can then invoke kvm_flush_coalesced_mmio_buffer, which blindly trusts the length and can be used to inflict further damage.

Add a sanity check on the length field to prevent this.

This patch was only compile tested.

Reported by: "Labs, STAR" <[email protected]>
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3863

---
  accel/kvm/kvm-all.c | 11 +++++++++--
  1 file changed, 9 insertions(+), 2 deletions(-)
accel/kvm/kvm-all.c +0 -0
index 83cbd120a847..82898fd89148 100644

accel/kvm/kvm-all.c void kvm_flush_coalesced_mmio_buffer(void) · line 3175

Quoted 2 lines
accel/kvm/kvm-all.c +0 -0
@@ -3174,8 +3174,15 @@ void kvm_flush_coalesced_mmio_buffer(void)
 
              ent = &ring->coalesced_mmio[ring->first];

Could we move the check here?

Do we want to keep processing the other entries if the current %len is broken or break and return?

Quoted 15 lines
accel/kvm/kvm-all.c +9 -2
              as = ent->pio == 1 ? &address_space_io : &address_space_memory;
-            address_space_write(as, ent->phys_addr, MEMTXATTRS_UNSPECIFIED,
-                                ent->data, ent->len);
+
+            if (ent->len > sizeof(ent->data)) {
+                warn_report("coalesced MMIO entry has invalid len %u",
+                            ent->len);
+            } else {
+                address_space_write(as, ent->phys_addr, MEMTXATTRS_UNSPECIFIED,
+                                    ent->data, ent->len);
+            }
+
              smp_wmb();
              ring->first = (ring->first + 1) % KVM_COALESCED_MMIO_MAX;
          }
#2 lore raw
Quoted 25 lines · Philippe Mathieu-Daudé

Hi Maxim,

Quoted 22 lines · Maxim Levitsky

KVM's coalesced mmio ring buffer has entries that contain deffered MMIO writes.

Each such entry contains the length, an 8 byte value and a guest address.

While the length value comes from the kernel, it is still possible for an attacker to corrupt the length field using another exploit.

The attacker can then invoke kvm_flush_coalesced_mmio_buffer, which blindly trusts the length and can be used to inflict further damage.

Add a sanity check on the length field to prevent this.

This patch was only compile tested.

Reported by: "Labs, STAR" <[email protected]>
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3863

---
  accel/kvm/kvm-all.c | 11 +++++++++--

  1 file changed, 9 insertions(+), 2 deletions(-)
Quoted 9 lines
Quoted 7 lines
accel/kvm/kvm-all.c +0 -0
index 83cbd120a847..82898fd89148 100644
@@ -3174,8 +3174,15 @@ void kvm_flush_coalesced_mmio_buffer(void)


              ent = &ring->coalesced_mmio[ring->first];

Could we move the check here?

Yes.

Quoted 3 lines

Do we want to keep processing the other entries if the current %len is broken or break and return?

Honestly this is such a theoretical situation that I don't know what is better here. One could say that skipping non corrupted writes also gives some advantage to the attacker.

This patch goal is mostly to silence the static analysis tools (read: AI), because technically this is a buffer overflow.

Best regards,
Maxim Levitsky

Quoted 16 lines
Quoted 16 lines

              as = ent->pio == 1 ? &address_space_io : &address_space_memory; -            address_space_write(as, ent->phys_addr, MEMTXATTRS_UNSPECIFIED, -                                ent->data, ent->len);

accel/kvm/kvm-all.c +9 -0
+
+            if (ent->len > sizeof(ent->data)) {
+                warn_report("coalesced MMIO entry has invalid len %u",
+                            ent->len);
+            } else {
+                address_space_write(as, ent->phys_addr, MEMTXATTRS_UNSPECIFIED,
+                                    ent->data, ent->len);
+            }
+

              smp_wmb();
              ring->first = (ring->first + 1) % KVM_COALESCED_MMIO_MAX; }

#3 lore raw
In reply to Maxim Levitsky

Queued, thanks. I changed the code to what Philippe suggested:

            ent = &ring->coalesced_mmio[ring->first];
            if (ent->len > sizeof(ent->data)) {
                warn_report("coalesced MMIO entry has invalid len %u",
                            ent->len);
            } else {
                as = ent->pio == 1 ? &address_space_io : &address_space_memory;
                address_space_write(as, ent->phys_addr, MEMTXATTRS_UNSPECIFIED,
                                    ent->data, ent->len);
            }

Paolo

#4 lore raw
Quoted 11 lines · Paolo Bonzini

Queued, thanks. I changed the code to what Philippe suggested:

             ent = &ring->coalesced_mmio[ring->first];
             if (ent->len > sizeof(ent->data)) {
                 warn_report("coalesced MMIO entry has invalid len %u",
                             ent->len);
             } else {
                 as = ent->pio == 1 ? &address_space_io : &address_space_memory;
                 address_space_write(as, ent->phys_addr, MEMTXATTRS_UNSPECIFIED,
                                     ent->data, ent->len);
             }

Thank you!