Blame SOURCES/0024-netlink-fix-leaked-instances-of-struct-nl_socket.patch

4f6fcd
From 5fad40a007b61092161d1e4fd0ffbbc3e7b8ba6c Mon Sep 17 00:00:00 2001
4f6fcd
From: Michal Kubecek <mkubecek@suse.cz>
4f6fcd
Date: Mon, 9 Nov 2020 13:30:57 +0100
4f6fcd
Subject: [PATCH 24/26] netlink: fix leaked instances of struct nl_socket
4f6fcd
4f6fcd
Valgrind detected memory leaks caused by missing cleanup of netlink
4f6fcd
context's ethnl_socket, ethnl2_socket and rtnl_socket. Also, contrary to
4f6fcd
its description, nlsock_done() does not free struct nl_socket itself.
4f6fcd
Fix nlsock_done() to free the structure and use it to dispose of sockets
4f6fcd
pointed to by struct nl_context members.
4f6fcd
4f6fcd
Fixes: 50efb3cdd2bb ("netlink: netlink socket wrapper and helpers")
4f6fcd
Fixes: 87307c30724d ("netlink: initialize ethtool netlink socket")
4f6fcd
Fixes: 7f3585b22a4b ("netlink: add handler for permaddr (-P)")
4f6fcd
Signed-off-by: Michal Kubecek <mkubecek@suse.cz>
4f6fcd
(cherry picked from commit d85f57499d75da5c98b73652488f3f62c6f6d8ea)
4f6fcd
---
4f6fcd
 netlink/netlink.c | 11 ++++++++---
4f6fcd
 netlink/nlsock.c  |  3 +++
4f6fcd
 2 files changed, 11 insertions(+), 3 deletions(-)
4f6fcd
4f6fcd
diff --git a/netlink/netlink.c b/netlink/netlink.c
4f6fcd
index 2a12bb8b1759..5677274c2fce 100644
4f6fcd
--- a/netlink/netlink.c
4f6fcd
+++ b/netlink/netlink.c
4f6fcd
@@ -281,11 +281,16 @@ out_free:
4f6fcd
 
4f6fcd
 static void netlink_done(struct cmd_context *ctx)
4f6fcd
 {
4f6fcd
-	if (!ctx->nlctx)
4f6fcd
+	struct nl_context *nlctx = ctx->nlctx;
4f6fcd
+
4f6fcd
+	if (!nlctx)
4f6fcd
 		return;
4f6fcd
 
4f6fcd
-	free(ctx->nlctx->ops_info);
4f6fcd
-	free(ctx->nlctx);
4f6fcd
+	nlsock_done(nlctx->ethnl_socket);
4f6fcd
+	nlsock_done(nlctx->ethnl2_socket);
4f6fcd
+	nlsock_done(nlctx->rtnl_socket);
4f6fcd
+	free(nlctx->ops_info);
4f6fcd
+	free(nlctx);
4f6fcd
 	ctx->nlctx = NULL;
4f6fcd
 	cleanup_all_strings();
4f6fcd
 }
4f6fcd
diff --git a/netlink/nlsock.c b/netlink/nlsock.c
4f6fcd
index ef31d8c33b29..0ec2738d81d2 100644
4f6fcd
--- a/netlink/nlsock.c
4f6fcd
+++ b/netlink/nlsock.c
4f6fcd
@@ -395,8 +395,11 @@ out_msgbuff:
4f6fcd
  */
4f6fcd
 void nlsock_done(struct nl_socket *nlsk)
4f6fcd
 {
4f6fcd
+	if (!nlsk)
4f6fcd
+		return;
4f6fcd
 	if (nlsk->sk)
4f6fcd
 		mnl_socket_close(nlsk->sk);
4f6fcd
 	msgbuff_done(&nlsk->msgbuff);
4f6fcd
 	memset(nlsk, '\0', sizeof(*nlsk));
4f6fcd
+	free(nlsk);
4f6fcd
 }
4f6fcd
-- 
4f6fcd
2.26.2
4f6fcd