Skip to content

Commit 8d42fda

Browse files
committed
Merge branch 'topic/core-vuln-fixes' into for-linus
2 parents 6c0c9a3 + 883a1d4 commit 8d42fda

3 files changed

Lines changed: 54 additions & 27 deletions

File tree

‎include/sound/core.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ struct snd_card {
116116
int user_ctl_count; /* count of all user controls */
117117
struct list_head controls; /* all controls for this card */
118118
struct list_head ctl_files; /* active control files */
119+
struct mutex user_ctl_lock; /* protects user controls against
120+
concurrent access */
119121

120122
struct snd_info_entry *proc_root; /* root for soundcard specific files */
121123
struct snd_info_entry *proc_id; /* the card id */

‎sound/core/control.c‎

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,10 @@ static bool snd_ctl_remove_numid_conflict(struct snd_card *card,
288288
{
289289
struct snd_kcontrol *kctl;
290290

291+
/* Make sure that the ids assigned to the control do not wrap around */
292+
if (card->last_numid >= UINT_MAX - count)
293+
card->last_numid = 0;
294+
291295
list_for_each_entry(kctl, &card->controls, list) {
292296
if (kctl->id.numid < card->last_numid + 1 + count &&
293297
kctl->id.numid + kctl->count > card->last_numid + 1) {
@@ -330,13 +334,17 @@ int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
330334
{
331335
struct snd_ctl_elem_id id;
332336
unsigned int idx;
337+
unsigned int count;
333338
int err = -EINVAL;
334339

335340
if (! kcontrol)
336341
return err;
337342
if (snd_BUG_ON(!card || !kcontrol->info))
338343
goto error;
339344
id = kcontrol->id;
345+
if (id.index > UINT_MAX - kcontrol->count)
346+
goto error;
347+
340348
down_write(&card->controls_rwsem);
341349
if (snd_ctl_find_id(card, &id)) {
342350
up_write(&card->controls_rwsem);
@@ -358,8 +366,9 @@ int snd_ctl_add(struct snd_card *card, struct snd_kcontrol *kcontrol)
358366
card->controls_count += kcontrol->count;
359367
kcontrol->id.numid = card->last_numid + 1;
360368
card->last_numid += kcontrol->count;
369+
count = kcontrol->count;
361370
up_write(&card->controls_rwsem);
362-
for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
371+
for (idx = 0; idx < count; idx++, id.index++, id.numid++)
363372
snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
364373
return 0;
365374

@@ -388,6 +397,7 @@ int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol,
388397
bool add_on_replace)
389398
{
390399
struct snd_ctl_elem_id id;
400+
unsigned int count;
391401
unsigned int idx;
392402
struct snd_kcontrol *old;
393403
int ret;
@@ -423,8 +433,9 @@ int snd_ctl_replace(struct snd_card *card, struct snd_kcontrol *kcontrol,
423433
card->controls_count += kcontrol->count;
424434
kcontrol->id.numid = card->last_numid + 1;
425435
card->last_numid += kcontrol->count;
436+
count = kcontrol->count;
426437
up_write(&card->controls_rwsem);
427-
for (idx = 0; idx < kcontrol->count; idx++, id.index++, id.numid++)
438+
for (idx = 0; idx < count; idx++, id.index++, id.numid++)
428439
snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_ADD, &id);
429440
return 0;
430441

@@ -897,9 +908,9 @@ static int snd_ctl_elem_write(struct snd_card *card, struct snd_ctl_file *file,
897908
result = kctl->put(kctl, control);
898909
}
899910
if (result > 0) {
911+
struct snd_ctl_elem_id id = control->id;
900912
up_read(&card->controls_rwsem);
901-
snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
902-
&control->id);
913+
snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, &id);
903914
return 0;
904915
}
905916
}
@@ -991,6 +1002,7 @@ static int snd_ctl_elem_unlock(struct snd_ctl_file *file,
9911002

9921003
struct user_element {
9931004
struct snd_ctl_elem_info info;
1005+
struct snd_card *card;
9941006
void *elem_data; /* element data */
9951007
unsigned long elem_data_size; /* size of element data in bytes */
9961008
void *tlv_data; /* TLV data */
@@ -1034,7 +1046,9 @@ static int snd_ctl_elem_user_get(struct snd_kcontrol *kcontrol,
10341046
{
10351047
struct user_element *ue = kcontrol->private_data;
10361048

1049+
mutex_lock(&ue->card->user_ctl_lock);
10371050
memcpy(&ucontrol->value, ue->elem_data, ue->elem_data_size);
1051+
mutex_unlock(&ue->card->user_ctl_lock);
10381052
return 0;
10391053
}
10401054

@@ -1043,10 +1057,12 @@ static int snd_ctl_elem_user_put(struct snd_kcontrol *kcontrol,
10431057
{
10441058
int change;
10451059
struct user_element *ue = kcontrol->private_data;
1046-
1060+
1061+
mutex_lock(&ue->card->user_ctl_lock);
10471062
change = memcmp(&ucontrol->value, ue->elem_data, ue->elem_data_size) != 0;
10481063
if (change)
10491064
memcpy(ue->elem_data, &ucontrol->value, ue->elem_data_size);
1065+
mutex_unlock(&ue->card->user_ctl_lock);
10501066
return change;
10511067
}
10521068

@@ -1066,19 +1082,32 @@ static int snd_ctl_elem_user_tlv(struct snd_kcontrol *kcontrol,
10661082
new_data = memdup_user(tlv, size);
10671083
if (IS_ERR(new_data))
10681084
return PTR_ERR(new_data);
1085+
mutex_lock(&ue->card->user_ctl_lock);
10691086
change = ue->tlv_data_size != size;
10701087
if (!change)
10711088
change = memcmp(ue->tlv_data, new_data, size);
10721089
kfree(ue->tlv_data);
10731090
ue->tlv_data = new_data;
10741091
ue->tlv_data_size = size;
1092+
mutex_unlock(&ue->card->user_ctl_lock);
10751093
} else {
1076-
if (! ue->tlv_data_size || ! ue->tlv_data)
1077-
return -ENXIO;
1078-
if (size < ue->tlv_data_size)
1079-
return -ENOSPC;
1094+
int ret = 0;
1095+
1096+
mutex_lock(&ue->card->user_ctl_lock);
1097+
if (!ue->tlv_data_size || !ue->tlv_data) {
1098+
ret = -ENXIO;
1099+
goto err_unlock;
1100+
}
1101+
if (size < ue->tlv_data_size) {
1102+
ret = -ENOSPC;
1103+
goto err_unlock;
1104+
}
10801105
if (copy_to_user(tlv, ue->tlv_data, ue->tlv_data_size))
1081-
return -EFAULT;
1106+
ret = -EFAULT;
1107+
err_unlock:
1108+
mutex_unlock(&ue->card->user_ctl_lock);
1109+
if (ret)
1110+
return ret;
10821111
}
10831112
return change;
10841113
}
@@ -1136,8 +1165,6 @@ static int snd_ctl_elem_add(struct snd_ctl_file *file,
11361165
struct user_element *ue;
11371166
int idx, err;
11381167

1139-
if (!replace && card->user_ctl_count >= MAX_USER_CONTROLS)
1140-
return -ENOMEM;
11411168
if (info->count < 1)
11421169
return -EINVAL;
11431170
access = info->access == 0 ? SNDRV_CTL_ELEM_ACCESS_READWRITE :
@@ -1146,21 +1173,16 @@ static int snd_ctl_elem_add(struct snd_ctl_file *file,
11461173
SNDRV_CTL_ELEM_ACCESS_TLV_READWRITE));
11471174
info->id.numid = 0;
11481175
memset(&kctl, 0, sizeof(kctl));
1149-
down_write(&card->controls_rwsem);
1150-
_kctl = snd_ctl_find_id(card, &info->id);
1151-
err = 0;
1152-
if (_kctl) {
1153-
if (replace)
1154-
err = snd_ctl_remove(card, _kctl);
1155-
else
1156-
err = -EBUSY;
1157-
} else {
1158-
if (replace)
1159-
err = -ENOENT;
1176+
1177+
if (replace) {
1178+
err = snd_ctl_remove_user_ctl(file, &info->id);
1179+
if (err)
1180+
return err;
11601181
}
1161-
up_write(&card->controls_rwsem);
1162-
if (err < 0)
1163-
return err;
1182+
1183+
if (card->user_ctl_count >= MAX_USER_CONTROLS)
1184+
return -ENOMEM;
1185+
11641186
memcpy(&kctl.id, &info->id, sizeof(info->id));
11651187
kctl.count = info->owner ? info->owner : 1;
11661188
access |= SNDRV_CTL_ELEM_ACCESS_USER;
@@ -1210,6 +1232,7 @@ static int snd_ctl_elem_add(struct snd_ctl_file *file,
12101232
ue = kzalloc(sizeof(struct user_element) + private_size, GFP_KERNEL);
12111233
if (ue == NULL)
12121234
return -ENOMEM;
1235+
ue->card = card;
12131236
ue->info = *info;
12141237
ue->info.access = 0;
12151238
ue->elem_data = (char *)ue + sizeof(*ue);
@@ -1321,8 +1344,9 @@ static int snd_ctl_tlv_ioctl(struct snd_ctl_file *file,
13211344
}
13221345
err = kctl->tlv.c(kctl, op_flag, tlv.length, _tlv->tlv);
13231346
if (err > 0) {
1347+
struct snd_ctl_elem_id id = kctl->id;
13241348
up_read(&card->controls_rwsem);
1325-
snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &kctl->id);
1349+
snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_TLV, &id);
13261350
return 0;
13271351
}
13281352
} else {

‎sound/core/init.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ int snd_card_new(struct device *parent, int idx, const char *xid,
232232
INIT_LIST_HEAD(&card->devices);
233233
init_rwsem(&card->controls_rwsem);
234234
rwlock_init(&card->ctl_files_rwlock);
235+
mutex_init(&card->user_ctl_lock);
235236
INIT_LIST_HEAD(&card->controls);
236237
INIT_LIST_HEAD(&card->ctl_files);
237238
spin_lock_init(&card->files_lock);

0 commit comments

Comments
 (0)