Skip to contentSkip to conversation

Fix memory leaks in callers of rtnl_talk()

PATCH New Benjamin Poirier 1 participant 6 messages Jul 10, 2022

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

cover Fix memory leaks in callers of rtnl_talk()

The answer returned by rtnl_recvmsg() must be freed by the caller. A few callers, through rtnl_talk(), mistakenly omit to free the result, leading to memory leaks. The impact of these leaks is minimal but they clutter the output of valgrind and similar tools and they lead to inconsistent code patterns.

Benjamin Poirier (5):
ip address: Fix memory leak when specifying device
bridge: Fix memory leak when doing 'fdb get'
mptcp: Fix memory leak when doing 'endpoint show'
mptcp: Fix memory leak when getting limits ip neigh: Fix memory leak when doing 'get'

 bridge/fdb.c   |  7 +++++--
 ip/ipaddress.c |  2 ++
 ip/ipmptcp.c   | 11 ++++++++---
 ip/ipneigh.c   |  2 ++
 4 files changed, 17 insertions(+), 5 deletions(-)
Signature · 1 line
2.36.1
#1 lore raw

PATCH 1/5 ip address: Fix memory leak when specifying device

Running a command like ip addr show dev lo under valgrind informs us that

32,768 bytes in 1 blocks are definitely lost in loss record 4 of 4
at 0x483577F: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so) by 0x16CBE2: rtnl_recvmsg (libnetlink.c:775)
by 0x16CF04: __rtnl_talk_iov (libnetlink.c:954)
by 0x16E257: __rtnl_talk (libnetlink.c:1059)
by 0x16E257: rtnl_talk (libnetlink.c:1065)
by 0x115CB1: ipaddr_link_get (ipaddress.c:1833)
by 0x11A0D1: ipaddr_list_flush_or_save (ipaddress.c:2030)
by 0x1152EB: do_cmd (ip.c:115)
by 0x114D6F: main (ip.c:321)

After calling store_nlmsg(), the original buffer should be freed. That is the pattern used elsewhere through the rtnl_dump_filter() call chain.

Fixes 884709785057 ("ip address: Set device index in dump request")
---
 ip/ipaddress.c | 2 ++
 1 file changed, 2 insertions(+)
ip/ipaddress.c +2 -0
index a288341c..59ef1e4b 100644
@@ -2030,8 +2030,10 @@ static int ipaddr_link_get(int index, struct nlmsg_chain *linfo)
 
 	if (store_nlmsg(answer, linfo) < 0) {
 		fprintf(stderr, "Failed to process link information\n");
+		free(answer);
 		return 1;
 	}
+	free(answer);
 
 	return 0;
 }
Signature · 1 line
2.36.1
#2 lore raw

PATCH 2/5 bridge: Fix memory leak when doing 'fdb get'

In reply to Benjamin Poirier

With the following command sequence:

ip link add br0 up type bridge
ip link add dummy0 up address 02:00:00:00:00:01 master br0 type dummy bridge fdb get 02:00:00:00:00:01 br br0

when running the last command under valgrind, it reports

32,768 bytes in 1 blocks are definitely lost in loss record 2 of 2
at 0x483F7B5: malloc (vg_replace_malloc.c:381)
by 0x11C1EC: rtnl_recvmsg (libnetlink.c:838)
by 0x11C4D1: __rtnl_talk_iov.constprop.0 (libnetlink.c:1040) by 0x11D994: __rtnl_talk (libnetlink.c:1141)
by 0x11D994: rtnl_talk (libnetlink.c:1147)
by 0x10D336: fdb_get (fdb.c:652)
by 0x48907FC: (below main) (libc-start.c:332)

Free the answer obtained from rtnl_talk().

Fixes 4ed5ad7bd3c6 ("bridge: fdb get support")
---
 bridge/fdb.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
bridge/fdb.c +5 -2
index 8912f092..08f6c72b 100644
@@ -571,6 +571,7 @@ static int fdb_get(int argc, char **argv)
 	char *addr = NULL;
 	short vlan = -1;
 	char *endptr;
+	int ret;
 
 	while (argc > 0) {
 		if ((strcmp(*argv, "brport") == 0) || strcmp(*argv, "dev") == 0) {
@@ -657,13 +658,15 @@ static int fdb_get(int argc, char **argv)
 	 * if -json was specified.
 	 */
 	new_json_obj(json);
+	ret = 0;
 	if (print_fdb(answer, stdout) < 0) {
 		fprintf(stderr, "An error :-)\n");
-		return -1;
+		ret = -1;
 	}
 	delete_json_obj();
+	free(answer);
 
-	return 0;
+	return ret;
 }
 
 int do_fdb(int argc, char **argv)
Signature · 1 line
2.36.1
#3 lore raw

PATCH 3/5 mptcp: Fix memory leak when doing 'endpoint show'

In reply to Benjamin Poirier

With the following command sequence:

ip mptcp endpoint add 127.0.0.1 id 1
ip mptcp endpoint show id 1

when running the last command under valgrind, it reports

32,768 bytes in 1 blocks are definitely lost in loss record 2 of 2
at 0x483F7B5: malloc (vg_replace_malloc.c:381)
by 0x17A0AC: rtnl_recvmsg (libnetlink.c:838)
by 0x17A391: __rtnl_talk_iov.constprop.0 (libnetlink.c:1040) by 0x17B854: __rtnl_talk (libnetlink.c:1141)
by 0x17B854: rtnl_talk (libnetlink.c:1147)
by 0x168A56: mptcp_addr_show (ipmptcp.c:334)
by 0x1174CB: do_cmd (ip.c:136)
by 0x116F7C: main (ip.c:324)

Free the answer obtained from rtnl_talk().

Fixes 7e0767cd862b ("add support for mptcp netlink interface")
---
 ip/ipmptcp.c | 1 +
 1 file changed, 1 insertion(+)
ip/ipmptcp.c +1 -0
index 0033f329..54817e46 100644
@@ -337,6 +337,7 @@ static int mptcp_addr_show(int argc, char **argv)
 	new_json_obj(json);
 	ret = print_mptcp_addr(answer, stdout);
 	delete_json_obj();
+	free(answer);
 	fflush(stdout);
 	return ret;
 }
Signature · 1 line
2.36.1
#4 lore raw

PATCH 4/5 mptcp: Fix memory leak when getting limits

In reply to Benjamin Poirier

When running the command ip mptcp limits under valgrind, it reports

32,768 bytes in 1 blocks are definitely lost in loss record 1 of 1
at 0x483F7B5: malloc (vg_replace_malloc.c:381)
by 0x17A0BC: rtnl_recvmsg (libnetlink.c:838)
by 0x17A3A1: __rtnl_talk_iov.constprop.0 (libnetlink.c:1040) by 0x17B864: __rtnl_talk (libnetlink.c:1141)
by 0x17B864: rtnl_talk (libnetlink.c:1147)
by 0x16837D: mptcp_limit_get_set (ipmptcp.c:436)
by 0x1174CB: do_cmd (ip.c:136)
by 0x116F7C: main (ip.c:324)

Free the answer obtained from rtnl_talk().

Fixes 7e0767cd862b ("add support for mptcp netlink interface")
---
 ip/ipmptcp.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
ip/ipmptcp.c +7 -3
index 54817e46..ce62ab9a 100644
@@ -436,9 +436,13 @@ static int mptcp_limit_get_set(int argc, char **argv, int cmd)
 	if (rtnl_talk(&genl_rth, &req.n, do_get ? &answer : NULL) < 0)
 		return -2;
 
-	if (do_get)
-		return print_mptcp_limit(answer, stdout);
-	return 0;
+	ret = 0;
+	if (do_get) {
+		ret = print_mptcp_limit(answer, stdout);
+		free(answer);
+	}
+
+	return ret;
 }
 
 static const char * const event_to_str[] = {
Signature · 1 line
2.36.1
#5 lore raw

PATCH 5/5 ip neigh: Fix memory leak when doing 'get'

In reply to Benjamin Poirier

With the following command sequence:

ip link add dummy0 type dummy
ip neigh add 192.168.0.1 dev dummy0
ip neigh get 192.168.0.1 dev dummy0

when running the last command under valgrind, it reports

32,768 bytes in 1 blocks are definitely lost in loss record 2 of 2
at 0x483F7B5: malloc (vg_replace_malloc.c:381)
by 0x17A0EC: rtnl_recvmsg (libnetlink.c:838)
by 0x17A3D1: __rtnl_talk_iov.constprop.0 (libnetlink.c:1040) by 0x17B894: __rtnl_talk (libnetlink.c:1141)
by 0x17B894: rtnl_talk (libnetlink.c:1147)
by 0x12E49B: ipneigh_get (ipneigh.c:728)
by 0x1174CB: do_cmd (ip.c:136)
by 0x116F7C: main (ip.c:324)

Free the answer obtained from rtnl_talk().

Fixes 62842362370b ("ipneigh: neigh get support")
---
 ip/ipneigh.c | 2 ++
 1 file changed, 2 insertions(+)
ip/ipneigh.c +2 -0
index 7facc399..61b0a4a2 100644
@@ -731,8 +731,10 @@ static int ipneigh_get(int argc, char **argv)
 	ipneigh_reset_filter(0);
 	if (print_neigh(answer, stdout) < 0) {
 		fprintf(stderr, "An error :-)\n");
+		free(answer);
 		return -1;
 	}
+	free(answer);
 
 	return 0;
 }
Signature · 1 line
2.36.1