All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jason Wang <jasowang@redhat.com>
To: peter.maydell@linaro.org, qemu-devel@nongnu.org
Cc: Petr Matousek <pmatouse@redhat.com>,
	Stefano Stabellini <stefano.stabellini@eu.citrix.com>,
	Jason Wang <jasowang@redhat.com>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	Michael Roth <mdroth@linux.vnet.ibm.com>,
	Prasad Pandit <ppandit@redhat.com>,
	qemu-stable@nongnu.org, Laszlo Ersek <lersek@redhat.com>
Subject: [Qemu-devel] [PULL 14/17] e1000: eliminate infinite loops on out-of-bounds transfer start
Date: Tue,  2 Feb 2016 10:36:18 +0800	[thread overview]
Message-ID: <1454380581-7881-15-git-send-email-jasowang@redhat.com> (raw)
In-Reply-To: <1454380581-7881-1-git-send-email-jasowang@redhat.com>

From: Laszlo Ersek <lersek@redhat.com>

The start_xmit() and e1000_receive_iov() functions implement DMA transfers
iterating over a set of descriptors that the guest's e1000 driver
prepares:

- the TDLEN and RDLEN registers store the total size of the descriptor
  area,

- while the TDH and RDH registers store the offset (in whole tx / rx
  descriptors) into the area where the transfer is supposed to start.

Each time a descriptor is processed, the TDH and RDH register is bumped
(as appropriate for the transfer direction).

QEMU already contains logic to deal with bogus transfers submitted by the
guest:

- Normally, the transmit case wants to increase TDH from its initial value
  to TDT. (TDT is allowed to be numerically smaller than the initial TDH
  value; wrapping at or above TDLEN bytes to zero is normal.) The failsafe
  that QEMU currently has here is a check against reaching the original
  TDH value again -- a complete wraparound, which should never happen.

- In the receive case RDH is increased from its initial value until
  "total_size" bytes have been received; preferably in a single step, or
  in "s->rxbuf_size" byte steps, if the latter is smaller. However, null
  RX descriptors are skipped without receiving data, while RDH is
  incremented just the same. QEMU tries to prevent an infinite loop
  (processing only null RX descriptors) by detecting whether RDH assumes
  its original value during the loop. (Again, wrapping from RDLEN to 0 is
  normal.)

What both directions miss is that the guest could program TDLEN and RDLEN
so low, and the initial TDH and RDH so high, that these registers will
immediately be truncated to zero, and then never reassume their initial
values in the loop -- a full wraparound will never occur.

The condition that expresses this is:

  xdh_start >= s->mac_reg[XDLEN] / sizeof(desc)

i.e., TDH or RDH start out after the last whole rx or tx descriptor that
fits into the TDLEN or RDLEN sized area.

This condition could be checked before we enter the loops, but
pci_dma_read() / pci_dma_write() knows how to fill in buffers safely for
bogus DMA addresses, so we just extend the existing failsafes with the
above condition.

This is CVE-2016-1981.

Cc: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Petr Matousek <pmatouse@redhat.com>
Cc: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
Cc: Prasad Pandit <ppandit@redhat.com>
Cc: Michael Roth <mdroth@linux.vnet.ibm.com>
Cc: Jason Wang <jasowang@redhat.com>
Cc: qemu-stable@nongnu.org
RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=1296044
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
---
 hw/net/e1000.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/hw/net/e1000.c b/hw/net/e1000.c
index 4eda7a3..0387fa0 100644
--- a/hw/net/e1000.c
+++ b/hw/net/e1000.c
@@ -909,7 +909,8 @@ start_xmit(E1000State *s)
          * bogus values to TDT/TDLEN.
          * there's nothing too intelligent we could do about this.
          */
-        if (s->mac_reg[TDH] == tdh_start) {
+        if (s->mac_reg[TDH] == tdh_start ||
+            tdh_start >= s->mac_reg[TDLEN] / sizeof(desc)) {
             DBGOUT(TXERR, "TDH wraparound @%x, TDT %x, TDLEN %x\n",
                    tdh_start, s->mac_reg[TDT], s->mac_reg[TDLEN]);
             break;
@@ -1166,7 +1167,8 @@ e1000_receive_iov(NetClientState *nc, const struct iovec *iov, int iovcnt)
         if (++s->mac_reg[RDH] * sizeof(desc) >= s->mac_reg[RDLEN])
             s->mac_reg[RDH] = 0;
         /* see comment in start_xmit; same here */
-        if (s->mac_reg[RDH] == rdh_start) {
+        if (s->mac_reg[RDH] == rdh_start ||
+            rdh_start >= s->mac_reg[RDLEN] / sizeof(desc)) {
             DBGOUT(RXERR, "RDH wraparound @%x, RDT %x, RDLEN %x\n",
                    rdh_start, s->mac_reg[RDT], s->mac_reg[RDLEN]);
             set_ics(s, 0, E1000_ICS_RXO);
-- 
2.5.0

  parent reply	other threads:[~2016-02-02  2:38 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-02  2:36 [Qemu-devel] [PULL 00/17] Net patches Jason Wang
2016-02-02  2:36 ` [Qemu-devel] [PULL 01/17] net/slirp: Tell the users when they are using deprecated options Jason Wang
2016-02-02  2:36 ` [Qemu-devel] [PULL 02/17] qemu-doc: Do not promote deprecated -smb and -redir options Jason Wang
2016-02-02  2:36 ` [Qemu-devel] [PULL 03/17] net: cadence_gem: check packet size in gem_recieve Jason Wang
2016-02-02  2:36 ` [Qemu-devel] [PULL 04/17] cadence_gem: fix buffer overflow Jason Wang
2016-02-02  2:36 ` [Qemu-devel] [PULL 05/17] slirp: goto bad in udp_input if sosendto fails Jason Wang
2016-02-02  2:36 ` [Qemu-devel] [PULL 06/17] slirp: Generalizing and neutralizing ARP code Jason Wang
2016-02-02  2:36 ` [Qemu-devel] [PULL 07/17] slirp: Adding address family switch for produced frames Jason Wang
2016-02-02  2:36 ` [Qemu-devel] [PULL 08/17] slirp: Make Socket structure IPv6 compatible Jason Wang
2016-02-02  2:36 ` [Qemu-devel] [PULL 09/17] slirp: Factorizing address translation Jason Wang
2016-02-02  2:36 ` [Qemu-devel] [PULL 10/17] slirp: Factorizing and cleaning solookup() Jason Wang
2016-02-02  2:36 ` [Qemu-devel] [PULL 11/17] slirp: Add sockaddr_equal, make solookup family-agnostic Jason Wang
2016-02-02  2:36 ` [Qemu-devel] [PULL 12/17] slirp: Make udp_attach IPv6 compatible Jason Wang
2016-02-02  2:36 ` [Qemu-devel] [PULL 13/17] slirp: Adding family argument to tcp_fconnect() Jason Wang
2016-02-02  2:36 ` Jason Wang [this message]
2016-02-02  2:36 ` [Qemu-devel] [PULL 15/17] net: netmap: use nm_open() to open netmap ports Jason Wang
2016-02-02  2:36 ` [Qemu-devel] [PULL 16/17] net: always walk through filters in reverse if traffic is egress Jason Wang
2016-02-02  2:36 ` [Qemu-devel] [PULL 17/17] net/filter: Fix the output information for command 'info network' Jason Wang
2016-02-02 11:05 ` [Qemu-devel] [PULL 00/17] Net patches Peter Maydell
2016-02-03  4:52   ` Jason Wang
2016-02-03  9:09     ` Samuel Thibault
2016-02-04  8:20       ` Jason Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1454380581-7881-15-git-send-email-jasowang@redhat.com \
    --to=jasowang@redhat.com \
    --cc=lersek@redhat.com \
    --cc=mdroth@linux.vnet.ibm.com \
    --cc=mst@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=pmatouse@redhat.com \
    --cc=ppandit@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-stable@nongnu.org \
    --cc=stefano.stabellini@eu.citrix.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.