Skip to contentSkip to conversation

hw/net/xgmac: Fix buffer overflow in xgmac_enet_send()

PATCH In review Mauro Matteo Cascella 3 participants 5 messages Jul 10, 2020 – Jul 20, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/net/xgmac: Fix buffer overflow in xgmac_enet_send()

A buffer overflow issue was reported by Mr. Ziming Zhang, CC'd here. It occurs while sending an Ethernet frame due to missing break statements and improper checking of the buffer size.

---
 hw/net/xgmac.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
hw/net/xgmac.c +5 -2
index 574dd47b41..b872afbb1a 100644
@@ -224,17 +224,20 @@ static void xgmac_enet_send(XgmacState *s)
             DEBUGF_BRK("qemu:%s:ERROR...ERROR...ERROR... -- "
                         "xgmac buffer 1 len on send > 2048 (0x%x)\n",
                          __func__, bd.buffer1_size & 0xfff);
+            break;
         }
         if ((bd.buffer2_size & 0xfff) != 0) {
             DEBUGF_BRK("qemu:%s:ERROR...ERROR...ERROR... -- "
                         "xgmac buffer 2 len on send != 0 (0x%x)\n",
                         __func__, bd.buffer2_size & 0xfff);
+            break;
         }
-        if (len >= sizeof(frame)) {
+        if (frame_size + len >= sizeof(frame)) {
             DEBUGF_BRK("qemu:%s: buffer overflow %d read into %zu "
-                        "buffer\n" , __func__, len, sizeof(frame));
+                        "buffer\n" , __func__, frame_size + len, sizeof(frame));
             DEBUGF_BRK("qemu:%s: buffer1.size=%d; buffer2.size=%d\n",
                         __func__, bd.buffer1_size, bd.buffer2_size);
+            break;
         }
 
         cpu_physical_memory_read(bd.buffer1_addr, ptr, len);
Signature · 1 line
2.26.2
#1 lore raw
Quoted 28 lines · Mauro Matteo Cascella

A buffer overflow issue was reported by Mr. Ziming Zhang, CC'd here. It occurs while sending an Ethernet frame due to missing break statements and improper checking of the buffer size.

---
 hw/net/xgmac.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
hw/net/xgmac.c +2 -1
index 574dd47b41..b872afbb1a 100644
@@ -224,17 +224,20 @@ static void xgmac_enet_send(XgmacState *s)
             DEBUGF_BRK("qemu:%s:ERROR...ERROR...ERROR... -- "
                         "xgmac buffer 1 len on send > 2048 (0x%x)\n",
                          __func__, bd.buffer1_size & 0xfff);
+            break;
         }
         if ((bd.buffer2_size & 0xfff) != 0) {
             DEBUGF_BRK("qemu:%s:ERROR...ERROR...ERROR... -- "
                         "xgmac buffer 2 len on send != 0 (0x%x)\n",
                         __func__, bd.buffer2_size & 0xfff);
+            break;
         }
-        if (len >= sizeof(frame)) {

hw/net/xgmac.c static void xgmac_enet_send(XgmacState *s) · line 243

Quoted 10 lines
hw/net/xgmac.c +3 -1
+        if (frame_size + len >= sizeof(frame)) {
             DEBUGF_BRK("qemu:%s: buffer overflow %d read into %zu "
-                        "buffer\n" , __func__, len, sizeof(frame));
+                        "buffer\n" , __func__, frame_size + len, sizeof(frame));
             DEBUGF_BRK("qemu:%s: buffer1.size=%d; buffer2.size=%d\n",
                         __func__, bd.buffer1_size, bd.buffer2_size);
+            break;
         }
 
         cpu_physical_memory_read(bd.buffer1_addr, ptr, len);

This is correct in the sense that it avoids the buffer overflow.

I suspect that we should probably also be reporting the error back to the guest via some kind of error flag in the descriptor and/or in a status register. Unfortunately I don't have a copy of the datasheet and it doesn't seem to be available online :-( Does anybody have a copy to check ?

thanks
-- PMM

#2 lore raw
Quoted 40 lines · Peter Maydell
Quoted 27 lines · Mauro Matteo Cascella

A buffer overflow issue was reported by Mr. Ziming Zhang, CC'd here. It occurs while sending an Ethernet frame due to missing break statements and improper checking of the buffer size.

---
  hw/net/xgmac.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
hw/net/xgmac.c +2 -1
index 574dd47b41..b872afbb1a 100644
@@ -224,17 +224,20 @@ static void xgmac_enet_send(XgmacState *s)
              DEBUGF_BRK("qemu:%s:ERROR...ERROR...ERROR... -- "
                          "xgmac buffer 1 len on send > 2048 (0x%x)\n",
                           __func__, bd.buffer1_size & 0xfff);
+            break;
          }
          if ((bd.buffer2_size & 0xfff) != 0) {
              DEBUGF_BRK("qemu:%s:ERROR...ERROR...ERROR... -- "
                          "xgmac buffer 2 len on send != 0 (0x%x)\n",
                          __func__, bd.buffer2_size & 0xfff);
+            break;
          }
-        if (len >= sizeof(frame)) {

hw/net/xgmac.c static void xgmac_enet_send(XgmacState *s) · line 243

Quoted 10 lines
hw/net/xgmac.c +3 -1
+        if (frame_size + len >= sizeof(frame)) {
              DEBUGF_BRK("qemu:%s: buffer overflow %d read into %zu "
-                        "buffer\n" , __func__, len, sizeof(frame));
+                        "buffer\n" , __func__, frame_size + len, sizeof(frame));
              DEBUGF_BRK("qemu:%s: buffer1.size=%d; buffer2.size=%d\n",
                          __func__, bd.buffer1_size, bd.buffer2_size);
+            break;
          }
 
          cpu_physical_memory_read(bd.buffer1_addr, ptr, len);

This is correct in the sense that it avoids the buffer overflow.

Quoted 8 lines

I suspect that we should probably also be reporting the error back to the guest via some kind of error flag in the descriptor and/or in a status register. Unfortunately I don't have a copy of the datasheet and it doesn't seem to be available online :-( Does anybody have a copy to check ?

thanks
-- PMM

I tried to download the datasheet from [1] but it's not a programmer manual.

I think we can apply this patch first and do follow-up fixes on top?

Thanks

[1] https://www.synopsys.com/dw/ipdir.php?ds=dwc_ether_xgmac

Quoted 1 line
Reviewed-by
#3 lore raw
Quoted 50 lines · Jason Wang
Quoted 41 lines · Peter Maydell
Quoted 27 lines · Mauro Matteo Cascella

A buffer overflow issue was reported by Mr. Ziming Zhang, CC'd here. It occurs while sending an Ethernet frame due to missing break statements and improper checking of the buffer size.

---
  hw/net/xgmac.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
hw/net/xgmac.c +2 -1
index 574dd47b41..b872afbb1a 100644
@@ -224,17 +224,20 @@ static void xgmac_enet_send(XgmacState *s)
              DEBUGF_BRK("qemu:%s:ERROR...ERROR...ERROR... -- "
                          "xgmac buffer 1 len on send > 2048 (0x%x)\n",
                           __func__, bd.buffer1_size & 0xfff);
+            break;
          }
          if ((bd.buffer2_size & 0xfff) != 0) {
              DEBUGF_BRK("qemu:%s:ERROR...ERROR...ERROR... -- "
                          "xgmac buffer 2 len on send != 0 (0x%x)\n",
                          __func__, bd.buffer2_size & 0xfff);
+            break;
          }
-        if (len >= sizeof(frame)) {

hw/net/xgmac.c static void xgmac_enet_send(XgmacState *s) · line 243

Quoted 10 lines
hw/net/xgmac.c +3 -1
+        if (frame_size + len >= sizeof(frame)) {
              DEBUGF_BRK("qemu:%s: buffer overflow %d read into %zu "
-                        "buffer\n" , __func__, len, sizeof(frame));
+                        "buffer\n" , __func__, frame_size + len, sizeof(frame));
              DEBUGF_BRK("qemu:%s: buffer1.size=%d; buffer2.size=%d\n",
                          __func__, bd.buffer1_size, bd.buffer2_size);
+            break;
          }
 
          cpu_physical_memory_read(bd.buffer1_addr, ptr, len);

This is correct in the sense that it avoids the buffer overflow.

Quoted 5 lines

I suspect that we should probably also be reporting the error back to the guest via some kind of error flag in the descriptor and/or in a status register. Unfortunately I don't have a copy of the datasheet and it doesn't seem to be available online :-( Does anybody have a copy to check ?

Quoted 4 lines

I tried to download the datasheet from [1] but it's not a programmer manual.

I think we can apply this patch first and do follow-up fixes on top?

Yes, agreed, since nobody seems to have docs for this device. This is an only-happens-with-malicious-or-buggy-guest case anyway, and the highbank/midway (only users of the device) aren't important or widely used machine types any more.

I suppose we could add a comment
/*
* FIXME: these cases of malformed tx descriptors (bad sizes)
* should probably be reported back to the guest somehow
* rather than simply silently stopping processing, but we
* don't know what the hardware does in this situation.
* This will only happen for buggy guests anyway. */

but anyway

Were you planning to take this patch, Jason, or should I?

thanks
-- PMM

#4 lore raw
Quoted 66 lines · Peter Maydell
Quoted 47 lines · Jason Wang
Quoted 37 lines · Peter Maydell
Quoted 36 lines · Mauro Matteo Cascella

A buffer overflow issue was reported by Mr. Ziming Zhang, CC'd here. It occurs while sending an Ethernet frame due to missing break statements and improper checking of the buffer size.

---
   hw/net/xgmac.c | 7 +++++--
   1 file changed, 5 insertions(+), 2 deletions(-)
hw/net/xgmac.c +5 -2
index 574dd47b41..b872afbb1a 100644
@@ -224,17 +224,20 @@ static void xgmac_enet_send(XgmacState *s)
               DEBUGF_BRK("qemu:%s:ERROR...ERROR...ERROR... -- "
                           "xgmac buffer 1 len on send > 2048 (0x%x)\n",
                            __func__, bd.buffer1_size & 0xfff);
+            break;
           }
           if ((bd.buffer2_size & 0xfff) != 0) {
               DEBUGF_BRK("qemu:%s:ERROR...ERROR...ERROR... -- "
                           "xgmac buffer 2 len on send != 0 (0x%x)\n",
                           __func__, bd.buffer2_size & 0xfff);
+            break;
           }
-        if (len >= sizeof(frame)) {
+        if (frame_size + len >= sizeof(frame)) {
               DEBUGF_BRK("qemu:%s: buffer overflow %d read into %zu "
-                        "buffer\n" , __func__, len, sizeof(frame));
+                        "buffer\n" , __func__, frame_size + len, sizeof(frame));
               DEBUGF_BRK("qemu:%s: buffer1.size=%d; buffer2.size=%d\n",
                           __func__, bd.buffer1_size, bd.buffer2_size);
+            break;
           }
 
Quoted 8 lines

hw/net/xgmac.c static void xgmac_enet_send(XgmacState *s) · line 243

Quoted 1 line
hw/net/xgmac.c +0 -0
           cpu_physical_memory_read(bd.buffer1_addr, ptr, len);

This is correct in the sense that it avoids the buffer overflow.

I suspect that we should probably also be reporting the error back to the guest via some kind of error flag in the descriptor and/or in a status register. Unfortunately I don't have a copy of the datasheet and it doesn't seem to be available online :-( Does anybody have a copy to check ?

Quoted 4 lines

I tried to download the datasheet from [1] but it's not a programmer manual.

I think we can apply this patch first and do follow-up fixes on top?

Yes, agreed, since nobody seems to have docs for this device. This is an only-happens-with-malicious-or-buggy-guest case anyway, and the highbank/midway (only users of the device) aren't important or widely used machine types any more.

I suppose we could add a comment
/*
* FIXME: these cases of malformed tx descriptors (bad sizes)
* should probably be reported back to the guest somehow
* rather than simply silently stopping processing, but we
* don't know what the hardware does in this situation.
* This will only happen for buggy guests anyway. */

Quoted 4 lines

but anyway

Were you planning to take this patch, Jason, or should I?

I've queued for rc1 this with the above suggested comment.

Thanks

Quoted 4 lines

thanks
-- PMM