BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Luis Gerhorst" <luis.gerhorst@fau.de>, bpf@vger.kernel.org
Subject: Re: [PATCH bpf 1/4] bpf: Add bpf_insert_insns() to insert insns next to an insn
Date: Wed, 25 Nov 2026 17:51:16 +0000	[thread overview]
Message-ID: <sashiko-outbox-233864@kernel.org> (raw)
In-Reply-To: <45c622fb6bba40629594679c01eed836d58acd89.1795625597.git.luis.gerhorst@fau.de>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] bpf: adjust_insn_aux_data() overwrites zext_dst of original
  insn
--

commit 4852fbba0f01b447927ce6cabc47568627f19025
Author: Luis Gerhorst <luis.gerhorst@fau.de>

bpf: Add bpf_insert_insns() to insert insns next to an insn

This commit adds bpf_insert_insns() to insert instructions before or after
an existing instruction while preserving all of the original instruction's
insn_aux_data fields, and converts insertion sites in fixups.c to use it.

> diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c
> index a656634fa9e46..485feffe05363 100644
> --- a/kernel/bpf/fixups.c
> +++ b/kernel/bpf/fixups.c
> @@ -170,82 +170,60 @@ enum bpf_patch_mode {
>  	BPF_PATCH_MOVE_TARGET,	/* targets of 'off' follow the original insn to 'off + len - 1' */
>  };
>  
> +/* The patch does not contain the insn that it replaces. */
> +#define BPF_PATCH_NO_ORIG	-1
> +
>  /* single env->prog->insni[off] instruction was replaced with the range
>   * insni[off, off + cnt).  Adjust corresponding insn_aux_data by copying
>   * [0, off) and [off, end) to new locations, so the patched range stays zero
> + *
> + * The aux data of the original insn stays with the insn if the range still
> + * contains it, at index 'orig'. Otherwise 'orig' is BPF_PATCH_NO_ORIG and the
> + * aux data moves to the last insn of the range.
>   */
>  static void adjust_insn_aux_data(struct bpf_verifier_env *env,
>  				 struct bpf_prog *new_prog, u32 off, u32 cnt,
> -				 struct bpf_insn *original_insn,
> -				 enum bpf_patch_mode mode)
> +				 int orig, enum bpf_patch_mode mode)
>  {
>  	struct bpf_insn_aux_data *data = env->insn_aux_data;
>  	struct bpf_insn *insn = new_prog->insnsi;
> +	u32 last = off + cnt - 1;
> +	u32 keep = orig == BPF_PATCH_NO_ORIG ? last : off + orig;
> +	u32 target = mode == BPF_PATCH_MOVE_TARGET ? keep : off;
> +	bool indirect_target = data[off].indirect_target;
>  	u32 old_seen = data[off].seen;
> -	u32 prog_len;
> +	u32 prog_len = new_prog->len;
>  	int i;
>  
> -	/* aux info at OFF always needs adjustment, no matter fast path
> -	 * (cnt == 1) is taken or not. There is no guarantee INSN at OFF is the
> -	 * original insn at old prog.
> -	 */
> -	data[off].zext_dst = bpf_insn_def32(new_prog, insn + off + cnt - 1) >= 0;
> -
> -	if (cnt == 1) {
> -		/*
> -		 * A non-memory accessing insn could have been replaced by a
> -		 * memory accessing insn, systematically mark it for non-stack
> -		 * access
> -		 */
> -		data[off].non_stack_access = bpf_is_mem_insn(insn + off);
> -		return;
> +	if (cnt > 1) {
> +		env->insn_aux_data_len = prog_len;
> +		memmove(data + last, data + off,
> +			sizeof(struct bpf_insn_aux_data) * (prog_len - last));
> +		/* The aux data of the original insn is at 'off' and 'last' now. */
> +		data[keep] = data[last];
> +		for (i = off; i <= last; i++)
> +			if (i != keep)
> +				memset(data + i, 0, sizeof(struct bpf_insn_aux_data));
>  	}
> -	prog_len = new_prog->len;
> -	env->insn_aux_data_len = prog_len;
>  
> -	memmove(data + off + cnt - 1, data + off,
> -		sizeof(struct bpf_insn_aux_data) * (prog_len - off - cnt + 1));
> -	memset(data + off, 0, sizeof(struct bpf_insn_aux_data) * (cnt - 1));
> -	for (i = off; i < off + cnt - 1; i++) {
> +	for (i = off; i <= last; i++) {
>  		/* Expand insni[off]'s seen count to the patched range. */
>  		data[i].seen = old_seen;
>  		data[i].zext_dst = bpf_insn_def32(new_prog, insn + i) >= 0;
>  		/*
> -		 * In BPF_PATCH_MOVE_TARGET mode the original insn is the last
> -		 * slot by construction.
> +		 * A new insn can access memory. Systematically mark it for
> +		 * non-stack access, only the original insn keeps its marking.
>  		 */
> -		if (mode == BPF_PATCH_KEEP_TARGET &&
> -		    !memcmp(insn + i, original_insn, sizeof(struct bpf_insn))) {
> -			data[i].non_stack_access =
> -				data[off + cnt - 1].non_stack_access;
> -			data[off + cnt - 1].non_stack_access = false;
> -		} else if (bpf_is_mem_insn(insn + i)) {
> -			data[i].non_stack_access = true;
> -		}
> +		if (orig == BPF_PATCH_NO_ORIG || i != keep)
> +			data[i].non_stack_access = bpf_is_mem_insn(insn + i);
>  	}

[Severity: High]
Does setting data[i].zext_dst unconditionally across [off, last] clobber
data[keep].zext_dst for the original instruction when called from
bpf_insert_insns()?

For BPF_ALU64 arithmetic on PTR_TO_ARENA, adjust_ptr_min_max_vals() sets
aux->needs_zext = true and aux->zext_dst = true while leaving insn->code as
BPF_ALU64 until bpf_do_misc_fixups() rewrites it to BPF_ALU.  If
bpf_insert_insns() runs on that instruction first (for example, inserting
BPF_ST_NOSPEC() in bpf_convert_ctx_accesses() when aux->nospec is set),
bpf_insn_def32(new_prog, insn + keep) sees BPF_ALU64 and overwrites
data[keep].zext_dst to false while preserving data[keep].needs_zext = true.

After bpf_do_misc_fixups() converts the instruction to 32-bit BPF_ALU,
bpf_opt_subreg_zext_lo32_rnd_hi32() sees zext_dst == false: it skips
BPF_ZEXT_REG on bpf_jit_needs_zext() architectures, and under
BPF_F_TEST_RND_HI32 it emits rnd_hi32_patch to OR random bits into the
upper 32 bits of the PTR_TO_ARENA register, causing subsequent
BPF_PROBE_MEM32 dereferences to access outside the 4GB arena region.

Conversely, for a 32-bit instruction where bpf_compute_live_registers()
cleared zext_dst because the upper 32 bits are dead, bpf_insert_insns()
resets data[keep].zext_dst back to true.

Should data[i].zext_dst also be updated only inside the
"if (orig == BPF_PATCH_NO_ORIG || i != keep)" block?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795625597.git.luis.gerhorst@fau.de?part=1

  reply	other threads:[~2026-11-25 17:51 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  4:08 [PATCH bpf 0/5] bpf: Fixes for speculation barriers and constant reads Alexei Starovoitov
2026-11-25  4:08 ` [PATCH bpf 1/5] bpf: Undo insn aux changes of a failed speculative path Alexei Starovoitov
2026-11-25  4:08 ` [PATCH bpf 2/5] bpf: Treat NOT_INIT as a pointer in __is_pointer_value() Alexei Starovoitov
2026-11-25  4:08 ` [PATCH bpf 3/5] bpf: Use get_unaligned() in bpf_map_direct_read() Alexei Starovoitov
2026-11-25  4:08 ` [PATCH bpf 4/5] bpf: Emit nospec after BPF_ATOMIC stores to the stack Alexei Starovoitov
2026-11-25 17:12   ` Luis Gerhorst
2026-11-25 17:12     ` [PATCH bpf 0/4] bpf: Emit nospec after every insn that " Luis Gerhorst
2026-11-25 17:12       ` [PATCH bpf 1/4] bpf: Add bpf_insert_insns() to insert insns next to an insn Luis Gerhorst
2026-11-25 17:51         ` sashiko-bot [this message]
2026-11-25 18:17         ` bot+bpf-ci
2026-11-25 17:12       ` [PATCH bpf 2/4] selftests/bpf: Test the insns that bpf_insert_insns() inserts Luis Gerhorst
2026-11-25 17:39         ` sashiko-bot
2026-11-25 17:12       ` [PATCH bpf 3/4] bpf: Emit nospec after every insn with nospec_result Luis Gerhorst
2026-11-25 18:17         ` bot+bpf-ci
2026-11-25 17:12       ` [PATCH bpf 4/4] selftests/bpf: Test nospec after stack writes of calls and atomics Luis Gerhorst
2026-11-25 17:45         ` sashiko-bot
2026-11-26  3:40     ` [PATCH bpf 4/5] bpf: Emit nospec after BPF_ATOMIC stores to the stack Alexei Starovoitov
2026-11-25  4:08 ` [PATCH bpf 5/5] bpf: Do not end a speculative path at a call Alexei Starovoitov
2026-11-25  5:00   ` bot+bpf-ci

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-233864@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=luis.gerhorst@fau.de \
    --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