Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Add return value checks for failed tests

The return ranges of some bpf lsm test progs can not be deduced by
the verifier accurately. To avoid erroneous rejections, add explicit
return value checks for these progs.

Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
Link: https://lore.kernel.org/r/20240719110059.797546-8-xukuohai@huaweicloud.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>

authored by

Xu Kuohai and committed by
Andrii Nakryiko
2b23b6c0 4dc75564

+26 -3
+10
tools/testing/selftests/bpf/progs/err.h
··· 5 5 #define MAX_ERRNO 4095 6 6 #define IS_ERR_VALUE(x) (unsigned long)(void *)(x) >= (unsigned long)-MAX_ERRNO 7 7 8 + #define __STR(x) #x 9 + 10 + #define set_if_not_errno_or_zero(x, y) \ 11 + ({ \ 12 + asm volatile ("if %0 s< -4095 goto +1\n" \ 13 + "if %0 s<= 0 goto +1\n" \ 14 + "%0 = " __STR(y) "\n" \ 15 + : "+r"(x)); \ 16 + }) 17 + 8 18 static inline int IS_ERR_OR_NULL(const void *ptr) 9 19 { 10 20 return !ptr || IS_ERR_VALUE((unsigned long)ptr);
+4
tools/testing/selftests/bpf/progs/test_sig_in_xattr.c
··· 6 6 #include <bpf/bpf_helpers.h> 7 7 #include <bpf/bpf_tracing.h> 8 8 #include "bpf_kfuncs.h" 9 + #include "err.h" 9 10 10 11 char _license[] SEC("license") = "GPL"; 11 12 ··· 80 79 ret = bpf_verify_pkcs7_signature(&digest_ptr, &sig_ptr, trusted_keyring); 81 80 82 81 bpf_key_put(trusted_keyring); 82 + 83 + set_if_not_errno_or_zero(ret, -EFAULT); 84 + 83 85 return ret; 84 86 }
+6 -2
tools/testing/selftests/bpf/progs/test_verify_pkcs7_sig.c
··· 11 11 #include <bpf/bpf_helpers.h> 12 12 #include <bpf/bpf_tracing.h> 13 13 #include "bpf_kfuncs.h" 14 + #include "err.h" 14 15 15 16 #define MAX_DATA_SIZE (1024 * 1024) 16 17 #define MAX_SIG_SIZE 1024 ··· 56 55 57 56 ret = bpf_probe_read_kernel(&value, sizeof(value), &attr->value); 58 57 if (ret) 59 - return ret; 58 + goto out; 60 59 61 60 ret = bpf_copy_from_user(data_val, sizeof(struct data), 62 61 (void *)(unsigned long)value); 63 62 if (ret) 64 - return ret; 63 + goto out; 65 64 66 65 if (data_val->data_len > sizeof(data_val->data)) 67 66 return -EINVAL; ··· 84 83 ret = bpf_verify_pkcs7_signature(&data_ptr, &sig_ptr, trusted_keyring); 85 84 86 85 bpf_key_put(trusted_keyring); 86 + 87 + out: 88 + set_if_not_errno_or_zero(ret, -EFAULT); 87 89 88 90 return ret; 89 91 }
+6 -1
tools/testing/selftests/bpf/progs/verifier_global_subprogs.c
··· 7 7 #include "bpf_misc.h" 8 8 #include "xdp_metadata.h" 9 9 #include "bpf_kfuncs.h" 10 + #include "err.h" 10 11 11 12 /* The compiler may be able to detect the access to uninitialized 12 13 memory in the routines performing out of bound memory accesses and ··· 332 331 __success __log_level(2) 333 332 int BPF_PROG(arg_tag_ctx_lsm) 334 333 { 335 - return tracing_subprog_void(ctx) + tracing_subprog_u64(ctx); 334 + int ret; 335 + 336 + ret = tracing_subprog_void(ctx) + tracing_subprog_u64(ctx); 337 + set_if_not_errno_or_zero(ret, -1); 338 + return ret; 336 339 } 337 340 338 341 SEC("?struct_ops/test_1")