Skip to contentSkip to conversation

net: cadence_gem: check packet size in gem_recieve

PATCH In review P J P 2 participants 4 messages Jan 15, 2016 – Jan 18, 2016

lore
Share & tools
lore

P J P

author
#0 lore raw

PATCH 1/1 net: cadence_gem: check packet size in gem_recieve

From: Prasad J Pandit <[email protected]>

While receiving packets in 'gem_receive' routine, if Frame Check Sequence(FCS) is enabled, it copies the packet into a local buffer without checking its size. Add check to validate packet length against the buffer size to avoid buffer overflow.

---
 hw/net/cadence_gem.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
hw/net/cadence_gem.c +5 -1
index 3639fc1..c3a273b 100644
@@ -677,10 +677,14 @@ static ssize_t gem_receive(NetClientState *nc, const uint8_t *buf, size_t size)
     } else {
         unsigned crc_val;
 
+        if (size > sizeof(rxbuf) - sizeof(crc_val)) {
+            size = sizeof(rxbuf) - sizeof(crc_val);
+        }
+        bytes_to_copy = size;
+
         /* The application wants the FCS field, which QEMU does not provide.
          * We must try and calculate one.
          */
-
         memcpy(rxbuf, buf, size);
         memset(rxbuf + size, 0, sizeof(rxbuf) - size);
         rxbuf_ptr = rxbuf;
Signature · 1 line
2.5.0
#1 lore raw
Quoted 20 lines · P J P

From: Prasad J Pandit <[email protected]>

While receiving packets in 'gem_receive' routine, if Frame Check Sequence(FCS) is enabled, it copies the packet into a local buffer without checking its size. Add check to validate packet length against the buffer size to avoid buffer overflow.

---
 hw/net/cadence_gem.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
hw/net/cadence_gem.c +0 -0
index 3639fc1..c3a273b 100644
@@ -677,10 +677,14 @@ static ssize_t gem_receive(NetClientState *nc, const uint8_t *buf, size_t size)
     } else {
         unsigned crc_val;

hw/net/cadence_gem.c static ssize_t gem_receive(NetClientState *nc, const uint8_t *buf, size_t size) · line 683

Quoted 10 lines
hw/net/cadence_gem.c +5 -1
 
+        if (size > sizeof(rxbuf) - sizeof(crc_val)) {
+            size = sizeof(rxbuf) - sizeof(crc_val);
+        }
+        bytes_to_copy = size;
+
         /* The application wants the FCS field, which QEMU does not provide.
          * We must try and calculate one.
          */
-

Unnecessary whitespace change.

hw/net/cadence_gem.c static ssize_t gem_receive(NetClientState *nc, const uint8_t *buf, size_t size) · line 688

Quoted 1 line
hw/net/cadence_gem.c +0 -0
         memcpy(rxbuf, buf, size);

We probably need more check, is there any guarantee that size <= 2048? If not, need fix.

Thanks

Quoted 2 lines
hw/net/cadence_gem.c +0 -0
         memset(rxbuf + size, 0, sizeof(rxbuf) - size);
         rxbuf_ptr = rxbuf;
author
#2 lore raw
+-- On Mon, 18 Jan 2016, Jason Wang wrote --+
| > +        if (size > sizeof(rxbuf) - sizeof(crc_val)) {
| > +            size = sizeof(rxbuf) - sizeof(crc_val);
| > +        }
| > +        bytes_to_copy = size;
| > +
| 
| We probably need more check, is there any guarantee that size <= 2048?
| If not, need fix.

Sorry? The above check would fix that, no?

--
- P J P
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F

#3 lore raw
Quoted 11 lines · P J P
+-- On Mon, 18 Jan 2016, Jason Wang wrote --+
| > +        if (size > sizeof(rxbuf) - sizeof(crc_val)) {
| > +            size = sizeof(rxbuf) - sizeof(crc_val);
| > +        }
| > +        bytes_to_copy = size;
| > +
| 
| We probably need more check, is there any guarantee that size <= 2048?
| If not, need fix.

Sorry? The above check would fix that, no?

You're right. Apply to my -net (and removing the unnecessary whitespace change).

Thanks

Quoted 3 lines

--
- P J P
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F