Re: [PATCH v10] m68k, bpf: Add initial BPF JIT compiler support

From: Kuan-Wei Chiu

Date: Tue Nov 17 2026 - 06:15:41 EST


On Tue, Nov 17, 2026 at 10:36:39AM +0000, bot+bpf-ci@xxxxxxxxxx wrote:
> > m68k, bpf: Add initial BPF JIT compiler support
>
> TL;DR: bpf_int_jit_compile() has no check for an already-JITed prog, so
> jit_subprogs()'s second pass may leak the first executable image, e.g. with an
> exception_callback tag, letting a CAP_BPF user exhaust JIT memory.
>
> > diff --git a/arch/m68k/net/bpf_jit_comp.c b/arch/m68k/net/bpf_jit_comp.c
> > --- /dev/null
> > +++ b/arch/m68k/net/bpf_jit_comp.c
>
> [ ... ]
>
> > +struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog)
> > +{
> > + struct bpf_binary_header *header;
> > + struct jit_ctx ctx;
> > + unsigned int image_size;
> > + u8 *image_ptr;
> > +
> > + if (!prog->jit_requested)
> > + return prog;
> > +
>
> [ ... ]
>
> > + header = bpf_jit_binary_alloc(image_size, &image_ptr, 2, jit_fill_hole);
> > + if (!header)
> > + goto out_off;
>
> [ ... ]
>
> > + prog->bpf_func = (void *)ctx.target;
> > + prog->jited = 1;
> > + prog->jited_len = image_size;
> > + goto out_off;
>
> If jit_subprogs() calls bpf_int_jit_compile() a second time on a func[i]
> that is already JITed, doesn't this allocate a new header and overwrite
> prog->bpf_func, leaving the first image unfreed?
>
> jit_subprogs() in kernel/bpf/fixups.c calls bpf_int_jit_compile() twice
> for every subprog, once in the first loop and again in the extra pass:
>
> old_bpf_func = func[i]->bpf_func;
> tmp = bpf_int_jit_compile(env, func[i]);
> if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) {
> verbose(env, "JIT doesn't support bpf-to-bpf calls\n");
> err = -ENOTSUPP;
> goto out_free;
> }
>
> On the second call only prog->jit_requested is checked, which is still
> set, so both passes run again. bpf_jit_binary_alloc() returns a new
> header and prog->bpf_func is overwritten. The out_free path then calls
> bpf_jit_free(func[i]), which frees only the header that
> bpf_jit_binary_hdr(func[i]) finds, i.e. the new one. The first image
> and its bpf_jit_charge_modmem() charge are never released.
>
> Rejecting BPF_PSEUDO_CALL in emit_call() and BPF_PSEUDO_FUNC in
> build_insn() doesn't cover every path to the extra pass. A program whose
> main function has an "exception_callback:" BTF decl tag gets a second
> subprog via bpf_find_exception_callback_insn_off() -> add_subprog() in
> kernel/bpf/verifier.c. The main program doesn't need to call that
> subprog or use bpf_throw(). Both functions JIT successfully in the first
> loop since they contain no calls, and the extra pass then leaks func[0]'s
> first image on every load. A CAP_BPF user (needed for BTF) loading such
> programs repeatedly could leak executable memory without bound and keep
> growing bpf_jit_current.
>
> The JITs that support bpf-to-bpf calls (x86, arm64, riscv, powerpc,
> loongarch, parisc, arc) detect the extra pass via prog->jited or jit_data
> and reuse the image. Would an early return when prog->jited is already
> set work here? jit_subprogs() would then see the same bpf_func and
> finish, and that seems safe because any real call or callback has already
> made the first pass fail.

Since this initial JIT doesn't support subprogs anyway, I'll just
reject prog->is_func upfront in bpf_int_jit_compile() in v11.

Regards,
Kuan-Wei