xref: /linux/tools/testing/selftests/bpf/prog_tests/ksock.c (revision 5a8cd539ac19f7a68e68e1d25ef9ca2ff55b8500)
1c7838e3dSMahe Tardy // SPDX-License-Identifier: GPL-2.0
2c7838e3dSMahe Tardy /* Copyright (c) 2026 Isovalent */
3c7838e3dSMahe Tardy 
4c7838e3dSMahe Tardy #include <arpa/inet.h>
5c7838e3dSMahe Tardy 
6c7838e3dSMahe Tardy #include "test_progs.h"
7c7838e3dSMahe Tardy #include "network_helpers.h"
8c7838e3dSMahe Tardy #include "ksock_lsm.skel.h"
9*7b0dfbf5SMahe Tardy #include "ksock_lsm_verifier.skel.h"
10c7838e3dSMahe Tardy 
11c7838e3dSMahe Tardy #define NS_TEST "ksock_lsm_ns"
12c7838e3dSMahe Tardy #define RECV_PORT 7777
13c7838e3dSMahe Tardy #define RECV_TIMEOUT_SEC 5
14c7838e3dSMahe Tardy 
15c7838e3dSMahe Tardy struct ksock_test_env {
16c7838e3dSMahe Tardy 	struct nstoken *nstoken;
17c7838e3dSMahe Tardy 	int rfd;
18c7838e3dSMahe Tardy };
19c7838e3dSMahe Tardy 
ksock_test_env_setup(struct ksock_test_env * env)20c7838e3dSMahe Tardy static bool ksock_test_env_setup(struct ksock_test_env *env)
21c7838e3dSMahe Tardy {
22c7838e3dSMahe Tardy 	struct sockaddr_in addr = {
23c7838e3dSMahe Tardy 		.sin_family = AF_INET,
24c7838e3dSMahe Tardy 		.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
25c7838e3dSMahe Tardy 		.sin_port = htons(RECV_PORT),
26c7838e3dSMahe Tardy 	};
27c7838e3dSMahe Tardy 	struct timeval tv = { .tv_sec = RECV_TIMEOUT_SEC };
28c7838e3dSMahe Tardy 	int err;
29c7838e3dSMahe Tardy 
30c7838e3dSMahe Tardy 	memset(env, 0, sizeof(*env));
31c7838e3dSMahe Tardy 	env->rfd = -1;
32c7838e3dSMahe Tardy 
33c7838e3dSMahe Tardy 	if (!ASSERT_OK(make_netns(NS_TEST), "make_netns"))
34c7838e3dSMahe Tardy 		goto fail;
35c7838e3dSMahe Tardy 
36c7838e3dSMahe Tardy 	env->nstoken = open_netns(NS_TEST);
37c7838e3dSMahe Tardy 	if (!ASSERT_OK_PTR(env->nstoken, "open_netns"))
38c7838e3dSMahe Tardy 		goto fail;
39c7838e3dSMahe Tardy 
40c7838e3dSMahe Tardy 	env->rfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
41c7838e3dSMahe Tardy 	if (!ASSERT_OK_FD(env->rfd, "receiver socket"))
42c7838e3dSMahe Tardy 		goto fail;
43c7838e3dSMahe Tardy 
44c7838e3dSMahe Tardy 	err = bind(env->rfd, (struct sockaddr *)&addr, sizeof(addr));
45c7838e3dSMahe Tardy 	if (!ASSERT_OK(err, "bind receiver"))
46c7838e3dSMahe Tardy 		goto fail;
47c7838e3dSMahe Tardy 
48c7838e3dSMahe Tardy 	err = setsockopt(env->rfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
49c7838e3dSMahe Tardy 	if (!ASSERT_OK(err, "set rcvtimeo"))
50c7838e3dSMahe Tardy 		goto fail;
51c7838e3dSMahe Tardy 
52c7838e3dSMahe Tardy 	return true;
53c7838e3dSMahe Tardy 
54c7838e3dSMahe Tardy fail:
55c7838e3dSMahe Tardy 	return false;
56c7838e3dSMahe Tardy }
57c7838e3dSMahe Tardy 
test_ksock_lsm(void)58c7838e3dSMahe Tardy void test_ksock_lsm(void)
59c7838e3dSMahe Tardy {
60c7838e3dSMahe Tardy 	LIBBPF_OPTS(bpf_test_run_opts, opts);
61c7838e3dSMahe Tardy 	struct ksock_test_env env;
62c7838e3dSMahe Tardy 	struct sockaddr_in trigger_addr = {
63c7838e3dSMahe Tardy 		.sin_family = AF_INET,
64c7838e3dSMahe Tardy 		.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
65c7838e3dSMahe Tardy 	};
66c7838e3dSMahe Tardy 	struct ksock_lsm *skel;
67c7838e3dSMahe Tardy 	char recv_data[sizeof(skel->data->send_data)] = {};
68c7838e3dSMahe Tardy 	ssize_t n;
69c7838e3dSMahe Tardy 	int tfd = -1;
70c7838e3dSMahe Tardy 	int err;
71c7838e3dSMahe Tardy 
72c7838e3dSMahe Tardy 	skel = ksock_lsm__open_and_load();
73c7838e3dSMahe Tardy 	if (!ASSERT_OK_PTR(skel, "skel open_and_load"))
74c7838e3dSMahe Tardy 		return;
75c7838e3dSMahe Tardy 
76c7838e3dSMahe Tardy 	if (!ksock_test_env_setup(&env))
77c7838e3dSMahe Tardy 		goto fail;
78c7838e3dSMahe Tardy 
79c7838e3dSMahe Tardy 	/* Step 1: Run the setup SYSCALL prog to create the ksock */
80c7838e3dSMahe Tardy 	skel->bss->ipv4_remote = htonl(INADDR_LOOPBACK);
81c7838e3dSMahe Tardy 	skel->bss->remote_port = RECV_PORT;
82c7838e3dSMahe Tardy 	err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.ksock_setup),
83c7838e3dSMahe Tardy 				     &opts);
84c7838e3dSMahe Tardy 	if (!ASSERT_OK(err, "ksock_setup run"))
85c7838e3dSMahe Tardy 		goto fail;
86c7838e3dSMahe Tardy 	if (!ASSERT_OK(opts.retval, "ksock_setup retval"))
87c7838e3dSMahe Tardy 		goto fail;
88c7838e3dSMahe Tardy 
89c7838e3dSMahe Tardy 	/* Step 2: Attach LSM prog and trigger socket_bind from userspace */
90c7838e3dSMahe Tardy 	skel->links.ksock_socket_bind =
91c7838e3dSMahe Tardy 		bpf_program__attach_lsm(skel->progs.ksock_socket_bind);
92c7838e3dSMahe Tardy 	if (!ASSERT_OK_PTR(skel->links.ksock_socket_bind,
93c7838e3dSMahe Tardy 			   "attach socket_bind lsm"))
94c7838e3dSMahe Tardy 		goto fail;
95c7838e3dSMahe Tardy 
96c7838e3dSMahe Tardy 	tfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
97c7838e3dSMahe Tardy 	if (!ASSERT_OK_FD(tfd, "trigger socket"))
98c7838e3dSMahe Tardy 		goto fail;
99c7838e3dSMahe Tardy 
100c7838e3dSMahe Tardy 	skel->bss->target_pid = getpid();
101c7838e3dSMahe Tardy 	err = bind(tfd, (struct sockaddr *)&trigger_addr, sizeof(trigger_addr));
102c7838e3dSMahe Tardy 	skel->bss->target_pid = 0;
103c7838e3dSMahe Tardy 	if (!ASSERT_OK(err, "trigger bind"))
104c7838e3dSMahe Tardy 		goto fail;
105c7838e3dSMahe Tardy 
106c7838e3dSMahe Tardy 	/* Step 3: Verify the LSM hook sent the notification */
107c7838e3dSMahe Tardy 	if (!ASSERT_EQ(skel->data->send_ret, sizeof(skel->data->send_data),
108c7838e3dSMahe Tardy 		       "LSM send bytes"))
109c7838e3dSMahe Tardy 		goto fail;
110c7838e3dSMahe Tardy 
111c7838e3dSMahe Tardy 	n = recvfrom(env.rfd, recv_data, sizeof(recv_data), 0, NULL, NULL);
112c7838e3dSMahe Tardy 	if (ASSERT_EQ(n, sizeof(recv_data), "recvfrom len"))
113c7838e3dSMahe Tardy 		ASSERT_MEMEQ(recv_data, skel->data->send_data, sizeof(recv_data),
114c7838e3dSMahe Tardy 			     "payload match");
115c7838e3dSMahe Tardy 
116c7838e3dSMahe Tardy fail:
117c7838e3dSMahe Tardy 	if (tfd >= 0)
118c7838e3dSMahe Tardy 		close(tfd);
119c7838e3dSMahe Tardy 	if (env.rfd >= 0)
120c7838e3dSMahe Tardy 		close(env.rfd);
121c7838e3dSMahe Tardy 	if (env.nstoken)
122c7838e3dSMahe Tardy 		close_netns(env.nstoken);
123c7838e3dSMahe Tardy 	remove_netns(NS_TEST);
124c7838e3dSMahe Tardy 	ksock_lsm__destroy(skel);
125c7838e3dSMahe Tardy }
126*7b0dfbf5SMahe Tardy 
test_ksock_lsm_verifier(void)127*7b0dfbf5SMahe Tardy void test_ksock_lsm_verifier(void)
128*7b0dfbf5SMahe Tardy {
129*7b0dfbf5SMahe Tardy 	RUN_TESTS(ksock_lsm_verifier);
130*7b0dfbf5SMahe Tardy }
131