Skip to content

Commit 83dff60

Browse files
Philo LuMartin KaFai Lau
Philo Lu
authored and
Martin KaFai Lau
committed
selftests/bpf: Expand skb dynptr selftests for tp_btf
Add 3 test cases for skb dynptr used in tp_btf: - test_dynptr_skb_tp_btf: use skb dynptr in tp_btf and make sure it is read-only. - skb_invalid_ctx_fentry/skb_invalid_ctx_fexit: bpf_dynptr_from_skb should fail in fentry/fexit. In test_dynptr_skb_tp_btf, to trigger the tracepoint in kfree_skb, test_pkt_access is used for its test_run, as in kfree_skb.c. Because the test process is different from others, a new setup type is defined, i.e., SETUP_SKB_PROG_TP. The result is like: $ ./test_progs -t 'dynptr/test_dynptr_skb_tp_btf' torvalds#84/14 dynptr/test_dynptr_skb_tp_btf:OK torvalds#84 dynptr:OK torvalds#127 kfunc_dynptr_param:OK Summary: 2/1 PASSED, 0 SKIPPED, 0 FAILED $ ./test_progs -t 'dynptr/skb_invalid_ctx_f' torvalds#84/85 dynptr/skb_invalid_ctx_fentry:OK torvalds#84/86 dynptr/skb_invalid_ctx_fexit:OK torvalds#84 dynptr:OK torvalds#127 kfunc_dynptr_param:OK Summary: 2/2 PASSED, 0 SKIPPED, 0 FAILED Also fix two coding style nits (change spaces to tabs). Signed-off-by: Philo Lu <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]>
1 parent ffc8386 commit 83dff60

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

Diff for: tools/testing/selftests/bpf/prog_tests/dynptr.c

+35-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
enum test_setup_type {
1010
SETUP_SYSCALL_SLEEP,
1111
SETUP_SKB_PROG,
12+
SETUP_SKB_PROG_TP,
1213
};
1314

1415
static struct {
@@ -28,14 +29,15 @@ static struct {
2829
{"test_dynptr_clone", SETUP_SKB_PROG},
2930
{"test_dynptr_skb_no_buff", SETUP_SKB_PROG},
3031
{"test_dynptr_skb_strcmp", SETUP_SKB_PROG},
32+
{"test_dynptr_skb_tp_btf", SETUP_SKB_PROG_TP},
3133
};
3234

3335
static void verify_success(const char *prog_name, enum test_setup_type setup_type)
3436
{
3537
struct dynptr_success *skel;
3638
struct bpf_program *prog;
3739
struct bpf_link *link;
38-
int err;
40+
int err;
3941

4042
skel = dynptr_success__open();
4143
if (!ASSERT_OK_PTR(skel, "dynptr_success__open"))
@@ -47,7 +49,7 @@ static void verify_success(const char *prog_name, enum test_setup_type setup_typ
4749
if (!ASSERT_OK_PTR(prog, "bpf_object__find_program_by_name"))
4850
goto cleanup;
4951

50-
bpf_program__set_autoload(prog, true);
52+
bpf_program__set_autoload(prog, true);
5153

5254
err = dynptr_success__load(skel);
5355
if (!ASSERT_OK(err, "dynptr_success__load"))
@@ -87,6 +89,37 @@ static void verify_success(const char *prog_name, enum test_setup_type setup_typ
8789

8890
break;
8991
}
92+
case SETUP_SKB_PROG_TP:
93+
{
94+
struct __sk_buff skb = {};
95+
struct bpf_object *obj;
96+
int aux_prog_fd;
97+
98+
/* Just use its test_run to trigger kfree_skb tracepoint */
99+
err = bpf_prog_test_load("./test_pkt_access.bpf.o", BPF_PROG_TYPE_SCHED_CLS,
100+
&obj, &aux_prog_fd);
101+
if (!ASSERT_OK(err, "prog_load sched cls"))
102+
goto cleanup;
103+
104+
LIBBPF_OPTS(bpf_test_run_opts, topts,
105+
.data_in = &pkt_v4,
106+
.data_size_in = sizeof(pkt_v4),
107+
.ctx_in = &skb,
108+
.ctx_size_in = sizeof(skb),
109+
);
110+
111+
link = bpf_program__attach(prog);
112+
if (!ASSERT_OK_PTR(link, "bpf_program__attach"))
113+
goto cleanup;
114+
115+
err = bpf_prog_test_run_opts(aux_prog_fd, &topts);
116+
bpf_link__destroy(link);
117+
118+
if (!ASSERT_OK(err, "test_run"))
119+
goto cleanup;
120+
121+
break;
122+
}
90123
}
91124

92125
ASSERT_EQ(skel->bss->err, 0, "err");

Diff for: tools/testing/selftests/bpf/progs/dynptr_fail.c

+25
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <stdbool.h>
77
#include <linux/bpf.h>
88
#include <bpf/bpf_helpers.h>
9+
#include <bpf/bpf_tracing.h>
910
#include <linux/if_ether.h>
1011
#include "bpf_misc.h"
1112
#include "bpf_kfuncs.h"
@@ -1254,6 +1255,30 @@ int skb_invalid_ctx(void *ctx)
12541255
return 0;
12551256
}
12561257

1258+
SEC("fentry/skb_tx_error")
1259+
__failure __msg("must be referenced or trusted")
1260+
int BPF_PROG(skb_invalid_ctx_fentry, void *skb)
1261+
{
1262+
struct bpf_dynptr ptr;
1263+
1264+
/* this should fail */
1265+
bpf_dynptr_from_skb(skb, 0, &ptr);
1266+
1267+
return 0;
1268+
}
1269+
1270+
SEC("fexit/skb_tx_error")
1271+
__failure __msg("must be referenced or trusted")
1272+
int BPF_PROG(skb_invalid_ctx_fexit, void *skb)
1273+
{
1274+
struct bpf_dynptr ptr;
1275+
1276+
/* this should fail */
1277+
bpf_dynptr_from_skb(skb, 0, &ptr);
1278+
1279+
return 0;
1280+
}
1281+
12571282
/* Reject writes to dynptr slot for uninit arg */
12581283
SEC("?raw_tp")
12591284
__failure __msg("potential write to dynptr at off=-16")

Diff for: tools/testing/selftests/bpf/progs/dynptr_success.c

+23
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <stdbool.h>
66
#include <linux/bpf.h>
77
#include <bpf/bpf_helpers.h>
8+
#include <bpf/bpf_tracing.h>
89
#include "bpf_misc.h"
910
#include "bpf_kfuncs.h"
1011
#include "errno.h"
@@ -544,3 +545,25 @@ int test_dynptr_skb_strcmp(struct __sk_buff *skb)
544545

545546
return 1;
546547
}
548+
549+
SEC("tp_btf/kfree_skb")
550+
int BPF_PROG(test_dynptr_skb_tp_btf, void *skb, void *location)
551+
{
552+
__u8 write_data[2] = {1, 2};
553+
struct bpf_dynptr ptr;
554+
int ret;
555+
556+
if (bpf_dynptr_from_skb(skb, 0, &ptr)) {
557+
err = 1;
558+
return 1;
559+
}
560+
561+
/* since tp_btf skbs are read only, writes should fail */
562+
ret = bpf_dynptr_write(&ptr, 0, write_data, sizeof(write_data), 0);
563+
if (ret != -EINVAL) {
564+
err = 2;
565+
return 1;
566+
}
567+
568+
return 1;
569+
}

0 commit comments

Comments
 (0)