KCSAN: data-race in snd_seq_poll / snd_seq_pool_init
syzbot
syzbotHello,
syzbot found the following issue on:
HEAD commit: 48a5eed9ad58 Merge tag 'devicetree-fixes-for-6.14-2' of gi.. git tree: upstream console output: https://syzkaller.appspot.com/x/log.txt?x=1657d8b7980000 kernel config: https://syzkaller.appspot.com/x/.config?x=523b0e2f15224775 dashboard link: https://syzkaller.appspot.com/bug?extid=2d373c9936c00d7e120c compiler: Debian clang version 15.0.6, GNU ld (GNU Binutils for Debian) 2.40
Unfortunately, I don't have any reproducer for this issue yet.
Downloadable assets:
disk image: https://storage.googleapis.com/syzbot-assets/877a63ac53f2/disk-48a5eed9.raw.xz
vmlinux: https://storage.googleapis.com/syzbot-assets/7b3a614d4df6/vmlinux-48a5eed9.xz
kernel image: https://storage.googleapis.com/syzbot-assets/2ce987fd9e0c/bzImage-48a5eed9.xz
IMPORTANT: if you fix the issue, please add the following tag to the commit:
==================================================================
BUG: KCSAN: data-race in snd_seq_poll / snd_seq_pool_init
write to 0xffff888114535610 of 4 bytes by task 7006 on cpu 1:
snd_seq_pool_init+0x1c1/0x200 sound/core/seq/seq_memory.c:469 snd_seq_write+0x17f/0x500 sound/core/seq/seq_clientmgr.c:1022 vfs_write+0x27d/0x920 fs/read_write.c:677 ksys_write+0xe8/0x1b0 fs/read_write.c:731 __do_sys_write fs/read_write.c:742 [inline] __se_sys_write fs/read_write.c:739 [inline] __x64_sys_write+0x42/0x50 fs/read_write.c:739 x64_sys_call+0x287e/0x2dc0 arch/x86/include/generated/asm/syscalls_64.h:2 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0xc9/0x1c0 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x77/0x7f
read to 0xffff888114535610 of 4 bytes by task 7005 on cpu 0:
snd_seq_total_cells sound/core/seq/seq_memory.h:83 [inline] snd_seq_write_pool_allocated sound/core/seq/seq_clientmgr.c:95 [inline] snd_seq_poll+0x103/0x170 sound/core/seq/seq_clientmgr.c:1139 vfs_poll include/linux/poll.h:82 [inline] __io_arm_poll_handler+0x1e5/0xd50 io_uring/poll.c:582 io_arm_poll_handler+0x464/0x5b0 io_uring/poll.c:707 io_queue_async+0x89/0x320 io_uring/io_uring.c:1925 io_queue_sqe io_uring/io_uring.c:1954 [inline] io_req_task_submit+0xb9/0xc0 io_uring/io_uring.c:1373 io_handle_tw_list+0x1b9/0x200 io_uring/io_uring.c:1059 tctx_task_work_run+0x6e/0x1c0 io_uring/io_uring.c:1123 tctx_task_work+0x40/0x80 io_uring/io_uring.c:1141 task_work_run+0x13a/0x1a0 kernel/task_work.c:227 get_signal+0xe78/0x1000 kernel/signal.c:2809 arch_do_signal_or_restart+0x95/0x4b0 arch/x86/kernel/signal.c:337 exit_to_user_mode_loop kernel/entry/common.c:111 [inline] exit_to_user_mode_prepare include/linux/entry-common.h:329 [inline] __syscall_exit_to_user_mode_work kernel/entry/common.c:207 [inline] syscall_exit_to_user_mode+0x62/0x120 kernel/entry/common.c:218 do_syscall_64+0xd6/0x1c0 arch/x86/entry/common.c:89 entry_SYSCALL_64_after_hwframe+0x77/0x7f
value changed: 0x00000000 -> 0x000001f4
Reported by Kernel Concurrency Sanitizer on:
CPU: 0 UID: 0 PID: 7005 Comm: syz.2.1455 Tainted: G W 6.14.0-rc5-syzkaller-00016-g48a5eed9ad58 #0 Tainted: [W]=WARN Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 02/12/2025 ==================================================================
---
This report is generated by a bot. It may contain errors. See https://goo.gl/tpsmEJ for more information about syzbot. syzbot engineers can be reached at [email protected].
syzbot will keep track of this issue. See:
https://goo.gl/tpsmEJ#status for how to communicate with syzbot.
If the report is already addressed, let syzbot know by replying with:
#syz fix: exact-commit-title
If you want to overwrite report's subsystems, reply with:
#syz set subsystems: new-subsystem
(See the list of subsystem names on the web dashboard)
If the report is a duplicate of another one, reply with:
#syz dup: exact-subject-of-another-report
If you want to undo deduplication, reply with:
#syz undup
PATCH 1/1 ALSA: seq: Use atomic to prevent data races in total_elements
syzbot reported a data-race in snd_seq_poll / snd_seq_pool_init. [1]
Just use atomic_set/atomic_read for handling this case.
[1]
BUG: KCSAN: data-race in snd_seq_poll / snd_seq_pool_init
write to 0xffff888114535610 of 4 bytes by task 7006 on cpu 1:
snd_seq_pool_init+0x1c1/0x200 sound/core/seq/seq_memory.c:469 snd_seq_write+0x17f/0x500 sound/core/seq/seq_clientmgr.c:1022 vfs_write+0x27d/0x920 fs/read_write.c:677 ksys_write+0xe8/0x1b0 fs/read_write.c:731 __do_sys_write fs/read_write.c:742 [inline] __se_sys_write fs/read_write.c:739 [inline] __x64_sys_write+0x42/0x50 fs/read_write.c:739 x64_sys_call+0x287e/0x2dc0 arch/x86/include/generated/asm/syscalls_64.h:2 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0xc9/0x1c0 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x77/0x7f
read to 0xffff888114535610 of 4 bytes by task 7005 on cpu 0:
snd_seq_total_cells sound/core/seq/seq_memory.h:83 [inline] snd_seq_write_pool_allocated sound/core/seq/seq_clientmgr.c:95 [inline] snd_seq_poll+0x103/0x170 sound/core/seq/seq_clientmgr.c:1139 vfs_poll include/linux/poll.h:82 [inline] __io_arm_poll_handler+0x1e5/0xd50 io_uring/poll.c:582 io_arm_poll_handler+0x464/0x5b0 io_uring/poll.c:707 io_queue_async+0x89/0x320 io_uring/io_uring.c:1925 io_queue_sqe io_uring/io_uring.c:1954 [inline] io_req_task_submit+0xb9/0xc0 io_uring/io_uring.c:1373 io_handle_tw_list+0x1b9/0x200 io_uring/io_uring.c:1059 tctx_task_work_run+0x6e/0x1c0 io_uring/io_uring.c:1123 tctx_task_work+0x40/0x80 io_uring/io_uring.c:1141 task_work_run+0x13a/0x1a0 kernel/task_work.c:227 get_signal+0xe78/0x1000 kernel/signal.c:2809 arch_do_signal_or_restart+0x95/0x4b0 arch/x86/kernel/signal.c:337 exit_to_user_mode_loop kernel/entry/common.c:111 [inline] exit_to_user_mode_prepare include/linux/entry-common.h:329 [inline] __syscall_exit_to_user_mode_work kernel/entry/common.c:207 [inline] syscall_exit_to_user_mode+0x62/0x120 kernel/entry/common.c:218 do_syscall_64+0xd6/0x1c0 arch/x86/entry/common.c:89 entry_SYSCALL_64_after_hwframe+0x77/0x7f
value changed: 0x00000000 -> 0x000001f4
--- sound/core/seq/seq_memory.c | 12 ++++++------ sound/core/seq/seq_memory.h | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-)
sound/core/seq/seq_memory.c +6 -6
@@ -21,7 +21,7 @@static inline int snd_seq_pool_available (struct snd_seq_pool *pool) { -return pool->total_elements -atomic_read (&pool->counter); +return atomic_read (&pool->total_elements) -atomic_read (&pool->counter); }static inline int snd_seq_output_ok (struct snd_seq_pool *pool)
@@ -353,7 +353,7 @@ int snd_seq_event_dup(struct snd_seq_pool *pool, struct snd_seq_event *event, extlen = event->data.ext.len & ~SNDRV_SEQ_EXT_MASK ; ncells =DIV_ROUND_UP (extlen,sizeof (struct snd_seq_event )); } -if (ncells >= pool->total_elements) +if (ncells >=atomic_read (&pool->total_elements))return -ENOMEM ; err =snd_seq_cell_alloc (pool, &cell, nonblock, file, mutexp);
@@ -466,7 +466,7 @@ int snd_seq_pool_init(struct snd_seq_pool *pool)/* init statistics */ pool->max_used =0 ; - pool->total_elements = pool->size; +atomic_set (&pool->total_elements, pool->size);return 0 ; }
@@ -499,7 +499,7 @@ int snd_seq_pool_done(struct snd_seq_pool *pool) ptr = pool->ptr; pool->ptr =NULL ; pool->free =NULL ; - pool->total_elements =0 ; +atomic_set (&pool->total_elements,0 ); }kvfree (ptr);
@@ -523,7 +523,7 @@ struct snd_seq_pool *snd_seq_pool_new(int poolsize)spin_lock_init (&pool->lock); pool->ptr =NULL ; pool->free =NULL ; - pool->total_elements =0 ; +atomic_set (&pool->total_elements,0 );atomic_set (&pool->counter,0 ); pool->closing =0 ;init_waitqueue_head (&pool->output_sleep);
@@ -555,7 +555,7 @@ void snd_seq_info_pool(struct snd_info_buffer *buffer, {if (pool ==NULL )return ; -snd_iprintf (buffer,"%sPool size : %d\n" , space, pool->total_elements); +snd_iprintf (buffer,"%sPool size : %d\n" , space,atomic_read (&pool->total_elements));snd_iprintf (buffer,"%sCells in use : %d\n" , space,atomic_read (&pool->counter));snd_iprintf (buffer,"%sPeak cells in use : %d\n" , space, pool->max_used);snd_iprintf (buffer,"%sAlloc success : %d\n" , space, pool->event_alloc_success);
sound/core/seq/seq_memory.h +3 -3
@@ -44,7 +44,7 @@ struct snd_seq_pool {struct snd_seq_event_cell *ptr;/* pointer to first event chunk */ struct snd_seq_event_cell *free;/* pointer to the head of the free list */ -int total_elements;/* pool size actually allocated */ +atomic_t total_elements;/* pool size actually allocated */ atomic_t counter;/* cells free */ int size;/* pool size to be allocated */
@@ -74,13 +74,13 @@ int snd_seq_event_dup(struct snd_seq_pool *pool, struct snd_seq_event *event,/* return number of unused (free) cells */ static inline int snd_seq_unused_cells (struct snd_seq_pool *pool) { -return pool ? pool->total_elements -atomic_read (&pool->counter) :0 ; +return pool ?atomic_read (&pool->total_elements) -atomic_read (&pool->counter) :0 ; }/* return total number of allocated cells */ static inline int snd_seq_total_cells (struct snd_seq_pool *pool) { -return pool ? pool->total_elements :0 ; +return pool ?atomic_read (&pool->total_elements) :0 ; }/* init pool - allocate events */
Signature · 1 line
2.43.0
Re: [PATCH] ALSA: seq: Use atomic to prevent data races in total_elements
Quoted 44 lines · Lizhi Xu
syzbot reported a data-race in
snd_seq_poll/snd_seq_pool_init. [1]Just use atomic_set/atomic_read for handling this case.
[1]
BUG: KCSAN: data-race in snd_seq_poll / snd_seq_pool_initwrite to
0xffff888114535610of 4 bytes by task 7006 on cpu 1:snd_seq_pool_init+0x1c1/0x200 sound/core/seq/seq_memory.c:469 snd_seq_write+0x17f/0x500 sound/core/seq/seq_clientmgr.c:1022 vfs_write+0x27d/0x920 fs/read_write.c:677 ksys_write+0xe8/0x1b0 fs/read_write.c:731 __do_sys_write fs/read_write.c:742 [inline] __se_sys_write fs/read_write.c:739 [inline] __x64_sys_write+0x42/0x50 fs/read_write.c:739 x64_sys_call+0x287e/0x2dc0 arch/x86/include/generated/asm/syscalls_64.h:2 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0xc9/0x1c0 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x77/0x7fread to
0xffff888114535610of 4 bytes by task 7005 on cpu 0:snd_seq_total_cells sound/core/seq/seq_memory.h:83 [inline] snd_seq_write_pool_allocated sound/core/seq/seq_clientmgr.c:95 [inline] snd_seq_poll+0x103/0x170 sound/core/seq/seq_clientmgr.c:1139 vfs_poll include/linux/poll.h:82 [inline] __io_arm_poll_handler+0x1e5/0xd50 io_uring/poll.c:582 io_arm_poll_handler+0x464/0x5b0 io_uring/poll.c:707 io_queue_async+0x89/0x320 io_uring/io_uring.c:1925 io_queue_sqe io_uring/io_uring.c:1954 [inline] io_req_task_submit+0xb9/0xc0 io_uring/io_uring.c:1373 io_handle_tw_list+0x1b9/0x200 io_uring/io_uring.c:1059 tctx_task_work_run+0x6e/0x1c0 io_uring/io_uring.c:1123 tctx_task_work+0x40/0x80 io_uring/io_uring.c:1141 task_work_run+0x13a/0x1a0 kernel/task_work.c:227 get_signal+0xe78/0x1000 kernel/signal.c:2809 arch_do_signal_or_restart+0x95/0x4b0 arch/x86/kernel/signal.c:337 exit_to_user_mode_loop kernel/entry/common.c:111 [inline] exit_to_user_mode_prepare include/linux/entry-common.h:329 [inline] __syscall_exit_to_user_mode_work kernel/entry/common.c:207 [inline] syscall_exit_to_user_mode+0x62/0x120 kernel/entry/common.c:218 do_syscall_64+0xd6/0x1c0 arch/x86/entry/common.c:89 entry_SYSCALL_64_after_hwframe+0x77/0x7f
Quoted 1 line
value changed:
0x00000000->0x000001f4
This is harmless as it's only a reference in poll() and that's rather volatile. So changing the whole with atomic_t is an overkill only for that.
OTOH, the check of pool size in the caller side is fragile, and it can purely rely on snd_seq_pool_poll_wait(). And, there, it should take the pool->lock for the data consistency.
So, if any, an alternative fix would be something like below.
thanks,
Takashi
-- 8< --
sound/core/seq/seq_clientmgr.c +1 -2
@@ -1150,8 +1150,7 @@ static __poll_t snd_seq_poll(struct file *file, poll_table * wait)if (snd_seq_file_flags (file) &SNDRV_SEQ_LFLG_OUTPUT ) {/* check if data is available in the pool */ -if (!snd_seq_write_pool_allocated (client) || -snd_seq_pool_poll_wait (client->pool, file, wait)) +if (snd_seq_pool_poll_wait (client->pool, file, wait)) mask |=EPOLLOUT |EPOLLWRNORM ; }
sound/core/seq/seq_memory.c +1 -0
@@ -427,6 +427,7 @@ int snd_seq_pool_poll_wait(struct snd_seq_pool *pool, struct file *file, poll_table *wait) {poll_wait (file, &pool->output_sleep, wait); +guard (spinlock_irq)(&pool->lock);return snd_seq_output_ok (pool); }