Skip to content

Commit ee67a8a

Browse files
Florian WestphalKernel Patches Daemon
Florian Westphal
authored and
Kernel Patches Daemon
committed
selftests/bpf: add missing netfilter return value and ctx access tests
Extend prog_tests with two test cases: # ./test_progs --allow=verifier_netfilter_retcode #278/1 verifier_netfilter_retcode/bpf_exit with invalid return code. test1:OK #278/2 verifier_netfilter_retcode/bpf_exit with valid return code. test2:OK #278/3 verifier_netfilter_retcode/bpf_exit with valid return code. test3:OK #278/4 verifier_netfilter_retcode/bpf_exit with invalid return code. test4:OK #278 verifier_netfilter_retcode:OK This checks that only accept and drop (0,1) are permitted. NF_QUEUE could be implemented later if we can guarantee that attachment of such programs can be rejected if they get attached to a pf/hook that doesn't support async reinjection. NF_STOLEN could be implemented via trusted helpers that can guarantee that the skb will eventually be free'd. v4: test case for bpf_nf_ctx access checks, requested by Alexei Starovoitov. v5: also check ctx->{state,skb} can be dereferenced (Alexei). # ./test_progs --allow=verifier_netfilter_ctx #281/1 verifier_netfilter_ctx/netfilter invalid context access, size too short:OK #281/2 verifier_netfilter_ctx/netfilter invalid context access, size too short:OK #281/3 verifier_netfilter_ctx/netfilter invalid context access, past end of ctx:OK #281/4 verifier_netfilter_ctx/netfilter invalid context, write:OK #281/5 verifier_netfilter_ctx/netfilter valid context read and invalid write:OK #281/6 verifier_netfilter_ctx/netfilter test prog with skb and state read access:OK #281/7 verifier_netfilter_ctx/netfilter test prog with skb and state read access @unpriv:OK #281 verifier_netfilter_ctx:OK Summary: 1/7 PASSED, 0 SKIPPED, 0 FAILED This checks: 1/2: partial reads of ctx->{skb,state} are rejected 3. read access past sizeof(ctx) is rejected 4. write to ctx content, e.g. 'ctx->skb = NULL;' is rejected 5. ctx->state content cannot be altered 6. ctx->state and ctx->skb can be dereferenced 7. ... same program fails for unpriv (CAP_NET_ADMIN needed). Link: https://lore.kernel.org/bpf/20230419021152.sjq4gttphzzy6b5f@dhcp-172-26-102-232.dhcp.thefacebook.com/ Link: https://lore.kernel.org/bpf/20230420201655.77kkgi3dh7fesoll@MacBook-Pro-6.local/ Signed-off-by: Florian Westphal <fw@strlen.de>
1 parent b10ceb6 commit ee67a8a

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include "verifier_map_ret_val.skel.h"
3030
#include "verifier_masking.skel.h"
3131
#include "verifier_meta_access.skel.h"
32+
#include "verifier_netfilter_ctx.skel.h"
33+
#include "verifier_netfilter_retcode.skel.h"
3234
#include "verifier_raw_stack.skel.h"
3335
#include "verifier_raw_tp_writable.skel.h"
3436
#include "verifier_reg_equal.skel.h"
@@ -103,6 +105,8 @@ void test_verifier_map_ptr(void) { RUN(verifier_map_ptr); }
103105
void test_verifier_map_ret_val(void) { RUN(verifier_map_ret_val); }
104106
void test_verifier_masking(void) { RUN(verifier_masking); }
105107
void test_verifier_meta_access(void) { RUN(verifier_meta_access); }
108+
void test_verifier_netfilter_ctx(void) { RUN(verifier_netfilter_ctx); }
109+
void test_verifier_netfilter_retcode(void) { RUN(verifier_netfilter_retcode); }
106110
void test_verifier_raw_stack(void) { RUN(verifier_raw_stack); }
107111
void test_verifier_raw_tp_writable(void) { RUN(verifier_raw_tp_writable); }
108112
void test_verifier_reg_equal(void) { RUN(verifier_reg_equal); }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include "vmlinux.h"
4+
5+
#include "bpf_misc.h"
6+
7+
#include <bpf/bpf_endian.h>
8+
#include <bpf/bpf_tracing.h>
9+
#include <bpf/bpf_helpers.h>
10+
11+
SEC("netfilter")
12+
__description("netfilter invalid context access, size too short")
13+
__failure __msg("invalid bpf_context access")
14+
__naked void with_invalid_ctx_access_test1(void)
15+
{
16+
asm volatile (" \
17+
r2 = *(u8*)(r1 + %[__bpf_nf_ctx_state]); \
18+
r0 = 0; \
19+
exit; \
20+
" :
21+
: __imm_const(__bpf_nf_ctx_state, offsetof(struct bpf_nf_ctx, state))
22+
: __clobber_all);
23+
}
24+
25+
SEC("netfilter")
26+
__description("netfilter invalid context access, size too short")
27+
__failure __msg("invalid bpf_context access")
28+
__naked void with_invalid_ctx_access_test2(void)
29+
{
30+
asm volatile (" \
31+
r2 = *(u16*)(r1 + %[__bpf_nf_ctx_skb]); \
32+
r0 = 0; \
33+
exit; \
34+
" :
35+
: __imm_const(__bpf_nf_ctx_skb, offsetof(struct bpf_nf_ctx, skb))
36+
: __clobber_all);
37+
}
38+
39+
SEC("netfilter")
40+
__description("netfilter invalid context access, past end of ctx")
41+
__failure __msg("invalid bpf_context access")
42+
__naked void with_invalid_ctx_access_test3(void)
43+
{
44+
asm volatile (" \
45+
r2 = *(u64*)(r1 + %[__bpf_nf_ctx_size]); \
46+
r0 = 0; \
47+
exit; \
48+
" :
49+
: __imm_const(__bpf_nf_ctx_size, sizeof(struct bpf_nf_ctx))
50+
: __clobber_all);
51+
}
52+
53+
SEC("netfilter")
54+
__description("netfilter invalid context, write")
55+
__failure __msg("invalid bpf_context access")
56+
__naked void with_invalid_ctx_access_test4(void)
57+
{
58+
asm volatile (" \
59+
r2 = r1; \
60+
*(u64*)(r2 + 0) = r1; \
61+
r0 = 1; \
62+
exit; \
63+
" :
64+
: __imm_const(__bpf_nf_ctx_skb, offsetof(struct bpf_nf_ctx, skb))
65+
: __clobber_all);
66+
}
67+
68+
#define NF_DROP 0
69+
#define NF_ACCEPT 1
70+
71+
SEC("netfilter")
72+
__description("netfilter valid context read and invalid write")
73+
__failure __msg("only read is supported")
74+
int with_invalid_ctx_access_test5(struct bpf_nf_ctx *ctx)
75+
{
76+
struct nf_hook_state *state = (void *)ctx->state;
77+
78+
state->sk = NULL;
79+
return NF_ACCEPT;
80+
}
81+
82+
extern int bpf_dynptr_from_skb(struct sk_buff *skb, __u64 flags,
83+
struct bpf_dynptr *ptr__uninit) __ksym;
84+
extern void *bpf_dynptr_slice(const struct bpf_dynptr *ptr, uint32_t offset,
85+
void *buffer, uint32_t buffer__sz) __ksym;
86+
87+
SEC("netfilter")
88+
__description("netfilter test prog with skb and state read access")
89+
__success __failure_unpriv
90+
__retval(0)
91+
int with_valid_ctx_access_test6(struct bpf_nf_ctx *ctx)
92+
{
93+
const struct nf_hook_state *state = ctx->state;
94+
struct sk_buff *skb = ctx->skb;
95+
const struct iphdr *iph;
96+
const struct tcphdr *th;
97+
u8 buffer_iph[20] = {};
98+
u8 buffer_th[40] = {};
99+
struct bpf_dynptr ptr;
100+
uint8_t ihl;
101+
102+
if (skb->len <= 20 || bpf_dynptr_from_skb(skb, 0, &ptr))
103+
return NF_ACCEPT;
104+
105+
iph = bpf_dynptr_slice(&ptr, 0, buffer_iph, sizeof(buffer_iph));
106+
if (!iph)
107+
return NF_ACCEPT;
108+
109+
if (state->pf != 2)
110+
return NF_ACCEPT;
111+
112+
ihl = iph->ihl << 2;
113+
114+
th = bpf_dynptr_slice(&ptr, ihl, buffer_th, sizeof(buffer_th));
115+
if (!th)
116+
return NF_ACCEPT;
117+
118+
return th->dest == bpf_htons(22) ? NF_ACCEPT : NF_DROP;
119+
}
120+
121+
char _license[] SEC("license") = "GPL";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <linux/bpf.h>
4+
#include <bpf/bpf_helpers.h>
5+
#include "bpf_misc.h"
6+
7+
SEC("netfilter")
8+
__description("bpf_exit with invalid return code. test1")
9+
__failure __msg("R0 is not a known value")
10+
__naked void with_invalid_return_code_test1(void)
11+
{
12+
asm volatile (" \
13+
r0 = *(u64*)(r1 + 0); \
14+
exit; \
15+
" ::: __clobber_all);
16+
}
17+
18+
SEC("netfilter")
19+
__description("bpf_exit with valid return code. test2")
20+
__success
21+
__naked void with_valid_return_code_test2(void)
22+
{
23+
asm volatile (" \
24+
r0 = 0; \
25+
exit; \
26+
" ::: __clobber_all);
27+
}
28+
29+
SEC("netfilter")
30+
__description("bpf_exit with valid return code. test3")
31+
__success
32+
__naked void with_valid_return_code_test3(void)
33+
{
34+
asm volatile (" \
35+
r0 = 1; \
36+
exit; \
37+
" ::: __clobber_all);
38+
}
39+
40+
SEC("netfilter")
41+
__description("bpf_exit with invalid return code. test4")
42+
__failure __msg("R0 has value (0x2; 0x0)")
43+
__naked void with_invalid_return_code_test4(void)
44+
{
45+
asm volatile (" \
46+
r0 = 2; \
47+
exit; \
48+
" ::: __clobber_all);
49+
}

0 commit comments

Comments
 (0)