1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <vmlinux.h> 4 #include <bpf/bpf_helpers.h> 5 #include <bpf/bpf_tracing.h> 6 #include "bpf_misc.h" 7 8 SEC("lsm/file_permission") 9 __description("lsm bpf prog with -4095~0 retval. test 1") 10 __success 11 __naked int errno_zero_retval_test1(void *ctx) 12 { 13 asm volatile ( 14 "r0 = 0;" 15 "exit;" 16 ::: __clobber_all); 17 } 18 19 SEC("lsm/file_permission") 20 __description("lsm bpf prog with -4095~0 retval. test 2") 21 __success 22 __naked int errno_zero_retval_test2(void *ctx) 23 { 24 asm volatile ( 25 "r0 = -4095;" 26 "exit;" 27 ::: __clobber_all); 28 } 29 30 SEC("lsm/file_mprotect") 31 __description("lsm bpf prog with -4095~0 retval. test 4") 32 __failure __msg("R0 has smin=-4096 smax=-4096 should have been in [-4095, 0]") 33 __naked int errno_zero_retval_test4(void *ctx) 34 { 35 asm volatile ( 36 "r0 = -4096;" 37 "exit;" 38 ::: __clobber_all); 39 } 40 41 SEC("lsm/file_mprotect") 42 __description("lsm bpf prog with -4095~0 retval. test 5") 43 __failure __msg("R0 has smin=4096 smax=4096 should have been in [-4095, 0]") 44 __naked int errno_zero_retval_test5(void *ctx) 45 { 46 asm volatile ( 47 "r0 = 4096;" 48 "exit;" 49 ::: __clobber_all); 50 } 51 52 SEC("lsm/file_mprotect") 53 __description("lsm bpf prog with -4095~0 retval. test 6") 54 __failure __msg("R0 has smin=1 smax=1 should have been in [-4095, 0]") 55 __naked int errno_zero_retval_test6(void *ctx) 56 { 57 asm volatile ( 58 "r0 = 1;" 59 "exit;" 60 ::: __clobber_all); 61 } 62 63 SEC("lsm/audit_rule_known") 64 __description("lsm bpf prog with bool retval. test 1") 65 __success 66 __naked int bool_retval_test1(void *ctx) 67 { 68 asm volatile ( 69 "r0 = 1;" 70 "exit;" 71 ::: __clobber_all); 72 } 73 74 SEC("lsm/audit_rule_known") 75 __description("lsm bpf prog with bool retval. test 2") 76 __success 77 __success 78 __naked int bool_retval_test2(void *ctx) 79 { 80 asm volatile ( 81 "r0 = 0;" 82 "exit;" 83 ::: __clobber_all); 84 } 85 86 SEC("lsm/audit_rule_known") 87 __description("lsm bpf prog with bool retval. test 3") 88 __failure __msg("R0 has smin=-1 smax=-1 should have been in [0, 1]") 89 __naked int bool_retval_test3(void *ctx) 90 { 91 asm volatile ( 92 "r0 = -1;" 93 "exit;" 94 ::: __clobber_all); 95 } 96 97 SEC("lsm/audit_rule_known") 98 __description("lsm bpf prog with bool retval. test 4") 99 __failure __msg("R0 has smin=2 smax=2 should have been in [0, 1]") 100 __naked int bool_retval_test4(void *ctx) 101 { 102 asm volatile ( 103 "r0 = 2;" 104 "exit;" 105 ::: __clobber_all); 106 } 107 108 SEC("lsm/file_free_security") 109 __success 110 __description("lsm bpf prog with void retval. test 1") 111 __naked int void_retval_test1(void *ctx) 112 { 113 asm volatile ( 114 "r0 = -4096;" 115 "exit;" 116 ::: __clobber_all); 117 } 118 119 SEC("lsm/file_free_security") 120 __success 121 __description("lsm bpf prog with void retval. test 2") 122 __naked int void_retval_test2(void *ctx) 123 { 124 asm volatile ( 125 "r0 = 4096;" 126 "exit;" 127 ::: __clobber_all); 128 } 129 130 SEC("lsm/getprocattr") 131 __description("lsm disabled hook: getprocattr") 132 __failure __msg("points to disabled hook") 133 __naked int disabled_hook_test1(void *ctx) 134 { 135 asm volatile ( 136 "r0 = 0;" 137 "exit;" 138 ::: __clobber_all); 139 } 140 141 SEC("lsm/setprocattr") 142 __description("lsm disabled hook: setprocattr") 143 __failure __msg("points to disabled hook") 144 __naked int disabled_hook_test2(void *ctx) 145 { 146 asm volatile ( 147 "r0 = 0;" 148 "exit;" 149 ::: __clobber_all); 150 } 151 152 SEC("lsm/ismaclabel") 153 __description("lsm disabled hook: ismaclabel") 154 __failure __msg("points to disabled hook") 155 __naked int disabled_hook_test3(void *ctx) 156 { 157 asm volatile ( 158 "r0 = 0;" 159 "exit;" 160 ::: __clobber_all); 161 } 162 163 SEC("lsm/mmap_file") 164 __description("not null checking nullable pointer in bpf_lsm_mmap_file") 165 __failure __msg("R1 invalid mem access 'trusted_ptr_or_null_'") 166 int BPF_PROG(no_null_check, struct file *file) 167 { 168 struct inode *inode; 169 170 inode = file->f_inode; 171 __sink(inode); 172 173 return 0; 174 } 175 176 SEC("lsm/mmap_file") 177 __description("null checking nullable pointer in bpf_lsm_mmap_file") 178 __success 179 int BPF_PROG(null_check, struct file *file) 180 { 181 struct inode *inode; 182 183 if (file) { 184 inode = file->f_inode; 185 __sink(inode); 186 } 187 188 return 0; 189 } 190 191 char _license[] SEC("license") = "GPL"; 192