mbox series

[v1,bpf-next,0/8] selftests/bpf: Add sockaddr tests for kernel networking

Message ID 20240329191907.1808635-1-jrife@google.com (mailing list archive)
Headers show
Series selftests/bpf: Add sockaddr tests for kernel networking | expand

Message

Jordan Rife March 29, 2024, 7:18 p.m. UTC
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