From: "Alexis Lothoré" <alexis.lothore@bootlin.com>
To: "Ihor Solodrai" <ihor.solodrai@linux.dev>,
"Alexis Lothoré (eBPF Foundation)" <alexis.lothore@bootlin.com>,
"Alexei Starovoitov" <ast@kernel.org>,
"Daniel Borkmann" <daniel@iogearbox.net>,
"John Fastabend" <john.fastabend@gmail.com>,
"Andrii Nakryiko" <andrii@kernel.org>,
"Martin KaFai Lau" <martin.lau@linux.dev>,
"Eduard Zingerman" <eddyz87@gmail.com>,
"Kumar Kartikeya Dwivedi" <memxor@gmail.com>,
"Song Liu" <song@kernel.org>,
"Yonghong Song" <yonghong.song@linux.dev>,
"Jiri Olsa" <jolsa@kernel.org>,
"Thomas Gleixner" <tglx@kernel.org>,
"Borislav Petkov" <bp@alien8.de>,
"Dave Hansen" <dave.hansen@linux.intel.com>,
x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
"Shuah Khan" <shuah@kernel.org>, "Ingo Molnar" <mingo@redhat.com>,
"Andrey Konovalov" <andreyknvl@gmail.com>
Cc: <ebpf@linuxfoundation.org>,
"Bastien Curutchet" <bastien.curutchet@bootlin.com>,
"Thomas Petazzoni" <thomas.petazzoni@bootlin.com>,
<bpf@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
<linux-kselftest@vger.kernel.org>
Subject: Re: [PATCH bpf-next v5 10/10] selftests/bpf: add tests to validate KASAN on JIT programs
Date: Thu, 16 Jul 2026 09:35:47 +0200 [thread overview]
Message-ID: <DJZTMNWA9JGC.1J4BR7LH0OT3V@bootlin.com> (raw)
In-Reply-To: <d326caf4-3d43-4880-8081-c21ef9748cdd@linux.dev>
On Thu Jul 16, 2026 at 12:35 AM CEST, Ihor Solodrai wrote:
> On 7/9/26 3:01 AM, Alexis Lothoré (eBPF Foundation) wrote:
>> Add a basic KASAN test runner that loads and test-run programs that can
>> trigger memory management bugs. The test captures kernel logs and ensure
>> that the expected KASAN splat is emitted by searching for the
>> corresponding first lines in the report, hence validated that the needed
>> instrumentation has been inserted by the JIT compiler before the
>> relevant memory accesses. To allow each test to trigger the expected
>> report, the kernel must run with the kasan_multi_shot configuration.
>>
>> The runner covers different cases and settings: in the nominal case, it
>> validates kasan reports on basic instructions (on all supported accesses
>> sizes) but also when report _should not_ be emitted (eg: for accesses on
>> program stack). The runner also comes with a few specialized tests that
>> are then not executed for all sizes/locations:
>> - specific atomic ops
>> - test for instructions involving different verifier states, with some
>> states flagging memory as stack, and other states as non-stack memory
>> - tests that validate the stack marking shifting when a patch is emitted
>> by the verifier (zext/rnd_hi32, constant blindind)
>>
>> A few of those tests depends on cpuv4 (load_acquire and store_release).
>>
>> # ./test_progs -a kasan
>> #165/1 kasan/st_1_not_on_stack:OK
>> #165/2 kasan/st_1_on_stack:OK
>> #165/3 kasan/st_2_not_on_stack:OK
>> #165/4 kasan/st_2_on_stack:OK
>> #165/5 kasan/st_4_not_on_stack:OK
>> #165/6 kasan/st_4_on_stack:OK
>> #165/7 kasan/st_8_not_on_stack:OK
>> #165/8 kasan/st_8_on_stack:OK
>> #165/9 kasan/stx_1_not_on_stack:OK
>> #165/10 kasan/stx_1_on_stack:OK
>> #165/11 kasan/stx_2_not_on_stack:OK
>> #165/12 kasan/stx_2_on_stack:OK
>> #165/13 kasan/stx_4_not_on_stack:OK
>> #165/14 kasan/stx_4_on_stack:OK
>> #165/15 kasan/stx_8_not_on_stack:OK
>> #165/16 kasan/stx_8_on_stack:OK
>> #165/17 kasan/ldx_1_not_on_stack:OK
>> #165/18 kasan/ldx_1_on_stack:OK
>> #165/19 kasan/ldx_2_not_on_stack:OK
>> #165/20 kasan/ldx_2_on_stack:OK
>> #165/21 kasan/ldx_4_not_on_stack:OK
>> #165/22 kasan/ldx_4_on_stack:OK
>> #165/23 kasan/ldx_8_not_on_stack:OK
>> #165/24 kasan/ldx_8_on_stack:OK
>> #165/25 kasan/simple_atomic_4_not_on_stack:OK
>> #165/26 kasan/simple_atomic_4_on_stack:OK
>> #165/27 kasan/simple_atomic_8_not_on_stack:OK
>> #165/28 kasan/simple_atomic_8_on_stack:OK
>> #165/29 kasan/load_acquire_1_not_on_stack:SKIP
>> #165/30 kasan/load_acquire_1_on_stack:SKIP
>> #165/31 kasan/load_acquire_2_not_on_stack:SKIP
>> #165/32 kasan/load_acquire_2_on_stack:SKIP
>> #165/33 kasan/load_acquire_4_not_on_stack:SKIP
>> #165/34 kasan/load_acquire_4_on_stack:SKIP
>> #165/35 kasan/load_acquire_8_not_on_stack:SKIP
>> #165/36 kasan/load_acquire_8_on_stack:SKIP
>> #165/37 kasan/store_release_1_not_on_stack:SKIP
>> #165/38 kasan/store_release_1_on_stack:SKIP
>> #165/39 kasan/store_release_2_not_on_stack:SKIP
>> #165/40 kasan/store_release_2_on_stack:SKIP
>> #165/41 kasan/store_release_4_not_on_stack:SKIP
>> #165/42 kasan/store_release_4_on_stack:SKIP
>> #165/43 kasan/store_release_8_not_on_stack:SKIP
>> #165/44 kasan/store_release_8_on_stack:SKIP
>> #165/45 kasan/ldx_patched:OK
>> #165/46 kasan/ldx_patched_on_stack:OK
>> #165/47 kasan/verifier_paths_stack_and_non_stack:OK
>> #165/48 kasan/st_blinded:OK
>> #165 kasan:OK (SKIP: 16/48)
>> Summary: 1/32 PASSED, 16 SKIPPED, 0 FAILED
>
> That's quite comprehensive, good work!
>
>>
>> Signed-off-by: Alexis Lothoré (eBPF Foundation) <alexis.lothore@bootlin.com>
>> ---
>> [...]
>> +
>> +#define MAX_LOG_SIZE (8 * 1024)
>> +#define READ_CHUNK_SIZE 256
>> +
>> +#define KASAN_PATTERN_SLAB_UAF "BUG: KASAN: slab-use-after-free " \
>> + "in bpf_prog_%02x%02x%02x%02x%02x%02x%02x%02x_%s"
>> +#define KASAN_PATTERN_REPORT "%s of size %d at addr"
>
> I don't know how feasible that is, but I think it would be good to
> have a test or two for other KASAN errors, like OOB for example.
For the record, I initially started with some real memory issues (with
kfuncks performing both OoB or UaF accesses), but I quickly switched
to the shadow memory tampering method (the poisoning kfunc surgically
modifying shadow memory) as soon as I started struggling to trigger some
cases (eg: pretty hard/close to impossible to trigger a kasan write
fault without the verifier catching it first), and so I got rid in the
process of any real access issue. But I guess I can reintroduce a few
real accesses to make sure that OoB from a prog indeed trigger a report,
even if it does not cover the whole range mentioned in the listing
above.
>> [...]
>> +
>> +SEC("tcx/ingress")
>> +int simple_atomic_on_stack(struct __sk_buff *skb)
>> +{
>> + struct kasan_write_val val;
>> +
>> + bpf_kfunc_kasan_poison(&val, sizeof(struct kasan_write_val));
>> + switch (access_size) {
>> + case 4:
>> + __sync_fetch_and_add(&val.data_4, 4);
>> + break;
>> + case 8:
>> + __sync_fetch_and_add(&val.data_8, 8);
>
> It looks like the BPF_FETCH wouldn't execute this way, because the
> result is discarded? And the whole if (is_atomic_fetch) branch in
> do_jit() is also not covered?
Hmmmm, for this prog, clang emits the following:
c3 1a f4 ff 01 00 00 00 w1 = atomic_fetch_add((u32 *)(r10 - 0xc), w1)
and despite the result being discarded, the operation is properly
instrumented and executed in my setup, I see an __asan_store4 check
being instrumented just before the jited "lock xadd $edi,0x4(%rbx)" for
this insn. As a test I tried storing and using the return value to check
the difference in the emitted code, but I get a build error:
progs/kasan.c:248:13: error: Invalid usage of the XADD return value
248 | old_val = __sync_fetch_and_add(&val->data_4, 4);
I'll dig into that. Anyway you're right about is_atomic_fetch, the
branch is not being tested here, I'll fix that.
>
> Maybe it's worth adding a test with __sync_val_compare_and_swap() into
> a poisoned map value, or something of the sort?
>
Thanks,
Alexis
--
Alexis Lothoré, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
next prev parent reply other threads:[~2026-07-16 7:35 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-09 10:01 [PATCH bpf-next v5 00/10] bpf: add support for KASAN checks in JITed programs Alexis Lothoré (eBPF Foundation)
2026-07-09 10:01 ` [PATCH bpf-next v5 01/10] bpf: propagate original instruction offset when patching program Alexis Lothoré (eBPF Foundation)
2026-07-09 10:25 ` sashiko-bot
2026-07-15 0:47 ` Ihor Solodrai
2026-07-16 6:17 ` Alexis Lothoré
2026-07-09 10:01 ` [PATCH bpf-next v5 02/10] bpf: mark instructions accessing program stack Alexis Lothoré (eBPF Foundation)
2026-07-09 10:35 ` sashiko-bot
2026-07-09 10:01 ` [PATCH bpf-next v5 03/10] bpf: add BPF_JIT_KASAN for KASAN instrumentation of JITed programs Alexis Lothoré (eBPF Foundation)
2026-07-15 0:49 ` Ihor Solodrai
2026-07-09 10:01 ` [PATCH bpf-next v5 04/10] bpf, x86: add helper to emit kasan checks in x86 " Alexis Lothoré (eBPF Foundation)
2026-07-09 10:54 ` sashiko-bot
2026-07-15 0:53 ` Ihor Solodrai
2026-07-16 7:41 ` Alexis Lothoré
2026-07-09 10:01 ` [PATCH bpf-next v5 05/10] bpf, x86: refactor BPF_ST management in do_jit Alexis Lothoré (eBPF Foundation)
2026-07-09 11:08 ` sashiko-bot
2026-07-15 22:27 ` Ihor Solodrai
2026-07-15 22:25 ` Ihor Solodrai
2026-07-09 10:01 ` [PATCH bpf-next v5 06/10] bpf, x86: emit KASAN checks into x86 JITed programs Alexis Lothoré (eBPF Foundation)
2026-07-09 11:20 ` sashiko-bot
2026-07-15 22:28 ` Ihor Solodrai
2026-07-16 6:31 ` Alexis Lothoré
2026-07-09 10:01 ` [PATCH bpf-next v5 07/10] bpf, x86: enable KASAN for JITed programs on x86 Alexis Lothoré (eBPF Foundation)
2026-07-09 11:29 ` sashiko-bot
2026-07-15 22:31 ` Ihor Solodrai
2026-07-09 10:01 ` [PATCH bpf-next v5 08/10] selftests/bpf: add helpers for KASAN in JIT testing Alexis Lothoré (eBPF Foundation)
2026-07-09 11:35 ` sashiko-bot
2026-07-15 22:32 ` Ihor Solodrai
2026-07-09 10:01 ` [PATCH bpf-next v5 09/10] selftests/bpf: move bpf_jit_harden helper into testing_helpers Alexis Lothoré (eBPF Foundation)
2026-07-15 22:33 ` Ihor Solodrai
2026-07-09 10:01 ` [PATCH bpf-next v5 10/10] selftests/bpf: add tests to validate KASAN on JIT programs Alexis Lothoré (eBPF Foundation)
2026-07-09 11:52 ` sashiko-bot
2026-07-15 22:35 ` Ihor Solodrai
2026-07-16 7:35 ` Alexis Lothoré [this message]
2026-07-09 18:53 ` [PATCH bpf-next v5 00/10] bpf: add support for KASAN checks in JITed programs Borislav Petkov
2026-07-09 19:09 ` Alexis Lothoré
2026-07-15 0:43 ` Ihor Solodrai
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=DJZTMNWA9JGC.1J4BR7LH0OT3V@bootlin.com \
--to=alexis.lothore@bootlin.com \
--cc=andreyknvl@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bastien.curutchet@bootlin.com \
--cc=bp@alien8.de \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=dave.hansen@linux.intel.com \
--cc=ebpf@linuxfoundation.org \
--cc=eddyz87@gmail.com \
--cc=hpa@zytor.com \
--cc=ihor.solodrai@linux.dev \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=mingo@redhat.com \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=tglx@kernel.org \
--cc=thomas.petazzoni@bootlin.com \
--cc=x86@kernel.org \
--cc=yonghong.song@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.