|
| 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 | +struct { |
| 8 | + __uint(type, BPF_MAP_TYPE_ARRAY); |
| 9 | + __uint(max_entries, 8); |
| 10 | + __type(key, __u32); |
| 11 | + __type(value, __u64); |
| 12 | +} map SEC(".maps"); |
| 13 | + |
| 14 | +struct { |
| 15 | + __uint(type, BPF_MAP_TYPE_USER_RINGBUF); |
| 16 | + __uint(max_entries, 8); |
| 17 | +} ringbuf SEC(".maps"); |
| 18 | + |
| 19 | +struct vm_area_struct; |
| 20 | +struct bpf_map; |
| 21 | + |
| 22 | +struct buf_context { |
| 23 | + char *buf; |
| 24 | +}; |
| 25 | + |
| 26 | +struct num_context { |
| 27 | + __u64 i; |
| 28 | +}; |
| 29 | + |
| 30 | +__u8 choice_arr[2] = { 0, 1 }; |
| 31 | + |
| 32 | +static int unsafe_on_2nd_iter_cb(__u32 idx, struct buf_context *ctx) |
| 33 | +{ |
| 34 | + if (idx == 0) { |
| 35 | + ctx->buf = (char *)(0xDEAD); |
| 36 | + return 0; |
| 37 | + } |
| 38 | + |
| 39 | + if (bpf_probe_read_user(ctx->buf, 8, (void *)(0xBADC0FFEE))) |
| 40 | + return 1; |
| 41 | + |
| 42 | + return 0; |
| 43 | +} |
| 44 | + |
| 45 | +SEC("?raw_tp") |
| 46 | +__failure __msg("R1 type=scalar expected=fp") |
| 47 | +int unsafe_on_2nd_iter(void *unused) |
| 48 | +{ |
| 49 | + char buf[4]; |
| 50 | + struct buf_context loop_ctx = { .buf = buf }; |
| 51 | + |
| 52 | + bpf_loop(100, unsafe_on_2nd_iter_cb, &loop_ctx, 0); |
| 53 | + return 0; |
| 54 | +} |
| 55 | + |
| 56 | +static int unsafe_on_zero_iter_cb(__u32 idx, struct num_context *ctx) |
| 57 | +{ |
| 58 | + ctx->i = 0; |
| 59 | + return 0; |
| 60 | +} |
| 61 | + |
| 62 | +SEC("?raw_tp") |
| 63 | +__failure __msg("invalid access to map value, value_size=2 off=32 size=1") |
| 64 | +int unsafe_on_zero_iter(void *unused) |
| 65 | +{ |
| 66 | + struct num_context loop_ctx = { .i = 32 }; |
| 67 | + |
| 68 | + bpf_loop(100, unsafe_on_zero_iter_cb, &loop_ctx, 0); |
| 69 | + return choice_arr[loop_ctx.i]; |
| 70 | +} |
| 71 | + |
| 72 | +static int loop_detection_cb(__u32 idx, struct num_context *ctx) |
| 73 | +{ |
| 74 | + for (;;) {} |
| 75 | + return 0; |
| 76 | +} |
| 77 | + |
| 78 | +SEC("?raw_tp") |
| 79 | +__failure __msg("infinite loop detected") |
| 80 | +int loop_detection(void *unused) |
| 81 | +{ |
| 82 | + struct num_context loop_ctx = { .i = 0 }; |
| 83 | + |
| 84 | + bpf_loop(100, loop_detection_cb, &loop_ctx, 0); |
| 85 | + return 0; |
| 86 | +} |
| 87 | + |
| 88 | +static __always_inline __u64 oob_state_machine(struct num_context *ctx) |
| 89 | +{ |
| 90 | + switch (ctx->i) { |
| 91 | + case 0: |
| 92 | + ctx->i = 1; |
| 93 | + break; |
| 94 | + case 1: |
| 95 | + ctx->i = 32; |
| 96 | + break; |
| 97 | + } |
| 98 | + return 0; |
| 99 | +} |
| 100 | + |
| 101 | +static __u64 for_each_map_elem_cb(struct bpf_map *map, __u32 *key, __u64 *val, void *data) |
| 102 | +{ |
| 103 | + return oob_state_machine(data); |
| 104 | +} |
| 105 | + |
| 106 | +SEC("?raw_tp") |
| 107 | +__failure __msg("invalid access to map value, value_size=2 off=32 size=1") |
| 108 | +int unsafe_for_each_map_elem(void *unused) |
| 109 | +{ |
| 110 | + struct num_context loop_ctx = { .i = 0 }; |
| 111 | + |
| 112 | + bpf_for_each_map_elem(&map, for_each_map_elem_cb, &loop_ctx, 0); |
| 113 | + return choice_arr[loop_ctx.i]; |
| 114 | +} |
| 115 | + |
| 116 | +static __u64 ringbuf_drain_cb(struct bpf_dynptr *dynptr, void *data) |
| 117 | +{ |
| 118 | + return oob_state_machine(data); |
| 119 | +} |
| 120 | + |
| 121 | +SEC("?raw_tp") |
| 122 | +__failure __msg("invalid access to map value, value_size=2 off=32 size=1") |
| 123 | +int unsafe_ringbuf_drain(void *unused) |
| 124 | +{ |
| 125 | + struct num_context loop_ctx = { .i = 0 }; |
| 126 | + |
| 127 | + bpf_user_ringbuf_drain(&ringbuf, ringbuf_drain_cb, &loop_ctx, 0); |
| 128 | + return choice_arr[loop_ctx.i]; |
| 129 | +} |
| 130 | + |
| 131 | +static __u64 find_vma_cb(struct task_struct *task, struct vm_area_struct *vma, void *data) |
| 132 | +{ |
| 133 | + return oob_state_machine(data); |
| 134 | +} |
| 135 | + |
| 136 | +SEC("?raw_tp") |
| 137 | +__failure __msg("invalid access to map value, value_size=2 off=32 size=1") |
| 138 | +int unsafe_find_vma(void *unused) |
| 139 | +{ |
| 140 | + struct task_struct *task = bpf_get_current_task_btf(); |
| 141 | + struct num_context loop_ctx = { .i = 0 }; |
| 142 | + |
| 143 | + bpf_find_vma(task, 0, find_vma_cb, &loop_ctx, 0); |
| 144 | + return choice_arr[loop_ctx.i]; |
| 145 | +} |
| 146 | + |
| 147 | +char _license[] SEC("license") = "GPL"; |
0 commit comments