Skip to content

Commit 3fbbf21

Browse files
Tom Rixvinodkoul
authored andcommitted
soundwire: fix double free of dangling pointer
clang static analysis flags this problem stream.c:844:9: warning: Use of memory after it is freed kfree(bus->defer_msg.msg->buf); ^~~~~~~~~~~~~~~~~~~~~~~ This happens in an error handler cleaning up memory allocated for elements in a list. list_for_each_entry(m_rt, &stream->master_list, stream_node) { bus = m_rt->bus; kfree(bus->defer_msg.msg->buf); kfree(bus->defer_msg.msg); } And is triggered when the call to sdw_bank_switch() fails. There are a two problems. First, when sdw_bank_switch() fails, though it frees memory it does not clear bus's reference 'defer_msg.msg' to that memory. The second problem is the freeing msg->buf. In some cases msg will be NULL so this will dereference a null pointer. Need to check before freeing. Fixes: 99b8a5d ("soundwire: Add bank switch routine") Signed-off-by: Tom Rix <[email protected]> Reviewed-by: Nick Desaulniers <[email protected]> Reviewed-by: Pierre-Louis Bossart <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Vinod Koul <[email protected]>
1 parent f8d0168 commit 3fbbf21

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

drivers/soundwire/stream.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@ static int sdw_bank_switch(struct sdw_bus *bus, int m_rt_count)
717717
kfree(wbuf);
718718
error_1:
719719
kfree(wr_msg);
720+
bus->defer_msg.msg = NULL;
720721
return ret;
721722
}
722723

@@ -840,9 +841,10 @@ static int do_bank_switch(struct sdw_stream_runtime *stream)
840841
error:
841842
list_for_each_entry(m_rt, &stream->master_list, stream_node) {
842843
bus = m_rt->bus;
843-
844-
kfree(bus->defer_msg.msg->buf);
845-
kfree(bus->defer_msg.msg);
844+
if (bus->defer_msg.msg) {
845+
kfree(bus->defer_msg.msg->buf);
846+
kfree(bus->defer_msg.msg);
847+
}
846848
}
847849

848850
msg_unlock:

0 commit comments

Comments
 (0)