BPF List
 help / color / mirror / Atom feed
* [PATCH bpf] bpf: Order map writecnt against frozen for read-only maps
@ 2026-12-05 20:47 Zhenwen Liang
  2026-12-05 21:03 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Zhenwen Liang @ 2026-12-05 20:47 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
	Eduard Zingerman, Kumar Kartikeya Dwivedi, John Fastabend,
	Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
	Emil Tsalapatis, Ihor Solodrai

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 <zhenwenliang@google.com>
---
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,

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-05 21:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 20:47 [PATCH bpf] bpf: Order map writecnt against frozen for read-only maps Zhenwen Liang
2026-12-05 21:03 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox