]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests/bpf: Add return value checks for failed tests
authorXu Kuohai <xukuohai@huawei.com>
Fri, 19 Jul 2024 11:00:57 +0000 (19:00 +0800)
committerAndrii Nakryiko <andrii@kernel.org>
Mon, 29 Jul 2024 20:09:37 +0000 (13:09 -0700)
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>
tools/testing/selftests/bpf/progs/err.h
tools/testing/selftests/bpf/progs/test_sig_in_xattr.c
tools/testing/selftests/bpf/progs/test_verify_pkcs7_sig.c
tools/testing/selftests/bpf/progs/verifier_global_subprogs.c

index d66d283d9e59a4b556180223f2d227f9f53ee6f0..38529779a2363d4db862f1739b5e55af70dfa138 100644 (file)
@@ -5,6 +5,16 @@
 #define MAX_ERRNO 4095
 #define IS_ERR_VALUE(x) (unsigned long)(void *)(x) >= (unsigned long)-MAX_ERRNO
 
+#define __STR(x) #x
+
+#define set_if_not_errno_or_zero(x, y)                 \
+({                                                     \
+       asm volatile ("if %0 s< -4095 goto +1\n"        \
+                     "if %0 s<= 0 goto +1\n"           \
+                     "%0 = " __STR(y) "\n"             \
+                     : "+r"(x));                       \
+})
+
 static inline int IS_ERR_OR_NULL(const void *ptr)
 {
        return !ptr || IS_ERR_VALUE((unsigned long)ptr);
index 2f0eb1334d65a36130c887b16002c06e39b10759..8ef6b39335b678224e4591887286c9fccb78d0b9 100644 (file)
@@ -6,6 +6,7 @@
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_tracing.h>
 #include "bpf_kfuncs.h"
+#include "err.h"
 
 char _license[] SEC("license") = "GPL";
 
@@ -79,5 +80,8 @@ int BPF_PROG(test_file_open, struct file *f)
        ret = bpf_verify_pkcs7_signature(&digest_ptr, &sig_ptr, trusted_keyring);
 
        bpf_key_put(trusted_keyring);
+
+       set_if_not_errno_or_zero(ret, -EFAULT);
+
        return ret;
 }
index f42e9f3831a126f1536d4e7a19902f16f9b49284..12034a73ee2d237966db0b3f4ec1ef60374359bc 100644 (file)
@@ -11,6 +11,7 @@
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_tracing.h>
 #include "bpf_kfuncs.h"
+#include "err.h"
 
 #define MAX_DATA_SIZE (1024 * 1024)
 #define MAX_SIG_SIZE 1024
@@ -55,12 +56,12 @@ int BPF_PROG(bpf, int cmd, union bpf_attr *attr, unsigned int size)
 
        ret = bpf_probe_read_kernel(&value, sizeof(value), &attr->value);
        if (ret)
-               return ret;
+               goto out;
 
        ret = bpf_copy_from_user(data_val, sizeof(struct data),
                                 (void *)(unsigned long)value);
        if (ret)
-               return ret;
+               goto out;
 
        if (data_val->data_len > sizeof(data_val->data))
                return -EINVAL;
@@ -84,5 +85,8 @@ int BPF_PROG(bpf, int cmd, union bpf_attr *attr, unsigned int size)
 
        bpf_key_put(trusted_keyring);
 
+out:
+       set_if_not_errno_or_zero(ret, -EFAULT);
+
        return ret;
 }
index a9fc30ed4d732b3935730bc2d30024bb0bd9bc85..20904cd2baa2fcbae5badc82877973992c2acb26 100644 (file)
@@ -7,6 +7,7 @@
 #include "bpf_misc.h"
 #include "xdp_metadata.h"
 #include "bpf_kfuncs.h"
+#include "err.h"
 
 /* The compiler may be able to detect the access to uninitialized
    memory in the routines performing out of bound memory accesses and
@@ -331,7 +332,11 @@ SEC("?lsm/bpf")
 __success __log_level(2)
 int BPF_PROG(arg_tag_ctx_lsm)
 {
-       return tracing_subprog_void(ctx) + tracing_subprog_u64(ctx);
+       int ret;
+
+       ret = tracing_subprog_void(ctx) + tracing_subprog_u64(ctx);
+       set_if_not_errno_or_zero(ret, -1);
+       return ret;
 }
 
 SEC("?struct_ops/test_1")