Skip to contentSkip to conversation

Covscan: Fix potential memory leaks

PATCH New Phil Sutter 1 participant 6 messages Aug 17, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover Covscan: Fix potential memory leaks

This series collects patches from v1 which deal with potential memory leaks.

No changes to the actual patches, just splitting into smaller series.

Phil Sutter (5):
ipvrf: Fix error path of vrf_switch()
ifstat: Fix memleak in error case
ifstat: Fix memleak in dump_kern_db() for json output
ss: Fix potential memleak in unix_stats_print() tipc/bearer: Fix resource leak in error path

 ip/ipvrf.c    |  9 +++++----
 misc/ifstat.c | 12 +++++++++---
 misc/ss.c     |  4 +++-
 tipc/bearer.c |  3 +++
 4 files changed, 20 insertions(+), 8 deletions(-)
Signature · 1 line
2.13.1
#1 lore raw

PATCH v2 1/5 ipvrf: Fix error path of vrf_switch()

Apart from trying to close(-1), this also leaked memory.

---
 ip/ipvrf.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
ip/ipvrf.c +5 -4
index 0094cf8557cd7..e6fad32abd956 100644
@@ -372,12 +372,12 @@ static int vrf_switch(const char *name)
 
 	/* -1 on length to add '/' to the end */
 	if (ipvrf_get_netns(netns, sizeof(netns) - 1) < 0)
-		return -1;
+		goto out;
 
 	if (vrf_path(vpath, sizeof(vpath)) < 0) {
 		fprintf(stderr, "Failed to get base cgroup path: %s\n",
 			strerror(errno));
-		return -1;
+		goto out;
 	}
 
 	/* if path already ends in netns then don't add it again */
@@ -428,13 +428,14 @@ static int vrf_switch(const char *name)
 	snprintf(pid, sizeof(pid), "%d", getpid());
 	if (write(fd, pid, strlen(pid)) < 0) {
 		fprintf(stderr, "Failed to join cgroup\n");
-		goto out;
+		goto out2;
 	}
 
 	rc = 0;
+out2:
+	close(fd);
 out:
 	free(mnt);
-	close(fd);
 
 	return rc;
 }
Signature · 1 line
2.13.1
#2 lore raw

PATCH v2 2/5 ifstat: Fix memleak in error case

In reply to Phil Sutter
---
 misc/ifstat.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
misc/ifstat.c +3 -1
index a853ee6d7e3b3..8fa354265a9a1 100644
@@ -143,8 +143,10 @@ static int get_nlmsg_extended(const struct sockaddr_nl *who,
 		struct rtattr *attr;
 
 		attr = parse_rtattr_one_nested(sub_type, tb[filter_type]);
-		if (attr == NULL)
+		if (attr == NULL) {
+			free(n);
 			return 0;
+		}
 		memcpy(&n->val, RTA_DATA(attr), sizeof(n->val));
 	}
 	memset(&n->rate, 0, sizeof(n->rate));
Signature · 1 line
2.13.1
#3 lore raw

PATCH v2 3/5 ifstat: Fix memleak in dump_kern_db() for json output

In reply to Phil Sutter

Looks like this was forgotten when converting to common json output formatter.

Fixes fcc16c2287bf8 ("provide common json output formatter")
---
 misc/ifstat.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
misc/ifstat.c +6 -2
index 8fa354265a9a1..1be21703bf14c 100644
@@ -535,8 +535,12 @@ static void dump_kern_db(FILE *fp)
 		else
 			print_one_if(fp, n, n->val);
 	}
-	if (json_output)
-		fprintf(fp, "\n} }\n");
+	if (jw) {
+		jsonw_end_object(jw);
+
+		jsonw_end_object(jw);
+		jsonw_destroy(&jw);
+	}
 }
 
 static void dump_incr_db(FILE *fp)
Signature · 1 line
2.13.1
#4 lore raw

PATCH v2 4/5 ss: Fix potential memleak in unix_stats_print()

In reply to Phil Sutter
Fixes 2d0e538f3e1cd ("ss: Drop list traversal from unix_stats_print()")
---
 misc/ss.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
misc/ss.c +3 -1
index 07eecfa7a36db..34c6da5443642 100644
@@ -3169,8 +3169,10 @@ static int unix_show(struct filter *f)
 
 		if (name[0]) {
 			u->name = strdup(name);
-			if (!u->name)
+			if (!u->name) {
+				free(u);
 				break;
+			}
 		}
 
 		if (u->rport) {
Signature · 1 line
2.13.1
#5 lore raw

PATCH v2 5/5 tipc/bearer: Fix resource leak in error path

In reply to Phil Sutter
---
 tipc/bearer.c | 3 +++
 1 file changed, 3 insertions(+)
tipc/bearer.c +3 -0
index 810344f672af1..c3d4491f8f6ef 100644
@@ -163,6 +163,7 @@ static int nl_add_udp_enable_opts(struct nlmsghdr *nlh, struct opt *opts,
 	if (!remip) {
 		if (generate_multicast(loc->ai_family, buf, sizeof(buf))) {
 			fprintf(stderr, "Failed to generate multicast address\n");
+			freeaddrinfo(loc);
 			return -EINVAL;
 		}
 		remip = buf;
@@ -177,6 +178,8 @@ static int nl_add_udp_enable_opts(struct nlmsghdr *nlh, struct opt *opts,
 
 	if (rem->ai_family != loc->ai_family) {
 		fprintf(stderr, "UDP local and remote AF mismatch\n");
+		freeaddrinfo(rem);
+		freeaddrinfo(loc);
 		return -EINVAL;
 	}
 
Signature · 1 line
2.13.1