From: Hari Bathini <hbathini@linux.ibm.com>
To: Saket Kumar Bhaskar <skb99@linux.ibm.com>,
bpf@vger.kernel.org, linuxppc-dev@lists.ozlabs.org
Cc: maddy@linux.ibm.com, ast@kernel.org, andrii@kernel.org,
daniel@iogearbox.net, shuah@kernel.org,
linux-kselftest@vger.kernel.org, stable@vger.kernel.org,
venkat88@linux.ibm.com, yeswanth@linux.ibm.com
Subject: Re: [PATCH v10 7/8] powerpc/bpf: fix buffer overflow in JIT for large BPF programs
Date: Thu, 6 Aug 2026 22:56:48 +0530 [thread overview]
Message-ID: <a4d54bca-2fea-4dbd-b5dc-e9229499da47@linux.ibm.com> (raw)
In-Reply-To: <45cfcf00a9ca1ec0a06c516fabc73e284d529dc1.1785906979.git.skb99@linux.ibm.com>
On 05/08/26 11:37 am, Saket Kumar Bhaskar wrote:
> From: Abhishek Dubey <adubey@linux.ibm.com>
>
> During size calculation in pass-0, exit_addr is 0 since addrs[fp->len]
> is not yet populated. bpf_jit_emit_exit_insn() treats a zero exit_addr
> as in-range and skips bpf_jit_build_epilogue(), so the alternate inline
> epilogue instructions are not counted in alloclen.
>
> In later passes, if the real exit_addr falls outside the 32MB branch
> range, the full inline epilogue is emitted into the already-allocated
> buffer, writing past its end and corrupting adjacent memory.
>
> Fix by ensuring exit_addr is non-zero before treating it as in-range,
> so pass-0 always falls through to bpf_jit_build_epilogue() and
> conservatively accounts for all epilogue instructions in alloclen.
> Also range check alt_exit_addr directly in the else-if condition.
>
> Since exit_addr handling now falls through to the epilogue, two
> related issues in bpf_int_jit_compile() must also be addressed:
>
> 1. Reset cgctx.alt_exit_addr before the second size-calculation pass.
> Without this, a stale alt_exit_addr from the first pass causes the
> second pass to emit a single jump instead of the full epilogue,
> undercounting alloclen and reintroducing the overflow.
>
> 2. Recompute addrs[fp->len] at the end of each code-generation pass.
> The larger pass-0 body can shrink in later passes as out-of-range
> exits settle into in-range jumps; a stale addrs[fp->len] would
> leave exit branches targeting past the real (shrunken) epilogue.
>
> Because shrinkage in a later pass can move the epilogue offset, the
> fixed two-pass loop is no longer sufficient: an exit that was out of
> range in an earlier pass may fall in range once the epilogue offset
> shrinks, shrinking the body further and overwriting the start of the
> epilogue. Convert the code-generation loop to iterate until the
> program size converges, bounded by CODEGEN_MAX_PASSES, and fail the
> JIT if it does not converge.
>
> Reported-by: sashiko-bot@kernel.org
> Closes: https://lore.kernel.org/bpf/20260529015855.364704-2-adubey@linux.ibm.com/T/#mfcb23909d977b949727cca4f59ee56a13fd69b92
> Fixes: d243b62b7bd3 ("powerpc64/bpf: Add support for bpf trampolines")
> Cc: stable@vger.kernel.org
> Signed-off-by: Hari Bathini <hbathini@linux.ibm.com>
> Signed-off-by: Abhishek Dubey <adubey@linux.ibm.com>
> Signed-off-by: Saket Kumar Bhaskar <skb99@linux.ibm.com>
> Link: https://lore.kernel.org/bpf/20260529015855.364704-2-adubey@linux.ibm.com/T/#mfcb23909d977b949727cca4f59ee56a13fd69b92
> Tested-by: Yeswanth Krishna Tellakula <yeswanth@linux.ibm.com>
> ---
> arch/powerpc/net/bpf_jit.h | 7 +++++++
> arch/powerpc/net/bpf_jit_comp.c | 34 +++++++++++++++++++++++++--------
> 2 files changed, 33 insertions(+), 8 deletions(-)
>
> diff --git a/arch/powerpc/net/bpf_jit.h b/arch/powerpc/net/bpf_jit.h
> index af510da12d8e..4da8bde92e1e 100644
> --- a/arch/powerpc/net/bpf_jit.h
> +++ b/arch/powerpc/net/bpf_jit.h
> @@ -14,6 +14,13 @@
> #include <asm/ppc-opcode.h>
> #include <linux/build_bug.h>
>
> +/*
> + * We need at least 2 passes for proper code generation, and may need
> + * additional passes if code size changes between passes.
> + */
> +#define CODEGEN_MIN_PASSES 2
> +#define CODEGEN_MAX_PASSES 3
> +
> #ifdef CONFIG_PPC64_ELF_ABI_V1
> #define FUNCTION_DESCR_SIZE 24
> #else
> diff --git a/arch/powerpc/net/bpf_jit_comp.c b/arch/powerpc/net/bpf_jit_comp.c
> index 8be5ded13a4a..3c20bb13cfd7 100644
> --- a/arch/powerpc/net/bpf_jit_comp.c
> +++ b/arch/powerpc/net/bpf_jit_comp.c
> @@ -128,11 +128,10 @@ void bpf_jit_build_fentry_stubs(u32 *image, u32 *fimage, struct codegen_context
> int bpf_jit_emit_exit_insn(u32 *image, u32 *fimage, struct codegen_context *ctx,
> int tmp_reg, long exit_addr)
> {
> - if (!exit_addr || is_offset_in_branch_range(exit_addr - (ctx->idx * 4))) {
> + if (exit_addr && is_offset_in_branch_range(exit_addr - (long)(ctx->idx * 4))) {
> PPC_JMP(exit_addr);
> - } else if (ctx->alt_exit_addr) {
> - if (WARN_ON(!is_offset_in_branch_range((long)ctx->alt_exit_addr - (ctx->idx * 4))))
> - return -1;
> + } else if (ctx->alt_exit_addr && is_offset_in_branch_range(
> + (long)(ctx->alt_exit_addr) - (long)(ctx->idx * 4))) {
> PPC_JMP(ctx->alt_exit_addr);
> } else {
> ctx->alt_exit_addr = ctx->idx * 4;
> @@ -303,6 +302,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
> */
> if (cgctx.seen & SEEN_TAILCALL || !is_offset_in_branch_range((long)cgctx.idx * 4)) {
> cgctx.idx = 0;
> + cgctx.alt_exit_addr = 0;
> if (bpf_jit_build_body(fp, NULL, NULL, &cgctx, addrs, 0, false))
> goto out_err;
> }
> @@ -335,10 +335,13 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
> code_base = (u32 *)(image + FUNCTION_DESCR_SIZE);
> fcode_base = (u32 *)(fimage + FUNCTION_DESCR_SIZE);
>
> - /* Code generation passes 1-2 */
> - for (pass = 1; pass < 3; pass++) {
> + /* Code generation passes 1-2+, loop until program size converges. */
> + for (pass = 1; pass <= CODEGEN_MAX_PASSES; pass++) {
> + u32 prev_proglen = proglen;
> +
> /* Now build the prologue, body code & epilogue for real. */
> cgctx.idx = 0;
> + cgctx.exentry_idx = 0;
> cgctx.alt_exit_addr = 0;
> bpf_jit_build_prologue(code_base, &cgctx);
> if (bpf_jit_build_body(fp, code_base, fcode_base, &cgctx, addrs, pass,
> @@ -347,11 +350,26 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_pr
> bpf_jit_binary_pack_free(fhdr, hdr);
> goto out_err;
> }
> + addrs[fp->len] = cgctx.idx * 4;
> bpf_jit_build_epilogue(code_base, fcode_base, &cgctx);
>
> + proglen = cgctx.idx * 4;
> +
> if (bpf_jit_enable > 1)
> pr_info("Pass %d: shrink = %d, seen = 0x%x\n", pass,
> - proglen - (cgctx.idx * 4), cgctx.seen);
> + prev_proglen - proglen, cgctx.seen);
> +
> + /* Check if program size has converged, but ensure minimum passes */
> + if (pass >= CODEGEN_MIN_PASSES && proglen == prev_proglen)
> + break;
> +
> + if (pass == CODEGEN_MAX_PASSES && proglen != prev_proglen) {
> + pr_err("BPF JIT: Program did not converge after %d passes\n",
> + CODEGEN_MAX_PASSES);
> + bpf_arch_text_copy(&fhdr->size, &hdr->size, sizeof(hdr->size));
> + bpf_jit_binary_pack_free(fhdr, hdr);
> + goto out_err;
> + }
> }
>
> if (bpf_jit_enable > 1)
> @@ -428,7 +446,7 @@ int bpf_add_extable_entry(struct bpf_prog *fp, u32 *image, u32 *fimage, int pass
> u32 *fixup;
>
> /* Populate extable entries only in the last pass */
> - if (pass != 2)
> + if (pass >= CODEGEN_MIN_PASSES)
This is wrong. Has to be:
if (pass < CODEGEN_MIN_PASSES)
> return 0;
>
> if (!fp->aux->extable ||
- Hari
next prev parent reply other threads:[~2026-08-06 17:27 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <cover.1785906979.git.skb99@linux.ibm.com>
2026-08-05 6:06 ` [PATCH v10 1/8] powerpc/bpf: fix alignment of long branch trampoline address Saket Kumar Bhaskar
2026-08-05 6:21 ` sashiko-bot
2026-08-06 17:15 ` Hari Bathini
2026-08-05 6:06 ` [PATCH v10 2/8] powerpc/bpf: Move out dummy_tramp_addr after Long branch stub Saket Kumar Bhaskar
2026-08-06 17:18 ` Hari Bathini
2026-08-05 6:06 ` [PATCH v10 3/8] selftest/bpf: Fixing powerpc JIT disassembly failure Saket Kumar Bhaskar
2026-08-05 7:37 ` bot+bpf-ci
2026-08-06 17:19 ` Hari Bathini
2026-08-06 17:20 ` Hari Bathini
2026-08-05 6:07 ` [PATCH v10 4/8] selftest/bpf: Enable verifier selftest for powerpc64 Saket Kumar Bhaskar
2026-08-05 7:07 ` bot+bpf-ci
2026-08-06 17:22 ` Hari Bathini
2026-08-05 6:07 ` [PATCH v10 5/8] powerpc64/bpf: fix compare instruction emitted for tailcall Saket Kumar Bhaskar
2026-08-06 17:24 ` Hari Bathini
2026-08-05 6:07 ` [PATCH v10 6/8] selftest/bpf: Add tailcall verifier selftest for powerpc64 Saket Kumar Bhaskar
2026-08-05 6:14 ` sashiko-bot
2026-08-05 6:07 ` [PATCH v10 7/8] powerpc/bpf: fix buffer overflow in JIT for large BPF programs Saket Kumar Bhaskar
2026-08-05 6:16 ` sashiko-bot
2026-08-05 7:22 ` bot+bpf-ci
2026-08-06 17:26 ` Hari Bathini [this message]
2026-08-05 6:07 ` [PATCH v10 8/8] powerpc64/bpf: fix percpu private stack leak on JIT failure Saket Kumar Bhaskar
2026-08-05 6:18 ` sashiko-bot
2026-08-06 17:27 ` Hari Bathini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=a4d54bca-2fea-4dbd-b5dc-e9229499da47@linux.ibm.com \
--to=hbathini@linux.ibm.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=linux-kselftest@vger.kernel.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=maddy@linux.ibm.com \
--cc=shuah@kernel.org \
--cc=skb99@linux.ibm.com \
--cc=stable@vger.kernel.org \
--cc=venkat88@linux.ibm.com \
--cc=yeswanth@linux.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox