Skip to content

Commit f583674

Browse files
fomichevborkmann
authored andcommitted
bpf: Add BPF_CGROUP_INET_SOCK_RELEASE hook
Sometimes it's handy to know when the socket gets freed. In particular, we'd like to try to use a smarter allocation of ports for bpf_bind and explore the possibility of limiting the number of SOCK_DGRAM sockets the process can have. Implement BPF_CGROUP_INET_SOCK_RELEASE hook that triggers on inet socket release. It triggers only for userspace sockets (not in-kernel ones) and therefore has the same semantics as the existing BPF_CGROUP_INET_SOCK_CREATE. Signed-off-by: Stanislav Fomichev <sdf@google.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Andrii Nakryiko <andriin@fb.com> Link: https://lore.kernel.org/bpf/20200706230128.4073544-2-sdf@google.com
1 parent 4e48978 commit f583674

5 files changed

Lines changed: 12 additions & 0 deletions

File tree

‎include/linux/bpf-cgroup.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ int bpf_percpu_cgroup_storage_update(struct bpf_map *map, void *key,
210210
#define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) \
211211
BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET_SOCK_CREATE)
212212

213+
#define BPF_CGROUP_RUN_PROG_INET_SOCK_RELEASE(sk) \
214+
BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET_SOCK_RELEASE)
215+
213216
#define BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk) \
214217
BPF_CGROUP_RUN_SK_PROG(sk, BPF_CGROUP_INET4_POST_BIND)
215218

@@ -401,6 +404,7 @@ static inline int bpf_percpu_cgroup_storage_update(struct bpf_map *map,
401404
#define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk,skb) ({ 0; })
402405
#define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk,skb) ({ 0; })
403406
#define BPF_CGROUP_RUN_PROG_INET_SOCK(sk) ({ 0; })
407+
#define BPF_CGROUP_RUN_PROG_INET_SOCK_RELEASE(sk) ({ 0; })
404408
#define BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr) ({ 0; })
405409
#define BPF_CGROUP_RUN_PROG_INET6_BIND(sk, uaddr) ({ 0; })
406410
#define BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk) ({ 0; })

‎include/uapi/linux/bpf.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ enum bpf_attach_type {
226226
BPF_CGROUP_INET4_GETSOCKNAME,
227227
BPF_CGROUP_INET6_GETSOCKNAME,
228228
BPF_XDP_DEVMAP,
229+
BPF_CGROUP_INET_SOCK_RELEASE,
229230
__MAX_BPF_ATTACH_TYPE
230231
};
231232

‎kernel/bpf/syscall.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,6 +1981,7 @@ bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
19811981
case BPF_PROG_TYPE_CGROUP_SOCK:
19821982
switch (expected_attach_type) {
19831983
case BPF_CGROUP_INET_SOCK_CREATE:
1984+
case BPF_CGROUP_INET_SOCK_RELEASE:
19841985
case BPF_CGROUP_INET4_POST_BIND:
19851986
case BPF_CGROUP_INET6_POST_BIND:
19861987
return 0;
@@ -2779,6 +2780,7 @@ attach_type_to_prog_type(enum bpf_attach_type attach_type)
27792780
return BPF_PROG_TYPE_CGROUP_SKB;
27802781
break;
27812782
case BPF_CGROUP_INET_SOCK_CREATE:
2783+
case BPF_CGROUP_INET_SOCK_RELEASE:
27822784
case BPF_CGROUP_INET4_POST_BIND:
27832785
case BPF_CGROUP_INET6_POST_BIND:
27842786
return BPF_PROG_TYPE_CGROUP_SOCK;
@@ -2929,6 +2931,7 @@ static int bpf_prog_query(const union bpf_attr *attr,
29292931
case BPF_CGROUP_INET_INGRESS:
29302932
case BPF_CGROUP_INET_EGRESS:
29312933
case BPF_CGROUP_INET_SOCK_CREATE:
2934+
case BPF_CGROUP_INET_SOCK_RELEASE:
29322935
case BPF_CGROUP_INET4_BIND:
29332936
case BPF_CGROUP_INET6_BIND:
29342937
case BPF_CGROUP_INET4_POST_BIND:

‎net/core/filter.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6890,6 +6890,7 @@ static bool __sock_filter_check_attach_type(int off,
68906890
case offsetof(struct bpf_sock, priority):
68916891
switch (attach_type) {
68926892
case BPF_CGROUP_INET_SOCK_CREATE:
6893+
case BPF_CGROUP_INET_SOCK_RELEASE:
68936894
goto full_access;
68946895
default:
68956896
return false;

‎net/ipv4/af_inet.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,9 @@ int inet_release(struct socket *sock)
411411
if (sk) {
412412
long timeout;
413413

414+
if (!sk->sk_kern_sock)
415+
BPF_CGROUP_RUN_PROG_INET_SOCK_RELEASE(sk);
416+
414417
/* Applications forget to leave groups before exiting */
415418
ip_mc_drop_socket(sk);
416419

0 commit comments

Comments
 (0)