Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp89772pxu; Thu, 10 Dec 2020 19:22:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJweFIXIoMT3ksBcg5szGuJyuyKZFAqGvGJwRGeb/eZg8jv6XOGGLEEdzH1G124+WYUQm9zg X-Received: by 2002:aa7:d593:: with SMTP id r19mr9960027edq.246.1607656968940; Thu, 10 Dec 2020 19:22:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607656968; cv=none; d=google.com; s=arc-20160816; b=bJhLnJw02tVslilJQwaWCv9ibaX1Eqre9ZLV0jM+b4xdqoprpdjEien4CLBfmXwBth W/JM/84I/X4VdYn6oZpSiZZnFqNH6QgOQul88mVtnWKwxnERShYvt+64WsXjTuxy9gnG qTG6DK8pmxR3h5cjR6TZmJUIlgR1SxCHuqBYWQ5Nt8oXcpV8M51CRX1FzsPFetZG/xGx yxLH5DWrt14vnWNK1qe6wJ3T0+sjhIKsa+GF/C+BYIFfACJDsMIaRNcQcAhh+QdmwOzc A1ZTb0bPsTqlhbeRMVgpLjyvkyAwViD6TADpQ/ic74NM94sLM8bE+5KW8koh5/LAoeoF 87TA== 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; bh=tYm3abJ0vUSQ0H1GXokUGKt+/aGGwm8Ue5Nb1rhXYoQ=; b=R1Q+kTeC81sEP3p5vuY39ROJLEkxcr6xZ9y6xmU6lOch5scgr+mpCHjXxZU3G//gtT EH91S47zozJUj58AcUlofGxZJcRVvXX3BEeIRX8OJOzbPp8aCXZzw+c+B+qNREL8ejHq mFDgodBh/oZ8wZiNps9+wmXN8Rwx9bDbak8v8/8idaDOkbPWQdNeleL8M4Va0CHVb/Ee HPIf36oWJWg8JDYoTN12YQ9Y47tgIWNcekWJ2EgMMPggfwDZ9BNF8PmkCXcwwD/ZRthJ 8xO7gYUZYEQUnAFQRL7SGfPW1EHSyWOhx7nvEm2Mxghas2TOZ4hao+QkLrJivB+36ECL wElQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (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 cz22si4013602edb.241.2020.12.10.19.22.12; Thu, 10 Dec 2020 19:22:48 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390102AbgLJO2P (ORCPT + 99 others); Thu, 10 Dec 2020 09:28:15 -0500 Received: from mail.kernel.org ([198.145.29.99]:36412 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390076AbgLJO2B (ORCPT ); Thu, 10 Dec 2020 09:28:01 -0500 From: Greg Kroah-Hartman Authentication-Results: mail.kernel.org; dkim=permerror (bad message/signature format) To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Toshiaki Makita , Daniel Borkmann , =?UTF-8?q?Toke=20H=C3=B8iland-J=C3=B8rgensen?= , "David S. Miller" , Sasha Levin Subject: [PATCH 4.4 14/39] vlan: consolidate VLAN parsing code and limit max parsing depth Date: Thu, 10 Dec 2020 15:26:25 +0100 Message-Id: <20201210142601.602997730@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201210142600.887734129@linuxfoundation.org> References: <20201210142600.887734129@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: Toke Høiland-Jørgensen [ Upstream commit 469aceddfa3ed16e17ee30533fae45e90f62efd8 ] Toshiaki pointed out that we now have two very similar functions to extract the L3 protocol number in the presence of VLAN tags. And Daniel pointed out that the unbounded parsing loop makes it possible for maliciously crafted packets to loop through potentially hundreds of tags. Fix both of these issues by consolidating the two parsing functions and limiting the VLAN tag parsing to a max depth of 8 tags. As part of this, switch over __vlan_get_protocol() to use skb_header_pointer() instead of pskb_may_pull(), to avoid the possible side effects of the latter and keep the skb pointer 'const' through all the parsing functions. v2: - Use limit of 8 tags instead of 32 (matching XMIT_RECURSION_LIMIT) Reported-by: Toshiaki Makita Reported-by: Daniel Borkmann Fixes: d7bf2ebebc2b ("sched: consistently handle layer3 header accesses in the presence of VLANs") Signed-off-by: Toke Høiland-Jørgensen Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- include/linux/if_vlan.h | 29 ++++++++++++++++++++++------- include/net/inet_ecn.h | 1 + 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/include/linux/if_vlan.h b/include/linux/if_vlan.h index dd676ba758ee7..40429b818b457 100644 --- a/include/linux/if_vlan.h +++ b/include/linux/if_vlan.h @@ -30,6 +30,8 @@ #define VLAN_ETH_DATA_LEN 1500 /* Max. octets in payload */ #define VLAN_ETH_FRAME_LEN 1518 /* Max. octets in frame sans FCS */ +#define VLAN_MAX_DEPTH 8 /* Max. number of nested VLAN tags parsed */ + /* * struct vlan_hdr - vlan header * @h_vlan_TCI: priority and VLAN ID @@ -478,10 +480,10 @@ static inline int vlan_get_tag(const struct sk_buff *skb, u16 *vlan_tci) * Returns the EtherType of the packet, regardless of whether it is * vlan encapsulated (normal or hardware accelerated) or not. */ -static inline __be16 __vlan_get_protocol(struct sk_buff *skb, __be16 type, +static inline __be16 __vlan_get_protocol(const struct sk_buff *skb, __be16 type, int *depth) { - unsigned int vlan_depth = skb->mac_len; + unsigned int vlan_depth = skb->mac_len, parse_depth = VLAN_MAX_DEPTH; /* if type is 802.1Q/AD then the header should already be * present at mac_len - VLAN_HLEN (if mac_len > 0), or at @@ -496,13 +498,12 @@ static inline __be16 __vlan_get_protocol(struct sk_buff *skb, __be16 type, vlan_depth = ETH_HLEN; } do { - struct vlan_hdr *vh; + struct vlan_hdr vhdr, *vh; - if (unlikely(!pskb_may_pull(skb, - vlan_depth + VLAN_HLEN))) + vh = skb_header_pointer(skb, vlan_depth, sizeof(vhdr), &vhdr); + if (unlikely(!vh || !--parse_depth)) return 0; - vh = (struct vlan_hdr *)(skb->data + vlan_depth); type = vh->h_vlan_encapsulated_proto; vlan_depth += VLAN_HLEN; } while (type == htons(ETH_P_8021Q) || @@ -522,11 +523,25 @@ static inline __be16 __vlan_get_protocol(struct sk_buff *skb, __be16 type, * Returns the EtherType of the packet, regardless of whether it is * vlan encapsulated (normal or hardware accelerated) or not. */ -static inline __be16 vlan_get_protocol(struct sk_buff *skb) +static inline __be16 vlan_get_protocol(const struct sk_buff *skb) { return __vlan_get_protocol(skb, skb->protocol, NULL); } +/* A getter for the SKB protocol field which will handle VLAN tags consistently + * whether VLAN acceleration is enabled or not. + */ +static inline __be16 skb_protocol(const struct sk_buff *skb, bool skip_vlan) +{ + if (!skip_vlan) + /* VLAN acceleration strips the VLAN header from the skb and + * moves it to skb->vlan_proto + */ + return skb_vlan_tag_present(skb) ? skb->vlan_proto : skb->protocol; + + return vlan_get_protocol(skb); +} + static inline void vlan_set_encap_proto(struct sk_buff *skb, struct vlan_hdr *vhdr) { diff --git a/include/net/inet_ecn.h b/include/net/inet_ecn.h index dce2d586d9cec..245d999c0eac8 100644 --- a/include/net/inet_ecn.h +++ b/include/net/inet_ecn.h @@ -3,6 +3,7 @@ #include #include +#include #include #include -- 2.27.0