Updaters increment map->writecnt and then check map->frozen, and bpf_map_is_rdonly() reads frozen and then writecnt. Neither pair is ordered, so on weakly ordered CPUs (not x86) the verifier can see the map frozen with no write active while an update that passed its frozen check is stalled, as described in the Fixes: commit, and still writes. That brings back the issue fixed there, where the known scalar "is not guaranteed to be exactly the same at a later point when the program is executed". It needs both reorderings at once and was not reproduced. The update side was reported by sashiko-bot [1] in the review of b707068e0ed9. Add smp_mb__after_atomic() after the increment and smp_mb() between the two reads. The problem was found, and this patch prepared, with the help of a formal analysis tool we are developing that uses AI assistance; I have reviewed the analysis and the patch. Link: https://lore.kernel.org/all/20260707195600.5C2851F000E9@smtp.kernel.org/ [1] Fixes: 353050be4c19 ("bpf: Fix toctou on read-only map's constant scalar tracking") Signed-off-by: Zhenwen Liang --- Compile-tested only (arm64, W=1). The signed-loader check added by b707068e0ed9 reads frozen and then writecnt in the same way; it would need the same smp_mb() between the two reads. It is not changed here because I have not analysed it. kernel/bpf/syscall.c | 2 ++ kernel/bpf/verifier.c | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 91d078b..a23685a 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -122,6 +122,8 @@ const struct bpf_map_ops bpf_map_offload_ops = { static void bpf_map_write_active_inc(struct bpf_map *map) { atomic64_inc(&map->writecnt); + /* Pairs with smp_mb() in bpf_map_is_rdonly() */ + smp_mb__after_atomic(); } static void bpf_map_write_active_dec(struct bpf_map *map) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 85fec1f..0202479 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -6363,9 +6363,11 @@ bool bpf_map_is_rdonly(const struct bpf_map *map) * side have been completed. Only after that point, it's safe to * assume that map value(s) are immutable. */ - return (map->map_flags & BPF_F_RDONLY_PROG) && - READ_ONCE(map->frozen) && - !bpf_map_write_active(map); + if (!(map->map_flags & BPF_F_RDONLY_PROG) || !READ_ONCE(map->frozen)) + return false; + /* Pairs with smp_mb__after_atomic() in bpf_map_write_active_inc() */ + smp_mb(); + return !bpf_map_write_active(map); } int bpf_map_direct_read(struct bpf_map *map, int off, int size, u64 *val,