Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Adding test for arg dereference in extension trace

Adding test that setup following program:

SEC("classifier/test_pkt_md_access")
int test_pkt_md_access(struct __sk_buff *skb)

with its extension:

SEC("freplace/test_pkt_md_access")
int test_pkt_md_access_new(struct __sk_buff *skb)

and tracing that extension with:

SEC("fentry/test_pkt_md_access_new")
int BPF_PROG(fentry, struct sk_buff *skb)

The test verifies that the tracing program can
dereference skb argument properly.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andriin@fb.com>
Link: https://lore.kernel.org/bpf/160138355603.48470.9072073357530773228.stgit@toke.dk

authored by

Jiri Olsa and committed by
Alexei Starovoitov
17d3f386 f6429476

+154
+111
tools/testing/selftests/bpf/prog_tests/trace_ext.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + 3 + #define _GNU_SOURCE 4 + #include <test_progs.h> 5 + #include <network_helpers.h> 6 + #include <sys/stat.h> 7 + #include <linux/sched.h> 8 + #include <sys/syscall.h> 9 + 10 + #include "test_pkt_md_access.skel.h" 11 + #include "test_trace_ext.skel.h" 12 + #include "test_trace_ext_tracing.skel.h" 13 + 14 + static __u32 duration; 15 + 16 + void test_trace_ext(void) 17 + { 18 + struct test_pkt_md_access *skel_pkt = NULL; 19 + struct test_trace_ext_tracing *skel_trace = NULL; 20 + struct test_trace_ext_tracing__bss *bss_trace; 21 + struct test_trace_ext *skel_ext = NULL; 22 + struct test_trace_ext__bss *bss_ext; 23 + int err, pkt_fd, ext_fd; 24 + struct bpf_program *prog; 25 + char buf[100]; 26 + __u32 retval; 27 + __u64 len; 28 + 29 + /* open/load/attach test_pkt_md_access */ 30 + skel_pkt = test_pkt_md_access__open_and_load(); 31 + if (CHECK(!skel_pkt, "setup", "classifier/test_pkt_md_access open failed\n")) 32 + goto cleanup; 33 + 34 + err = test_pkt_md_access__attach(skel_pkt); 35 + if (CHECK(err, "setup", "classifier/test_pkt_md_access attach failed: %d\n", err)) 36 + goto cleanup; 37 + 38 + prog = skel_pkt->progs.test_pkt_md_access; 39 + pkt_fd = bpf_program__fd(prog); 40 + 41 + /* open extension */ 42 + skel_ext = test_trace_ext__open(); 43 + if (CHECK(!skel_ext, "setup", "freplace/test_pkt_md_access open failed\n")) 44 + goto cleanup; 45 + 46 + /* set extension's attach target - test_pkt_md_access */ 47 + prog = skel_ext->progs.test_pkt_md_access_new; 48 + bpf_program__set_attach_target(prog, pkt_fd, "test_pkt_md_access"); 49 + 50 + /* load/attach extension */ 51 + err = test_trace_ext__load(skel_ext); 52 + if (CHECK(err, "setup", "freplace/test_pkt_md_access load failed\n")) { 53 + libbpf_strerror(err, buf, sizeof(buf)); 54 + fprintf(stderr, "%s\n", buf); 55 + goto cleanup; 56 + } 57 + 58 + err = test_trace_ext__attach(skel_ext); 59 + if (CHECK(err, "setup", "freplace/test_pkt_md_access attach failed: %d\n", err)) 60 + goto cleanup; 61 + 62 + prog = skel_ext->progs.test_pkt_md_access_new; 63 + ext_fd = bpf_program__fd(prog); 64 + 65 + /* open tracing */ 66 + skel_trace = test_trace_ext_tracing__open(); 67 + if (CHECK(!skel_trace, "setup", "tracing/test_pkt_md_access_new open failed\n")) 68 + goto cleanup; 69 + 70 + /* set tracing's attach target - fentry */ 71 + prog = skel_trace->progs.fentry; 72 + bpf_program__set_attach_target(prog, ext_fd, "test_pkt_md_access_new"); 73 + 74 + /* set tracing's attach target - fexit */ 75 + prog = skel_trace->progs.fexit; 76 + bpf_program__set_attach_target(prog, ext_fd, "test_pkt_md_access_new"); 77 + 78 + /* load/attach tracing */ 79 + err = test_trace_ext_tracing__load(skel_trace); 80 + if (CHECK(err, "setup", "tracing/test_pkt_md_access_new load failed\n")) { 81 + libbpf_strerror(err, buf, sizeof(buf)); 82 + fprintf(stderr, "%s\n", buf); 83 + goto cleanup; 84 + } 85 + 86 + err = test_trace_ext_tracing__attach(skel_trace); 87 + if (CHECK(err, "setup", "tracing/test_pkt_md_access_new attach failed: %d\n", err)) 88 + goto cleanup; 89 + 90 + /* trigger the test */ 91 + err = bpf_prog_test_run(pkt_fd, 1, &pkt_v4, sizeof(pkt_v4), 92 + NULL, NULL, &retval, &duration); 93 + CHECK(err || retval, "run", "err %d errno %d retval %d\n", err, errno, retval); 94 + 95 + bss_ext = skel_ext->bss; 96 + bss_trace = skel_trace->bss; 97 + 98 + len = bss_ext->ext_called; 99 + 100 + CHECK(bss_ext->ext_called == 0, 101 + "check", "failed to trigger freplace/test_pkt_md_access\n"); 102 + CHECK(bss_trace->fentry_called != len, 103 + "check", "failed to trigger fentry/test_pkt_md_access_new\n"); 104 + CHECK(bss_trace->fexit_called != len, 105 + "check", "failed to trigger fexit/test_pkt_md_access_new\n"); 106 + 107 + cleanup: 108 + test_trace_ext_tracing__destroy(skel_trace); 109 + test_trace_ext__destroy(skel_ext); 110 + test_pkt_md_access__destroy(skel_pkt); 111 + }
+18
tools/testing/selftests/bpf/progs/test_trace_ext.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + // Copyright (c) 2019 Facebook 3 + #include <linux/bpf.h> 4 + #include <stdbool.h> 5 + #include <bpf/bpf_helpers.h> 6 + #include <bpf/bpf_endian.h> 7 + #include <bpf/bpf_tracing.h> 8 + 9 + __u64 ext_called = 0; 10 + 11 + SEC("freplace/test_pkt_md_access") 12 + int test_pkt_md_access_new(struct __sk_buff *skb) 13 + { 14 + ext_called = skb->len; 15 + return 0; 16 + } 17 + 18 + char _license[] SEC("license") = "GPL";
+25
tools/testing/selftests/bpf/progs/test_trace_ext_tracing.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + 3 + #include "vmlinux.h" 4 + #include <bpf/bpf_helpers.h> 5 + #include <bpf/bpf_tracing.h> 6 + 7 + __u64 fentry_called = 0; 8 + 9 + SEC("fentry/test_pkt_md_access_new") 10 + int BPF_PROG(fentry, struct sk_buff *skb) 11 + { 12 + fentry_called = skb->len; 13 + return 0; 14 + } 15 + 16 + __u64 fexit_called = 0; 17 + 18 + SEC("fexit/test_pkt_md_access_new") 19 + int BPF_PROG(fexit, struct sk_buff *skb) 20 + { 21 + fexit_called = skb->len; 22 + return 0; 23 + } 24 + 25 + char _license[] SEC("license") = "GPL";