1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <linux/bpf.h> 4 #include <bpf/bpf_helpers.h> 5 #include <bpf/bpf_tracing.h> 6 #include "bpf_misc.h" 7 8 SEC("lsm_cgroup/socket_create") 9 __description("lsm_cgroup bpf_set_retval success") 10 __success 11 int BPF_PROG(lsm_cgroup_set_retval_zero_valid, int family, int type, int protocol, int kern) 12 { 13 bpf_set_retval(0); 14 return 0; 15 } 16 17 SEC("lsm_cgroup/socket_create") 18 __description("lsm_cgroup bpf_set_retval valid errno") 19 __success 20 int BPF_PROG(lsm_cgroup_set_retval_negative_valid, int family, int type, int protocol, int kern) 21 { 22 bpf_set_retval(-12); 23 return 0; 24 } 25 26 SEC("lsm_cgroup/socket_create") 27 __description("lsm_cgroup bpf_set_retval invalid negative value") 28 __failure __msg("should have been in [-4095, 0]") 29 int BPF_PROG(lsm_cgroup_set_retval_negative_invalid, int family, int type, int protocol, int kern) 30 { 31 bpf_set_retval(-4096); 32 return 0; 33 } 34 35 SEC("lsm_cgroup/socket_create") 36 __description("lsm_cgroup bpf_set_retval invalid positive value") 37 __failure __msg("should have been in [-4095, 0]") 38 int BPF_PROG(lsm_cgroup_set_retval_positive_invalid, int family, int type, int protocol, int kern) 39 { 40 bpf_set_retval(1); 41 return 0; 42 } 43 44 SEC("cgroup/dev") 45 __description("cgroup_device bpf_set_retval success") 46 __success 47 int cgroup_dev_set_retval_0(struct bpf_cgroup_dev_ctx *ctx) 48 { 49 bpf_set_retval(0); 50 return 1; 51 } 52 53 SEC("cgroup/dev") 54 __description("cgroup_device bpf_set_retval valid errno") 55 __success 56 int cgroup_dev_set_retval_neg_maxerrno(struct bpf_cgroup_dev_ctx *ctx) 57 { 58 bpf_set_retval(-4095); 59 return 1; 60 } 61 62 SEC("cgroup/dev") 63 __description("cgroup_device bpf_set_retval invalid positive value") 64 __failure __msg("should have been in [-4095, 0]") 65 int cgroup_dev_set_retval_1(struct bpf_cgroup_dev_ctx *ctx) 66 { 67 bpf_set_retval(1); 68 return 1; 69 } 70 71 SEC("cgroup/dev") 72 __description("cgroup_device bpf_set_retval invalid negative value") 73 __failure __msg("should have been in [-4095, 0]") 74 int cgroup_dev_set_retval_neg_4096(struct bpf_cgroup_dev_ctx *ctx) 75 { 76 bpf_set_retval(-4096); 77 return 1; 78 } 79 80 SEC("cgroup/dev") 81 __description("bpf_set_retval bounds check survives state pruning") 82 __failure __msg("should have been in [-4095, 0]") 83 __naked int cgroup_dev_set_retval_pruning_bypass(struct bpf_cgroup_dev_ctx *ctx) 84 { 85 asm volatile ( 86 "call %[bpf_get_prandom_u32];" 87 "if r0 != 0 goto 1f;" 88 "r0 = r0;" 89 "r0 = r0;" 90 "r0 = r0;" 91 "r0 = r0;" 92 "goto 2f;" 93 "1:" 94 "call %[bpf_get_prandom_u32];" 95 "2:" 96 "r1 = r0;" 97 "call %[bpf_set_retval];" 98 "r0 = 1;" 99 "exit;" 100 : 101 : __imm(bpf_get_prandom_u32), 102 __imm(bpf_set_retval) 103 : __clobber_common 104 ); 105 } 106 107 char _license[] SEC("license") = "GPL"; 108