Skip to content

Commit 9d6d7f1

Browse files
edumazetkuba-moo
authored andcommitted
af_unix: annote lockless accesses to unix_tot_inflight & gc_in_progress
wait_for_unix_gc() reads unix_tot_inflight & gc_in_progress without synchronization. Adds READ_ONCE()/WRITE_ONCE() and their associated comments to better document the intent. BUG: KCSAN: data-race in unix_inflight / wait_for_unix_gc write to 0xffffffff86e2b7c0 of 4 bytes by task 9380 on cpu 0: unix_inflight+0x1e8/0x260 net/unix/scm.c:63 unix_attach_fds+0x10c/0x1e0 net/unix/scm.c:121 unix_scm_to_skb net/unix/af_unix.c:1674 [inline] unix_dgram_sendmsg+0x679/0x16b0 net/unix/af_unix.c:1817 unix_seqpacket_sendmsg+0xcc/0x110 net/unix/af_unix.c:2258 sock_sendmsg_nosec net/socket.c:704 [inline] sock_sendmsg net/socket.c:724 [inline] ____sys_sendmsg+0x39a/0x510 net/socket.c:2409 ___sys_sendmsg net/socket.c:2463 [inline] __sys_sendmmsg+0x267/0x4c0 net/socket.c:2549 __do_sys_sendmmsg net/socket.c:2578 [inline] __se_sys_sendmmsg net/socket.c:2575 [inline] __x64_sys_sendmmsg+0x53/0x60 net/socket.c:2575 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x44/0xd0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x44/0xae read to 0xffffffff86e2b7c0 of 4 bytes by task 9375 on cpu 1: wait_for_unix_gc+0x24/0x160 net/unix/garbage.c:196 unix_dgram_sendmsg+0x8e/0x16b0 net/unix/af_unix.c:1772 unix_seqpacket_sendmsg+0xcc/0x110 net/unix/af_unix.c:2258 sock_sendmsg_nosec net/socket.c:704 [inline] sock_sendmsg net/socket.c:724 [inline] ____sys_sendmsg+0x39a/0x510 net/socket.c:2409 ___sys_sendmsg net/socket.c:2463 [inline] __sys_sendmmsg+0x267/0x4c0 net/socket.c:2549 __do_sys_sendmmsg net/socket.c:2578 [inline] __se_sys_sendmmsg net/socket.c:2575 [inline] __x64_sys_sendmmsg+0x53/0x60 net/socket.c:2575 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x44/0xd0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x44/0xae value changed: 0x00000002 -> 0x00000004 Reported by Kernel Concurrency Sanitizer on: CPU: 1 PID: 9375 Comm: syz-executor.1 Not tainted 5.16.0-rc7-syzkaller #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 Fixes: 9915672 ("af_unix: limit unix_tot_inflight") Signed-off-by: Eric Dumazet <[email protected]> Reported-by: syzbot <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent d90d0c1 commit 9d6d7f1

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

net/unix/garbage.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,11 @@ void wait_for_unix_gc(void)
192192
{
193193
/* If number of inflight sockets is insane,
194194
* force a garbage collect right now.
195+
* Paired with the WRITE_ONCE() in unix_inflight(),
196+
* unix_notinflight() and gc_in_progress().
195197
*/
196-
if (unix_tot_inflight > UNIX_INFLIGHT_TRIGGER_GC && !gc_in_progress)
198+
if (READ_ONCE(unix_tot_inflight) > UNIX_INFLIGHT_TRIGGER_GC &&
199+
!READ_ONCE(gc_in_progress))
197200
unix_gc();
198201
wait_event(unix_gc_wait, gc_in_progress == false);
199202
}
@@ -213,7 +216,9 @@ void unix_gc(void)
213216
if (gc_in_progress)
214217
goto out;
215218

216-
gc_in_progress = true;
219+
/* Paired with READ_ONCE() in wait_for_unix_gc(). */
220+
WRITE_ONCE(gc_in_progress, true);
221+
217222
/* First, select candidates for garbage collection. Only
218223
* in-flight sockets are considered, and from those only ones
219224
* which don't have any external reference.
@@ -299,7 +304,10 @@ void unix_gc(void)
299304

300305
/* All candidates should have been detached by now. */
301306
BUG_ON(!list_empty(&gc_candidates));
302-
gc_in_progress = false;
307+
308+
/* Paired with READ_ONCE() in wait_for_unix_gc(). */
309+
WRITE_ONCE(gc_in_progress, false);
310+
303311
wake_up(&unix_gc_wait);
304312

305313
out:

net/unix/scm.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ void unix_inflight(struct user_struct *user, struct file *fp)
6060
} else {
6161
BUG_ON(list_empty(&u->link));
6262
}
63-
unix_tot_inflight++;
63+
/* Paired with READ_ONCE() in wait_for_unix_gc() */
64+
WRITE_ONCE(unix_tot_inflight, unix_tot_inflight + 1);
6465
}
6566
user->unix_inflight++;
6667
spin_unlock(&unix_gc_lock);
@@ -80,7 +81,8 @@ void unix_notinflight(struct user_struct *user, struct file *fp)
8081

8182
if (atomic_long_dec_and_test(&u->inflight))
8283
list_del_init(&u->link);
83-
unix_tot_inflight--;
84+
/* Paired with READ_ONCE() in wait_for_unix_gc() */
85+
WRITE_ONCE(unix_tot_inflight, unix_tot_inflight - 1);
8486
}
8587
user->unix_inflight--;
8688
spin_unlock(&unix_gc_lock);

0 commit comments

Comments
 (0)