|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +#include "vmlinux.h" |
| 4 | + |
| 5 | +#include "bpf_misc.h" |
| 6 | + |
| 7 | +#include <bpf/bpf_tracing.h> |
| 8 | +#include <bpf/bpf_helpers.h> |
| 9 | + |
| 10 | +SEC("netfilter") |
| 11 | +__description("netfilter invalid context access, size too short") |
| 12 | +__failure __msg("invalid bpf_context access") |
| 13 | +__naked void with_invalid_ctx_access_test1(void) |
| 14 | +{ |
| 15 | + asm volatile (" \ |
| 16 | + r2 = *(u8*)(r1 + %[__bpf_nf_ctx_state]); \ |
| 17 | + r0 = 0; \ |
| 18 | + exit; \ |
| 19 | +" : |
| 20 | + : __imm_const(__bpf_nf_ctx_state, offsetof(struct bpf_nf_ctx, state)) |
| 21 | + : __clobber_all); |
| 22 | +} |
| 23 | + |
| 24 | +SEC("netfilter") |
| 25 | +__description("netfilter invalid context access, size too short") |
| 26 | +__failure __msg("invalid bpf_context access") |
| 27 | +__naked void with_invalid_ctx_access_test2(void) |
| 28 | +{ |
| 29 | + asm volatile (" \ |
| 30 | + r2 = *(u16*)(r1 + %[__bpf_nf_ctx_skb]); \ |
| 31 | + r0 = 0; \ |
| 32 | + exit; \ |
| 33 | +" : |
| 34 | + : __imm_const(__bpf_nf_ctx_skb, offsetof(struct bpf_nf_ctx, skb)) |
| 35 | + : __clobber_all); |
| 36 | +} |
| 37 | + |
| 38 | +SEC("netfilter") |
| 39 | +__description("netfilter invalid context access, past end of ctx") |
| 40 | +__failure __msg("invalid bpf_context access") |
| 41 | +__naked void with_invalid_ctx_access_test3(void) |
| 42 | +{ |
| 43 | + asm volatile (" \ |
| 44 | + r2 = *(u64*)(r1 + %[__bpf_nf_ctx_size]); \ |
| 45 | + r0 = 0; \ |
| 46 | + exit; \ |
| 47 | +" : |
| 48 | + : __imm_const(__bpf_nf_ctx_size, sizeof(struct bpf_nf_ctx)) |
| 49 | + : __clobber_all); |
| 50 | +} |
| 51 | + |
| 52 | +SEC("netfilter") |
| 53 | +__description("netfilter invalid context, write") |
| 54 | +__failure __msg("invalid bpf_context access") |
| 55 | +__naked void with_invalid_ctx_access_test4(void) |
| 56 | +{ |
| 57 | + asm volatile (" \ |
| 58 | + r2 = r1; \ |
| 59 | + *(u64*)(r2 + 0) = r1; \ |
| 60 | + r0 = 1; \ |
| 61 | + exit; \ |
| 62 | +" : |
| 63 | + : __imm_const(__bpf_nf_ctx_skb, offsetof(struct bpf_nf_ctx, skb)) |
| 64 | + : __clobber_all); |
| 65 | +} |
| 66 | + |
| 67 | +SEC("netfilter") |
| 68 | +__description("netfilter valid context access") |
| 69 | +__success __failure_unpriv |
| 70 | +__retval(1) |
| 71 | +__naked void with_invalid_ctx_access_test5(void) |
| 72 | +{ |
| 73 | + asm volatile (" \ |
| 74 | + r2 = *(u64*)(r1 + %[__bpf_nf_ctx_state]); \ |
| 75 | + r1 = *(u64*)(r1 + %[__bpf_nf_ctx_skb]); \ |
| 76 | + r0 = 1; \ |
| 77 | + exit; \ |
| 78 | +" : |
| 79 | + : __imm_const(__bpf_nf_ctx_state, offsetof(struct bpf_nf_ctx, state)), |
| 80 | + __imm_const(__bpf_nf_ctx_skb, offsetof(struct bpf_nf_ctx, skb)) |
| 81 | + : __clobber_all); |
| 82 | +} |
0 commit comments