Skip to contentSkip to conversation

net: tulip: add checks to avoid OOB access

PATCH Superseded P J P 4 participants 14 messages Mar 3, 2020 – Mar 19, 2020

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

P J P

author
#0 lore raw

v3 cover net: tulip: add checks to avoid OOB access

From: Prasad J Pandit <[email protected]>

Hello,

* This series adds checks to avoid potential OOB access and infinite loop issues while processing rx/tx data.

* Tulip tx descriptors are capped at 128 to avoid infinite loop in
tulip_xmit_list_update(), wrt Tulip kernel driver
-> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/dec/tulip/tulip.h#n319

* Update v3: add .can_receive routine
-> https://lists.gnu.org/archive/html/qemu-devel/2020-02/msg06275.html

Thank you.
--
Prasad J Pandit (2):
net: tulip: check frame size and r/w data length
net: tulip: add .can_recieve routine

 hw/net/tulip.c | 49 ++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 40 insertions(+), 9 deletions(-)

--
2.24.1

author
#1 lore raw

PATCH v3 1/2 net: tulip: check frame size and r/w data length

From: Prasad J Pandit <[email protected]>

Tulip network driver while copying tx/rx buffers does not check frame size against r/w data length. This may lead to OOB buffer access. Add check to avoid it.

Limit iterations over descriptors to avoid potential infinite loop issue in tulip_xmit_list_update.

---
 hw/net/tulip.c | 36 +++++++++++++++++++++++++++---------
 1 file changed, 27 insertions(+), 9 deletions(-)

Update v3: return a value from tulip_copy_tx_buffers() and avoid infinite loop
-> https://lists.gnu.org/archive/html/qemu-devel/2020-02/msg06275.html

hw/net/tulip.c +27 -9
index cfac2719d3..fbe40095da 100644
@@ -170,6 +170,10 @@ static void tulip_copy_rx_bytes(TULIPState *s, struct tulip_descriptor *desc)
         } else {
             len = s->rx_frame_len;
         }
+
+        if (s->rx_frame_len + len >= sizeof(s->rx_frame)) {
+            return;
+        }
         pci_dma_write(&s->dev, desc->buf_addr1, s->rx_frame +
             (s->rx_frame_size - s->rx_frame_len), len);
         s->rx_frame_len -= len;
@@ -181,6 +185,10 @@ static void tulip_copy_rx_bytes(TULIPState *s, struct tulip_descriptor *desc)
         } else {
             len = s->rx_frame_len;
         }
+
+        if (s->rx_frame_len + len >= sizeof(s->rx_frame)) {
+            return;
+        }
         pci_dma_write(&s->dev, desc->buf_addr2, s->rx_frame +
             (s->rx_frame_size - s->rx_frame_len), len);
         s->rx_frame_len -= len;
@@ -227,7 +235,8 @@ static ssize_t tulip_receive(TULIPState *s, const uint8_t *buf, size_t size)
 
     trace_tulip_receive(buf, size);
 
-    if (size < 14 || size > 2048 || s->rx_frame_len || tulip_rx_stopped(s)) {
+    if (size < 14 || size > sizeof(s->rx_frame) - 4
+        || s->rx_frame_len || tulip_rx_stopped(s)) {
         return 0;
     }
 
@@ -275,7 +284,6 @@ static ssize_t tulip_receive_nc(NetClientState *nc,
     return tulip_receive(qemu_get_nic_opaque(nc), buf, size);
 }
 
-
 static NetClientInfo net_tulip_info = {
     .type = NET_CLIENT_DRIVER_NIC,
     .size = sizeof(NICState),
@@ -558,7 +566,7 @@ static void tulip_tx(TULIPState *s, struct tulip_descriptor *desc)
         if ((s->csr[6] >> CSR6_OM_SHIFT) & CSR6_OM_MASK) {
             /* Internal or external Loopback */
             tulip_receive(s, s->tx_frame, s->tx_frame_len);
-        } else {
+        } else if (s->tx_frame_len <= sizeof(s->tx_frame)) {
             qemu_send_packet(qemu_get_queue(s->nic),
                 s->tx_frame, s->tx_frame_len);
         }
@@ -570,23 +578,31 @@ static void tulip_tx(TULIPState *s, struct tulip_descriptor *desc)
     }
 }
 
-static void tulip_copy_tx_buffers(TULIPState *s, struct tulip_descriptor *desc)
+static int tulip_copy_tx_buffers(TULIPState *s, struct tulip_descriptor *desc)
 {
     int len1 = (desc->control >> TDES1_BUF1_SIZE_SHIFT) & TDES1_BUF1_SIZE_MASK;
     int len2 = (desc->control >> TDES1_BUF2_SIZE_SHIFT) & TDES1_BUF2_SIZE_MASK;
 
+    if (s->tx_frame_len + len1 >= sizeof(s->tx_frame)) {
+        return -1;
+    }
     if (len1) {
         pci_dma_read(&s->dev, desc->buf_addr1,
             s->tx_frame + s->tx_frame_len, len1);
         s->tx_frame_len += len1;
     }
 
+    if (s->tx_frame_len + len2 >= sizeof(s->tx_frame)) {
+        return -1;
+    }
     if (len2) {
         pci_dma_read(&s->dev, desc->buf_addr2,
             s->tx_frame + s->tx_frame_len, len2);
         s->tx_frame_len += len2;
     }
     desc->status = (len1 + len2) ? 0 : 0x7fffffff;
+
+    return 0;
 }
 
 static void tulip_setup_filter_addr(TULIPState *s, uint8_t *buf, int n)
@@ -651,13 +667,15 @@ static uint32_t tulip_ts(TULIPState *s)
 
 static void tulip_xmit_list_update(TULIPState *s)
 {
+#define TULIP_DESC_MAX 128
+    uint8_t i = 0;
     struct tulip_descriptor desc;
 
     if (tulip_ts(s) != CSR5_TS_SUSPENDED) {
         return;
     }
 
-    for (;;) {
+    for (i = 0; i < TULIP_DESC_MAX; i++) {
         tulip_desc_read(s, s->current_tx_desc, &desc);
         tulip_dump_tx_descriptor(s, &desc);
 
@@ -675,10 +693,10 @@ static void tulip_xmit_list_update(TULIPState *s)
                 s->tx_frame_len = 0;
             }
 
-            tulip_copy_tx_buffers(s, &desc);
-
-            if (desc.control & TDES1_LS) {
-                tulip_tx(s, &desc);
+            if (!tulip_copy_tx_buffers(s, &desc)) {
+                if (desc.control & TDES1_LS) {
+                    tulip_tx(s, &desc);
+                }
             }
         }
         tulip_desc_write(s, s->current_tx_desc, &desc);
Signature · 1 line
2.24.1
author
#2 lore raw

PATCH v3 2/2 net: tulip: add .can_recieve routine

In reply to P J P

From: Prasad J Pandit <[email protected]>

Define .can_receive routine to do sanity checks before receiving packet data.

---
 hw/net/tulip.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

Update v3: define .can_receive routine
-> https://lists.gnu.org/archive/html/qemu-devel/2020-02/msg06275.html

hw/net/tulip.c +14 -1
index fbe40095da..757f12c710 100644
@@ -229,6 +229,18 @@ static bool tulip_filter_address(TULIPState *s, const uint8_t *addr)
     return ret;
 }
 
+static int
+tulip_can_receive(NetClientState *nc)
+{
+    TULIPState *s = qemu_get_nic_opaque(nc);
+
+    if (s->rx_frame_len || tulip_rx_stopped(s)) {
+        return false;
+    }
+
+    return true;
+}
+
 static ssize_t tulip_receive(TULIPState *s, const uint8_t *buf, size_t size)
 {
     struct tulip_descriptor desc;
@@ -236,7 +248,7 @@ static ssize_t tulip_receive(TULIPState *s, const uint8_t *buf, size_t size)
     trace_tulip_receive(buf, size);
 
     if (size < 14 || size > sizeof(s->rx_frame) - 4
-        || s->rx_frame_len || tulip_rx_stopped(s)) {
+        || !tulip_can_receive(s->nic->ncs)) {
         return 0;
     }
 
@@ -288,6 +300,7 @@ static NetClientInfo net_tulip_info = {
     .type = NET_CLIENT_DRIVER_NIC,
     .size = sizeof(NICState),
     .receive = tulip_receive_nc,
+    .can_receive = tulip_can_receive,
 };
 
 static const char *tulip_reg_name(const hwaddr addr)
Signature · 1 line
2.24.1
#3 lore raw

Re: [PATCH v3 2/2] net: tulip: add .can_recieve routine

Quoted 9 lines · P J P
hw/net/tulip.c +1 -0
index fbe40095da..757f12c710 100644
@@ -229,6 +229,18 @@ static bool tulip_filter_address(TULIPState *s, const uint8_t *addr)
     return ret;
 }
 
+static int

hw/net/tulip.c static bool tulip_filter_address(TULIPState *s, const uint8_t *addr) · line 242

Quoted 10 lines
hw/net/tulip.c +10 -0
+tulip_can_receive(NetClientState *nc)
+{
+    TULIPState *s = qemu_get_nic_opaque(nc);
+
+    if (s->rx_frame_len || tulip_rx_stopped(s)) {
+        return false;
+    }
+
+    return true;
+}

Are the required qemu_flush_queued_packets() calls in place so that packet transfer wakes up again when .can_receive() transitions from false to true?

(If qemu_flush_queued_packets() is missing then transmission hangs after .can_receive() becomes false.)

Stefan

author
#4 lore raw

Re: [PATCH v3 2/2] net: tulip: add .can_recieve routine

Hello Stefan, Jason,

+-- On Fri, 6 Mar 2020, Stefan Hajnoczi wrote --+
| > +static int
| > +tulip_can_receive(NetClientState *nc)
| > +{
| > +    TULIPState *s = qemu_get_nic_opaque(nc);
| > +
| > +    if (s->rx_frame_len || tulip_rx_stopped(s)) {
| > +        return false;
| > +    }
| > +
| > +    return true;
| > +}
| 
| Are the required qemu_flush_queued_packets() calls in place so that
| packet transfer wakes up again when .can_receive() transitions from
| false to true?

Yes, qemu_flush_queued_packets() calls are in tulip_write(). Do we need to
call tulip_can_receive() before each call?

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D

#5 lore raw

Re: [PATCH v3 2/2] net: tulip: add .can_recieve routine

Quoted 19 lines · P J P

Hello Stefan, Jason,

+-- On Fri, 6 Mar 2020, Stefan Hajnoczi wrote --+
| > +static int
| > +tulip_can_receive(NetClientState *nc)
| > +{
| > +    TULIPState *s = qemu_get_nic_opaque(nc);
| > +
| > +    if (s->rx_frame_len || tulip_rx_stopped(s)) {
| > +        return false;
| > +    }
| > +
| > +    return true;
| > +}
|
| Are the required qemu_flush_queued_packets() calls in place so that
| packet transfer wakes up again when .can_receive() transitions from
| false to true?
Quoted 2 lines

Yes, qemu_flush_queued_packets() calls are in tulip_write(). Do we need to
call tulip_can_receive() before each call?

Probably not, just need to make sure the check in tulip_rx_stopped(s) matches the action that triggers qemu_flush_queued_packets() in tulip_write() is sufficient.

This to make sure net core can restore the receiving.

Btw, what's the point of checking rx_frame_len here?

Thanks

Quoted 5 lines

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D

author
#6 lore raw

Re: [PATCH v3 2/2] net: tulip: add .can_recieve routine

+-- On Tue, 17 Mar 2020, Jason Wang wrote --+
| > +-- On Fri, 6 Mar 2020, Stefan Hajnoczi wrote --+
| > | > +static int
| > | > +tulip_can_receive(NetClientState *nc)
| > | > +{
| > | > +    TULIPState *s = qemu_get_nic_opaque(nc);
| > | > +
| > | > +    if (s->rx_frame_len || tulip_rx_stopped(s)) {
| > | > +        return false;
| > | > +    }
|
| Btw, what's the point of checking rx_frame_len here?

tulip_can_recive() is called from tulip_receive(). IIUC non zero(0) 'rx_frame_len' hints that s->rs_frame[] buffer still has unread data bytes and it can not receive new bytes. The check was earlier in tulip_receive().

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D

#7 lore raw

Re: [PATCH v3 2/2] net: tulip: add .can_recieve routine

Quoted 13 lines · P J P
+-- On Tue, 17 Mar 2020, Jason Wang wrote --+
| > +-- On Fri, 6 Mar 2020, Stefan Hajnoczi wrote --+
| > | > +static int
| > | > +tulip_can_receive(NetClientState *nc)
| > | > +{
| > | > +    TULIPState *s = qemu_get_nic_opaque(nc);
| > | > +
| > | > +    if (s->rx_frame_len || tulip_rx_stopped(s)) {
| > | > +        return false;
| > | > +    }
|
| Btw, what's the point of checking rx_frame_len here?
Quoted 3 lines

tulip_can_recive() is called from tulip_receive(). IIUC non zero(0) 'rx_frame_len' hints that s->rs_frame[] buffer still has unread data bytes and it can not receive new bytes. The check was earlier in tulip_receive().

Right, so need to make sure qemu_flush_ququed_packets() was called when rx_frame_len is zero.

Thanks

Quoted 5 lines

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D

author
#8 lore raw

Re: [PATCH v3 2/2] net: tulip: add .can_recieve routine

Hello Jason,

+-- On Wed, 18 Mar 2020, Jason Wang wrote --+
| Right, so need to make sure qemu_flush_ququed_packets() was called when 
| rx_frame_len is zero.

Sent patch v4, with this call. Please see when you've time.

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D

#9 lore raw
In reply to P J P
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series seems to have some coding style problems. See output below for more information:

Subject: [PATCH v3 0/2] net: tulip: add checks to avoid OOB access
Message-id: [email protected]
Type: series

=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram
./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===

fatal: unable to access 'https://github.com/patchew-project/qemu/': Failed connect to github.com:443; No route to host Traceback (most recent call last):
File "patchew-tester2/src/patchew-cli", line 531, in test_one

    git_clone_repo(clone, r["repo"], r["head"], logf, True)

File "patchew-tester2/src/patchew-cli", line 54, in git_clone_repo
stdout=logf, stderr=logf)
File "/opt/rh/rh-python36/root/usr/lib64/python3.6/subprocess.py", line 291, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['git', 'fetch', '3c8cf5a9c21ff8782164d1def7f44bd888713384']' returned non-zero exit status 128.

The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]

#11 lore raw
In reply to P J P
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series failed build test on FreeBSD host. Please find the details below.

=== TEST SCRIPT BEGIN ===
#!/bin/bash
# Testing script will be invoked under the git checkout with # HEAD pointing to a commit that has the patches applied on top of "base"

# branch
if qemu-system-x86_64 --help >/dev/null 2>&1; then
  QEMU=qemu-system-x86_64
elif /usr/libexec/qemu-kvm --help >/dev/null 2>&1; then
  QEMU=/usr/libexec/qemu-kvm
else
  exit 1
fi
make vm-build-freebsd J=21 QEMU=$QEMU
exit 0
=== TEST SCRIPT END ===

The full log is available at
http://patchew.org/logs/[email protected]/testing.FreeBSD/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]

#12 lore raw
In reply to P J P
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series failed the docker-mingw@fedora build test. Please find the testing commands and their output below. If you have Docker installed, you can probably reproduce it locally.

=== TEST SCRIPT BEGIN ===
#! /bin/bash
export ARCH=x86_64
make docker-image-fedora V=1 NETWORK=1
time make docker-test-mingw@fedora J=14 NETWORK=1
=== TEST SCRIPT END ===

The full log is available at
http://patchew.org/logs/[email protected]/testing.docker-mingw@fedora/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]

#13 lore raw
In reply to P J P
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series failed the docker-quick@centos7 build test. Please find the testing commands and their output below. If you have Docker installed, you can probably reproduce it locally.

=== TEST SCRIPT BEGIN ===
#!/bin/bash
make docker-image-centos7 V=1 NETWORK=1
time make docker-test-quick@centos7 SHOW_ENV=1 J=14 NETWORK=1 === TEST SCRIPT END ===

The full log is available at
http://patchew.org/logs/[email protected]/testing.docker-quick@centos7/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]