Skip to content

Commit 339c1f8

Browse files
kknjhAlexei Starovoitov
authored andcommitted
selftests/bpf: Fix cap_enable_effective() return code
The caller of cap_enable_effective() expects negative error code. Fix it. Before: failed to restore CAP_SYS_ADMIN: -1, Unknown error -1 After: failed to restore CAP_SYS_ADMIN: -3, No such process failed to restore CAP_SYS_ADMIN: -22, Invalid argument Signed-off-by: Feng Yang <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 8bda5b7 commit 339c1f8

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

tools/testing/selftests/bpf/cap_helpers.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ int cap_enable_effective(__u64 caps, __u64 *old_caps)
1919

2020
err = capget(&hdr, data);
2121
if (err)
22-
return err;
22+
return -errno;
2323

2424
if (old_caps)
2525
*old_caps = (__u64)(data[1].effective) << 32 | data[0].effective;
@@ -32,7 +32,7 @@ int cap_enable_effective(__u64 caps, __u64 *old_caps)
3232
data[1].effective |= cap1;
3333
err = capset(&hdr, data);
3434
if (err)
35-
return err;
35+
return -errno;
3636

3737
return 0;
3838
}
@@ -49,7 +49,7 @@ int cap_disable_effective(__u64 caps, __u64 *old_caps)
4949

5050
err = capget(&hdr, data);
5151
if (err)
52-
return err;
52+
return -errno;
5353

5454
if (old_caps)
5555
*old_caps = (__u64)(data[1].effective) << 32 | data[0].effective;
@@ -61,7 +61,7 @@ int cap_disable_effective(__u64 caps, __u64 *old_caps)
6161
data[1].effective &= ~cap1;
6262
err = capset(&hdr, data);
6363
if (err)
64-
return err;
64+
return -errno;
6565

6666
return 0;
6767
}

tools/testing/selftests/bpf/cap_helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <linux/types.h>
66
#include <linux/capability.h>
7+
#include <errno.h>
78

89
#ifndef CAP_PERFMON
910
#define CAP_PERFMON 38

tools/testing/selftests/bpf/prog_tests/verifier.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ static void run_tests_aux(const char *skel_name,
123123
/* test_verifier tests are executed w/o CAP_SYS_ADMIN, do the same here */
124124
err = cap_disable_effective(1ULL << CAP_SYS_ADMIN, &old_caps);
125125
if (err) {
126-
PRINT_FAIL("failed to drop CAP_SYS_ADMIN: %i, %s\n", err, strerror(err));
126+
PRINT_FAIL("failed to drop CAP_SYS_ADMIN: %i, %s\n", err, strerror(-err));
127127
return;
128128
}
129129

@@ -133,7 +133,7 @@ static void run_tests_aux(const char *skel_name,
133133

134134
err = cap_enable_effective(old_caps, NULL);
135135
if (err)
136-
PRINT_FAIL("failed to restore CAP_SYS_ADMIN: %i, %s\n", err, strerror(err));
136+
PRINT_FAIL("failed to restore CAP_SYS_ADMIN: %i, %s\n", err, strerror(-err));
137137
}
138138

139139
#define RUN(skel) run_tests_aux(#skel, skel##__elf_bytes, NULL)

tools/testing/selftests/bpf/test_loader.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ static int drop_capabilities(struct cap_state *caps)
793793

794794
err = cap_disable_effective(caps_to_drop, &caps->old_caps);
795795
if (err) {
796-
PRINT_FAIL("failed to drop capabilities: %i, %s\n", err, strerror(err));
796+
PRINT_FAIL("failed to drop capabilities: %i, %s\n", err, strerror(-err));
797797
return err;
798798
}
799799

@@ -810,7 +810,7 @@ static int restore_capabilities(struct cap_state *caps)
810810

811811
err = cap_enable_effective(caps->old_caps, NULL);
812812
if (err)
813-
PRINT_FAIL("failed to restore capabilities: %i, %s\n", err, strerror(err));
813+
PRINT_FAIL("failed to restore capabilities: %i, %s\n", err, strerror(-err));
814814
caps->initialized = false;
815815
return err;
816816
}
@@ -985,7 +985,7 @@ void run_subtest(struct test_loader *tester,
985985
if (subspec->caps) {
986986
err = cap_enable_effective(subspec->caps, NULL);
987987
if (err) {
988-
PRINT_FAIL("failed to set capabilities: %i, %s\n", err, strerror(err));
988+
PRINT_FAIL("failed to set capabilities: %i, %s\n", err, strerror(-err));
989989
goto subtest_cleanup;
990990
}
991991
}

0 commit comments

Comments
 (0)