Fix memory leaks in callers of rtnl_talk()
Subsystems (2)
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
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.
--- ip/ipaddress.c | 2 ++ 1 file changed, 2 insertions(+)
ip/ipaddress.c +2 -0
@@ -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
PATCH 2/5 bridge: Fix memory leak when doing 'fdb get'
In reply to Benjamin PoirierWith 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().
--- bridge/fdb.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
bridge/fdb.c +5 -2
@@ -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
PATCH 3/5 mptcp: Fix memory leak when doing 'endpoint show'
In reply to Benjamin PoirierWith 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().
--- ip/ipmptcp.c | 1 + 1 file changed, 1 insertion(+)
ip/ipmptcp.c +1 -0
@@ -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
PATCH 4/5 mptcp: Fix memory leak when getting limits
In reply to Benjamin PoirierWhen 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().
--- ip/ipmptcp.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
ip/ipmptcp.c +7 -3
@@ -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
PATCH 5/5 ip neigh: Fix memory leak when doing 'get'
In reply to Benjamin PoirierWith 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().
--- ip/ipneigh.c | 2 ++ 1 file changed, 2 insertions(+)
ip/ipneigh.c +2 -0
@@ -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