From patchwork Thu Dec 6 04:40:47 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 1008607 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="IEYzyaF0"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 439NGx2pZ2z9s0t for ; Thu, 6 Dec 2018 15:41:44 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728833AbeLFElm (ORCPT ); Wed, 5 Dec 2018 23:41:42 -0500 Received: from mail-qt1-f201.google.com ([209.85.160.201]:56827 "EHLO mail-qt1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728144AbeLFElm (ORCPT ); Wed, 5 Dec 2018 23:41:42 -0500 Received: by mail-qt1-f201.google.com with SMTP id q33so22574025qte.23 for ; Wed, 05 Dec 2018 20:41:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=2QBaitPSK65LSPOpBTjVlJhZ3nIO/w6vV+4LvKbbSX8=; b=IEYzyaF0S9A916VuSx/N9ulAf6dK9DtziAH6j5bMvWupPjVP8lJKpYdLc2Sxu1MM4J +RoG358HMx/kvfiCmO0hMVRx4OandJ5jkFCkFKkonpbUOo1FsxNjhLDNWT0NW8yoVGBE UJtexsfTpelLPWoXqcI+VpV1naCA2yAT66GbqPJ2sqNW1UqOyWtVvux43laMeCGAi5bT m0WOG8gq6mNpb9kL7eAggV3w5lDes2NsyLtrEoxn2s6ZeEXX8IQ1KCuFLm/5IM9JJa/K RKQlvYmblXMAXkzfEb/3pUbPwQ0Mpj1x3U9m+nNFPWVT078TlwQTQnQj8e3BYw4Glubh 9ybA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=2QBaitPSK65LSPOpBTjVlJhZ3nIO/w6vV+4LvKbbSX8=; b=l4E4m7poU/jaGyf/iJA46UtsdKGic+eBDstGjPDjXp1C47EoxjYIlA2ugvnLv1thmS Xeyi4kTlJZfERtC2TWCSjYSUaC1Vlijo7ades2AKnjntaVA2uR6OFs+mXYXoaQtaskR0 DgUx7+1TEZh4a3KlGLx1sRzo61V6uCICXXYKvNYp8j+ZDI+Y1IBcSiQWOffoDFszJAFx huw+Dp7baSmSi4igOmXMZa9+35NDr0eby2oJYhNm9bjlZ17tg5faa5CE1vq+/eoaAziw jC6t0Ep3VEk7wfLQ1EDd9H0z9SCC9zbzapXUxk1B/qPbUmgUbZMEAa1qYar9IGGCnMk0 D1dg== X-Gm-Message-State: AA+aEWaAbFi/OmqLWDd+iHizh6agPwv1/xOslxKQwNY5MmR7r1GSPh5o BBto1pbV2dL5VAKPTjIHaQpZMLbYGYsLhYWhlyTdRmisZzi/CXqEjP4Nww0jFZkXgqW1N17lyI4 f1AyIzADvmanZbVLGasUMci05zaMRzUH/PxKaZiep5qeuuYQMl5dxMg== X-Google-Smtp-Source: AFSGD/VGHy8NYpKXDF8/zZI7ztm6VBBD7G2S7jfG9lj6l7QyWbMQ6T0YcAkbX+36oYpRYUQvFVQ0ok8= X-Received: by 2002:a37:652:: with SMTP id 79mr19455028qkg.62.1544071300713; Wed, 05 Dec 2018 20:41:40 -0800 (PST) Date: Wed, 5 Dec 2018 20:40:47 -0800 Message-Id: Mime-Version: 1.0 X-Mailer: git-send-email 2.20.0.rc1.387.gf8505762e3-goog Subject: [PATCH bpf 1/2] selftests/bpf: use thoff instead of nhoff in BPF flow dissector From: Stanislav Fomichev To: netdev@vger.kernel.org, ast@kernel.org Cc: davem@davemloft.net, daniel@iogearbox.net, simon.horman@netronome.com, liu.song.a23@gmail.com, Stanislav Fomichev Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org We are returning thoff from the flow dissector, not the nhoff. Pass thoff along with nhoff to the bpf program (initially thoff == nhoff) and expect flow dissector amend/return thoff, not nhoff. This avoids confusion, when by the time bpf flow dissector exits, nhoff == thoff, which doesn't make much sense. Signed-off-by: Stanislav Fomichev Acked-by: Song Liu --- net/core/flow_dissector.c | 1 + tools/testing/selftests/bpf/bpf_flow.c | 36 ++++++++++++-------------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/net/core/flow_dissector.c b/net/core/flow_dissector.c index 588f475019d4..ff5556d80570 100644 --- a/net/core/flow_dissector.c +++ b/net/core/flow_dissector.c @@ -783,6 +783,7 @@ bool __skb_flow_dissect(const struct sk_buff *skb, /* Pass parameters to the BPF program */ cb->qdisc_cb.flow_keys = &flow_keys; flow_keys.nhoff = nhoff; + flow_keys.thoff = nhoff; bpf_compute_data_pointers((struct sk_buff *)skb); result = BPF_PROG_RUN(attached, skb); diff --git a/tools/testing/selftests/bpf/bpf_flow.c b/tools/testing/selftests/bpf/bpf_flow.c index 107350a7821d..df9d32fd2055 100644 --- a/tools/testing/selftests/bpf/bpf_flow.c +++ b/tools/testing/selftests/bpf/bpf_flow.c @@ -70,18 +70,18 @@ static __always_inline void *bpf_flow_dissect_get_header(struct __sk_buff *skb, { void *data_end = (void *)(long)skb->data_end; void *data = (void *)(long)skb->data; - __u16 nhoff = skb->flow_keys->nhoff; + __u16 thoff = skb->flow_keys->thoff; __u8 *hdr; /* Verifies this variable offset does not overflow */ - if (nhoff > (USHRT_MAX - hdr_size)) + if (thoff > (USHRT_MAX - hdr_size)) return NULL; - hdr = data + nhoff; + hdr = data + thoff; if (hdr + hdr_size <= data_end) return hdr; - if (bpf_skb_load_bytes(skb, nhoff, buffer, hdr_size)) + if (bpf_skb_load_bytes(skb, thoff, buffer, hdr_size)) return NULL; return buffer; @@ -158,13 +158,13 @@ static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto) /* Only inspect standard GRE packets with version 0 */ return BPF_OK; - keys->nhoff += sizeof(*gre); /* Step over GRE Flags and Proto */ + keys->thoff += sizeof(*gre); /* Step over GRE Flags and Proto */ if (GRE_IS_CSUM(gre->flags)) - keys->nhoff += 4; /* Step over chksum and Padding */ + keys->thoff += 4; /* Step over chksum and Padding */ if (GRE_IS_KEY(gre->flags)) - keys->nhoff += 4; /* Step over key */ + keys->thoff += 4; /* Step over key */ if (GRE_IS_SEQ(gre->flags)) - keys->nhoff += 4; /* Step over sequence number */ + keys->thoff += 4; /* Step over sequence number */ keys->is_encap = true; @@ -174,7 +174,7 @@ static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto) if (!eth) return BPF_DROP; - keys->nhoff += sizeof(*eth); + keys->thoff += sizeof(*eth); return parse_eth_proto(skb, eth->h_proto); } else { @@ -191,7 +191,6 @@ static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto) if ((__u8 *)tcp + (tcp->doff << 2) > data_end) return BPF_DROP; - keys->thoff = keys->nhoff; keys->sport = tcp->source; keys->dport = tcp->dest; return BPF_OK; @@ -201,7 +200,6 @@ static __always_inline int parse_ip_proto(struct __sk_buff *skb, __u8 proto) if (!udp) return BPF_DROP; - keys->thoff = keys->nhoff; keys->sport = udp->source; keys->dport = udp->dest; return BPF_OK; @@ -252,8 +250,8 @@ PROG(IP)(struct __sk_buff *skb) keys->ipv4_src = iph->saddr; keys->ipv4_dst = iph->daddr; - keys->nhoff += iph->ihl << 2; - if (data + keys->nhoff > data_end) + keys->thoff += iph->ihl << 2; + if (data + keys->thoff > data_end) return BPF_DROP; if (iph->frag_off & bpf_htons(IP_MF | IP_OFFSET)) { @@ -285,7 +283,7 @@ PROG(IPV6)(struct __sk_buff *skb) keys->addr_proto = ETH_P_IPV6; memcpy(&keys->ipv6_src, &ip6h->saddr, 2*sizeof(ip6h->saddr)); - keys->nhoff += sizeof(struct ipv6hdr); + keys->thoff += sizeof(struct ipv6hdr); return parse_ipv6_proto(skb, ip6h->nexthdr); } @@ -301,7 +299,7 @@ PROG(IPV6OP)(struct __sk_buff *skb) /* hlen is in 8-octets and does not include the first 8 bytes * of the header */ - skb->flow_keys->nhoff += (1 + ip6h->hdrlen) << 3; + skb->flow_keys->thoff += (1 + ip6h->hdrlen) << 3; return parse_ipv6_proto(skb, ip6h->nexthdr); } @@ -315,7 +313,7 @@ PROG(IPV6FR)(struct __sk_buff *skb) if (!fragh) return BPF_DROP; - keys->nhoff += sizeof(*fragh); + keys->thoff += sizeof(*fragh); keys->is_frag = true; if (!(fragh->frag_off & bpf_htons(IP6_OFFSET))) keys->is_first_frag = true; @@ -341,7 +339,7 @@ PROG(VLAN)(struct __sk_buff *skb) __be16 proto; /* Peek back to see if single or double-tagging */ - if (bpf_skb_load_bytes(skb, keys->nhoff - sizeof(proto), &proto, + if (bpf_skb_load_bytes(skb, keys->thoff - sizeof(proto), &proto, sizeof(proto))) return BPF_DROP; @@ -354,14 +352,14 @@ PROG(VLAN)(struct __sk_buff *skb) if (vlan->h_vlan_encapsulated_proto != bpf_htons(ETH_P_8021Q)) return BPF_DROP; - keys->nhoff += sizeof(*vlan); + keys->thoff += sizeof(*vlan); } vlan = bpf_flow_dissect_get_header(skb, sizeof(*vlan), &_vlan); if (!vlan) return BPF_DROP; - keys->nhoff += sizeof(*vlan); + keys->thoff += sizeof(*vlan); /* Only allow 8021AD + 8021Q double tagging and no triple tagging.*/ if (vlan->h_vlan_encapsulated_proto == bpf_htons(ETH_P_8021AD) || vlan->h_vlan_encapsulated_proto == bpf_htons(ETH_P_8021Q)) From patchwork Thu Dec 6 04:40:48 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stanislav Fomichev X-Patchwork-Id: 1008608 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="SrR/KdVA"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 439NGz1fXmz9s3Z for ; Thu, 6 Dec 2018 15:41:47 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728951AbeLFElp (ORCPT ); Wed, 5 Dec 2018 23:41:45 -0500 Received: from mail-pg1-f202.google.com ([209.85.215.202]:55274 "EHLO mail-pg1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728144AbeLFElo (ORCPT ); Wed, 5 Dec 2018 23:41:44 -0500 Received: by mail-pg1-f202.google.com with SMTP id p4so12549543pgj.21 for ; Wed, 05 Dec 2018 20:41:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=EWBWFW17Lr/UhmSk/UKyBs74HwJ6mMGJj+/HRR4JM+I=; b=SrR/KdVAwY6hNNXh6dTmvqM8LBsJ8A0hd/XFZ72+hEgGssaYlkHwXQpO3J7MeoWW2t w4NvF7pswAnbuyVAuSHoCL11YtM3wcoY3qF/XWULmPmUG64FoaMkIY563CcdUeuhmExV xXN288FTSR+QSQCS8JC3YDevnToZdaGXKcDlNTMnbRM+9VsyAzN+KZ0rZBtIM2EYr3IC ByUVLuPDZG9ykd9Jpi5vl3ZPbsIeTD2xOOB/lrB84P+leCEaFHCRxSnIe9g3qeORy742 LJgxNgiv9nthb7KBcpgo/YcFVxdtnw8OtuIjaHd2rmP13H21Xh6OXh2Q/SjddBWXh6di RGMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=EWBWFW17Lr/UhmSk/UKyBs74HwJ6mMGJj+/HRR4JM+I=; b=fnfq4b3OZpJu22oRinU2QD2FQP3rWls6BVx68/eikqXbztqmLgtZGxy9dIUNXhBAhv A2f1gNVH4wna8BjaaJcum5L4Rqf8UVitefumPTmBXzeII8O6qu4jiy9ioy3d0rmPoRQl SwoDA5XplrwPF8W+Be0ZibN3jEy8RVTjg1czYK6xwNgsLvtGEaD0G+1pk1b1ibkpu6tF BzLtV1TPVbKZd9U347wwI74xORjNaUV5UfUYC/lZv0CVZPX4UsN7e4Ja+7J/e714o5nR xALS++GrkOFD4d8Dn+3ZjSyJ9B7HDoYxH0GnUHwiwevYcG6QiAcLru8SHOAogOW36ObJ Oq4A== X-Gm-Message-State: AA+aEWapn+X1mkbojbBcNMIxjJWDjLM6PyxlxsRoOCfFCcwpHjx8F0pr OD4qTet52Lhgc9bCB5/f9I5vd7T/PKw+Ksd78kVvB/vE9MczVRjPIyh5KukxrSAsT0HvY79qfq2 u5XtF6IYoxdlMA7GT3bKZUDPi7BAPuxL9q45ITQVWff7T0luF4h/WKg== X-Google-Smtp-Source: AFSGD/XkPlG6TrMhXN2hPYKIjU8SfhuvdGgtE+DiYN4ypZrzZ4zC/Lf9LOdSpV8Xh+kCE09amrnByis= X-Received: by 2002:a62:564b:: with SMTP id k72mr12549951pfb.30.1544071303539; Wed, 05 Dec 2018 20:41:43 -0800 (PST) Date: Wed, 5 Dec 2018 20:40:48 -0800 In-Reply-To: Message-Id: <33711939695bf620703d60064c6242a8e4f4b64a.1544071106.git.sdf@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.20.0.rc1.387.gf8505762e3-goog Subject: [PATCH bpf 2/2] net/flow_dissector: correctly cap nhoff and thoff in case of BPF From: Stanislav Fomichev To: netdev@vger.kernel.org, ast@kernel.org Cc: davem@davemloft.net, daniel@iogearbox.net, simon.horman@netronome.com, liu.song.a23@gmail.com, Stanislav Fomichev Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org We want to make sure that the following condition holds: 0 <= nhoff <= thoff <= skb->len BPF program can set out-of-bounds nhoff and thoff, which is dangerous, see recent commit d0c081b49137 ("flow_dissector: properly cap thoff field")'. Signed-off-by: Stanislav Fomichev Acked-by: Song Liu --- net/core/flow_dissector.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/net/core/flow_dissector.c b/net/core/flow_dissector.c index ff5556d80570..af68207ee56c 100644 --- a/net/core/flow_dissector.c +++ b/net/core/flow_dissector.c @@ -791,9 +791,12 @@ bool __skb_flow_dissect(const struct sk_buff *skb, /* Restore state */ memcpy(cb, &cb_saved, sizeof(cb_saved)); + flow_keys.nhoff = clamp_t(u16, flow_keys.nhoff, 0, skb->len); + flow_keys.thoff = clamp_t(u16, flow_keys.thoff, + flow_keys.nhoff, skb->len); + __skb_flow_bpf_to_target(&flow_keys, flow_dissector, target_container); - key_control->thoff = min_t(u16, key_control->thoff, skb->len); rcu_read_unlock(); return result == BPF_OK; }