Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794314052; b=bBJ5wgVabR2Q4ZH/Zqc2tz5WAp+8SrFyNUqTQThpuqC2dE5GxhOp7r6M3hW36NNghJjP UCqsHOtOKwS77TtDQfIAUj0UfPw90qQ2DhkXYCTVwEg7zZ76Wfw1koMrs7EiM4aRef1up 7FrLf4iG+f3O6oYp+YtYEkruMV7R7zEvdhIwJU2HduhBJEMY2hupEmM+1N8U3/4a/2gsp KHlhUcyP7nXZf7euw3pm4LaVis9rhboDkCzwY9/zsNafzZ0zYP76QHttuyrjhEGqYs/OV JcmC1bwdyU9qPfeFYU9294ow3mHp8+/6uRplXRiV3tzCgPWractEM64S8eV0qU62d1A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794314052; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=Kr07HqSk1dXFk+p2tNfIUkhP/sScvBmev9NavxvM4dA=; b=p/0Ir2Fv0zFId5hyYvqZN+Yg445Gb18ab2IbeByGcnqgDNnKOKySYqbeo+3HpeyZXe9/ 3p56B/Ex8uhLqI9+EqQwvMD+cgAxlSTZQlo3UZIKlSsidNYPYl8QLoS7Darzi2onf3ZIb bsXg5IAP17japI/gCg3ywrz+YKfqNormITe2dv9xwibR76Zbxg7eIcjxQPRr2Y+E1I2ec H5aapknPkhdY494ClvKwNoj2/9DeNhgZgUF+54DIAkTaZ9lfNoYBmoo3SSc/UsGGnsLy8 rXZM91RPqBMEWoKi4rMmcQKruiCUx2YlIrLaPnTWRH2WcfUioItOs7k5R4MVqMBNd5Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=aH3z+2NS; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B6CC606A339 for ; Tue, 10 Nov 2026 12:34:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=aH3z+2NS Received: by smtp.kernel.org (Postfix) with ESMTPSA id E35B51F0089F; Tue, 10 Nov 2026 12:34:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1794314052; bh=Kr07HqSk1dXFk+p2tNfIUkhP/sScvBmev9NavxvM4dA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=aH3z+2NSJxVFpCykUaPbHaRJ6B1DJjuyuIJmYmlPWUKqJ2qdUYMJanhkorBlB+eov su//J0DyM8V27ZIAMe+pRNoULg6MB6zmXODtdRePhTPWtxs3rUfoFxCqh7KTwgvDZk Yr2YKefKzpjVExUFzDu2bjxsVtS/xuSmBFTBZz1jiS/5dQpssPddNgQnlIk7N2o88l 7QR0eS7jbNQpit9pjeFEEsnsFGTb3+FTjybKt+jTteDV8D0KBLKT7ht7NZv08c1o6V s9s+fCRRMuVnJW64ADx1bZTDKoLP7nhlmCAwswAVN5SE5K2TuPovucxva0fq93+M0U ce2ZwWIh7g3Bg== From: Sasha Levin To: stable@vger.kernel.org Cc: Abhishek Dubey , sashiko-bot@kernel.org, Hari Bathini , Saket Kumar Bhaskar , Yeswanth Krishna Tellakula , R Nageswara Sastry , Madhavan Srinivasan , Sasha Levin Subject: [PATCH 6.18.y] powerpc/bpf: fix buffer overflow in JIT for large BPF programs Date: Tue, 10 Nov 2026 07:34:09 -0500 Message-ID: <20261110123328.1215640-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261107050746-failed-6.18-78ce55b7913a@kernel.org> References: <20261107050746-failed-6.18-78ce55b7913a@kernel.org> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Abhishek Dubey [ Upstream commit 78ce55b7913a78e1687306668e078b86bc6508ee ] 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: 0ffdbce6f4a8 ("powerpc/bpf: Handle large branch ranges with BPF_EXIT") Cc: stable@vger.kernel.org Signed-off-by: Hari Bathini Signed-off-by: Abhishek Dubey Signed-off-by: Saket Kumar Bhaskar Tested-by: Yeswanth Krishna Tellakula Tested-by: R Nageswara Sastry Link: https://lore.kernel.org/bpf/20260529015855.364704-2-adubey@linux.ibm.com/T/#mfcb23909d977b949727cca4f59ee56a13fd69b92 Signed-off-by: Madhavan Srinivasan Link: https://patch.msgid.link/eebffa997e07781b384c4480e2b03c5b87c1aeb1.1789982239.git.skb99@linux.ibm.com [ sashal: Replaced goto out_err with fp = org_fp followed by goto out_addrs because the out_err label is absent. ] Signed-off-by: Sasha Levin --- arch/powerpc/net/bpf_jit.h | 7 +++++++ arch/powerpc/net/bpf_jit_comp.c | 35 +++++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/arch/powerpc/net/bpf_jit.h b/arch/powerpc/net/bpf_jit.h index 8334cd667bba1..ae2c92a8b6e77 100644 --- a/arch/powerpc/net/bpf_jit.h +++ b/arch/powerpc/net/bpf_jit.h @@ -14,6 +14,13 @@ #include #include +/* + * 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 cedab25aa3a82..829b60ae485ff 100644 --- a/arch/powerpc/net/bpf_jit_comp.c +++ b/arch/powerpc/net/bpf_jit_comp.c @@ -99,11 +99,10 @@ void bpf_jit_build_fentry_stubs(u32 *image, struct codegen_context *ctx) int bpf_jit_emit_exit_insn(u32 *image, 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; @@ -223,6 +222,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp) */ 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)) { fp = org_fp; goto out_addrs; @@ -259,10 +259,13 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp) 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, @@ -272,11 +275,27 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *fp) fp = org_fp; goto out_addrs; } + addrs[fp->len] = cgctx.idx * 4; bpf_jit_build_epilogue(code_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); + fp = org_fp; + goto out_addrs; + } } if (bpf_jit_enable > 1) @@ -336,7 +355,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) return 0; if (!fp->aux->extable || -- 2.53.0