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

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