iw: Fix memory leak if nla_put fails
PATCH 1/1 iw: Fix memory leak if nla_put fails
The NLA_PUT macro will automatically goto nla_put_failure if the underlying nla_put fails. This will in turn leak our malloced memory in both the scan and wowlan commands.
Fix that by not using the macro in the cases where we have allocated heap mem.
Signed-off-by Ola Olsson <[email protected]>
--- scan.c | 5 ++++- wowlan.c | 8 ++++++-- 2 files changed, 10 insertions(+), 3 deletions(-)
scan.c +4 -1
@@ -458,7 +458,10 @@ static int handle_scan(struct nl80211_state *state,memcpy (&tmpies[ies_len], meshid, meshid_len);free (meshid); } -NLA_PUT (msg,NL80211_ATTR_IE , ies_len + meshid_len, tmpies); +if (nla_put (msg,NL80211_ATTR_IE , ies_len + meshid_len, tmpies) <0 ) { +free (tmpies); + goto nla_put_failure; + }free (tmpies); }
wowlan.c +6 -2
@@ -159,8 +159,12 @@ static int wowlan_parse_tcp_file(struct nl_msg *msg, const char *fn) tok->offset =atoi (offs);memcpy (tok->token_stream, stream, stream_len); -NLA_PUT (msg,NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN , -sizeof (*tok) + stream_len, tok); +if (nla_put (msg,NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN , +sizeof (*tok) + stream_len, tok) <0 ) { +free (stream); +free (tok); + goto nla_put_failure; + }free (stream);free (tok); }else {
Signature · 1 line
1.7.9.5
Johannes Berg
AppliedQuoted 6 lines · Ola Olsson
The
NLA_PUTmacro will automatically gotonla_put_failureif the underlyingnla_putfails. This will in turn leak our malloced memory in both the scan and wowlan commands.
Fix that by not using the macro in the cases where we have allocated heap mem.
Applied, thanks.
Luckily the tool just exits right away so we don't have to care all that much :)
But of course it's still good to fix it since people can (and do) copy it into other code.
johannes