Skip to content

Commit e3a5d59

Browse files
tiwaiJaroslav Kysela
authored andcommitted
[ALSA] Fix races in PCM OSS emulation
Fixed the race among multiple threads accessing the OSS PCM instance concurrently by simply introducing a mutex for protecting a setup of the PCM. Signed-off-by: Takashi Iwai <tiwai@suse.de> Signed-off-by: Jaroslav Kysela <perex@suse.cz>
1 parent ba8bdf8 commit e3a5d59

2 files changed

Lines changed: 41 additions & 12 deletions

File tree

‎include/sound/pcm_oss.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ struct snd_pcm_oss_runtime {
5656
size_t mmap_bytes;
5757
char *buffer; /* vmallocated period */
5858
size_t buffer_used; /* used length from period buffer */
59+
struct mutex params_lock;
5960
#ifdef CONFIG_SND_PCM_OSS_PLUGINS
6061
struct snd_pcm_plugin *plugin_first;
6162
struct snd_pcm_plugin *plugin_last;

‎sound/core/oss/pcm_oss.c‎

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,8 @@ static int snd_pcm_oss_change_params(struct snd_pcm_substream *substream)
810810
struct snd_mask sformat_mask;
811811
struct snd_mask mask;
812812

813+
if (mutex_lock_interruptible(&runtime->oss.params_lock))
814+
return -EINTR;
813815
sw_params = kmalloc(sizeof(*sw_params), GFP_KERNEL);
814816
params = kmalloc(sizeof(*params), GFP_KERNEL);
815817
sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);
@@ -1020,6 +1022,7 @@ static int snd_pcm_oss_change_params(struct snd_pcm_substream *substream)
10201022
kfree(sw_params);
10211023
kfree(params);
10221024
kfree(sparams);
1025+
mutex_unlock(&runtime->oss.params_lock);
10231026
return err;
10241027
}
10251028

@@ -1307,14 +1310,17 @@ static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const cha
13071310

13081311
if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
13091312
return tmp;
1313+
mutex_lock(&runtime->oss.params_lock);
13101314
while (bytes > 0) {
13111315
if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
13121316
tmp = bytes;
13131317
if (tmp + runtime->oss.buffer_used > runtime->oss.period_bytes)
13141318
tmp = runtime->oss.period_bytes - runtime->oss.buffer_used;
13151319
if (tmp > 0) {
1316-
if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp))
1317-
return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
1320+
if (copy_from_user(runtime->oss.buffer + runtime->oss.buffer_used, buf, tmp)) {
1321+
tmp = -EFAULT;
1322+
goto err;
1323+
}
13181324
}
13191325
runtime->oss.buffer_used += tmp;
13201326
buf += tmp;
@@ -1325,22 +1331,24 @@ static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const cha
13251331
tmp = snd_pcm_oss_write2(substream, runtime->oss.buffer + runtime->oss.period_ptr,
13261332
runtime->oss.buffer_used - runtime->oss.period_ptr, 1);
13271333
if (tmp <= 0)
1328-
return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1334+
goto err;
13291335
runtime->oss.bytes += tmp;
13301336
runtime->oss.period_ptr += tmp;
13311337
runtime->oss.period_ptr %= runtime->oss.period_bytes;
13321338
if (runtime->oss.period_ptr == 0 ||
13331339
runtime->oss.period_ptr == runtime->oss.buffer_used)
13341340
runtime->oss.buffer_used = 0;
1335-
else if ((substream->f_flags & O_NONBLOCK) != 0)
1336-
return xfer > 0 ? xfer : -EAGAIN;
1341+
else if ((substream->f_flags & O_NONBLOCK) != 0) {
1342+
tmp = -EAGAIN;
1343+
goto err;
1344+
}
13371345
}
13381346
} else {
13391347
tmp = snd_pcm_oss_write2(substream,
13401348
(const char __force *)buf,
13411349
runtime->oss.period_bytes, 0);
13421350
if (tmp <= 0)
1343-
return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1351+
goto err;
13441352
runtime->oss.bytes += tmp;
13451353
buf += tmp;
13461354
bytes -= tmp;
@@ -1350,7 +1358,12 @@ static ssize_t snd_pcm_oss_write1(struct snd_pcm_substream *substream, const cha
13501358
break;
13511359
}
13521360
}
1361+
mutex_unlock(&runtime->oss.params_lock);
13531362
return xfer;
1363+
1364+
err:
1365+
mutex_unlock(&runtime->oss.params_lock);
1366+
return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
13541367
}
13551368

13561369
static ssize_t snd_pcm_oss_read2(struct snd_pcm_substream *substream, char *buf, size_t bytes, int in_kernel)
@@ -1397,21 +1410,24 @@ static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __use
13971410

13981411
if ((tmp = snd_pcm_oss_make_ready(substream)) < 0)
13991412
return tmp;
1413+
mutex_lock(&runtime->oss.params_lock);
14001414
while (bytes > 0) {
14011415
if (bytes < runtime->oss.period_bytes || runtime->oss.buffer_used > 0) {
14021416
if (runtime->oss.buffer_used == 0) {
14031417
tmp = snd_pcm_oss_read2(substream, runtime->oss.buffer, runtime->oss.period_bytes, 1);
14041418
if (tmp <= 0)
1405-
return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1419+
goto err;
14061420
runtime->oss.bytes += tmp;
14071421
runtime->oss.period_ptr = tmp;
14081422
runtime->oss.buffer_used = tmp;
14091423
}
14101424
tmp = bytes;
14111425
if ((size_t) tmp > runtime->oss.buffer_used)
14121426
tmp = runtime->oss.buffer_used;
1413-
if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp))
1414-
return xfer > 0 ? (snd_pcm_sframes_t)xfer : -EFAULT;
1427+
if (copy_to_user(buf, runtime->oss.buffer + (runtime->oss.period_ptr - runtime->oss.buffer_used), tmp)) {
1428+
tmp = -EFAULT;
1429+
goto err;
1430+
}
14151431
buf += tmp;
14161432
bytes -= tmp;
14171433
xfer += tmp;
@@ -1420,14 +1436,19 @@ static ssize_t snd_pcm_oss_read1(struct snd_pcm_substream *substream, char __use
14201436
tmp = snd_pcm_oss_read2(substream, (char __force *)buf,
14211437
runtime->oss.period_bytes, 0);
14221438
if (tmp <= 0)
1423-
return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
1439+
goto err;
14241440
runtime->oss.bytes += tmp;
14251441
buf += tmp;
14261442
bytes -= tmp;
14271443
xfer += tmp;
14281444
}
14291445
}
1446+
mutex_unlock(&runtime->oss.params_lock);
14301447
return xfer;
1448+
1449+
err:
1450+
mutex_unlock(&runtime->oss.params_lock);
1451+
return xfer > 0 ? (snd_pcm_sframes_t)xfer : tmp;
14311452
}
14321453

14331454
static int snd_pcm_oss_reset(struct snd_pcm_oss_file *pcm_oss_file)
@@ -1528,6 +1549,7 @@ static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
15281549
return err;
15291550
format = snd_pcm_oss_format_from(runtime->oss.format);
15301551
width = snd_pcm_format_physical_width(format);
1552+
mutex_lock(&runtime->oss.params_lock);
15311553
if (runtime->oss.buffer_used > 0) {
15321554
#ifdef OSS_DEBUG
15331555
printk("sync: buffer_used\n");
@@ -1537,8 +1559,10 @@ static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
15371559
runtime->oss.buffer + runtime->oss.buffer_used,
15381560
size);
15391561
err = snd_pcm_oss_sync1(substream, runtime->oss.period_bytes);
1540-
if (err < 0)
1562+
if (err < 0) {
1563+
mutex_unlock(&runtime->oss.params_lock);
15411564
return err;
1565+
}
15421566
} else if (runtime->oss.period_ptr > 0) {
15431567
#ifdef OSS_DEBUG
15441568
printk("sync: period_ptr\n");
@@ -1548,8 +1572,10 @@ static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
15481572
runtime->oss.buffer,
15491573
size * 8 / width);
15501574
err = snd_pcm_oss_sync1(substream, size);
1551-
if (err < 0)
1575+
if (err < 0) {
1576+
mutex_unlock(&runtime->oss.params_lock);
15521577
return err;
1578+
}
15531579
}
15541580
/*
15551581
* The ALSA's period might be a bit large than OSS one.
@@ -1579,6 +1605,7 @@ static int snd_pcm_oss_sync(struct snd_pcm_oss_file *pcm_oss_file)
15791605
snd_pcm_lib_writev(substream, buffers, size);
15801606
}
15811607
}
1608+
mutex_unlock(&runtime->oss.params_lock);
15821609
/*
15831610
* finish sync: drain the buffer
15841611
*/
@@ -2172,6 +2199,7 @@ static void snd_pcm_oss_init_substream(struct snd_pcm_substream *substream,
21722199
runtime->oss.params = 1;
21732200
runtime->oss.trigger = 1;
21742201
runtime->oss.rate = 8000;
2202+
mutex_init(&runtime->oss.params_lock);
21752203
switch (SNDRV_MINOR_OSS_DEVICE(minor)) {
21762204
case SNDRV_MINOR_OSS_PCM_8:
21772205
runtime->oss.format = AFMT_U8;

0 commit comments

Comments
 (0)