1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */
5 #include <bpf/bpf_helpers.h>
8 char _license[] SEC("license") = "GPL";
18 __uint(type, BPF_MAP_TYPE_USER_RINGBUF);
19 __uint(max_entries, 4096);
20 } user_ringbuf SEC(".maps");
23 __uint(type, BPF_MAP_TYPE_RINGBUF);
24 __uint(max_entries, 2);
25 } ringbuf SEC(".maps");
30 bad_access1(struct bpf_dynptr *dynptr, void *context)
32 const struct sample *sample;
34 sample = bpf_dynptr_data(dynptr - 1, 0, sizeof(*sample));
35 bpf_printk("Was able to pass bad pointer %lx\n", (__u64)dynptr - 1);
40 /* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should
41 * not be able to read before the pointer.
44 __failure __msg("negative offset dynptr_ptr ptr")
45 int user_ringbuf_callback_bad_access1(void *ctx)
47 bpf_user_ringbuf_drain(&user_ringbuf, bad_access1, NULL, 0);
53 bad_access2(struct bpf_dynptr *dynptr, void *context)
55 const struct sample *sample;
57 sample = bpf_dynptr_data(dynptr + 1, 0, sizeof(*sample));
58 bpf_printk("Was able to pass bad pointer %lx\n", (__u64)dynptr + 1);
63 /* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should
64 * not be able to read past the end of the pointer.
67 __failure __msg("dereference of modified dynptr_ptr ptr")
68 int user_ringbuf_callback_bad_access2(void *ctx)
70 bpf_user_ringbuf_drain(&user_ringbuf, bad_access2, NULL, 0);
76 write_forbidden(struct bpf_dynptr *dynptr, void *context)
78 *((long *)dynptr) = 0;
83 /* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should
84 * not be able to write to that pointer.
87 __failure __msg("invalid mem access 'dynptr_ptr'")
88 int user_ringbuf_callback_write_forbidden(void *ctx)
90 bpf_user_ringbuf_drain(&user_ringbuf, write_forbidden, NULL, 0);
96 null_context_write(struct bpf_dynptr *dynptr, void *context)
98 *((__u64 *)context) = 0;
103 /* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should
104 * not be able to write to that pointer.
107 __failure __msg("invalid mem access 'scalar'")
108 int user_ringbuf_callback_null_context_write(void *ctx)
110 bpf_user_ringbuf_drain(&user_ringbuf, null_context_write, NULL, 0);
116 null_context_read(struct bpf_dynptr *dynptr, void *context)
118 __u64 id = *((__u64 *)context);
120 bpf_printk("Read id %lu\n", id);
125 /* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should
126 * not be able to write to that pointer.
129 __failure __msg("invalid mem access 'scalar'")
130 int user_ringbuf_callback_null_context_read(void *ctx)
132 bpf_user_ringbuf_drain(&user_ringbuf, null_context_read, NULL, 0);
138 try_discard_dynptr(struct bpf_dynptr *dynptr, void *context)
140 bpf_ringbuf_discard_dynptr(dynptr, 0);
145 /* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should
146 * not be able to read past the end of the pointer.
149 __failure __msg("cannot release unowned const bpf_dynptr")
150 int user_ringbuf_callback_discard_dynptr(void *ctx)
152 bpf_user_ringbuf_drain(&user_ringbuf, try_discard_dynptr, NULL, 0);
158 try_submit_dynptr(struct bpf_dynptr *dynptr, void *context)
160 bpf_ringbuf_submit_dynptr(dynptr, 0);
165 /* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should
166 * not be able to read past the end of the pointer.
169 __failure __msg("cannot release unowned const bpf_dynptr")
170 int user_ringbuf_callback_submit_dynptr(void *ctx)
172 bpf_user_ringbuf_drain(&user_ringbuf, try_submit_dynptr, NULL, 0);
178 invalid_drain_callback_return(struct bpf_dynptr *dynptr, void *context)
183 /* A callback that accesses a dynptr in a bpf_user_ringbuf_drain callback should
184 * not be able to write to that pointer.
187 __failure __msg("At callback return the register R0 has ")
188 int user_ringbuf_callback_invalid_return(void *ctx)
190 bpf_user_ringbuf_drain(&user_ringbuf, invalid_drain_callback_return, NULL, 0);
196 try_reinit_dynptr_mem(struct bpf_dynptr *dynptr, void *context)
198 bpf_dynptr_from_mem(&map_value, 4, 0, dynptr);
203 try_reinit_dynptr_ringbuf(struct bpf_dynptr *dynptr, void *context)
205 bpf_ringbuf_reserve_dynptr(&ringbuf, 8, 0, dynptr);
210 __failure __msg("Dynptr has to be an uninitialized dynptr")
211 int user_ringbuf_callback_reinit_dynptr_mem(void *ctx)
213 bpf_user_ringbuf_drain(&user_ringbuf, try_reinit_dynptr_mem, NULL, 0);
218 __failure __msg("Dynptr has to be an uninitialized dynptr")
219 int user_ringbuf_callback_reinit_dynptr_ringbuf(void *ctx)
221 bpf_user_ringbuf_drain(&user_ringbuf, try_reinit_dynptr_ringbuf, NULL, 0);
225 __noinline long global_call_bpf_dynptr_data(struct bpf_dynptr *dynptr)
227 bpf_dynptr_data(dynptr, 0xA, 0xA);
231 static long callback_adjust_bpf_dynptr_reg_off(struct bpf_dynptr *dynptr,
234 global_call_bpf_dynptr_data(dynptr += 1024);
239 __failure __msg("dereference of modified dynptr_ptr ptr R1 off=16384 disallowed")
240 int user_ringbuf_callback_const_ptr_to_dynptr_reg_off(void *ctx)
242 bpf_user_ringbuf_drain(&user_ringbuf,
243 callback_adjust_bpf_dynptr_reg_off, NULL, 0);