Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp641694pxv; Thu, 15 Jul 2021 12:14:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzYvA5CiATT0J2kahuOywzyLg1gNzDFhkWsHnF2ncHorzhfoFfwVO2+Kog/51N6R1LIMQJy X-Received: by 2002:a02:a595:: with SMTP id b21mr5353161jam.122.1626376485749; Thu, 15 Jul 2021 12:14:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626376485; cv=none; d=google.com; s=arc-20160816; b=ArfVdW6S+W08eyH68TjiGNSgxHhx4LyM3POS8vimPfaoGuo/alWwI+O4HcY0Bd484J RGcmtWs701T8gYXWDyfQIVsg+7rGI38ojhcvBNiAT2l8Ous48o9niOAjTOVg3N/whV5q KvzMp1Lzm2VgjftrDz3LeVncYDPr8E1+1mgdRyTQ7dKsxDm8NTZ97IRXeH0/e8IgNL08 NBrxI4lLiAe71ebZh5UwKGJJ0Dl/DJq9gr1aqVfLK4ZI/sX90tLhMOzxZ5/Vjd/VBiYy 9LE/TdCK32YHej7g/6d5PX1koT0lXCSK8BPEFGFNwcxkwBnvoeKeJn36rDfbkg7x64lJ 1aDg== 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=I1Xt2aHdGn9eC6hYb8uERmF6gX44r0uWKAiwl5BTk/E=; b=xX7nOFdYnupfg8j58d+7vPRg+CjNQfC88kXKEAPRgWmJ7l/B5LpTk9N+UwGDHsTMM9 +EEemZfx+nY5ubM1UFB+733bWe0HOsQWV1/wFi6LzjloCG/cuZL7CuHoHjt9Yknd7j5u VdaOY9ZfslHLRMN9hObVdQgeowB6ZiqBgAXGnmCWmx0Nx2wVcvK0xRgL0jN/lhaCVOpH 2HAHPSwatf9/obA22T7+CGVYtceWRfx9MTdjKSjNxX1MHyzjbdaO9FRY+J76AXm6lTLk mp1rbyWeupVB8wMgZ0NxS8m95TtDeE9GfhLvpB5HgbPRZ91aTE3jg7rfI/tSRYmp3ghU L0xg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=zK2w+NLR; 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 p10si260623iov.66.2021.07.15.12.14.33; Thu, 15 Jul 2021 12:14:45 -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=zK2w+NLR; 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 S244475AbhGOTOu (ORCPT + 99 others); Thu, 15 Jul 2021 15:14:50 -0400 Received: from mail.kernel.org ([198.145.29.99]:34984 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240997AbhGOS6t (ORCPT ); Thu, 15 Jul 2021 14:58:49 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id A2ADA613D4; Thu, 15 Jul 2021 18:55:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626375343; bh=X0bu4H7WOFKLkbG4+PvRP5lJ23vKTpaLCOAkkv0fX30=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=zK2w+NLRL88ER8JupN/fp18vOgW2SZ583ZoNr8nLA75qngFiGPZ9FeRBYdG/7uMKa 1W+9eeUlqnXzMAo9sdpBDwBwv7+U1VCH53akz1oHISLqlzwJfoAimdKDtF7JIOKnQe kz7mynOcJ1DlA+Fx9SOTBKHOLE3q8xuSbfY84RKU= 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.12 046/242] virtio-net: Add validation for used length Date: Thu, 15 Jul 2021 20:36:48 +0200 Message-Id: <20210715182600.275545240@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210715182551.731989182@linuxfoundation.org> References: <20210715182551.731989182@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 0824e6999e49..447582fa20a5 100644 --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -660,6 +660,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) { @@ -761,6 +767,7 @@ err: err_xdp: rcu_read_unlock(); stats->xdp_drops++; +err_len: stats->drops++; put_page(page); xdp_xmit: @@ -814,6 +821,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) { @@ -938,13 +951,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); curr_skb = head_skb; -- 2.30.2