syzbot
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: 58m, last: 58m
✨ 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
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
- 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.
- 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.
- 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.
Sample crash report:
* Struck through repros no longer work on HEAD.