Check that userspace cannot submit the kernel-private IRQ save and restore opcodes directly. Add nested IRQ save/restore tests in the main program and across a BPF subprogram call. Inspect translated instructions for the expected operation counts, flags register and reserved fields. Check the native x86-64 or arm64 instruction sequences when inlining is enabled, and the kfunc fallback when the kernel configuration disables it. Run the JIT tests with hardening enabled and restore the original sysctl setting afterwards. The selftests are discovered automatically by the existing build rules. Signed-off-by: Emil Tsalapatis --- .../selftests/bpf/prog_tests/irq_jit.c | 128 ++++++++++++++++++ tools/testing/selftests/bpf/progs/irq.c | 24 ++++ tools/testing/selftests/bpf/progs/irq_jit.c | 55 ++++++++ 3 files changed, 207 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/irq_jit.c create mode 100644 tools/testing/selftests/bpf/progs/irq_jit.c diff --git a/tools/testing/selftests/bpf/prog_tests/irq_jit.c b/tools/testing/selftests/bpf/prog_tests/irq_jit.c new file mode 100644 index 000000000000..3d5dc3c77edb --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/irq_jit.c @@ -0,0 +1,128 @@ +#include +#include +#include +#include "irq_jit.skel.h" + +static void check_irq_native(int prog_fd, __u32 jited_len) +{ +#if defined(__x86_64__) + static const __u8 save[] = { 0x9c, 0x41, 0x5a, 0xfa, 0x4c, 0x89, 0x17 }; + static const __u8 restore[] = { + 0x4c, 0x8b, 0x17, 0x49, 0xf7, 0xc2, 0x00, 0x02, 0x00, 0x00, 0x74, 0x01, 0xfb, + }; +#elif defined(__aarch64__) + static const __u8 save[] = { 0xdf, 0x43, 0x03, 0xd5 }; + static const __u8 restore[] = { 0x0a, 0x00, 0x40, 0xf9, 0x2a, 0x42, 0x1b, 0xd5 }; + static const __u8 pmr_save[] = { 0x0b, 0x46, 0x18, 0xd5 }; + static const __u8 pmr_restore[] = { 0x0a, 0x00, 0x40, 0xf9, 0x0a, 0x46, 0x18, 0xd5 }; +#else + return; +#endif +#if defined(__x86_64__) || defined(__aarch64__) + struct bpf_prog_info info = {}; + __u32 info_len = sizeof(info); + __u8 *image; + int err; + + if (!ASSERT_GT(jited_len, 0, "jited_len")) + return; + image = malloc(jited_len); + if (!ASSERT_OK_PTR(image, "jited_image")) + return; + info.jited_prog_len = jited_len; + info.jited_prog_insns = ptr_to_u64(image); + err = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len); + if (!ASSERT_OK(err, "jited_prog_info")) + goto out; +#if defined(__x86_64__) + ASSERT_TRUE(memmem(image, jited_len, save, sizeof(save)), "native_irq_save"); + ASSERT_TRUE(memmem(image, jited_len, restore, sizeof(restore)), "native_irq_restore"); +#else + ASSERT_TRUE(memmem(image, jited_len, save, sizeof(save)) || + memmem(image, jited_len, pmr_save, sizeof(pmr_save)), "native_irq_save"); + ASSERT_TRUE(memmem(image, jited_len, restore, sizeof(restore)) || + memmem(image, jited_len, pmr_restore, sizeof(pmr_restore)), "native_irq_restore"); +#endif +out: + free(image); +#endif +} + +static void check_irq_insns(int prog_fd, bool expect_inline) +{ + struct bpf_prog_info info = {}; + __u32 info_len = sizeof(info); + struct bpf_insn *insns; + __u32 xlated_len; + int save_count = 0, restore_count = 0; + int err; + + err = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len); + if (!ASSERT_OK(err, "prog_info")) + return; + xlated_len = info.xlated_prog_len; + insns = malloc(xlated_len); + if (!ASSERT_OK_PTR(insns, "xlated_insns")) + return; + memset(&info, 0, sizeof(info)); + info.xlated_prog_len = xlated_len; + info.xlated_prog_insns = ptr_to_u64(insns); + err = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len); + if (!ASSERT_OK(err, "xlated_prog_info")) + goto out; + for (size_t insn_idx = 0; insn_idx < info.xlated_prog_len / sizeof(*insns); insn_idx++) { + struct bpf_insn *insn = &insns[insn_idx]; + + if (insn->code != (BPF_ST | 0xe0)) + continue; + ASSERT_EQ((__u32)insn->dst_reg, BPF_REG_1, "flags_reg"); + ASSERT_EQ((__u32)insn->src_reg, 0, "reserved_src_reg"); + ASSERT_EQ(insn->off, 0, "reserved_off"); + if (insn->imm == 0) + save_count++; + else if (insn->imm == 1) + restore_count++; + else + ASSERT_FAIL("unexpected IRQ operation %d", insn->imm); + } + ASSERT_EQ(save_count, expect_inline ? 2 : 0, "save_count"); + ASSERT_EQ(restore_count, expect_inline ? 2 : 0, "restore_count"); + if (expect_inline) + check_irq_native(prog_fd, info.jited_prog_len); +out: + free(insns); +} + +void test_irq_jit(void) +{ + struct irq_jit *skel; + const char *harden = "/proc/sys/net/core/bpf_jit_harden"; + char old_harden[32]; + int err; + + if (sysctl_set_or_fail(harden, old_harden, "2")) + return; + skel = irq_jit__open_and_load(); + if (!ASSERT_OK_PTR(skel, "irq_jit_open_and_load")) + goto restore_harden; + for (int prog_idx = 0; prog_idx < 2; prog_idx++) { + struct bpf_program *prog = prog_idx ? skel->progs.irq_jit_subprog : + skel->progs.irq_jit_basic; + LIBBPF_OPTS(bpf_test_run_opts, opts, + .data_in = &pkt_v4, + .data_size_in = sizeof(pkt_v4), + .repeat = 16, + ); + + if (!test__start_subtest(bpf_program__name(prog))) + continue; + err = bpf_prog_test_run_opts(bpf_program__fd(prog), &opts); + if (!ASSERT_OK(err, "test_run")) + continue; + ASSERT_LE(opts.retval, 1, "test_retval"); + check_irq_insns(bpf_program__fd(prog), opts.retval == 0); + } + irq_jit__destroy(skel); +restore_harden: + sysctl_set_or_fail(harden, NULL, old_harden); +} diff --git a/tools/testing/selftests/bpf/progs/irq.c b/tools/testing/selftests/bpf/progs/irq.c index 50727fa9b11d..ffb74d3c55c0 100644 --- a/tools/testing/selftests/bpf/progs/irq.c +++ b/tools/testing/selftests/bpf/progs/irq.c @@ -11,6 +11,30 @@ extern void bpf_local_irq_save(unsigned long *) __weak __ksym; extern void bpf_local_irq_restore(unsigned long *) __weak __ksym; extern int bpf_copy_from_user_str(void *dst, u32 dst__sz, const void *unsafe_ptr__ign, u64 flags) __weak __ksym; +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +#define IRQ_FLAGS_REG_BYTE 0x01 +#else +#define IRQ_FLAGS_REG_BYTE 0x10 +#endif + +SEC("?tc") +__failure __msg("unknown opcode e2") +__naked void irq_internal_save_rejected(void) +{ + asm volatile (".byte 0xe2, %[flags_reg]; .short 0; .long 0; r0 = 0; exit;" + : : __imm_const(flags_reg, IRQ_FLAGS_REG_BYTE) + : __clobber_all); +} + +SEC("?tc") +__failure __msg("unknown opcode e2") +__naked void irq_internal_restore_rejected(void) +{ + asm volatile (".byte 0xe2, %[flags_reg]; .short 0; .long 1; r0 = 0; exit;" + : : __imm_const(flags_reg, IRQ_FLAGS_REG_BYTE) + : __clobber_all); +} + struct bpf_res_spin_lock lockA __hidden SEC(".data.A"); struct bpf_res_spin_lock lockB __hidden SEC(".data.B"); diff --git a/tools/testing/selftests/bpf/progs/irq_jit.c b/tools/testing/selftests/bpf/progs/irq_jit.c new file mode 100644 index 000000000000..45834448333b --- /dev/null +++ b/tools/testing/selftests/bpf/progs/irq_jit.c @@ -0,0 +1,55 @@ +#include +#include + +extern void bpf_local_irq_save(unsigned long *) __ksym; +extern void bpf_local_irq_restore(unsigned long *) __ksym; +extern bool CONFIG_TRACE_IRQFLAGS __kconfig __weak; +extern bool CONFIG_PARAVIRT_XXL __kconfig __weak; +extern bool CONFIG_ARM64_DEBUG_PRIORITY_MASKING __kconfig __weak; + +static __always_inline int irq_jit_result(void) +{ + bool fallback = CONFIG_TRACE_IRQFLAGS; + +#if defined(__TARGET_ARCH_x86) + fallback |= CONFIG_PARAVIRT_XXL; +#elif defined(__TARGET_ARCH_arm64) + fallback |= CONFIG_ARM64_DEBUG_PRIORITY_MASKING; +#else + fallback = true; +#endif + return fallback; +} + +SEC("tc") +int irq_jit_basic(struct __sk_buff *ctx) +{ + unsigned long outer_flags, inner_flags; + + bpf_local_irq_save(&outer_flags); + bpf_local_irq_save(&inner_flags); + bpf_local_irq_restore(&inner_flags); + bpf_local_irq_restore(&outer_flags); + return ctx->len ? irq_jit_result() : 2; +} + +static __noinline void irq_jit_nested(void) +{ + unsigned long flags; + + bpf_local_irq_save(&flags); + bpf_local_irq_restore(&flags); +} + +SEC("tc") +int irq_jit_subprog(struct __sk_buff *ctx) +{ + unsigned long flags; + + bpf_local_irq_save(&flags); + irq_jit_nested(); + bpf_local_irq_restore(&flags); + return ctx->len ? irq_jit_result() : 2; +} + +char _license[] SEC("license") = "GPL"; -- 2.52.0