Skip to content

Commit f772ff5

Browse files
Baoli.Zhangvinodkoul
authored andcommitted
soundwire: fix bug in sdw_add_element_group_count found by syzkaller
The original implementation caused an out-of-bounds memory access in the sdw_add_element_group_count for-loop when i == num. for (i = 0; i <= num; i++) { if (rate == group->rates[i] && lane == group->lanes[i]) ... To fix this error, the function now checks for existing rate/lane entries in the group(a function parameter) using a for-loop before adding them. No functional changes apart from this fix. Fixes: 9026118 ("soundwire: Add generic bandwidth allocation algorithm") Reviewed-by: Bard Liao <yung-chuan.liao@linux.intel.com> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Signed-off-by: Baoli.Zhang <baoli.zhang@linux.intel.com> Link: https://patch.msgid.link/20260506055039.3751028-2-baoli.zhang@linux.intel.com Signed-off-by: Vinod Koul <vkoul@kernel.org>
1 parent c368dd5 commit f772ff5

1 file changed

Lines changed: 22 additions & 25 deletions

File tree

drivers/soundwire/generic_bandwidth_allocation.c

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -299,39 +299,36 @@ static int sdw_add_element_group_count(struct sdw_group *group,
299299
int num = group->count;
300300
int i;
301301

302-
for (i = 0; i <= num; i++) {
302+
for (i = 0; i < num; i++) {
303303
if (rate == group->rates[i] && lane == group->lanes[i])
304-
break;
305-
306-
if (i != num)
307-
continue;
308-
309-
if (group->count >= group->max_size) {
310-
unsigned int *rates;
311-
unsigned int *lanes;
304+
return 0;
305+
}
312306

313-
group->max_size += 1;
314-
rates = krealloc(group->rates,
315-
(sizeof(int) * group->max_size),
316-
GFP_KERNEL);
317-
if (!rates)
318-
return -ENOMEM;
307+
if (group->count >= group->max_size) {
308+
unsigned int *rates;
309+
unsigned int *lanes;
319310

320-
group->rates = rates;
311+
group->max_size += 1;
312+
rates = krealloc(group->rates,
313+
(sizeof(int) * group->max_size),
314+
GFP_KERNEL);
315+
if (!rates)
316+
return -ENOMEM;
321317

322-
lanes = krealloc(group->lanes,
323-
(sizeof(int) * group->max_size),
324-
GFP_KERNEL);
325-
if (!lanes)
326-
return -ENOMEM;
318+
group->rates = rates;
327319

328-
group->lanes = lanes;
329-
}
320+
lanes = krealloc(group->lanes,
321+
(sizeof(int) * group->max_size),
322+
GFP_KERNEL);
323+
if (!lanes)
324+
return -ENOMEM;
330325

331-
group->rates[group->count] = rate;
332-
group->lanes[group->count++] = lane;
326+
group->lanes = lanes;
333327
}
334328

329+
group->rates[group->count] = rate;
330+
group->lanes[group->count++] = lane;
331+
335332
return 0;
336333
}
337334

0 commit comments

Comments
 (0)