Skip to contentSkip to conversation

bpf: add support for KASAN checks in JITed programs

PATCH Superseded Alexis Lothoré (eBPF Foundation) 4 participants 38 messages Jul 9 – Jul 23

Subsystems (7)
x86 Kselftest BPF Arm STM32 +3 more subsystems

v5 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v5 cover bpf: add support for KASAN checks in JITed programs

Hello,
this is v5 of the series aiming to bring basic support for KASAN checks to BPF JITed programs.

Original cover letter:

"Traditional" KASAN allows to spot memory management mistakes by reserving a fraction of memory as "shadow memory" that will map to the rest of the memory and allow its monitoring. Each memory-accessing instruction is then instrumented at build time to call some ASAN check function, that will analyze the corresponding bits in shadow memory, and if it detects the access as invalid, trigger a detailed report. The goal of this series is to replicate this mechanism for BPF programs when they are being JITed into native instructions: that's then the JIT compiler that is in charge of inserting calls to the corresponding kasan checks, when a program is being loaded into the kernel. This task involves:
- identifying at program load time the instructions performing memory accesses
- identifying those accesses properties (size ? read or write ?) to define the relevant kasan check function to call
- just before the identified instructions:
- perform the basic context saving (ie: saving registers)
- inserting a call to the relevant kasan check function
- restore context
- whenever the instrumented program executes, if it performs an invalid access, it triggers a kasan report identical to those instrumented on kernel side at build time.

The series comes with new selftests programs that generate a wide variety of kasan reports: those need the kernel to be running with kasan_multi_shot enabled.

As discussed in [1], this series is based on some choices and assumptions:
- it focuses on x86_64 for now, and so only on KASAN_GENERIC
- not all memory accessing BPF instructions are being instrumented:
- it discards instructions accessing BPF program stack (already monitored by page guards)
- it discards possibly faulting instructions, like BPF_PROBE_MEM or BPF_PROBE_ATOMIC insns

---
Changes in v5:
- fixed a few instruction offset for generated fixups
- fix insn marking for single insn patches
- enforce more checks in tests
- skip tests if kasan_multi_shot isn't enabled
- Link to v4: https://patch.msgid.link/[email protected]

Changes in v4:
- fix insn_offs_in_patch leakage in bpf_convert_ctx_access
- handle BPF_ATOMIC in is_mem_insn
- correctly mark fixup instructions if a single insn is generated
- clarify new kconfig (Andrey) and drop VMAP_STACK dep
- refactor BPF_FETCH atomic handling in JIT loop
- make kernel log reading resilient to unrelated, interleaved logs in the selftests
- make new test kfuncs depend on BPF_JIT_KASAN rather than KASAN_GENERIC
- Link to v3: https://patch.msgid.link/[email protected]

Changes in v3:
- Do not insert KASAN instrumentation when dealing with cBPF
- Fix stack-accessing insn tracking for verifier patches, as original instruction location in the generated patch may vary
- drop cBPF support for stack-accessing insn marking
- make sure to flag correctly memory access if different verifier states involve different memory types (eg: stack in one path, non-stack in another path)
- refactor BPF_ST handling in x86 JIT compiler
- improve tests coverage (cover instrumentation for a few patches emitted by the verifier)
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- declare asan functions as extern in JIT compiler rather than exposing them in kasan header
- invert stack-accessing instructions marking to make sure not to skip instructions that could end up accessing to-be-checked memory
- fix stack accesses marking when verifier patches instructions
- add best effort marking for cBPF
- add missing call depth accounting in jited instrumentation
- skip unused registers in kasan instrumentation save/restore
- remove faulty stack align in kasan instrumentation
- drop commit skipping some jit-related tests
- cover missing instructions: BPF_ST and atomics
- completely rework tests: directly tune shadow memory, increase coverage, do not consume kernel logs
- Link to v1: https://patch.msgid.link/[email protected]

To: Alexei Starovoitov <[email protected]>
To: Daniel Borkmann <[email protected]>
To: John Fastabend <[email protected]>
To: Andrii Nakryiko <[email protected]>
To: Martin KaFai Lau <[email protected]>
To: Eduard Zingerman <[email protected]>
To: Kumar Kartikeya Dwivedi <[email protected]>
To: Song Liu <[email protected]>
To: Yonghong Song <[email protected]>
To: Jiri Olsa <[email protected]>
To: Thomas Gleixner <[email protected]>
To: Borislav Petkov <[email protected]>
To: Dave Hansen <[email protected]>
To: [email protected]
To: "H. Peter Anvin" <[email protected]>
To: Shuah Khan <[email protected]>
To: Ingo Molnar <[email protected]>
To: Andrey Konovalov <[email protected]>
Cc: [email protected]
Cc: Bastien Curutchet <[email protected]>
Cc: Thomas Petazzoni <[email protected]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
Alexis Lothoré (eBPF Foundation) (10):
bpf: propagate original instruction offset when patching program
bpf: mark instructions accessing program stack
bpf: add BPF_JIT_KASAN for KASAN instrumentation of JITed programs bpf, x86: add helper to emit kasan checks in x86 JITed programs bpf, x86: refactor BPF_ST management in do_jit
bpf, x86: emit KASAN checks into x86 JITed programs bpf, x86: enable KASAN for JITed programs on x86 selftests/bpf: add helpers for KASAN in JIT testing selftests/bpf: move bpf_jit_harden helper into testing_helpers selftests/bpf: add tests to validate KASAN on JIT programs

 arch/x86/Kconfig                                   |   1 +
 arch/x86/net/bpf_jit_comp.c                        | 271 ++++++++++---
 include/linux/bpf_verifier.h                       |   2 +
 include/linux/filter.h                             |  10 +-
 kernel/bpf/Kconfig                                 |  17 +
 kernel/bpf/core.c                                  |   2 +-
 kernel/bpf/fixups.c                                | 132 +++++--
 kernel/bpf/verifier.c                              |   9 +
 .../selftests/bpf/prog_tests/bpf_insn_array.c      |  44 +--
 tools/testing/selftests/bpf/prog_tests/kasan.c     | 430 +++++++++++++++++++++
 tools/testing/selftests/bpf/progs/kasan.c          | 394 +++++++++++++++++++
 tools/testing/selftests/bpf/progs/kasan_harden.c   |  41 ++
 .../testing/selftests/bpf/test_kmods/bpf_testmod.c |  22 ++
 tools/testing/selftests/bpf/testing_helpers.c      |  32 ++
 tools/testing/selftests/bpf/testing_helpers.h      |   1 +
 tools/testing/selftests/bpf/unpriv_helpers.c       |  10 +
 tools/testing/selftests/bpf/unpriv_helpers.h       |   2 +
 17 files changed, 1288 insertions(+), 132 deletions(-)

---
base-commit: 950d646b505ae37ee75f4c2b11d41e7190036c42
change-id: 20260126-kasan-fcd68f64cd7b

Best regards,

Signature · 1 line
Alexis Lothoré (eBPF Foundation) <[email protected]>
#1 lore raw

PATCH v5 1/10 bpf: propagate original instruction offset when patching program

When the verifier patches an ebpf program with bpf_patch_insn_data, it then calls adjust_insn_aux_data to make sure that insn_aux_data takes into account the newly inserted patch. Some of the data offset is pretty straightforward to deduce, it is for example the case for indirect_target, as any patch affecting indirect calls will systematically move the original instruction to the end of the new patch.

In order to introduce KASAN support for eBPF JIT, we need to mark any load/store instruction that accesses non-stack memory, but updating this new marking after a patch is not as straightforward as for indirect
calls: the original BPF_ST/BPF_STX/BPF_LDX can be at the beginning, at the end or somewhere in the middle of the new patch: we then need some additional info to properly update this marking.

Add a new parameter to bpf_patch_insn_data and adjust_insn_aux_data to convey the info about the new location in the patch of the original instruction. This info does not always make sense depending on the generated patch (eg, some bpf helpers being inlined by the verifier, and so turned into multiple new instructions without any remaining BPF_CALL), the parameter can then be set to -1. It will be used in next patches to properly handle insn_aux_data adjustment for the new KASAN instrumentation

---
Changes in v5:
- fix a few incorrect offset values

Changes in v4:
- define insn_off_in_patch inside convert_ctx_accesses main loop to avoid old value leakage

Changes in v3:
- new patch

---
 include/linux/filter.h |  10 +++--
 kernel/bpf/core.c      |   2 +-
 kernel/bpf/fixups.c    | 100 ++++++++++++++++++++++++++++++++-----------------
 3 files changed, 74 insertions(+), 38 deletions(-)
include/linux/filter.h +7 -3
index 14acb2455746..1ebcd247ef4e 100644
@@ -1210,13 +1210,17 @@ struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
 
 #ifdef CONFIG_BPF_SYSCALL
 struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
-				     const struct bpf_insn *patch, u32 len);
+				     const struct bpf_insn *patch, u32 len,
+				     s32 insn_off_in_patch);
 struct bpf_insn_aux_data *bpf_dup_insn_aux_data(struct bpf_verifier_env *env);
 void bpf_restore_insn_aux_data(struct bpf_verifier_env *env,
 			       struct bpf_insn_aux_data *orig_insn_aux);
 #else
-static inline struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
-						   const struct bpf_insn *patch, u32 len)
+static inline struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env,
+						   u32 off,
+						   const struct bpf_insn *patch,
+						   u32 len,
+						   s32 insn_off_in_patch)
 {
 	return ERR_PTR(-ENOTSUPP);
 }
kernel/bpf/core.c +1 -1
index 47fe047ad30b..cff0035f401a 100644
@@ -1599,7 +1599,7 @@ struct bpf_prog *bpf_jit_blind_constants(struct bpf_verifier_env *env, struct bp
 			continue;
 
 		if (env)
-			tmp = bpf_patch_insn_data(env, i, insn_buff, rewritten);
+			tmp = bpf_patch_insn_data(env, i, insn_buff, rewritten, rewritten - 1);
 		else
 			tmp = bpf_patch_insn_single(clone, i, insn_buff, rewritten);
 
kernel/bpf/fixups.c +66 -34

338 lines not on this page · raw message

Signature · 1 line
2.54.0
#2 lore raw

Re: [PATCH bpf-next v5 01/10] bpf: propagate original instruction offset when patching program

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Incorrect `insn_off_in_patch` offset passed during instruction rewrites for pointer arithmetic…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Incorrect insn_off_in_patch offset passed during instruction rewrites for pointer arithmetic, may_goto, and LD_ABS/LD_IND. --

--- Patch [1]: [PATCH bpf-next v5 01/10] bpf: propagate original instruction offset when patching program --- commit 93b475f9be8b7e4db55e416665410ec667566b5a
Author: Alexis Lothoré (eBPF Foundation) <[email protected]>

bpf: propagate original instruction offset when patching program

This commit updates the BPF verifier to propagate the original instruction's offset within a patch when calling bpf_patch_insn_data and adjust_insn_aux_data. This information is needed to correctly map auxiliary instruction data (like KASAN markings) after instructions are rewritten.

Quoted 4 lines
kernel/bpf/fixups.c +0 -0
index d3be972714b22..188ea6205d617 100644

[ ... ]

kernel/bpf/fixups.c int bpf_do_misc_fixups(struct bpf_verifier_env *env) · line 1709

Quoted 7 lines
kernel/bpf/fixups.c +2 -1
@@ -1689,7 +1705,8 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
 				return -EFAULT;
 			}
 
-			new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
+			new_prog = bpf_patch_insn_data(env, i + delta, insn_buf,
+						       cnt, 0);

[Severity: Medium]
For LD_ABS and LD_IND rewrites, the original instruction is completely replaced by the generated instructions. Should insn_off_in_patch be set to -1 here instead of 0, to avoid incorrectly identifying the first setup instruction as the original one?

[ ... ]

kernel/bpf/fixups.c int bpf_do_misc_fixups(struct bpf_verifier_env *env) · line 1763

Quoted 7 lines
kernel/bpf/fixups.c +2 -1
@@ -1742,7 +1759,8 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
 				*patch++ = BPF_ALU64_IMM(BPF_MUL, off_reg, -1);
 			cnt = patch - insn_buf;
 
-			new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
+			new_prog = bpf_patch_insn_data(env, i + delta, insn_buf,
+						       cnt, cnt - 1);

[Severity: Medium]
In this pointer arithmetic mitigation, if a trailing MUL instruction is appended (when issrc && isneg && !isimm are true), the original instruction ends up at cnt - 2 instead of cnt - 1. Does passing cnt - 1 here risk misidentifying the appended MUL instruction as the original instruction?

[ ... ]

kernel/bpf/fixups.c int bpf_do_misc_fixups(struct bpf_verifier_env *env) · line 1809

Quoted 7 lines
kernel/bpf/fixups.c +2 -1
@@ -1787,7 +1805,8 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
 			insn_buf[6] = BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_AX, stack_off_cnt);
 			cnt = 7;
 
-			new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
+			new_prog = bpf_patch_insn_data(env, i + delta, insn_buf,
+						       cnt, 3);

[Severity: Medium]
For timed may_goto, the arbitrary offset 3 points to an intermediate JNE instruction rather than the original instruction, since the original is fully replaced. Should this pass -1 instead of 3?

[ ... ]

kernel/bpf/fixups.c int bpf_do_misc_fixups(struct bpf_verifier_env *env) · line 1831

Quoted 7 lines
kernel/bpf/fixups.c +2 -1
@@ -1808,7 +1827,8 @@ int bpf_do_misc_fixups(struct bpf_verifier_env *env)
 			insn_buf[3] = BPF_STX_MEM(BPF_DW, BPF_REG_10, BPF_REG_AX, stack_off);
 			cnt = 4;
 
-			new_prog = bpf_patch_insn_data(env, i + delta, insn_buf, cnt);
+			new_prog = bpf_patch_insn_data(env, i + delta, insn_buf,
+						       cnt, 2);

[Severity: Medium]
Similarly, for non-timed may_goto, the offset 2 points to a SUB instruction. Since the original may_goto is entirely replaced, would it be more accurate to pass -1 here?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
reviewer
#3 lore raw

Re: [PATCH bpf-next v5 01/10] bpf: propagate original instruction offset when patching program

In reply to Alexis Lothoré (eBPF Foundation)
Quoted 7 lines · Alexis Lothoré (eBPF Foundation)

When the verifier patches an ebpf program with bpf_patch_insn_data, it then calls adjust_insn_aux_data to make sure that insn_aux_data takes into account the newly inserted patch. Some of the data offset is pretty straightforward to deduce, it is for example the case for indirect_target, as any patch affecting indirect calls will systematically move the original instruction to the end of the new patch.

I think an additional KASAN-specific argument to adjust_insn_aux_data() is not a good idea. It's threaded through ~30 call sites, and it's been error-prone too: you had to fix the offsets a few times already.

indirect_target needs no help from the callers, so it's not really the same pattern. Similar for the other aux fields: seen is broadcast and zext_dst is re-derived.

Quoted 7 lines

In order to introduce KASAN support for eBPF JIT, we need to mark any load/store instruction that accesses non-stack memory, but updating this new marking after a patch is not as straightforward as for indirect
calls: the original BPF_ST/BPF_STX/BPF_LDX can be at the beginning, at the end or somewhere in the middle of the new patch: we then need some additional info to properly update this marking.

I don't think we need to track the exact offset here.

It looks like .non_stack_access is set to is_mem_insn(insn + off) in every single case *except* for when a stack access happens to be in the middle of a patch.

Given that the cost of getting the flag "wrong" is an unnecessary kasan check only for that case, I think it'll be cleaner to just unconditionally do:

    data[off].non_stack_access = is_mem_insn(insn + off);

in adjust_insn_aux_data()

And then the code change can be folded in patch #2

The only problem with this I can think of is that in reality *most* stack accesses go trough that special case, which would defeat the purpose of the .non_stack_access flag. This can be checked empirically however.

Anything else I'm missing here?

Quoted 55 lines

Add a new parameter to bpf_patch_insn_data and adjust_insn_aux_data to convey the info about the new location in the patch of the original instruction. This info does not always make sense depending on the generated patch (eg, some bpf helpers being inlined by the verifier, and so turned into multiple new instructions without any remaining BPF_CALL), the parameter can then be set to -1. It will be used in next patches to properly handle insn_aux_data adjustment for the new KASAN instrumentation

---
Changes in v5:
- fix a few incorrect offset values

Changes in v4:
- define insn_off_in_patch inside convert_ctx_accesses main loop to avoid old value leakage

Changes in v3:
- new patch

---
 include/linux/filter.h |  10 +++--
 kernel/bpf/core.c      |   2 +-
 kernel/bpf/fixups.c    | 100 ++++++++++++++++++++++++++++++++-----------------
 3 files changed, 74 insertions(+), 38 deletions(-)
include/linux/filter.h +7 -3
index 14acb2455746..1ebcd247ef4e 100644
@@ -1210,13 +1210,17 @@ struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
 
 #ifdef CONFIG_BPF_SYSCALL
 struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
-				     const struct bpf_insn *patch, u32 len);
+				     const struct bpf_insn *patch, u32 len,
+				     s32 insn_off_in_patch);
 struct bpf_insn_aux_data *bpf_dup_insn_aux_data(struct bpf_verifier_env *env);
 void bpf_restore_insn_aux_data(struct bpf_verifier_env *env,
 			       struct bpf_insn_aux_data *orig_insn_aux);
 #else
-static inline struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env, u32 off,
-						   const struct bpf_insn *patch, u32 len)
+static inline struct bpf_prog *bpf_patch_insn_data(struct bpf_verifier_env *env,
+						   u32 off,
+						   const struct bpf_insn *patch,
+						   u32 len,
+						   s32 insn_off_in_patch)
 {
 	return ERR_PTR(-ENOTSUPP);
 }
[...]
#4 lore raw

Re: [PATCH bpf-next v5 01/10] bpf: propagate original instruction offset when patching program

Hi Ihor,
thanks for the thorough review !

Quoted 9 lines · Ihor Solodrai
Quoted 7 lines · Alexis Lothoré (eBPF Foundation)

When the verifier patches an ebpf program with bpf_patch_insn_data, it then calls adjust_insn_aux_data to make sure that insn_aux_data takes into account the newly inserted patch. Some of the data offset is pretty straightforward to deduce, it is for example the case for indirect_target, as any patch affecting indirect calls will systematically move the original instruction to the end of the new patch.

Quoted 3 lines

I think an additional KASAN-specific argument to adjust_insn_aux_data() is not a good idea. It's threaded through ~30 call sites, and it's been error-prone too: you had to fix the offsets a few times already.

Agree. I've indeed already made a few back and forth on it, it looks like there are still more to do, and not in the good direction (I mean, just passing -1 to ignore original insn offset), so that sounds more and more like a hassle for no significant gain. This ends up being pretty intrusive for a debug feature, and as you state below, the only downside of not tracking too finely those stack accesses is about getting a few unecessary KASAN checks.

Quoted 26 lines

indirect_target needs no help from the callers, so it's not really the same pattern. Similar for the other aux fields: seen is broadcast and zext_dst is re-derived.

Quoted 7 lines

In order to introduce KASAN support for eBPF JIT, we need to mark any load/store instruction that accesses non-stack memory, but updating this new marking after a patch is not as straightforward as for indirect
calls: the original BPF_ST/BPF_STX/BPF_LDX can be at the beginning, at the end or somewhere in the middle of the new patch: we then need some additional info to properly update this marking.

I don't think we need to track the exact offset here.

It looks like .non_stack_access is set to is_mem_insn(insn + off) in every single case *except* for when a stack access happens to be in the middle of a patch.

Given that the cost of getting the flag "wrong" is an unnecessary kasan check only for that case, I think it'll be cleaner to just unconditionally do:

    data[off].non_stack_access = is_mem_insn(insn + off);

in adjust_insn_aux_data()

Quoted 8 lines

And then the code change can be folded in patch #2

The only problem with this I can think of is that in reality *most* stack accesses go trough that special case, which would defeat the purpose of the .non_stack_access flag. This can be checked empirically however.

Anything else I'm missing here?

Sounds good to me. I'll try to get a rough idea about how often the flag, in this scenario, correctly triggers or not.

Thanks,

Alexis

Signature · 3 lines
Alexis Lothoré, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#5 lore raw

Re: [PATCH bpf-next v5 01/10] bpf: propagate original instruction offset when patching program

On Thu Jul 16, 2026 at 8:17 AM CEST, Alexis Lothoré wrote:

[...]

Quoted 28 lines
Quoted 27 lines
Quoted 6 lines

In order to introduce KASAN support for eBPF JIT, we need to mark any load/store instruction that accesses non-stack memory, but updating this new marking after a patch is not as straightforward as for indirect
calls: the original BPF_ST/BPF_STX/BPF_LDX can be at the beginning, at the end or somewhere in the middle of the new patch: we then need some additional info to properly update this marking.

I don't think we need to track the exact offset here.

It looks like .non_stack_access is set to is_mem_insn(insn + off) in every single case *except* for when a stack access happens to be in the middle of a patch.

Given that the cost of getting the flag "wrong" is an unnecessary kasan check only for that case, I think it'll be cleaner to just unconditionally do:

    data[off].non_stack_access = is_mem_insn(insn + off);

in adjust_insn_aux_data()

And then the code change can be folded in patch #2

The only problem with this I can think of is that in reality *most* stack accesses go trough that special case, which would defeat the purpose of the .non_stack_access flag. This can be checked empirically however.

Quoted 4 lines
Quoted 1 line

Anything else I'm missing here?

Sounds good to me. I'll try to get a rough idea about how often the flag, in this scenario, correctly triggers or not.

So this new logic indeed let some "unwanted" cases being instrumented, like the ldx_patched_on_stack (testing the rnd_hi32 case, so involving bpf_opt_subreg_zext_lo32_rnd_hi32):

0000000000000710 <ldx_patched_on_stack>:

     226:       bf a6 00 00 00 00 00 00 r6 = r10
     227:       07 06 00 00 f0 ff ff ff r6 += -0x10
     228:       bf 61 00 00 00 00 00 00 r1 = r6
     229:       b4 02 00 00 10 00 00 00 w2 = 0x10
     230:       85 10 00 00 ff ff ff ff call -0x1
     231:       61 a1 f4 ff 00 00 00 00 w1 = *(u32 *)(r10 - 0xc)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                Is instrumented, shouldn't
     232:       63 1a f4 ff 00 00 00 00 *(u32 *)(r10 - 0xc) = w1
     233:       bf 61 00 00 00 00 00 00 r1 = r6
     234:       b4 02 00 00 10 00 00 00 w2 = 0x10
     235:       85 10 00 00 ff ff ff ff call -0x1
     236:       b4 00 00 00 00 00 00 00 w0 = 0x0
     237:       95 00 00 00 00 00 00 00 exit

While taking a further look at it, I thought about an intermediate solution that would prevent the unwanted instrumentation, without spreading the insn_off_in_patch value I initially proposed. What if:
- in bpf_patch_insn_data, we copy the original instruction being patched (the one at "off") in a temp var
- we pass this saved insn to adjust_insn_aux_data
- in the loop going over the patched zone, if the current insn matches the saved insn, we apply back the original non_stack_access flag, otherwise we systematically mark the insn as "non stack accessing" if this is a mem insn

Assuming that memcp'ing a struct bpf_insn is enough/correct to check whether we are processing the original insn or a new one. Some initial tests in my env correctly reduce the amount of instrumentation for those stack accesses, without having to edit all bpf_patch_insn_data sites.

Alexis

Signature · 3 lines
Alexis Lothoré, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#6 lore raw

PATCH v5 2/10 bpf: mark instructions accessing program stack

In reply to Alexis Lothoré (eBPF Foundation)

In order to prepare to emit KASAN checks in JITed programs, JIT compilers need to be aware about whether some load/store instructions are targeting the bpf program stack, as those should not be monitored (we already have guard pages for that, and it is difficult anyway to correctly monitor any kind of data passed on stack).

To support this need, make the BPF verifier mark the instructions depending on whether they could access or not memory other than stack. As different states in the verifier could lead to different memory types for the same access, just marking an instruction as accessing stack only is not enough (it could be some other memory type in another verifier state), so the algorithm rather sets by default any load/store instruction as stack only, and if _any_ state leads to any memory access type other than PTR_TO_STACK, it overrides this setting. It also takes care about shifting back the instruction marking in adjust_insn_aux_data if the verifier patches instructions. However, if the verifier generates new BPF_ST/BPF_STX/BPF_LDX while patching some instructions, those new ones are systematically marked as non-stack-accessing: this may over-instrument a few memory accessing instructions, but it allows making sure that we will not miss accidentally any.

---
Changes in v5:
- fix incorrect marking for single instruction patch

Changes in v4:
- include BPF_ATOMIC in is_mem_insn
- correctly mark instructions in adjust_insn_aux_data if patch generates a single instruction (ie replace an instruction)

Changes in v3:
- drop getter
- drop cBPF handling
- update marking shifting logic to track more precisely orignal instructions
- systematically mark newly generated instructions as non-stack accessing

Changes in v2:
- invert marking logic to cover possible different reg types when the verifier covers different states
- add a best-effort processing for classical bpf programs, inspecting directly src and dst registers since we don't have verifier env
- make sure to keep marking in sync with prog when it is patched by verifier

---
 include/linux/bpf_verifier.h |  2 ++
 kernel/bpf/fixups.c          | 32 +++++++++++++++++++++++++++++++-
 kernel/bpf/verifier.c        |  9 +++++++++
 3 files changed, 42 insertions(+), 1 deletion(-)
include/linux/bpf_verifier.h +2 -0
index 317e99b9acc0..abe8459cc701 100644
@@ -723,6 +723,8 @@ struct bpf_insn_aux_data {
 	u16 const_reg_map_mask;
 	u16 const_reg_subprog_mask;
 	u32 const_reg_vals[10];
+	/* instruction can access non-stack memory */
+	bool non_stack_access;
 };
 
 #define MAX_USED_MAPS 64 /* max number of maps accessed by one eBPF program */
kernel/bpf/fixups.c +31 -1
index 188ea6205d61..0eda5512baec 100644
@@ -152,6 +152,18 @@ static int get_callee_stack_depth(struct bpf_verifier_env *env,
 }
 #endif
 
+static bool is_mem_insn(struct bpf_insn *insn)
+{
+	if (BPF_CLASS(insn->code) != BPF_ST &&
+	    BPF_CLASS(insn->code) != BPF_STX &&
+	    BPF_CLASS(insn->code) != BPF_LDX)
+		return false;
+
+	return (BPF_MODE(insn->code) == BPF_MEM ||
+		BPF_MODE(insn->code) == BPF_MEMSX ||
+		BPF_MODE(insn->code) == BPF_ATOMIC);
+}
+
 /* 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
@@ -172,8 +184,14 @@ static void adjust_insn_aux_data(struct bpf_verifier_env *env,
 	 */
 	data[off].zext_dst = insn_has_def32(insn + off + cnt - 1);
 
-	if (cnt == 1)
+	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 = is_mem_insn(insn + off);
 		return;
+	}
 	prog_len = new_prog->len;
 
 	memmove(data + off + cnt - 1, data + off,
@@ -183,7 +201,19 @@ static void adjust_insn_aux_data(struct bpf_verifier_env *env,
 		/* Expand insni[off]'s seen count to the patched range. */
 		data[i].seen = old_seen;
 		data[i].zext_dst = insn_has_def32(insn + i);
+		if (i == off + insn_off_in_patch) {
+			data[i].non_stack_access = data[off + cnt - 1].non_stack_access;
+			data[off + cnt - 1].non_stack_access = false;
+		} else if (is_mem_insn(insn + i)) {
+			data[i].non_stack_access = true;
+		}
 	}
+	/*
+	 * Last slot instruction could be a newly generated
+	 * BPF_ST/BPF_LDX/BPF_STX
+	 */
+	if (is_mem_insn(insn + off + cnt - 1) && insn_off_in_patch != cnt - 1)
+		data[off + cnt - 1].non_stack_access = true;
 
 	/*
 	 * The indirect_target flag of the original instruction was moved to the last of the
kernel/bpf/verifier.c +9 -0
index 233472a871be..bc3b4771396a 100644
@@ -3207,6 +3207,11 @@ static void mark_indirect_target(struct bpf_verifier_env *env, int idx)
 	env->insn_aux_data[idx].indirect_target = true;
 }
 
+static void mark_non_stack_access(struct bpf_verifier_env *env, int idx)
+{
+	env->insn_aux_data[idx].non_stack_access = true;
+}
+
 #define LR_FRAMENO_BITS	4
 #define LR_SPI_BITS	6
 #define LR_ENTRY_BITS	(LR_SPI_BITS + LR_FRAMENO_BITS + 1)
@@ -6400,6 +6405,10 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, struct b
 		else
 			coerce_reg_to_size_sx(&regs[value_regno], size);
 	}
+
+	if (!err && reg->type != PTR_TO_STACK)
+		mark_non_stack_access(env, insn_idx);
+
 	return err;
 }
 
Signature · 1 line
2.54.0
#7 lore raw

Re: [PATCH bpf-next v5 02/10] bpf: mark instructions accessing program stack

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Multi-line comment does not follow the modern kernel/BPF comment style. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Multi-line comment does not follow the modern kernel/BPF comment style. --

--- Patch [2]: [PATCH bpf-next v5 02/10] bpf: mark instructions accessing program stack --- commit 03ab01d124457ab321033266bf176b6f34230aec
Author: Alexis Lothoré (eBPF Foundation) <[email protected]>

bpf: mark instructions accessing program stack

JIT compilers need to know if load/store instructions target the BPF stack to avoid over-instrumenting them with KASAN checks. This commit makes the verifier mark instructions accordingly, setting non-stack-access true if any state leads to an access other than PTR_TO_STACK. It also ensures instruction patching maintains correct markings.

Quoted 4 lines
kernel/bpf/fixups.c +0 -0
index 188ea6205d617..0eda5512baece 100644

[ ... ]

kernel/bpf/fixups.c static void adjust_insn_aux_data(struct bpf_verifier_env *env, · line 191

Quoted 10 lines
kernel/bpf/fixups.c +5 -1
@@ -172,8 +184,14 @@ static void adjust_insn_aux_data(struct bpf_verifier_env *env,
 	 */
 	data[off].zext_dst = insn_has_def32(insn + off + cnt - 1);
 
-	if (cnt == 1)
+	if (cnt == 1) {
+		/* A non-memory accessing insn could have been replaced by a
+		 * memory accessing insn, systematically mark it for non-stack
+		 * access
+		 */

[Severity: Low]
This isn't a bug, but does this multi-line comment in adjust_insn_aux_data() follow the preferred BPF subsystem comment style? The BPF guidelines require the opening /* to be on its own line for multi-line comments.

Quoted 4 lines
kernel/bpf/fixups.c +2 -0
+		data[off].non_stack_access = is_mem_insn(insn + off);
 		return;
+	}
 	prog_len = new_prog->len;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#8 lore raw

PATCH v5 3/10 bpf: add BPF_JIT_KASAN for KASAN instrumentation of JITed programs

In reply to Alexis Lothoré (eBPF Foundation)

Add a new Kconfig option CONFIG_BPF_JIT_KASAN that automatically enables generic KASAN (Kernel Address SANitizer) memory access checks for JIT-compiled BPF programs as well, when both KASAN (and more specifically, generic KASAN with KASAN_VMALLOC) and JIT compiler are enabled. This new Kconfig is not a user selectable one: it is either automatically enabled if KASAN is enabled on a compatible platform. When enabled, the JIT compiler will emit shadow memory checks before memory loads and stores to detect use-after-free or out-of-bounds accesses at runtime. The option is gated behind HAVE_EBPF_JIT_KASAN, as it needs proper arch-specific implementation.

---
Changes in v4:
- reorganize dependencies (Andrey)
- drop VMAP_STACK dependency

Changes in v2:
- add dependency on kasan for vmalloc and vmalloc'ed stack

---
 kernel/bpf/Kconfig | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
kernel/bpf/Kconfig +17 -0
index eb3de35734f0..e1f3850d2f5a 100644
@@ -17,6 +17,10 @@ config HAVE_CBPF_JIT
 config HAVE_EBPF_JIT
 	bool
 
+# KASAN support for JIT compiler
+config HAVE_EBPF_JIT_KASAN
+	bool
+
 # Used by archs to tell that they want the BPF JIT compiler enabled by
 # default for kernels that were compiled with BPF JIT support.
 config ARCH_WANT_DEFAULT_BPF_JIT
@@ -101,4 +105,17 @@ config BPF_LSM
 
 	  If you are unsure how to answer this question, answer N.
 
+config BPF_JIT_KASAN
+	bool
+	depends on HAVE_EBPF_JIT_KASAN
+	depends on KASAN_GENERIC
+	depends on KASAN_VMALLOC
+	depends on BPF_JIT
+	default y if KASAN
+	help
+	  Makes JIT compiler insert generic outline KASAN checks in BPF
+	  programs when they are inserted in the kernel. This feature is
+	  automatically enabled if the needed set of KASAN and BPF
+	  configuration options is enabled.
+
 endmenu # "BPF subsystem"
Signature · 1 line
2.54.0
reviewer Acked-by on v5 3/10
#9 lore raw

Re: [PATCH bpf-next v5 03/10] bpf: add BPF_JIT_KASAN for KASAN instrumentation of JITed programs

Quoted 5 lines · Alexis Lothoré (eBPF Foundation)

Add a new Kconfig option CONFIG_BPF_JIT_KASAN that automatically enables generic KASAN (Kernel Address SANitizer) memory access checks for JIT-compiled BPF programs as well, when both KASAN (and more specifically, generic KASAN with KASAN_VMALLOC) and JIT compiler are enabled.

An auto-selected flag makes sense to me.

Two nits below.

Quoted 2 lines

... This new Kconfig is not a user selectable one: it is either automatically enabled if KASAN is enabled on a compatible platform. When

nit: dangling "either"

Quoted 33 lines

enabled, the JIT compiler will emit shadow memory checks before memory loads and stores to detect use-after-free or out-of-bounds accesses at runtime. The option is gated behind HAVE_EBPF_JIT_KASAN, as it needs proper arch-specific implementation.

---
Changes in v4:
- reorganize dependencies (Andrey)
- drop VMAP_STACK dependency

Changes in v2:
- add dependency on kasan for vmalloc and vmalloc'ed stack

---
 kernel/bpf/Kconfig | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
kernel/bpf/Kconfig +4 -0
index eb3de35734f0..e1f3850d2f5a 100644
@@ -17,6 +17,10 @@ config HAVE_CBPF_JIT
 config HAVE_EBPF_JIT
 	bool
 
+# KASAN support for JIT compiler
+config HAVE_EBPF_JIT_KASAN
+	bool
+
 # Used by archs to tell that they want the BPF JIT compiler enabled by
 # default for kernels that were compiled with BPF JIT support.
 config ARCH_WANT_DEFAULT_BPF_JIT

kernel/bpf/Kconfig config BPF_LSM · line 114

Quoted 10 lines
kernel/bpf/Kconfig +7 -0
@@ -101,4 +105,17 @@ config BPF_LSM
 
 	  If you are unsure how to answer this question, answer N.
 
+config BPF_JIT_KASAN
+	bool
+	depends on HAVE_EBPF_JIT_KASAN
+	depends on KASAN_GENERIC
+	depends on KASAN_VMALLOC
+	depends on BPF_JIT
+	default y if KASAN

nit: KASAN_GENERIC already implies KASAN, so "if KASAN" is always true when this symbol is visible, should be safe to drop.

Quoted 8 lines
kernel/bpf/Kconfig +6 -0
+	help
+	  Makes JIT compiler insert generic outline KASAN checks in BPF
+	  programs when they are inserted in the kernel. This feature is
+	  automatically enabled if the needed set of KASAN and BPF
+	  configuration options is enabled.
+
 endmenu # "BPF subsystem"
#10 lore raw

PATCH v5 4/10 bpf, x86: add helper to emit kasan checks in x86 JITed programs

In reply to Alexis Lothoré (eBPF Foundation)

Add the emit_kasan_check() function that emits KASAN shadow memory checks before memory accesses in JIT-compiled BPF programs. The implementation relies on the existing __asan_{load,store}X functions from KASAN subsystem. The helper:
- ensures that the kasan instrumention is actually needed: if the instruction being processed accesses the program stack, we skip the instrumentation, as those accesses are already protected with page guards
- saves registers. This includes caller-saved registers, but also temporary registers, as those were possibly used by the affected program. Theoretically, r10 and r11 should be saved as well, but the number of called function and their scope being limited, they are skipped for the sake of reducing the overhead
- computes the accessed address and stores it in %rdi
- calls the relevant function, depending on the instruction being a load or a store, and the size of the access.
- restores registers

The special care needed when inserting this instrumentation comes at the cost of a non negligeable increase in JITed code size. For example, a bare

mov 0x0(%si),rbx # Load in rbx content at address stored in rsi

becomes

  push    %rax
  push    %rcx
  push    %rdx
  push    %rsi
  push    %rdi
  push    %r8
  push    %r9
  mov     %rsi,%rdi
  call    0xffffffff81da0a60 <__asan_load8>
  pop     %r9
  pop     %r8
  pop     %rdi
  pop     %rsi
  pop     %rdx
  pop     %rcx
  pop     %rax
  mov     0x0(%rsi),rbx

---
Changes in v3:
- skip kasan instrumentation if there is no verifier env (cBPF)
- move helper up in the file

Changes in v2:
- move asan functions declaration directly into jit compiler, and guard them with IS_ENABLED
- remove faulty stack alignment, no arg is passed to kasan funcs on the stack anyway
- make sure to emit call depth accounting code
- do not save unneeded registers
- update helper signature to let caller configure some values (eg:
is_write)

---
 arch/x86/net/bpf_jit_comp.c | 95 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
arch/x86/net/bpf_jit_comp.c +95 -0
index de7515ea1bea..d35f58350d71 100644
@@ -21,6 +21,17 @@
 #include <asm/unwind.h>
 #include <asm/cfi.h>
 
+#if IS_ENABLED(CONFIG_BPF_JIT_KASAN)
+void __asan_load1(void *p);
+void __asan_store1(void *p);
+void __asan_load2(void *p);
+void __asan_store2(void *p);
+void __asan_load4(void *p);
+void __asan_store4(void *p);
+void __asan_load8(void *p);
+void __asan_store8(void *p);
+#endif
+
 static bool all_callee_regs_used[4] = {true, true, true, true};
 
 static u8 *emit_code(u8 *ptr, u32 bytes, unsigned int len)
@@ -1110,6 +1121,90 @@ static void maybe_emit_1mod(u8 **pprog, u32 reg, bool is64)
 	*pprog = prog;
 }
 
+static int emit_kasan_check(struct bpf_verifier_env *env, u8 **pprog,
+			    u32 addr_reg, struct bpf_insn *insn, u8 *ip,
+			    bool is_write, bool accesses_stack_only)
+{
+#ifdef CONFIG_BPF_JIT_KASAN
+	u32 bpf_size = BPF_SIZE(insn->code);
+	s32 off = insn->off;
+	u8 *prog = *pprog;
+	void *kasan_func;
+
+	if (!env)
+		return 0;
+
+	if (accesses_stack_only)
+		return 0;
+
+	/* Derive KASAN check function from access type and size */
+	switch (bpf_size) {
+	case BPF_B:
+		kasan_func = is_write ? __asan_store1 : __asan_load1;
+		break;
+	case BPF_H:
+		kasan_func = is_write ? __asan_store2 : __asan_load2;
+		break;
+	case BPF_W:
+		kasan_func = is_write ? __asan_store4 : __asan_load4;
+		break;
+	case BPF_DW:
+		kasan_func = is_write ? __asan_store8 : __asan_load8;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	/* Save rax */
+	EMIT1(0x50);
+	/* Save rcx */
+	EMIT1(0x51);
+	/* Save rdx */
+	EMIT1(0x52);
+	/* Save rsi */
+	EMIT1(0x56);
+	/* Save rdi */
+	EMIT1(0x57);
+	/* Save r8 */
+	EMIT2(0x41, 0x50);
+	/* Save r9 */
+	EMIT2(0x41, 0x51);
+
+	/* mov rdi, addr_reg */
+	EMIT_mov(BPF_REG_1, addr_reg);
+
+	/* add rdi, off (if offset is non-zero) */
+	if (off) {
+		if (is_imm8(off)) {
+			/* add rdi, imm8 */
+			EMIT4(0x48, 0x83, 0xC7, (u8)off);
+		} else {
+			/* add rdi, imm32 */
+			EMIT3_off32(0x48, 0x81, 0xC7, off);
+		}
+	}
+
+	/* Adjust ip to account for the instrumentation generated so far */
+	ip += (prog - *pprog);
+	/* We emit a call, so update call depth counting */
+	ip += x86_call_depth_emit_accounting(&prog, kasan_func, ip);
+	/* call kasan_func */
+	if (emit_call(&prog, kasan_func, ip))
+		return -ERANGE;
+
+	EMIT2(0x41, 0x59);
+	EMIT2(0x41, 0x58);
+	EMIT1(0x5F);
+	EMIT1(0x5E);
+	EMIT1(0x5A);
+	EMIT1(0x59);
+	EMIT1(0x58);
+
+	*pprog = prog;
+#endif /* CONFIG_BPF_JIT_KASAN */
+	return 0;
+}
+
 /* LDX: dst_reg = *(u8*)(src_reg + off) */
 static void emit_ldx(u8 **pprog, u32 size, u32 dst_reg, u32 src_reg, int off)
 {
Signature · 1 line
2.54.0
#11 lore raw

Re: [PATCH bpf-next v5 04/10] bpf, x86: add helper to emit kasan checks in x86 JITed programs

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] `emit_kasan_check()` fails to save and restore caller-saved registers `%r10` and `%r11`, causing…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] emit_kasan_check() fails to save and restore caller-saved registers %r10 and %r11, causing ABI violation and memory corruption.
- [Low] emit_kasan_check() is static but unreferenced, breaking the build, and KASAN configuration is incomplete.
--

--- Patch [4]: [PATCH bpf-next v5 04/10] bpf, x86: add helper to emit kasan checks in x86 JITed programs --- commit 95547145be173ba9a87f9eff2492384b8ac25f18
Author: Alexis Lothoré (eBPF Foundation) <[email protected]>

bpf, x86: add helper to emit kasan checks in x86 JITed programs

This commit adds emit_kasan_check(), which emits KASAN shadow memory checks before memory accesses in JIT-compiled BPF programs. The helper calls standard KASAN C functions to validate the memory accesses.

Quoted 4 lines
arch/x86/net/bpf_jit_comp.c +0 -0
index de7515ea1beae..d35f58350d710 100644

[ ... ]

arch/x86/net/bpf_jit_comp.c static void maybe_emit_1mod(u8 **pprog, u32 reg, bool is64) · line 1127

Quoted 8 lines
arch/x86/net/bpf_jit_comp.c +4 -0
@@ -1110,6 +1121,90 @@ static void maybe_emit_1mod(u8 **pprog, u32 reg, bool is64)
 	*pprog = prog;
 }
 
+static int emit_kasan_check(struct bpf_verifier_env *env, u8 **pprog,
+			    u32 addr_reg, struct bpf_insn *insn, u8 *ip,
+			    bool is_write, bool accesses_stack_only)
+{

[Severity: Low]
Is this static function used anywhere in this patch? It looks like it is declared and defined but never referenced, which will cause a build failure when compiled with -Werror=unused-function.

Additionally, the HAVE_EBPF_JIT_KASAN Kconfig option does not appear to be selected by any architecture in this series, making the feature unreachable. (Also noting a potential issue where adjust_insn_aux_data() in kernel/bpf/fixups.c may retain stale state when insn_off_in_patch == -1).

Quoted 3 lines
arch/x86/net/bpf_jit_comp.c +3 -0
+#ifdef CONFIG_BPF_JIT_KASAN
+	u32 bpf_size = BPF_SIZE(insn->code);
+	s32 off = insn->off;

[ ... ]

Quoted 4 lines
arch/x86/net/bpf_jit_comp.c +4 -0
+	/* Save rax */
+	EMIT1(0x50);
+	/* Save rcx */
+	EMIT1(0x51);

arch/x86/net/bpf_jit_comp.c static void maybe_emit_1mod(u8 **pprog, u32 reg, bool is64) · line 1171

Quoted 10 lines
arch/x86/net/bpf_jit_comp.c +10 -0
+	/* Save rdx */
+	EMIT1(0x52);
+	/* Save rsi */
+	EMIT1(0x56);
+	/* Save rdi */
+	EMIT1(0x57);
+	/* Save r8 */
+	EMIT2(0x41, 0x50);
+	/* Save r9 */
+	EMIT2(0x41, 0x51);

[Severity: High]
Does skipping the save and restore of %r10 and %r11 violate the System V ABI?

While the commit message notes they are skipped to reduce overhead, %r10 is mapped to BPF_REG_AX in the x86 JIT. Because standard C functions like the KASAN helpers clobber caller-saved registers, wouldn't this corrupt the BPF_REG_AX register (which is used for operations like blinded constant stores) when the KASAN check is executed?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
reviewer
#12 lore raw

Re: [PATCH bpf-next v5 04/10] bpf, x86: add helper to emit kasan checks in x86 JITed programs

In reply to Alexis Lothoré (eBPF Foundation)
Quoted 5 lines · Alexis Lothoré (eBPF Foundation)

Add the emit_kasan_check() function that emits KASAN shadow memory checks before memory accesses in JIT-compiled BPF programs. The implementation relies on the existing __asan_{load,store}X functions from KASAN subsystem. The helper:
- ensures that the kasan instrumention is actually needed: if the

Quoted 8 lines

instruction being processed accesses the program stack, we skip the instrumentation, as those accesses are already protected with page guards
- saves registers. This includes caller-saved registers, but also temporary registers, as those were possibly used by the affected program. Theoretically, r10 and r11 should be saved as well, but the number of called function and their scope being limited, they are skipped for the sake of reducing the overhead

My clanker got very excited about r10 and r11 not being saved, just like sashiko. So I looked at this a bit closer. TL;DR it's fine

I built the kernel with gcc 11.5, gcc 15.2 and clang 22 and disassembled __asan_{load,store}{1,2,4,8}: none of them touch r10 or r11. gcc goes up to r8, clang uses nothing above rdi.

However, kasan_check_range() does use r10/r11. And looks like we are ok only because the fixed-size helpers inline the shadow check and never call kasan_check_range().

If any of the fixed-size 1/2/4/8 helpers ever actually call kasan_check_range(), r10 may get clobbered.

Maybe we should leave a comment on emit_kasan_check() about this?..

Quoted 73 lines

- computes the accessed address and stores it in %rdi
- calls the relevant function, depending on the instruction being a load or a store, and the size of the access.
- restores registers

The special care needed when inserting this instrumentation comes at the cost of a non negligeable increase in JITed code size. For example, a bare

mov 0x0(%si),rbx # Load in rbx content at address stored in rsi

becomes

  push    %rax
  push    %rcx
  push    %rdx
  push    %rsi
  push    %rdi
  push    %r8
  push    %r9
  mov     %rsi,%rdi
  call    0xffffffff81da0a60 <__asan_load8>
  pop     %r9
  pop     %r8
  pop     %rdi
  pop     %rsi
  pop     %rdx
  pop     %rcx
  pop     %rax
  mov     0x0(%rsi),rbx

---
Changes in v3:
- skip kasan instrumentation if there is no verifier env (cBPF)
- move helper up in the file

Changes in v2:
- move asan functions declaration directly into jit compiler, and guard them with IS_ENABLED
- remove faulty stack alignment, no arg is passed to kasan funcs on the stack anyway
- make sure to emit call depth accounting code
- do not save unneeded registers
- update helper signature to let caller configure some values (eg:
is_write)

---
 arch/x86/net/bpf_jit_comp.c | 95 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)
arch/x86/net/bpf_jit_comp.c +11 -0
index de7515ea1bea..d35f58350d71 100644
@@ -21,6 +21,17 @@
 #include <asm/unwind.h>
 #include <asm/cfi.h>
 
+#if IS_ENABLED(CONFIG_BPF_JIT_KASAN)
+void __asan_load1(void *p);
+void __asan_store1(void *p);
+void __asan_load2(void *p);
+void __asan_store2(void *p);
+void __asan_load4(void *p);
+void __asan_store4(void *p);
+void __asan_load8(void *p);
+void __asan_store8(void *p);
+#endif
+
 static bool all_callee_regs_used[4] = {true, true, true, true};
 
 static u8 *emit_code(u8 *ptr, u32 bytes, unsigned int len)

arch/x86/net/bpf_jit_comp.c static void maybe_emit_1mod(u8 **pprog, u32 reg, bool is64) · line 1126

Quoted 6 lines
arch/x86/net/bpf_jit_comp.c +3 -0
@@ -1110,6 +1121,90 @@ static void maybe_emit_1mod(u8 **pprog, u32 reg, bool is64)
 	*pprog = prog;
 }
 
+static int emit_kasan_check(struct bpf_verifier_env *env, u8 **pprog,
+			    u32 addr_reg, struct bpf_insn *insn, u8 *ip,
+			    bool is_write, bool accesses_stack_only)

nit: IMO it's not nice to introduce functions with no caller as a separate patch: it triggers -Wunused-function on this commit, which may or may not be used in a build. I'd fold this in patch #6 where the function is actually called.

Quoted 1 line

[...]

#13 lore raw

Re: [PATCH bpf-next v5 04/10] bpf, x86: add helper to emit kasan checks in x86 JITed programs

Quoted 15 lines · Ihor Solodrai
Quoted 5 lines · Alexis Lothoré (eBPF Foundation)

Add the emit_kasan_check() function that emits KASAN shadow memory checks before memory accesses in JIT-compiled BPF programs. The implementation relies on the existing __asan_{load,store}X functions from KASAN subsystem. The helper:
- ensures that the kasan instrumention is actually needed: if the

Quoted 8 lines

instruction being processed accesses the program stack, we skip the instrumentation, as those accesses are already protected with page guards
- saves registers. This includes caller-saved registers, but also temporary registers, as those were possibly used by the affected program. Theoretically, r10 and r11 should be saved as well, but the number of called function and their scope being limited, they are skipped for the sake of reducing the overhead

Quoted 6 lines

My clanker got very excited about r10 and r11 not being saved, just like sashiko. So I looked at this a bit closer. TL;DR it's fine

I built the kernel with gcc 11.5, gcc 15.2 and clang 22 and disassembled __asan_{load,store}{1,2,4,8}: none of them touch r10 or r11. gcc goes up to r8, clang uses nothing above rdi.

Thanks for the double-check, my observations are aligned with yours :)

(https://lore.kernel.org/all/[email protected]/)

Quoted 8 lines

However, kasan_check_range() does use r10/r11. And looks like we are ok only because the fixed-size helpers inline the shadow check and never call kasan_check_range().

If any of the fixed-size 1/2/4/8 helpers ever actually call kasan_check_range(), r10 may get clobbered.

Maybe we should leave a comment on emit_kasan_check() about this?..

Ok, I'll add a comment about this in emit_kasan_check.

[...]

Quoted 7 lines
Quoted 7 lines

static bool all_callee_regs_used[4] = {true, true, true, true};

static u8 *emit_code(u8 *ptr, u32 bytes, unsigned int len)

arch/x86/net/bpf_jit_comp.c +0 -0
@@ -1110,6 +1121,90 @@ static void maybe_emit_1mod(u8 **pprog, u32 reg, bool is64)
 	*pprog = prog;
 }
 
Quoted 9 lines

arch/x86/net/bpf_jit_comp.c static void maybe_emit_1mod(u8 **pprog, u32 reg, bool is64) · line 1126

Quoted 3 lines
arch/x86/net/bpf_jit_comp.c +3 -0
+static int emit_kasan_check(struct bpf_verifier_env *env, u8 **pprog,
+			    u32 addr_reg, struct bpf_insn *insn, u8 *ip,
+			    bool is_write, bool accesses_stack_only)

nit: IMO it's not nice to introduce functions with no caller as a separate patch: it triggers -Wunused-function on this commit, which may or may not be used in a build. I'd fold this in patch #6 where the function is actually called.

Ok, I'll fold it in the commit actually using the helper.

Thanks,

Alexis

Signature · 3 lines
Alexis Lothoré, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#14 lore raw

PATCH v5 5/10 bpf, x86: refactor BPF_ST management in do_jit

In reply to Alexis Lothoré (eBPF Foundation)

In order to prepare for KASAN checks insertion before every memory-related load or store, group all BPF_ST instructions that indeed access memory in a single helper to allow instrumenting those in one call, rather than having to instrument all cases individually.

---
Changes in v3:
- new patch

---
 arch/x86/net/bpf_jit_comp.c | 99 +++++++++++++++++++++++++++------------------
 1 file changed, 60 insertions(+), 39 deletions(-)
arch/x86/net/bpf_jit_comp.c +60 -39
index d35f58350d71..959fcaa0cde9 100644
@@ -1410,6 +1410,64 @@ static void emit_st_index(u8 **pprog, u32 size, u32 dst_reg, u32 index_reg, int
 	*pprog = prog;
 }
 
+static void emit_st(u8 **pprog, struct bpf_insn *insn, int dst_reg,
+		    s32 outgoing_arg_base, u16 outgoing_rsp)
+{
+	s32 imm32 = insn->imm;
+	u8 *prog = *pprog;
+	s32 insn_off;
+
+	switch (BPF_SIZE(insn->code)) {
+	case BPF_B:
+		if (is_ereg(dst_reg))
+			EMIT2(0x41, 0xC6);
+		else
+			EMIT1(0xC6);
+		break;
+	case BPF_H:
+		if (is_ereg(dst_reg))
+			EMIT3(0x66, 0x41, 0xC7);
+		else
+			EMIT2(0x66, 0xC7);
+		break;
+	case BPF_W:
+		if (is_ereg(dst_reg))
+			EMIT2(0x41, 0xC7);
+		else
+			EMIT1(0xC7);
+		break;
+	case BPF_DW:
+		if (dst_reg == BPF_REG_PARAMS && insn->off == -8) {
+			/* Arg 6: store immediate in r9 register */
+			emit_mov_imm64(&prog, X86_REG_R9, imm32 >> 31,
+				       (u32)imm32);
+			*pprog = prog;
+			return;
+		}
+		EMIT2(add_1mod(0x48, dst_reg), 0xC7);
+		break;
+	}
+
+	insn_off = insn->off;
+	if (dst_reg == BPF_REG_PARAMS) {
+		/*
+		 * Args 7+: reverse BPF negative offsets to
+		 * x86 positive rsp offsets.
+		 * BPF off=-16 → [rsp+0], off=-24 → [rsp+8], ...
+		 */
+		insn_off = outgoing_arg_base - outgoing_rsp -
+			insn_off - 16;
+		dst_reg = BPF_REG_FP;
+	}
+	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)));
+	*pprog = prog;
+}
+
 static void emit_st_r12(u8 **pprog, u32 size, u32 dst_reg, int off, int imm)
 {
 	emit_st_index(pprog, size, dst_reg, X86_REG_R12, off, imm);
@@ -2302,49 +2360,12 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 			EMIT_LFENCE();
 			break;
 
-			/* 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_H:
-			if (is_ereg(dst_reg))
-				EMIT3(0x66, 0x41, 0xC7);
-			else
-				EMIT2(0x66, 0xC7);
-			goto st;
 		case BPF_ST | BPF_MEM | BPF_W:
-			if (is_ereg(dst_reg))
-				EMIT2(0x41, 0xC7);
-			else
-				EMIT1(0xC7);
-			goto st;
 		case BPF_ST | BPF_MEM | BPF_DW:
-			if (dst_reg == BPF_REG_PARAMS && insn->off == -8) {
-				/* Arg 6: store immediate in r9 register */
-				emit_mov_imm64(&prog, X86_REG_R9, imm32 >> 31, (u32)imm32);
-				break;
-			}
-			EMIT2(add_1mod(0x48, dst_reg), 0xC7);
-
-st:			insn_off = insn->off;
-			if (dst_reg == BPF_REG_PARAMS) {
-				/*
-				 * Args 7+: reverse BPF negative offsets to
-				 * x86 positive rsp offsets.
-				 * BPF off=-16 → [rsp+0], off=-24 → [rsp+8], ...
-				 */
-				insn_off = outgoing_arg_base - outgoing_rsp - insn_off - 16;
-				dst_reg = BPF_REG_FP;
-			}
-			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)));
+			emit_st(&prog, insn, dst_reg, outgoing_arg_base,
+				outgoing_rsp);
 			break;
 
 			/* STX: *(u8*)(dst_reg + off) = src_reg */
Signature · 1 line
2.54.0
#15 lore raw

Re: [PATCH bpf-next v5 05/10] bpf, x86: refactor BPF_ST management in do_jit

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The `emit_st()` helper absorbs `Arg 6` pseudo-store and `BPF_REG_PARAMS` translation, causing…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The emit_st() helper absorbs Arg 6 pseudo-store and BPF_REG_PARAMS translation, causing invalid KASAN instrumentation for BPF_ST instructions. --

--- Patch [5]: [PATCH bpf-next v5 05/10] bpf, x86: refactor BPF_ST management in do_jit --- commit b88dd49e34d6904772392eec8dfdeba3542805c4
Author: Alexis Lothoré (eBPF Foundation) <[email protected]>

bpf, x86: refactor BPF_ST management in do_jit

This commit extracts the x86-64 BPF_ST instruction emission logic into a single emit_st() helper function. It is intended to prepare for KASAN checks being inserted before memory loads and stores.

Quoted 16 lines
arch/x86/net/bpf_jit_comp.c +8 -0
index d35f58350d710..959fcaa0cde9a 100644
@@ -1410,6 +1410,64 @@ static void emit_st_index(u8 **pprog, u32 size, u32 dst_reg, u32 index_reg, int
 	*pprog = prog;
 }
 
+static void emit_st(u8 **pprog, struct bpf_insn *insn, int dst_reg,
+		    s32 outgoing_arg_base, u16 outgoing_rsp)
+{
+	s32 imm32 = insn->imm;
+	u8 *prog = *pprog;
+	s32 insn_off;
+
+	switch (BPF_SIZE(insn->code)) {

[ ... ]

Quoted 13 lines
arch/x86/net/bpf_jit_comp.c +13 -0
+	case BPF_DW:
+		if (dst_reg == BPF_REG_PARAMS && insn->off == -8) {
+			/* Arg 6: store immediate in r9 register */
+			emit_mov_imm64(&prog, X86_REG_R9, imm32 >> 31,
+				       (u32)imm32);
+			*pprog = prog;
+			return;
+		}
+		EMIT2(add_1mod(0x48, dst_reg), 0xC7);
+		break;
+	}
+
+	insn_off = insn->off;

arch/x86/net/bpf_jit_comp.c static void emit_st_index(u8 **pprog, u32 size, u32 dst_reg, u32 index_reg, int · line 1461

Quoted 10 lines
arch/x86/net/bpf_jit_comp.c +10 -0
+	if (dst_reg == BPF_REG_PARAMS) {
+		/*
+		 * Args 7+: reverse BPF negative offsets to
+		 * x86 positive rsp offsets.
+		 * BPF off=-16 → [rsp+0], off=-24 → [rsp+8], ...
+		 */
+		insn_off = outgoing_arg_base - outgoing_rsp -
+			insn_off - 16;
+		dst_reg = BPF_REG_FP;
+	}

[Severity: High]
Does hiding the BPF_REG_PARAMS translation inside emit_st() conflict with the stated goal of adding KASAN instrumentation before the call?

If KASAN checks are inserted in do_jit() before emit_st() is called, wouldn't they attempt to instrument Arg 6 (which uses register r9, not memory)?

Additionally, wouldn't any KASAN checks added before this helper use the raw BPF_REG_PARAMS pseudo-register before it is translated to BPF_REG_FP, causing instrumentation against an unmapped register (which evaluates to rax)?

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
reviewer
#16 lore raw

Re: [PATCH bpf-next v5 05/10] bpf, x86: refactor BPF_ST management in do_jit

Quoted 59 lines · On 7/9/26 4:08 AM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The emit_st() helper absorbs Arg 6 pseudo-store and BPF_REG_PARAMS translation, causing invalid KASAN instrumentation for BPF_ST instructions. --

--- Patch [5]: [PATCH bpf-next v5 05/10] bpf, x86: refactor BPF_ST management in do_jit --- commit b88dd49e34d6904772392eec8dfdeba3542805c4
Author: Alexis Lothoré (eBPF Foundation) <[email protected]>

bpf, x86: refactor BPF_ST management in do_jit

This commit extracts the x86-64 BPF_ST instruction emission logic into a single emit_st() helper function. It is intended to prepare for KASAN checks being inserted before memory loads and stores.

Quoted 16 lines
arch/x86/net/bpf_jit_comp.c +8 -0
index d35f58350d710..959fcaa0cde9a 100644
@@ -1410,6 +1410,64 @@ static void emit_st_index(u8 **pprog, u32 size, u32 dst_reg, u32 index_reg, int
 	*pprog = prog;
 }
 
+static void emit_st(u8 **pprog, struct bpf_insn *insn, int dst_reg,
+		    s32 outgoing_arg_base, u16 outgoing_rsp)
+{
+	s32 imm32 = insn->imm;
+	u8 *prog = *pprog;
+	s32 insn_off;
+
+	switch (BPF_SIZE(insn->code)) {

[ ... ]

Quoted 13 lines
arch/x86/net/bpf_jit_comp.c +13 -0
+	case BPF_DW:
+		if (dst_reg == BPF_REG_PARAMS && insn->off == -8) {
+			/* Arg 6: store immediate in r9 register */
+			emit_mov_imm64(&prog, X86_REG_R9, imm32 >> 31,
+				       (u32)imm32);
+			*pprog = prog;
+			return;
+		}
+		EMIT2(add_1mod(0x48, dst_reg), 0xC7);
+		break;
+	}
+
+	insn_off = insn->off;

arch/x86/net/bpf_jit_comp.c static void emit_st_index(u8 **pprog, u32 size, u32 dst_reg, u32 index_reg, int · line 1461

Quoted 10 lines
arch/x86/net/bpf_jit_comp.c +10 -0
+	if (dst_reg == BPF_REG_PARAMS) {
+		/*
+		 * Args 7+: reverse BPF negative offsets to
+		 * x86 positive rsp offsets.
+		 * BPF off=-16 → [rsp+0], off=-24 → [rsp+8], ...
+		 */
+		insn_off = outgoing_arg_base - outgoing_rsp -
+			insn_off - 16;
+		dst_reg = BPF_REG_FP;
+	}

[Severity: High]
Does hiding the BPF_REG_PARAMS translation inside emit_st() conflict with the stated goal of adding KASAN instrumentation before the call?

Quoted 6 lines

If KASAN checks are inserted in do_jit() before emit_st() is called, wouldn't they attempt to instrument Arg 6 (which uses register r9, not memory)?

Additionally, wouldn't any KASAN checks added before this helper use the raw BPF_REG_PARAMS pseudo-register before it is translated to BPF_REG_FP, causing instrumentation against an unmapped register (which evaluates to rax)?

I think sashiko is wrong, because BPF_REG_PARAMS stores should always have non_stack_access = false, but please double check

Quoted 3 lines

[ ... ]

#17 lore raw

Re: [PATCH bpf-next v5 05/10] bpf, x86: refactor BPF_ST management in do_jit

Quoted 1 line · Ihor Solodrai

On 7/9/26 4:08 AM, [email protected] wrote:

[...]

Quoted 19 lines
Quoted 19 lines

arch/x86/net/bpf_jit_comp.c static void emit_st_index(u8 **pprog, u32 size, u32 dst_reg, u32 index_reg, int · line 1461

Quoted 11 lines
arch/x86/net/bpf_jit_comp.c +11 -0
+	insn_off = insn->off;
+	if (dst_reg == BPF_REG_PARAMS) {
+		/*
+		 * Args 7+: reverse BPF negative offsets to
+		 * x86 positive rsp offsets.
+		 * BPF off=-16 → [rsp+0], off=-24 → [rsp+8], ...
+		 */
+		insn_off = outgoing_arg_base - outgoing_rsp -
+			insn_off - 16;
+		dst_reg = BPF_REG_FP;
+	}

[Severity: High]
Does hiding the BPF_REG_PARAMS translation inside emit_st() conflict with the stated goal of adding KASAN instrumentation before the call?

If KASAN checks are inserted in do_jit() before emit_st() is called, wouldn't they attempt to instrument Arg 6 (which uses register r9, not memory)?

Quoted 6 lines
Quoted 3 lines

Additionally, wouldn't any KASAN checks added before this helper use the raw BPF_REG_PARAMS pseudo-register before it is translated to BPF_REG_FP, causing instrumentation against an unmapped register (which evaluates to rax)?

I think sashiko is wrong, because BPF_REG_PARAMS stores should always have non_stack_access = false, but please double check

So I've checked this empirically by monitoring any call to emit_kasan_check with addr_reg >= MAX_BPF_REG and running the whole selftests set. I confirm that BPF_REG_PARAMS is never passed to emit_kasan_check.

While doing so however, I have spotted an unrelated example of undesired instrumentation on some stack access (seeing emit_kasan_check receiving 15 -> X86_REG_R9)
- pyperf600_bpf_loop.bpf.o uses a bpf_loop() and is of type "raw tracepoint"
- the verifier inlines the bpf_loop() call, and so generates those insns:
[...]
185: (7b) *(u64 *)(r10 -352) = r6
186: (7b) *(u64 *)(r10 -344) = r7
187: (7b) *(u64 *)(r10 -336) = r8
[...]
- those instructions, not bearing any former marking (because they are new/generated by the fixups) , are systematically marked with non_stack_access=true, despite only accessing stack.
- when the JIT comp encounters those insn, it turns r10 into X86 r9, and are systematically instrumented, hence the value received by emit_kasan_check.

I _think_ it's ok (to be confirmed) and if so, this looks like a slight overhead that we may agree to afford, based on the discussions on the other commits. Or, I can make the verifier fixups (adjust_insn_aux_data) slightly smarter, and systematically set non_stack_access=true for mem accessing insn:
1. that aren't the original insn
2. and for which _neither dst_reg nor src_reg is r10_

Alexis

NB: I am OoO until 03/08, I'll resume this investigation when I get back.

Signature · 3 lines
Alexis Lothoré, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
reviewer Acked-by on v5 5/10
#18 lore raw

Re: [PATCH bpf-next v5 05/10] bpf, x86: refactor BPF_ST management in do_jit

In reply to Alexis Lothoré (eBPF Foundation)
Quoted 6 lines · Alexis Lothoré (eBPF Foundation)

In order to prepare for KASAN checks insertion before every memory-related load or store, group all BPF_ST instructions that indeed access memory in a single helper to allow instrumenting those in one call, rather than having to instrument all cases individually.

The refactoring looks good to me.

Quoted 9 lines

---
Changes in v3:
- new patch

---
 arch/x86/net/bpf_jit_comp.c | 99 +++++++++++++++++++++++++++------------------
 1 file changed, 60 insertions(+), 39 deletions(-)

[...]

#19 lore raw

PATCH v5 6/10 bpf, x86: emit KASAN checks into x86 JITed programs

In reply to Alexis Lothoré (eBPF Foundation)

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.

The main instructions being targeted are BPF_ST, BPF_STX and BPF_LDX, but not all of them are being instrumented:
- if the load/store instruction is in fact accessing the program stack, emit_kasan_check silently skips the instrumentation, as we can already benefit from guard pages to monitor stack accesses.
- if the load/store instruction is a BPF_PROBE_MEM or a BPF_PROBE_ATOMIC instruction, we do not instrument it, as the passed address can fault (hence the custom fault management with BPF_PROBE_XXX instructions), and so the corresponding kasan check could fault as well.

---
Changes in v5:
- change access type (read -> write) for atomic RMW check

Changes in v4:
- refactor BPF_FETCH handling

Changes in v3:
- fix LLVM23 build failure

Changes in v2:
- support BPF_ATOMICS
- support BPF_ST
- make sure to systematically pass correct instruction to kasan check

---
 arch/x86/net/bpf_jit_comp.c | 79 ++++++++++++++++++++++++++++++++++++---------
 1 file changed, 63 insertions(+), 16 deletions(-)
arch/x86/net/bpf_jit_comp.c +63 -16
index 959fcaa0cde9..9dbe3a52444e 100644
@@ -1576,17 +1576,31 @@ static int emit_atomic_rmw_index(u8 **pprog, u32 atomic_op, u32 size,
 	return 0;
 }
 
-static int emit_atomic_ld_st(u8 **pprog, u32 atomic_op, u32 dst_reg,
-			     u32 src_reg, s16 off, u8 bpf_size)
+static int emit_atomic_ld_st(struct bpf_verifier_env *env, u8 **pprog,
+			     struct bpf_insn *insn, u8 *ip, u32 dst_reg,
+			     u32 src_reg, bool accesses_stack_only)
 {
+	u32 atomic_op = insn->imm;
+	int err;
+
 	switch (atomic_op) {
 	case BPF_LOAD_ACQ:
+		err = emit_kasan_check(env, pprog, src_reg, insn, ip, false,
+				       accesses_stack_only);
+		if (err)
+			return err;
 		/* dst_reg = smp_load_acquire(src_reg + off16) */
-		emit_ldx(pprog, bpf_size, dst_reg, src_reg, off);
+		emit_ldx(pprog, BPF_SIZE(insn->code), dst_reg, src_reg,
+			 insn->off);
 		break;
 	case BPF_STORE_REL:
+		err = emit_kasan_check(env, pprog, dst_reg, insn, ip, true,
+				       accesses_stack_only);
+		if (err)
+			return err;
 		/* smp_store_release(dst_reg + off16, src_reg) */
-		emit_stx(pprog, bpf_size, dst_reg, src_reg, off);
+		emit_stx(pprog, BPF_SIZE(insn->code), dst_reg, src_reg,
+			 insn->off);
 		break;
 	default:
 		pr_err("bpf_jit: unknown atomic load/store opcode %02x\n",
@@ -1964,10 +1978,12 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 		const s32 imm32 = insn->imm;
 		u32 dst_reg = insn->dst_reg;
 		u32 src_reg = insn->src_reg;
+		bool accesses_stack_only;
 		u8 b2 = 0, b3 = 0;
 		u8 *start_of_ldx;
 		s64 jmp_offset;
 		s32 insn_off;
+		int insn_idx;
 		u8 jmp_cond;
 		u8 *func;
 		int nops;
@@ -1984,6 +2000,10 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 			EMIT_ENDBR();
 
 		ip = image + addrs[i - 1] + (prog - temp);
+		insn_idx = i - 1 + bpf_prog->aux->subprog_start;
+		accesses_stack_only =
+			env ? !env->insn_aux_data[insn_idx].non_stack_access :
+			      false;
 
 		switch (insn->code) {
 			/* ALU */
@@ -2364,6 +2384,11 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 		case BPF_ST | BPF_MEM | BPF_H:
 		case BPF_ST | BPF_MEM | BPF_W:
 		case BPF_ST | BPF_MEM | BPF_DW:
+			err = emit_kasan_check(env, &prog, dst_reg, insn, ip,
+					       true, accesses_stack_only);
+			if (err)
+				return err;
+
 			emit_st(&prog, insn, dst_reg, outgoing_arg_base,
 				outgoing_rsp);
 			break;
@@ -2383,6 +2408,10 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 				insn_off = outgoing_arg_base - outgoing_rsp - insn_off - 16;
 				dst_reg = BPF_REG_FP;
 			}
+			err = emit_kasan_check(env, &prog, dst_reg, insn, ip,
+					       true, accesses_stack_only);
+			if (err)
+				return err;
 			emit_stx(&prog, BPF_SIZE(insn->code), dst_reg, src_reg, insn_off);
 			break;
 
@@ -2544,6 +2573,12 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 				/* 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(env, &prog, src_reg,
+						       insn, ip, false,
+						       accesses_stack_only);
+				if (err)
+					return err;
 			}
 			if (BPF_MODE(insn->code) == BPF_PROBE_MEMSX ||
 			    BPF_MODE(insn->code) == BPF_MEMSX)
@@ -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, true,
+						       accesses_stack_only);
+				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);
@@ -2658,15 +2704,16 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 			}
 
 			if (bpf_atomic_is_load_store(insn))
-				err = emit_atomic_ld_st(&prog, insn->imm, dst_reg, src_reg,
-							insn->off, BPF_SIZE(insn->code));
+				err = emit_atomic_ld_st(env, &prog, insn, ip,
+							dst_reg, src_reg,
+							accesses_stack_only);
 			else
 				err = emit_atomic_rmw(&prog, insn->imm, dst_reg, src_reg,
 						      insn->off, BPF_SIZE(insn->code));
 			if (err)
 				return err;
 			break;
-
+		}
 		case BPF_STX | BPF_PROBE_ATOMIC | BPF_B:
 		case BPF_STX | BPF_PROBE_ATOMIC | BPF_H:
 			if (!bpf_atomic_is_load_store(insn)) {
Signature · 1 line
2.54.0
#20 lore raw

Re: [PATCH bpf-next v5 06/10] bpf, x86: emit KASAN checks into x86 JITed programs

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] KASAN checks clobber caller-saved registers %r10 (BPF_REG_AX) and %r11 (AUX_REG), leading to…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] KASAN checks clobber caller-saved registers %r10 (BPF_REG_AX) and %r11 (AUX_REG), leading to memory corruption --

--- Patch [6]: [PATCH bpf-next v5 06/10] bpf, x86: emit KASAN checks into x86 JITed programs --- commit e841cd22d378b4b1ec9b5d8608f051fedbf3837f
Author: Alexis Lothoré (eBPF Foundation) <[email protected]>

bpf, x86: emit KASAN checks into x86 JITed programs

This commit inserts 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.

Quoted 2 lines
arch/x86/net/bpf_jit_comp.c +0 -0

[ ... ]

Quoted 27 lines
arch/x86/net/bpf_jit_comp.c +10 -9
@@ -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.

arch/x86/net/bpf_jit_comp.c static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * · line 2670

Quoted 9 lines
arch/x86/net/bpf_jit_comp.c +6 -0
@@ -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, true,
+						       accesses_stack_only);

[Severity: Critical]
Does this sequence clobber the temporarily saved BPF_REG_0 register?

The CMPXCHG loop saves BPF_REG_0 into BPF_REG_AX just before this block:

    /* Will need RAX as a CMPXCHG operand so save R0 */
    emit_mov_reg(&prog, true, BPF_REG_AX, BPF_REG_0);

Immediately after, emit_kasan_check() emits a C function call to the KASAN runtime. However, emit_kasan_check() does not push and pop BPF_REG_AX or AUX_REG in its register save sequence.

Since those registers map to %r10 and %r11, which are caller-saved in the x86-64 System V ABI, the KASAN C functions can overwrite them. The later restore of BPF_REG_0 from BPF_REG_AX would then yield a garbage value.

Could this lead to memory corruption, especially if the verifier also utilizes BPF_REG_AX for constant blinding across BPF_ST instructions which are similarly instrumented by emit_kasan_check()?

Quoted 4 lines
arch/x86/net/bpf_jit_comp.c +3 -0
+				if (err)
+					return err;
 				branch_target = prog;
+			}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
reviewer
#21 lore raw

Re: [PATCH bpf-next v5 06/10] bpf, x86: emit KASAN checks into x86 JITed programs

In reply to Alexis Lothoré (eBPF Foundation)
Quoted 87 lines · Alexis Lothoré (eBPF Foundation)

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.

The main instructions being targeted are BPF_ST, BPF_STX and BPF_LDX, but not all of them are being instrumented:
- if the load/store instruction is in fact accessing the program stack, emit_kasan_check silently skips the instrumentation, as we can already benefit from guard pages to monitor stack accesses.
- if the load/store instruction is a BPF_PROBE_MEM or a BPF_PROBE_ATOMIC instruction, we do not instrument it, as the passed address can fault (hence the custom fault management with BPF_PROBE_XXX instructions), and so the corresponding kasan check could fault as well.

---
Changes in v5:
- change access type (read -> write) for atomic RMW check

Changes in v4:
- refactor BPF_FETCH handling

Changes in v3:
- fix LLVM23 build failure

Changes in v2:
- support BPF_ATOMICS
- support BPF_ST
- make sure to systematically pass correct instruction to kasan check

---
 arch/x86/net/bpf_jit_comp.c | 79 ++++++++++++++++++++++++++++++++++++---------
 1 file changed, 63 insertions(+), 16 deletions(-)
arch/x86/net/bpf_jit_comp.c +20 -4
index 959fcaa0cde9..9dbe3a52444e 100644
@@ -1576,17 +1576,31 @@ static int emit_atomic_rmw_index(u8 **pprog, u32 atomic_op, u32 size,
 	return 0;
 }
 
-static int emit_atomic_ld_st(u8 **pprog, u32 atomic_op, u32 dst_reg,
-			     u32 src_reg, s16 off, u8 bpf_size)
+static int emit_atomic_ld_st(struct bpf_verifier_env *env, u8 **pprog,
+			     struct bpf_insn *insn, u8 *ip, u32 dst_reg,
+			     u32 src_reg, bool accesses_stack_only)
 {
+	u32 atomic_op = insn->imm;
+	int err;
+
 	switch (atomic_op) {
 	case BPF_LOAD_ACQ:
+		err = emit_kasan_check(env, pprog, src_reg, insn, ip, false,
+				       accesses_stack_only);
+		if (err)
+			return err;
 		/* dst_reg = smp_load_acquire(src_reg + off16) */
-		emit_ldx(pprog, bpf_size, dst_reg, src_reg, off);
+		emit_ldx(pprog, BPF_SIZE(insn->code), dst_reg, src_reg,
+			 insn->off);
 		break;
 	case BPF_STORE_REL:
+		err = emit_kasan_check(env, pprog, dst_reg, insn, ip, true,
+				       accesses_stack_only);
+		if (err)
+			return err;
 		/* smp_store_release(dst_reg + off16, src_reg) */
-		emit_stx(pprog, bpf_size, dst_reg, src_reg, off);
+		emit_stx(pprog, BPF_SIZE(insn->code), dst_reg, src_reg,
+			 insn->off);
 		break;
 	default:
 		pr_err("bpf_jit: unknown atomic load/store opcode %02x\n",
@@ -1964,10 +1978,12 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 		const s32 imm32 = insn->imm;
 		u32 dst_reg = insn->dst_reg;
 		u32 src_reg = insn->src_reg;
+		bool accesses_stack_only;
 		u8 b2 = 0, b3 = 0;
 		u8 *start_of_ldx;
 		s64 jmp_offset;
 		s32 insn_off;
+		int insn_idx;
 		u8 jmp_cond;
 		u8 *func;
 		int nops;

arch/x86/net/bpf_jit_comp.c static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * · line 2006

Quoted 7 lines
arch/x86/net/bpf_jit_comp.c +4 -0
@@ -1984,6 +2000,10 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 			EMIT_ENDBR();
 
 		ip = image + addrs[i - 1] + (prog - temp);
+		insn_idx = i - 1 + bpf_prog->aux->subprog_start;
+		accesses_stack_only =
+			env ? !env->insn_aux_data[insn_idx].non_stack_access :
+			      false;

I don't like how accesses_stack_only is a boolean argument to emit_kasan_check(), that is only used for early return. I can see not wanting to do

    if (!accesses_stack_only)
        err = emit_kasan_check(...);

on every call site, but a bool arg is even worse IMO.

Quoted 66 lines
arch/x86/net/bpf_jit_comp.c +25 -9
 		switch (insn->code) {
 			/* ALU */
@@ -2364,6 +2384,11 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 		case BPF_ST | BPF_MEM | BPF_H:
 		case BPF_ST | BPF_MEM | BPF_W:
 		case BPF_ST | BPF_MEM | BPF_DW:
+			err = emit_kasan_check(env, &prog, dst_reg, insn, ip,
+					       true, accesses_stack_only);
+			if (err)
+				return err;
+
 			emit_st(&prog, insn, dst_reg, outgoing_arg_base,
 				outgoing_rsp);
 			break;
@@ -2383,6 +2408,10 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 				insn_off = outgoing_arg_base - outgoing_rsp - insn_off - 16;
 				dst_reg = BPF_REG_FP;
 			}
+			err = emit_kasan_check(env, &prog, dst_reg, insn, ip,
+					       true, accesses_stack_only);
+			if (err)
+				return err;
 			emit_stx(&prog, BPF_SIZE(insn->code), dst_reg, src_reg, insn_off);
 			break;
 
@@ -2544,6 +2573,12 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 				/* 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(env, &prog, src_reg,
+						       insn, ip, false,
+						       accesses_stack_only);
+				if (err)
+					return err;
 			}
 			if (BPF_MODE(insn->code) == BPF_PROBE_MEMSX ||
 			    BPF_MODE(insn->code) == BPF_MEMSX)
@@ -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.

arch/x86/net/bpf_jit_comp.c static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * · line 2665

Quoted 4 lines
arch/x86/net/bpf_jit_comp.c +1 -0
@@ -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;

nit

IIUC the 3 comes from emit_mov_reg() above it (:2659), right? I was thinking to suggest a comment, but well I figured it out from the code, so others should too...

Maybe instead of hardcoding 3 we could calculcate the diff before/after the mov is emitted?

Quoted 34 lines
arch/x86/net/bpf_jit_comp.c +13 -3
+			}
+			if (!bpf_atomic_is_load_store(insn)) {
+				err = emit_kasan_check(env, &prog, real_dst_reg,
+						       insn, ip, true,
+						       accesses_stack_only);
+				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);
@@ -2658,15 +2704,16 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 			}
 
 			if (bpf_atomic_is_load_store(insn))
-				err = emit_atomic_ld_st(&prog, insn->imm, dst_reg, src_reg,
-							insn->off, BPF_SIZE(insn->code));
+				err = emit_atomic_ld_st(env, &prog, insn, ip,
+							dst_reg, src_reg,
+							accesses_stack_only);
 			else
 				err = emit_atomic_rmw(&prog, insn->imm, dst_reg, src_reg,
 						      insn->off, BPF_SIZE(insn->code));
 			if (err)
 				return err;
 			break;
-
+		}
 		case BPF_STX | BPF_PROBE_ATOMIC | BPF_B:
 		case BPF_STX | BPF_PROBE_ATOMIC | BPF_H:
 			if (!bpf_atomic_is_load_store(insn)) {
#22 lore raw

Re: [PATCH bpf-next v5 06/10] bpf, x86: emit KASAN checks into x86 JITed programs

Quoted 1 line · Ihor Solodrai

On 7/9/26 3:01 AM, Alexis Lothoré (eBPF Foundation) wrote:

[...]

Quoted 13 lines

arch/x86/net/bpf_jit_comp.c static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * · line 2006

Quoted 8 lines
arch/x86/net/bpf_jit_comp.c +4 -0
@@ -1984,6 +2000,10 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *
 			EMIT_ENDBR();
 
 		ip = image + addrs[i - 1] + (prog - temp);
+		insn_idx = i - 1 + bpf_prog->aux->subprog_start;
+		accesses_stack_only =
+			env ? !env->insn_aux_data[insn_idx].non_stack_access :
+			      false;

I don't like how accesses_stack_only is a boolean argument to emit_kasan_check(), that is only used for early return. I can see not wanting to do

Quoted 4 lines
    if (!accesses_stack_only)
        err = emit_kasan_check(...);

on every call site, but a bool arg is even worse IMO.

Yes, that was indeed the intent, but ok, I can get back to set the stack access check out of the function.

[...]

Quoted 11 lines

arch/x86/net/bpf_jit_comp.c static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int * · line 2665

Quoted 5 lines
arch/x86/net/bpf_jit_comp.c +1 -0
@@ -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;

nit

IIUC the 3 comes from emit_mov_reg() above it (:2659), right? I was thinking to suggest a comment, but well I figured it out from the code, so others should too...

Yes, that's indeed where it comes from (and this cost me quite some debugging time to spot that I was missing this offset !)

Quoted 3 lines

Maybe instead of hardcoding 3 we could calculcate the diff before/after the mov is emitted?

ACK, I'll remove the hardcoded scalar and set it to (prog - *pprog)

Alexis

Signature · 3 lines
Alexis Lothoré, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#23 lore raw

PATCH v5 7/10 bpf, x86: enable KASAN for JITed programs on x86

In reply to Alexis Lothoré (eBPF Foundation)

Mark x86 as supporting KASAN checks in JITed programs so that the corresponding JIT compiler inserts checks on the translated instructions.

---
 arch/x86/Kconfig | 1 +
 1 file changed, 1 insertion(+)
arch/x86/Kconfig +1 -0
index bdad90f210e4..705a9ab85f3a 100644
@@ -233,6 +233,7 @@ config X86
 	select HAVE_SAMPLE_FTRACE_DIRECT	if X86_64
 	select HAVE_SAMPLE_FTRACE_DIRECT_MULTI	if X86_64
 	select HAVE_EBPF_JIT
+	select HAVE_EBPF_JIT_KASAN		if X86_64
 	select HAVE_EFFICIENT_UNALIGNED_ACCESS
 	select HAVE_EISA			if X86_32
 	select HAVE_EXIT_THREAD
Signature · 1 line
2.54.0
#24 lore raw

Re: [PATCH bpf-next v5 07/10] bpf, x86: enable KASAN for JITed programs on x86

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The BPF JIT KASAN integration on x86-64 fails to preserve caller-saved registers `r10` and `r11`…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The BPF JIT KASAN integration on x86-64 fails to preserve caller-saved registers r10 and r11 across calls to KASAN functions, corrupting BPF internal state. --

--- Patch [7]: [PATCH bpf-next v5 07/10] bpf, x86: enable KASAN for JITed programs on x86 --- commit ecf3ad3bc8d5ddd31f31891c28722d7d1b08f230
Author: Alexis Lothoré (eBPF Foundation) <[email protected]>

bpf, x86: enable KASAN for JITed programs on x86

This commit marks x86 as supporting KASAN checks in JITed programs. It enables the corresponding JIT compiler to insert checks on translated instructions.

arch/x86/Kconfig config X86 · line 236

Quoted 9 lines
arch/x86/Kconfig +1 -0
index bdad90f210e4b..705a9ab85f3a9 100644
@@ -233,6 +233,7 @@ config X86
 	select HAVE_SAMPLE_FTRACE_DIRECT	if X86_64
 	select HAVE_SAMPLE_FTRACE_DIRECT_MULTI	if X86_64
 	select HAVE_EBPF_JIT
+	select HAVE_EBPF_JIT_KASAN		if X86_64

[Severity: High]
Enabling this option exposes a register corruption regression in the underlying KASAN JIT implementation.

Looking at how emit_kasan_check() is implemented in arch/x86/net/bpf_jit_comp.c, it saves most caller-saved registers but explicitly skips r10 and r11:

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

	...
	/* call kasan_func */
	if (emit_call(&prog, kasan_func, ip))
		return -ERANGE;
	...
}

However, during atomic fetch operations, do_jit() relies on BPF_REG_AX (which maps to r10) holding live state across the emit_kasan_check() call:

arch/x86/net/bpf_jit_comp.c:do_jit() {
		...
		case BPF_STX | BPF_ATOMIC | BPF_DW: {
			...
			if (is_atomic_fetch) {
				...
				/* Will need RAX as a CMPXCHG operand so save R0 */
				emit_mov_reg(&prog, true, BPF_REG_AX, BPF_REG_0);
				if (src_reg == BPF_REG_0)
					real_src_reg = BPF_REG_AX;
				if (dst_reg == BPF_REG_0)
					real_dst_reg = BPF_REG_AX;
			}
			if (!bpf_atomic_is_load_store(insn)) {
				err = emit_kasan_check(env, &prog, real_dst_reg,
						       insn, ip, true,
						       accesses_stack_only);
			...
}

Since __asan_store* is a standard C function, it can clobber r10 (BPF_REG_AX) according to the System V AMD64 ABI.

Could the omission of r10 and r11 in emit_kasan_check() lead to corrupted internal BPF state when the verifier expects BPF_REG_AX to be preserved?

Quoted 3 lines
arch/x86/Kconfig +0 -0
 	select HAVE_EFFICIENT_UNALIGNED_ACCESS
 	select HAVE_EISA			if X86_32
 	select HAVE_EXIT_THREAD
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
reviewer Acked-by on v5 7/10
#25 lore raw

Re: [PATCH bpf-next v5 07/10] bpf, x86: enable KASAN for JITed programs on x86

In reply to Alexis Lothoré (eBPF Foundation)
Quoted 14 lines · Alexis Lothoré (eBPF Foundation)

Mark x86 as supporting KASAN checks in JITed programs so that the corresponding JIT compiler inserts checks on the translated instructions.

---
 arch/x86/Kconfig | 1 +
 1 file changed, 1 insertion(+)
arch/x86/Kconfig +0 -0
index bdad90f210e4..705a9ab85f3a 100644

arch/x86/Kconfig config X86 · line 236

Quoted 4 lines
arch/x86/Kconfig +1 -0
@@ -233,6 +233,7 @@ config X86
 	select HAVE_SAMPLE_FTRACE_DIRECT	if X86_64
 	select HAVE_SAMPLE_FTRACE_DIRECT_MULTI	if X86_64
 	select HAVE_EBPF_JIT
+	select HAVE_EBPF_JIT_KASAN		if X86_64
Quoted 4 lines
arch/x86/Kconfig +0 -0
 	select HAVE_EFFICIENT_UNALIGNED_ACCESS
 	select HAVE_EISA			if X86_32
 	select HAVE_EXIT_THREAD
#26 lore raw

PATCH v5 8/10 selftests/bpf: add helpers for KASAN in JIT testing

In reply to Alexis Lothoré (eBPF Foundation)

Add two simple helpers to allow checking whether KASAN for eBPF tests should be executed:
- one helper to check if BPF_JIT_KASAN is enabled in kernel configuration
- one helper to check if the kernel is running with kasan_multi_shot (otherwise only the first test will be able to trigger a report)

---
Changes in v5:
- add kasan_multi_shot helper

Changes in v2:
- fix condition

---
 tools/testing/selftests/bpf/unpriv_helpers.c | 10 ++++++++++
 tools/testing/selftests/bpf/unpriv_helpers.h |  2 ++
 2 files changed, 12 insertions(+)
tools/testing/selftests/bpf/unpriv_helpers.c +10 -0
index f997d7ec8fd0..95be83937883 100644
@@ -142,3 +142,13 @@ bool get_unpriv_disabled(void)
 	}
 	return mitigations_off;
 }
+
+bool get_kasan_jit_enabled(void)
+{
+	return config_contains("CONFIG_BPF_JIT_KASAN=y") == 1;
+}
+
+bool get_kasan_multi_shot_enabled(void)
+{
+	return cmdline_contains("kasan_multi_shot");
+}
tools/testing/selftests/bpf/unpriv_helpers.h +2 -0
index 151f67329665..a7ceb51577cd 100644
@@ -5,3 +5,5 @@
 #define UNPRIV_SYSCTL "kernel/unprivileged_bpf_disabled"
 
 bool get_unpriv_disabled(void);
+bool get_kasan_jit_enabled(void);
+bool get_kasan_multi_shot_enabled(void);
Signature · 1 line
2.54.0
#27 lore raw

Re: [PATCH bpf-next v5 08/10] selftests/bpf: add helpers for KASAN in JIT testing

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The newly added get_kasan_multi_shot_enabled() helper is highly unreliable due to its reliance…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The newly added get_kasan_multi_shot_enabled() helper is highly unreliable due to its reliance on the broken cmdline_contains() helper, which reads uninitialized stack memory and performs incorrect prefix matching. --

--- Patch [8]: [PATCH bpf-next v5 08/10] selftests/bpf: add helpers for KASAN in JIT testing --- commit 89c5dbabce02201a3bf78105cd83cff83602d41f
Author: Alexis Lothoré (eBPF Foundation) <[email protected]>

selftests/bpf: add helpers for KASAN in JIT testing

Add two simple helpers to allow checking whether KASAN for eBPF tests should be executed.

Quoted 4 lines
tools/testing/selftests/bpf/unpriv_helpers.c +0 -0
index f997d7ec8fd08..95be839378837 100644

[ ... ]

Quoted 4 lines
tools/testing/selftests/bpf/unpriv_helpers.c +0 -0
@@ -142,3 +142,13 @@ bool get_unpriv_disabled(void)
 	}
 	return mitigations_off;
 }

tools/testing/selftests/bpf/unpriv_helpers.c bool get_unpriv_disabled(void) · line 154

Quoted 10 lines
tools/testing/selftests/bpf/unpriv_helpers.c +10 -0
+
+bool get_kasan_jit_enabled(void)
+{
+	return config_contains("CONFIG_BPF_JIT_KASAN=y") == 1;
+}
+
+bool get_kasan_multi_shot_enabled(void)
+{
+	return cmdline_contains("kasan_multi_shot");
+}

[Severity: Medium]
Can this return false positives due to the parsing logic in cmdline_contains()?

Looking at cmdline_contains(), the buffer isn't correctly null-terminated after reading:

    if (read(fd, cmdline, sizeof(cmdline) - 1) < 0) {
        perror("read /proc/cmdline");
        goto out;
    }

    cmdline[sizeof(cmdline) - 1] = '\0';

This leaves uninitialized stack memory between the read bytes and the end of the array, which gets tokenized by strtok().

Additionally, the token matching logic uses strlen(c) instead of the pattern length:

    for (c = strtok(cmdline, " \n"); c; c = strtok(NULL, " \n")) {
        if (strncmp(c, pat, strlen(c)))
            continue;

Because strncmp() checks only the length of the token, any kernel parameter that happens to be a prefix of "kasan_multi_shot" (such as "k" or "kasan") would incorrectly return true.

Could cmdline_contains() be updated to null-terminate at the read length and use strcmp() for exact matching to avoid non-deterministic test behavior?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
reviewer Acked-by on v5 8/10
#28 lore raw

Re: [PATCH bpf-next v5 08/10] selftests/bpf: add helpers for KASAN in JIT testing

In reply to Alexis Lothoré (eBPF Foundation)
Quoted 27 lines · Alexis Lothoré (eBPF Foundation)

Add two simple helpers to allow checking whether KASAN for eBPF tests should be executed:
- one helper to check if BPF_JIT_KASAN is enabled in kernel configuration
- one helper to check if the kernel is running with kasan_multi_shot (otherwise only the first test will be able to trigger a report)

---
Changes in v5:
- add kasan_multi_shot helper

Changes in v2:
- fix condition

---
 tools/testing/selftests/bpf/unpriv_helpers.c | 10 ++++++++++
 tools/testing/selftests/bpf/unpriv_helpers.h |  2 ++
 2 files changed, 12 insertions(+)
tools/testing/selftests/bpf/unpriv_helpers.c +0 -0
index f997d7ec8fd0..95be83937883 100644
@@ -142,3 +142,13 @@ bool get_unpriv_disabled(void)
 	}
 	return mitigations_off;
 }

tools/testing/selftests/bpf/unpriv_helpers.c bool get_unpriv_disabled(void) · line 154

Quoted 10 lines
tools/testing/selftests/bpf/unpriv_helpers.c +10 -0
+
+bool get_kasan_jit_enabled(void)
+{
+	return config_contains("CONFIG_BPF_JIT_KASAN=y") == 1;
+}
+
+bool get_kasan_multi_shot_enabled(void)
+{
+	return cmdline_contains("kasan_multi_shot");
+}
Quoted 11 lines
tools/testing/selftests/bpf/unpriv_helpers.h +2 -0
index 151f67329665..a7ceb51577cd 100644
@@ -5,3 +5,5 @@
 #define UNPRIV_SYSCTL "kernel/unprivileged_bpf_disabled"
 
 bool get_unpriv_disabled(void);
+bool get_kasan_jit_enabled(void);
+bool get_kasan_multi_shot_enabled(void);
#29 lore raw

PATCH v5 9/10 selftests/bpf: move bpf_jit_harden helper into testing_helpers

In reply to Alexis Lothoré (eBPF Foundation)

Move set_bpf_jit_harden to testing helpers so that other selftests can change the hardening configuration without re-implementing a helper.

---
Changes in v5:
- check set_bpf_jit_harden return code in cleanup path

Changes in v3:
- new patch

---
 .../selftests/bpf/prog_tests/bpf_insn_array.c      | 44 +++-------------------
 tools/testing/selftests/bpf/testing_helpers.c      | 32 ++++++++++++++++
 tools/testing/selftests/bpf/testing_helpers.h      |  1 +
 3 files changed, 38 insertions(+), 39 deletions(-)
tools/testing/selftests/bpf/prog_tests/bpf_insn_array.c +5 -39
index 0222a9a5d076..815f3e04540f 100644
@@ -227,42 +227,6 @@ static void check_incorrect_index(void)
 	check_mid_insn_index();
 }
 
-static int set_bpf_jit_harden(char *level)
-{
-	char old_level;
-	int err = -1;
-	int fd = -1;
-
-	fd = open("/proc/sys/net/core/bpf_jit_harden", O_RDWR | O_NONBLOCK);
-	if (fd < 0) {
-		ASSERT_FAIL("open .../bpf_jit_harden returned %d (errno=%d)", fd, errno);
-		return -1;
-	}
-
-	err = read(fd, &old_level, 1);
-	if (err != 1) {
-		ASSERT_FAIL("read from .../bpf_jit_harden returned %d (errno=%d)", err, errno);
-		err = -1;
-		goto end;
-	}
-
-	lseek(fd, 0, SEEK_SET);
-
-	err = write(fd, level, 1);
-	if (err != 1) {
-		ASSERT_FAIL("write to .../bpf_jit_harden returned %d (errno=%d)", err, errno);
-		err = -1;
-		goto end;
-	}
-
-	err = 0;
-	*level = old_level;
-end:
-	if (fd >= 0)
-		close(fd);
-	return err;
-}
-
 static void check_blindness(void)
 {
 	struct bpf_insn insns[] = {
@@ -272,7 +236,7 @@ static void check_blindness(void)
 		BPF_MOV64_IMM(BPF_REG_0, 1),
 		BPF_EXIT_INSN(),
 	};
-	int prog_fd = -1, map_fd;
+	int prog_fd = -1, map_fd, ret;
 	struct bpf_insn_array_value val = {};
 	char bpf_jit_harden = '@'; /* non-exizsting value */
 	int i;
@@ -291,7 +255,8 @@ static void check_blindness(void)
 		goto cleanup;
 
 	bpf_jit_harden = '2';
-	if (set_bpf_jit_harden(&bpf_jit_harden)) {
+	ret = set_bpf_jit_harden(&bpf_jit_harden);
+	if (!ASSERT_OK(ret, "set bpf_jit_harden")) {
 		bpf_jit_harden = '@'; /* open, read or write failed => no write was done */
 		goto cleanup;
 	}
@@ -313,7 +278,8 @@ static void check_blindness(void)
 cleanup:
 	/* restore the old one */
 	if (bpf_jit_harden != '@')
-		set_bpf_jit_harden(&bpf_jit_harden);
+		ASSERT_OK(set_bpf_jit_harden(&bpf_jit_harden),
+			  "restore hardening configuration");
 
 	close(prog_fd);
 	close(map_fd);
tools/testing/selftests/bpf/testing_helpers.c +32 -0
index c970e7793dfc..737f668b35e2 100644
@@ -519,6 +519,38 @@ bool is_jit_enabled(void)
 	return enabled;
 }
 
+int set_bpf_jit_harden(char *level)
+{
+	char old_level;
+	int err = -1;
+	int fd = -1;
+
+	fd = open("/proc/sys/net/core/bpf_jit_harden", O_RDWR | O_NONBLOCK);
+	if (fd < 0)
+		return -1;
+
+	err = read(fd, &old_level, 1);
+	if (err != 1) {
+		err = -1;
+		goto end;
+	}
+
+	lseek(fd, 0, SEEK_SET);
+
+	err = write(fd, level, 1);
+	if (err != 1) {
+		err = -1;
+		goto end;
+	}
+
+	err = 0;
+	*level = old_level;
+end:
+	if (fd >= 0)
+		close(fd);
+	return err;
+}
+
 int stack_mprotect(void)
 {
 	void *buf;
tools/testing/selftests/bpf/testing_helpers.h +1 -0
index 2edc6fb7fc52..e00642afe86f 100644
@@ -59,6 +59,7 @@ struct bpf_insn;
 int get_xlated_program(int fd_prog, struct bpf_insn **buf, __u32 *cnt);
 int testing_prog_flags(void);
 bool is_jit_enabled(void);
+int set_bpf_jit_harden(char *level);
 int stack_mprotect(void);
 
 #endif /* __TESTING_HELPERS_H */
Signature · 1 line
2.54.0
reviewer Acked-by on v5 9/10
#30 lore raw

Re: [PATCH bpf-next v5 09/10] selftests/bpf: move bpf_jit_harden helper into testing_helpers

Quoted 8 lines · Alexis Lothoré (eBPF Foundation)

Move set_bpf_jit_harden to testing helpers so that other selftests can change the hardening configuration without re-implementing a helper.

---
Changes in v5:
- check set_bpf_jit_harden return code in cleanup path

Quoted 7 lines

Changes in v3:
- new patch

---
 .../selftests/bpf/prog_tests/bpf_insn_array.c      | 44 +++-------------------
 tools/testing/selftests/bpf/testing_helpers.c      | 32 ++++++++++++++++
 tools/testing/selftests/bpf/testing_helpers.h      |  1 +
 3 files changed, 38 insertions(+), 39 deletions(-)

The move makes sense, thanks.

Quoted 133 lines
tools/testing/selftests/bpf/prog_tests/bpf_insn_array.c +5 -39
index 0222a9a5d076..815f3e04540f 100644
@@ -227,42 +227,6 @@ static void check_incorrect_index(void)
 	check_mid_insn_index();
 }
 
-static int set_bpf_jit_harden(char *level)
-{
-	char old_level;
-	int err = -1;
-	int fd = -1;
-
-	fd = open("/proc/sys/net/core/bpf_jit_harden", O_RDWR | O_NONBLOCK);
-	if (fd < 0) {
-		ASSERT_FAIL("open .../bpf_jit_harden returned %d (errno=%d)", fd, errno);
-		return -1;
-	}
-
-	err = read(fd, &old_level, 1);
-	if (err != 1) {
-		ASSERT_FAIL("read from .../bpf_jit_harden returned %d (errno=%d)", err, errno);
-		err = -1;
-		goto end;
-	}
-
-	lseek(fd, 0, SEEK_SET);
-
-	err = write(fd, level, 1);
-	if (err != 1) {
-		ASSERT_FAIL("write to .../bpf_jit_harden returned %d (errno=%d)", err, errno);
-		err = -1;
-		goto end;
-	}
-
-	err = 0;
-	*level = old_level;
-end:
-	if (fd >= 0)
-		close(fd);
-	return err;
-}
-
 static void check_blindness(void)
 {
 	struct bpf_insn insns[] = {
@@ -272,7 +236,7 @@ static void check_blindness(void)
 		BPF_MOV64_IMM(BPF_REG_0, 1),
 		BPF_EXIT_INSN(),
 	};
-	int prog_fd = -1, map_fd;
+	int prog_fd = -1, map_fd, ret;
 	struct bpf_insn_array_value val = {};
 	char bpf_jit_harden = '@'; /* non-exizsting value */
 	int i;
@@ -291,7 +255,8 @@ static void check_blindness(void)
 		goto cleanup;
 
 	bpf_jit_harden = '2';
-	if (set_bpf_jit_harden(&bpf_jit_harden)) {
+	ret = set_bpf_jit_harden(&bpf_jit_harden);
+	if (!ASSERT_OK(ret, "set bpf_jit_harden")) {
 		bpf_jit_harden = '@'; /* open, read or write failed => no write was done */
 		goto cleanup;
 	}
@@ -313,7 +278,8 @@ static void check_blindness(void)
 cleanup:
 	/* restore the old one */
 	if (bpf_jit_harden != '@')
-		set_bpf_jit_harden(&bpf_jit_harden);
+		ASSERT_OK(set_bpf_jit_harden(&bpf_jit_harden),
+			  "restore hardening configuration");
 
 	close(prog_fd);
 	close(map_fd);
tools/testing/selftests/bpf/testing_helpers.c +32 -0
index c970e7793dfc..737f668b35e2 100644
@@ -519,6 +519,38 @@ bool is_jit_enabled(void)
 	return enabled;
 }
 
+int set_bpf_jit_harden(char *level)
+{
+	char old_level;
+	int err = -1;
+	int fd = -1;
+
+	fd = open("/proc/sys/net/core/bpf_jit_harden", O_RDWR | O_NONBLOCK);
+	if (fd < 0)
+		return -1;
+
+	err = read(fd, &old_level, 1);
+	if (err != 1) {
+		err = -1;
+		goto end;
+	}
+
+	lseek(fd, 0, SEEK_SET);
+
+	err = write(fd, level, 1);
+	if (err != 1) {
+		err = -1;
+		goto end;
+	}
+
+	err = 0;
+	*level = old_level;
+end:
+	if (fd >= 0)
+		close(fd);
+	return err;
+}
+
 int stack_mprotect(void)
 {
 	void *buf;
tools/testing/selftests/bpf/testing_helpers.h +1 -0
index 2edc6fb7fc52..e00642afe86f 100644
@@ -59,6 +59,7 @@ struct bpf_insn;
 int get_xlated_program(int fd_prog, struct bpf_insn **buf, __u32 *cnt);
 int testing_prog_flags(void);
 bool is_jit_enabled(void);
+int set_bpf_jit_harden(char *level);
 int stack_mprotect(void);
 
 #endif /* __TESTING_HELPERS_H */
#31 lore raw

PATCH v5 10/10 selftests/bpf: add tests to validate KASAN on JIT programs

In reply to Alexis Lothoré (eBPF Foundation)

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

---
Changes in v5:
- check snprintf return codes when checking kernel logs
- skip tests in kasan_multi_shot is not enabled
- update simple atomic test expected access type (read -> write)

Changes in v4:
- prevent interleaved kernel logs from breaking kasan report search
- make new kfuncs in bpf_testmod depend on BPF_JIT_KASAN rather than KASAN_GENERIC
- initialize buffer passed to bpf_prog_test_run_opts

Changes in v3:
- increase kernel log buffer size
- fix comment style
- check bpf_program__fd return code
- document kasan_multi_shot
- fix copy-paste mistakes on poisoning/unpoisoning sequences
- add test for patch due to constant blinding

Changes in v2:
- simplify tests by just manually poisoning test areas with a dedicated kfunc
- introduce one prog per covered instruction family
- make sure that tests do not consume kernel logs (use /dev/kmgs rather than klogctl)
- add tests for stack accesses:
- marking correctly set when there are diverging verifier states leading to different memory types
- marking kept in sync with prog when it is patched

---
 tools/testing/selftests/bpf/prog_tests/kasan.c     | 430 +++++++++++++++++++++
 tools/testing/selftests/bpf/progs/kasan.c          | 394 +++++++++++++++++++
 tools/testing/selftests/bpf/progs/kasan_harden.c   |  41 ++
 .../testing/selftests/bpf/test_kmods/bpf_testmod.c |  22 ++
 4 files changed, 887 insertions(+)
tools/testing/selftests/bpf/prog_tests/kasan.c new file +430 -0
new file mode 100644
index 000000000000..2bc545e96259
@@ -0,0 +1,430 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
+
+/*
+ * Tests validating that KASAN reports are properly instrumented and
+ * generated on a wide variety of instructions. The running kernel needs
+ * kasan_multi_shot to run multiple kasan-generating subtests at once
+ */
+#include <bpf/bpf.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <linux/if_ether.h>
+#include <unistd.h>
+#include <test_progs.h>
+#include <unpriv_helpers.h>
+#include "kasan.skel.h"
+#include "kasan_harden.skel.h"
+
+#define SUBTEST_NAME_MAX_LEN	128
+#define PROG_NAME_MAX_LEN	128
+
+#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"
+
+static char klog_buffer[MAX_LOG_SIZE];
+static char record[MAX_LOG_SIZE];
+
+struct test_spec {
+	char *prog_type;
+	bool is_write;
+	bool only_32_or_64;
+	bool needs_load_acq_store_rel;
+	bool skip_multi_size_testing;
+	bool skip_on_stack_testing;
+	int run_size;
+	bool expect_no_report;
+	bool rnd_hi32;
+};
+
+struct kasan_write_val {
+	__u8 data_1;
+	__u16 data_2;
+	__u32 data_4;
+	__u64 data_8;
+};
+
+struct test_ctx {
+	__u8  prog_tag[BPF_TAG_SIZE];
+	struct bpf_object *obj;
+	int *access_size;
+	bool skip_load_acq_store_rel;
+	struct bpf_program *prog;
+	char prog_name[SUBTEST_NAME_MAX_LEN];
+	int klog_fd;
+};
+
+static int open_kernel_logs(void)
+{
+	int fd;
+
+	fd = open("/dev/kmsg", O_RDONLY | O_NONBLOCK);
+
+	return fd;
+}
+
+static void skip_kernel_logs(int fd)
+{
+	lseek(fd, 0, SEEK_END);
+}
+
+static int read_kernel_logs(int fd, char *buf, size_t max_len)
+{
+	size_t total = 0;
+	ssize_t n;
+
+	buf[0] = '\0';
+	while (1) {
+		char *msg, *eol;
+		size_t len;
+
+		n = read(fd, record, sizeof(record) - 1);
+		if (n < 0) {
+			if (errno == EAGAIN)
+				break;
+			return n;
+		}
+		record[n] = '\0';
+
+		/*
+		 * Each kmsg record starts with some metadata, separated
+		 * from the actual content by a semi-colon
+		 */
+		msg = strchr(record, ';');
+		if (!msg)
+			continue;
+		msg++;
+		eol = strchr(msg, '\n');
+		if (eol)
+			*eol = '\0';
+
+		len = strlen(msg);
+		if (total + len + 2 > max_len)
+			break;
+		memcpy(buf + total, msg, len);
+		total += len;
+		buf[total++] = '\n';
+		buf[total] = '\0';
+	}
+
+	return total;
+}
+
+static int check_kasan_report_in_kernel_logs(char *buf, struct test_ctx *ctx,
+					     bool is_write, int size)
+{
+	char access_log[READ_CHUNK_SIZE];
+	char *kasan_report_start;
+	int nsize;
+
+	nsize = snprintf(access_log, READ_CHUNK_SIZE, KASAN_PATTERN_SLAB_UAF,
+			 ctx->prog_tag[0], ctx->prog_tag[1], ctx->prog_tag[2],
+			 ctx->prog_tag[3], ctx->prog_tag[4], ctx->prog_tag[5],
+			 ctx->prog_tag[6], ctx->prog_tag[7], ctx->prog_name);
+	if (!ASSERT_GE(nsize, 0, "format kasan access header line"))
+		return nsize;
+	/*
+	 * Searched kasan report is valid if
+	 * - it contains the expected kasan pattern
+	 * - the description of the faulty access is found somewhere
+	 *   after the header (not necessarily on the very next line,
+	 *   because other kernel messages may interleave)
+	 * - faulty access properties match the tested type and size
+	 */
+	kasan_report_start = strstr(buf, access_log);
+
+	if (!kasan_report_start)
+		return 1;
+
+	nsize = snprintf(access_log, READ_CHUNK_SIZE, KASAN_PATTERN_REPORT,
+			 is_write ? "Write" : "Read", size);
+	if (!ASSERT_GE(nsize, 0, "format kasan access report line"))
+		return nsize;
+
+	if (!strstr(kasan_report_start, access_log))
+		return 1;
+
+	return 0;
+}
+
+static void exec_subtest(struct test_ctx *ctx, struct test_spec *test,
+			 int access_size, bool on_stack)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, topts);
+	struct bpf_prog_info info;
+	uint8_t buf[ETH_HLEN] = {0};
+	int ret, prog_fd;
+	__u32 info_len;
+
+	ctx->prog = bpf_object__find_program_by_name(ctx->obj,
+						     ctx->prog_name);
+	if (!ASSERT_OK_PTR(ctx->prog, "find test prog"))
+		return;
+
+	info_len = sizeof(info);
+	memset(&info, 0, info_len);
+	prog_fd = bpf_program__fd(ctx->prog);
+	if (!ASSERT_OK_FD(prog_fd, "get prog fd"))
+		return;
+	ret = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len);
+	if (!ASSERT_OK(ret, "fetch loaded program info"))
+		return;
+	memcpy(ctx->prog_tag, info.tag, BPF_TAG_SIZE);
+
+	skip_kernel_logs(ctx->klog_fd);
+
+	topts.sz = sizeof(struct bpf_test_run_opts);
+	topts.data_size_in = ETH_HLEN;
+	topts.data_in = buf;
+	if (ctx->access_size)
+		*ctx->access_size = access_size;
+	ret = bpf_prog_test_run_opts(bpf_program__fd(ctx->prog),
+				     &topts);
+	if (!ASSERT_OK(ret, "run prog"))
+		return;
+
+	ret = read_kernel_logs(ctx->klog_fd, klog_buffer, MAX_LOG_SIZE);
+	if (!ASSERT_GE(ret, 0, "read kernel logs"))
+		return;
+
+	ret = check_kasan_report_in_kernel_logs(klog_buffer, ctx,
+						test->is_write, access_size);
+	if (on_stack || test->expect_no_report)
+		ASSERT_NEQ(ret, 0, "no report should be generated");
+	else
+		ASSERT_OK(ret, "report should be generated");
+}
+
+static void run_subtest_with_size_and_location(struct test_ctx *ctx,
+					       struct test_spec *test,
+					       int access_size,
+					       bool on_stack)
+{
+	char subtest_name[SUBTEST_NAME_MAX_LEN];
+
+	if (test->skip_multi_size_testing) {
+		snprintf(subtest_name, SUBTEST_NAME_MAX_LEN, "%s",
+			 test->prog_type);
+		strncpy(ctx->prog_name, test->prog_type, PROG_NAME_MAX_LEN);
+	} else {
+		snprintf(subtest_name, SUBTEST_NAME_MAX_LEN, "%s_%d_%s",
+			 test->prog_type, access_size,
+			 on_stack ? "on_stack" : "not_on_stack");
+		snprintf(ctx->prog_name, PROG_NAME_MAX_LEN, "%s_%s",
+			 test->prog_type,
+			 on_stack ? "on_stack" : "not_on_stack");
+	}
+
+	if (!test__start_subtest(subtest_name))
+		return;
+
+	if (test->needs_load_acq_store_rel && ctx->skip_load_acq_store_rel) {
+		test__skip();
+		return;
+	}
+
+	exec_subtest(ctx, test, access_size, on_stack);
+}
+
+static void run_subtest_with_size(struct test_ctx *ctx, struct test_spec *test,
+				  int size)
+{
+	run_subtest_with_size_and_location(ctx, test, size, false);
+	if (!test->skip_on_stack_testing)
+		run_subtest_with_size_and_location(ctx, test, size, true);
+}
+
+static void run_subtest(struct test_ctx *ctx, struct test_spec *test)
+{
+	if (test->skip_multi_size_testing) {
+		run_subtest_with_size(ctx, test, test->run_size);
+		return;
+	}
+
+	if (!test->only_32_or_64) {
+		run_subtest_with_size(ctx, test, 1);
+		run_subtest_with_size(ctx, test, 2);
+	}
+	run_subtest_with_size(ctx, test, 4);
+	run_subtest_with_size(ctx, test, 8);
+}
+
+static void run_blinding_subtest(void)
+{
+	struct test_spec blinding_spec = {
+		.prog_type = "st_blinded",
+		.is_write = true,
+	};
+	char bpf_jit_harden = '2';
+	struct kasan_harden *skel;
+	struct test_ctx *ctx;
+
+	if (!test__start_subtest("st_blinded"))
+		return;
+
+	ctx = calloc(1, sizeof(*ctx));
+	if (!ASSERT_OK_PTR(ctx, "alloc blinding ctx"))
+		return;
+	ctx->klog_fd = -1;
+
+	if (set_bpf_jit_harden(&bpf_jit_harden))
+		goto free_ctx;
+
+	skel = kasan_harden__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open and load blinded prog"))
+		goto restore;
+
+	ctx->klog_fd = open_kernel_logs();
+	if (!ASSERT_OK_FD(ctx->klog_fd, "open kernel logs"))
+		goto destroy;
+
+	ctx->obj = skel->obj;
+	strncpy(ctx->prog_name, "st_blinded", PROG_NAME_MAX_LEN);
+
+	exec_subtest(ctx, &blinding_spec, 1, false);
+
+destroy:
+	close(ctx->klog_fd);
+	kasan_harden__destroy(skel);
+restore:
+	set_bpf_jit_harden(&bpf_jit_harden);
+free_ctx:
+	free(ctx);
+}
+
+static struct test_spec tests[] = {
+	{
+		.prog_type = "st",
+		.is_write = true
+	},
+	{
+		.prog_type = "stx",
+		.is_write = true
+	},
+	{
+		.prog_type = "ldx",
+		.is_write = false
+	},
+	{
+		.prog_type = "simple_atomic",
+		.is_write = true,
+		.only_32_or_64 = true
+	},
+	{
+		.prog_type = "load_acquire",
+		.is_write = false,
+		.needs_load_acq_store_rel = true
+	},
+	{
+		.prog_type = "store_release",
+		.is_write = true,
+		.needs_load_acq_store_rel = true
+	},
+	{
+		.prog_type = "ldx_patched",
+		.is_write = false,
+		.skip_multi_size_testing = true,
+		.skip_on_stack_testing = true,
+		.run_size = 4,
+		.rnd_hi32 = true
+	},
+	{
+		.prog_type = "ldx_patched_on_stack",
+		.is_write = false,
+		.skip_multi_size_testing = true,
+		.skip_on_stack_testing = true,
+		.run_size = 4,
+		.expect_no_report = true,
+		.rnd_hi32 = true
+	},
+	{
+		.prog_type = "verifier_paths_stack_and_non_stack",
+		.is_write = true,
+		.skip_multi_size_testing = true,
+		.skip_on_stack_testing = true,
+		.run_size = 1
+	}
+};
+
+void test_kasan(void)
+{
+	struct kasan_write_val val;
+	struct test_spec *test;
+	struct test_ctx *ctx;
+	struct kasan *skel;
+	__u32 key = 0;
+	int i, ret;
+
+	ctx = calloc(1, sizeof(struct test_ctx));
+	if (!ASSERT_OK_PTR(ctx, "alloc test ctx"))
+		return;
+
+	if (!is_jit_enabled() || !get_kasan_jit_enabled() ||
+	    !get_kasan_multi_shot_enabled()) {
+		test__skip();
+		goto end;
+	}
+
+	skel = kasan__open();
+	if (!ASSERT_OK_PTR(skel, "open prog"))
+		goto end;
+
+	for (i = 0; i < ARRAY_SIZE(tests); i++) {
+		struct bpf_program *prog;
+
+		if (!tests[i].rnd_hi32)
+			continue;
+
+		prog = bpf_object__find_program_by_name(skel->obj,
+							tests[i].prog_type);
+		if (!ASSERT_OK_PTR(prog, "find rnd_hi32 prog"))
+			goto destroy;
+		bpf_program__set_flags(prog, BPF_F_TEST_RND_HI32);
+	}
+
+	if (!ASSERT_OK(kasan__load(skel), "load prog"))
+		goto destroy;
+
+	ctx->obj = skel->obj;
+	ctx->access_size = &skel->bss->access_size;
+	ctx->skip_load_acq_store_rel = skel->data->skip_load_acq_store_rel_tests;
+
+	ctx->klog_fd = open_kernel_logs();
+	if (!ASSERT_OK_FD(ctx->klog_fd, "open kernel logs"))
+		goto destroy;
+
+	/* Fill map with recognizable values */
+	ret = bpf_map__lookup_elem(skel->maps.test_map, &key, sizeof(key),
+				   &val, sizeof(val), 0);
+	if (!ASSERT_OK(ret, "get map"))
+		goto close;
+	val.data_1 = 0xAA;
+	val.data_2 = 0xBBBB;
+	val.data_4 = 0xCCCCCCCC;
+	val.data_8 = 0xDDDDDDDDDDDDDDDD;
+	ret = bpf_map__update_elem(skel->maps.test_map, &key, sizeof(key),
+				   &val, sizeof(val), 0);
+	if (!ASSERT_OK(ret, "set map"))
+		goto close;
+
+	for (i = 0; i < ARRAY_SIZE(tests); i++) {
+		test = &tests[i];
+		run_subtest(ctx, test);
+	}
+
+	/*
+	 * Blinding subtest is handled differently as it needs the
+	 * corresponding program to be loaded with bpf_jit_harden raised
+	 */
+	run_blinding_subtest();
+
+close:
+	close(ctx->klog_fd);
+destroy:
+	kasan__destroy(skel);
+end:
+	free(ctx);
+}
tools/testing/selftests/bpf/progs/kasan.c new file +394 -0

394 lines not on this page · raw message

tools/testing/selftests/bpf/progs/kasan_harden.c new file +41 -0

41 lines not on this page · raw message

tools/testing/selftests/bpf/test_kmods/bpf_testmod.c +22 -0
index 30f1cd23093c..5d6a834f7d75 100644
@@ -271,6 +271,26 @@ __bpf_kfunc void bpf_kfunc_put_default_trusted_ptr_test(struct prog_test_member
 	 */
 }
 
+#ifdef CONFIG_BPF_JIT_KASAN
+
+extern void kasan_poison(const void *addr, size_t size, u8 value, bool init);
+
+#define KASAN_SLAB_FREE 0xFB
+
+__bpf_kfunc void bpf_kfunc_kasan_poison(void *mem, u32 mem__sz)
+{
+	kasan_poison(mem, mem__sz, KASAN_SLAB_FREE, false);
+}
+
+__bpf_kfunc void bpf_kfunc_kasan_unpoison(void *mem, u32 mem__sz)
+{
+	kasan_poison(mem, mem__sz, 0x00, false);
+}
+#else
+__bpf_kfunc void bpf_kfunc_kasan_poison(void *mem, u32 mem__sz) { }
+__bpf_kfunc void bpf_kfunc_kasan_unpoison(void *mem, u32 mem__sz) { }
+#endif
+
 __bpf_kfunc struct bpf_testmod_ctx *
 bpf_testmod_ctx_create(int *err)
 {
@@ -740,6 +760,8 @@ BTF_ID_FLAGS(func, bpf_testmod_ops3_call_test_1)
 BTF_ID_FLAGS(func, bpf_testmod_ops3_call_test_2)
 BTF_ID_FLAGS(func, bpf_kfunc_get_default_trusted_ptr_test);
 BTF_ID_FLAGS(func, bpf_kfunc_put_default_trusted_ptr_test);
+BTF_ID_FLAGS(func, bpf_kfunc_kasan_poison)
+BTF_ID_FLAGS(func, bpf_kfunc_kasan_unpoison)
 BTF_KFUNCS_END(bpf_testmod_common_kfunc_ids)
 
 BTF_ID_LIST(bpf_testmod_dtor_ids)
Signature · 1 line
2.54.0
#32 lore raw

Re: [PATCH bpf-next v5 10/10] selftests/bpf: add tests to validate KASAN on JIT programs

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Infinite loop in `read_kernel_logs` if `read()` returns 0. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Infinite loop in read_kernel_logs if read() returns 0.
--

--- Patch [10]: [PATCH bpf-next v5 10/10] selftests/bpf: add tests to validate KASAN on JIT programs --- commit f689181d893dcf7bbac97d28845dc342a1fe71e5
Author: Alexis Lothoré (eBPF Foundation) <[email protected]>

selftests/bpf: add tests to validate KASAN on JIT programs

This commit adds a basic KASAN test runner that loads and runs BPF programs designed to trigger memory management bugs. The tests capture kernel logs to verify that the expected KASAN splats are emitted, ensuring the JIT compiler has correctly inserted KASAN instrumentation.

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/kasan.c new file +0 -0
new file mode 100644
index 0000000000000..2bc545e962595
@@ -0,0 +1,430 @@

[ ... ]

Quoted 15 lines
tools/testing/selftests/bpf/prog_tests/kasan.c new file +15 -0
+static int read_kernel_logs(int fd, char *buf, size_t max_len)
+{
+	size_t total = 0;
+	ssize_t n;
+
+	buf[0] = '\0';
+	while (1) {
+		char *msg, *eol;
+		size_t len;
+
+		n = read(fd, record, sizeof(record) - 1);
+		if (n < 0) {
+			if (errno == EAGAIN)
+				break;
+			return n;

tools/testing/selftests/bpf/prog_tests/kasan.c line 98

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/kasan.c new file +10 -0
+		}
+		record[n] = '\0';
+
+		/*
+		 * Each kmsg record starts with some metadata, separated
+		 * from the actual content by a semi-colon
+		 */
+		msg = strchr(record, ';');
+		if (!msg)
+			continue;

[Severity: Low]
Will this loop indefinitely if read() returns 0?

If n == 0, record is null-terminated at index 0, making strchr() return NULL. The continue statement then restarts the loop, causing read() to be called again. While /dev/kmsg typically returns -EAGAIN when no data is available, should an unexpected 0 return from read() be explicitly handled to prevent a potential infinite loop?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
reviewer
#33 lore raw

Re: [PATCH bpf-next v5 10/10] selftests/bpf: add tests to validate KASAN on JIT programs

In reply to Alexis Lothoré (eBPF Foundation)
Quoted 64 lines · Alexis Lothoré (eBPF Foundation)

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
Quoted 8 lines
  #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!

tools/testing/selftests/bpf/prog_tests/kasan.c line 26

Quoted 11 lines

---
[...]

tools/testing/selftests/bpf/prog_tests/kasan.c new file +7 -0
+
+#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.

Quoted 4 lines

[...]

tools/testing/selftests/bpf/progs/kasan.c new file +3 -0
+
+SEC("tcx/ingress")
+int simple_atomic_on_stack(struct __sk_buff *skb)

tools/testing/selftests/bpf/progs/kasan.c line 227

Quoted 10 lines
tools/testing/selftests/bpf/progs/kasan.c new file +10 -0
+{
+	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?

Maybe it's worth adding a test with __sync_val_compare_and_swap() into a poisoned map value, or something of the sort?

Quoted 31 lines
tools/testing/selftests/bpf/progs/kasan.c new file +29 -0
+		break;
+	}
+	bpf_kfunc_kasan_unpoison(&val, sizeof(struct kasan_write_val));
+	return 0;
+}
+
+SEC("tcx/ingress")
+int simple_atomic_not_on_stack(struct __sk_buff *skb)
+{
+	struct kasan_write_val *val;
+	__u32 key = 0;
+
+	val = bpf_map_lookup_elem(&test_map, &key);
+	if (!val)
+		return 0;
+
+	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);
+		break;
+	}
+	bpf_kfunc_kasan_unpoison(val, sizeof(struct kasan_write_val));
+	return 0;
+}
+
[...]
#34 lore raw

Re: [PATCH bpf-next v5 10/10] selftests/bpf: add tests to validate KASAN on JIT programs

Quoted 77 lines · Ihor Solodrai
Quoted 64 lines · Alexis Lothoré (eBPF Foundation)

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
Quoted 8 lines
  #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!

Quoted 1 line
Quoted 13 lines

tools/testing/selftests/bpf/prog_tests/kasan.c line 26

Quoted 10 lines

---
[...]

tools/testing/selftests/bpf/prog_tests/kasan.c new file +7 -0
+
+#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.

Quoted 10 lines
Quoted 10 lines

[...]

tools/testing/selftests/bpf/progs/kasan.c new file +9 -0
+
+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:
Quoted 8 lines

tools/testing/selftests/bpf/progs/kasan.c line 227

Quoted 4 lines
tools/testing/selftests/bpf/progs/kasan.c new file +4 -0
+		__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.

Quoted 4 lines

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

Signature · 3 lines
Alexis Lothoré, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
maintainer
#35 lore raw
In reply to Alexis Lothoré (eBPF Foundation)
Quoted 3 lines · Alexis Lothoré (eBPF Foundation)

Hello,
this is v5 of the series aiming to bring basic support for KASAN checks to BPF JITed programs.

Can we slow down a bit here please?

From: Documentation/process/submitting-patches.rst

Don't get discouraged - or impatient
------------------------------------

After you have submitted your change, be patient and wait. Reviewers are busy people and may not get to your patch right away.

Once upon a time, patches used to disappear into the void without comment, but the development process works more smoothly than that now. You should receive comments within a week or so; if that does not happen, make sure that you have sent your patches to the right place. Wait for a minimum of

						     ^^^^^^^^^^^^^^^^^^^^^

one week before resubmitting or pinging reviewers - possibly longer during ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

busy times like merge windows.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#36 lore raw
Quoted 6 lines · Borislav Petkov
Quoted 3 lines · Alexis Lothoré (eBPF Foundation)

Hello,
this is v5 of the series aiming to bring basic support for KASAN checks to BPF JITed programs.

Can we slow down a bit here please?

Yes, sure, my bad for the noise. I may have been a bit over-enthusiastic here, while iterating on the many issues raised by Sashiko.

Alexis

Signature · 3 lines
Alexis Lothoré, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
reviewer
#37 lore raw
In reply to Alexis Lothoré (eBPF Foundation)
Quoted 5 lines · Alexis Lothoré (eBPF Foundation)

Hello,
this is v5 of the series aiming to bring basic support for KASAN checks to BPF JITed programs.

[...]

Hi Alexis,

I started reviewing this revision today, but didn't get through everything. Sending a few comments in a bit, will continue tomorrow.

Thank you for working on it.

Quoted 40 lines

---
Alexis Lothoré (eBPF Foundation) (10):
bpf: propagate original instruction offset when patching program
bpf: mark instructions accessing program stack
bpf: add BPF_JIT_KASAN for KASAN instrumentation of JITed programs bpf, x86: add helper to emit kasan checks in x86 JITed programs bpf, x86: refactor BPF_ST management in do_jit
bpf, x86: emit KASAN checks into x86 JITed programs bpf, x86: enable KASAN for JITed programs on x86 selftests/bpf: add helpers for KASAN in JIT testing selftests/bpf: move bpf_jit_harden helper into testing_helpers selftests/bpf: add tests to validate KASAN on JIT programs

 arch/x86/Kconfig                                   |   1 +
 arch/x86/net/bpf_jit_comp.c                        | 271 ++++++++++---
 include/linux/bpf_verifier.h                       |   2 +
 include/linux/filter.h                             |  10 +-
 kernel/bpf/Kconfig                                 |  17 +
 kernel/bpf/core.c                                  |   2 +-
 kernel/bpf/fixups.c                                | 132 +++++--
 kernel/bpf/verifier.c                              |   9 +
 .../selftests/bpf/prog_tests/bpf_insn_array.c      |  44 +--
 tools/testing/selftests/bpf/prog_tests/kasan.c     | 430 +++++++++++++++++++++
 tools/testing/selftests/bpf/progs/kasan.c          | 394 +++++++++++++++++++
 tools/testing/selftests/bpf/progs/kasan_harden.c   |  41 ++
 .../testing/selftests/bpf/test_kmods/bpf_testmod.c |  22 ++
 tools/testing/selftests/bpf/testing_helpers.c      |  32 ++
 tools/testing/selftests/bpf/testing_helpers.h      |   1 +
 tools/testing/selftests/bpf/unpriv_helpers.c       |  10 +
 tools/testing/selftests/bpf/unpriv_helpers.h       |   2 +
 17 files changed, 1288 insertions(+), 132 deletions(-)

---
base-commit: 950d646b505ae37ee75f4c2b11d41e7190036c42
change-id: 20260126-kasan-fcd68f64cd7b

Best regards,

Signature · 1 line
Alexis Lothoré (eBPF Foundation) <[email protected]>