Skip to contentSkip to conversation

libbpf: fix detection of BPF helper call instruction

PATCH In review Andrii Nakryiko 3 participants 9 messages Aug 20, 2020

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

PATCH 1/4 libbpf: fix detection of BPF helper call instruction

BPF_CALL | BPF_JMP32 is explicitly not allowed by verifier for BPF helper calls, so don't detect it as a valid call. Also drop the check on func_id pointer, as it's currently always non-null.

Fixes 109cea5a594f ("libbpf: Sanitize BPF program code for bpf_probe_read_{kernel, user}[_str]")
---
 tools/lib/bpf/libbpf.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
tools/lib/bpf/libbpf.c +4 -6
index 77d420c02094..92ca4eb6ba2d 100644
@@ -5844,14 +5844,12 @@ static int bpf_object__collect_reloc(struct bpf_object *obj)
 
 static bool insn_is_helper_call(struct bpf_insn *insn, enum bpf_func_id *func_id)
 {
-	__u8 class = BPF_CLASS(insn->code);
-
-	if ((class == BPF_JMP || class == BPF_JMP32) &&
+	if (BPF_CLASS(insn->code) == BPF_JMP &&
 	    BPF_OP(insn->code) == BPF_CALL &&
 	    BPF_SRC(insn->code) == BPF_K &&
-	    insn->src_reg == 0 && insn->dst_reg == 0) {
-		    if (func_id)
-			    *func_id = insn->imm;
+	    insn->src_reg == 0 &&
+	    insn->dst_reg == 0) {
+		    *func_id = insn->imm;
 		    return true;
 	}
 	return false;
Signature · 1 line
2.24.1
#1 lore raw

PATCH 2/4 libbpf: fix libbpf build on compilers missing __builtin_mul_overflow

GCC compilers older than version 5 don't support __builtin_mul_overflow yet. Given GCC 4.9 is the minimal supported compiler for building kernel and the fact that libbpf is a dependency of resolve_btfids, which is dependency of CONFIG_DEBUG_INFO_BTF=y, this needs to be handled. This patch fixes the issue by falling back to slower detection of integer overflow in such cases.

Fixes 029258d7b228 ("libbpf: Remove any use of reallocarray() in libbpf")
---
 tools/lib/bpf/libbpf_internal.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
tools/lib/bpf/libbpf_internal.h +10 -0
index 61dff515a2f0..4d1c366fca2c 100644
@@ -10,6 +10,7 @@
 #define __LIBBPF_LIBBPF_INTERNAL_H
 
 #include <stdlib.h>
+#include <limits.h>
 
 /* make sure libbpf doesn't use kernel-only integer typedefs */
 #pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
@@ -77,6 +78,9 @@ do {				\
 #define pr_info(fmt, ...)	__pr(LIBBPF_INFO, fmt, ##__VA_ARGS__)
 #define pr_debug(fmt, ...)	__pr(LIBBPF_DEBUG, fmt, ##__VA_ARGS__)
 
+#ifndef __has_builtin
+#define __has_builtin(x) 0
+#endif
 /*
  * Re-implement glibc's reallocarray() for libbpf internal-only use.
  * reallocarray(), unfortunately, is not available in all versions of glibc,
@@ -90,8 +94,14 @@ static inline void *libbpf_reallocarray(void *ptr, size_t nmemb, size_t size)
 {
 	size_t total;
 
+#if __has_builtin(__builtin_mul_overflow)
 	if (unlikely(__builtin_mul_overflow(nmemb, size, &total)))
 		return NULL;
+#else
+	if (size == 0 || nmemb > ULONG_MAX / size)
+		return NULL;
+	total = nmemb * size;
+#endif
 	return realloc(ptr, total);
 }
 
Signature · 1 line
2.24.1
Acked-by on v1 2/4
#2 lore raw

Re: [PATCH bpf-next 2/4] libbpf: fix libbpf build on compilers missing __builtin_mul_overflow

Quoted 8 lines · Andrii Nakryiko

GCC compilers older than version 5 don't support __builtin_mul_overflow yet. Given GCC 4.9 is the minimal supported compiler for building kernel and the fact that libbpf is a dependency of resolve_btfids, which is dependency of CONFIG_DEBUG_INFO_BTF=y, this needs to be handled. This patch fixes the issue by falling back to slower detection of integer overflow in such cases.

Fixes 029258d7b228 ("libbpf: Remove any use of reallocarray() in libbpf")
#3 lore raw

PATCH 3/4 selftests/bpf: fix two minor compilation warnings reported by GCC 4.9

In reply to Andrii Nakryiko

GCC 4.9 seems to be more strict in some regards. Fix two minor issue it reported.

Fixes 1c1052e0140a ("tools/testing/selftests/bpf: Add self-tests for new helper bpf_get_ns_current_pid_tgid.")
Fixes 2d7824ffd25c ("selftests: bpf: Add test for sk_assign")
---
 tools/testing/selftests/bpf/prog_tests/sk_assign.c         | 3 ++-
 tools/testing/selftests/bpf/test_current_pid_tgid_new_ns.c | 1 +
 2 files changed, 3 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/sk_assign.c +2 -1
index 47fa04adc147..d43038d2b9e1 100644
@@ -268,6 +268,7 @@ void test_sk_assign(void)
 	int server = -1;
 	int server_map;
 	int self_net;
+	int i;
 
 	self_net = open(NS_SELF, O_RDONLY);
 	if (CHECK_FAIL(self_net < 0)) {
@@ -286,7 +287,7 @@ void test_sk_assign(void)
 		goto cleanup;
 	}
 
-	for (int i = 0; i < ARRAY_SIZE(tests) && !READ_ONCE(stop); i++) {
+	for (i = 0; i < ARRAY_SIZE(tests) && !READ_ONCE(stop); i++) {
 		struct test_sk_cfg *test = &tests[i];
 		const struct sockaddr *addr;
 		const int zero = 0;
tools/testing/selftests/bpf/test_current_pid_tgid_new_ns.c +1 -0
index ed253f252cd0..ec53b1ef90d2 100644
@@ -156,4 +156,5 @@ int main(int argc, char **argv)
 			bpf_object__close(obj);
 		}
 	}
+	return 0;
 }
Signature · 1 line
2.24.1
Acked-by on v1 3/4
#4 lore raw

Re: [PATCH bpf-next 3/4] selftests/bpf: fix two minor compilation warnings reported by GCC 4.9

Quoted 6 lines · Andrii Nakryiko

GCC 4.9 seems to be more strict in some regards. Fix two minor issue it reported.

Fixes 1c1052e0140a ("tools/testing/selftests/bpf: Add self-tests for new helper bpf_get_ns_current_pid_tgid.")
Fixes 2d7824ffd25c ("selftests: bpf: Add test for sk_assign")
#5 lore raw

PATCH 4/4 selftests/bpf: list newest Clang built-ins needed for some CO-RE selftests

In reply to Andrii Nakryiko

Record which built-ins are optional and needed for some of recent BPF CO-RE subtests. Document Clang diff that fixed corner-case issue with __builtin_btf_type_id().

---
 tools/testing/selftests/bpf/README.rst        | 21 +++++++++++++++++++
 .../bpf/progs/test_core_reloc_type_id.c       |  4 +++-
 2 files changed, 24 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/README.rst +21 -0
index e885d351595f..66acfcf15ff2 100644
@@ -43,3 +43,24 @@ This is due to a llvm BPF backend bug. The fix
   https://reviews.llvm.org/D78466
 has been pushed to llvm 10.x release branch and will be
 available in 10.0.1. The fix is available in llvm 11.0.0 trunk.
+
+BPF CO-RE-based tests and Clang version
+=======================================
+
+A set of selftests use BPF target-specific built-ins, which might require
+bleeding-edge Clang versions (Clang 12 nightly at this time).
+
+Few sub-tests of core_reloc test suit (part of test_progs test runner) require
+the following built-ins, listed with corresponding Clang diffs introducing
+them to Clang/LLVM. These sub-tests are going to be skipped if Clang is too
+old to support them, they shouldn't cause build failures or runtime test
+failures:
+
+  - __builtin_btf_type_id() ([0], [1], [2]);
+  - __builtin_preserve_type_info(), __builtin_preserve_enum_value() ([3], [4]).
+
+  [0] https://reviews.llvm.org/D74572
+  [1] https://reviews.llvm.org/D74668
+  [2] https://reviews.llvm.org/D85174
+  [3] https://reviews.llvm.org/D83878
+  [4] https://reviews.llvm.org/D83242
tools/testing/selftests/bpf/progs/test_core_reloc_type_id.c +3 -1
index 23e6e6bf276c..22aba3f6e344 100644
@@ -75,10 +75,12 @@ int test_core_type_id(void *ctx)
 {
 	/* We use __builtin_btf_type_id() in this tests, but up until the time
 	 * __builtin_preserve_type_info() was added it contained a bug that
-	 * would make this test fail. The bug was fixed with addition of
+	 * would make this test fail. The bug was fixed ([0]) with addition of
 	 * __builtin_preserve_type_info(), though, so that's what we are using
 	 * to detect whether this test has to be executed, however strange
 	 * that might look like.
+	 *
+	 *   [0] https://reviews.llvm.org/D85174
 	 */
 #if __has_builtin(__builtin_preserve_type_info)
 	struct core_reloc_type_id_output *out = (void *)&data.out;
Signature · 1 line
2.24.1
Acked-by on v1 4/4
#6 lore raw

Re: [PATCH bpf-next 4/4] selftests/bpf: list newest Clang built-ins needed for some CO-RE selftests

Quoted 6 lines · Andrii Nakryiko

Record which built-ins are optional and needed for some of recent BPF CO-RE subtests. Document Clang diff that fixed corner-case issue with __builtin_btf_type_id().

Acked-by on v1 1/4
#7 lore raw
In reply to Andrii Nakryiko
Quoted 7 lines · Andrii Nakryiko

BPF_CALL | BPF_JMP32 is explicitly not allowed by verifier for BPF helper calls, so don't detect it as a valid call. Also drop the check on func_id pointer, as it's currently always non-null.

Fixes 109cea5a594f ("libbpf: Sanitize BPF program code for bpf_probe_read_{kernel, user}[_str]")
maintainer
#8 lore raw
In reply to Andrii Nakryiko
Quoted 7 lines · Andrii Nakryiko

BPF_CALL | BPF_JMP32 is explicitly not allowed by verifier for BPF helper calls, so don't detect it as a valid call. Also drop the check on func_id pointer, as it's currently always non-null.

Fixes 109cea5a594f ("libbpf: Sanitize BPF program code for bpf_probe_read_{kernel, user}[_str]")

Series applied, thanks!