selftests/bpf: Check stack_mprotect() return value
authorIlya Leoshkevich <iii@linux.ibm.com>
Sat, 28 Jan 2023 00:06:31 +0000 (01:06 +0100)
committerAlexei Starovoitov <ast@kernel.org>
Sat, 28 Jan 2023 20:30:08 +0000 (12:30 -0800)
If stack_mprotect() succeeds, errno is not changed. This can produce
misleading error messages, that show stale errno.

Signed-off-by: Ilya Leoshkevich <iii@linux.ibm.com>
Link: https://lore.kernel.org/r/20230128000650.1516334-13-iii@linux.ibm.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/prog_tests/bpf_cookie.c
tools/testing/selftests/bpf/prog_tests/test_lsm.c

index 2be2d61954bc63f0f77fce9a72aae7885c75c103..26b2d1bffdfd546c915b09242ef220de78b72303 100644 (file)
@@ -472,6 +472,7 @@ static void lsm_subtest(struct test_bpf_cookie *skel)
        int prog_fd;
        int lsm_fd = -1;
        LIBBPF_OPTS(bpf_link_create_opts, link_opts);
+       int err;
 
        skel->bss->lsm_res = 0;
 
@@ -482,8 +483,9 @@ static void lsm_subtest(struct test_bpf_cookie *skel)
        if (!ASSERT_GE(lsm_fd, 0, "lsm.link_create"))
                goto cleanup;
 
-       stack_mprotect();
-       if (!ASSERT_EQ(errno, EPERM, "stack_mprotect"))
+       err = stack_mprotect();
+       if (!ASSERT_EQ(err, -1, "stack_mprotect") ||
+           !ASSERT_EQ(errno, EPERM, "stack_mprotect"))
                goto cleanup;
 
        usleep(1);
index 244c01125126185c661ce37a8f0e9da0567aae85..16175d579bc79fded4f8acbec7d566ddcf54d106 100644 (file)
@@ -75,7 +75,8 @@ static int test_lsm(struct lsm *skel)
        skel->bss->monitored_pid = getpid();
 
        err = stack_mprotect();
-       if (!ASSERT_EQ(errno, EPERM, "stack_mprotect"))
+       if (!ASSERT_EQ(err, -1, "stack_mprotect") ||
+           !ASSERT_EQ(errno, EPERM, "stack_mprotect"))
                return err;
 
        ASSERT_EQ(skel->bss->mprotect_count, 1, "mprotect_count");