Skip to contentSkip to conversation

iw: Fix memory leak if nla_put fails

PATCH Applied Ola Olsson 2 participants 2 messages Oct 23, 2015 – Nov 3, 2015 Arm/actions semi architecture

lore
Share & tools
lore

Ola Olsson

author
#0 lore raw

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.

---
 scan.c   |    5 ++++-
 wowlan.c |    8 ++++++--
 2 files changed, 10 insertions(+), 3 deletions(-)
scan.c +4 -1
index 8762784..06c4255 100644
@@ -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
index e1d3750..c30eab7 100644
@@ -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
#1 lore raw
Quoted 6 lines · Ola Olsson

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.

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