Skip to content

Commit 15d4d15

Browse files
menglongdongNobody
authored andcommitted
net: bpf: handle return value of BPF_CGROUP_RUN_PROG_INET4_POST_BIND()
The return value of BPF_CGROUP_RUN_PROG_INET4_POST_BIND() in __inet_bind() is not handled properly. While the return value is non-zero, it will set inet_saddr and inet_rcv_saddr to 0 and exit: err = BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk); if (err) { inet->inet_saddr = inet->inet_rcv_saddr = 0; goto out_release_sock; } Let's take UDP for example and see what will happen. For UDP socket, it will be added to 'udp_prot.h.udp_table->hash' and 'udp_prot.h.udp_table->hash2' after the sk->sk_prot->get_port() called success. If 'inet->inet_rcv_saddr' is specified here, then 'sk' will be in the 'hslot2' of 'hash2' that it don't belong to (because inet_saddr is changed to 0), and UDP packet received will not be passed to this sock. If 'inet->inet_rcv_saddr' is not specified here, the sock will work fine, as it can receive packet properly, which is wired, as the 'bind()' is already failed. I'm not sure what should do here, maybe we should unhash the sock for UDP? Therefor, user can try to bind another port? Signed-off-by: Menglong Dong <[email protected]>
1 parent 80912cd commit 15d4d15

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

net/ipv4/af_inet.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,14 @@ int __inet_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
530530
if (!(flags & BIND_FROM_BPF)) {
531531
err = BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk);
532532
if (err) {
533+
if (sk->sk_prot == &udp_prot)
534+
sk->sk_prot->unhash(sk);
535+
else if (sk->sk_prot == &tcp_prot)
536+
inet_put_port(sk);
537+
533538
inet->inet_saddr = inet->inet_rcv_saddr = 0;
539+
err = -EPERM;
540+
534541
goto out_release_sock;
535542
}
536543
}

0 commit comments

Comments
 (0)