Skip to contentSkip to conversation

memset/sizeof abuse

PATCH Applied Jim Meyering 5 participants 11 messages May 10, 2012 – Jun 11, 2012

lore
Share & tools
lore
#0 lore raw

cover memset/sizeof abuse

From: Jim Meyering <[email protected]>

I ran coverity on all of qemu and have begun going through the results. A couple problems jumped out as obvious and easy to fix:

Jim Meyering (2):
kvm/apic: correct short memset
cadence_gem: avoid stack-writing buffer-overrun

 hw/cadence_gem.c | 2 +-
 hw/kvm/apic.c    | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

--
1.7.10.1.487.ga3935e6

#1 lore raw

PATCH 1/2 kvm/apic: correct short memset

From: Jim Meyering <[email protected]>

kvm_put_apic_state's attempt to clear *kapic before setting its bits cleared sizeof(void*) bytes (no more than 8) rather than the intended 1024 (KVM_APIC_REG_SIZE) bytes. Spotted by coverity.

---
 hw/kvm/apic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/kvm/apic.c +1 -1
index ffe7a52..a0ab503 100644
@@ -29,7 +29,7 @@ void kvm_put_apic_state(DeviceState *d, struct kvm_lapic_state *kapic)
     APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
     int i;
 
-    memset(kapic, 0, sizeof(kapic));
+    memset(kapic, 0, sizeof(*kapic));
     kvm_apic_set_reg(kapic, 0x2, s->id << 24);
     kvm_apic_set_reg(kapic, 0x8, s->tpr);
     kvm_apic_set_reg(kapic, 0xd, s->log_dest << 24);
Signature · 1 line
1.7.10.1.487.ga3935e6
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] kvm/apic: correct short memset

Quoted 16 lines · Jim Meyering

From: Jim Meyering <[email protected]>

kvm_put_apic_state's attempt to clear *kapic before setting its bits cleared sizeof(void*) bytes (no more than 8) rather than the intended 1024 (KVM_APIC_REG_SIZE) bytes. Spotted by coverity.

---
 hw/kvm/apic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/kvm/apic.c +0 -0
index ffe7a52..a0ab503 100644

hw/kvm/apic.c void kvm_put_apic_state(DeviceState *d, struct kvm_lapic_state *kapic) · line 35

Quoted 8 lines
hw/kvm/apic.c +1 -1
@@ -29,7 +29,7 @@ void kvm_put_apic_state(DeviceState *d, struct kvm_lapic_state *kapic)
     APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
     int i;
 
-    memset(kapic, 0, sizeof(kapic));
+    memset(kapic, 0, sizeof(*kapic));
     kvm_apic_set_reg(kapic, 0x2, s->id << 24);
     kvm_apic_set_reg(kapic, 0x8, s->tpr);
     kvm_apic_set_reg(kapic, 0xd, s->log_dest << 24);

Yep, that's what I actually meant...

Thanks,
Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
Reviewed-by
#3 lore raw

Re: [Qemu-devel] [PATCH 1.1 1/2] kvm/apic: correct short memset

Am 10.05.2012 19:28, schrieb Jan Kiszka:

Quoted 29 lines · Jim Meyering
Quoted 18 lines

From: Jim Meyering<[email protected]>

kvm_put_apic_state's attempt to clear *kapic before setting its bits cleared sizeof(void*) bytes (no more than 8) rather than the intended 1024 (KVM_APIC_REG_SIZE) bytes. Spotted by coverity.

---
  hw/kvm/apic.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/kvm/apic.c +0 -0
index ffe7a52..a0ab503 100644

hw/kvm/apic.c void kvm_put_apic_state(DeviceState *d, struct kvm_lapic_state *kapic) · line 35

Quoted 8 lines
hw/kvm/apic.c +1 -1
@@ -29,7 +29,7 @@ void kvm_put_apic_state(DeviceState *d, struct kvm_lapic_state *kapic)
      APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
      int i;
 
-    memset(kapic, 0, sizeof(kapic));
+    memset(kapic, 0, sizeof(*kapic));
      kvm_apic_set_reg(kapic, 0x2, s->id<<  24);
      kvm_apic_set_reg(kapic, 0x8, s->tpr);
      kvm_apic_set_reg(kapic, 0xd, s->log_dest<<  24);

Yep, that's what I actually meant...

Quoted 4 lines

Thanks,
Jan

Hello Anthony,

this patch should be committed to QEMU 1.1. I had sent a patch with the same fix 6 days later.

Regards,

Stefan W.

#4 lore raw

Re: [Qemu-devel] [PATCH 1.1 1/2] kvm/apic: correct short memset

Quoted 35 lines · Stefan Weil

Am 10.05.2012 19:28, schrieb Jan Kiszka:

Quoted 29 lines · Jim Meyering
Quoted 18 lines

From: Jim Meyering<[email protected]>

kvm_put_apic_state's attempt to clear *kapic before setting its bits cleared sizeof(void*) bytes (no more than 8) rather than the intended 1024 (KVM_APIC_REG_SIZE) bytes. Spotted by coverity.

---
  hw/kvm/apic.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/kvm/apic.c +0 -0
index ffe7a52..a0ab503 100644

hw/kvm/apic.c void kvm_put_apic_state(DeviceState *d, struct kvm_lapic_state *kapic) · line 35

Quoted 8 lines
hw/kvm/apic.c +1 -1
@@ -29,7 +29,7 @@ void kvm_put_apic_state(DeviceState *d, struct kvm_lapic_state *kapic)
      APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
      int i;
 
-    memset(kapic, 0, sizeof(kapic));
+    memset(kapic, 0, sizeof(*kapic));
      kvm_apic_set_reg(kapic, 0x2, s->id<<  24);
      kvm_apic_set_reg(kapic, 0x8, s->tpr);
      kvm_apic_set_reg(kapic, 0xd, s->log_dest<<  24);

Yep, that's what I actually meant...

Quoted 4 lines

Thanks,
Jan

Quoted 6 lines

Hello Anthony,

this patch should be committed to QEMU 1.1. I had sent a patch with the same fix 6 days later.

Thanks for reminding. Yes, please merge!

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
#5 lore raw

Re: [Qemu-devel] [PATCH 1.1 1/2] kvm/apic: correct short memset

Am 23.05.2012 12:33, schrieb Jan Kiszka:

Quoted 38 lines · Stefan Weil
Quoted 37 lines

Am 10.05.2012 19:28, schrieb Jan Kiszka:

Quoted 29 lines · Jim Meyering
Quoted 18 lines

From: Jim Meyering<[email protected]>

kvm_put_apic_state's attempt to clear *kapic before setting its bits cleared sizeof(void*) bytes (no more than 8) rather than the intended 1024 (KVM_APIC_REG_SIZE) bytes. Spotted by coverity.

---
   hw/kvm/apic.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
hw/kvm/apic.c +0 -0
index ffe7a52..a0ab503 100644

hw/kvm/apic.c void kvm_put_apic_state(DeviceState *d, struct kvm_lapic_state *kapic) · line 35

Quoted 8 lines
hw/kvm/apic.c +1 -1
@@ -29,7 +29,7 @@ void kvm_put_apic_state(DeviceState *d, struct kvm_lapic_state *kapic)
       APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
       int i;
 
-    memset(kapic, 0, sizeof(kapic));
+    memset(kapic, 0, sizeof(*kapic));
       kvm_apic_set_reg(kapic, 0x2, s->id<<   24);
       kvm_apic_set_reg(kapic, 0x8, s->tpr);
       kvm_apic_set_reg(kapic, 0xd, s->log_dest<<   24);

Yep, that's what I actually meant...

Quoted 4 lines

Thanks,
Jan

Quoted 8 lines
Quoted 4 lines

Hello Anthony,

this patch should be committed to QEMU 1.1. I had sent a patch with the same fix 6 days later.

Thanks for reminding. Yes, please merge!

Jan

Ping?

This is one of the bug fixes which is missing in QEMU 1.1, and it is also missing in latest QEMU git master.

What can be done to get it committed?

Cheers,

Stefan W.

#6 lore raw

Re: [Qemu-devel] [PATCH 1.1 1/2] kvm/apic: correct short memset

Quoted 49 lines · Stefan Weil

Am 23.05.2012 12:33, schrieb Jan Kiszka:

Quoted 38 lines · Stefan Weil
Quoted 37 lines

Am 10.05.2012 19:28, schrieb Jan Kiszka:

Quoted 29 lines · Jim Meyering
Quoted 18 lines

From: Jim Meyering<[email protected]>

kvm_put_apic_state's attempt to clear *kapic before setting its bits cleared sizeof(void*) bytes (no more than 8) rather than the intended 1024 (KVM_APIC_REG_SIZE) bytes. Spotted by coverity.

---
   hw/kvm/apic.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
hw/kvm/apic.c +0 -0
index ffe7a52..a0ab503 100644

hw/kvm/apic.c void kvm_put_apic_state(DeviceState *d, struct kvm_lapic_state *kapic) · line 35

Quoted 8 lines
hw/kvm/apic.c +1 -1
@@ -29,7 +29,7 @@ void kvm_put_apic_state(DeviceState *d, struct kvm_lapic_state *kapic)
       APICCommonState *s = DO_UPCAST(APICCommonState, busdev.qdev, d);
       int i;
 
-    memset(kapic, 0, sizeof(kapic));
+    memset(kapic, 0, sizeof(*kapic));
       kvm_apic_set_reg(kapic, 0x2, s->id<<   24);
       kvm_apic_set_reg(kapic, 0x8, s->tpr);
       kvm_apic_set_reg(kapic, 0xd, s->log_dest<<   24);

Yep, that's what I actually meant...

Quoted 4 lines

Thanks,
Jan

Quoted 8 lines
Quoted 4 lines

Hello Anthony,

this patch should be committed to QEMU 1.1. I had sent a patch with the same fix 6 days later.

Thanks for reminding. Yes, please merge!

Jan

Quoted 6 lines

Ping?

This is one of the bug fixes which is missing in QEMU 1.1, and it is also missing in latest QEMU git master.

What can be done to get it committed?

Avi or Marcelo, please queue in uq/master and send a pull soon!

That reminds me that [1] is still awaiting comments (and further testing). Critical for qemu-kvm 1.1 and qemu 1.1.1 as well.

Jan

[1] http://thread.gmane.org/gmane.comp.emulators.kvm.devel/92036

Signature · 2 lines
Siemens AG, Corporate Technology, CT T DE IT 1
Corporate Competence Center Embedded Linux
Applied
#7 lore raw

Re: [Qemu-devel] [PATCH 1/2] kvm/apic: correct short memset

In reply to Jim Meyering
Quoted 5 lines · Jim Meyering

From: Jim Meyering <[email protected]>

kvm_put_apic_state's attempt to clear *kapic before setting its bits cleared sizeof(void*) bytes (no more than 8) rather than the intended 1024 (KVM_APIC_REG_SIZE) bytes. Spotted by coverity.

Thanks, applied to uq/master.

Signature · 1 line
error compiling committee.c: too many arguments to function
#8 lore raw

PATCH 2/2 cadence_gem: avoid stack-writing buffer-overrun

In reply to Jim Meyering

From: Jim Meyering <[email protected]>

Use sizeof(rxbuf)-size (not sizeof(rxbuf-size)) as the number of bytes to clear. The latter would always clear 4 or 8 bytes, possibly writing beyond the end of that stack buffer. Alternatively, depending on the value of the "size" parameter, it could fail to initialize the end of "rxbuf". Spotted by coverity.

---
 hw/cadence_gem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/cadence_gem.c +1 -1
index e2140ae..dbde392 100644
@@ -664,7 +664,7 @@ static ssize_t gem_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
          */
 
         memcpy(rxbuf, buf, size);
-        memset(rxbuf + size, 0, sizeof(rxbuf - size));
+        memset(rxbuf + size, 0, sizeof(rxbuf) - size);
         rxbuf_ptr = rxbuf;
         crc_val = cpu_to_le32(crc32(0, rxbuf, MAX(size, 60)));
         if (size < 60) {
Signature · 1 line
1.7.10.1.487.ga3935e6
Reviewed-by on v1 2/2
#9 lore raw

Re: [Qemu-devel] [PATCH 2/2] cadence_gem: avoid stack-writing buffer-overrun

ACK and Thanks Jim,

Quoted 30 lines · Jim Meyering

From: Jim Meyering <[email protected]>

Use sizeof(rxbuf)-size (not sizeof(rxbuf-size)) as the number of bytes to clear.  The latter would always clear 4 or 8 bytes, possibly writing beyond the end of that stack buffer. Alternatively, depending on the value of the "size" parameter, it could fail to initialize the end of "rxbuf". Spotted by coverity.

---
 hw/cadence_gem.c | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
hw/cadence_gem.c +1 -2
index e2140ae..dbde392 100644
@@ -664,7 +664,7 @@ static ssize_t gem_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
          */
 
         memcpy(rxbuf, buf, size);
-        memset(rxbuf + size, 0, sizeof(rxbuf - size));
+        memset(rxbuf + size, 0, sizeof(rxbuf) - size);
         rxbuf_ptr = rxbuf;
         crc_val = cpu_to_le32(crc32(0, rxbuf, MAX(size, 60)));
         if (size < 60) {
--

1.7.10.1.487.ga3935e6

#10 lore raw

Re: [Qemu-devel] [PATCH 2/2] cadence_gem: avoid stack-writing buffer-overrun

Am 14.05.2012 06:57, schrieb Peter Crosthwaite:

Quoted 27 lines

ACK and Thanks Jim,

Quoted 21 lines · Jim Meyering

From: Jim Meyering<[email protected]>

Use sizeof(rxbuf)-size (not sizeof(rxbuf-size)) as the number of bytes to clear. The latter would always clear 4 or 8 bytes, possibly writing beyond the end of that stack buffer. Alternatively, depending on the value of the "size" parameter, it could fail to initialize the end of "rxbuf". Spotted by coverity.

---
  hw/cadence_gem.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/cadence_gem.c +0 -0
index e2140ae..dbde392 100644
@@ -664,7 +664,7 @@ static ssize_t gem_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
          */
 
Quoted 9 lines
Quoted 9 lines
hw/cadence_gem.c +1 -2
         memcpy(rxbuf, buf, size);
-        memset(rxbuf + size, 0, sizeof(rxbuf - size));
+        memset(rxbuf + size, 0, sizeof(rxbuf) - size);
         rxbuf_ptr = rxbuf;
         crc_val = cpu_to_le32(crc32(0, rxbuf, MAX(size, 60)));
         if (size<  60) {
--

1.7.10.1.487.ga3935e6

Ping. This patch is still missing in 1.1 and master.