KCSAN: data-race in bcmp / htab_lru_map_update_elem

Status: moderation: reported on 2026/11/24 01:11
Subsystems: bpf
Labels: race:harmful prio:low
[Documentation on labels]
Reported-by: syzbot+56ef68f94473b6af7eb4@syzkaller.appspotmail.com
First crash: 1h05m, last: 1h05m
✨ AI-generated root cause summary:

Lockless RCU lookups in BPF_MAP_TYPE_LRU_HASH maps (__lookup_elem_raw()) traverse hlist_nulls buckets and compare l->key via memcmp() without waiting for an RCU grace period when elements are recycled. Concurrently, htab_lru_map_update_elem() -> prealloc_lru_pop() evicts/pops an element l via bpf_lru_pop_free() (which unlinks it with hlist_nulls_del_rcu() and immediately writes the new hash) and overwrites l->elem.key with memcpy(). An RCU reader that already loaded l and observes htab_elem_hash(l) == hash concurrently executes memcmp(&l->key, key, key_size) against the in-progress memcpy(), triggering a KCSAN data race and allowing torn key comparisons.

▶ ▼ ✨ Full AI root cause analysis
Based on kernel commit b6a9fa34ce41:

Root Cause Analysis

The KCSAN data race occurs between concurrent lockless RCU lookup (__lookup_elem_raw() in kernel/bpf/hashtab.c) and LRU element recycling/initialization (prealloc_lru_pop() in kernel/bpf/hashtab.c) on the key buffer of a preallocated struct htab_elem_lru (htab->elems) in a BPF_MAP_TYPE_LRU_HASH map.

Involved Functions, Structs, and Fields

  • Structs: struct bpf_htab, struct htab_elem_lru, struct htab_elem_hashed, struct htab_elem (kernel/bpf/hashtab.c), and struct bpf_lru_node (kernel/bpf/bpf_lru_list.h).
  • Fields: htab_elem::key (280 bytes in the report: writer writes 280 bytes at 0xffff88811ee5cda0, reader reads 8 bytes at 0xffff88811ee5ce88, offset 0xe8 = 232), htab_elem_hashed::hash, htab_elem::hash_node, and bpf_htab::has_hash (always true for LRU maps per htab_map_alloc()).
  • Reader path: map_lookup_elem() -> bpf_map_copy_value() (under rcu_read_lock(), without bucket spinlock) -> htab_lru_map_lookup_elem_sys() -> __htab_lru_map_lookup_elem() -> __htab_map_lookup_elem() -> __htab_lookup() -> lookup_nulls_elem_raw() -> __lookup_elem_raw() (kernel/bpf/hashtab.c:827), which calls memcmp(&l->key, key, key_size) -> bcmp() (lib/string.c:697).
  • Writer path: generic_map_update_batch() -> bpf_map_update_value() -> htab_lru_map_update_elem() -> prealloc_lru_pop() (kernel/bpf/hashtab.c:407), which pops a struct bpf_lru_node via bpf_lru_pop_free() (kernel/bpf/bpf_lru_list.c) and writes memcpy(l->elem.key, key, htab->map.key_size).

Mechanism & Violated Invariant

  1. Immediate element reuse without an RCU grace period: Preallocated BPF LRU hash maps (htab_is_lru(htab)) recycle struct htab_elem_lru entries from htab->elems immediately upon deletion/eviction without waiting for an RCU grace period (synchronize_rcu()). During eviction, htab_lru_map_delete_node() (kernel/bpf/hashtab.c:1039) acquires the old bucket lock and calls hlist_nulls_del_rcu(&l->hash_node), which unlinks l from the bucket list while preserving l->hash_node.next so concurrent hlist_nulls RCU readers can continue traversing the list.
  2. Concurrent key overwrite during RCU traversal: A reader on CPU 1 (__lookup_elem_raw(), kernel/bpf/hashtab.c:824-828) walks the bucket with hlist_nulls_for_each_entry_rcu(l, n, head, hash_node) and checks:
    if (htab_elem_hash(l) == hash && !memcmp(&l->key, key, key_size))
    
    While CPU 1 holds a reference to l from its RCU traversal (before or after checking htab_elem_hash(l)):
    • CPU 0 calls prealloc_lru_pop() -> bpf_lru_pop_free(&htab->lru, hash).
    • bpf_lru_pop_free() (via bpf_common_lru_pop_free() -> __local_list_pop_pending() or bpf_lru_list_pop_free_to_local() -> __bpf_lru_list_shrink(), or bpf_percpu_lru_pop_free()) evicts l using htab_lru_map_delete_node(), and immediately writes the new hash into l->hash (*(u32 *)((void *)node + lru->hash_offset) = hash in __local_list_add_pending() / bpf_percpu_lru_pop_free()).
    • prealloc_lru_pop() (kernel/bpf/hashtab.c:407) then immediately executes memcpy(l->elem.key, key, htab->map.key_size) before htab_lru_map_update_elem() links l into the target bucket.
  3. Why memcmp races with memcpy: If l's previous hash matched the reader's hash (e.g., same key or hash collision), or if bpf_lru_pop_free() already overwrote l->hash with a new hash matching the reader's hash, CPU 1 passes htab_elem_hash(l) == hash and invokes memcmp(&l->key, key, key_size) concurrently with CPU 0's memcpy(l->elem.key, key, htab->map.key_size). Because elem_has_hash is true (both because htab_is_lru(htab) and because key_size == 280 > sizeof(unsigned long)), neither side uses atomic/ordered access or data_race() annotations, and l->key is not protected by an RCU grace period or sequence/generation check. Consequently, KCSAN flags the data race, and in principle a reader can perform a torn comparison across the 280-byte key while memcpy() is partially overwriting it (potentially causing a false-positive match in lookup_nulls_elem_raw()). Note that the same lockless reuse vs. memcmp()/memcpy() pattern also exists in alloc_htab_elem() (kernel/bpf/hashtab.c:1315) for non-LRU preallocated hash maps when htab_has_hash(htab) is true.
▶ ▼ ✨ AI Jobs (2)
ID Workflow Result Correct Bug Created Started Finished Revision
8e424952 assessment-security DenialOfService: ❌ Exploitable: ❌ FilesystemTrigger: ❌ FuzzingArtifact: ❌ MemoryCorruption: ❌ NetworkNeedsSetup: ❌ NetworkTrigger: ❌ PeripheralTrigger: ❌ RemoteTrigger: ❌ Unprivileged: ❌ UserNamespace: ❌ VMGuestTrigger: ❌ VMHostTrigger: ❌ ❓ KCSAN: data-race in bcmp / htab_lru_map_update_elem 2026/11/24 01:04 2026/11/24 01:04 2026/11/24 01:09 fab64d89
5d8399d9 assessment-kcsan Benign: ❌ ❓ KCSAN: data-race in bcmp / htab_lru_map_update_elem 2026/11/24 00:58 2026/11/24 00:58 2026/11/24 01:04 fab64d89

Sample crash report:
==================================================================
BUG: KCSAN: data-race in bcmp / htab_lru_map_update_elem

write to 0xffff88811ee5cda0 of 280 bytes by task 12413 on cpu 0:
 prealloc_lru_pop kernel/bpf/hashtab.c:407 [inline]
 htab_lru_map_update_elem+0x223/0x7d0 kernel/bpf/hashtab.c:1516
 bpf_map_update_value+0x4f7/0x570 kernel/bpf/syscall.c:297
 generic_map_update_batch+0x4e6/0x700 kernel/bpf/syscall.c:2125
 bpf_map_do_batch+0x26a/0x3b0 kernel/bpf/syscall.c:5849
 __sys_bpf+0x784/0xc50 kernel/bpf/syscall.c:-1
 __do_sys_bpf kernel/bpf/syscall.c:6582 [inline]
 __se_sys_bpf kernel/bpf/syscall.c:6579 [inline]
 __x64_sys_bpf+0x69/0x80 kernel/bpf/syscall.c:6579
 x64_sys_call+0xf26/0x2550 arch/x86/include/generated/asm/syscalls_64.h:322
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x11e/0x3a0 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

read to 0xffff88811ee5ce88 of 8 bytes by task 12419 on cpu 1:
 memcmp lib/string.c:665 [inline]
 bcmp+0x23/0x90 lib/string.c:697
 memcmp include/linux/fortify-string.h:675 [inline]
 __lookup_elem_raw kernel/bpf/hashtab.c:827 [inline]
 lookup_nulls_elem_raw kernel/bpf/hashtab.c:865 [inline]
 __htab_lookup kernel/bpf/hashtab.c:889 [inline]
 __htab_map_lookup_elem+0x246/0x390 kernel/bpf/hashtab.c:906
 __htab_lru_map_lookup_elem kernel/bpf/hashtab.c:973 [inline]
 htab_lru_map_lookup_elem_sys+0x1d/0x60 kernel/bpf/hashtab.c:991
 bpf_map_copy_value+0x442/0x500 kernel/bpf/syscall.c:-1
 map_lookup_elem+0x511/0x700 kernel/bpf/syscall.c:1847
 __sys_bpf+0x683/0xc50 kernel/bpf/syscall.c:6440
 __do_sys_bpf kernel/bpf/syscall.c:6582 [inline]
 __se_sys_bpf kernel/bpf/syscall.c:6579 [inline]
 __x64_sys_bpf+0x69/0x80 kernel/bpf/syscall.c:6579
 x64_sys_call+0xf26/0x2550 arch/x86/include/generated/asm/syscalls_64.h:322
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x11e/0x3a0 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Reported by Kernel Concurrency Sanitizer on:
CPU: 1 UID: 0 PID: 12419 Comm: syz.0.3950 Not tainted syzkaller #0 PREEMPT(lazy) 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
==================================================================

Crashes (1):
Time Kernel Commit Syzkaller Config Log Report Syz repro C repro VM info Assets (help?) Manager Title
2026/11/24 00:52 upstream b6a9fa34ce41 fab64d89 .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in bcmp / htab_lru_map_update_elem
* Struck through repros no longer work on HEAD.