Skip to content

Commit cec8a80

Browse files
lmbtsipa
authored andcommitted
Being able to pass a BTF struct sock* to bpf_sk_release would screw up
reference counting, and must therefore be prevented. Add a test which ensures that this property holds. Signed-off-by: Lorenz Bauer <[email protected]> --- .../bpf/prog_tests/reference_tracking.c | 20 ++++++++++++++++++- .../bpf/progs/test_sk_ref_track_invalid.c | 20 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/progs/test_sk_ref_track_invalid.c
1 parent 86b85e2 commit cec8a80

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// SPDX-License-Identifier: GPL-2.0
22
#include <test_progs.h>
3+
#include "test_sk_ref_track_invalid.skel.h"
34

4-
void test_reference_tracking(void)
5+
static void test_sk_lookup(void)
56
{
67
const char *file = "test_sk_lookup_kern.o";
78
const char *obj_name = "ref_track";
@@ -50,3 +51,20 @@ void test_reference_tracking(void)
5051
cleanup:
5152
bpf_object__close(obj);
5253
}
54+
55+
static void test_sk_release_invalid(void)
56+
{
57+
struct test_sk_ref_track_invalid *skel;
58+
int duration = 0;
59+
60+
skel = test_sk_ref_track_invalid__open_and_load();
61+
if (CHECK(skel, "open_and_load", "verifier accepted sk_release of BTF struct sock*\n"))
62+
test_sk_ref_track_invalid__destroy(skel);
63+
}
64+
65+
void test_reference_tracking(void)
66+
{
67+
test_sk_lookup();
68+
if (test__start_subtest("invalid sk_release"))
69+
test_sk_release_invalid();
70+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (c) 2020 Cloudflare
3+
4+
#include "bpf_iter.h"
5+
#include <bpf/bpf_helpers.h>
6+
#include <bpf/bpf_endian.h>
7+
#include <bpf/bpf_tracing.h>
8+
9+
char _license[] SEC("license") = "GPL";
10+
11+
SEC("iter/bpf_sk_storage_map")
12+
int dump_bpf_sk_storage_map(struct bpf_iter__bpf_sk_storage_map *ctx)
13+
{
14+
struct sock *sk = ctx->sk;
15+
16+
if (sk)
17+
bpf_sk_release((struct bpf_sock *)sk);
18+
19+
return 0;
20+
}

0 commit comments

Comments
 (0)