Skip to contentSkip to conversation

cadence_gem: fix buffer overflow

PATCH In review Michael S. Tsirkin 6 participants 20 messages Jan 14, 2016 – Jan 19, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cadence_gem: fix buffer overflow

gem_receive copies a packet received from network into an rxbuf[2048] array on stack, with size limited by descriptor length set by guest. If guest is malicious and specifies a descriptor length that is too large, and should packet size exceed array size, this results in a buffer overflow.

---
 hw/net/cadence_gem.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/net/cadence_gem.c +8 -0
index 3639fc1..15a0786 100644
@@ -862,6 +862,14 @@ static void gem_transmit(CadenceGEMState *s)
             break;
         }
 
+        if (tx_desc_get_length(desc) > sizeof(tx_packet) - (p - tx_packet)) {
+            DB_PRINT("TX descriptor @ 0x%x too large: size 0x%x space 0x%x\n",
+                     (unsigned)packet_desc_addr,
+                     (unsigned)tx_desc_get_length(desc),
+                     sizeof(tx_packet) - (p - tx_packet));
+            break;
+        }
+
         /* Gather this fragment of the packet from "dma memory" to our contig.
          * buffer.
          */
Signature · 1 line
MST
#1 lore raw
Quoted 17 lines · Michael S. Tsirkin

gem_receive copies a packet received from network into an rxbuf[2048] array on stack, with size limited by descriptor length set by guest. If guest is malicious and specifies a descriptor length that is too large, and should packet size exceed array size, this results in a buffer overflow.

---
 hw/net/cadence_gem.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/net/cadence_gem.c +0 -0
index 3639fc1..15a0786 100644

hw/net/cadence_gem.c static void gem_transmit(CadenceGEMState *s) · line 871

Quoted 10 lines
hw/net/cadence_gem.c +7 -0
@@ -862,6 +862,14 @@ static void gem_transmit(CadenceGEMState *s)
             break;
         }
 
+        if (tx_desc_get_length(desc) > sizeof(tx_packet) - (p - tx_packet)) {
+            DB_PRINT("TX descriptor @ 0x%x too large: size 0x%x space 0x%x\n",
+                     (unsigned)packet_desc_addr,
+                     (unsigned)tx_desc_get_length(desc),
+                     sizeof(tx_packet) - (p - tx_packet));
+            break;
+        }

Is this what the real hardware does in this situation? Should we log this as a guest error?

Quoted 7 lines
hw/net/cadence_gem.c +1 -1
+
         /* Gather this fragment of the packet from "dma memory" to our contig.
          * buffer.
          */
--

MST

thanks
-- PMM

#2 lore raw
Quoted 21 lines · Peter Maydell
Quoted 20 lines · Michael S. Tsirkin

gem_receive copies a packet received from network into an rxbuf[2048] array on stack, with size limited by descriptor length set by guest. If guest is malicious and specifies a descriptor length that is too large, and should packet size exceed array size, this results in a buffer overflow.

---
 hw/net/cadence_gem.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/net/cadence_gem.c +0 -0
index 3639fc1..15a0786 100644
@@ -862,6 +862,14 @@ static void gem_transmit(CadenceGEMState *s)
             break;
         }
 
Quoted 10 lines

hw/net/cadence_gem.c static void gem_transmit(CadenceGEMState *s) · line 871

Quoted 7 lines
hw/net/cadence_gem.c +7 -0
+        if (tx_desc_get_length(desc) > sizeof(tx_packet) - (p - tx_packet)) {
+            DB_PRINT("TX descriptor @ 0x%x too large: size 0x%x space 0x%x\n",
+                     (unsigned)packet_desc_addr,
+                     (unsigned)tx_desc_get_length(desc),
+                     sizeof(tx_packet) - (p - tx_packet));
+            break;
+        }

Is this what the real hardware does in this situation? Should we log this as a guest error?

I don't really know. This is just consistent with what this device does for other descriptor errors.

Quoted 10 lines
Quoted 7 lines
hw/net/cadence_gem.c +1 -1
+
         /* Gather this fragment of the packet from "dma memory" to our contig.
          * buffer.
          */
--

MST

thanks
-- PMM

#3 lore raw
In reply to Peter Maydell
Quoted 21 lines · Peter Maydell
Quoted 20 lines · Michael S. Tsirkin

gem_receive copies a packet received from network into an rxbuf[2048] array on stack, with size limited by descriptor length set by guest. If guest is malicious and specifies a descriptor length that is too large, and should packet size exceed array size, this results in a buffer overflow.

---
 hw/net/cadence_gem.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/net/cadence_gem.c +0 -0
index 3639fc1..15a0786 100644
@@ -862,6 +862,14 @@ static void gem_transmit(CadenceGEMState *s)
             break;
         }
 
Quoted 11 lines

hw/net/cadence_gem.c static void gem_transmit(CadenceGEMState *s) · line 871

Quoted 7 lines
hw/net/cadence_gem.c +7 -0
+        if (tx_desc_get_length(desc) > sizeof(tx_packet) - (p - tx_packet)) {
+            DB_PRINT("TX descriptor @ 0x%x too large: size 0x%x space 0x%x\n",
+                     (unsigned)packet_desc_addr,
+                     (unsigned)tx_desc_get_length(desc),
+                     sizeof(tx_packet) - (p - tx_packet));
+            break;
+        }

Is this what the real hardware does in this situation? Should we log this as a guest error?

I'm not sure it is a guest error. I think its just a shortcut in the original implementation. I guess QEMU needs the whole packet before handing off to the net layer and the assumption is that the packet is always within 2048. I think the hardware is just going to put the data on the wire as it goes. The easiest solution is to realloc the buffer as it goes with the increasing sizes. Otherwise you could refactor the code to be two pass over the descriptor ring section (containing the packet). If we want to fix the buffer overflow more urgently, the correct error would be an assert().

Regards,
Peter

Quoted 11 lines
Quoted 7 lines
hw/net/cadence_gem.c +1 -1
+
         /* Gather this fragment of the packet from "dma memory" to our contig.
          * buffer.
          */
--

MST

thanks
-- PMM

#4 lore raw
+-- On Thu, 14 Jan 2016, Peter Crosthwaite wrote --+
| I guess QEMU needs the whole packet before handing off to the net layer and 
| the assumption is that the packet is always within 2048. The easiest 
| solution is to realloc the buffer as it goes with the increasing sizes.

Yes, I was considering increasing buffer size with view of the jumbo
packets. In gem_transmit(), 'tx_desc_get_length' returns length masked by DESC_1_LENGTH(=0x1FFF=8191) bytes. But thought dynamic allocation might lead to excessive buffer allocation, not sure how constrained the Xilinx platform is for memory.

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

#5 lore raw
Quoted 10 lines · P J P
+-- On Thu, 14 Jan 2016, Peter Crosthwaite wrote --+
| I guess QEMU needs the whole packet before handing off to the net layer and
| the assumption is that the packet is always within 2048. The easiest
| solution is to realloc the buffer as it goes with the increasing sizes.

Yes, I was considering increasing buffer size with view of the jumbo
packets. In gem_transmit(), 'tx_desc_get_length' returns length masked by DESC_1_LENGTH(=0x1FFF=8191) bytes. But thought dynamic allocation might lead to excessive buffer allocation, not sure how constrained the Xilinx platform is for memory.

Won't the allocation/reallocation happen on the host?

Thanks,

Alistair

Quoted 5 lines

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

#6 lore raw
+-- On Fri, 15 Jan 2016, Alistair Francis wrote --+
| Won't the allocation/reallocation happen on the host?

Ah yes, don't know what I was thinking.

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

#7 lore raw
In reply to Peter Crosthwaite
Quoted 32 lines · Peter Crosthwaite
Quoted 21 lines · Peter Maydell
Quoted 20 lines · Michael S. Tsirkin

gem_receive copies a packet received from network into an rxbuf[2048] array on stack, with size limited by descriptor length set by guest. If guest is malicious and specifies a descriptor length that is too large, and should packet size exceed array size, this results in a buffer overflow.

---
 hw/net/cadence_gem.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/net/cadence_gem.c +0 -0
index 3639fc1..15a0786 100644
@@ -862,6 +862,14 @@ static void gem_transmit(CadenceGEMState *s)
             break;
         }
 
Quoted 10 lines

hw/net/cadence_gem.c static void gem_transmit(CadenceGEMState *s) · line 871

Quoted 7 lines
hw/net/cadence_gem.c +7 -0
+        if (tx_desc_get_length(desc) > sizeof(tx_packet) - (p - tx_packet)) {
+            DB_PRINT("TX descriptor @ 0x%x too large: size 0x%x space 0x%x\n",
+                     (unsigned)packet_desc_addr,
+                     (unsigned)tx_desc_get_length(desc),
+                     sizeof(tx_packet) - (p - tx_packet));
+            break;
+        }

Is this what the real hardware does in this situation? Should we log this as a guest error?

Quoted 5 lines

I'm not sure it is a guest error. I think its just a shortcut in the original implementation. I guess QEMU needs the whole packet before handing off to the net layer and the assumption is that the packet is always within 2048. I think the hardware is just going to put the data on the wire as it goes.

If we are not sure this is what real hardware did, dropping looks more safe than sending the truncated packets on the wire.

Quoted 2 lines

The easiest solution is to realloc the buffer
as it goes with the increasing sizes.

This could allow possible DOS from guest (see
cde31a0e3dc0e4ac83e454d6096350cec584adf1).

Quoted 7 lines

Otherwise you could refactor the
code to be two pass over the descriptor ring section (containing the packet). If we want to fix the buffer overflow more urgently, the correct error would be an assert().

Regards,
Peter

Let's avoid putting guest trigger-able assert() here. The patch looks good for fixing the issue. Refactoring could be done on top.

Thanks

Quoted 11 lines
Quoted 11 lines
Quoted 8 lines
hw/net/cadence_gem.c +1 -1
+
         /* Gather this fragment of the packet from "dma memory" to our contig.
          * buffer.
          */
--

MST

thanks
-- PMM

#8 lore raw
In reply to Michael S. Tsirkin
+-- On Thu, 14 Jan 2016, Michael S. Tsirkin wrote --+
| gem_receive copies a packet received from network into an rxbuf[2048]
| array on stack, with size limited by descriptor length set by guest.  If
| guest is malicious and specifies a descriptor length that is too large,
| and should packet size exceed array size, this results in a buffer
| overflow.
| 
| diff --git a/hw/net/cadence_gem.c b/hw/net/cadence_gem.c
| index 3639fc1..15a0786 100644
| --- a/hw/net/cadence_gem.c
| +++ b/hw/net/cadence_gem.c
| @@ -862,6 +862,14 @@ static void gem_transmit(CadenceGEMState *s)
|              break;
|          }
|  
| +        if (tx_desc_get_length(desc) > sizeof(tx_packet) - (p - tx_packet)) {
| +            DB_PRINT("TX descriptor @ 0x%x too large: size 0x%x space 0x%x\n",
| +                     (unsigned)packet_desc_addr,
| +                     (unsigned)tx_desc_get_length(desc),
| +                     sizeof(tx_packet) - (p - tx_packet));
| +            break;
| +        }
| +

Commit message says gem_receive, but the patch fixes gem_transmit() routine.

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

#9 lore raw
In reply to Michael S. Tsirkin
Quoted 21 lines · Michael S. Tsirkin

gem_receive copies a packet received from network into an rxbuf[2048] array on stack, with size limited by descriptor length set by guest. If guest is malicious and specifies a descriptor length that is too large, and should packet size exceed array size, this results in a buffer overflow.

---
 hw/net/cadence_gem.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/net/cadence_gem.c +1 -0
index 3639fc1..15a0786 100644
@@ -862,6 +862,14 @@ static void gem_transmit(CadenceGEMState *s)
             break;
         }
 
+        if (tx_desc_get_length(desc) > sizeof(tx_packet) - (p - tx_packet)) {

hw/net/cadence_gem.c static void gem_transmit(CadenceGEMState *s) · line 875

Quoted 10 lines
hw/net/cadence_gem.c +7 -0
+            DB_PRINT("TX descriptor @ 0x%x too large: size 0x%x space 0x%x\n",
+                     (unsigned)packet_desc_addr,
+                     (unsigned)tx_desc_get_length(desc),
+                     sizeof(tx_packet) - (p - tx_packet));
+            break;
+        }
+
         /* Gather this fragment of the packet from "dma memory" to our contig.
          * buffer.
          */

Looks like we need similar issue in gen_receive(), need to fix that?

#10 lore raw
+-- On Fri, 15 Jan 2016, Jason Wang wrote --+
| Looks like we need similar issue in gen_receive(), need to fix that?

Yes, I'm preparing a patch.
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F

#11 lore raw
In reply to Michael S. Tsirkin
Quoted 11 lines · Michael S. Tsirkin

gem_receive copies a packet received from network into an rxbuf[2048] array on stack, with size limited by descriptor length set by guest. If guest is malicious and specifies a descriptor length that is too large, and should packet size exceed array size, this results in a buffer overflow.

---
 hw/net/cadence_gem.c | 8 ++++++++
 1 file changed, 8 insertions(+)

Apply to my -net with tweak on commit log (changing receive to transmit as noticed).

Thanks

Quoted 20 lines
hw/net/cadence_gem.c +8 -0
index 3639fc1..15a0786 100644
@@ -862,6 +862,14 @@ static void gem_transmit(CadenceGEMState *s)
             break;
         }
 
+        if (tx_desc_get_length(desc) > sizeof(tx_packet) - (p - tx_packet)) {
+            DB_PRINT("TX descriptor @ 0x%x too large: size 0x%x space 0x%x\n",
+                     (unsigned)packet_desc_addr,
+                     (unsigned)tx_desc_get_length(desc),
+                     sizeof(tx_packet) - (p - tx_packet));
+            break;
+        }
+
         /* Gather this fragment of the packet from "dma memory" to our contig.
          * buffer.
          */
#12 lore raw
Quoted 9 lines · Jason Wang
Quoted 6 lines · Michael S. Tsirkin

gem_receive copies a packet received from network into an rxbuf[2048] array on stack, with size limited by descriptor length set by guest. If guest is malicious and specifies a descriptor length that is too large, and should packet size exceed array size, this results in a buffer overflow.

Quoted 9 lines
Quoted 5 lines
---
 hw/net/cadence_gem.c | 8 ++++++++
 1 file changed, 8 insertions(+)

Apply to my -net with tweak on commit log (changing receive to transmit as noticed).

As this is actually an unimplemented feature you should change the message to a LOG_UNIMP rather than a debug printf.

Regards,
Peter

Quoted 23 lines

Thanks

Quoted 20 lines
hw/net/cadence_gem.c +8 -0
index 3639fc1..15a0786 100644
@@ -862,6 +862,14 @@ static void gem_transmit(CadenceGEMState *s)
             break;
         }
 
+        if (tx_desc_get_length(desc) > sizeof(tx_packet) - (p - tx_packet)) {
+            DB_PRINT("TX descriptor @ 0x%x too large: size 0x%x space 0x%x\n",
+                     (unsigned)packet_desc_addr,
+                     (unsigned)tx_desc_get_length(desc),
+                     sizeof(tx_packet) - (p - tx_packet));
+            break;
+        }
+
         /* Gather this fragment of the packet from "dma memory" to our contig.
          * buffer.
          */
#13 lore raw
Quoted 14 lines · Peter Crosthwaite
Quoted 13 lines · Jason Wang
Quoted 11 lines · Michael S. Tsirkin

gem_receive copies a packet received from network into an rxbuf[2048] array on stack, with size limited by descriptor length set by guest. If guest is malicious and specifies a descriptor length that is too large, and should packet size exceed array size, this results in a buffer overflow.

---
 hw/net/cadence_gem.c | 8 ++++++++
 1 file changed, 8 insertions(+)
Quoted 8 lines
Quoted 3 lines

Apply to my -net with tweak on commit log (changing receive to transmit as noticed).

As this is actually an unimplemented feature you should change the message to a LOG_UNIMP rather than a debug printf.

Regards,
Peter

Thanks for the reminding. But we need know the whether real device could send packet whose length is greater than 2048. Do you know the link to the manual? (Haven't fond it in cadence page.) A hint is the linux driver (drivers/net/ethernet/cadence/macb.c), use 1500 as its MTU.

Quoted 22 lines
Quoted 22 lines

Thanks

Quoted 19 lines
hw/net/cadence_gem.c +8 -0
index 3639fc1..15a0786 100644
@@ -862,6 +862,14 @@ static void gem_transmit(CadenceGEMState *s)
             break;
         }
 
+        if (tx_desc_get_length(desc) > sizeof(tx_packet) - (p - tx_packet)) {
+            DB_PRINT("TX descriptor @ 0x%x too large: size 0x%x space 0x%x\n",
+                     (unsigned)packet_desc_addr,
+                     (unsigned)tx_desc_get_length(desc),
+                     sizeof(tx_packet) - (p - tx_packet));
+            break;
+        }
+
         /* Gather this fragment of the packet from "dma memory" to our contig.
          * buffer.
          */
#14 lore raw
Quoted 23 lines · Jason Wang
Quoted 20 lines · Peter Crosthwaite
Quoted 8 lines · Jason Wang
Quoted 6 lines · Michael S. Tsirkin

gem_receive copies a packet received from network into an rxbuf[2048] array on stack, with size limited by descriptor length set by guest. If guest is malicious and specifies a descriptor length that is too large, and should packet size exceed array size, this results in a buffer overflow.

Quoted 8 lines
Quoted 5 lines
---
 hw/net/cadence_gem.c | 8 ++++++++
 1 file changed, 8 insertions(+)

Apply to my -net with tweak on commit log (changing receive to transmit as noticed).

As this is actually an unimplemented feature you should change the message to a LOG_UNIMP rather than a debug printf.

Quoted 6 lines
Quoted 2 lines

Regards,
Peter

Thanks for the reminding. But we need know the whether real device could send packet whose length is greater than 2048. Do you know the link to the manual? (Haven't fond it in cadence page.) A hint is the linux

Xilinx UG585 has details:

http://www.xilinx.com/support/documentation/user_guides/ug585-Zynq-7000-TRM.pdf

Regards,
Peter

Quoted 25 lines

driver (drivers/net/ethernet/cadence/macb.c), use 1500 as its MTU.

Quoted 22 lines
Quoted 22 lines

Thanks

Quoted 19 lines
hw/net/cadence_gem.c +8 -0
index 3639fc1..15a0786 100644
@@ -862,6 +862,14 @@ static void gem_transmit(CadenceGEMState *s)
             break;
         }
 
+        if (tx_desc_get_length(desc) > sizeof(tx_packet) - (p - tx_packet)) {
+            DB_PRINT("TX descriptor @ 0x%x too large: size 0x%x space 0x%x\n",
+                     (unsigned)packet_desc_addr,
+                     (unsigned)tx_desc_get_length(desc),
+                     sizeof(tx_packet) - (p - tx_packet));
+            break;
+        }
+
         /* Gather this fragment of the packet from "dma memory" to our contig.
          * buffer.
          */
#15 lore raw
Quoted 27 lines · Peter Crosthwaite
Quoted 18 lines · Jason Wang
Quoted 16 lines · Peter Crosthwaite
Quoted 15 lines · Jason Wang
Quoted 11 lines · Michael S. Tsirkin

gem_receive copies a packet received from network into an rxbuf[2048] array on stack, with size limited by descriptor length set by guest. If guest is malicious and specifies a descriptor length that is too large, and should packet size exceed array size, this results in a buffer overflow.

---
 hw/net/cadence_gem.c | 8 ++++++++
 1 file changed, 8 insertions(+)

Apply to my -net with tweak on commit log (changing receive to transmit as noticed).

Quoted 8 lines
Quoted 5 lines

As this is actually an unimplemented feature you should change the message to a LOG_UNIMP rather than a debug printf.

Regards,
Peter

Thanks for the reminding. But we need know the whether real device could send packet whose length is greater than 2048. Do you know the link to the manual? (Haven't fond it in cadence page.) A hint is the linux

Quoted 8 lines

Xilinx UG585 has details:

http://www.xilinx.com/support/documentation/user_guides/ug585-Zynq-7000-TRM.pdf

Regards,
Peter

Thanks for the pointer.

In section 16.1.5, it said

"Jumbo frames are not supported."

So it was in fact not an unimplemented feature?

#16 lore raw
Quoted 7 lines · Jason Wang

Thanks for the pointer.

In section 16.1.5, it said

"Jumbo frames are not supported."

So it was in fact not an unimplemented feature?

I have a vague feeling from the last time I looked at something like this that what often happens is the hardware happily goes on dumping data out on the wire but this is a violation of the ethernet protocol, ie a guest error. But I'm no ethernet expert...

thanks
-- PMM

#17 lore raw
Quoted 8 lines · Peter Maydell
Quoted 7 lines · Jason Wang

Thanks for the pointer.

In section 16.1.5, it said

"Jumbo frames are not supported."

So it was in fact not an unimplemented feature?

I'd say this should be a guest error then. Unless anyone who knows Ethernet well thinks otherwise?

Thanks,

Alistair

Quoted 9 lines

I have a vague feeling from the last time I looked at something like this that what often happens is the hardware happily goes on dumping data out on the wire but this is a violation of the ethernet protocol, ie a guest error. But I'm no ethernet expert...

thanks
-- PMM

#18 lore raw
Quoted 8 lines · Alistair Francis
Quoted 7 lines · Peter Maydell
Quoted 6 lines · Jason Wang

Thanks for the pointer.

In section 16.1.5, it said

"Jumbo frames are not supported."

Quoted 7 lines
Quoted 1 line
Quoted 1 line

So it was in fact not an unimplemented feature?

I'd say this should be a guest error then. Unless anyone who knows Ethernet well thinks otherwise?

Thanks,

Alistair

Yes, agree.

Quoted 9 lines
Quoted 9 lines

I have a vague feeling from the last time I looked at something like this that what often happens is the hardware happily goes on dumping data out on the wire but this is a violation of the ethernet protocol, ie a guest error. But I'm no ethernet expert...

thanks
-- PMM

#19 lore raw
In reply to Peter Maydell
Quoted 7 lines · Peter Maydell
Quoted 6 lines · Jason Wang

Thanks for the pointer.

In section 16.1.5, it said

"Jumbo frames are not supported."

Quoted 9 lines
Quoted 1 line

So it was in fact not an unimplemented feature?

I have a vague feeling from the last time I looked at something like this that what often happens is the hardware happily goes on dumping data out on the wire but this is a violation of the ethernet protocol, ie a guest error. But I'm no ethernet expert...

thanks
-- PMM

I think it's a guest error (unless we find a guest/driver that depends on this). Anyway, fixing the buffer overflow should be fine.