1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2021 Facebook */
4 #include <bpf/bpf_helpers.h>
5 #include "../bpf_testmod/bpf_testmod_kfunc.h"
7 struct syscall_test_args {
13 int kfunc_syscall_test_fail(struct syscall_test_args *args)
15 bpf_kfunc_call_test_mem_len_pass1(&args->data, sizeof(*args) + 1);
21 int kfunc_syscall_test_null_fail(struct syscall_test_args *args)
23 /* Must be called with args as a NULL pointer
24 * we do not check for it to have the verifier consider that
25 * the pointer might not be null, and so we can load it.
27 * So the following can not be added:
33 bpf_kfunc_call_test_mem_len_pass1(args, sizeof(*args));
39 int kfunc_call_test_get_mem_fail_rdonly(struct __sk_buff *skb)
41 struct prog_test_ref_kfunc *pt;
46 pt = bpf_kfunc_call_test_acquire(&s);
48 p = bpf_kfunc_call_test_get_rdonly_mem(pt, 2 * sizeof(int));
50 p[0] = 42; /* this is a read-only buffer, so -EACCES */
54 bpf_kfunc_call_test_release(pt);
60 int kfunc_call_test_get_mem_fail_use_after_free(struct __sk_buff *skb)
62 struct prog_test_ref_kfunc *pt;
67 pt = bpf_kfunc_call_test_acquire(&s);
69 p = bpf_kfunc_call_test_get_rdwr_mem(pt, 2 * sizeof(int));
77 bpf_kfunc_call_test_release(pt);
80 ret = p[0]; /* p is not valid anymore */
86 int kfunc_call_test_get_mem_fail_oob(struct __sk_buff *skb)
88 struct prog_test_ref_kfunc *pt;
93 pt = bpf_kfunc_call_test_acquire(&s);
95 p = bpf_kfunc_call_test_get_rdonly_mem(pt, 2 * sizeof(int));
97 ret = p[2 * sizeof(int)]; /* oob access, so -EACCES */
101 bpf_kfunc_call_test_release(pt);
106 int not_const_size = 2 * sizeof(int);
109 int kfunc_call_test_get_mem_fail_not_const(struct __sk_buff *skb)
111 struct prog_test_ref_kfunc *pt;
116 pt = bpf_kfunc_call_test_acquire(&s);
118 p = bpf_kfunc_call_test_get_rdonly_mem(pt, not_const_size); /* non const size, -EINVAL */
124 bpf_kfunc_call_test_release(pt);
130 int kfunc_call_test_mem_acquire_fail(struct __sk_buff *skb)
132 struct prog_test_ref_kfunc *pt;
137 pt = bpf_kfunc_call_test_acquire(&s);
139 /* we are failing on this one, because we are not acquiring a PTR_TO_BTF_ID (a struct ptr) */
140 p = bpf_kfunc_call_test_acq_rdonly_mem(pt, 2 * sizeof(int));
146 bpf_kfunc_call_int_mem_release(p);
148 bpf_kfunc_call_test_release(pt);
153 char _license[] SEC("license") = "GPL";