From: Kuniyuki Iwashima <kuniyu@amazon.com>
To: <kuniyu@amazon.com>
Cc: <andrii@kernel.org>, <ast@kernel.org>, <bpf@vger.kernel.org>,
<daniel@iogearbox.net>, <davem@davemloft.net>,
<dsahern@kernel.org>, <edumazet@google.com>, <haoluo@google.com>,
<hemanthmalla@gmail.com>, <joe@cilium.io>, <joe@wand.net.nz>,
<john.fastabend@gmail.com>, <jolsa@kernel.org>,
<kpsingh@kernel.org>, <kuba@kernel.org>,
<linux-kernel@vger.kernel.org>, <linux-kselftest@vger.kernel.org>,
<lmb@isovalent.com>, <martin.lau@linux.dev>, <mykolal@fb.com>,
<netdev@vger.kernel.org>, <pabeni@redhat.com>, <sdf@google.com>,
<shuah@kernel.org>, <song@kernel.org>,
<willemdebruijn.kernel@gmail.com>, <yhs@fb.com>
Subject: Re: [PATCH bpf-next v3 6/7] bpf, net: Support SO_REUSEPORT sockets with bpf_sk_assign
Date: Mon, 26 Jun 2023 14:23:03 -0700 [thread overview]
Message-ID: <20230626212303.91671-1-kuniyu@amazon.com> (raw)
In-Reply-To: <20230626210846.84206-1-kuniyu@amazon.com>
From: Kuniyuki Iwashima <kuniyu@amazon.com>
Date: Mon, 26 Jun 2023 14:08:46 -0700
> From: Lorenz Bauer <lmb@isovalent.com>
> Date: Mon, 26 Jun 2023 16:09:03 +0100
> > Currently the bpf_sk_assign helper in tc BPF context refuses SO_REUSEPORT
> > sockets. This means we can't use the helper to steer traffic to Envoy,
> > which configures SO_REUSEPORT on its sockets. In turn, we're blocked
> > from removing TPROXY from our setup.
> >
> > The reason that bpf_sk_assign refuses such sockets is that the
> > bpf_sk_lookup helpers don't execute SK_REUSEPORT programs. Instead,
> > one of the reuseport sockets is selected by hash. This could cause
> > dispatch to the "wrong" socket:
> >
> > sk = bpf_sk_lookup_tcp(...) // select SO_REUSEPORT by hash
> > bpf_sk_assign(skb, sk) // SK_REUSEPORT wasn't executed
> >
> > Fixing this isn't as simple as invoking SK_REUSEPORT from the lookup
> > helpers unfortunately. In the tc context, L2 headers are at the start
> > of the skb, while SK_REUSEPORT expects L3 headers instead.
> >
> > Instead, we execute the SK_REUSEPORT program when the assigned socket
> > is pulled out of the skb, further up the stack. This creates some
> > trickiness with regards to refcounting as bpf_sk_assign will put both
> > refcounted and RCU freed sockets in skb->sk. reuseport sockets are RCU
> > freed. We can infer that the sk_assigned socket is RCU freed if the
> > reuseport lookup succeeds, but convincing yourself of this fact isn't
> > straight forward. Therefore we defensively check refcounting on the
> > sk_assign sock even though it's probably not required in practice.
> >
> > Fixes: 8e368dc72e86 ("bpf: Fix use of sk->sk_reuseport from sk_assign")
> > Fixes: cf7fbe660f2d ("bpf: Add socket assign support")
> > Co-developed-by: Daniel Borkmann <daniel@iogearbox.net>
> > Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
> > Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
> > Cc: Joe Stringer <joe@cilium.io>
> > Link: https://lore.kernel.org/bpf/CACAyw98+qycmpQzKupquhkxbvWK4OFyDuuLMBNROnfWMZxUWeA@mail.gmail.com/
> > ---
> > include/net/inet6_hashtables.h | 59 ++++++++++++++++++++++++++++++++++++++----
> > include/net/inet_hashtables.h | 52 +++++++++++++++++++++++++++++++++++--
> > include/net/sock.h | 7 +++--
> > include/uapi/linux/bpf.h | 3 ---
> > net/core/filter.c | 2 --
> > net/ipv4/udp.c | 8 ++++--
> > net/ipv6/udp.c | 10 ++++---
> > tools/include/uapi/linux/bpf.h | 3 ---
> > 8 files changed, 122 insertions(+), 22 deletions(-)
> >
> > diff --git a/include/net/inet6_hashtables.h b/include/net/inet6_hashtables.h
> > index 4d2a1a3c0be7..4d300af6ccb6 100644
> > --- a/include/net/inet6_hashtables.h
> > +++ b/include/net/inet6_hashtables.h
> > @@ -103,6 +103,49 @@ static inline struct sock *__inet6_lookup(struct net *net,
> > daddr, hnum, dif, sdif);
> > }
> >
> > +static inline
> > +struct sock *inet6_steal_sock(struct net *net, struct sk_buff *skb, int doff,
> > + const struct in6_addr *saddr, const __be16 sport,
> > + const struct in6_addr *daddr, const __be16 dport,
> > + bool *refcounted, inet6_ehashfn_t ehashfn)
> > +{
> > + struct sock *sk, *reuse_sk;
> > + bool prefetched;
> > +
> > + sk = skb_steal_sock(skb, refcounted, &prefetched);
> > + if (!sk)
> > + return NULL;
> > +
> > + if (!prefetched)
> > + return sk;
> > +
> > + if (sk->sk_protocol == IPPROTO_TCP) {
> > + if (sk->sk_state != TCP_LISTEN)
> > + return sk;
> > + } else if (sk->sk_protocol == IPPROTO_UDP) {
> > + if (sk->sk_state != TCP_CLOSE)
> > + return sk;
> > + } else {
> > + return sk;
> > + }
> > +
> > + reuse_sk = inet6_lookup_reuseport(net, sk, skb, doff,
> > + saddr, sport, daddr, ntohs(dport),
> > + ehashfn);
> > + if (!reuse_sk || reuse_sk == sk)
> > + return sk;
> > +
> > + /* We've chosen a new reuseport sock which is never refcounted.
> > + * sk might be refcounted however, drop the reference if necessary.
> > + */
> > + if (*refcounted) {
> > + sock_put(sk);
> > + *refcounted = false;
> > + }
>
> As *refcounted should be false here (TCP_LISTEN and UDP sk have
> SOCK_RCU_FREE and other sk does not reach here), I prefer adding
> WARN_ON_ONCE() to catch a future bug:
>
> WARN_ON_ONCE(*refcounted);
> sock_put(sk);
Sorry, sock_put(sk) is not needed here.
next prev parent reply other threads:[~2023-06-26 21:23 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-26 15:08 [PATCH bpf-next v3 0/7] Add SO_REUSEPORT support for TC bpf_sk_assign Lorenz Bauer
2023-06-26 15:08 ` [PATCH bpf-next v3 1/7] udp: re-score reuseport groups when connected sockets are present Lorenz Bauer
2023-06-26 17:25 ` Kuniyuki Iwashima
2023-06-26 15:08 ` [PATCH bpf-next v3 2/7] net: export inet_lookup_reuseport and inet6_lookup_reuseport Lorenz Bauer
2023-06-26 17:32 ` Kuniyuki Iwashima
2023-06-27 8:56 ` Lorenz Bauer
2023-06-27 10:19 ` Daniel Borkmann
2023-06-26 15:09 ` [PATCH bpf-next v3 3/7] net: document inet[6]_lookup_reuseport sk_state requirements Lorenz Bauer
2023-06-26 17:57 ` Kuniyuki Iwashima
2023-06-26 15:09 ` [PATCH bpf-next v3 4/7] net: remove duplicate reuseport_lookup functions Lorenz Bauer
2023-06-26 18:11 ` Kuniyuki Iwashima
2023-06-26 15:09 ` [PATCH bpf-next v3 5/7] net: remove duplicate sk_lookup helpers Lorenz Bauer
2023-06-26 20:02 ` Kuniyuki Iwashima
2023-06-26 15:09 ` [PATCH bpf-next v3 6/7] bpf, net: Support SO_REUSEPORT sockets with bpf_sk_assign Lorenz Bauer
2023-06-26 21:08 ` Kuniyuki Iwashima
2023-06-26 21:23 ` Kuniyuki Iwashima [this message]
2023-06-26 15:09 ` [PATCH bpf-next v3 7/7] selftests/bpf: Test that SO_REUSEPORT can be used with sk_assign helper Lorenz Bauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230626212303.91671-1-kuniyu@amazon.com \
--to=kuniyu@amazon.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=dsahern@kernel.org \
--cc=edumazet@google.com \
--cc=haoluo@google.com \
--cc=hemanthmalla@gmail.com \
--cc=joe@cilium.io \
--cc=joe@wand.net.nz \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=lmb@isovalent.com \
--cc=martin.lau@linux.dev \
--cc=mykolal@fb.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sdf@google.com \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=willemdebruijn.kernel@gmail.com \
--cc=yhs@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox