Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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: 99b8a5d608a6 ("soundwire: Add bank switch routine")
Signed-off-by: Tom Rix <trix@redhat.com>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Reviewed-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
Link: https://lore.kernel.org/r/20200902202650.14189-1-trix@redhat.com
Signed-off-by: Vinod Koul <vkoul@kernel.org>

authored by

Tom Rix and committed by
Vinod Koul
3fbbf214 f8d0168e

+5 -3
+5 -3
drivers/soundwire/stream.c
··· 717 717 kfree(wbuf); 718 718 error_1: 719 719 kfree(wr_msg); 720 + bus->defer_msg.msg = NULL; 720 721 return ret; 721 722 } 722 723 ··· 841 840 error: 842 841 list_for_each_entry(m_rt, &stream->master_list, stream_node) { 843 842 bus = m_rt->bus; 844 - 845 - kfree(bus->defer_msg.msg->buf); 846 - kfree(bus->defer_msg.msg); 843 + if (bus->defer_msg.msg) { 844 + kfree(bus->defer_msg.msg->buf); 845 + kfree(bus->defer_msg.msg); 846 + } 847 847 } 848 848 849 849 msg_unlock: