public inbox for bpf@vger.kernel.org
 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 RFC bpf-next 5/8] bpf, x86: emit KASAN checks into x86 JITed programs
Date: Sat, 25 Apr 2026 06:08:27 +0000	[thread overview]
Message-ID: <20260425060828.1DAF8C2BCB0@smtp.kernel.org> (raw)
In-Reply-To: <20260413-kasan-v1-5-1a5831230821@bootlin.com>

Sashiko AI review found 3 potential issue(s):
- [High] Missing KASAN instrumentation for immediate-based memory stores (BPF_ST | BPF_MEM) and incorrect write detection.
- [High] Missing KASAN instrumentation for standard atomic memory operations (BPF_STX | BPF_ATOMIC).
- [High] Incorrect IP calculation in emit_kasan_check() leads to wrong jump offsets and kernel crashes when IBT is enabled.
--

commit bdea14177f5d099598de93e92ea9aecf3714ba64
Author: Alexis Lothoré (eBPF Foundation) <alexis.lothore@bootlin.com>

bpf, x86: emit KASAN checks into x86 JITed programs

This commit inserts KASAN shadow memory checks before load and store
operations in JIT-compiled BPF programs. It aims to detect memory safety bugs
while explicitly skipping stack accesses and faulting PROBE instructions.

> diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
> index b90103bd00809..111fe1d551219 100644
> --- a/arch/x86/net/bpf_jit_comp.c
> +++ b/arch/x86/net/bpf_jit_comp.c
[ ... ]
> @@ -2242,6 +2244,11 @@ st:			if (is_imm8(insn->off))
>  		case BPF_STX | BPF_MEM | BPF_H:
>  		case BPF_STX | BPF_MEM | BPF_W:
>  		case BPF_STX | BPF_MEM | BPF_DW:
> +			err = emit_kasan_check(&prog, dst_reg, insn,
> +					       image + addrs[i - 1],
> +					       accesses_stack);
> +			if (err)
> +				return err;
>  			emit_stx(&prog, BPF_SIZE(insn->code), dst_reg, src_reg, insn->off);
>  			break;

Is the correct instruction pointer being passed to emit_kasan_check() here?

Earlier in do_jit(), the local ip variable is updated to account for
conditionally emitted prefixes:

    if (bpf_insn_is_indirect_target(env, bpf_prog, i - 1))
        EMIT_ENDBR();

    ip = image + addrs[i - 1] + (prog - temp);

Passing image + addrs[i - 1] directly ignores the size of the ENDBR prefix.
Could this cause the relative jump offset in emit_call() to be off by 4
bytes, leading to a kernel crash when branching to the KASAN function?

Additionally, looking at the code surrounding this hunk:

        /* ST: *(u8*)(dst_reg + off) = imm */
    case BPF_ST | BPF_MEM | BPF_B:
        if (is_ereg(dst_reg))
            EMIT2(0x41, 0xC6);
        else
            EMIT1(0xC6);
        goto st;
[ ... ]
    case BPF_ST | BPF_MEM | BPF_DW:
        EMIT2(add_1mod(0x48, dst_reg), 0xC7);

st:     if (is_imm8(insn->off))
            EMIT2(add_1reg(0x40, dst_reg), insn->off);
        else
            EMIT1_off32(add_1reg(0x80, dst_reg), insn->off);

        EMIT(imm32, bpf_size_to_x86_bytes(BPF_SIZE(insn->code)));
        break;

Are immediate-based memory stores (BPF_ST | BPF_MEM) meant to be skipped?

Unlike BPF_STX, there is no emit_kasan_check() added here, which seems to
allow uninstrumented writes to kernel memory.

If instrumentation is added here, there might be a second issue. Looking at
the helper:

static int emit_kasan_check(..., struct bpf_insn *insn, ...)
{
    bool is_write = BPF_CLASS(insn->code) == BPF_STX;
    ...
}

Since BPF_ST has a different class, would this evaluate to false and
incorrectly emit a read check instead of a write check?

[ ... ]
> @@ -2390,6 +2397,12 @@ st:			if (is_imm8(insn->off))
>  				/* populate jmp_offset for JAE above to jump to start_of_ldx */
>  				start_of_ldx = prog;
>  				end_of_jmp[-1] = start_of_ldx - end_of_jmp;
> +			} else {
> +				err = emit_kasan_check(&prog, src_reg, insn,
> +						       image + addrs[i - 1],
> +						       accesses_stack);
> +				if (err)
> +					return err;
>  			}
>  			if (BPF_MODE(insn->code) == BPF_PROBE_MEMSX ||
>  			    BPF_MODE(insn->code) == BPF_MEMSX)

Further down in the function, atomic operations are handled:

    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;

Should standard atomic memory operations be instrumented as well?

While the commit message notes that BPF_PROBE_ATOMIC instructions are skipped
because they handle faults, standard BPF_STX | BPF_ATOMIC operations perform
memory modifications without fault-handling semantics.

Since emit_kasan_check() is omitted here, could an invalid base pointer used
in an atomic operation bypass KASAN and corrupt memory?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260413-kasan-v1-0-1a5831230821@bootlin.com?part=5

  reply	other threads:[~2026-04-25  6:08 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-13 18:28 [PATCH RFC bpf-next 0/8] bpf: add support for KASAN checks in JITed programs Alexis Lothoré (eBPF Foundation)
2026-04-13 18:28 ` [PATCH RFC bpf-next 1/8] kasan: expose generic kasan helpers Alexis Lothoré (eBPF Foundation)
2026-04-13 22:19   ` Andrey Konovalov
2026-04-14 13:12     ` Alexis Lothoré
2026-04-14 14:36       ` Alexei Starovoitov
2026-04-14 15:10         ` Andrey Konovalov
2026-04-14 15:58           ` Alexei Starovoitov
2026-04-19 21:48             ` Andrey Konovalov
2026-04-19 22:51               ` Alexei Starovoitov
2026-04-20 14:27                 ` Alexis Lothoré
2026-04-24 23:31                 ` Ihor Solodrai
2026-04-14 18:41         ` Alexis Lothoré
2026-04-14 19:16           ` Alexei Starovoitov
2026-04-14 20:44             ` Alexis Lothoré
2026-04-25  3:13   ` sashiko-bot
2026-04-13 18:28 ` [PATCH RFC bpf-next 2/8] bpf: mark instructions accessing program stack Alexis Lothoré (eBPF Foundation)
2026-04-24 23:18   ` Ihor Solodrai
2026-04-25  5:05   ` sashiko-bot
2026-04-13 18:28 ` [PATCH RFC bpf-next 3/8] bpf: add BPF_JIT_KASAN for KASAN instrumentation of JITed programs Alexis Lothoré (eBPF Foundation)
2026-04-13 22:20   ` Andrey Konovalov
2026-04-14 13:24     ` Alexis Lothoré
2026-04-14 14:38       ` Alexei Starovoitov
2026-04-25  5:18   ` sashiko-bot
2026-04-13 18:28 ` [PATCH RFC bpf-next 4/8] bpf, x86: add helper to emit kasan checks in x86 " Alexis Lothoré (eBPF Foundation)
2026-04-25  5:46   ` sashiko-bot
2026-04-13 18:28 ` [PATCH RFC bpf-next 5/8] bpf, x86: emit KASAN checks into " Alexis Lothoré (eBPF Foundation)
2026-04-25  6:08   ` sashiko-bot [this message]
2026-04-13 18:28 ` [PATCH RFC bpf-next 6/8] selftests/bpf: do not run verifier JIT tests when BPF_JIT_KASAN is enabled Alexis Lothoré (eBPF Foundation)
2026-04-25  6:21   ` sashiko-bot
2026-04-13 18:28 ` [PATCH RFC bpf-next 7/8] bpf, x86: enable KASAN for JITed programs on x86 Alexis Lothoré (eBPF Foundation)
2026-04-25  6:33   ` sashiko-bot
2026-04-13 18:28 ` [PATCH RFC bpf-next 8/8] selftests/bpf: add tests to validate KASAN on JIT programs Alexis Lothoré (eBPF Foundation)
2026-04-13 22:20   ` Andrey Konovalov
2026-04-14 13:43     ` Alexis Lothoré
2026-04-25  6:50   ` sashiko-bot
2026-04-24 23:10 ` [PATCH RFC bpf-next 0/8] bpf: add support for KASAN checks in JITed programs Ihor Solodrai
2026-04-24 23:28   ` Alexei Starovoitov
2026-04-27  8:54     ` Alexis Lothoré
2026-04-27  8:45   ` 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=20260425060828.1DAF8C2BCB0@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=alexis.lothore@bootlin.com \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko@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