Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp713525pxv; Thu, 15 Jul 2021 14:12:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw+22486knjfgaa1BlmkKSUL5fWUzAUBkJVcLOIqd1wpqRuYsNz5sV3tuVBDp8GPr/XfVau X-Received: by 2002:a17:906:1789:: with SMTP id t9mr7773695eje.61.1626383560011; Thu, 15 Jul 2021 14:12:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626383560; cv=none; d=google.com; s=arc-20160816; b=LMLksg8q0dMzhUOMYMJCbRj6acO2421lM4/cAyGxO176r7Fl6s2Dk4aYiUmkoLvDwp Y/aTY0LT9Ng2YCaJkH95N/0McM52z2TMM9QKoHPPOnA2mDYGfO25c/IYpINxHZhpzyKA zoYBgy80A1tQWVTn1VFl47mr1FQTNqxVfLpIrj+hv25iMEfe688evgM4A4FgBBRmIERE uhaUpdv2HRhwyGisI/o6IKTb+CcKb1gtbq+Yo22rvdEJxVFRLCfl5/jN2svS3A4lbXmG MrVEU5AYwViOFpwIC+MlejW55urXPUuaeFlSOkTAOSyvlIaOY9w1TE/7vKBkEtHhNV2v hH+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=H8thi3BzpRO40/vuBO4xrqPCAHkpoM1FotD3HWYwWQs=; b=gMtWoa/t231fymrqYD6GFvzGq0QhaM6DgEhMnt0eX+d74kRClKcSRZdnYGgxVSSGNO U8w02Nh0LKrBF3iSj2N96ZPmc/NwBuoSaxMz+iin7mxZ5aEMNWnexgVIh8Dbe/obg7zl mfARf8mfduuUW1vglmKyL0EXIaJcptCrDyFUOX+zsLFQ5R2OOFosgCVA0s7kudOJwItK MYgumnEGH8IbC3MgNsaP56B7HRi6oWW6X90n9dQ0LkQijC8xUCCMbke+xUMTyqOlXKpa ugt0HofFqpOC8WIsAfYsRgSyEyjaztULFe6gnE8ob8Z0IcUWEprBFXZbNmT6TQEF0MVZ q2mg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=V3JMdhyx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i22si7505582ejw.286.2021.07.15.14.12.16; Thu, 15 Jul 2021 14:12:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=V3JMdhyx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243864AbhGOTbF (ORCPT + 99 others); Thu, 15 Jul 2021 15:31:05 -0400 Received: from mail.kernel.org ([198.145.29.99]:46538 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243387AbhGOTJs (ORCPT ); Thu, 15 Jul 2021 15:09:48 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 817DD613D6; Thu, 15 Jul 2021 19:05:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626375938; bh=8YwgmblhjtGcpiJc/o7ZXz/97jBEtBqAhD+Kruy3VJw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=V3JMdhyxGEbXhiOKSxu5f7/twLOLp7gjUroybfC8bE7MrLe4oAMvfERBxjqpKpjzV Fq176sOa6HqJAHxOlNe7Uy/DAXsozrdxSOzxKnLARWpWgwj/Vj2He6zXbEI2iIGDZo iYd2vGV+6IrjmY/qY5z5EhoxHK0mUNF53h2vW/OU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Xie Yongji , Jason Wang , Jakub Kicinski , Sasha Levin Subject: [PATCH 5.13 056/266] virtio-net: Add validation for used length Date: Thu, 15 Jul 2021 20:36:51 +0200 Message-Id: <20210715182624.097949419@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210715182613.933608881@linuxfoundation.org> References: <20210715182613.933608881@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Xie Yongji [ Upstream commit ad993a95c508417acdeb15244109e009e50d8758 ] This adds validation for used length (might come from an untrusted device) to avoid data corruption or loss. Signed-off-by: Xie Yongji Acked-by: Jason Wang Link: https://lore.kernel.org/r/20210531135852.113-1-xieyongji@bytedance.com Signed-off-by: Jakub Kicinski Signed-off-by: Sasha Levin --- drivers/net/virtio_net.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c index 78a01c71a17c..252f6718d730 100644 --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -721,6 +721,12 @@ static struct sk_buff *receive_small(struct net_device *dev, len -= vi->hdr_len; stats->bytes += len; + if (unlikely(len > GOOD_PACKET_LEN)) { + pr_debug("%s: rx error: len %u exceeds max size %d\n", + dev->name, len, GOOD_PACKET_LEN); + dev->stats.rx_length_errors++; + goto err_len; + } rcu_read_lock(); xdp_prog = rcu_dereference(rq->xdp_prog); if (xdp_prog) { @@ -824,6 +830,7 @@ err: err_xdp: rcu_read_unlock(); stats->xdp_drops++; +err_len: stats->drops++; put_page(page); xdp_xmit: @@ -877,6 +884,12 @@ static struct sk_buff *receive_mergeable(struct net_device *dev, head_skb = NULL; stats->bytes += len - vi->hdr_len; + if (unlikely(len > truesize)) { + pr_debug("%s: rx error: len %u exceeds truesize %lu\n", + dev->name, len, (unsigned long)ctx); + dev->stats.rx_length_errors++; + goto err_skb; + } rcu_read_lock(); xdp_prog = rcu_dereference(rq->xdp_prog); if (xdp_prog) { @@ -1004,13 +1017,6 @@ static struct sk_buff *receive_mergeable(struct net_device *dev, } rcu_read_unlock(); - if (unlikely(len > truesize)) { - pr_debug("%s: rx error: len %u exceeds truesize %lu\n", - dev->name, len, (unsigned long)ctx); - dev->stats.rx_length_errors++; - goto err_skb; - } - head_skb = page_to_skb(vi, rq, page, offset, len, truesize, !xdp_prog, metasize, headroom); curr_skb = head_skb; -- 2.30.2