> powerpc32/bpf: don't fold MOV into jump targets

TL;DR:
- ppc32 MOV folding may still miscompile jump targets for classic BPF (no
  verifier env) and after verifier rewrites move jump_target
- Possible wrong results or an out-of-bounds access

> diff --git a/arch/powerpc/net/bpf_jit_comp32.c 
> b/arch/powerpc/net/bpf_jit_comp32.c
> --- a/arch/powerpc/net/bpf_jit_comp32.c
> +++ b/arch/powerpc/net/bpf_jit_comp32.c

[ ... ]

> @@ -368,7 +368,7 @@ int bpf_jit_build_body(struct bpf_prog *fp, u32 *image, 
> u32 *fimage, struct code
>                   (BPF_CLASS(prevcode) == BPF_ALU64 || BPF_CLASS(prevcode) == 
> BPF_ALU) &&
>                   BPF_OP(prevcode) == BPF_MOV && BPF_SRC(prevcode) == BPF_X &&
>                   insn[i - 1].dst_reg == insn[i].dst_reg && insn[i - 1].imm 
> != 1 &&
> -                 insn[i - 1].off == 0) {
> +                 insn[i - 1].off == 0 && !bpf_insn_is_jump_target(env, fp, 
> i)) {

[ ... ]

> diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c
> --- a/kernel/bpf/core.c
> +++ b/kernel/bpf/core.c

[ ... ]

> @@ -1984,6 +1984,15 @@ bool bpf_insn_is_indirect_target(const struct 
> bpf_verifier_env *env, const struc
>       return env->insn_aux_data[insn_idx].indirect_target;
>  }
>
> +bool bpf_insn_is_jump_target(const struct bpf_verifier_env *env, const 
> struct bpf_prog *prog,
> +                              int insn_idx)
> +{
> +     if (!env)
> +             return false;

Without a verifier env this reports "not a jump target", so the ppc32 JIT
keeps folding the MOV into the next ALU insn even when that insn is a
branch target.  The NULL default is safe in bpf_insn_is_indirect_target()
because programs without a verifier have no indirect jumps, but they do
have direct jumps.

bpf_prog_select_runtime() calls __bpf_prog_select_runtime(NULL, fp, err),
which reaches bpf_int_jit_compile(NULL, fp).  This is the path for classic
BPF (bpf_migrate_filter(): SO_ATTACH_FILTER, seccomp, bpf_prog_create()).
bpf_convert_filter() emits:

    case BPF_MISC | BPF_TXA:
            *insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_X);

so a classic filter such as:

    jeq #0, L, 0
    txa
 L: add #1
    ret a

becomes 'if A == 0 goto L; A = X; L: w0 += 1'.  The fold condition
matches, and the taken branch computes X + 1 instead of A + 1.

Can this path, reachable without privileges, still miscompile?  Should the
NULL env case return true, or should the JIT compute branch targets itself
from fp->insnsi?

> +     insn_idx += prog->aux->subprog_start;
> +     return env->insn_aux_data[insn_idx].jump_target;

Is jump_target still valid at JIT time?  It is set once by
mark_jump_target() in bpf_check_cfg(), but the rewrite passes later in
bpf_check() move the instructions around.

When a pass replaces an insn with a multi-insn patch through
bpf_patch_list_add(), jumps to the old insn land on the first insn of the
patch, while patch_list_adj_aux() copies the old aux data (including
jump_target) to the last insn of the patch.  Only indirect_target and
nospec are moved to the landing insn:

    if (p->target != p->self) {
            slot[p->target].indirect_target = last->indirect_target;
            slot[p->target].nospec = last->nospec;

PowerPC has bpf_jit_needs_zext() == true, so
bpf_opt_subreg_zext_lo32_rnd_hi32() rewrites ALU32 insns whose upper half
is live into [insn, BPF_ZEXT_REG(dst)] via bpf_patch_list_add().  After
that, the ALU32 insn at the jump target has jump_target == 0 and the zext
after it has jump_target == 1.

For example:

    if w3 > 15 goto L
    w1 = w3                // MOV X, off 0
 L: w1 += 1                // ALU32, jump target
    r1 <<= 3               // reads r1 as 64-bit, so zext is inserted after L

bpf_insn_is_jump_target(env, fp, L) now returns false, so
bpf_jit_build_body() still folds the MOV into the ADD, and the taken
branch lands on the fused 'w1 = w3 + 1'.  Since w3 is unbounded here, the
runtime value differs from what the verifier tracked, which could lead to
an out-of-bounds access.

Jumps inserted by rewrites also never call mark_jump_target().  The other
users of bpf_is_jump_target() (verifier.c) run before any rewrite, so this
is the first consumer after the rewrites.

Would it be more robust to have the ppc32 JIT compute branch targets from
fp->insnsi (as arm64 does with ctx.tb/find_tb()), or to have
patch_list_adj_aux() move jump_target to p->target and mark the targets of
inserted jumps?

> +}

[ ... ]


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/46701268444

Reply via email to