Skip to contentSkip to conversation

hw/net: Fix read of uninitialized memory in ftgmac100

PATCH In review Stephen Longfield 2 participants 5 messages Dec 20, 2022 – Jan 9, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/net: Fix read of uninitialized memory in ftgmac100

With the size += 4 before the call to crc32, the CRC calculation would overrun the buffer. Size is used in the while loop starting on line 1009 to determine how much data to write back, with the last four bytes coming from crc_ptr, so do need to increase it, but should do this after the computation.

I'm unsure why this use of uninitialized memory in the CRC doesn't result in CRC errors, but it seems clear to me that it should not be included in the calculation.

---
 hw/net/ftgmac100.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/net/ftgmac100.c +2 -2
index 83ef0a783e..d3bf14be53 100644
@@ -980,9 +980,9 @@ static ssize_t ftgmac100_receive(NetClientState *nc, const uint8_t *buf,
         return size;
     }
 
-    /* 4 bytes for the CRC.  */
-    size += 4;
     crc = cpu_to_be32(crc32(~0, buf, size));
+    /* Increase size by 4, loop below reads the last 4 bytes from crc_ptr. */
+    size += 4;
     crc_ptr = (uint8_t *) &crc;
 
     /* Huge frames are truncated.  */

--
2.39.0.314.g84b9a713c41-goog

Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Stephen Longfield

With the size += 4 before the call to crc32, the CRC calculation would overrun the buffer. Size is used in the while loop starting on line 1009 to determine how much data to write back, with the last four bytes coming from crc_ptr, so do need to increase it, but should do this after the computation.

Quoted 6 lines

I'm unsure why this use of uninitialized memory in the CRC doesn't result in CRC errors, but it seems clear to me that it should not be included in the calculation.

I think imx_fec.c is impacted also.

Thanks,

C.

Quoted 23 lines
---
  hw/net/ftgmac100.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
hw/net/ftgmac100.c +2 -3
index 83ef0a783e..d3bf14be53 100644
@@ -980,9 +980,9 @@ static ssize_t ftgmac100_receive(NetClientState *nc, const uint8_t *buf,
          return size;
      }
 
-    /* 4 bytes for the CRC.  */
-    size += 4;
      crc = cpu_to_be32(crc32(~0, buf, size));
+    /* Increase size by 4, loop below reads the last 4 bytes from crc_ptr. */
+    size += 4;
      crc_ptr = (uint8_t *) &crc;
 
      /* Huge frames are truncated.  */
--

2.39.0.314.g84b9a713c41-goog

#2 lore raw
Quoted 16 lines · Cédric Le Goater
Quoted 6 lines · Stephen Longfield

With the size += 4 before the call to crc32, the CRC calculation would overrun the buffer. Size is used in the while loop starting on line 1009 to determine how much data to write back, with the last four bytes coming from crc_ptr, so do need to increase it, but should do this after the computation.

Quoted 6 lines

I'm unsure why this use of uninitialized memory in the CRC doesn't result in CRC errors, but it seems clear to me that it should not be included in the calculation.

Quoted 8 lines

I think imx_fec.c is impacted also.

Thanks,

C.

Thanks for pointing that out, looks to be exactly the same. I'll send out a separate patch that fixes the issue in that file.

Best,

--Stephen

Quoted 25 lines
Quoted 24 lines
---
  hw/net/ftgmac100.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
hw/net/ftgmac100.c +2 -3
index 83ef0a783e..d3bf14be53 100644
@@ -980,9 +980,9 @@ static ssize_t ftgmac100_receive(NetClientState *nc, const uint8_t *buf,
          return size;
      }
 
-    /* 4 bytes for the CRC.  */
-    size += 4;
      crc = cpu_to_be32(crc32(~0, buf, size));
+    /* Increase size by 4, loop below reads the last 4 bytes from crc_ptr. */
+    size += 4;
      crc_ptr = (uint8_t *) &crc;
 
      /* Huge frames are truncated.  */
--

2.39.0.314.g84b9a713c41-goog

#3 lore raw

Does anything more need to happen with this patch before it can be applied? Not sure if it had gotten lost over the holidays.

Best,

--Stephen

Quoted 59 lines · Stephen Longfield
Quoted 16 lines · Cédric Le Goater
Quoted 6 lines · Stephen Longfield

With the size += 4 before the call to crc32, the CRC calculation would overrun the buffer. Size is used in the while loop starting on line 1009 to determine how much data to write back, with the last four bytes coming from crc_ptr, so do need to increase it, but should do this after the computation.

Quoted 6 lines

I'm unsure why this use of uninitialized memory in the CRC doesn't result in CRC errors, but it seems clear to me that it should not be included in the calculation.

Quoted 8 lines

I think imx_fec.c is impacted also.

Thanks,

C.

Thanks for pointing that out, looks to be exactly the same. I'll send out a separate patch that fixes the issue in that file.

Best,

--Stephen

Quoted 25 lines
Quoted 24 lines
---
  hw/net/ftgmac100.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
hw/net/ftgmac100.c +2 -3
index 83ef0a783e..d3bf14be53 100644
@@ -980,9 +980,9 @@ static ssize_t ftgmac100_receive(NetClientState *nc, const uint8_t *buf,
          return size;
      }
 
-    /* 4 bytes for the CRC.  */
-    size += 4;
      crc = cpu_to_be32(crc32(~0, buf, size));
+    /* Increase size by 4, loop below reads the last 4 bytes from crc_ptr. */
+    size += 4;
      crc_ptr = (uint8_t *) &crc;
 
      /* Huge frames are truncated.  */
--

2.39.0.314.g84b9a713c41-goog

#4 lore raw
Quoted 2 lines · Stephen Longfield

Does anything more need to happen with this patch before it can be applied? Not sure if it had gotten lost over the holidays.

I queued it with other aspeed changes :

https://github.com/legoater/qemu/commits/aspeed-8.0

We have some time before 8.0 is released.

Thanks,

C.

Quoted 66 lines

Best,

--Stephen

Quoted 59 lines · Stephen Longfield
Quoted 16 lines · Cédric Le Goater
Quoted 6 lines · Stephen Longfield

With the size += 4 before the call to crc32, the CRC calculation would overrun the buffer. Size is used in the while loop starting on line 1009 to determine how much data to write back, with the last four bytes coming from crc_ptr, so do need to increase it, but should do this after the computation.

Quoted 6 lines

I'm unsure why this use of uninitialized memory in the CRC doesn't result in CRC errors, but it seems clear to me that it should not be included in the calculation.

Quoted 8 lines

I think imx_fec.c is impacted also.

Thanks,

C.

Thanks for pointing that out, looks to be exactly the same. I'll send out a separate patch that fixes the issue in that file.

Best,

--Stephen

Quoted 25 lines
Quoted 24 lines
---
   hw/net/ftgmac100.c | 4 ++--
   1 file changed, 2 insertions(+), 2 deletions(-)
hw/net/ftgmac100.c +2 -3
index 83ef0a783e..d3bf14be53 100644
@@ -980,9 +980,9 @@ static ssize_t ftgmac100_receive(NetClientState *nc, const uint8_t *buf,
           return size;
       }
 
-    /* 4 bytes for the CRC.  */
-    size += 4;
       crc = cpu_to_be32(crc32(~0, buf, size));
+    /* Increase size by 4, loop below reads the last 4 bytes from crc_ptr. */
+    size += 4;
       crc_ptr = (uint8_t *) &crc;
 
       /* Huge frames are truncated.  */
--

2.39.0.314.g84b9a713c41-goog