060-v5.18-03-bpf-selftests-Remove-libcap-usage-from-test_progs.patch 3.8 KB

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