linux/sound/core/seq/oss
Cen Zhang 49ce92d207 ALSA: seq: oss: Serialize readq reset state with q->lock
snd_seq_oss_readq_clear() resets qlen, head, and tail without
q->lock even though the normal reader and producer paths serialize the
same ring state under that spinlock. A reset can therefore race
snd_seq_oss_readq_free() or snd_seq_oss_readq_put_event() and leave
stale records in the queue, drop freshly queued ones, or report the
wrong readiness after wakeup. KCSAN reports a data race between
snd_seq_oss_readq_clear() and snd_seq_oss_readq_free().

Take q->lock while clearing the ring and resetting input_time. Factor
the enqueue logic into a caller-locked helper so
snd_seq_oss_readq_put_timestamp() updates its suppression state under
the same lock instead of racing the reset path.

The buggy scenario involves two paths, with each column showing the
order within that path:

reset path:                      locked readq updater:
1. snd_seq_oss_reset() or        1. A reader or callback producer
   release reaches                  takes q->lock on the same queue.
   snd_seq_oss_readq_clear().
2. snd_seq_oss_readq_clear()     2. The updater tests or modifies
   resets qlen, head, tail,         qlen, head, and tail.
   and input_time.
3. snd_seq_oss_readq_clear()     3. The updater completes its
   wakes sleepers on                read-modify-write sequence.
   q->midi_sleep.
4. Without q->lock, the reset    4. The resulting ring state drives
   can overlap the locked           later reads and readiness.
   update.

KCSAN reports:

BUG: KCSAN: data-race in snd_seq_oss_readq_clear /
snd_seq_oss_readq_free

write to 0xffff8881069fe608 of 4 bytes by task 120516 on cpu 0:
  snd_seq_oss_readq_free+0x6c/0x80
  snd_seq_oss_read+0xcb/0x250
  odev_read+0x38/0x60
  vfs_read+0xff/0x600
  ksys_read+0xb4/0x140
  __x64_sys_read+0x46/0x60
  do_syscall_64+0xbb/0x2f0
  entry_SYSCALL_64_after_hwframe+0x77/0x7f

read to 0xffff8881069fe608 of 4 bytes by task 120517 on cpu 1:
  snd_seq_oss_readq_clear+0x1f/0x90
  snd_seq_oss_reset+0xa7/0xf0
  snd_seq_oss_ioctl+0x6f6/0x7e0
  odev_ioctl+0x56/0xc0
  __x64_sys_ioctl+0xd1/0x120
  do_syscall_64+0xbb/0x2f0
  entry_SYSCALL_64_after_hwframe+0x77/0x7f

value changed: 0x00000001 -> 0x00000000

Fixes: 1da177e4c3 ("Linux-2.6.12-rc2")

Signed-off-by: Cen Zhang <zzzccc427@gmail.com>
Link: https://patch.msgid.link/20260614004801.3507773-1-zzzccc427@gmail.com
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2026-06-14 10:54:21 +02:00
..
Makefile ALSA: core: Use *-y instead of *-objs in Makefile 2024-05-08 18:17:32 +02:00
seq_oss_device.h ALSA: seq: Simplify internal command operation from OSS layer 2025-08-29 11:52:35 +02:00
seq_oss_event.c ALSA: seq: oss: Fix UAF at handling events with embedded SysEx data 2026-05-27 07:29:43 +02:00
seq_oss_event.h ALSA: seq: oss: Use scoped cleanup for temporary MIDI use lock 2026-06-04 10:20:41 +02:00
seq_oss_init.c ALSA: core: Mark some variables as __ro_after_init 2026-05-25 09:31:40 +02:00
seq_oss_ioctl.c ALSA: seq: oss: Use scoped cleanup for temporary MIDI use lock 2026-06-04 10:20:41 +02:00
seq_oss_midi.c ALSA: seq: oss: Fix UAF at handling events with embedded SysEx data 2026-05-27 07:29:43 +02:00
seq_oss_midi.h ALSA: seq: oss: Fix UAF at handling events with embedded SysEx data 2026-05-27 07:29:43 +02:00
seq_oss_readq.c ALSA: seq: oss: Serialize readq reset state with q->lock 2026-06-14 10:54:21 +02:00
seq_oss_readq.h ALSA: seq: oss/rw: allocate evrec with main struct 2026-05-04 13:24:53 +02:00
seq_oss_rw.c ALSA: seq: oss: Use scoped cleanup for temporary MIDI use lock 2026-06-04 10:20:41 +02:00
seq_oss_synth.c Convert remaining multi-line kmalloc_obj/flex GFP_KERNEL uses 2026-02-22 08:26:33 -08:00
seq_oss_synth.h ALSA: seq: oss: Convert to snd_seq bus probe mechanism 2025-12-14 11:08:09 +01:00
seq_oss_timer.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
seq_oss_timer.h ALSA: seq: oss: remove unused inline function snd_seq_oss_timer_is_realtime 2020-04-29 15:58:55 +02:00
seq_oss_writeq.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
seq_oss_writeq.h treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 156 2019-05-30 11:26:35 -07:00
seq_oss.c ALSA: seq: oss: Convert to snd_seq bus probe mechanism 2025-12-14 11:08:09 +01:00