Skip to content

Commit 2cdc7b6

Browse files
committed
ALSA: seq: Fix yet another races among ALSA timer accesses
ALSA sequencer may open/close and control ALSA timer instance dynamically either via sequencer events or direct ioctls. These are done mostly asynchronously, and it may call still some timer action like snd_timer_start() while another is calling snd_timer_close(). Since the instance gets removed by snd_timer_close(), it may lead to a use-after-free. This patch tries to address such a race by protecting each snd_timer_*() call via the existing spinlock and also by avoiding the access to timer during close call. BugLink: http://lkml.kernel.org/r/CACT4Y+Z6RzW5MBr-HUdV-8zwg71WQfKTdPpYGvOeS7v4cyurNQ@mail.gmail.com Reported-by: Dmitry Vyukov <[email protected]> Cc: <[email protected]> Signed-off-by: Takashi Iwai <[email protected]>
1 parent b248371 commit 2cdc7b6

File tree

1 file changed

+67
-20
lines changed

1 file changed

+67
-20
lines changed

sound/core/seq/seq_timer.c

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ void snd_seq_timer_delete(struct snd_seq_timer **tmr)
9090

9191
void snd_seq_timer_defaults(struct snd_seq_timer * tmr)
9292
{
93+
unsigned long flags;
94+
95+
spin_lock_irqsave(&tmr->lock, flags);
9396
/* setup defaults */
9497
tmr->ppq = 96; /* 96 PPQ */
9598
tmr->tempo = 500000; /* 120 BPM */
@@ -105,21 +108,25 @@ void snd_seq_timer_defaults(struct snd_seq_timer * tmr)
105108
tmr->preferred_resolution = seq_default_timer_resolution;
106109

107110
tmr->skew = tmr->skew_base = SKEW_BASE;
111+
spin_unlock_irqrestore(&tmr->lock, flags);
108112
}
109113

110-
void snd_seq_timer_reset(struct snd_seq_timer * tmr)
114+
static void seq_timer_reset(struct snd_seq_timer *tmr)
111115
{
112-
unsigned long flags;
113-
114-
spin_lock_irqsave(&tmr->lock, flags);
115-
116116
/* reset time & songposition */
117117
tmr->cur_time.tv_sec = 0;
118118
tmr->cur_time.tv_nsec = 0;
119119

120120
tmr->tick.cur_tick = 0;
121121
tmr->tick.fraction = 0;
122+
}
123+
124+
void snd_seq_timer_reset(struct snd_seq_timer *tmr)
125+
{
126+
unsigned long flags;
122127

128+
spin_lock_irqsave(&tmr->lock, flags);
129+
seq_timer_reset(tmr);
123130
spin_unlock_irqrestore(&tmr->lock, flags);
124131
}
125132

@@ -138,8 +145,11 @@ static void snd_seq_timer_interrupt(struct snd_timer_instance *timeri,
138145
tmr = q->timer;
139146
if (tmr == NULL)
140147
return;
141-
if (!tmr->running)
148+
spin_lock_irqsave(&tmr->lock, flags);
149+
if (!tmr->running) {
150+
spin_unlock_irqrestore(&tmr->lock, flags);
142151
return;
152+
}
143153

144154
resolution *= ticks;
145155
if (tmr->skew != tmr->skew_base) {
@@ -148,8 +158,6 @@ static void snd_seq_timer_interrupt(struct snd_timer_instance *timeri,
148158
(((resolution & 0xffff) * tmr->skew) >> 16);
149159
}
150160

151-
spin_lock_irqsave(&tmr->lock, flags);
152-
153161
/* update timer */
154162
snd_seq_inc_time_nsec(&tmr->cur_time, resolution);
155163

@@ -296,26 +304,30 @@ int snd_seq_timer_open(struct snd_seq_queue *q)
296304
t->callback = snd_seq_timer_interrupt;
297305
t->callback_data = q;
298306
t->flags |= SNDRV_TIMER_IFLG_AUTO;
307+
spin_lock_irq(&tmr->lock);
299308
tmr->timeri = t;
309+
spin_unlock_irq(&tmr->lock);
300310
return 0;
301311
}
302312

303313
int snd_seq_timer_close(struct snd_seq_queue *q)
304314
{
305315
struct snd_seq_timer *tmr;
316+
struct snd_timer_instance *t;
306317

307318
tmr = q->timer;
308319
if (snd_BUG_ON(!tmr))
309320
return -EINVAL;
310-
if (tmr->timeri) {
311-
snd_timer_stop(tmr->timeri);
312-
snd_timer_close(tmr->timeri);
313-
tmr->timeri = NULL;
314-
}
321+
spin_lock_irq(&tmr->lock);
322+
t = tmr->timeri;
323+
tmr->timeri = NULL;
324+
spin_unlock_irq(&tmr->lock);
325+
if (t)
326+
snd_timer_close(t);
315327
return 0;
316328
}
317329

318-
int snd_seq_timer_stop(struct snd_seq_timer * tmr)
330+
static int seq_timer_stop(struct snd_seq_timer *tmr)
319331
{
320332
if (! tmr->timeri)
321333
return -EINVAL;
@@ -326,6 +338,17 @@ int snd_seq_timer_stop(struct snd_seq_timer * tmr)
326338
return 0;
327339
}
328340

341+
int snd_seq_timer_stop(struct snd_seq_timer *tmr)
342+
{
343+
unsigned long flags;
344+
int err;
345+
346+
spin_lock_irqsave(&tmr->lock, flags);
347+
err = seq_timer_stop(tmr);
348+
spin_unlock_irqrestore(&tmr->lock, flags);
349+
return err;
350+
}
351+
329352
static int initialize_timer(struct snd_seq_timer *tmr)
330353
{
331354
struct snd_timer *t;
@@ -358,13 +381,13 @@ static int initialize_timer(struct snd_seq_timer *tmr)
358381
return 0;
359382
}
360383

361-
int snd_seq_timer_start(struct snd_seq_timer * tmr)
384+
static int seq_timer_start(struct snd_seq_timer *tmr)
362385
{
363386
if (! tmr->timeri)
364387
return -EINVAL;
365388
if (tmr->running)
366-
snd_seq_timer_stop(tmr);
367-
snd_seq_timer_reset(tmr);
389+
seq_timer_stop(tmr);
390+
seq_timer_reset(tmr);
368391
if (initialize_timer(tmr) < 0)
369392
return -EINVAL;
370393
snd_timer_start(tmr->timeri, tmr->ticks);
@@ -373,14 +396,25 @@ int snd_seq_timer_start(struct snd_seq_timer * tmr)
373396
return 0;
374397
}
375398

376-
int snd_seq_timer_continue(struct snd_seq_timer * tmr)
399+
int snd_seq_timer_start(struct snd_seq_timer *tmr)
400+
{
401+
unsigned long flags;
402+
int err;
403+
404+
spin_lock_irqsave(&tmr->lock, flags);
405+
err = seq_timer_start(tmr);
406+
spin_unlock_irqrestore(&tmr->lock, flags);
407+
return err;
408+
}
409+
410+
static int seq_timer_continue(struct snd_seq_timer *tmr)
377411
{
378412
if (! tmr->timeri)
379413
return -EINVAL;
380414
if (tmr->running)
381415
return -EBUSY;
382416
if (! tmr->initialized) {
383-
snd_seq_timer_reset(tmr);
417+
seq_timer_reset(tmr);
384418
if (initialize_timer(tmr) < 0)
385419
return -EINVAL;
386420
}
@@ -390,11 +424,24 @@ int snd_seq_timer_continue(struct snd_seq_timer * tmr)
390424
return 0;
391425
}
392426

427+
int snd_seq_timer_continue(struct snd_seq_timer *tmr)
428+
{
429+
unsigned long flags;
430+
int err;
431+
432+
spin_lock_irqsave(&tmr->lock, flags);
433+
err = seq_timer_continue(tmr);
434+
spin_unlock_irqrestore(&tmr->lock, flags);
435+
return err;
436+
}
437+
393438
/* return current 'real' time. use timeofday() to get better granularity. */
394439
snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr)
395440
{
396441
snd_seq_real_time_t cur_time;
442+
unsigned long flags;
397443

444+
spin_lock_irqsave(&tmr->lock, flags);
398445
cur_time = tmr->cur_time;
399446
if (tmr->running) {
400447
struct timeval tm;
@@ -410,7 +457,7 @@ snd_seq_real_time_t snd_seq_timer_get_cur_time(struct snd_seq_timer *tmr)
410457
}
411458
snd_seq_sanity_real_time(&cur_time);
412459
}
413-
460+
spin_unlock_irqrestore(&tmr->lock, flags);
414461
return cur_time;
415462
}
416463

0 commit comments

Comments
 (0)