Skip to contentSkip to conversation

test_bpf.ko blinding fixes

PATCH In review Mathias Krause 2 participants 5 messages Nov 4, 2024 – Nov 16, 2024

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

cover test_bpf.ko blinding fixes

With BPF constant blinding enabled, various tests exercising jump offset limits currently fail because size assumptions in the code no longer apply. The created BPF program will be expanded through BPF constant blinding, pushing jump target offsets over the limit.

This small series fixes these by either avoiding the use of instructions with immediate values (patch 1) or accounting for the expansion (patch 3).

Before:

root@box:~# sysctl net.core.bpf_jit_harden=2
root@box:~# insmod test_bpf.ko
insmod: ERROR: could not insert module test_bpf.ko: Invalid parameters
root@box:~# dmesg | grep Summary
[  177.628099] test_bpf: Summary: 1000 PASSED, 49 FAILED, [988/988 JIT'ed]

After:

root@box:~# sysctl net.core.bpf_jit_harden=2
root@box:~# insmod test_bpf.ko
root@box:~# dmesg | grep Summary
[  220.437597] test_bpf: Summary: 1049 PASSED, 0 FAILED, [1037/1037 JIT'ed]
[  220.477987] test_bpf: test_tail_calls: Summary: 10 PASSED, 0 FAILED, [10/10 JIT'ed]
[  220.480525] test_bpf: test_skb_segment: Summary: 2 PASSED, 0 FAILED

Please apply!

Thanks,
Mathias

Mathias Krause (3):
bpf/tests: Make max jump tests constant blinding compatible
bpf: Allow calling bpf_jit_blinding_enabled() with a NULL program bpf/tests: Make staggered jump tests constant blinding compatible

 include/linux/filter.h |  5 +++--
 kernel/bpf/core.c      |  3 +++
 kernel/bpf/token.c     |  3 +++
 lib/test_bpf.c         | 21 ++++++++++++++++++---
 4 files changed, 27 insertions(+), 5 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/3 bpf/tests: Make max jump tests constant blinding compatible

Constant blinding may expand instructions that make use of immediate values into multiple, resulting in preceding jump instructions to get a bigger target offset. The "max jump" tests specifically attempt to test the biggest possible jump offset, requiring to take special care about the final (after blinding) offset while crafting the input program.

To make these tests independent of constant blinding's instruction expansion, use a register-only operation to load R0 with the intended value.

This fixes the "Long conditional jump" tests with enabled blinding.

Fixes f1517eb790f9 ("bpf/tests: Expand branch conversion JIT test")
Cc Johan Almbladh <[email protected]>
---
 lib/test_bpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/test_bpf.c +1 -1
index fa5edd6ef7f7..c1140bab280d 100644
@@ -491,7 +491,7 @@ static int __bpf_fill_max_jmp(struct bpf_test *self, int jmp, int imm)
 	i = __bpf_ld_imm64(insns, R1, 0x0123456789abcdefULL);
 	insns[i++] = BPF_ALU64_IMM(BPF_MOV, R0, 1);
 	insns[i++] = BPF_JMP_IMM(jmp, R0, imm, S16_MAX);
-	insns[i++] = BPF_ALU64_IMM(BPF_MOV, R0, 2);
+	insns[i++] = BPF_ALU64_REG(BPF_ADD, R0, R0);
 	insns[i++] = BPF_EXIT_INSN();
 
 	while (i < len - 1) {
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/3 bpf: Allow calling bpf_jit_blinding_enabled() with a NULL program

In reply to Mathias Krause

To allow probing if constant blinding will be applied, support calling bpf_jit_blinding_enabled() with a NULL program.

This allows to tailor the creation of a BPF program accordingly.

---
 include/linux/filter.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
include/linux/filter.h +3 -2
index 7d7578a8eac1..a1b4a9739f81 100644
@@ -1297,11 +1297,12 @@ static inline bool bpf_jit_blinding_enabled(struct bpf_prog *prog)
 	 */
 	if (!bpf_jit_is_ebpf())
 		return false;
-	if (!prog->jit_requested)
+	if (prog && !prog->jit_requested)
 		return false;
 	if (!bpf_jit_harden)
 		return false;
-	if (bpf_jit_harden == 1 && bpf_token_capable(prog->aux->token, CAP_BPF))
+	if (bpf_jit_harden == 1 &&
+	    bpf_token_capable(prog ? prog->aux->token : NULL, CAP_BPF))
 		return false;
 
 	return true;
Signature · 1 line
2.30.2
#3 lore raw

PATCH 3/3 bpf/tests: Make staggered jump tests constant blinding compatible

In reply to Mathias Krause

The "staggered jumps" tests currently fail with constant blinding enabled as the increased program size makes jump offsets overflow.

Fix that by decreasing the number of jumps depending on the expected size increase caused by blinding the program.

As the test for JIT blinding makes use of bpf_jit_blinding_enabled(NULL) and test_bpf.ko is a kernel modules, 'bpf_token_capable' and 'bpf_jit_harden' need to be exported.

Fixes a7d2e752e520 ("bpf/tests: Add staggered JMP and JMP32 tests")
Cc Johan Almbladh <[email protected]>
---
 kernel/bpf/core.c  |  3 +++
 kernel/bpf/token.c |  3 +++
 lib/test_bpf.c     | 19 +++++++++++++++++--
 3 files changed, 23 insertions(+), 2 deletions(-)
kernel/bpf/core.c +3 -0
index 233ea78f8f1b..fe7eada54d4b 100644
@@ -570,6 +570,9 @@ int bpf_jit_kallsyms __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_DEFAULT_ON);
 int bpf_jit_harden   __read_mostly;
 long bpf_jit_limit   __read_mostly;
 long bpf_jit_limit_max __read_mostly;
+#if IS_MODULE(CONFIG_TEST_BPF)
+EXPORT_SYMBOL_GPL(bpf_jit_harden);
+#endif
 
 static void
 bpf_prog_ksym_set_addr(struct bpf_prog *prog)
kernel/bpf/token.c +3 -0
index dcbec1a0dfb3..aed98a958c73 100644
@@ -26,6 +26,9 @@ bool bpf_token_capable(const struct bpf_token *token, int cap)
 		return false;
 	return true;
 }
+#if IS_MODULE(CONFIG_TEST_BPF)
+EXPORT_SYMBOL_GPL(bpf_token_capable);
+#endif
 
 void bpf_token_inc(struct bpf_token *token)
 {
lib/test_bpf.c +17 -2
index c1140bab280d..3469631c0aba 100644
@@ -2700,10 +2700,25 @@ static int __bpf_fill_staggered_jumps(struct bpf_test *self,
 				      u64 r1, u64 r2)
 {
 	int size = self->test[0].result - 1;
-	int len = 4 + 3 * (size + 1);
 	struct bpf_insn *insns;
-	int off, ind;
+	int len, off, ind;
 
+	/* Constant blinding triples the size of each instruction making use
+	 * of immediate values. Tweak the test to not overflow jump offsets.
+	 */
+	if (bpf_jit_blinding_enabled(NULL)) {
+		int bloat_factor = 2 * 3;
+
+		if (BPF_SRC(jmp->code) == BPF_K)
+			bloat_factor += 3;
+
+		size /= bloat_factor;
+		size &= ~1;
+
+		self->test[0].result = size + 1;
+	}
+
+	len = 4 + 3 * (size + 1);
 	insns = kmalloc_array(len, sizeof(*insns), GFP_KERNEL);
 	if (!insns)
 		return -ENOMEM;
Signature · 1 line
2.30.2
#4 lore raw

Re: [PATCH bpf-next 3/3] bpf/tests: Make staggered jump tests constant blinding compatible

Quoted 39 lines · Mathias Krause

The "staggered jumps" tests currently fail with constant blinding enabled as the increased program size makes jump offsets overflow.

Fix that by decreasing the number of jumps depending on the expected size increase caused by blinding the program.

As the test for JIT blinding makes use of bpf_jit_blinding_enabled(NULL) and test_bpf.ko is a kernel modules, 'bpf_token_capable' and 'bpf_jit_harden' need to be exported.

Fixes a7d2e752e520 ("bpf/tests: Add staggered JMP and JMP32 tests")
Cc Johan Almbladh <[email protected]>
---
 kernel/bpf/core.c  |  3 +++
 kernel/bpf/token.c |  3 +++
 lib/test_bpf.c     | 19 +++++++++++++++++--
 3 files changed, 23 insertions(+), 2 deletions(-)
kernel/bpf/core.c +3 -0
index 233ea78f8f1b..fe7eada54d4b 100644
@@ -570,6 +570,9 @@ int bpf_jit_kallsyms __read_mostly = IS_BUILTIN(CONFIG_BPF_JIT_DEFAULT_ON);
 int bpf_jit_harden   __read_mostly;
 long bpf_jit_limit   __read_mostly;
 long bpf_jit_limit_max __read_mostly;
+#if IS_MODULE(CONFIG_TEST_BPF)
+EXPORT_SYMBOL_GPL(bpf_jit_harden);
+#endif
 
 static void
 bpf_prog_ksym_set_addr(struct bpf_prog *prog)
kernel/bpf/token.c +0 -0
index dcbec1a0dfb3..aed98a958c73 100644

kernel/bpf/token.c bool bpf_token_capable(const struct bpf_token *token, int cap) · line 31

Quoted 6 lines
kernel/bpf/token.c +3 -0
@@ -26,6 +26,9 @@ bool bpf_token_capable(const struct bpf_token *token, int cap)
                return false;
        return true;
 }
+#if IS_MODULE(CONFIG_TEST_BPF)
+EXPORT_SYMBOL_GPL(bpf_token_capable);
+#endif

I don't like the extra exports and hack in patch 2 just to test jit blinding.

In general lib/test_bpf is there for testing JITs
path that are impossible to do with normal asm/c from selftests.

I don't think jit blinding false into this category. The current code coverage is good enough.

pw-bot: cr

Quoted 37 lines
kernel/bpf/token.c +0 -0
 void bpf_token_inc(struct bpf_token *token)
 {
lib/test_bpf.c +17 -3
index c1140bab280d..3469631c0aba 100644
@@ -2700,10 +2700,25 @@ static int __bpf_fill_staggered_jumps(struct bpf_test *self,
                                      u64 r1, u64 r2)
 {
        int size = self->test[0].result - 1;
-       int len = 4 + 3 * (size + 1);
        struct bpf_insn *insns;
-       int off, ind;
+       int len, off, ind;
 
+       /* Constant blinding triples the size of each instruction making use
+        * of immediate values. Tweak the test to not overflow jump offsets.
+        */
+       if (bpf_jit_blinding_enabled(NULL)) {
+               int bloat_factor = 2 * 3;
+
+               if (BPF_SRC(jmp->code) == BPF_K)
+                       bloat_factor += 3;
+
+               size /= bloat_factor;
+               size &= ~1;
+
+               self->test[0].result = size + 1;
+       }
+
+       len = 4 + 3 * (size + 1);
        insns = kmalloc_array(len, sizeof(*insns), GFP_KERNEL);
        if (!insns)
                return -ENOMEM;
--

2.30.2