diff mbox series

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

Message ID 20241104200452.2651529-4-minipli@grsecurity.net (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series test_bpf.ko blinding fixes | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 5 this patch: 5
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers warning 10 maintainers not CCed: kpsingh@kernel.org martin.lau@linux.dev akpm@linux-foundation.org eddyz87@gmail.com sdf@fomichev.me john.fastabend@gmail.com song@kernel.org jolsa@kernel.org haoluo@google.com yonghong.song@linux.dev
netdev/build_clang success Errors and warnings before: 3 this patch: 3
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 111 this patch: 111
netdev/checkpatch warning CHECK: Please use a blank line after function/struct/union/enum declarations
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18-O2
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-18 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17-O2
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc

Commit Message

Mathias Krause Nov. 4, 2024, 8:04 p.m. UTC
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 <johan.almbladh@anyfinetworks.com>
Signed-off-by: Mathias Krause <minipli@grsecurity.net>
---
 kernel/bpf/core.c  |  3 +++
 kernel/bpf/token.c |  3 +++
 lib/test_bpf.c     | 19 +++++++++++++++++--
 3 files changed, 23 insertions(+), 2 deletions(-)

Comments

Alexei Starovoitov Nov. 16, 2024, 6:12 p.m. UTC | #1
On Mon, Nov 4, 2024 at 12:05 PM Mathias Krause <minipli@grsecurity.net> wrote:
>
> 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 <johan.almbladh@anyfinetworks.com>
> Signed-off-by: Mathias Krause <minipli@grsecurity.net>
> ---
>  kernel/bpf/core.c  |  3 +++
>  kernel/bpf/token.c |  3 +++
>  lib/test_bpf.c     | 19 +++++++++++++++++--
>  3 files changed, 23 insertions(+), 2 deletions(-)
>
> diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
> index 233ea78f8f1b..fe7eada54d4b 100644
> --- a/kernel/bpf/core.c
> +++ b/kernel/bpf/core.c
> @@ -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)
> diff --git a/kernel/bpf/token.c b/kernel/bpf/token.c
> index dcbec1a0dfb3..aed98a958c73 100644
> --- a/kernel/bpf/token.c
> +++ b/kernel/bpf/token.c
> @@ -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

>  void bpf_token_inc(struct bpf_token *token)
>  {
> diff --git a/lib/test_bpf.c b/lib/test_bpf.c
> index c1140bab280d..3469631c0aba 100644
> --- a/lib/test_bpf.c
> +++ b/lib/test_bpf.c
> @@ -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
>
diff mbox series

Patch

diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
index 233ea78f8f1b..fe7eada54d4b 100644
--- a/kernel/bpf/core.c
+++ b/kernel/bpf/core.c
@@ -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)
diff --git a/kernel/bpf/token.c b/kernel/bpf/token.c
index dcbec1a0dfb3..aed98a958c73 100644
--- a/kernel/bpf/token.c
+++ b/kernel/bpf/token.c
@@ -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)
 {
diff --git a/lib/test_bpf.c b/lib/test_bpf.c
index c1140bab280d..3469631c0aba 100644
--- a/lib/test_bpf.c
+++ b/lib/test_bpf.c
@@ -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;