test_bpf.ko blinding fixes
Subsystems (3)
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
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.
--- lib/test_bpf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/test_bpf.c +1 -1
@@ -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
PATCH 2/3 bpf: Allow calling bpf_jit_blinding_enabled() with a NULL program
In reply to Mathias KrauseTo 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
@@ -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
PATCH 3/3 bpf/tests: Make staggered jump tests constant blinding compatible
In reply to Mathias KrauseThe "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.
--- 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
@@ -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
@@ -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
@@ -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
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]>Signed-off-by Mathias Krause <[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@@ -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
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
-0void bpf_token_inc (struct bpf_token *token) {lib/test_bpf.c +17
-3@@ -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