BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Pu Lehui" <pulehui@huaweicloud.com>, bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next 1/4] riscv, bpf: Fix forward jump offset calculation during JIT passes
Date: Tue, 01 Dec 2026 13:51:07 +0000	[thread overview]
Message-ID: <sashiko-outbox-243426@kernel.org> (raw)
In-Reply-To: <20261201133404.2227100-2-pulehui@huaweicloud.com>

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] riscv, bpf: UHMUL and SHMUL miscompilation in
  bpf_jit_emit_insn() [1]
--

commit de0e129fdf81e58df6e6ff584831841fe64b9d55
Author: Pu Lehui <pulehui@huawei.com>

riscv, bpf: Fix forward jump offset calculation during JIT passes

This commit preserves the previous pass's instruction offsets in
ctx->offset[] during instruction emission and extends ctx->offset[] to
prog->len + 1 entries so forward branch and epilogue jump calculations do
not mix offsets from different JIT passes.

> diff --git a/arch/riscv/net/bpf_jit_comp32.c b/arch/riscv/net/bpf_jit_comp32.c
> index a9e0bd5cc81d2..a32fb59b74e93 100644
> --- a/arch/riscv/net/bpf_jit_comp32.c
> +++ b/arch/riscv/net/bpf_jit_comp32.c

[ ... ]

> @@ -1305,7 +1304,7 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
>  		if (i == ctx->prog->len - 1)
>  			break;
>  
> -		rvoff = epilogue_offset(ctx);
> +		rvoff = rv_offset(i, ctx->prog->len - 1 - i, ctx);
>  		emit_jump_and_link(RV_REG_ZERO, rvoff, false, ctx);
>  		break;

[Severity: Critical]
This problem wasn't introduced by this patch, but in bpf_jit_emit_insn(),
does the BPF_ALU64 | BPF_MUL handling miscompile [1] 64-bit UHMUL
and SHMUL instructions by ignoring insn->off?

arch/riscv/net/bpf_jit_comp32.c:bpf_jit_emit_insn() {
	...
	case BPF_ALU64 | BPF_MUL | BPF_X:
	case BPF_ALU64 | BPF_MUL | BPF_K:

	case BPF_ALU64 | BPF_LSH | BPF_X:
	case BPF_ALU64 | BPF_RSH | BPF_X:
	case BPF_ALU64 | BPF_ARSH | BPF_X:
		if (BPF_SRC(code) == BPF_K) {
			emit_imm32(tmp2, imm, ctx);
			src = tmp2;
		}
		emit_alu_r64(dst, src, ctx, BPF_OP(code));
		break;
	...
}

BPF cpuv4 encodes UHMUL and SHMUL as BPF_ALU64 | BPF_MUL with a non-zero
insn->off, but bpf_jit_emit_insn() passes only BPF_OP(code) to
emit_alu_r64(), which always emits the low 64 bits of the product.  The
JITed value then diverges from the verifier's register bounds, allowing
out-of-bounds memory accesses.

[1] https://sashiko.dev/#/bug/linux-1ce6a684-cefb-42c5-b3ec-d9542a7923f7

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201133404.2227100-1-pulehui@huaweicloud.com?part=1

  reply	other threads:[~2026-12-01 13:51 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 13:34 [PATCH bpf-next 0/4] riscv, bpf: Miscellaneous JIT optimizations Pu Lehui
2026-12-01 13:34 ` [PATCH bpf-next 1/4] riscv, bpf: Fix forward jump offset calculation during JIT passes Pu Lehui
2026-12-01 13:51   ` sashiko-bot [this message]
2026-12-01 13:34 ` [PATCH bpf-next 2/4] riscv, bpf: Reuse tailcall epilogue across tailcall sites Pu Lehui
2026-12-01 13:34 ` [PATCH bpf-next 3/4] riscv, bpf: Eliminate stop_machine in bpf_arch_text_poke with per-image PLT Pu Lehui
2026-12-01 14:19   ` bot+bpf-ci
2026-12-01 13:34 ` [PATCH bpf-next 4/4] riscv, bpf: Remove arch_bpf_trampoline_skip Pu Lehui

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=sashiko-outbox-243426@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=pulehui@huaweicloud.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox