Skip to content

Commit e802515

Browse files
jsitnickiborkmann
authored andcommitted
tcp_bpf: Don't let child socket inherit parent protocol ops on copy
Prepare for cloning listening sockets that have their protocol callbacks overridden by sk_msg. Child sockets must not inherit parent callbacks that access state stored in sk_user_data owned by the parent. Restore the child socket protocol callbacks before it gets hashed and any of the callbacks can get invoked. Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: John Fastabend <john.fastabend@gmail.com> Link: https://lore.kernel.org/bpf/20200218171023.844439-4-jakub@cloudflare.com
1 parent f1ff5ce commit e802515

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

‎include/net/tcp.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,6 +2203,13 @@ int tcp_bpf_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
22032203
int nonblock, int flags, int *addr_len);
22042204
int __tcp_bpf_recvmsg(struct sock *sk, struct sk_psock *psock,
22052205
struct msghdr *msg, int len, int flags);
2206+
#ifdef CONFIG_NET_SOCK_MSG
2207+
void tcp_bpf_clone(const struct sock *sk, struct sock *newsk);
2208+
#else
2209+
static inline void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
2210+
{
2211+
}
2212+
#endif
22062213

22072214
/* Call BPF_SOCK_OPS program that returns an int. If the return value
22082215
* is < 0, then the BPF op failed (for example if the loaded BPF

‎net/ipv4/tcp_bpf.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,3 +693,17 @@ int tcp_bpf_init(struct sock *sk)
693693
rcu_read_unlock();
694694
return 0;
695695
}
696+
697+
/* If a child got cloned from a listening socket that had tcp_bpf
698+
* protocol callbacks installed, we need to restore the callbacks to
699+
* the default ones because the child does not inherit the psock state
700+
* that tcp_bpf callbacks expect.
701+
*/
702+
void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
703+
{
704+
int family = sk->sk_family == AF_INET6 ? TCP_BPF_IPV6 : TCP_BPF_IPV4;
705+
struct proto *prot = newsk->sk_prot;
706+
707+
if (prot == &tcp_bpf_prots[family][TCP_BPF_BASE])
708+
newsk->sk_prot = sk->sk_prot_creator;
709+
}

‎net/ipv4/tcp_minisocks.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,8 @@ struct sock *tcp_create_openreq_child(const struct sock *sk,
548548
newtp->fastopen_req = NULL;
549549
RCU_INIT_POINTER(newtp->fastopen_rsk, NULL);
550550

551+
tcp_bpf_clone(sk, newsk);
552+
551553
__TCP_INC_STATS(sock_net(sk), TCP_MIB_PASSIVEOPENS);
552554

553555
return newsk;

0 commit comments

Comments
 (0)