Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp7427768pxb; Thu, 18 Feb 2021 09:46:44 -0800 (PST) X-Google-Smtp-Source: ABdhPJyANxHPv7cMIyOuYlDjHEEKY34gMshqz7svyfpzt45x7MqUKCz/X5VWUyowxYNOkCBph7kx X-Received: by 2002:a05:6402:1bc7:: with SMTP id ch7mr5331884edb.84.1613670404644; Thu, 18 Feb 2021 09:46:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1613670404; cv=none; d=google.com; s=arc-20160816; b=jiq+yrSAWgd/w4URUk4zbIm06eqOq3heWppP1nj1htvdmpOJLLOAgy+f33DTZm0CHG JxfdM2CSzWC3StD0UYn1pYeCVHXcuTRQoizr73ZYZEuaERd8nDUvCizKXdmAld70398e Lu1mttLACbifX2VrpEnzaWf8NJ0AfHs0sAMaodnV4tjgW7sksw7ryvh7Fp78mT8KE+X9 IHfTt/zTBmQmICSDRj5VONm43iEEwNMCiOHmvRQTFk8jP4/PHiIdjBdL6/rqnaMKAwc+ h5hGN1/MMEtjRmexX6C/7VsD2UUIpcTPgv0AmSOOZQQUTCBXCSN5rAyx5wzhVnt/TYlA GxaQ== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=96TuIx8B9Sg12x8R4gtF94r3wYssraiv2dr3ZsuIF9U=; b=NXkpfGx/8rxCvtkokfQaMZmXGILTdTVkdJRn8ybCnDP69kzN5XiUsTau1ZTvLfH4CC +WeS5GDcqrYy1gfgAf0AAKgph03l4YGLiYX0T9FqXXUebjrKA0rIetyYT3qgiDUvt9lA 4ACe9zUqn2hZI1AHk5lJF2M7j9QV1+HbPQJZUw+4KKksI8so8Cbo0Ecn67j4YyPAVAJb /0fHFHWfm0R0iIzopjzdKcP4HJAuoywe/j7urIhlDG9Bvt6Ryp79O92s8rwZBD6p8nzD DKIRljm1eyMjYYW0ooSTt9nLsTIPZbQ/VJgIdKS4nra8nd0T+CQ1mgUy1PIJjgn3YVq0 EE0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="Btkn4nC/"; 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s15si3884389edq.605.2021.02.18.09.46.20; Thu, 18 Feb 2021 09:46:44 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b="Btkn4nC/"; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234604AbhBRRn4 (ORCPT + 99 others); Thu, 18 Feb 2021 12:43:56 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:35750 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231449AbhBRPEZ (ORCPT ); Thu, 18 Feb 2021 10:04:25 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1613660496; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=96TuIx8B9Sg12x8R4gtF94r3wYssraiv2dr3ZsuIF9U=; b=Btkn4nC/5ACD8KZ02u6BSQLgvAjEO9A3S+2sJOb9PTk0zXOe9RXMJS4o49PNapd9ikPmcq fVGvdV3uB9TacWJn1Ec5GHO6TF8CEvwZFnpsUn6yXhOme2bJLxDFlP5UReK7YIJrHHhghA WC8mlp7CUcObgltFLbfe+RTExI9tA/k= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-435-yooqRrcDNXe02Q1acWS98Q-1; Thu, 18 Feb 2021 10:01:32 -0500 X-MC-Unique: yooqRrcDNXe02Q1acWS98Q-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4225480196C; Thu, 18 Feb 2021 15:01:31 +0000 (UTC) Received: from bnemeth.users.ipa.redhat.com (ovpn-114-242.ams2.redhat.com [10.36.114.242]) by smtp.corp.redhat.com (Postfix) with ESMTP id C9035100AE2B; Thu, 18 Feb 2021 15:01:25 +0000 (UTC) From: Balazs Nemeth To: netdev@vger.kernel.org Cc: linux-kernel@vger.kernel.org, mst@redhat.com, jasowang@redhat.com, davem@davemloft.net, willemb@google.com, virtualization@lists.linux-foundation.org, bnemeth@redhat.com Subject: [PATCH] net: check if protocol extracted by virtio_net_hdr_set_proto is correct Date: Thu, 18 Feb 2021 15:57:54 +0100 Message-Id: <5e910d11a14da17c41317417fc41d3a9d472c6e7.1613659844.git.bnemeth@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org For gso packets, virtio_net_hdr_set_proto sets the protocol (if it isn't set) based on the type in the virtio net hdr, but the skb could contain anything since it could come from packet_snd through a raw socket. If there is a mismatch between what virtio_net_hdr_set_proto sets and the actual protocol, then the skb could be handled incorrectly later on by gso. The network header of gso packets starts at 14 bytes, but a specially crafted packet could fool the call to skb_flow_dissect_flow_keys_basic as the network header offset in the skb could be incorrect. Consequently, EINVAL is not returned. There are even packets that can cause an infinite loop. For example, a packet with ethernet type ETH_P_MPLS_UC (which is unnoticed by virtio_net_hdr_to_skb) that is sent to a geneve interface will be handled by geneve_build_skb. In turn, it calls udp_tunnel_handle_offloads which then calls skb_reset_inner_headers. After that, the packet gets passed to mpls_gso_segment. That function calculates the mpls header length by taking the difference between network_header and inner_network_header. Since the two are equal (due to the earlier call to skb_reset_inner_headers), it will calculate a header of length 0, and it will not pull any headers. Then, it will call skb_mac_gso_segment which will again call mpls_gso_segment, etc... This leads to the infinite loop. For that reason, address the root cause of the issue: don't blindly trust the information provided by the virtio net header. Instead, check if the protocol in the packet actually matches the protocol set by virtio_net_hdr_set_proto. Fixes: 9274124f023b ("net: stricter validation of untrusted gso packets") Signed-off-by: Balazs Nemeth --- include/linux/virtio_net.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/include/linux/virtio_net.h b/include/linux/virtio_net.h index e8a924eeea3d..cf2c53563f22 100644 --- a/include/linux/virtio_net.h +++ b/include/linux/virtio_net.h @@ -79,8 +79,13 @@ static inline int virtio_net_hdr_to_skb(struct sk_buff *skb, if (gso_type && skb->network_header) { struct flow_keys_basic keys; - if (!skb->protocol) + if (!skb->protocol) { + const struct ethhdr *eth = skb_eth_hdr(skb); + virtio_net_hdr_set_proto(skb, hdr); + if (skb->protocol != eth->h_proto) + return -EINVAL; + } retry: if (!skb_flow_dissect_flow_keys_basic(NULL, skb, &keys, NULL, 0, 0, 0, -- 2.29.2