Commit 202702e8 authored by Ilya Leoshkevich's avatar Ilya Leoshkevich Committed by Andrii Nakryiko

selftests/bpf: Attach to fopen()/fclose() in attach_probe

malloc() and free() may be completely replaced by sanitizers, use
fopen() and fclose() instead.
Signed-off-by: default avatarIlya Leoshkevich <iii@linux.ibm.com>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20230210001210.395194-7-iii@linux.ibm.com
parent 907300c7
...@@ -33,8 +33,8 @@ void test_attach_probe(void) ...@@ -33,8 +33,8 @@ void test_attach_probe(void)
struct test_attach_probe* skel; struct test_attach_probe* skel;
ssize_t uprobe_offset, ref_ctr_offset; ssize_t uprobe_offset, ref_ctr_offset;
struct bpf_link *uprobe_err_link; struct bpf_link *uprobe_err_link;
FILE *devnull;
bool legacy; bool legacy;
char *mem;
/* Check if new-style kprobe/uprobe API is supported. /* Check if new-style kprobe/uprobe API is supported.
* Kernels that support new FD-based kprobe and uprobe BPF attachment * Kernels that support new FD-based kprobe and uprobe BPF attachment
...@@ -147,7 +147,7 @@ void test_attach_probe(void) ...@@ -147,7 +147,7 @@ void test_attach_probe(void)
/* test attach by name for a library function, using the library /* test attach by name for a library function, using the library
* as the binary argument. libc.so.6 will be resolved via dlopen()/dlinfo(). * as the binary argument. libc.so.6 will be resolved via dlopen()/dlinfo().
*/ */
uprobe_opts.func_name = "malloc"; uprobe_opts.func_name = "fopen";
uprobe_opts.retprobe = false; uprobe_opts.retprobe = false;
skel->links.handle_uprobe_byname2 = skel->links.handle_uprobe_byname2 =
bpf_program__attach_uprobe_opts(skel->progs.handle_uprobe_byname2, bpf_program__attach_uprobe_opts(skel->progs.handle_uprobe_byname2,
...@@ -157,7 +157,7 @@ void test_attach_probe(void) ...@@ -157,7 +157,7 @@ void test_attach_probe(void)
if (!ASSERT_OK_PTR(skel->links.handle_uprobe_byname2, "attach_uprobe_byname2")) if (!ASSERT_OK_PTR(skel->links.handle_uprobe_byname2, "attach_uprobe_byname2"))
goto cleanup; goto cleanup;
uprobe_opts.func_name = "free"; uprobe_opts.func_name = "fclose";
uprobe_opts.retprobe = true; uprobe_opts.retprobe = true;
skel->links.handle_uretprobe_byname2 = skel->links.handle_uretprobe_byname2 =
bpf_program__attach_uprobe_opts(skel->progs.handle_uretprobe_byname2, bpf_program__attach_uprobe_opts(skel->progs.handle_uretprobe_byname2,
...@@ -195,8 +195,8 @@ void test_attach_probe(void) ...@@ -195,8 +195,8 @@ void test_attach_probe(void)
usleep(1); usleep(1);
/* trigger & validate shared library u[ret]probes attached by name */ /* trigger & validate shared library u[ret]probes attached by name */
mem = malloc(1); devnull = fopen("/dev/null", "r");
free(mem); fclose(devnull);
/* trigger & validate uprobe & uretprobe */ /* trigger & validate uprobe & uretprobe */
trigger_func(); trigger_func();
......
...@@ -92,18 +92,19 @@ int handle_uretprobe_byname(struct pt_regs *ctx) ...@@ -92,18 +92,19 @@ int handle_uretprobe_byname(struct pt_regs *ctx)
} }
SEC("uprobe") SEC("uprobe")
int handle_uprobe_byname2(struct pt_regs *ctx) int BPF_UPROBE(handle_uprobe_byname2, const char *pathname, const char *mode)
{ {
unsigned int size = PT_REGS_PARM1(ctx); char mode_buf[2] = {};
/* verify malloc size */ /* verify fopen mode */
if (size == 1) bpf_probe_read_user(mode_buf, sizeof(mode_buf), mode);
if (mode_buf[0] == 'r' && mode_buf[1] == 0)
uprobe_byname2_res = 7; uprobe_byname2_res = 7;
return 0; return 0;
} }
SEC("uretprobe") SEC("uretprobe")
int handle_uretprobe_byname2(struct pt_regs *ctx) int BPF_URETPROBE(handle_uretprobe_byname2, void *ret)
{ {
uretprobe_byname2_res = 8; uretprobe_byname2_res = 8;
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment