Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp746146pxu; Fri, 11 Dec 2020 13:17:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJwUA9uO0ZdswUC//QeQ0FWAlbqIaTuKNkVp8itBSIGk44Prp6EyjNj8bvxaWsKjfeRh/fJG X-Received: by 2002:a17:906:cd14:: with SMTP id oz20mr11889138ejb.99.1607721469155; Fri, 11 Dec 2020 13:17:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607721469; cv=none; d=google.com; s=arc-20160816; b=fI0M9pEHRmZoPohr/+Xd8ofRRBmmwJCoy246JS0MWB4bP9xbeN3mFDBa5Ugxy54jKW TxliZFv61d37z6W+CvdCu/NorMRzI5NN9nLp2PeuDXIknsGSh/LDulhMleb25mQoItPd LWiaJYTsVh9/LdH4ga4KOEPmj13QiDO9OkQRfk0Wz/YSlaBAYTp6zHuo24EaEq7/Uifs KfqRHqcN5BxbmmRmTT67FNiTx4TZhi6iR/lwaBonUi0CUNybooT04VXO9hleyvrheRQD umvutHwmrjOuI4fjg6OH/6dEiXle3QtN+gCCxRJAv2XsZK8W/r5JlzeAc2HN8LO0d/ZA ExdA== 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=wwdGPuUGunfpIx+/6ROaQnSor1lkRExU528KjH7sBXs=; b=YjwjyIaKfitBmz9l60FgLQVOkcM2mnXmslkazj0ZpjV392VlrEoWOb9XlmNzRxDkwK nvIyrLQhE8ILfSSHLbMCGP3qNXrsG+PaDfMQ5t6a2BinCc9UilQSwFvqTOYR+1vVLcb5 WppPsAKS1zpKGFuTs9bUR7vyEYlUFdViy4l/QZwMnk8IskQeLkGLP/V6nKhm1lY3ahfS BZPXuWSH99gdn6XZO6smjc3keghnIASuhb11/CeQGXyQX6x/N6KKNrDVXfOshE3l90r/ uqKkrPpgyMWDREITQoSJ3FhoOJT2yJ1ddeuam1tUXAiE8Oezxp3gUOvTKiWZUU2qBaal z2Jg== 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 h16si5325439ejt.260.2020.12.11.13.17.26; Fri, 11 Dec 2020 13:17:49 -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 S2390503AbgLJTcy (ORCPT + 99 others); Thu, 10 Dec 2020 14:32:54 -0500 Received: from mail.kernel.org ([198.145.29.99]:37714 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732596AbgLJO3p (ORCPT ); Thu, 10 Dec 2020 09:29:45 -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.9 20/45] vlan: consolidate VLAN parsing code and limit max parsing depth Date: Thu, 10 Dec 2020 15:26:34 +0100 Message-Id: <20201210142603.364043007@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201210142602.361598591@linuxfoundation.org> References: <20201210142602.361598591@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 7e39719e27cbc..27edc7f2de31b 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 @@ -495,10 +497,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 @@ -513,13 +515,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 (eth_type_vlan(type)); @@ -538,11 +539,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