Skip to contentSkip to conversation

soundwire: fix bug in sdw_add_element_group_count found by syzkaller

PATCH RESEND Applied Baoli Zhang 2 participants 4 messages May 6 – May 7 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH RESEND 1/3 soundwire: fix bug in sdw_add_element_group_count found by syzkaller

From: "Baoli.Zhang" <[email protected]>

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 9026118f20e2 ("soundwire: Add generic bandwidth allocation algorithm")
---
 .../soundwire/generic_bandwidth_allocation.c  | 47 +++++++++----------
 1 file changed, 22 insertions(+), 25 deletions(-)
drivers/soundwire/generic_bandwidth_allocation.c +22 -25
index fb3970e12dac9..f016ad088a1db 100644
@@ -299,39 +299,36 @@ static int sdw_add_element_group_count(struct sdw_group *group,
 	int num = group->count;
 	int i;
 
-	for (i = 0; i <= num; i++) {
+	for (i = 0; i < num; i++) {
 		if (rate == group->rates[i] && lane == group->lanes[i])
-			break;
-
-		if (i != num)
-			continue;
-
-		if (group->count >= group->max_size) {
-			unsigned int *rates;
-			unsigned int *lanes;
+			return 0;
+	}
 
-			group->max_size += 1;
-			rates = krealloc(group->rates,
-					 (sizeof(int) * group->max_size),
-					 GFP_KERNEL);
-			if (!rates)
-				return -ENOMEM;
+	if (group->count >= group->max_size) {
+		unsigned int *rates;
+		unsigned int *lanes;
 
-			group->rates = rates;
+		group->max_size += 1;
+		rates = krealloc(group->rates,
+				 (sizeof(int) * group->max_size),
+				 GFP_KERNEL);
+		if (!rates)
+			return -ENOMEM;
 
-			lanes = krealloc(group->lanes,
-					 (sizeof(int) * group->max_size),
-					 GFP_KERNEL);
-			if (!lanes)
-				return -ENOMEM;
+		group->rates = rates;
 
-			group->lanes = lanes;
-		}
+		lanes = krealloc(group->lanes,
+				 (sizeof(int) * group->max_size),
+				 GFP_KERNEL);
+		if (!lanes)
+			return -ENOMEM;
 
-		group->rates[group->count] = rate;
-		group->lanes[group->count++] = lane;
+		group->lanes = lanes;
 	}
 
+	group->rates[group->count] = rate;
+	group->lanes[group->count++] = lane;
+
 	return 0;
 }
 
Signature · 1 line
2.43.0
maintainer Applied
#2 lore raw
Quoted 8 lines · Baoli Zhang

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])
        ...

[...]

Applied, thanks!

[1/3] soundwire: fix bug in sdw_add_element_group_count found by syzkaller

      commit: f772ff5a0e6758fd412803c09e03ba3bca5f5878
[2/3] soundwire: increase group->max_size after allocation
      commit: 654a7ae10b2ee6b07d5d9193c1c5465410781908
[3/3] soundwire: use krealloc_array to prevent integer overflow
      commit: 35a5ab8ef7f0f00b30eab9d917f3f0f4a2bec5d6

Best regards,

Signature · 1 line
~Vinod
#3 lore raw

PATCH RESEND 2/3 soundwire: increase group->max_size after allocation

In reply to a message not in the archive

From: "Baoli.Zhang" <[email protected]>

Only update group->max_size after both allocations succeed to avoid leaving the group's state inconsistent if one allocation fails.

---
 drivers/soundwire/generic_bandwidth_allocation.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/soundwire/generic_bandwidth_allocation.c +4 -3
index f016ad088a1db..cd9ccbaf0e46f 100644
@@ -308,9 +308,8 @@ static int sdw_add_element_group_count(struct sdw_group *group,
 		unsigned int *rates;
 		unsigned int *lanes;
 
-		group->max_size += 1;
 		rates = krealloc(group->rates,
-				 (sizeof(int) * group->max_size),
+				 sizeof(int) * (group->max_size + 1),
 				 GFP_KERNEL);
 		if (!rates)
 			return -ENOMEM;
@@ -318,12 +317,14 @@ static int sdw_add_element_group_count(struct sdw_group *group,
 		group->rates = rates;
 
 		lanes = krealloc(group->lanes,
-				 (sizeof(int) * group->max_size),
+				 sizeof(int) * (group->max_size + 1),
 				 GFP_KERNEL);
 		if (!lanes)
 			return -ENOMEM;
 
 		group->lanes = lanes;
+
+		group->max_size += 1;
 	}
 
 	group->rates[group->count] = rate;
Signature · 1 line
2.43.0
#4 lore raw

PATCH RESEND 3/3 soundwire: use krealloc_array to prevent integer overflow

In reply to a message not in the archive

From: "Baoli.Zhang" <[email protected]>

Replace the use of krealloc() with krealloc_array() in sdw_add_element_group_count to mitigate the risk of integer overflow during memory allocation size calculation.

---
 drivers/soundwire/generic_bandwidth_allocation.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/soundwire/generic_bandwidth_allocation.c +4 -6
index cd9ccbaf0e46f..3575d69ce1c50 100644
@@ -308,17 +308,15 @@ static int sdw_add_element_group_count(struct sdw_group *group,
 		unsigned int *rates;
 		unsigned int *lanes;
 
-		rates = krealloc(group->rates,
-				 sizeof(int) * (group->max_size + 1),
-				 GFP_KERNEL);
+		rates = krealloc_array(group->rates, group->max_size + 1,
+				       sizeof(*group->rates), GFP_KERNEL);
 		if (!rates)
 			return -ENOMEM;
 
 		group->rates = rates;
 
-		lanes = krealloc(group->lanes,
-				 sizeof(int) * (group->max_size + 1),
-				 GFP_KERNEL);
+		lanes = krealloc_array(group->lanes, group->max_size + 1,
+				       sizeof(*group->lanes), GFP_KERNEL);
 		if (!lanes)
 			return -ENOMEM;
 
Signature · 1 line
2.43.0