soundwire: fix error handling
PATCH 1/1 soundwire: fix error handling
From: Tom Rix <[email protected]>
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.
--- drivers/soundwire/stream.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/soundwire/stream.c +5 -3
@@ -717,6 +717,7 @@ static int sdw_bank_switch(struct sdw_bus *bus, int m_rt_count)kfree (wbuf); error_1:kfree (wr_msg); + bus->defer_msg.msg =NULL ;return ret; }
@@ -840,9 +841,10 @@ static int do_bank_switch(struct sdw_stream_runtime *stream) error: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); +if (bus->defer_msg.msg) { +kfree (bus->defer_msg.msg->buf); +kfree (bus->defer_msg.msg); + } } msg_unlock:
Signature · 1 line
2.18.1
Quoted 41 lines · On Sat, Aug 29, 2020 at 8:35 AM <[email protected]> wrote:
From: Tom Rix <[email protected]>
clang static analysis flags this problem
stream.c:844:9: warning: Use of memory after
it is freedkfree(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 <[email protected]>--- drivers/soundwire/stream.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)drivers/soundwire/stream.c +0
-0
drivers/soundwire/stream.c static int sdw_bank_switch(struct sdw_bus *bus, int m_rt_count) · line 720
Quoted 4 lines
drivers/soundwire/stream.c +1
-0@@ -717,6 +717,7 @@ static int sdw_bank_switch(struct sdw_bus *bus, int m_rt_count)kfree (wbuf); error_1:kfree (wr_msg); + bus->defer_msg.msg =NULL ;
This fix looks correct to me because L668 sets bus->defer_msg.msg = wr_msg;, but on error L719 frees wr_msg, so now bus->defer_msg.msg is a dangling pointer.
Quoted 4 lines
drivers/soundwire/stream.c +0
-0return ret; }
drivers/soundwire/stream.c static int do_bank_switch(struct sdw_stream_runtime *stream) · line 847
Quoted 10 lines
drivers/soundwire/stream.c +4
-3@@ -840,9 +841,10 @@ static int do_bank_switch(struct sdw_stream_runtime *stream) error: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); +if (bus->defer_msg.msg) { +kfree (bus->defer_msg.msg->buf); +kfree (bus->defer_msg.msg); + }
I'd prefer a conditional check for each, but sdw_ml_sync_bank_switch() has this same pattern, so it looks like the lifetime of these two match.
Thanks for the patch!
Quoted 6 lines
drivers/soundwire/stream.c +0
-1} msg_unlock: --2.18.1
Signature · 2 lines
Thanks, ~Nick Desaulniers
Quoted 6 lines · Nick Desaulniers
Quoted 4 lines · On Sat, Aug 29, 2020 at 8:35 AM <[email protected]> wrote:
From: Tom Rix <[email protected]>
clang static analysis flags this problem
Also, Tom, please use ./scripts/get_maintainer.pl on your patches to CC our mailing list; [email protected].
Signature · 2 lines
Thanks, ~Nick Desaulniers
Quoted 8 lines · Nick Desaulniers
Quoted 4 lines · Nick Desaulniers
Quoted 3 lines · On Sat, Aug 29, 2020 at 8:35 AM <[email protected]> wrote:
From: Tom Rix <[email protected]>
clang static analysis flags this problem
Also, Tom, please use
./scripts/get_maintainer.plon your patches to CC our mailing list; [email protected].
gcc is still doing the building, so it this appropriate ?
Asking because i have been sed-ing clang-build-linux out.
Tom
Quoted 8 lines · Tom Rix
Quoted 5 lines · Nick Desaulniers
Quoted 3 lines · Nick Desaulniers
Quoted 2 lines · On Sat, Aug 29, 2020 at 8:35 AM <[email protected]> wrote:
From: Tom Rix <[email protected]>
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
clang static analysis flags this problem
Also, Tom, please use
./scripts/get_maintainer.plon your patches to CC our mailing list; [email protected].gcc is still doing the building, so it this appropriate ?
Asking because i have been sed-ing clang-build-linux out.
ah, right, you can use --use-cc=clang for analyses of builds with clang. It doesn't hurt to include our mailing list, since we'd like to know if issues get reported with the analyzer itself.
Signature · 2 lines
Thanks, ~Nick Desaulniers
Quoted 9 lines · Nick Desaulniers
Quoted 8 lines · Tom Rix
Quoted 6 lines · Nick Desaulniers
Quoted 4 lines · Nick Desaulniers
Quoted 3 lines · On Sat, Aug 29, 2020 at 8:35 AM <[email protected]> wrote:
From: Tom Rix <[email protected]>
clang static analysis flags this problem
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Also, Tom, please use
./scripts/get_maintainer.plon your patches to CC our mailing list; [email protected].gcc is still doing the building, so it this appropriate ?
Asking because i have been sed-ing clang-build-linux out.
ah, right, you can use
--use-cc=clangfor analyses of builds with clang. It doesn't hurt to include our mailing list, since we'd like to know if issues get reported with the analyzer itself.
Ok, i'll include it.
The only real issue so far has been https://reviews.llvm.org/D83984
which fixes reporting on a couple of files.
Tom
Pierre-Louis Bossart
Reviewed-byQuoted 70 lines · Nick Desaulniers
Quoted 41 lines · On Sat, Aug 29, 2020 at 8:35 AM <[email protected]> wrote:
From: Tom Rix <[email protected]>
clang static analysis flags this problem
stream.c:844:9: warning: Use of memory after
it is freedkfree(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 <[email protected]>--- drivers/soundwire/stream.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)drivers/soundwire/stream.c +0
-0drivers/soundwire/stream.c static int sdw_bank_switch(struct sdw_bus *bus, int m_rt_count) · line 720
Quoted 4 lines
drivers/soundwire/stream.c +1
-0@@ -717,6 +717,7 @@ static int sdw_bank_switch(struct sdw_bus *bus, int m_rt_count)kfree (wbuf); error_1:kfree (wr_msg); + bus->defer_msg.msg =NULL ;This fix looks correct to me because L668 sets
bus->defer_msg.msg = wr_msg;, but on error L719 freeswr_msg, so nowbus->defer_msg.msgis a dangling pointer.Quoted 4 lines
drivers/soundwire/stream.c +0
-0return ret; }drivers/soundwire/stream.c static int do_bank_switch(struct sdw_stream_runtime *stream) · line 847
Quoted 10 lines
drivers/soundwire/stream.c +4
-3@@ -840,9 +841,10 @@ static int do_bank_switch(struct sdw_stream_runtime *stream) error: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); +if (bus->defer_msg.msg) { +kfree (bus->defer_msg.msg->buf); +kfree (bus->defer_msg.msg); + }I'd prefer a conditional check for each, but
sdw_ml_sync_bank_switch()has this same pattern, so it looks like the lifetime of these two match.
Quoted 1 line
Reviewed-by Nick Desaulniers <[email protected]>
Also looks good to me.
Vinod Koul
maintainerHello Tom,
Quoted 22 lines · On 29-08-20, 08:35, [email protected] wrote:
From: Tom Rix <[email protected]>
clang static analysis flags this problem
stream.c:844:9: warning: Use of memory after
it is freedkfree(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.
Quoted 6 lines
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.
The change looks good to me, but the title of patch should be revised.
The patch subject should describe the patch, in this case is setting pointer to null on cleanup, so an appropriate subject may be" "[PATCH]: soundwire: set defer_msg to null".
Please revise subject line and update including the ack/reviews received
Thanks
Quoted 35 lines
Fixes 99b8a5d608a6 ("soundwire: Add bank switch routine")Signed-off-by Tom Rix <[email protected]>--- drivers/soundwire/stream.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)drivers/soundwire/stream.c +5
-3@@ -717,6 +717,7 @@ static int sdw_bank_switch(struct sdw_bus *bus, int m_rt_count)kfree (wbuf); error_1:kfree (wr_msg); + bus->defer_msg.msg =NULL ;return ret; }@@ -840,9 +841,10 @@ static int do_bank_switch(struct sdw_stream_runtime *stream) error: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); +if (bus->defer_msg.msg) { +kfree (bus->defer_msg.msg->buf); +kfree (bus->defer_msg.msg); + } } msg_unlock:Signature · 1 line
2.18.1
Signature · 1 line
~Vinod
Quoted 33 lines · Vinod Koul
Hello Tom,
Quoted 22 lines · On 29-08-20, 08:35, [email protected] wrote:
From: Tom Rix <[email protected]>
clang static analysis flags this problem
stream.c:844:9: warning: Use of memory after
it is freedkfree(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.Quoted 6 lines
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.
Quoted 8 lines
The change looks good to me, but the title of patch should be revised.
The patch subject should describe the patch, in this case is setting pointer to null on cleanup, so an appropriate subject may be" "[PATCH]: soundwire: set
defer_msgto null".Please revise subject line and update including the ack/reviews received
That's fair, I think
soundwire: fix double free of dangling pointer
would be most precise.
Signature · 2 lines
Thanks, ~Nick Desaulniers