Skip to contentSkip to conversation

hw/net/dp8393x: fix integer underflow in dp8393x_do_transmit_packets()

PATCH Applied Mauro Matteo Cascella 4 participants 5 messages Nov 24, 2020 – Dec 1, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/net/dp8393x: fix integer underflow in dp8393x_do_transmit_packets()

An integer underflow could occur during packet transmission due to 'tx_len' not being updated if SONIC_TFC register is set to zero. Check for negative 'tx_len' when removing existing FCS.

RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=1899722

---
 hw/net/dp8393x.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/net/dp8393x.c +4 -0
index 674b04b354..205c0decc5 100644
@@ -495,6 +495,10 @@ static void dp8393x_do_transmit_packets(dp8393xState *s)
         } else {
             /* Remove existing FCS */
             tx_len -= 4;
+            if (tx_len < 0) {
+                SONIC_ERROR("tx_len is %d\n", tx_len);
+                break;
+            }
         }
 
         if (s->regs[SONIC_RCR] & (SONIC_RCR_LB1 | SONIC_RCR_LB0)) {
Signature · 1 line
2.28.0
#1 lore raw

+Laurent/Finn

Quoted 17 lines · Mauro Matteo Cascella

An integer underflow could occur during packet transmission due to 'tx_len' not being updated if SONIC_TFC register is set to zero. Check for negative 'tx_len' when removing existing FCS.

RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=1899722

---
 hw/net/dp8393x.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/net/dp8393x.c +0 -0
index 674b04b354..205c0decc5 100644

hw/net/dp8393x.c static void dp8393x_do_transmit_packets(dp8393xState *s) · line 504

Quoted 10 lines
hw/net/dp8393x.c +4 -0
@@ -495,6 +495,10 @@ static void dp8393x_do_transmit_packets(dp8393xState *s)
         } else {
             /* Remove existing FCS */
             tx_len -= 4;
+            if (tx_len < 0) {
+                SONIC_ERROR("tx_len is %d\n", tx_len);
+                break;
+            }
         }
 
         if (s->regs[SONIC_RCR] & (SONIC_RCR_LB1 | SONIC_RCR_LB0)) {

Doesn't it make more sense to check 'tx_len >= 4'
and skip tx/rx when 'tx_len == 0'?

-- >8 --
@@ -488,25 +488,29 @@ static void
dp8393x_do_transmit_packets(dp8393xState *s)

             }
         }

- /* Handle Ethernet checksum */
- if (!(s->regs[SONIC_TCR] & SONIC_TCR_CRCI)) {
- /* Don't append FCS there, to look like slirp packets
- * which don't have one */
- } else {
- /* Remove existing FCS */
- tx_len -= 4;
+ if (tx_len >= 4) {
+ /* Handle Ethernet checksum */
+ if (!(s->regs[SONIC_TCR] & SONIC_TCR_CRCI)) {
+ /* Don't append FCS there, to look like slirp packets
+ * which don't have one */
+ } else {
+ /* Remove existing FCS */
+ tx_len -= 4;
+ }

         }

- if (s->regs[SONIC_RCR] & (SONIC_RCR_LB1 | SONIC_RCR_LB0)) {

-            /* Loopback */
-            s->regs[SONIC_TCR] |= SONIC_TCR_CRSL;
-            if (nc->info->can_receive(nc)) {

- s->loopback_packet = 1;
- nc->info->receive(nc, s->tx_buffer, tx_len);
+ if (tx_len > 0) {
+ if (s->regs[SONIC_RCR] & (SONIC_RCR_LB1 | SONIC_RCR_LB0)) {

+                /* Loopback */
+                s->regs[SONIC_TCR] |= SONIC_TCR_CRSL;
+                if (nc->info->can_receive(nc)) {

+ s->loopback_packet = 1;
+ nc->info->receive(nc, s->tx_buffer, tx_len);
+ }
+ } else {
+ /* Transmit packet */

+                qemu_send_packet(nc, s->tx_buffer, tx_len);
             }
-        } else {
-            /* Transmit packet */
-            qemu_send_packet(nc, s->tx_buffer, tx_len);
         }
         s->regs[SONIC_TCR] |= SONIC_TCR_PTX;

---

#2 lore raw
Quoted 29 lines · Philippe Mathieu-Daudé

+Laurent/Finn

Quoted 25 lines · Mauro Matteo Cascella

An integer underflow could occur during packet transmission due to 'tx_len' not being updated if SONIC_TFC register is set to zero. Check for negative 'tx_len' when removing existing FCS.

RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=1899722

---
 hw/net/dp8393x.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/net/dp8393x.c +4 -0
index 674b04b354..205c0decc5 100644
@@ -495,6 +495,10 @@ static void dp8393x_do_transmit_packets(dp8393xState *s)
         } else {
             /* Remove existing FCS */
             tx_len -= 4;
+            if (tx_len < 0) {
+                SONIC_ERROR("tx_len is %d\n", tx_len);
+                break;
+            }
         }
 
Quoted 6 lines

hw/net/dp8393x.c static void dp8393x_do_transmit_packets(dp8393xState *s) · line 504

Quoted 2 lines
hw/net/dp8393x.c +0 -0
         if (s->regs[SONIC_RCR] & (SONIC_RCR_LB1 | SONIC_RCR_LB0)) {

Doesn't it make more sense to check 'tx_len >= 4'
and skip tx/rx when 'tx_len == 0'?

Yes, it makes sense. I thought that skipping tx/rx in case of null 'tx_len' could lead to potential inconsistencies when writing the status or reading the footer of the packet. but I'm not really sure. I can send a new version of the patch if needed, otherwise feel free to apply your changes. Thank you.

Quoted 40 lines

-- >8 --

(quoted diff) +18 -13
@@ -488,25 +488,29 @@ static void
 dp8393x_do_transmit_packets(dp8393xState *s)
             }
         }
 
-        /* Handle Ethernet checksum */
-        if (!(s->regs[SONIC_TCR] & SONIC_TCR_CRCI)) {
-            /* Don't append FCS there, to look like slirp packets
-             * which don't have one */
-        } else {
-            /* Remove existing FCS */
-            tx_len -= 4;
+        if (tx_len >= 4) {
+            /* Handle Ethernet checksum */
+            if (!(s->regs[SONIC_TCR] & SONIC_TCR_CRCI)) {
+                /* Don't append FCS there, to look like slirp packets
+                 * which don't have one */
+            } else {
+                /* Remove existing FCS */
+                tx_len -= 4;
+            }
         }
 
-        if (s->regs[SONIC_RCR] & (SONIC_RCR_LB1 | SONIC_RCR_LB0)) {
-            /* Loopback */
-            s->regs[SONIC_TCR] |= SONIC_TCR_CRSL;
-            if (nc->info->can_receive(nc)) {
-                s->loopback_packet = 1;
-                nc->info->receive(nc, s->tx_buffer, tx_len);
+        if (tx_len > 0) {
+            if (s->regs[SONIC_RCR] & (SONIC_RCR_LB1 | SONIC_RCR_LB0)) {
+                /* Loopback */
+                s->regs[SONIC_TCR] |= SONIC_TCR_CRSL;
+                if (nc->info->can_receive(nc)) {
+                    s->loopback_packet = 1;
+                    nc->info->receive(nc, s->tx_buffer, tx_len);
+                }
+            } else {

Review static void

Quoted 10 lines
(quoted diff) +2 -4
+                /* Transmit packet */
+                qemu_send_packet(nc, s->tx_buffer, tx_len);
             }
-        } else {
-            /* Transmit packet */
-            qemu_send_packet(nc, s->tx_buffer, tx_len);
         }
         s->regs[SONIC_TCR] |= SONIC_TCR_PTX;
 
---

Regards,

Signature · 3 lines
Mauro Matteo Cascella
Red Hat Product Security
PGP-Key ID: BB3410B0
Acked-by
#3 lore raw
Quoted 34 lines · Mauro Matteo Cascella
Quoted 28 lines · Philippe Mathieu-Daudé

+Laurent/Finn

Quoted 25 lines · Mauro Matteo Cascella

An integer underflow could occur during packet transmission due to 'tx_len' not being updated if SONIC_TFC register is set to zero. Check for negative 'tx_len' when removing existing FCS.

RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=1899722

---
  hw/net/dp8393x.c | 4 ++++
  1 file changed, 4 insertions(+)
hw/net/dp8393x.c +4 -0
index 674b04b354..205c0decc5 100644
@@ -495,6 +495,10 @@ static void dp8393x_do_transmit_packets(dp8393xState *s)
          } else {
              /* Remove existing FCS */
              tx_len -= 4;
+            if (tx_len < 0) {
+                SONIC_ERROR("tx_len is %d\n", tx_len);
+                break;
+            }
          }
 
Quoted 5 lines

hw/net/dp8393x.c static void dp8393x_do_transmit_packets(dp8393xState *s) · line 504

Quoted 2 lines
hw/net/dp8393x.c +0 -0
          if (s->regs[SONIC_RCR] & (SONIC_RCR_LB1 | SONIC_RCR_LB0)) {

Doesn't it make more sense to check 'tx_len >= 4'
and skip tx/rx when 'tx_len == 0'?

Quoted 5 lines

Yes, it makes sense. I thought that skipping tx/rx in case of null 'tx_len' could lead to potential inconsistencies when writing the status or reading the footer of the packet. but I'm not really sure. I can send a new version of the patch if needed, otherwise feel free to apply your changes. Thank you.

I think we can go with this patch first and tweak on top consider it's near the release. So:

Peter, do you want to merge this patch?

Thanks

Quoted 52 lines
Quoted 41 lines

-- >8 --

(quoted diff) +18 -13
@@ -488,25 +488,29 @@ static void
 dp8393x_do_transmit_packets(dp8393xState *s)
              }
          }
 
-        /* Handle Ethernet checksum */
-        if (!(s->regs[SONIC_TCR] & SONIC_TCR_CRCI)) {
-            /* Don't append FCS there, to look like slirp packets
-             * which don't have one */
-        } else {
-            /* Remove existing FCS */
-            tx_len -= 4;
+        if (tx_len >= 4) {
+            /* Handle Ethernet checksum */
+            if (!(s->regs[SONIC_TCR] & SONIC_TCR_CRCI)) {
+                /* Don't append FCS there, to look like slirp packets
+                 * which don't have one */
+            } else {
+                /* Remove existing FCS */
+                tx_len -= 4;
+            }
          }
 
-        if (s->regs[SONIC_RCR] & (SONIC_RCR_LB1 | SONIC_RCR_LB0)) {
-            /* Loopback */
-            s->regs[SONIC_TCR] |= SONIC_TCR_CRSL;
-            if (nc->info->can_receive(nc)) {
-                s->loopback_packet = 1;
-                nc->info->receive(nc, s->tx_buffer, tx_len);
+        if (tx_len > 0) {
+            if (s->regs[SONIC_RCR] & (SONIC_RCR_LB1 | SONIC_RCR_LB0)) {
+                /* Loopback */
+                s->regs[SONIC_TCR] |= SONIC_TCR_CRSL;
+                if (nc->info->can_receive(nc)) {
+                    s->loopback_packet = 1;
+                    nc->info->receive(nc, s->tx_buffer, tx_len);
+                }
+            } else {

Review static void

Quoted 10 lines
(quoted diff) +2 -4
+                /* Transmit packet */
+                qemu_send_packet(nc, s->tx_buffer, tx_len);
              }
-        } else {
-            /* Transmit packet */
-            qemu_send_packet(nc, s->tx_buffer, tx_len);
          }
          s->regs[SONIC_TCR] |= SONIC_TCR_PTX;
 
---

Regards,

#4 lore raw
Quoted 44 lines · Jason Wang
Quoted 34 lines · Mauro Matteo Cascella
Quoted 28 lines · Philippe Mathieu-Daudé

+Laurent/Finn

Quoted 25 lines · Mauro Matteo Cascella

An integer underflow could occur during packet transmission due to 'tx_len' not being updated if SONIC_TFC register is set to zero. Check for negative 'tx_len' when removing existing FCS.

RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=1899722

---
  hw/net/dp8393x.c | 4 ++++
  1 file changed, 4 insertions(+)
hw/net/dp8393x.c +4 -0
index 674b04b354..205c0decc5 100644
@@ -495,6 +495,10 @@ static void dp8393x_do_transmit_packets(dp8393xState *s)
          } else {
              /* Remove existing FCS */
              tx_len -= 4;
+            if (tx_len < 0) {
+                SONIC_ERROR("tx_len is %d\n", tx_len);
+                break;
+            }
          }
 
Quoted 5 lines

hw/net/dp8393x.c static void dp8393x_do_transmit_packets(dp8393xState *s) · line 504

Quoted 2 lines
hw/net/dp8393x.c +0 -0
          if (s->regs[SONIC_RCR] & (SONIC_RCR_LB1 | SONIC_RCR_LB0)) {

Doesn't it make more sense to check 'tx_len >= 4'
and skip tx/rx when 'tx_len == 0'?

Quoted 5 lines

Yes, it makes sense. I thought that skipping tx/rx in case of null 'tx_len' could lead to potential inconsistencies when writing the status or reading the footer of the packet. but I'm not really sure. I can send a new version of the patch if needed, otherwise feel free to apply your changes. Thank you.

Quoted 6 lines

I think we can go with this patch first and tweak on top consider it's near the release. So:

Peter, do you want to merge this patch?

rc4 is due for release today, and the dp8393x is a device not used by any KVM platform, so this isn't a security fix. So we don't *need* to take it for 5.2. On the other hand this is a pretty small and constrained fix that won't affect anything except the mips jazz and m68k q800 boards.

Applied to master for 5.2, thanks.

-- PMM