All of lore.kernel.org
 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

* Re: [PATCH bpf] bpf: Order map writecnt against frozen for read-only maps
  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
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-05 21:03 UTC (permalink / raw)
  To: Zhenwen Liang, bpf

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] bpf: inaccurate commit message claim that RWC race needs both
  reorderings
--

commit ac10bb0a7f54106e48572a1161df774277051ed1
Author: Zhenwen Liang <zhenwenliang@google.com>

bpf: Order map writecnt against frozen for read-only maps

This commit adds smp_mb__after_atomic() to bpf_map_write_active_inc() and
smp_mb() to bpf_map_is_rdonly() to order accesses to map->writecnt and
map->frozen on weakly ordered architectures.

Link: https://lore.kernel.org/all/20260707195600.5C2851F000E9@smtp.kernel.org/ [1]

> 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.

[Severity: Low]
Is it accurate that this race requires both reorderings at once?

With three CPUs (updater, freezer, verifier), either reordering alone lets
the updater see frozen == false while the verifier sees frozen == true and
writecnt == 0.  For example, if only bpf_map_is_rdonly() reorders its
reads while the updater executes in program order:

CPU0 (updater)           CPU1 (freezer)     CPU2 (verifier)
                                            read writecnt == 0 (reordered)
writecnt = 1
smp_mb__after_atomic()
read frozen == false
                         frozen = true
                                            read frozen == true

Symmetrically, if only the updater reorders reading frozen before
incrementing writecnt, the same outcome occurs even with smp_mb() in
bpf_map_is_rdonly().

Because either reordering alone triggers the race, both barriers are
strictly needed; saying both reorderings are required could mislead
readers into thinking a single barrier on the updater side is enough for
other readers such as bpf_prog_verify_signature().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205204758.954959-1-zhenwenliang@google.com?part=1

^ permalink raw reply	[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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.