2019-11-01 23:18:03 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include <test_progs.h>
|
|
|
|
|
2021-10-06 18:56:19 +00:00
|
|
|
/* TODO: corrupts other tests uses connect() */
|
|
|
|
void serial_test_probe_user(void)
|
2019-11-01 23:18:03 +00:00
|
|
|
{
|
selftests/bpf: Fix probe_user test failure with clang build kernel
clang build kernel failed the selftest probe_user.
$ ./test_progs -t probe_user
$ ...
$ test_probe_user:PASS:get_kprobe_res 0 nsec
$ test_probe_user:FAIL:check_kprobe_res wrong kprobe res from probe read: 0.0.0.0:0
$ #94 probe_user:FAIL
The test attached to kernel function __sys_connect(). In net/socket.c, we have
int __sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
{
......
}
...
SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
int, addrlen)
{
return __sys_connect(fd, uservaddr, addrlen);
}
The gcc compiler (8.5.0) does not inline __sys_connect() in syscall entry
function. But latest clang trunk did the inlining. So the bpf program
is not triggered.
To make the test more reliable, let us kprobe the syscall entry function
instead. Note that x86_64, arm64 and s390 have syscall wrappers and they have
to be handled specially.
Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210929033000.3711921-1-yhs@fb.com
2021-09-29 03:30:00 +00:00
|
|
|
const char *prog_name = "handle_sys_connect";
|
2019-11-01 23:18:03 +00:00
|
|
|
const char *obj_file = "./test_probe_user.o";
|
|
|
|
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts, );
|
|
|
|
int err, results_map_fd, sock_fd, duration = 0;
|
|
|
|
struct sockaddr curr, orig, tmp;
|
|
|
|
struct sockaddr_in *in = (struct sockaddr_in *)&curr;
|
|
|
|
struct bpf_link *kprobe_link = NULL;
|
|
|
|
struct bpf_program *kprobe_prog;
|
|
|
|
struct bpf_object *obj;
|
|
|
|
static const int zero = 0;
|
|
|
|
|
|
|
|
obj = bpf_object__open_file(obj_file, &opts);
|
2021-05-25 03:59:32 +00:00
|
|
|
if (!ASSERT_OK_PTR(obj, "obj_open_file"))
|
2019-11-01 23:18:03 +00:00
|
|
|
return;
|
|
|
|
|
selftests/bpf: Fix probe_user test failure with clang build kernel
clang build kernel failed the selftest probe_user.
$ ./test_progs -t probe_user
$ ...
$ test_probe_user:PASS:get_kprobe_res 0 nsec
$ test_probe_user:FAIL:check_kprobe_res wrong kprobe res from probe read: 0.0.0.0:0
$ #94 probe_user:FAIL
The test attached to kernel function __sys_connect(). In net/socket.c, we have
int __sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
{
......
}
...
SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
int, addrlen)
{
return __sys_connect(fd, uservaddr, addrlen);
}
The gcc compiler (8.5.0) does not inline __sys_connect() in syscall entry
function. But latest clang trunk did the inlining. So the bpf program
is not triggered.
To make the test more reliable, let us kprobe the syscall entry function
instead. Note that x86_64, arm64 and s390 have syscall wrappers and they have
to be handled specially.
Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210929033000.3711921-1-yhs@fb.com
2021-09-29 03:30:00 +00:00
|
|
|
kprobe_prog = bpf_object__find_program_by_name(obj, prog_name);
|
2019-11-01 23:18:03 +00:00
|
|
|
if (CHECK(!kprobe_prog, "find_probe",
|
|
|
|
"prog '%s' not found\n", prog_name))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
err = bpf_object__load(obj);
|
|
|
|
if (CHECK(err, "obj_load", "err %d\n", err))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
results_map_fd = bpf_find_map(__func__, obj, "test_pro.bss");
|
|
|
|
if (CHECK(results_map_fd < 0, "find_bss_map",
|
|
|
|
"err %d\n", results_map_fd))
|
|
|
|
goto cleanup;
|
|
|
|
|
2019-12-14 01:43:26 +00:00
|
|
|
kprobe_link = bpf_program__attach(kprobe_prog);
|
2021-05-25 03:59:32 +00:00
|
|
|
if (!ASSERT_OK_PTR(kprobe_link, "attach_kprobe"))
|
2019-11-01 23:18:03 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
memset(&curr, 0, sizeof(curr));
|
|
|
|
in->sin_family = AF_INET;
|
|
|
|
in->sin_port = htons(5555);
|
|
|
|
in->sin_addr.s_addr = inet_addr("255.255.255.255");
|
|
|
|
memcpy(&orig, &curr, sizeof(curr));
|
|
|
|
|
|
|
|
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (CHECK(sock_fd < 0, "create_sock_fd", "err %d\n", sock_fd))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
connect(sock_fd, &curr, sizeof(curr));
|
|
|
|
close(sock_fd);
|
|
|
|
|
|
|
|
err = bpf_map_lookup_elem(results_map_fd, &zero, &tmp);
|
|
|
|
if (CHECK(err, "get_kprobe_res",
|
|
|
|
"failed to get kprobe res: %d\n", err))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
in = (struct sockaddr_in *)&tmp;
|
|
|
|
if (CHECK(memcmp(&tmp, &orig, sizeof(orig)), "check_kprobe_res",
|
|
|
|
"wrong kprobe res from probe read: %s:%u\n",
|
|
|
|
inet_ntoa(in->sin_addr), ntohs(in->sin_port)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
memset(&tmp, 0xab, sizeof(tmp));
|
|
|
|
|
|
|
|
in = (struct sockaddr_in *)&curr;
|
|
|
|
if (CHECK(memcmp(&curr, &tmp, sizeof(tmp)), "check_kprobe_res",
|
|
|
|
"wrong kprobe res from probe write: %s:%u\n",
|
|
|
|
inet_ntoa(in->sin_addr), ntohs(in->sin_port)))
|
|
|
|
goto cleanup;
|
|
|
|
cleanup:
|
|
|
|
bpf_link__destroy(kprobe_link);
|
|
|
|
bpf_object__close(obj);
|
|
|
|
}
|