Skip to content

Commit 805defa

Browse files
borkmannrtg-canonical
authored andcommitted
bpf, testing: Add selftest to read/write sockaddr from user space
BugLink: https://bugs.launchpad.net/bugs/1990009 Tested on x86-64 and Ilya was also kind enough to give it a spin on s390x, both passing with probe_user:OK there. The test is using the newly added bpf_probe_read_user() to dump sockaddr from connect call into .bss BPF map and overrides the user buffer via bpf_probe_write_user(): # ./test_progs [...] #17 pkt_md_access:OK #18 probe_user:OK #19 prog_run_xattr:OK [...] Signed-off-by: Daniel Borkmann <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Tested-by: Ilya Leoshkevich <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/90f449d8af25354e05080e82fc6e2d3179da30ea.1572649915.git.daniel@iogearbox.net (cherry picked from commit fa553d9) Signed-off-by: Tim Gardner <[email protected]> Acked-by: Cengiz Can <[email protected]> Acked-by: Joseph Salisbury <[email protected]> Signed-off-by: Tim Gardner <[email protected]>
1 parent b0b8c1a commit 805defa

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <test_progs.h>
3+
4+
void test_probe_user(void)
5+
{
6+
#define kprobe_name "__sys_connect"
7+
const char *prog_name = "kprobe/" kprobe_name;
8+
const char *obj_file = "./test_probe_user.o";
9+
DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts, );
10+
int err, results_map_fd, sock_fd, duration = 0;
11+
struct sockaddr curr, orig, tmp;
12+
struct sockaddr_in *in = (struct sockaddr_in *)&curr;
13+
struct bpf_link *kprobe_link = NULL;
14+
struct bpf_program *kprobe_prog;
15+
struct bpf_object *obj;
16+
static const int zero = 0;
17+
18+
obj = bpf_object__open_file(obj_file, &opts);
19+
if (CHECK(IS_ERR(obj), "obj_open_file", "err %ld\n", PTR_ERR(obj)))
20+
return;
21+
22+
kprobe_prog = bpf_object__find_program_by_title(obj, prog_name);
23+
if (CHECK(!kprobe_prog, "find_probe",
24+
"prog '%s' not found\n", prog_name))
25+
goto cleanup;
26+
27+
err = bpf_object__load(obj);
28+
if (CHECK(err, "obj_load", "err %d\n", err))
29+
goto cleanup;
30+
31+
results_map_fd = bpf_find_map(__func__, obj, "test_pro.bss");
32+
if (CHECK(results_map_fd < 0, "find_bss_map",
33+
"err %d\n", results_map_fd))
34+
goto cleanup;
35+
36+
kprobe_link = bpf_program__attach_kprobe(kprobe_prog, false,
37+
kprobe_name);
38+
if (CHECK(IS_ERR(kprobe_link), "attach_kprobe",
39+
"err %ld\n", PTR_ERR(kprobe_link))) {
40+
kprobe_link = NULL;
41+
goto cleanup;
42+
}
43+
44+
memset(&curr, 0, sizeof(curr));
45+
in->sin_family = AF_INET;
46+
in->sin_port = htons(5555);
47+
in->sin_addr.s_addr = inet_addr("255.255.255.255");
48+
memcpy(&orig, &curr, sizeof(curr));
49+
50+
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
51+
if (CHECK(sock_fd < 0, "create_sock_fd", "err %d\n", sock_fd))
52+
goto cleanup;
53+
54+
connect(sock_fd, &curr, sizeof(curr));
55+
close(sock_fd);
56+
57+
err = bpf_map_lookup_elem(results_map_fd, &zero, &tmp);
58+
if (CHECK(err, "get_kprobe_res",
59+
"failed to get kprobe res: %d\n", err))
60+
goto cleanup;
61+
62+
in = (struct sockaddr_in *)&tmp;
63+
if (CHECK(memcmp(&tmp, &orig, sizeof(orig)), "check_kprobe_res",
64+
"wrong kprobe res from probe read: %s:%u\n",
65+
inet_ntoa(in->sin_addr), ntohs(in->sin_port)))
66+
goto cleanup;
67+
68+
memset(&tmp, 0xab, sizeof(tmp));
69+
70+
in = (struct sockaddr_in *)&curr;
71+
if (CHECK(memcmp(&curr, &tmp, sizeof(tmp)), "check_kprobe_res",
72+
"wrong kprobe res from probe write: %s:%u\n",
73+
inet_ntoa(in->sin_addr), ntohs(in->sin_port)))
74+
goto cleanup;
75+
cleanup:
76+
bpf_link__destroy(kprobe_link);
77+
bpf_object__close(obj);
78+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <linux/ptrace.h>
4+
#include <linux/bpf.h>
5+
6+
#include <netinet/in.h>
7+
8+
#include "bpf_helpers.h"
9+
#include "bpf_tracing.h"
10+
11+
static struct sockaddr_in old;
12+
13+
SEC("kprobe/__sys_connect")
14+
int handle_sys_connect(struct pt_regs *ctx)
15+
{
16+
void *ptr = (void *)PT_REGS_PARM2(ctx);
17+
struct sockaddr_in new;
18+
19+
bpf_probe_read_user(&old, sizeof(old), ptr);
20+
__builtin_memset(&new, 0xab, sizeof(new));
21+
bpf_probe_write_user(ptr, &new, sizeof(new));
22+
23+
return 0;
24+
}
25+
26+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)