Skip to content

Commit eff14fc

Browse files
author
Alexei Starovoitov
committed
Merge branch 'net: bpf: handle return value of post_bind{4,6} and add selftests for it'
Menglong Dong says: ==================== From: Menglong Dong <[email protected]> The return value of BPF_CGROUP_RUN_PROG_INET{4,6}_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. To undo the get_port() operation, introduce the 'put_port' field for 'struct proto'. For TCP proto, it is inet_put_port(); For UDP proto, it is udp_lib_unhash(); For icmp proto, it is ping_unhash(). Therefore, after sys_bind() fail caused by BPF_CGROUP_RUN_PROG_INET4_POST_BIND(), it will be unbinded, which means that it can try to be binded to another port. The second patch use C99 initializers in test_sock.c The third patch is the selftests for this modification. Changes since v4: - use C99 initializers in test_sock.c before adding the test case Changes since v3: - add the third patch which use C99 initializers in test_sock.c Changes since v2: - NULL check for sk->sk_prot->put_port Changes since v1: - introduce 'put_port' field for 'struct proto' - add selftests for it ==================== Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 44bab87 + f734248 commit eff14fc

File tree

10 files changed

+233
-148
lines changed

10 files changed

+233
-148
lines changed

include/net/sock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,7 @@ struct proto {
12091209
void (*unhash)(struct sock *sk);
12101210
void (*rehash)(struct sock *sk);
12111211
int (*get_port)(struct sock *sk, unsigned short snum);
1212+
void (*put_port)(struct sock *sk);
12121213
#ifdef CONFIG_BPF_SYSCALL
12131214
int (*psock_update_sk_prot)(struct sock *sk,
12141215
struct sk_psock *psock,

net/ipv4/af_inet.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,8 @@ int __inet_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
531531
err = BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk);
532532
if (err) {
533533
inet->inet_saddr = inet->inet_rcv_saddr = 0;
534+
if (sk->sk_prot->put_port)
535+
sk->sk_prot->put_port(sk);
534536
goto out_release_sock;
535537
}
536538
}

net/ipv4/ping.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,7 @@ struct proto ping_prot = {
994994
.hash = ping_hash,
995995
.unhash = ping_unhash,
996996
.get_port = ping_get_port,
997+
.put_port = ping_unhash,
997998
.obj_size = sizeof(struct inet_sock),
998999
};
9991000
EXPORT_SYMBOL(ping_prot);

net/ipv4/tcp_ipv4.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3076,6 +3076,7 @@ struct proto tcp_prot = {
30763076
.hash = inet_hash,
30773077
.unhash = inet_unhash,
30783078
.get_port = inet_csk_get_port,
3079+
.put_port = inet_put_port,
30793080
#ifdef CONFIG_BPF_SYSCALL
30803081
.psock_update_sk_prot = tcp_bpf_update_proto,
30813082
#endif

net/ipv4/udp.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2927,6 +2927,7 @@ struct proto udp_prot = {
29272927
.unhash = udp_lib_unhash,
29282928
.rehash = udp_v4_rehash,
29292929
.get_port = udp_v4_get_port,
2930+
.put_port = udp_lib_unhash,
29302931
#ifdef CONFIG_BPF_SYSCALL
29312932
.psock_update_sk_prot = udp_bpf_update_proto,
29322933
#endif

net/ipv6/af_inet6.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,8 @@ static int __inet6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
413413
if (err) {
414414
sk->sk_ipv6only = saved_ipv6only;
415415
inet_reset_saddr(sk);
416+
if (sk->sk_prot->put_port)
417+
sk->sk_prot->put_port(sk);
416418
goto out;
417419
}
418420
}

net/ipv6/ping.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ struct proto pingv6_prot = {
177177
.hash = ping_hash,
178178
.unhash = ping_unhash,
179179
.get_port = ping_get_port,
180+
.put_port = ping_unhash,
180181
.obj_size = sizeof(struct raw6_sock),
181182
};
182183
EXPORT_SYMBOL_GPL(pingv6_prot);

net/ipv6/tcp_ipv6.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2181,6 +2181,7 @@ struct proto tcpv6_prot = {
21812181
.hash = inet6_hash,
21822182
.unhash = inet_unhash,
21832183
.get_port = inet_csk_get_port,
2184+
.put_port = inet_put_port,
21842185
#ifdef CONFIG_BPF_SYSCALL
21852186
.psock_update_sk_prot = tcp_bpf_update_proto,
21862187
#endif

net/ipv6/udp.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,6 +1732,7 @@ struct proto udpv6_prot = {
17321732
.unhash = udp_lib_unhash,
17331733
.rehash = udp_v6_rehash,
17341734
.get_port = udp_v6_get_port,
1735+
.put_port = udp_lib_unhash,
17351736
#ifdef CONFIG_BPF_SYSCALL
17361737
.psock_update_sk_prot = udp_bpf_update_proto,
17371738
#endif

0 commit comments

Comments
 (0)