All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: adubey@linux.ibm.com
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH v4 2/5] powerpc/bpf: Move out dummy_tramp_addr after Long branch stub
Date: Sun, 17 May 2026 18:23:39 +0000	[thread overview]
Message-ID: <20260517182339.AAA9BC2BCB0@smtp.kernel.org> (raw)
In-Reply-To: <20260517214043.12975-3-adubey@linux.ibm.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] The JIT alignment logic for `dummy_tramp_addr` is skipped during the sizing pass but executed during code generation, causing buffer overflows and BPF trampoline corruption.
- [High] The alignment prediction logic for `dummy_tramp_addr` incorrectly adds unscaled bytes instead of instructions, breaking the alignment and causing `bpf_arch_text_poke()` to fail.
- [Medium] Commit message claims an implementation that does not exist in the code.
--

commit e4939a5c93f678ddd053f906d998b2151b87ea0f
Author: Abhishek Dubey <adubey@linux.ibm.com>

powerpc/bpf: Move out dummy_tramp_addr after Long branch stub

This commit moves the dummy_tramp_addr field to the bottom of the long
branch stub to allow uninterrupted disassembly. It also updates the offset
calculation for bpf_arch_text_poke() to reflect the new location.

> Move the long branch address space to the bottom of the long
> branch stub. This allows uninterrupted disassembly until the
> last 8 bytes. Exclude these last bytes from the overall
> program length to prevent failure in assembly generation.

The commit message states that the last bytes are excluded from the overall
program length. Is this description accurate?

Looking at the code, ctx->idx is not decremented, and the length calculation
still appears to include the 8 bytes of dummy_tramp_addr.

> diff --git a/arch/powerpc/net/bpf_jit_comp.c b/arch/powerpc/net/bpf_jit_comp.c
> index ef7614177cb1f..b73bc9295c31d 100644
> --- a/arch/powerpc/net/bpf_jit_comp.c
> +++ b/arch/powerpc/net/bpf_jit_comp.c
> @@ -57,19 +57,21 @@ void bpf_jit_build_fentry_stubs(u32 *image, u32 *fimage, struct codegen_context
[ ... ]
>  #ifdef CONFIG_PPC64
>  	if (fimage && image) {
>  		/*
>  		 * pc points to first instruction of OOL stub,
> -		 * dummy_tramp_addr is past 4/3 instructions depending on
> +		 * dummy_tramp_addr is past 11/10 instructions depending on
>  		 * CONFIG_PPC_FTRACE_OUT_OF_LINE is enabled/not respectively.
>  		 *
>  		 * The decision to emit alignment NOP must depend on the alignment
>  		 * of dummy_tramp_addr field.
>  		 */
>  		unsigned long pc = (unsigned long)fimage + CTX_NIA(ctx);
> -		pc += IS_ENABLED(CONFIG_PPC_FTRACE_OUT_OF_LINE) ? 4 : 3;
> +		pc += IS_ENABLED(CONFIG_PPC_FTRACE_OUT_OF_LINE) ? 11 : 10;
>  
>  		if (!IS_ALIGNED(pc, 8))
>  			EMIT(PPC_RAW_NOP());
>  	}
>  #endif

Does this alignment prediction logic incorrectly add unscaled bytes instead
of instructions?

Since pc is a byte address, adding 11 or 10 bytes instead of multiplying by
4 (the instruction size) guarantees pc is no longer a multiple of 8. This
would cause the NOP to be unconditionally emitted, merely shifting the layout
by 4 bytes instead of dynamically padding it.

If the NOP is unconditionally emitted here, could dummy_tramp_addr remain
8-byte misaligned for many BPF programs, causing patch_ulong() to strictly
reject the address and BPF trampoline attachments to fail with -EINVAL?

Additionally, does this conditional alignment logic cause buffer overflows?

During the sizing pass, fimage is NULL, so the NOP emission is skipped and
the program length is calculated without it. During the code generation
pass, the NOP is emitted, causing the generated code to exceed the allocated
length by 4 bytes. Could this silently overwrite JIT metadata allocated
immediately after the program?

[ ... ]
> @@ -1400,10 +1404,12 @@ int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type old_t,
>  	 * 1. Update the address in the long branch stub:
>  	 * If new_addr is out of range, we will have to use the long branch stub, so patch new_addr
>  	 * here. Otherwise, revert to dummy_tramp, but only if we had patched old_addr here.
> +	 *
> +	 * dummy_tramp_addr moved to bottom of long branch stub.
>  	 */
>  	if ((new_addr && !is_offset_in_branch_range(new_addr - ip)) ||
>  	    (old_addr && !is_offset_in_branch_range(old_addr - ip)))
> -		ret = patch_ulong((void *)(bpf_func_end - bpf_jit_long_branch_stub - SZL),
> +		ret = patch_ulong((void *)(bpf_func_end - SZL), /* SZL: dummy_tramp_addr offset */
>  				  (new_addr && !is_offset_in_branch_range(new_addr - ip)) ?
>  				  (unsigned long)new_addr : (unsigned long)dummy_tramp);
>  	if (ret)

If the extra NOP is emitted during generation and the layout shifts by 4
bytes, dummy_tramp_addr would be physically located at bpf_func_end - 4.

Since bpf_arch_text_poke() patches bpf_func_end - SZL (which evaluates to
8 bytes), could patch_ulong() overwrite the bctr instruction with the new
target address instead of dummy_tramp_addr, leading to an illegal instruction
panic when the trampoline executes?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260517214043.12975-1-adubey@linux.ibm.com?part=2

  reply	other threads:[~2026-05-17 18:23 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-17 21:40 [PATCH v4 0/5] powerpc/bpf: Add support for verifier selftest adubey
2026-05-17 21:40 ` [PATCH v4 1/5] powerpc/bpf: fix alignment of long branch trampoline address adubey
2026-05-17 18:02   ` sashiko-bot
2026-05-17 18:30   ` bot+bpf-ci
2026-05-18  7:18   ` Hari Bathini
2026-05-17 21:40 ` [PATCH v4 2/5] powerpc/bpf: Move out dummy_tramp_addr after Long branch stub adubey
2026-05-17 18:23   ` sashiko-bot [this message]
2026-05-17 18:30   ` bot+bpf-ci
2026-05-18  7:25   ` Hari Bathini
2026-05-18  7:53     ` Hari Bathini
2026-05-17 21:40 ` [PATCH v4 3/5] selftest/bpf: Fixing powerpc JIT disassembly failure adubey
2026-05-17 18:18   ` bot+bpf-ci
2026-05-17 18:38   ` sashiko-bot
2026-05-17 21:40 ` [PATCH v4 4/5] selftest/bpf: Enable verifier selftest for powerpc64 adubey
2026-05-17 18:18   ` bot+bpf-ci
2026-05-17 21:40 ` [PATCH v4 5/5] selftest/bpf: Add tailcall " adubey
2026-05-17 19:14   ` sashiko-bot
2026-05-18 11:44 ` [PATCH v4 0/5] powerpc/bpf: Add support for verifier selftest Christophe Leroy (CS GROUP)

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=20260517182339.AAA9BC2BCB0@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=adubey@linux.ibm.com \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.