selftests/bpf: Add sockaddr tests for kernel networking
Subsystems (6)
cover selftests/bpf: Add sockaddr tests for kernel networking
In a follow up to these patches,
- commit 0bdf399342c5("net: Avoid address overwrite in kernel_connect")
- commit 86a7e0b69bd5("net: prevent rewrite of msg_name in sock_sendmsg()")
- commit c889a99a21bf("net: prevent address rewrite in kernel_bind()")
- commit 01b2885d9415("net: Save and restore msg_namelen in sock_sendmsg")
this patch series introduces BPF selftests that test the interaction between BPF sockaddr hooks and socket operations in kernel space. It focuses on regression test coverage to ensure that these operations do not overwrite their address parameter and also provides some sanity checks around kernel_getpeername() and kernel_getsockname().
It introduces two new components: a kernel module called sock_addr_testmod and a new test program called sock_addr_kern which is loosely modeled after and adapted from the old-style bpf/test_sock_addr.c selftest. When loaded, the kernel module will perform some socket operation in kernel space. The kernel module accepts five parameters controlling which socket operation will be performed and its inputs:
MODULE_PARM_DESC(ip, "IPv4/IPv6/Unix address to use for socket operation");MODULE_PARM_DESC(port, "Port number to use for socket operation");MODULE_PARM_DESC(af, "Address family (AF_INET, AF_INET6, or AF_UNIX)");MODULE_PARM_DESC(type, "Socket type (SOCK_STREAM or SOCK_DGRAM)");MODULE_PARM_DESC(op, "Socket operation (BIND=0, CONNECT=1, SENDMSG=2)");
On module init, the socket operation is performed and results of are exposed through debugfs.
- /sys/kernel/debug/sock_addr_testmod/success
Indicates success or failure of the operation.
- /sys/kernel/debug/sock_addr_testmod/addr
The value of the address parameter after the operation.
- /sys/kernel/debug/sock_addr_testmod/sock_name
The value of kernel_getsockname() after the socket operation (if relevant).
- /sys/kernel/debug/sock_addr_testmod/peer_name
The value of kernel_getpeername(() after the socket operation (if relevant).
The sock_addr_kern test program loads and unloads the kernel module to drive kernel socket operations, reads the results from debugfs, makes sure that the operation did not overwrite the address, and any result from kernel_getpeername() or kernel_getsockname() were as expected.
== Patches ==
- Patch 1 introduces sock_addr_testmod and functions necessary for the test program to load and unload the module.
- Patches 2-6 transform existing test helpers and introduce new test helpers to enable the sock_addr_kern test program.
- Patch 7 implements the sock_addr_kern test program.
- Patch 8 fixes the sock_addr bind test program to work for big endian architectures such as s390x.
Jordan Rife (8):
selftests/bpf: Introduce sock_addr_testmod selftests/bpf: Add module load helpers
selftests/bpf: Factor out cmp_addr
selftests/bpf: Add recv_msg_from_client to network helpers selftests/bpf: Factor out load_path and defines from test_sock_addr selftests/bpf: Add setup/cleanup subcommands selftests/bpf: Add sock_addr_kern prog_test selftests/bpf: Fix bind program for big endian systems
tools/testing/selftests/bpf/Makefile | 46 +- tools/testing/selftests/bpf/network_helpers.c | 65 ++ tools/testing/selftests/bpf/network_helpers.h | 5 + .../selftests/bpf/prog_tests/sock_addr.c | 34 - .../selftests/bpf/prog_tests/sock_addr_kern.c | 631 ++++++++++++++++++ .../testing/selftests/bpf/progs/bind4_prog.c | 18 +- .../testing/selftests/bpf/progs/bind6_prog.c | 18 +- tools/testing/selftests/bpf/progs/bind_prog.h | 19 + .../testing/selftests/bpf/sock_addr_helpers.c | 46 ++ .../testing/selftests/bpf/sock_addr_helpers.h | 44 ++ .../bpf/sock_addr_testmod/.gitignore | 6 + .../selftests/bpf/sock_addr_testmod/Makefile | 20 + .../bpf/sock_addr_testmod/sock_addr_testmod.c | 256 +++++++ tools/testing/selftests/bpf/test_sock_addr.c | 76 +-- tools/testing/selftests/bpf/test_sock_addr.sh | 10 +- tools/testing/selftests/bpf/testing_helpers.c | 44 +- tools/testing/selftests/bpf/testing_helpers.h | 2 + 17 files changed, 1196 insertions(+), 144 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/sock_addr_kern.c create mode 100644 tools/testing/selftests/bpf/progs/bind_prog.h create mode 100644 tools/testing/selftests/bpf/sock_addr_helpers.c create mode 100644 tools/testing/selftests/bpf/sock_addr_helpers.h create mode 100644 tools/testing/selftests/bpf/sock_addr_testmod/.gitignore create mode 100644 tools/testing/selftests/bpf/sock_addr_testmod/Makefile create mode 100644 tools/testing/selftests/bpf/sock_addr_testmod/sock_addr_testmod.c
Signature · 1 line
2.44.0.478.gd926399ef9-goog
PATCH 1/8 selftests/bpf: Introduce sock_addr_testmod
sock_addr_testmod provides a mechanism for the sock_addr_kern prog_test to drive socket operations in kernel space. On init, one of the following socket operations is performed based on the module parameters:kernel_bind(), kernel_connect(), or sock_sendmsg()/kernel_sendmsg() and results are exposed through debugfs.
--- tools/testing/selftests/bpf/Makefile | 11 +- .../bpf/sock_addr_testmod/.gitignore | 6 + .../selftests/bpf/sock_addr_testmod/Makefile | 20 ++ .../bpf/sock_addr_testmod/sock_addr_testmod.c | 256 ++++++++++++++++++ 4 files changed, 292 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/sock_addr_testmod/.gitignore create mode 100644 tools/testing/selftests/bpf/sock_addr_testmod/Makefile create mode 100644 tools/testing/selftests/bpf/sock_addr_testmod/sock_addr_testmod.c
tools/testing/selftests/bpf/Makefile +10 -1
@@ -132,7 +132,7 @@ TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \ flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \ test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \ xskxceiver xdp_redirect_multi xdp_synproxy veristat xdp_hw_metadata \ - xdp_features bpf_test_no_cfi.ko + xdp_features bpf_test_no_cfi.ko sock_addr_testmod.ko TEST_GEN_FILES += liburandom_read.so urandom_read sign-file uprobe_multi
@@ -160,6 +160,7 @@ override define CLEAN $(Q)$(RM) -r $(TEST_GEN_FILES) $(Q)$(RM) -r $(EXTRA_CLEAN) $(Q)$(MAKE) -C bpf_testmod clean + $(Q)$(MAKE) -C sock_addr_testmod clean $(Q)$(MAKE) docs-clean endef
@@ -260,6 +261,12 @@ $(OUTPUT)/bpf_test_no_cfi.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard bpf_te $(Q)$(MAKE) $(submake_extras) RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) -C bpf_test_no_cfi $(Q)cp bpf_test_no_cfi/bpf_test_no_cfi.ko $@ +$(OUTPUT)/sock_addr_testmod.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard sock_addr_testmod/Makefile sock_addr_testmod/*.[ch]) + $(call msg,MOD,,$@) + $(Q)$(RM) sock_addr_testmod/sock_addr_testmod.ko # force re-compilation + $(Q)$(MAKE) $(submake_extras) RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) -C sock_addr_testmod + $(Q)cp sock_addr_testmod/sock_addr_testmod.ko $@ + DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool ifneq ($(CROSS_COMPILE),) CROSS_BPFTOOL := $(SCRATCH_DIR)/sbin/bpftool
@@ -638,6 +645,7 @@ TRUNNER_EXTRA_SOURCES := test_progs.c \ ip_check_defrag_frags.h TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko \ $(OUTPUT)/bpf_test_no_cfi.ko \ + $(OUTPUT)/sock_addr_testmod.ko \ $(OUTPUT)/liburandom_read.so \ $(OUTPUT)/xdp_synproxy \ $(OUTPUT)/sign-file \
@@ -767,6 +775,7 @@ EXTRA_CLEAN := $(SCRATCH_DIR) $(HOST_SCRATCH_DIR) \ $(addprefix $(OUTPUT)/,*.o *.skel.h *.lskel.h *.subskel.h \ no_alu32 cpuv4 bpf_gcc bpf_testmod.ko \ bpf_test_no_cfi.ko \ + sock_addr_testmod.ko \ liburandom_read.so) .PHONY: docs docs-clean
tools/testing/selftests/bpf/sock_addr_testmod/.gitignore new file +6 -0
@@ -0,0 +1,6 @@ +*.mod +*.mod.c +*.o +.ko +/Module.symvers +/modules.order
tools/testing/selftests/bpf/sock_addr_testmod/Makefile new file +20 -0
@@ -0,0 +1,20 @@ +SOCK_ADDR_TEST_MOD_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST))))) +KDIR ?= $(abspath $(SOCK_ADDR_TEST_MOD_DIR)/../../../../..) + +ifeq ($(V),1) +Q = +else +Q = @ +endif + +MODULES = sock_addr_testmod.ko + +obj-m += sock_addr_testmod.o +CFLAGS_sock_addr_testmod.o = -I$(src) + +all: + +$(Q)make -C $(KDIR) M=$(SOCK_ADDR_TEST_MOD_DIR) modules + +clean: + +$(Q)make -C $(KDIR) M=$(SOCK_ADDR_TEST_MOD_DIR) clean +
tools/testing/selftests/bpf/sock_addr_testmod/sock_addr_testmod.c new file +256 -0
256 lines not on this page · raw message
Signature · 1 line
2.44.0.478.gd926399ef9-goog
Re: [PATCH v1 bpf-next 1/8] selftests/bpf: Introduce sock_addr_testmod
Quoted 23 lines · Jordan Rife
sock_addr_testmodprovides a mechanism for thesock_addr_kernprog_testto drive socket operations in kernel space. On init, one of the following socket operations is performed based on the module parameters:kernel_bind(),kernel_connect(), or sock_sendmsg()/kernel_sendmsg() and results are exposed through debugfs.Signed-off-by Jordan Rife <[email protected]>--- tools/testing/selftests/bpf/Makefile | 11 +- .../bpf/sock_addr_testmod/.gitignore | 6 + .../selftests/bpf/sock_addr_testmod/Makefile | 20 ++ .../bpf/sock_addr_testmod/sock_addr_testmod.c | 256 ++++++++++++++++++ 4 files changed, 292 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/sock_addr_testmod/.gitignore create mode 100644 tools/testing/selftests/bpf/sock_addr_testmod/Makefile create mode 100644 tools/testing/selftests/bpf/sock_addr_testmod/sock_addr_testmod.ctools/testing/selftests/bpf/Makefile +0
-0
tools/testing/selftests/bpf/Makefile TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \ · line 136
Quoted 6 lines
tools/testing/selftests/bpf/Makefile +1
-1@@ -132,7 +132,7 @@ TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \ flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \ test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \ xskxceiver xdp_redirect_multi xdp_synproxy veristat xdp_hw_metadata \ - xdp_features bpf_test_no_cfi.ko + xdp_features bpf_test_no_cfi.ko sock_addr_testmod.ko
Do we need yet another kernel module? Can this be done as part of existing bpf_testmod module?
Quoted 10 lines
tools/testing/selftests/bpf/Makefile +1
-0TEST_GEN_FILES += liburandom_read.so urandom_read sign-file uprobe_multi@@ -160,6 +160,7 @@ override define CLEAN $(Q)$(RM) -r $(TEST_GEN_FILES) $(Q)$(RM) -r $(EXTRA_CLEAN) $(Q)$(MAKE) -C bpf_testmod clean + $(Q)$(MAKE) -C sock_addr_testmod clean $(Q)$(MAKE) docs-clean endef
[...]
Martin KaFai Lau
reviewerRe: [PATCH v1 bpf-next 1/8] selftests/bpf: Introduce sock_addr_testmod
In reply to Jordan RifeQuoted 1 line · Jordan Rife
+static int
do_sock_op(int op, struct sockaddr *addr, int addrlen)
This function can be made as a new kfunc in bpf_testmod.c. The sock_create_kern() could be moved to here also. Take a look at the register_btf_kfunc_id_set() usage in bpf_testmod.c and how those registered kfunc(s) can be called by the bpf prog in progs/*.
If the do_kernel_{bind,connect,sendmsg} and the sock_create_kern need a sleepable context, it will need to mark the kfunc KF_SLEEPABLE. The kfunc can be registered to the BPF_PROG_TYPE_SYSCALL sleepable prog type. There are some examples in progs/kfunc_call_test.c and how the "syscall" bpf prog can be run by bpf_prog_test_run_opts().
The result (e.g. ensuring the addr and addrlen have not been changed) can be checked in the bpf prog itself. Then the new sock_addr_testmod is not needed.
Quoted 13 lines
tools/testing/selftests/bpf/sock_addr_testmod/sock_addr_testmod.c new file +13
-0+{ +switch (op) { +case BIND : +return do_kernel_bind (addr, addrlen); +case CONNECT : +return do_kernel_connect (addr, addrlen); +case SENDMSG : +return do_kernel_sendmsg (addr, addrlen); +default : +return -EINVAL ; + } +} +
Re: [PATCH v1 bpf-next 1/8] selftests/bpf: Introduce sock_addr_testmod
Martin and Andrii,
Quoted 4 lines
This function can be made as a new kfunc in
bpf_testmod.c. Thesock_create_kern()could be moved to here also. Take a look at theregister_btf_kfunc_id_set()usage inbpf_testmod.cand how those registered kfunc(s) can be called by the bpf prog in progs/*.
Thanks for the feedback. I will explore this approach and see if I can get rid of the additional test module.
-Jordan
PATCH 2/8 selftests/bpf: Add module load helpers
In reply to Jordan RifeThis patch introduces a helpers used by the sock_addr_kern test program that allow it to load and unload sock_addr_testmod.
--- tools/testing/selftests/bpf/testing_helpers.c | 44 ++++++++++++++----- tools/testing/selftests/bpf/testing_helpers.h | 2 + 2 files changed, 34 insertions(+), 12 deletions(-)
tools/testing/selftests/bpf/testing_helpers.c +32 -12
@@ -366,48 +366,68 @@ int delete_module(const char *name, int flags)return syscall (__NR_delete_module, name, flags); } -int unload_bpf_testmod (bool verbose) +static int unload_mod (char name[],bool verbose) {if (kern_sync_rcu ())fprintf (stdout,"Failed to trigger kernel-side RCU sync!\n" ); -if (delete_module ("bpf_testmod" ,0 )) { +if (delete_module (name,0 )) {if (errno ==ENOENT ) {if (verbose) -fprintf (stdout,"bpf_testmod.ko is already unloaded.\n" ); +fprintf (stdout,"%s is already unloaded.\n" , name);return -1 ; } -fprintf (stdout,"Failed to unload bpf_testmod.ko from kernel: %d\n" , -errno); +fprintf (stdout,"Failed to unload %so from kernel: %d\n" , name, -errno);return -1 ; }if (verbose) -fprintf (stdout,"Successfully unloaded bpf_testmod.ko.\n" ); +fprintf (stdout,"Successfully unloaded %s.\n" , name);return 0 ; } -int load_bpf_testmod (bool verbose) +int unload_bpf_testmod (bool verbose) +{ +return unload_mod ("bpf_testmod" , verbose); +} + +int unload_bpf_sock_addr_testmod (bool verbose) +{ +return unload_mod ("sock_addr_testmod" , verbose); +} + +static int load_mod (const char *name,const char *param_values,bool verbose) {int fd;if (verbose) -fprintf (stdout,"Loading bpf_testmod.ko...\n" ); +fprintf (stdout,"Loading %s...\n" , name); - fd =open ("bpf_testmod.ko" ,O_RDONLY ); + fd =open (name,O_RDONLY );if (fd <0 ) { -fprintf (stdout,"Can't find bpf_testmod.ko kernel module: %d\n" , -errno); +fprintf (stdout,"Can't find %s kernel module: %d\n" , name, -errno);return -ENOENT ; } -if (finit_module (fd,"" ,0 )) { -fprintf (stdout,"Failed to load bpf_testmod.ko into the kernel: %d\n" , -errno); +if (finit_module (fd, param_values,0 )) { +fprintf (stdout,"Failed to load %s into the kernel: %d\n" , name, -errno);close (fd);return -EINVAL ; }close (fd);if (verbose) -fprintf (stdout,"Successfully loaded bpf_testmod.ko.\n" ); +fprintf (stdout,"Successfully loaded %s.\n" , name);return 0 ; } +int load_bpf_testmod (bool verbose) +{ +return load_mod ("bpf_testmod.ko" ,"" , verbose); +} + +int load_bpf_sock_addr_testmod (const char *param_values,bool verbose) +{ +return load_mod ("sock_addr_testmod.ko" , param_values, verbose); +} +/* * Trigger synchronize_rcu() in kernel. */
tools/testing/selftests/bpf/testing_helpers.h +2 -0
@@ -34,7 +34,9 @@ int parse_test_list_file(const char *path,__u64 read_perf_max_sample_freq (void );int load_bpf_testmod (bool verbose); +int load_bpf_sock_addr_testmod (const char *param_values,bool verbose);int unload_bpf_testmod (bool verbose); +int unload_bpf_sock_addr_testmod (bool verbose);int kern_sync_rcu (void );int finit_module (int fd,const char *param_values,int flags);int delete_module (const char *name,int flags);
Signature · 1 line
2.44.0.478.gd926399ef9-goog
PATCH 3/8 selftests/bpf: Factor out cmp_addr
In reply to Jordan RifeFactor out cmp_addr into network_helpers.c to so that it can be used by the sock_addr_kern test program.
--- tools/testing/selftests/bpf/network_helpers.c | 34 +++++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 4 +++ .../selftests/bpf/prog_tests/sock_addr.c | 34 ------------------- 3 files changed, 38 insertions(+), 34 deletions(-)
tools/testing/selftests/bpf/network_helpers.c +34 -0
@@ -497,3 +497,37 @@ int get_socket_local_port(int sock_fd)return -1 ; } + +int cmp_addr (const struct sockaddr_storage *addr1,socklen_t addr1_len, +const struct sockaddr_storage *addr2,socklen_t addr2_len, +bool cmp_port) +{ +const struct sockaddr_in *four1, *four2; +const struct sockaddr_in6 *six1, *six2; +const struct sockaddr_un *un1, *un2; + +if (addr1->ss_family != addr2->ss_family) +return -1 ; + +if (addr1_len != addr2_len) +return -1 ; + +if (addr1->ss_family ==AF_INET ) { + four1 = (const struct sockaddr_in *)addr1; + four2 = (const struct sockaddr_in *)addr2; +return !((four1->sin_port == four2->sin_port || !cmp_port) && + four1->sin_addr.s_addr == four2->sin_addr.s_addr); + }else if (addr1->ss_family ==AF_INET6 ) { + six1 = (const struct sockaddr_in6 *)addr1; + six2 = (const struct sockaddr_in6 *)addr2; +return !((six1->sin6_port == six2->sin6_port || !cmp_port) && + !memcmp (&six1->sin6_addr, &six2->sin6_addr, +sizeof (struct in6_addr ))); + }else if (addr1->ss_family ==AF_UNIX ) { + un1 = (const struct sockaddr_un *)addr1; + un2 = (const struct sockaddr_un *)addr2; +return memcmp (un1, un2, addr1_len); + } + +return -1 ; +}
tools/testing/selftests/bpf/network_helpers.h +4 -0
@@ -61,6 +61,10 @@ int make_sockaddr(int family, const char *addr_str, __u16 port, struct sockaddr_storage *addr, socklen_t *len);char *ping_command (int family);int get_socket_local_port (int sock_fd); +int cmp_addr (const struct sockaddr_storage *addr1,socklen_t addr1_len, +const struct sockaddr_storage *addr2,socklen_t addr2_len, +bool cmp_port); +struct nstoken ;/**
tools/testing/selftests/bpf/prog_tests/sock_addr.c +0 -34
@@ -246,40 +246,6 @@ static struct sock_addr_test tests[] = {typedef int (*info_fn )(int ,struct sockaddr *,socklen_t *); -static int cmp_addr (const struct sockaddr_storage *addr1,socklen_t addr1_len, -const struct sockaddr_storage *addr2,socklen_t addr2_len, -bool cmp_port) -{ -const struct sockaddr_in *four1, *four2; -const struct sockaddr_in6 *six1, *six2; -const struct sockaddr_un *un1, *un2; - -if (addr1->ss_family != addr2->ss_family) -return -1 ; - -if (addr1_len != addr2_len) -return -1 ; - -if (addr1->ss_family ==AF_INET ) { - four1 = (const struct sockaddr_in *)addr1; - four2 = (const struct sockaddr_in *)addr2; -return !((four1->sin_port == four2->sin_port || !cmp_port) && - four1->sin_addr.s_addr == four2->sin_addr.s_addr); - }else if (addr1->ss_family ==AF_INET6 ) { - six1 = (const struct sockaddr_in6 *)addr1; - six2 = (const struct sockaddr_in6 *)addr2; -return !((six1->sin6_port == six2->sin6_port || !cmp_port) && - !memcmp (&six1->sin6_addr, &six2->sin6_addr, -sizeof (struct in6_addr ))); - }else if (addr1->ss_family ==AF_UNIX ) { - un1 = (const struct sockaddr_un *)addr1; - un2 = (const struct sockaddr_un *)addr2; -return memcmp (un1, un2, addr1_len); - } - -return -1 ; -} -static int cmp_sock_addr (info_fn fn,int sock1,const struct sockaddr_storage *addr2,socklen_t addr2_len,bool cmp_port)
Signature · 1 line
2.44.0.478.gd926399ef9-goog
PATCH 4/8 selftests/bpf: Add recv_msg_from_client to network helpers
In reply to Jordan RifeCopy recv_msg_from_client helper from test_sock_addr self test in order to use it in the sock_addr_kern test program.
--- tools/testing/selftests/bpf/network_helpers.c | 31 +++++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 1 + 2 files changed, 32 insertions(+)
tools/testing/selftests/bpf/network_helpers.c +31 -0
@@ -498,6 +498,37 @@ int get_socket_local_port(int sock_fd)return -1 ; } +int recvmsg_from_client (int sockfd,struct sockaddr_storage *src_addr) +{ +struct timeval tv; +struct msghdr hdr; +struct iovec iov; +char data[64 ]; +fd_set rfds; + +FD_ZERO (&rfds); +FD_SET (sockfd, &rfds); + + tv.tv_sec =2 ; + tv.tv_usec =0 ; + +if (select (sockfd +1 , &rfds,NULL ,NULL , &tv) <=0 || + !FD_ISSET (sockfd, &rfds)) +return -1 ; + +memset (&iov,0 ,sizeof (iov)); + iov.iov_base = data; + iov.iov_len =sizeof (data); + +memset (&hdr,0 ,sizeof (hdr)); + hdr.msg_name = src_addr; + hdr.msg_namelen =sizeof (struct sockaddr_storage ); + hdr.msg_iov = &iov; + hdr.msg_iovlen =1 ; + +return recvmsg (sockfd, &hdr,0 ); +} +int cmp_addr (const struct sockaddr_storage *addr1,socklen_t addr1_len,const struct sockaddr_storage *addr2,socklen_t addr2_len,bool cmp_port)
tools/testing/selftests/bpf/network_helpers.h +1 -0
@@ -61,6 +61,7 @@ int make_sockaddr(int family, const char *addr_str, __u16 port, struct sockaddr_storage *addr, socklen_t *len);char *ping_command (int family);int get_socket_local_port (int sock_fd); +int recvmsg_from_client (int sockfd,struct sockaddr_storage *src_addr);int cmp_addr (const struct sockaddr_storage *addr1,socklen_t addr1_len,const struct sockaddr_storage *addr2,socklen_t addr2_len,bool cmp_port);
Signature · 1 line
2.44.0.478.gd926399ef9-goog
Martin KaFai Lau
reviewerRe: [PATCH v1 bpf-next 4/8] selftests/bpf: Add recv_msg_from_client to network helpers
Quoted 5 lines · Jordan Rife
tools/testing/selftests/bpf/network_helpers.c +5
-0+int recvmsg_from_client (int sockfd,struct sockaddr_storage *src_addr) +{ +struct timeval tv; +struct msghdr hdr; +struct iovec iov;
tools/testing/selftests/bpf/network_helpers.c int get_socket_local_port(int sock_fd) · line 515
Quoted 10 lines
tools/testing/selftests/bpf/network_helpers.c +10
-0+char data[64 ]; +fd_set rfds; + +FD_ZERO (&rfds); +FD_SET (sockfd, &rfds); + + tv.tv_sec =2 ; + tv.tv_usec =0 ; + + if (select(sockfd + 1, &rfds, NULL, NULL, &tv) <= 0 ||
The socket fd created by the helpers in network_helpers.c should be a blocking socket and has a 3s default delay. select should not be needed. Try to stay with the default. If the default turned out to be too flaky in the bpf CI in the future, one default change is easier.
Quoted 15 lines
tools/testing/selftests/bpf/network_helpers.c +15
-0+ !FD_ISSET(sockfd, &rfds)) +return -1 ; + +memset (&iov,0 ,sizeof (iov)); + iov.iov_base = data; + iov.iov_len =sizeof (data); + +memset (&hdr,0 ,sizeof (hdr)); + hdr.msg_name = src_addr; + hdr.msg_namelen =sizeof (struct sockaddr_storage ); + hdr.msg_iov = &iov; + hdr.msg_iovlen =1 ; + +return recvmsg (sockfd, &hdr,0 ); +}
PATCH 5/8 selftests/bpf: Factor out load_path and defines from test_sock_addr
In reply to Jordan RifeFactor out useful defines and load_path() from the test_sock_addr self test to use them in sock_addr_kern.
--- tools/testing/selftests/bpf/Makefile | 34 +++++---- .../testing/selftests/bpf/sock_addr_helpers.c | 46 +++++++++++ .../testing/selftests/bpf/sock_addr_helpers.h | 44 +++++++++++ tools/testing/selftests/bpf/test_sock_addr.c | 76 +++---------------- 4 files changed, 120 insertions(+), 80 deletions(-) create mode 100644 tools/testing/selftests/bpf/sock_addr_helpers.c create mode 100644 tools/testing/selftests/bpf/sock_addr_helpers.h
tools/testing/selftests/bpf/Makefile +18 -16
@@ -291,6 +291,7 @@ TEST_GEN_PROGS_EXTENDED += $(TRUNNER_BPFTOOL) $(TEST_GEN_PROGS) $(TEST_GEN_PROGS_EXTENDED): $(BPFOBJ) +SOCK_ADDR_HELPERS := $(OUTPUT)/sock_addr_helpers.o TESTING_HELPERS := $(OUTPUT)/testing_helpers.o CGROUP_HELPERS := $(OUTPUT)/cgroup_helpers.o UNPRIV_HELPERS := $(OUTPUT)/unpriv_helpers.o
@@ -301,7 +302,7 @@ CAP_HELPERS := $(OUTPUT)/cap_helpers.o $(OUTPUT)/test_dev_cgroup: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_skb_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_sock: $(CGROUP_HELPERS) $(TESTING_HELPERS) -$(OUTPUT)/test_sock_addr: $(CGROUP_HELPERS) $(TESTING_HELPERS) +$(OUTPUT)/test_sock_addr: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(SOCK_ADDR_HELPERS) $(OUTPUT)/test_sockmap: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(OUTPUT)/test_tcpnotify_user: $(CGROUP_HELPERS) $(TESTING_HELPERS) $(TRACE_HELPERS) $(OUTPUT)/get_cgroup_id_user: $(CGROUP_HELPERS) $(TESTING_HELPERS)
@@ -628,21 +629,22 @@ endef # Define test_progs test runner. TRUNNER_TESTS_DIR := prog_tests TRUNNER_BPF_PROGS_DIR := progs -TRUNNER_EXTRA_SOURCES := test_progs.c \ - cgroup_helpers.c \ - trace_helpers.c \ - network_helpers.c \ - testing_helpers.c \ - btf_helpers.c \ - cap_helpers.c \ - unpriv_helpers.c \ - netlink_helpers.c \ - test_loader.c \ - xsk.c \ - disasm.c \ - json_writer.c \ - flow_dissector_load.h \ - ip_check_defrag_frags.h +TRUNNER_EXTRA_SOURCES := test_progs.c \ + cgroup_helpers.c \ + trace_helpers.c \ + network_helpers.c \ + testing_helpers.c \ + btf_helpers.c \ + cap_helpers.c \ + unpriv_helpers.c \ + netlink_helpers.c \ + test_loader.c \ + xsk.c \ + disasm.c \ + json_writer.c \ + flow_dissector_load.h \ + ip_check_defrag_frags.h \ + sock_addr_helpers.c TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko \ $(OUTPUT)/bpf_test_no_cfi.ko \ $(OUTPUT)/sock_addr_testmod.ko \
tools/testing/selftests/bpf/sock_addr_helpers.c new file +46 -0
46 lines not on this page · raw message
tools/testing/selftests/bpf/sock_addr_helpers.h new file +44 -0
44 lines not on this page · raw message
tools/testing/selftests/bpf/test_sock_addr.c +12 -64
133 lines not on this page · raw message
Signature · 1 line
2.44.0.478.gd926399ef9-goog
Martin KaFai Lau
reviewerRe: [PATCH v1 bpf-next 5/8] selftests/bpf: Factor out load_path and defines from test_sock_addr
Quoted 16 lines · Jordan Rife
tools/testing/selftests/bpf/sock_addr_helpers.c new file +10
-0@@ -0,0 +1,46 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include <bpf/bpf.h> +#include <bpf/libbpf.h> + +#include "cgroup_helpers.h" +#include "sock_addr_helpers.h"
tools/testing/selftests/bpf/sock_addr_helpers.c line 20
Quoted 10 lines
tools/testing/selftests/bpf/sock_addr_helpers.c new file +10
-0+#include "testing_helpers.h" + +int load_path (const char *path,enum bpf_attach_type attach_type, +bool expect_reject) +{ +struct bpf_object *obj; +struct bpf_program *prog; +int err; + + obj =bpf_object__open_file (path,NULL );
Although it works, it is heading to the opposite direction by reusing things from the older test_sock_addr.c.
test_sock_addr.c should have been moved to the test_progs. It is not run by bpf CI and bits get rotten [e.g. the bug fix in patch 8]. There is also old practice like bpf_object__open_file() should have been replaced with the skeleton __open_and_load() instead of refactoring it out to create new use cases.
The newer prog_tests/sock_addr.c was created when adding AF_UNIX support. It has a very similar setup as the older test_sock_addr.c and the intention was to finally retire test_sock_addr.c. e.g. It also has "load_fn loadfn" but is done with skeleton, the program is also attached to cgroup...etc.
Instead of adding a new sock_addr_kern.c in patch 7, it probably will be easier to add the kernel socket tests into the existing prog_tests/sock_addr.c.
Also setup the netns and veth in the prog_tests/sock_addr.c instead of calling out the test_sock_addr.sh (which should also go away eventually), there are examples in prog_tests/ (e.g. mptcp.c).
Quoted 26 lines
tools/testing/selftests/bpf/sock_addr_helpers.c new file +26
-0+ err =libbpf_get_error (obj); +if (err) { +log_err (">>> Opening BPF object (%s) error.\n" , path); +return -1 ; + } + + prog =bpf_object__next_program (obj,NULL ); +if (!prog) + goto err_out; + +bpf_program__set_type (prog,BPF_PROG_TYPE_CGROUP_SOCK_ADDR ); +bpf_program__set_expected_attach_type (prog, attach_type); +bpf_program__set_flags (prog,testing_prog_flags ()); + + err =bpf_object__load (obj); +if (err) { +if (!expect_reject) +log_err (">>> Loading program (%s) error.\n" , path); + goto err_out; + } + +return bpf_program__fd (prog); +err_out: +bpf_object__close (obj); +return -1 ; +}
Re: [PATCH v1 bpf-next 5/8] selftests/bpf: Factor out load_path and defines from test_sock_addr
Quoted 4 lines
The newer
prog_tests/sock_addr.cwas created when addingAF_UNIXsupport. It has a very similar setup as the oldertest_sock_addr.cand the intention was to finally retiretest_sock_addr.c. e.g. It also has "load_fnloadfn" but is done with skeleton, the program is also attached to cgroup...etc.
Thanks, I wasn't sure what the preferred direction would be here. I saw some discussion
in the AF_UNIX support patch series about prog_tests being the preferred approach and
there being plans to migrate the old test_sock_addr.c tests to the new format.
I suspect the changes mentioned here (load done with skeleton, etc.) and helpers needed
to support the tests in my patch series may also be helpful in retiring test_sock_addr.c.
Quoted 2 lines
Instead of adding a new
sock_addr_kern.cin patch 7, it probably will be easier to add the kernel socket tests into the existingprog_tests/sock_addr.c.
Originally it felt awkward to try to merge these tests with prog_tests/sock_addr.c, because
while the goals are similar the flow is different with loading/unloading a kernel module to
drive socket operations. I'll revisit this and see if I can merge everything intoprog_tests/sock_addr.c. Switching to kfuncs as mentioned earlier might make this simpler
as well.
Quoted 3 lines
Also setup the netns and veth in the
prog_tests/sock_addr.cinstead of calling out thetest_sock_addr.sh(which should also go away eventually), there are examples in prog_tests/ (e.g.mptcp.c).
Ack. Sure thing.
-Jordan
PATCH 6/8 selftests/bpf: Add setup/cleanup subcommands
In reply to Jordan RifeAdd optional setup/cleanup subcommands to test_sock_addr.sh to allow those phases to be driven externally by the sock_addr_kern test program.
--- tools/testing/selftests/bpf/test_sock_addr.sh | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/test_sock_addr.sh +9 -1
@@ -55,4 +55,12 @@ TEST_IPv4="127.0.0.4/8" TEST_IPv6="::6/128" MAX_PING_TRIES=5 -main +if [ $# = 0 ]; then + main +elif [ $1 = "setup" ]; then + setup +elif [ $1 = "cleanup" ]; then + cleanup +else + echo "invalid option: $1" +fi
Signature · 1 line
2.44.0.478.gd926399ef9-goog
PATCH 7/8 selftests/bpf: Add sock_addr_kern prog_test
In reply to Jordan RifeThe sock_addr_kern test program leverages the sock_addr_testmod kernel module to test the interaction between kernel socket operations and BPF sockaddr hooks. It focuses on operations that may modify an input address, namely connect, bind, and sendmsg to add regression test coverage for
- commit 0bdf399342c5("net: Avoid address overwrite in kernel_connect")
- commit 86a7e0b69bd5("net: prevent rewrite of msg_name in sock_sendmsg()")
- commit c889a99a21bf("net: prevent address rewrite in kernel_bind()")
with some additional sanity checks in place to make sure kernel_getpeername() and kernel_getsockname() work as expected. It provides coverage for IPv4, IPv6, and the recently added Unix sockaddr hooks.
--- tools/testing/selftests/bpf/Makefile | 1 + .../selftests/bpf/prog_tests/sock_addr_kern.c | 631 ++++++++++++++++++ 2 files changed, 632 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/sock_addr_kern.c
tools/testing/selftests/bpf/Makefile +1 -0
@@ -654,6 +654,7 @@ TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko \ $(OUTPUT)/uprobe_multi \ ima_setup.sh \ verify_sig_setup.sh \ + test_sock_addr.sh \ $(wildcard progs/btf_dump_test_case_*.c) \ $(wildcard progs/*.bpf.o) TRUNNER_BPF_BUILD_RULE := CLANG_BPF_BUILD_RULE
tools/testing/selftests/bpf/prog_tests/sock_addr_kern.c new file +631 -0
631 lines not on this page · raw message
Signature · 1 line
2.44.0.478.gd926399ef9-goog
PATCH 8/8 selftests/bpf: Fix bind program for big endian systems
In reply to Jordan RifeWithout this fix, the bind4 and bind6 programs will reject bind attempts on big endian systems. This patch ensures that CI tests pass for the s390x architecture.
--- .../testing/selftests/bpf/progs/bind4_prog.c | 18 ++++++++++-------- .../testing/selftests/bpf/progs/bind6_prog.c | 18 ++++++++++-------- tools/testing/selftests/bpf/progs/bind_prog.h | 19 +++++++++++++++++++ 3 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/bind_prog.h
tools/testing/selftests/bpf/progs/bind4_prog.c +10 -8
@@ -12,6 +12,8 @@#include <bpf/bpf_helpers.h> #include <bpf/bpf_endian.h> +#include "bind_prog.h" +#define SERV4_IP 0xc0a801feU/* 192.168.1.254 */ #define SERV4_PORT 4040#define SERV4_REWRITE_IP 0x7f000001U/* 127.0.0.1 */
@@ -118,23 +120,23 @@ int bind_v4_prog(struct bpf_sock_addr *ctx)// u8 narrow loads: user_ip4 =0 ; - user_ip4 |= ((volatile __u8 *)&ctx->user_ip4)[0 ] <<0 ; - user_ip4 |= ((volatile __u8 *)&ctx->user_ip4)[1 ] <<8 ; - user_ip4 |= ((volatile __u8 *)&ctx->user_ip4)[2 ] <<16 ; - user_ip4 |= ((volatile __u8 *)&ctx->user_ip4)[3 ] <<24 ; + user_ip4 |=load_byte_ntoh (ctx->user_ip4,0 ,sizeof (user_ip4)); + user_ip4 |=load_byte_ntoh (ctx->user_ip4,1 ,sizeof (user_ip4)); + user_ip4 |=load_byte_ntoh (ctx->user_ip4,2 ,sizeof (user_ip4)); + user_ip4 |=load_byte_ntoh (ctx->user_ip4,3 ,sizeof (user_ip4));if (ctx->user_ip4 != user_ip4)return 0 ; user_port =0 ; - user_port |= ((volatile __u8 *)&ctx->user_port)[0 ] <<0 ; - user_port |= ((volatile __u8 *)&ctx->user_port)[1 ] <<8 ; + user_port |=load_byte_ntoh (ctx->user_port,0 ,sizeof (user_port)); + user_port |=load_byte_ntoh (ctx->user_port,1 ,sizeof (user_port));if (ctx->user_port != user_port)return 0 ;// u16 narrow loads: user_ip4 =0 ; - user_ip4 |= ((volatile __u16 *)&ctx->user_ip4)[0 ] <<0 ; - user_ip4 |= ((volatile __u16 *)&ctx->user_ip4)[1 ] <<16 ; + user_ip4 |=load_word_ntoh (ctx->user_ip4,0 ,sizeof (user_ip4)); + user_ip4 |=load_word_ntoh (ctx->user_ip4,1 ,sizeof (user_ip4));if (ctx->user_ip4 != user_ip4)return 0 ;
tools/testing/selftests/bpf/progs/bind6_prog.c +10 -8
@@ -12,6 +12,8 @@#include <bpf/bpf_helpers.h> #include <bpf/bpf_endian.h> +#include "bind_prog.h" +#define SERV6_IP_0 0xfaceb00c/* face:b00c:1234:5678::abcd */ #define SERV6_IP_1 0x12345678#define SERV6_IP_2 0x00000000
@@ -129,25 +131,25 @@ int bind_v6_prog(struct bpf_sock_addr *ctx)// u8 narrow loads: for (i =0 ; i <4 ; i++) { user_ip6 =0 ; - user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[0 ] <<0 ; - user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[1 ] <<8 ; - user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[2 ] <<16 ; - user_ip6 |= ((volatile __u8 *)&ctx->user_ip6[i])[3 ] <<24 ; + user_ip6 |=load_byte_ntoh (ctx->user_ip6[i],0 ,sizeof (user_ip6)); + user_ip6 |=load_byte_ntoh (ctx->user_ip6[i],1 ,sizeof (user_ip6)); + user_ip6 |=load_byte_ntoh (ctx->user_ip6[i],2 ,sizeof (user_ip6)); + user_ip6 |=load_byte_ntoh (ctx->user_ip6[i],3 ,sizeof (user_ip6));if (ctx->user_ip6[i] != user_ip6)return 0 ; } user_port =0 ; - user_port |= ((volatile __u8 *)&ctx->user_port)[0 ] <<0 ; - user_port |= ((volatile __u8 *)&ctx->user_port)[1 ] <<8 ; + user_port |=load_byte_ntoh (ctx->user_port,0 ,sizeof (user_port)); + user_port |=load_byte_ntoh (ctx->user_port,1 ,sizeof (user_port));if (ctx->user_port != user_port)return 0 ;// u16 narrow loads: for (i =0 ; i <4 ; i++) { user_ip6 =0 ; - user_ip6 |= ((volatile __u16 *)&ctx->user_ip6[i])[0 ] <<0 ; - user_ip6 |= ((volatile __u16 *)&ctx->user_ip6[i])[1 ] <<16 ; + user_ip6 |=load_word_ntoh (ctx->user_ip6[i],0 ,sizeof (user_ip6)); + user_ip6 |=load_word_ntoh (ctx->user_ip6[i],1 ,sizeof (user_ip6));if (ctx->user_ip6[i] != user_ip6)return 0 ; }
tools/testing/selftests/bpf/progs/bind_prog.h new file +19 -0
@@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __BIND_PROG_H__ +#define __BIND_PROG_H__ + +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +#define load_byte_ntoh (src, b, s) \ + (((volatile __u8 *)&(src))[b] << 8 * b) +#define load_word_ntoh (src, w, s) \ + (((volatile __u16 *)&(src))[w] << 16 * w) +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +#define load_byte_ntoh (src, b, s) \ + (((volatile __u8 *)&(src))[(b) + (sizeof(src) - (s))] << 8 * ((s) - (b) - 1)) +#define load_word_ntoh (src, w, s) \ + (((volatile __u16 *)&(src))[w] << 16 * (((s) / 2) - (w) - 1)) +#else +# error "Fix your compiler's __BYTE_ORDER__?!" +#endif + +#endif
Signature · 1 line
2.44.0.478.gd926399ef9-goog