Skip to content

Commit f5bc918

Browse files
tiwaigregkh
authored andcommitted
ALSA: timer: Fix race between read and ioctl
commit d11662f4f798b50d8c8743f433842c3e40fe3378 upstream. The read from ALSA timer device, the function snd_timer_user_tread(), may access to an uninitialized struct snd_timer_user fields when the read is concurrently performed while the ioctl like snd_timer_user_tselect() is invoked. We have already fixed the races among ioctls via a mutex, but we seem to have forgotten the race between read vs ioctl. This patch simply applies (more exactly extends the already applied range of) tu->ioctl_lock in snd_timer_user_tread() for closing the race window. Reported-by: Alexander Potapenko <glider@google.com> Tested-by: Alexander Potapenko <glider@google.com> Signed-off-by: Takashi Iwai <tiwai@suse.de> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 5dffc1b commit f5bc918

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

sound/core/timer.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,6 +1958,7 @@ static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
19581958

19591959
tu = file->private_data;
19601960
unit = tu->tread ? sizeof(struct snd_timer_tread) : sizeof(struct snd_timer_read);
1961+
mutex_lock(&tu->ioctl_lock);
19611962
spin_lock_irq(&tu->qlock);
19621963
while ((long)count - result >= unit) {
19631964
while (!tu->qused) {
@@ -1973,7 +1974,9 @@ static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
19731974
add_wait_queue(&tu->qchange_sleep, &wait);
19741975

19751976
spin_unlock_irq(&tu->qlock);
1977+
mutex_unlock(&tu->ioctl_lock);
19761978
schedule();
1979+
mutex_lock(&tu->ioctl_lock);
19771980
spin_lock_irq(&tu->qlock);
19781981

19791982
remove_wait_queue(&tu->qchange_sleep, &wait);
@@ -1993,7 +1996,6 @@ static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
19931996
tu->qused--;
19941997
spin_unlock_irq(&tu->qlock);
19951998

1996-
mutex_lock(&tu->ioctl_lock);
19971999
if (tu->tread) {
19982000
if (copy_to_user(buffer, &tu->tqueue[qhead],
19992001
sizeof(struct snd_timer_tread)))
@@ -2003,7 +2005,6 @@ static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
20032005
sizeof(struct snd_timer_read)))
20042006
err = -EFAULT;
20052007
}
2006-
mutex_unlock(&tu->ioctl_lock);
20072008

20082009
spin_lock_irq(&tu->qlock);
20092010
if (err < 0)
@@ -2013,6 +2014,7 @@ static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
20132014
}
20142015
_error:
20152016
spin_unlock_irq(&tu->qlock);
2017+
mutex_unlock(&tu->ioctl_lock);
20162018
return result > 0 ? result : err;
20172019
}
20182020

0 commit comments

Comments
 (0)