xref: /linux/tools/testing/selftests/bpf/prog_tests/probe_user.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 
4 /* TODO: corrupts other tests uses connect() */
serial_test_probe_user(void)5 void serial_test_probe_user(void)
6 {
7 	static const char *const prog_names[] = {
8 		"handle_sys_connect",
9 #if defined(__s390x__)
10 		"handle_sys_socketcall",
11 #endif
12 	};
13 	enum { prog_count = ARRAY_SIZE(prog_names) };
14 	const char *obj_file = "./test_probe_user.bpf.o";
15 	DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts, );
16 	int err, results_map_fd, sock_fd, duration = 0;
17 	struct sockaddr curr, orig, tmp;
18 	struct sockaddr_in *in = (struct sockaddr_in *)&curr;
19 	struct bpf_link *kprobe_links[prog_count] = {};
20 	struct bpf_program *kprobe_progs[prog_count];
21 	struct bpf_object *obj;
22 	static const int zero = 0;
23 	size_t i;
24 
25 	obj = bpf_object__open_file(obj_file, &opts);
26 	if (!ASSERT_OK_PTR(obj, "obj_open_file"))
27 		return;
28 
29 	for (i = 0; i < prog_count; i++) {
30 		kprobe_progs[i] =
31 			bpf_object__find_program_by_name(obj, prog_names[i]);
32 		if (CHECK(!kprobe_progs[i], "find_probe",
33 			  "prog '%s' not found\n", prog_names[i]))
34 			goto cleanup;
35 	}
36 
37 	err = bpf_object__load(obj);
38 	if (CHECK(err, "obj_load", "err %d\n", err))
39 		goto cleanup;
40 
41 	results_map_fd = bpf_find_map(__func__, obj, "test_pro.bss");
42 	if (CHECK(results_map_fd < 0, "find_bss_map",
43 		  "err %d\n", results_map_fd))
44 		goto cleanup;
45 
46 	for (i = 0; i < prog_count; i++) {
47 		kprobe_links[i] = bpf_program__attach(kprobe_progs[i]);
48 		if (!ASSERT_OK_PTR(kprobe_links[i], "attach_kprobe"))
49 			goto cleanup;
50 	}
51 
52 	memset(&curr, 0, sizeof(curr));
53 	in->sin_family = AF_INET;
54 	in->sin_port = htons(5555);
55 	in->sin_addr.s_addr = inet_addr("255.255.255.255");
56 	memcpy(&orig, &curr, sizeof(curr));
57 
58 	sock_fd = socket(AF_INET, SOCK_STREAM, 0);
59 	if (CHECK(sock_fd < 0, "create_sock_fd", "err %d\n", sock_fd))
60 		goto cleanup;
61 
62 	connect(sock_fd, &curr, sizeof(curr));
63 	close(sock_fd);
64 
65 	err = bpf_map_lookup_elem(results_map_fd, &zero, &tmp);
66 	if (CHECK(err, "get_kprobe_res",
67 		  "failed to get kprobe res: %d\n", err))
68 		goto cleanup;
69 
70 	in = (struct sockaddr_in *)&tmp;
71 	if (CHECK(memcmp(&tmp, &orig, sizeof(orig)), "check_kprobe_res",
72 		  "wrong kprobe res from probe read: %s:%u\n",
73 		  inet_ntoa(in->sin_addr), ntohs(in->sin_port)))
74 		goto cleanup;
75 
76 	memset(&tmp, 0xab, sizeof(tmp));
77 
78 	in = (struct sockaddr_in *)&curr;
79 	if (CHECK(memcmp(&curr, &tmp, sizeof(tmp)), "check_kprobe_res",
80 		  "wrong kprobe res from probe write: %s:%u\n",
81 		  inet_ntoa(in->sin_addr), ntohs(in->sin_port)))
82 		goto cleanup;
83 cleanup:
84 	for (i = 0; i < prog_count; i++)
85 		bpf_link__destroy(kprobe_links[i]);
86 	bpf_object__close(obj);
87 }
88