Skip to contentSkip to conversation

soundwire: fix double free of pointer

PATCH In review Daniil Dulov 2 participants 2 messages Feb 11, 2024 – Feb 12, 2024 SoundWire

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 soundwire: fix double free of pointer

If sdw_ml_sync_bank_switch() returns error not on the first iteration, it leads to freeing prevously freed memory. So, set the pointer to NULL after each successful bank switch.

---
 drivers/soundwire/stream.c | 1 +
 1 file changed, 1 insertion(+)
drivers/soundwire/stream.c +1 -0
index 304ff2ee7d75..d650e6f0f8e7 100644
@@ -833,6 +833,7 @@ static int do_bank_switch(struct sdw_stream_runtime *stream)
 				"multi link bank switch failed: %d\n", ret);
 			goto error;
 		}
+		bus->defer_msg.msg = NULL;
 
 		if (multi_link)
 			mutex_unlock(&bus->msg_lock);
Signature · 1 line
2.25.1
#1 lore raw
Quoted 14 lines · Daniil Dulov

If sdw_ml_sync_bank_switch() returns error not on the first iteration, it leads to freeing prevously freed memory. So, set the pointer to NULL after each successful bank switch.

---
 drivers/soundwire/stream.c | 1 +
 1 file changed, 1 insertion(+)
drivers/soundwire/stream.c +0 -0
index 304ff2ee7d75..d650e6f0f8e7 100644

drivers/soundwire/stream.c static int do_bank_switch(struct sdw_stream_runtime *stream) · line 839

Quoted 7 lines
drivers/soundwire/stream.c +1 -0
@@ -833,6 +833,7 @@ static int do_bank_switch(struct sdw_stream_runtime *stream)
 				"multi link bank switch failed: %d\n", ret);
 			goto error;
 		}
+		bus->defer_msg.msg = NULL;
 
 		if (multi_link)
 			mutex_unlock(&bus->msg_lock);

Not following what the issue is...

On success, sdw_ml_sync_bank_switch() frees the buffers with

	if (bus->defer_msg.msg) {
		kfree(bus->defer_msg.msg->buf);
		kfree(bus->defer_msg.msg);
		bus->defer_msg.msg = NULL;
	}

So if there is an issue on the second iteration, then the loop will detect already freed memory in the previous iteration and skip it:

                /* Check if bank switch was successful */
		ret = sdw_ml_sync_bank_switch(bus);
		if (ret < 0) {
			dev_err(bus->dev,
				"multi link bank switch failed: %d\n", ret);
			goto error;
		}

error:

	list_for_each_entry(m_rt, &stream->master_list, stream_node) {
		bus = m_rt->bus;
		if (bus->defer_msg.msg) { <<<< TEST FOR FREED MEMORY
			kfree(bus->defer_msg.msg->buf);
			kfree(bus->defer_msg.msg);
			bus->defer_msg.msg = NULL;
		}
	}

It could very well be that I need more coffee on this post-SuperBowl Monday morning, but I just don't see the problem.