Skip to content

Commit ddc3d98

Browse files
Jordan RomeAlexei Starovoitov
authored andcommitted
selftests/bpf: Add tests for bpf_copy_from_user_str kfunc.
This adds tests for both the happy path and the error path. Signed-off-by: Jordan Rome <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 65ab5ac commit ddc3d98

File tree

4 files changed

+75
-7
lines changed

4 files changed

+75
-7
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,11 @@ static void test_uprobe_sleepable(struct test_attach_probe *skel)
283283
trigger_func3();
284284

285285
ASSERT_EQ(skel->bss->uprobe_byname3_sleepable_res, 9, "check_uprobe_byname3_sleepable_res");
286-
ASSERT_EQ(skel->bss->uprobe_byname3_res, 10, "check_uprobe_byname3_res");
287-
ASSERT_EQ(skel->bss->uretprobe_byname3_sleepable_res, 11, "check_uretprobe_byname3_sleepable_res");
288-
ASSERT_EQ(skel->bss->uretprobe_byname3_res, 12, "check_uretprobe_byname3_res");
286+
ASSERT_EQ(skel->bss->uprobe_byname3_str_sleepable_res, 10, "check_uprobe_byname3_str_sleepable_res");
287+
ASSERT_EQ(skel->bss->uprobe_byname3_res, 11, "check_uprobe_byname3_res");
288+
ASSERT_EQ(skel->bss->uretprobe_byname3_sleepable_res, 12, "check_uretprobe_byname3_sleepable_res");
289+
ASSERT_EQ(skel->bss->uretprobe_byname3_str_sleepable_res, 13, "check_uretprobe_byname3_str_sleepable_res");
290+
ASSERT_EQ(skel->bss->uretprobe_byname3_res, 14, "check_uretprobe_byname3_res");
289291
}
290292

291293
void test_attach_probe(void)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct read_ret_desc {
2323
{ .name = "probe_read_user_str", .ret = -EFAULT },
2424
{ .name = "copy_from_user", .ret = -EFAULT },
2525
{ .name = "copy_from_user_task", .ret = -EFAULT },
26+
{ .name = "copy_from_user_str", .ret = -EFAULT },
2627
};
2728

2829
void test_read_vsyscall(void)

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
// SPDX-License-Identifier: GPL-2.0
22
/* Copyright (C) 2024. Huawei Technologies Co., Ltd */
3+
#include "vmlinux.h"
34
#include <linux/types.h>
45
#include <bpf/bpf_helpers.h>
56

67
#include "bpf_misc.h"
78

89
int target_pid = 0;
910
void *user_ptr = 0;
10-
int read_ret[8];
11+
int read_ret[9];
1112

1213
char _license[] SEC("license") = "GPL";
1314

15+
/*
16+
* This is the only kfunc, the others are helpers
17+
*/
18+
int bpf_copy_from_user_str(void *dst, u32, const void *, u64) __weak __ksym;
19+
1420
SEC("fentry/" SYS_PREFIX "sys_nanosleep")
1521
int do_probe_read(void *ctx)
1622
{
@@ -40,6 +46,7 @@ int do_copy_from_user(void *ctx)
4046
read_ret[6] = bpf_copy_from_user(buf, sizeof(buf), user_ptr);
4147
read_ret[7] = bpf_copy_from_user_task(buf, sizeof(buf), user_ptr,
4248
bpf_get_current_task_btf(), 0);
49+
read_ret[8] = bpf_copy_from_user_str((char *)buf, sizeof(buf), user_ptr, 0);
4350

4451
return 0;
4552
}

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

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,26 @@
55
#include <bpf/bpf_helpers.h>
66
#include <bpf/bpf_tracing.h>
77
#include <bpf/bpf_core_read.h>
8+
#include <errno.h>
89
#include "bpf_misc.h"
910

11+
u32 dynamic_sz = 1;
1012
int kprobe2_res = 0;
1113
int kretprobe2_res = 0;
1214
int uprobe_byname_res = 0;
1315
int uretprobe_byname_res = 0;
1416
int uprobe_byname2_res = 0;
1517
int uretprobe_byname2_res = 0;
1618
int uprobe_byname3_sleepable_res = 0;
19+
int uprobe_byname3_str_sleepable_res = 0;
1720
int uprobe_byname3_res = 0;
1821
int uretprobe_byname3_sleepable_res = 0;
22+
int uretprobe_byname3_str_sleepable_res = 0;
1923
int uretprobe_byname3_res = 0;
2024
void *user_ptr = 0;
2125

26+
int bpf_copy_from_user_str(void *dst, u32, const void *, u64) __weak __ksym;
27+
2228
SEC("ksyscall/nanosleep")
2329
int BPF_KSYSCALL(handle_kprobe_auto, struct __kernel_timespec *req, struct __kernel_timespec *rem)
2430
{
@@ -87,11 +93,61 @@ static __always_inline bool verify_sleepable_user_copy(void)
8793
return bpf_strncmp(data, sizeof(data), "test_data") == 0;
8894
}
8995

96+
static __always_inline bool verify_sleepable_user_copy_str(void)
97+
{
98+
int ret;
99+
char data_long[20];
100+
char data_long_pad[20];
101+
char data_long_err[20];
102+
char data_short[4];
103+
char data_short_pad[4];
104+
105+
ret = bpf_copy_from_user_str(data_short, sizeof(data_short), user_ptr, 0);
106+
107+
if (bpf_strncmp(data_short, 4, "tes\0") != 0 || ret != 4)
108+
return false;
109+
110+
ret = bpf_copy_from_user_str(data_short_pad, sizeof(data_short_pad), user_ptr, BPF_F_PAD_ZEROS);
111+
112+
if (bpf_strncmp(data_short, 4, "tes\0") != 0 || ret != 4)
113+
return false;
114+
115+
/* Make sure this passes the verifier */
116+
ret = bpf_copy_from_user_str(data_long, dynamic_sz & sizeof(data_long), user_ptr, 0);
117+
118+
if (ret != 0)
119+
return false;
120+
121+
ret = bpf_copy_from_user_str(data_long, sizeof(data_long), user_ptr, 0);
122+
123+
if (bpf_strncmp(data_long, 10, "test_data\0") != 0 || ret != 10)
124+
return false;
125+
126+
ret = bpf_copy_from_user_str(data_long_pad, sizeof(data_long_pad), user_ptr, BPF_F_PAD_ZEROS);
127+
128+
if (bpf_strncmp(data_long_pad, 10, "test_data\0") != 0 || ret != 10 || data_long_pad[19] != '\0')
129+
return false;
130+
131+
ret = bpf_copy_from_user_str(data_long_err, sizeof(data_long_err), (void *)data_long, BPF_F_PAD_ZEROS);
132+
133+
if (ret > 0 || data_long_err[19] != '\0')
134+
return false;
135+
136+
ret = bpf_copy_from_user_str(data_long, sizeof(data_long), user_ptr, 2);
137+
138+
if (ret != -EINVAL)
139+
return false;
140+
141+
return true;
142+
}
143+
90144
SEC("uprobe.s//proc/self/exe:trigger_func3")
91145
int handle_uprobe_byname3_sleepable(struct pt_regs *ctx)
92146
{
93147
if (verify_sleepable_user_copy())
94148
uprobe_byname3_sleepable_res = 9;
149+
if (verify_sleepable_user_copy_str())
150+
uprobe_byname3_str_sleepable_res = 10;
95151
return 0;
96152
}
97153

@@ -102,22 +158,24 @@ int handle_uprobe_byname3_sleepable(struct pt_regs *ctx)
102158
SEC("uprobe//proc/self/exe:trigger_func3")
103159
int handle_uprobe_byname3(struct pt_regs *ctx)
104160
{
105-
uprobe_byname3_res = 10;
161+
uprobe_byname3_res = 11;
106162
return 0;
107163
}
108164

109165
SEC("uretprobe.s//proc/self/exe:trigger_func3")
110166
int handle_uretprobe_byname3_sleepable(struct pt_regs *ctx)
111167
{
112168
if (verify_sleepable_user_copy())
113-
uretprobe_byname3_sleepable_res = 11;
169+
uretprobe_byname3_sleepable_res = 12;
170+
if (verify_sleepable_user_copy_str())
171+
uretprobe_byname3_str_sleepable_res = 13;
114172
return 0;
115173
}
116174

117175
SEC("uretprobe//proc/self/exe:trigger_func3")
118176
int handle_uretprobe_byname3(struct pt_regs *ctx)
119177
{
120-
uretprobe_byname3_res = 12;
178+
uretprobe_byname3_res = 14;
121179
return 0;
122180
}
123181

0 commit comments

Comments
 (0)