]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests/bpf: test instructions arrays with blinding
authorAnton Protopopov <a.s.protopopov@gmail.com>
Wed, 5 Nov 2025 09:04:04 +0000 (09:04 +0000)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 6 Nov 2025 01:53:22 +0000 (17:53 -0800)
Add a specific test for instructions arrays with blinding enabled.

Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/r/20251105090410.1250500-7-a.s.protopopov@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/prog_tests/bpf_insn_array.c

index fb74af020eee65bb956952c8d77052cb0b7562e6..269870bec94117ef951e3fe2a734e148103c96de 100644 (file)
@@ -227,6 +227,98 @@ static void check_incorrect_index(void)
        check_mid_insn_index();
 }
 
+static int set_bpf_jit_harden(char *level)
+{
+       char old_level;
+       int err = -1;
+       int fd = -1;
+
+       fd = open("/proc/sys/net/core/bpf_jit_harden", O_RDWR | O_NONBLOCK);
+       if (fd < 0) {
+               ASSERT_FAIL("open .../bpf_jit_harden returned %d (errno=%d)", fd, errno);
+               return -1;
+       }
+
+       err = read(fd, &old_level, 1);
+       if (err != 1) {
+               ASSERT_FAIL("read from .../bpf_jit_harden returned %d (errno=%d)", err, errno);
+               err = -1;
+               goto end;
+       }
+
+       lseek(fd, 0, SEEK_SET);
+
+       err = write(fd, level, 1);
+       if (err != 1) {
+               ASSERT_FAIL("write to .../bpf_jit_harden returned %d (errno=%d)", err, errno);
+               err = -1;
+               goto end;
+       }
+
+       err = 0;
+       *level = old_level;
+end:
+       if (fd >= 0)
+               close(fd);
+       return err;
+}
+
+static void check_blindness(void)
+{
+       struct bpf_insn insns[] = {
+               BPF_MOV64_IMM(BPF_REG_0, 4),
+               BPF_MOV64_IMM(BPF_REG_0, 3),
+               BPF_MOV64_IMM(BPF_REG_0, 2),
+               BPF_MOV64_IMM(BPF_REG_0, 1),
+               BPF_EXIT_INSN(),
+       };
+       int prog_fd = -1, map_fd;
+       struct bpf_insn_array_value val = {};
+       char bpf_jit_harden = '@'; /* non-exizsting value */
+       int i;
+
+       map_fd = map_create(BPF_MAP_TYPE_INSN_ARRAY, ARRAY_SIZE(insns));
+       if (!ASSERT_GE(map_fd, 0, "map_create"))
+               return;
+
+       for (i = 0; i < ARRAY_SIZE(insns); i++) {
+               val.orig_off = i;
+               if (!ASSERT_EQ(bpf_map_update_elem(map_fd, &i, &val, 0), 0, "bpf_map_update_elem"))
+                       goto cleanup;
+       }
+
+       if (!ASSERT_EQ(bpf_map_freeze(map_fd), 0, "bpf_map_freeze"))
+               goto cleanup;
+
+       bpf_jit_harden = '2';
+       if (set_bpf_jit_harden(&bpf_jit_harden)) {
+               bpf_jit_harden = '@'; /* open, read or write failed => no write was done */
+               goto cleanup;
+       }
+
+       prog_fd = prog_load(insns, ARRAY_SIZE(insns), &map_fd, 1);
+       if (!ASSERT_GE(prog_fd, 0, "bpf(BPF_PROG_LOAD)"))
+               goto cleanup;
+
+       for (i = 0; i < ARRAY_SIZE(insns); i++) {
+               char fmt[32];
+
+               if (!ASSERT_EQ(bpf_map_lookup_elem(map_fd, &i, &val), 0, "bpf_map_lookup_elem"))
+                       goto cleanup;
+
+               snprintf(fmt, sizeof(fmt), "val should be equal 3*%d", i);
+               ASSERT_EQ(val.xlated_off, i * 3, fmt);
+       }
+
+cleanup:
+       /* restore the old one */
+       if (bpf_jit_harden != '@')
+               set_bpf_jit_harden(&bpf_jit_harden);
+
+       close(prog_fd);
+       close(map_fd);
+}
+
 /* Once map was initialized, it should be frozen */
 static void check_load_unfrozen_map(void)
 {
@@ -382,6 +474,9 @@ static void __test_bpf_insn_array(void)
        if (test__start_subtest("deletions-with-functions"))
                check_deletions_with_functions();
 
+       if (test__start_subtest("blindness"))
+               check_blindness();
+
        /* Check all kinds of operations and related restrictions */
 
        if (test__start_subtest("incorrect-index"))