BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Alexis Lothoré (eBPF Foundation)" <alexis.lothore@bootlin.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next v4 06/10] bpf, x86: emit KASAN checks into x86 JITed programs
Date: Wed, 08 Jul 2026 08:16:49 +0000	[thread overview]
Message-ID: <20260708081650.5E8BB1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260708-kasan-v4-6-d5c177ab8227@bootlin.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] `emit_kasan_check` fails to save and restore the caller-saved registers %r10 (`BPF_REG_AX`) and %r11 (`AUX_REG`). This corrupts state during atomic fetch operations and constant blinding, potentially leading to arbitrary memory corruption or kernel crashes.
- [Low] Atomic read-modify-write (RMW) operations are incorrectly instrumented as KASAN reads instead of KASAN writes.
--

--- Patch [6]: [PATCH bpf-next v4 06/10] bpf, x86: emit KASAN checks into x86 JITed programs ---
commit ea72c3645f886fbb17349184882dfa35cd20282f
Author: Alexis Lothoré (eBPF Foundation) <alexis.lothore@bootlin.com>

bpf, x86: emit KASAN checks into x86 JITed programs

Insert KASAN shadow memory checks before memory load and store operations
in JIT-compiled BPF programs. This helps detect memory safety bugs such as
use-after-free and out-of-bounds accesses at runtime.

> diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
> index 959fcaa0cde9a..65a00e84799cd 100644
> --- a/arch/x86/net/bpf_jit_comp.c
> +++ b/arch/x86/net/bpf_jit_comp.c
[ ... ]
> @@ -2605,15 +2640,16 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
>  			}
>  			fallthrough;
>  		case BPF_STX | BPF_ATOMIC | BPF_W:
> -		case BPF_STX | BPF_ATOMIC | BPF_DW:
> -			if (insn->imm == (BPF_AND | BPF_FETCH) ||
> -			    insn->imm == (BPF_OR | BPF_FETCH) ||
> -			    insn->imm == (BPF_XOR | BPF_FETCH)) {
> -				bool is64 = BPF_SIZE(insn->code) == BPF_DW;
> -				u32 real_src_reg = src_reg;
> -				u32 real_dst_reg = dst_reg;
> -				u8 *branch_target;
> -
> +		case BPF_STX | BPF_ATOMIC | BPF_DW: {
> +			bool is64 = BPF_SIZE(insn->code) == BPF_DW;
> +			u32 real_src_reg = src_reg;
> +			u32 real_dst_reg = dst_reg;
> +			u8 *branch_target;
> +			bool is_atomic_fetch =
> +				(insn->imm == (BPF_AND | BPF_FETCH) ||
> +				 insn->imm == (BPF_OR | BPF_FETCH) ||
> +				 insn->imm == (BPF_XOR | BPF_FETCH));
> +			if (is_atomic_fetch) {
>  				/*
>  				 * Can't be implemented with a single x86 insn.
>  				 * Need to do a CMPXCHG loop.
> @@ -2626,7 +2662,17 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
>  				if (dst_reg == BPF_REG_0)
>  					real_dst_reg = BPF_REG_AX;
>  
> +				ip += 3;
> +			}
> +			if (!bpf_atomic_is_load_store(insn)) {
> +				err = emit_kasan_check(env, &prog, real_dst_reg,
> +						       insn, ip, false,
> +						       accesses_stack_only);

[Severity: Critical]
Does this KASAN check clobber %r10 (BPF_REG_AX) and %r11 (AUX_REG)?

If this is an atomic fetch operation and BPF_REG_0 is used, the JIT saves
BPF_REG_0 into BPF_REG_AX (%r10). However, the emit_kasan_check() function
emits a C function call without saving %r10 or %r11:

arch/x86/net/bpf_jit_comp.c:emit_kasan_check() {
	...
	/* Save r8 */
	EMIT2(0x41, 0x50);
	/* Save r9 */
	EMIT2(0x41, 0x51);

	/* mov rdi, addr_reg */
	EMIT_mov(BPF_REG_1, addr_reg);
	...
	/* call kasan_func */
	if (emit_call(&prog, kasan_func, ip))
		return -ERANGE;
	...
}

Since C functions can clobber %r10 under the System V ABI, could the
subsequent use of real_dst_reg (which points to %r10) lead to wild memory
accesses?

[Severity: Low]
Are atomic RMW operations incorrectly instrumented as KASAN reads?

The 6th argument to emit_kasan_check() is is_write. Here it is hardcoded to
false for mutating atomic RMW instructions. Should this be true instead, so
that KASAN diagnostics report out-of-bounds writes rather than out-of-bounds
reads?

> +				if (err)
> +					return err;
>  				branch_target = prog;
> +			}
> +			if (is_atomic_fetch) {
>  				/* Load old value */
>  				emit_ldx(&prog, BPF_SIZE(insn->code),
>  					 BPF_REG_0, real_dst_reg, insn->off);

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260708-kasan-v4-0-d5c177ab8227@bootlin.com?part=6

  reply	other threads:[~2026-07-08  8:16 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-08  8:00 [PATCH bpf-next v4 00/10] bpf: add support for KASAN checks in JITed programs Alexis Lothoré (eBPF Foundation)
2026-07-08  8:00 ` [PATCH bpf-next v4 01/10] bpf: propagate original instruction offset when patching program Alexis Lothoré (eBPF Foundation)
2026-07-08  8:19   ` sashiko-bot
2026-07-08 14:11     ` Alexis Lothoré
2026-07-08  8:00 ` [PATCH bpf-next v4 02/10] bpf: mark instructions accessing program stack Alexis Lothoré (eBPF Foundation)
2026-07-08  8:10   ` sashiko-bot
2026-07-08 14:15     ` Alexis Lothoré
2026-07-08  8:00 ` [PATCH bpf-next v4 03/10] bpf: add BPF_JIT_KASAN for KASAN instrumentation of JITed programs Alexis Lothoré (eBPF Foundation)
2026-07-08 12:48   ` Andrey Konovalov
2026-07-08  8:00 ` [PATCH bpf-next v4 04/10] bpf, x86: add helper to emit kasan checks in x86 " Alexis Lothoré (eBPF Foundation)
2026-07-08  8:16   ` sashiko-bot
2026-07-08 14:20     ` Alexis Lothoré
2026-07-08  8:00 ` [PATCH bpf-next v4 05/10] bpf, x86: refactor BPF_ST management in do_jit Alexis Lothoré (eBPF Foundation)
2026-07-08  8:00 ` [PATCH bpf-next v4 06/10] bpf, x86: emit KASAN checks into x86 JITed programs Alexis Lothoré (eBPF Foundation)
2026-07-08  8:16   ` sashiko-bot [this message]
2026-07-08 14:29     ` Alexis Lothoré
2026-07-08  8:00 ` [PATCH bpf-next v4 07/10] bpf, x86: enable KASAN for JITed programs on x86 Alexis Lothoré (eBPF Foundation)
2026-07-08  8:19   ` sashiko-bot
2026-07-08 14:33     ` Alexis Lothoré
2026-07-08  8:00 ` [PATCH bpf-next v4 08/10] selftests/bpf: add helper to check whether eBPF KASAN is active Alexis Lothoré (eBPF Foundation)
2026-07-08  8:00 ` [PATCH bpf-next v4 09/10] selftests/bpf: move bpf_jit_harden helper into testing_helpers Alexis Lothoré (eBPF Foundation)
2026-07-08  8:16   ` sashiko-bot
2026-07-08 14:43     ` Alexis Lothoré
2026-07-08  8:00 ` [PATCH bpf-next v4 10/10] selftests/bpf: add tests to validate KASAN on JIT programs Alexis Lothoré (eBPF Foundation)
2026-07-08  8:28   ` sashiko-bot
2026-07-08 14:47     ` Alexis Lothoré

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=20260708081650.5E8BB1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=alexis.lothore@bootlin.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox