Skip to content

Commit 502b0e3

Browse files
committed
Merge branch 'libbpf: uprobe name-based attach followups'
Alan Maguire says: ==================== Follow-up series to [1] to address some suggestions from Andrii to improve parsing and make it more robust (patches 1, 2) and to improve validation of u[ret]probe firing by validating expected argument and return values (patch 3). [1] https://lore.kernel.org/bpf/164903521182.13106.12656654142629368774.git-patchwork-notify@kernel.org/ Changes since v1: - split library name, auto-attach parsing into separate patches (Andrii, patches 1, 2) - made str_has_sfx() static inline, avoided repeated strlen()s by storing lengths, used strlen() instead of strnlen() (Andrii, patch 1) - fixed sscanf() arg to use %li, switched logging to use "prog '%s'" format, used direct strcmp() on probe_type instead of prefix check (Andrii, patch 2) - switched auto-attach tests to log parameter/return values to be checked by user-space side of tests. Needed to add pid filtering to avoid capturing stray malloc()s (Andrii, patch 3) ==================== Signed-off-by: Andrii Nakryiko <[email protected]>
2 parents 9fc4476 + 1717e24 commit 502b0e3

File tree

4 files changed

+95
-67
lines changed

4 files changed

+95
-67
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 34 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10766,7 +10766,7 @@ static int resolve_full_path(const char *file, char *result, size_t result_sz)
1076610766
const char *search_paths[3] = {};
1076710767
int i;
1076810768

10769-
if (strstr(file, ".so")) {
10769+
if (str_has_sfx(file, ".so") || strstr(file, ".so.")) {
1077010770
search_paths[0] = getenv("LD_LIBRARY_PATH");
1077110771
search_paths[1] = "/usr/lib64:/usr/lib";
1077210772
search_paths[2] = arch_specific_lib_paths();
@@ -10913,60 +10913,45 @@ bpf_program__attach_uprobe_opts(const struct bpf_program *prog, pid_t pid,
1091310913
static int attach_uprobe(const struct bpf_program *prog, long cookie, struct bpf_link **link)
1091410914
{
1091510915
DECLARE_LIBBPF_OPTS(bpf_uprobe_opts, opts);
10916-
char *func, *probe_name, *func_end;
10917-
char *func_name, binary_path[512];
10918-
unsigned long long raw_offset;
10919-
size_t offset = 0;
10920-
int n;
10916+
char *probe_type = NULL, *binary_path = NULL, *func_name = NULL;
10917+
int n, ret = -EINVAL;
10918+
long offset = 0;
1092110919

1092210920
*link = NULL;
1092310921

10924-
opts.retprobe = str_has_pfx(prog->sec_name, "uretprobe");
10925-
if (opts.retprobe)
10926-
probe_name = prog->sec_name + sizeof("uretprobe") - 1;
10927-
else
10928-
probe_name = prog->sec_name + sizeof("uprobe") - 1;
10929-
if (probe_name[0] == '/')
10930-
probe_name++;
10931-
10932-
/* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */
10933-
if (strlen(probe_name) == 0)
10934-
return 0;
10935-
10936-
snprintf(binary_path, sizeof(binary_path), "%s", probe_name);
10937-
/* ':' should be prior to function+offset */
10938-
func_name = strrchr(binary_path, ':');
10939-
if (!func_name) {
10940-
pr_warn("section '%s' missing ':function[+offset]' specification\n",
10922+
n = sscanf(prog->sec_name, "%m[^/]/%m[^:]:%m[a-zA-Z0-9_.]+%li",
10923+
&probe_type, &binary_path, &func_name, &offset);
10924+
switch (n) {
10925+
case 1:
10926+
/* handle SEC("u[ret]probe") - format is valid, but auto-attach is impossible. */
10927+
ret = 0;
10928+
break;
10929+
case 2:
10930+
pr_warn("prog '%s': section '%s' missing ':function[+offset]' specification\n",
10931+
prog->name, prog->sec_name);
10932+
break;
10933+
case 3:
10934+
case 4:
10935+
opts.retprobe = strcmp(probe_type, "uretprobe") == 0;
10936+
if (opts.retprobe && offset != 0) {
10937+
pr_warn("prog '%s': uretprobes do not support offset specification\n",
10938+
prog->name);
10939+
break;
10940+
}
10941+
opts.func_name = func_name;
10942+
*link = bpf_program__attach_uprobe_opts(prog, -1, binary_path, offset, &opts);
10943+
ret = libbpf_get_error(*link);
10944+
break;
10945+
default:
10946+
pr_warn("prog '%s': invalid format of section definition '%s'\n", prog->name,
1094110947
prog->sec_name);
10942-
return -EINVAL;
10943-
}
10944-
func_name[0] = '\0';
10945-
func_name++;
10946-
n = sscanf(func_name, "%m[a-zA-Z0-9_.]+%li", &func, &offset);
10947-
if (n < 1) {
10948-
pr_warn("uprobe name '%s' is invalid\n", func_name);
10949-
return -EINVAL;
10950-
}
10951-
if (opts.retprobe && offset != 0) {
10952-
free(func);
10953-
pr_warn("uretprobes do not support offset specification\n");
10954-
return -EINVAL;
10955-
}
10956-
10957-
/* Is func a raw address? */
10958-
errno = 0;
10959-
raw_offset = strtoull(func, &func_end, 0);
10960-
if (!errno && !*func_end) {
10961-
free(func);
10962-
func = NULL;
10963-
offset = (size_t)raw_offset;
10948+
break;
1096410949
}
10965-
opts.func_name = func;
10950+
free(probe_type);
10951+
free(binary_path);
10952+
free(func_name);
1096610953

10967-
*link = bpf_program__attach_uprobe_opts(prog, -1, binary_path, offset, &opts);
10968-
free(func);
10969-
return libbpf_get_error(*link);
10954+
return ret;
1097010955
}
1097110956

1097210957
struct bpf_link *bpf_program__attach_uprobe(const struct bpf_program *prog,

tools/lib/bpf/libbpf_internal.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@
103103
#define str_has_pfx(str, pfx) \
104104
(strncmp(str, pfx, __builtin_constant_p(pfx) ? sizeof(pfx) - 1 : strlen(pfx)) == 0)
105105

106+
/* suffix check */
107+
static inline bool str_has_sfx(const char *str, const char *sfx)
108+
{
109+
size_t str_len = strlen(str);
110+
size_t sfx_len = strlen(sfx);
111+
112+
if (sfx_len <= str_len)
113+
return strcmp(str + str_len - sfx_len, sfx);
114+
return false;
115+
}
116+
106117
/* Symbol versioning is different between static and shared library.
107118
* Properly versioned symbols are needed for shared library, but
108119
* only the symbol of the new version is needed for static library.

tools/testing/selftests/bpf/prog_tests/uprobe_autoattach.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
#include "test_uprobe_autoattach.skel.h"
66

77
/* uprobe attach point */
8-
static void autoattach_trigger_func(void)
8+
static noinline int autoattach_trigger_func(int arg)
99
{
1010
asm volatile ("");
11+
return arg + 1;
1112
}
1213

1314
void test_uprobe_autoattach(void)
1415
{
1516
struct test_uprobe_autoattach *skel;
17+
int trigger_val = 100, trigger_ret;
18+
size_t malloc_sz = 1;
1619
char *mem;
1720

1821
skel = test_uprobe_autoattach__open_and_load();
@@ -22,17 +25,25 @@ void test_uprobe_autoattach(void)
2225
if (!ASSERT_OK(test_uprobe_autoattach__attach(skel), "skel_attach"))
2326
goto cleanup;
2427

28+
skel->bss->test_pid = getpid();
29+
2530
/* trigger & validate uprobe & uretprobe */
26-
autoattach_trigger_func();
31+
trigger_ret = autoattach_trigger_func(trigger_val);
32+
33+
skel->bss->test_pid = getpid();
2734

2835
/* trigger & validate shared library u[ret]probes attached by name */
29-
mem = malloc(1);
36+
mem = malloc(malloc_sz);
3037
free(mem);
3138

32-
ASSERT_EQ(skel->bss->uprobe_byname_res, 1, "check_uprobe_byname_res");
33-
ASSERT_EQ(skel->bss->uretprobe_byname_res, 2, "check_uretprobe_byname_res");
34-
ASSERT_EQ(skel->bss->uprobe_byname2_res, 3, "check_uprobe_byname2_res");
35-
ASSERT_EQ(skel->bss->uretprobe_byname2_res, 4, "check_uretprobe_byname2_res");
39+
ASSERT_EQ(skel->bss->uprobe_byname_parm1, trigger_val, "check_uprobe_byname_parm1");
40+
ASSERT_EQ(skel->bss->uprobe_byname_ran, 1, "check_uprobe_byname_ran");
41+
ASSERT_EQ(skel->bss->uretprobe_byname_rc, trigger_ret, "check_uretprobe_byname_rc");
42+
ASSERT_EQ(skel->bss->uretprobe_byname_ran, 2, "check_uretprobe_byname_ran");
43+
ASSERT_EQ(skel->bss->uprobe_byname2_parm1, malloc_sz, "check_uprobe_byname2_parm1");
44+
ASSERT_EQ(skel->bss->uprobe_byname2_ran, 3, "check_uprobe_byname2_ran");
45+
ASSERT_EQ(skel->bss->uretprobe_byname2_rc, mem, "check_uretprobe_byname2_rc");
46+
ASSERT_EQ(skel->bss->uretprobe_byname2_ran, 4, "check_uretprobe_byname2_ran");
3647
cleanup:
3748
test_uprobe_autoattach__destroy(skel);
3849
}

tools/testing/selftests/bpf/progs/test_uprobe_autoattach.c

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
// SPDX-License-Identifier: GPL-2.0
22
/* Copyright (c) 2022, Oracle and/or its affiliates. */
33

4-
#include <linux/ptrace.h>
5-
#include <linux/bpf.h>
4+
#include "vmlinux.h"
5+
6+
#include <bpf/bpf_core_read.h>
67
#include <bpf/bpf_helpers.h>
78
#include <bpf/bpf_tracing.h>
89

9-
int uprobe_byname_res = 0;
10-
int uretprobe_byname_res = 0;
11-
int uprobe_byname2_res = 0;
12-
int uretprobe_byname2_res = 0;
10+
int uprobe_byname_parm1 = 0;
11+
int uprobe_byname_ran = 0;
12+
int uretprobe_byname_rc = 0;
13+
int uretprobe_byname_ran = 0;
14+
size_t uprobe_byname2_parm1 = 0;
15+
int uprobe_byname2_ran = 0;
16+
char *uretprobe_byname2_rc = NULL;
17+
int uretprobe_byname2_ran = 0;
18+
19+
int test_pid;
1320

1421
/* This program cannot auto-attach, but that should not stop other
1522
* programs from attaching.
@@ -23,29 +30,43 @@ int handle_uprobe_noautoattach(struct pt_regs *ctx)
2330
SEC("uprobe//proc/self/exe:autoattach_trigger_func")
2431
int handle_uprobe_byname(struct pt_regs *ctx)
2532
{
26-
uprobe_byname_res = 1;
33+
uprobe_byname_parm1 = PT_REGS_PARM1_CORE(ctx);
34+
uprobe_byname_ran = 1;
2735
return 0;
2836
}
2937

3038
SEC("uretprobe//proc/self/exe:autoattach_trigger_func")
3139
int handle_uretprobe_byname(struct pt_regs *ctx)
3240
{
33-
uretprobe_byname_res = 2;
41+
uretprobe_byname_rc = PT_REGS_RC_CORE(ctx);
42+
uretprobe_byname_ran = 2;
3443
return 0;
3544
}
3645

3746

3847
SEC("uprobe/libc.so.6:malloc")
3948
int handle_uprobe_byname2(struct pt_regs *ctx)
4049
{
41-
uprobe_byname2_res = 3;
50+
int pid = bpf_get_current_pid_tgid() >> 32;
51+
52+
/* ignore irrelevant invocations */
53+
if (test_pid != pid)
54+
return 0;
55+
uprobe_byname2_parm1 = PT_REGS_PARM1_CORE(ctx);
56+
uprobe_byname2_ran = 3;
4257
return 0;
4358
}
4459

45-
SEC("uretprobe/libc.so.6:free")
60+
SEC("uretprobe/libc.so.6:malloc")
4661
int handle_uretprobe_byname2(struct pt_regs *ctx)
4762
{
48-
uretprobe_byname2_res = 4;
63+
int pid = bpf_get_current_pid_tgid() >> 32;
64+
65+
/* ignore irrelevant invocations */
66+
if (test_pid != pid)
67+
return 0;
68+
uretprobe_byname2_rc = (char *)PT_REGS_RC_CORE(ctx);
69+
uretprobe_byname2_ran = 4;
4970
return 0;
5071
}
5172

0 commit comments

Comments
 (0)