1 // SPDX-License-Identifier: GPL-2.0
7 #include <bpf/bpf_endian.h>
8 #include <bpf/bpf_tracing.h>
9 #include <bpf/bpf_helpers.h>
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)
17 r2 = *(u8*)(r1 + %[__bpf_nf_ctx_state]); \
21 : __imm_const(__bpf_nf_ctx_state, offsetof(struct bpf_nf_ctx, state))
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)
31 r2 = *(u16*)(r1 + %[__bpf_nf_ctx_skb]); \
35 : __imm_const(__bpf_nf_ctx_skb, offsetof(struct bpf_nf_ctx, skb))
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)
45 r2 = *(u64*)(r1 + %[__bpf_nf_ctx_size]); \
49 : __imm_const(__bpf_nf_ctx_size, sizeof(struct bpf_nf_ctx))
54 __description("netfilter invalid context, write")
55 __failure __msg("invalid bpf_context access")
56 __naked void with_invalid_ctx_access_test4(void)
60 *(u64*)(r2 + 0) = r1; \
64 : __imm_const(__bpf_nf_ctx_skb, offsetof(struct bpf_nf_ctx, skb))
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)
76 struct nf_hook_state *state = (void *)ctx->state;
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;
88 __description("netfilter test prog with skb and state read access")
89 __success __failure_unpriv
91 int with_valid_ctx_access_test6(struct bpf_nf_ctx *ctx)
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;
102 if (skb->len <= 20 || bpf_dynptr_from_skb(skb, 0, &ptr))
105 iph = bpf_dynptr_slice(&ptr, 0, buffer_iph, sizeof(buffer_iph));
114 th = bpf_dynptr_slice(&ptr, ihl, buffer_th, sizeof(buffer_th));
118 return th->dest == bpf_htons(22) ? NF_ACCEPT : NF_DROP;
121 char _license[] SEC("license") = "GPL";