123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194// SPDX-License-Identifier: GPL-2.0 #include "vmlinux.h" #include "bpf_misc.h" #include <bpf/bpf_endian.h> #include <bpf/bpf_tracing.h> #include <bpf/bpf_helpers.h> SEC("netfilter") __description("netfilter invalid context access, size too short") __failure __msg("invalid bpf_context access") __naked void with_invalid_ctx_access_test1(void) { asm volatile (" \ r2 = *(u8*)(r1 + %[__bpf_nf_ctx_state]); \ r0 = 0; \ exit; \ " : : __imm_const(__bpf_nf_ctx_state, offsetof(struct bpf_nf_ctx, state)) : __clobber_all); } SEC("netfilter") __description("netfilter invalid context access, size too short") __failure __msg("invalid bpf_context access") __naked void with_invalid_ctx_access_test2(void) { asm volatile (" \ r2 = *(u16*)(r1 + %[__bpf_nf_ctx_skb]); \ r0 = 0; \ exit; \ " : : __imm_const(__bpf_nf_ctx_skb, offsetof(struct bpf_nf_ctx, skb)) : __clobber_all); } SEC("netfilter") __description("netfilter invalid context access, past end of ctx") __failure __msg("invalid bpf_context access") __naked void with_invalid_ctx_access_test3(void) { asm volatile (" \ r2 = *(u64*)(r1 + %[__bpf_nf_ctx_size]); \ r0 = 0; \ exit; \ " : : __imm_const(__bpf_nf_ctx_size, sizeof(struct bpf_nf_ctx)) : __clobber_all); } SEC("netfilter") __description("netfilter invalid context, write") __failure __msg("invalid bpf_context access") __naked void with_invalid_ctx_access_test4(void) { asm volatile (" \ r2 = r1; \ *(u64*)(r2 + 0) = r1; \ r0 = 1; \ exit; \ " : : __imm_const(__bpf_nf_ctx_skb, offsetof(struct bpf_nf_ctx, skb)) : __clobber_all); } #define NF_DROP 0 #define NF_ACCEPT 1 SEC("netfilter") __description("netfilter valid context read and invalid write") __failure __msg("only read is supported") int with_invalid_ctx_access_test5(struct bpf_nf_ctx *ctx) { struct nf_hook_state *state = (void *)ctx->state; state->sk = NULL; return NF_ACCEPT; } SEC("netfilter") __description("netfilter test prog with skb and state read access") __success __failure_unpriv __retval(0) int with_valid_ctx_access_test6(struct bpf_nf_ctx *ctx) { struct __sk_buff *skb = (struct __sk_buff *)ctx->skb; const struct nf_hook_state *state = ctx->state; const struct iphdr *iph; const struct tcphdr *th; u8 buffer_iph[20] = {}; u8 buffer_th[40] = {}; struct bpf_dynptr ptr; uint8_t ihl; if (ctx->skb->len <= 20 || bpf_dynptr_from_skb(skb, 0, &ptr)) return NF_ACCEPT; iph = bpf_dynptr_slice(&ptr, 0, buffer_iph, sizeof(buffer_iph)); if (!iph) return NF_ACCEPT; if (state->pf != 2) return NF_ACCEPT; ihl = iph->ihl << 2; th = bpf_dynptr_slice(&ptr, ihl, buffer_th, sizeof(buffer_th)); if (!th) return NF_ACCEPT; return th->dest == bpf_htons(22) ? NF_ACCEPT : NF_DROP; } SEC("netfilter") __description("netfilter test prog with skb write access") __failure __msg("only read is supported") int skb_len_write(struct bpf_nf_ctx *ctx) { ctx->skb->len = 1; return 1; } SEC("netfilter") __description("netfilter test prog with skb data write access") __failure __msg("cannot write into rdonly_untrusted_mem") int skb_data_write(struct bpf_nf_ctx *ctx) { ctx->skb->data[0] = 0; return 1; } SEC("netfilter") __description("netfilter test prog with bpf_dynptr_write") __success __failure_unpriv __retval(0) int with_dynptr_write(struct bpf_nf_ctx *ctx) { struct __sk_buff *skb = (struct __sk_buff *)ctx->skb; struct bpf_dynptr ptr; u8 buffer[1] = {}; if (bpf_dynptr_from_skb(skb, 0, &ptr)) return 1; if (bpf_dynptr_write(&ptr, 0, buffer, sizeof(buffer), 0)) return 0; /* must always fail */ return 1; } SEC("netfilter") __description("netfilter test prog with bpf_dynptr_slice_rdwr") __failure __msg("the prog does not allow writes to packet data") int with_dynptr_rdwr(struct bpf_nf_ctx *ctx) { struct __sk_buff *skb = (struct __sk_buff *)ctx->skb; u8 buffer_iph[20] = {}; struct bpf_dynptr ptr; struct iphdr *iph; if (bpf_dynptr_from_skb(skb, 0, &ptr)) return 1; iph = bpf_dynptr_slice_rdwr(&ptr, 0, buffer_iph, sizeof(buffer_iph)); if (!iph) return 0; return 1; } SEC("netfilter") __description("netfilter test prog with bpf_dynptr_slice + write") __failure __msg("cannot write into rdonly_mem") int with_dynptr_store(struct bpf_nf_ctx *ctx) { struct __sk_buff *skb = (struct __sk_buff *)ctx->skb; u8 buffer_iph[20] = {}; struct bpf_dynptr ptr; struct iphdr *iph; if (bpf_dynptr_from_skb(skb, 0, &ptr)) return 1; iph = bpf_dynptr_slice(&ptr, 0, buffer_iph, sizeof(buffer_iph)); if (!iph) return 0; iph->protocol = 42; return 1; } char _license[] SEC("license") = "GPL";