9badba7f8eaa366b7283ae8426f0646ff9b4deb0
[openwrt/staging/xback.git] /
1 From 1ac00fea13c576e2b13dabf9a72ad3034e3bb804 Mon Sep 17 00:00:00 2001
2 From: Martin KaFai Lau <kafai@fb.com>
3 Date: Wed, 16 Mar 2022 10:38:35 -0700
4 Subject: [PATCH 3/3] bpf: selftests: Remove libcap usage from test_progs
5
6 This patch removes the libcap usage from test_progs.
7 bind_perm.c is the only user. cap_*_effective() helpers added in the
8 earlier patch are directly used instead.
9
10 No other selftest binary is using libcap, so '-lcap' is also removed
11 from the Makefile.
12
13 Signed-off-by: Martin KaFai Lau <kafai@fb.com>
14 Signed-off-by: Alexei Starovoitov <ast@kernel.org>
15 Reviewed-by: Stanislav Fomichev <sdf@google.com>
16 Acked-by: John Fastabend <john.fastabend@gmail.com>
17 Link: https://lore.kernel.org/bpf/20220316173835.2039334-1-kafai@fb.com
18 ---
19 tools/testing/selftests/bpf/Makefile | 5 ++-
20 .../selftests/bpf/prog_tests/bind_perm.c | 44 ++++---------------
21 2 files changed, 11 insertions(+), 38 deletions(-)
22
23 --- a/tools/testing/selftests/bpf/Makefile
24 +++ b/tools/testing/selftests/bpf/Makefile
25 @@ -26,7 +26,7 @@ CFLAGS += -g -O0 -rdynamic -Wall $(GENFL
26 -I$(TOOLSINCDIR) -I$(APIDIR) -I$(OUTPUT) \
27 -Dbpf_prog_load=bpf_prog_test_load \
28 -Dbpf_load_program=bpf_test_load_program
29 -LDLIBS += -lcap -lelf -lz -lrt -lpthread
30 +LDLIBS += -lelf -lz -lrt -lpthread
31
32 # Silence some warnings when compiled with clang
33 ifneq ($(LLVM),)
34 @@ -471,7 +471,8 @@ TRUNNER_TESTS_DIR := prog_tests
35 TRUNNER_BPF_PROGS_DIR := progs
36 TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c \
37 network_helpers.c testing_helpers.c \
38 - btf_helpers.c flow_dissector_load.h
39 + btf_helpers.c flow_dissector_load.h \
40 + cap_helpers.c
41 TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko \
42 ima_setup.sh \
43 $(wildcard progs/btf_dump_test_case_*.c)
44 --- a/tools/testing/selftests/bpf/prog_tests/bind_perm.c
45 +++ b/tools/testing/selftests/bpf/prog_tests/bind_perm.c
46 @@ -4,9 +4,9 @@
47 #include <stdlib.h>
48 #include <sys/types.h>
49 #include <sys/socket.h>
50 -#include <sys/capability.h>
51
52 #include "test_progs.h"
53 +#include "cap_helpers.h"
54 #include "bind_perm.skel.h"
55
56 static int duration;
57 @@ -49,41 +49,11 @@ close_socket:
58 close(fd);
59 }
60
61 -bool cap_net_bind_service(cap_flag_value_t flag)
62 -{
63 - const cap_value_t cap_net_bind_service = CAP_NET_BIND_SERVICE;
64 - cap_flag_value_t original_value;
65 - bool was_effective = false;
66 - cap_t caps;
67 -
68 - caps = cap_get_proc();
69 - if (CHECK(!caps, "cap_get_proc", "errno %d", errno))
70 - goto free_caps;
71 -
72 - if (CHECK(cap_get_flag(caps, CAP_NET_BIND_SERVICE, CAP_EFFECTIVE,
73 - &original_value),
74 - "cap_get_flag", "errno %d", errno))
75 - goto free_caps;
76 -
77 - was_effective = (original_value == CAP_SET);
78 -
79 - if (CHECK(cap_set_flag(caps, CAP_EFFECTIVE, 1, &cap_net_bind_service,
80 - flag),
81 - "cap_set_flag", "errno %d", errno))
82 - goto free_caps;
83 -
84 - if (CHECK(cap_set_proc(caps), "cap_set_proc", "errno %d", errno))
85 - goto free_caps;
86 -
87 -free_caps:
88 - CHECK(cap_free(caps), "cap_free", "errno %d", errno);
89 - return was_effective;
90 -}
91 -
92 void test_bind_perm(void)
93 {
94 - bool cap_was_effective;
95 + const __u64 net_bind_svc_cap = 1ULL << CAP_NET_BIND_SERVICE;
96 struct bind_perm *skel;
97 + __u64 old_caps = 0;
98 int cgroup_fd;
99
100 if (create_netns())
101 @@ -105,7 +75,8 @@ void test_bind_perm(void)
102 if (!ASSERT_OK_PTR(skel, "bind_v6_prog"))
103 goto close_skeleton;
104
105 - cap_was_effective = cap_net_bind_service(CAP_CLEAR);
106 + ASSERT_OK(cap_disable_effective(net_bind_svc_cap, &old_caps),
107 + "cap_disable_effective");
108
109 try_bind(AF_INET, 110, EACCES);
110 try_bind(AF_INET6, 110, EACCES);
111 @@ -113,8 +84,9 @@ void test_bind_perm(void)
112 try_bind(AF_INET, 111, 0);
113 try_bind(AF_INET6, 111, 0);
114
115 - if (cap_was_effective)
116 - cap_net_bind_service(CAP_SET);
117 + if (old_caps & net_bind_svc_cap)
118 + ASSERT_OK(cap_enable_effective(net_bind_svc_cap, NULL),
119 + "cap_enable_effective");
120
121 close_skeleton:
122 bind_perm__destroy(skel);