From patchwork Sun Jan 31 18:13:27 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Xu X-Patchwork-Id: 576237 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 478BE140C05 for ; Mon, 1 Feb 2016 05:18:18 +1100 (AEDT) Received: from localhost ([::1]:42752 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aPwZk-00022h-4A for incoming@patchwork.ozlabs.org; Sun, 31 Jan 2016 13:18:16 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36803) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aPwVw-0002z7-4c for qemu-devel@nongnu.org; Sun, 31 Jan 2016 13:14:21 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aPwVr-0006By-4z for qemu-devel@nongnu.org; Sun, 31 Jan 2016 13:14:20 -0500 Received: from mx1.redhat.com ([209.132.183.28]:36658) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aPwVq-0006Bu-Ow for qemu-devel@nongnu.org; Sun, 31 Jan 2016 13:14:15 -0500 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id 7DC4070D66 for ; Sun, 31 Jan 2016 18:14:14 +0000 (UTC) Received: from wei-thinkpad.nay.redhat.com (vpn1-6-127.pek2.redhat.com [10.72.6.127]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u0VIDVgU014091; Sun, 31 Jan 2016 13:14:09 -0500 From: wexu@redhat.com To: qemu-devel@nongnu.org Date: Mon, 1 Feb 2016 02:13:27 +0800 Message-Id: <1454264009-24094-9-git-send-email-wexu@redhat.com> In-Reply-To: <1454264009-24094-1-git-send-email-wexu@redhat.com> References: <1454264009-24094-1-git-send-email-wexu@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Wei Xu , victork@redhat.com, mst@redhat.com, jasowang@redhat.com, yvugenfi@redhat.com, Wei Xu , marcel@redhat.com, dfleytma@redhat.com Subject: [Qemu-devel] [RFC Patch v2 08/10] virtio-net rsc: Sanity check & More bypass cases check X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Wei Xu More general exception cases check 1. Incorrect version in IP header 2. IP options & IP fragment 3. Not a TCP packets 4. Sanity size check to prevent buffer overflow attack. Signed-off-by: Wei Xu --- hw/net/virtio-net.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c index b0987d0..9b44762 100644 --- a/hw/net/virtio-net.c +++ b/hw/net/virtio-net.c @@ -1948,6 +1948,46 @@ static size_t virtio_net_rsc_drain_one(NetRscChain *chain, NetClientState *nc, return virtio_net_do_receive(nc, buf, size); } + +static int32_t virtio_net_rsc_filter4(NetRscChain *chain, struct ip_header *ip, + const uint8_t *buf, size_t size) +{ + uint16_t ip_len; + + if (size < (TCP4_OFFSET + sizeof(tcp_header))) { + return RSC_BYPASS; + } + + /* Not an ipv4 one */ + if (0x4 != ((0xF0 & ip->ip_ver_len) >> 4)) { + return RSC_BYPASS; + } + + /* Don't handle packets with ip option */ + if (5 != (0xF & ip->ip_ver_len)) { + return RSC_BYPASS; + } + + /* Don't handle packets with ip fragment */ + if (!(htons(ip->ip_off) & IP_DF)) { + return RSC_BYPASS; + } + + if (ip->ip_p != IPPROTO_TCP) { + return RSC_BYPASS; + } + + /* Sanity check */ + ip_len = htons(ip->ip_len); + if (ip_len < (sizeof(struct ip_header) + sizeof(struct tcp_header)) + || ip_len > (size - IP_OFFSET)) { + return RSC_BYPASS; + } + + return RSC_WANT; +} + + static size_t virtio_net_rsc_receive4(void *opq, NetClientState* nc, const uint8_t *buf, size_t size) { @@ -1958,6 +1998,10 @@ static size_t virtio_net_rsc_receive4(void *opq, NetClientState* nc, chain = (NetRscChain *)opq; ip = (struct ip_header *)(buf + IP_OFFSET); + if (RSC_WANT != virtio_net_rsc_filter4(chain, ip, buf, size)) { + return virtio_net_do_receive(nc, buf, size); + } + ret = virtio_net_rsc_parse_tcp_ctrl((uint8_t *)ip, (0xF & ip->ip_ver_len) << 2); if (RSC_BYPASS == ret) {