Skip to content

Commit 9901c5d

Browse files
jrfastabborkmann
authored andcommitted
bpf: sockmap, fix crash when ipv6 sock is added
This fixes a crash where we assign tcp_prot to IPv6 sockets instead of tcpv6_prot. Previously we overwrote the sk->prot field with tcp_prot even in the AF_INET6 case. This patch ensures the correct tcp_prot and tcpv6_prot are used. Tested with 'netserver -6' and 'netperf -H [IPv6]' as well as 'netperf -H [IPv4]'. The ESTABLISHED check resolves the previously crashing case here. Fixes: 174a79f ("bpf: sockmap with sk redirect support") Reported-by: syzbot+5c063698bdbfac19f363@syzkaller.appspotmail.com Acked-by: Martin KaFai Lau <kafai@fb.com> Signed-off-by: John Fastabend <john.fastabend@gmail.com> Signed-off-by: Wei Wang <weiwan@google.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent ca09cb0 commit 9901c5d

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

kernel/bpf/sockmap.c

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ static int bpf_tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
140140
static int bpf_tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
141141
static int bpf_tcp_sendpage(struct sock *sk, struct page *page,
142142
int offset, size_t size, int flags);
143+
static void bpf_tcp_close(struct sock *sk, long timeout);
143144

144145
static inline struct smap_psock *smap_psock_sk(const struct sock *sk)
145146
{
@@ -161,7 +162,42 @@ static bool bpf_tcp_stream_read(const struct sock *sk)
161162
return !empty;
162163
}
163164

164-
static struct proto tcp_bpf_proto;
165+
enum {
166+
SOCKMAP_IPV4,
167+
SOCKMAP_IPV6,
168+
SOCKMAP_NUM_PROTS,
169+
};
170+
171+
enum {
172+
SOCKMAP_BASE,
173+
SOCKMAP_TX,
174+
SOCKMAP_NUM_CONFIGS,
175+
};
176+
177+
static struct proto *saved_tcpv6_prot __read_mostly;
178+
static DEFINE_SPINLOCK(tcpv6_prot_lock);
179+
static struct proto bpf_tcp_prots[SOCKMAP_NUM_PROTS][SOCKMAP_NUM_CONFIGS];
180+
static void build_protos(struct proto prot[SOCKMAP_NUM_CONFIGS],
181+
struct proto *base)
182+
{
183+
prot[SOCKMAP_BASE] = *base;
184+
prot[SOCKMAP_BASE].close = bpf_tcp_close;
185+
prot[SOCKMAP_BASE].recvmsg = bpf_tcp_recvmsg;
186+
prot[SOCKMAP_BASE].stream_memory_read = bpf_tcp_stream_read;
187+
188+
prot[SOCKMAP_TX] = prot[SOCKMAP_BASE];
189+
prot[SOCKMAP_TX].sendmsg = bpf_tcp_sendmsg;
190+
prot[SOCKMAP_TX].sendpage = bpf_tcp_sendpage;
191+
}
192+
193+
static void update_sk_prot(struct sock *sk, struct smap_psock *psock)
194+
{
195+
int family = sk->sk_family == AF_INET6 ? SOCKMAP_IPV6 : SOCKMAP_IPV4;
196+
int conf = psock->bpf_tx_msg ? SOCKMAP_TX : SOCKMAP_BASE;
197+
198+
sk->sk_prot = &bpf_tcp_prots[family][conf];
199+
}
200+
165201
static int bpf_tcp_init(struct sock *sk)
166202
{
167203
struct smap_psock *psock;
@@ -181,14 +217,17 @@ static int bpf_tcp_init(struct sock *sk)
181217
psock->save_close = sk->sk_prot->close;
182218
psock->sk_proto = sk->sk_prot;
183219

184-
if (psock->bpf_tx_msg) {
185-
tcp_bpf_proto.sendmsg = bpf_tcp_sendmsg;
186-
tcp_bpf_proto.sendpage = bpf_tcp_sendpage;
187-
tcp_bpf_proto.recvmsg = bpf_tcp_recvmsg;
188-
tcp_bpf_proto.stream_memory_read = bpf_tcp_stream_read;
220+
/* Build IPv6 sockmap whenever the address of tcpv6_prot changes */
221+
if (sk->sk_family == AF_INET6 &&
222+
unlikely(sk->sk_prot != smp_load_acquire(&saved_tcpv6_prot))) {
223+
spin_lock_bh(&tcpv6_prot_lock);
224+
if (likely(sk->sk_prot != saved_tcpv6_prot)) {
225+
build_protos(bpf_tcp_prots[SOCKMAP_IPV6], sk->sk_prot);
226+
smp_store_release(&saved_tcpv6_prot, sk->sk_prot);
227+
}
228+
spin_unlock_bh(&tcpv6_prot_lock);
189229
}
190-
191-
sk->sk_prot = &tcp_bpf_proto;
230+
update_sk_prot(sk, psock);
192231
rcu_read_unlock();
193232
return 0;
194233
}
@@ -1111,8 +1150,7 @@ static void bpf_tcp_msg_add(struct smap_psock *psock,
11111150

11121151
static int bpf_tcp_ulp_register(void)
11131152
{
1114-
tcp_bpf_proto = tcp_prot;
1115-
tcp_bpf_proto.close = bpf_tcp_close;
1153+
build_protos(bpf_tcp_prots[SOCKMAP_IPV4], &tcp_prot);
11161154
/* Once BPF TX ULP is registered it is never unregistered. It
11171155
* will be in the ULP list for the lifetime of the system. Doing
11181156
* duplicate registers is not a problem.

0 commit comments

Comments
 (0)