Skip to content

Commit e4ddc81

Browse files
DaanDeMeyerqmonnet
authored andcommitted
bpf: Implement cgroup sockaddr hooks for unix sockets
These hooks allows intercepting connect(), getsockname(), getpeername(), sendmsg() and recvmsg() for unix sockets. The unix socket hooks get write access to the address length because the address length is not fixed when dealing with unix sockets and needs to be modified when a unix socket address is modified by the hook. Because abstract socket unix addresses start with a NUL byte, we cannot recalculate the socket address in kernelspace after running the hook by calculating the length of the unix socket path using strlen(). These hooks can be used when users want to multiplex syscall to a single unix socket to multiple different processes behind the scenes by redirecting the connect() and other syscalls to process specific sockets. We do not implement support for intercepting bind() because when using bind() with unix sockets with a pathname address, this creates an inode in the filesystem which must be cleaned up. If we rewrite the address, the user might try to clean up the wrong file, leaking the socket in the filesystem where it is never cleaned up. Until we figure out a solution for this (and a use case for intercepting bind()), we opt to not allow rewriting the sockaddr in bind() calls. We also implement recvmsg() support for connected streams so that after a connect() that is modified by a sockaddr hook, any corresponding recmvsg() on the connected socket can also be modified to make the connected program think it is connected to the "intended" remote. Reviewed-by: Kuniyuki Iwashima <[email protected]> Signed-off-by: Daan De Meyer <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]>
1 parent 0a9ad5d commit e4ddc81

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

include/uapi/linux/bpf.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,11 @@ enum bpf_attach_type {
10471047
BPF_TCX_INGRESS,
10481048
BPF_TCX_EGRESS,
10491049
BPF_TRACE_UPROBE_MULTI,
1050+
BPF_CGROUP_UNIX_CONNECT,
1051+
BPF_CGROUP_UNIX_SENDMSG,
1052+
BPF_CGROUP_UNIX_RECVMSG,
1053+
BPF_CGROUP_UNIX_GETPEERNAME,
1054+
BPF_CGROUP_UNIX_GETSOCKNAME,
10501055
__MAX_BPF_ATTACH_TYPE
10511056
};
10521057

@@ -2704,8 +2709,8 @@ union bpf_attr {
27042709
* *bpf_socket* should be one of the following:
27052710
*
27062711
* * **struct bpf_sock_ops** for **BPF_PROG_TYPE_SOCK_OPS**.
2707-
* * **struct bpf_sock_addr** for **BPF_CGROUP_INET4_CONNECT**
2708-
* and **BPF_CGROUP_INET6_CONNECT**.
2712+
* * **struct bpf_sock_addr** for **BPF_CGROUP_INET4_CONNECT**,
2713+
* **BPF_CGROUP_INET6_CONNECT** and **BPF_CGROUP_UNIX_CONNECT**.
27092714
*
27102715
* This helper actually implements a subset of **setsockopt()**.
27112716
* It supports the following *level*\ s:
@@ -2943,8 +2948,8 @@ union bpf_attr {
29432948
* *bpf_socket* should be one of the following:
29442949
*
29452950
* * **struct bpf_sock_ops** for **BPF_PROG_TYPE_SOCK_OPS**.
2946-
* * **struct bpf_sock_addr** for **BPF_CGROUP_INET4_CONNECT**
2947-
* and **BPF_CGROUP_INET6_CONNECT**.
2951+
* * **struct bpf_sock_addr** for **BPF_CGROUP_INET4_CONNECT**,
2952+
* **BPF_CGROUP_INET6_CONNECT** and **BPF_CGROUP_UNIX_CONNECT**.
29482953
*
29492954
* This helper actually implements a subset of **getsockopt()**.
29502955
* It supports the same set of *optname*\ s that is supported by

0 commit comments

Comments
 (0)