netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jordan Rife <jrife@google.com>
To: bpf@vger.kernel.org
Cc: Jordan Rife <jrife@google.com>,
	linux-kselftest@vger.kernel.org,  netdev@vger.kernel.org,
	Alexei Starovoitov <ast@kernel.org>,
	 Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	 Martin KaFai Lau <martin.lau@linux.dev>,
	Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
	 Yonghong Song <yonghong.song@linux.dev>,
	John Fastabend <john.fastabend@gmail.com>,
	 KP Singh <kpsingh@kernel.org>,
	Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>,
	 Jiri Olsa <jolsa@kernel.org>, Mykola Lysenko <mykolal@fb.com>,
	Shuah Khan <shuah@kernel.org>,
	 "David S. Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>,
	 Jesper Dangaard Brouer <hawk@kernel.org>,
	Daan De Meyer <daan.j.demeyer@gmail.com>
Subject: [PATCH v1 bpf-next 0/8] selftests/bpf: Add sockaddr tests for kernel networking
Date: Fri, 29 Mar 2024 14:18:45 -0500	[thread overview]
Message-ID: <20240329191907.1808635-1-jrife@google.com> (raw)

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

-- 
2.44.0.478.gd926399ef9-goog


             reply	other threads:[~2024-03-29 19:19 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-29 19:18 Jordan Rife [this message]
2024-03-29 19:18 ` [PATCH v1 bpf-next 1/8] selftests/bpf: Introduce sock_addr_testmod Jordan Rife
2024-03-29 22:09   ` Andrii Nakryiko
2024-03-29 22:09     ` Andrii Nakryiko
2024-04-02 17:57   ` Martin KaFai Lau
2024-04-02 18:14     ` Jordan Rife
2024-03-29 19:18 ` [PATCH v1 bpf-next 2/8] selftests/bpf: Add module load helpers Jordan Rife
2024-03-29 19:18 ` [PATCH v1 bpf-next 3/8] selftests/bpf: Factor out cmp_addr Jordan Rife
2024-03-29 19:18 ` [PATCH v1 bpf-next 4/8] selftests/bpf: Add recv_msg_from_client to network helpers Jordan Rife
2024-04-02 22:33   ` Martin KaFai Lau
2024-03-29 19:18 ` [PATCH v1 bpf-next 5/8] selftests/bpf: Factor out load_path and defines from test_sock_addr Jordan Rife
2024-04-02 23:14   ` Martin KaFai Lau
2024-04-03  0:05     ` Jordan Rife
2024-03-29 19:18 ` [PATCH v1 bpf-next 6/8] selftests/bpf: Add setup/cleanup subcommands Jordan Rife
2024-03-29 19:18 ` [PATCH v1 bpf-next 7/8] selftests/bpf: Add sock_addr_kern prog_test Jordan Rife
2024-03-29 19:18 ` [PATCH v1 bpf-next 8/8] selftests/bpf: Fix bind program for big endian systems Jordan Rife

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240329191907.1808635-1-jrife@google.com \
    --to=jrife@google.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daan.j.demeyer@gmail.com \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=eddyz87@gmail.com \
    --cc=haoluo@google.com \
    --cc=hawk@kernel.org \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mykolal@fb.com \
    --cc=netdev@vger.kernel.org \
    --cc=sdf@google.com \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).