naccyde / rpms / iproute

Forked from rpms/iproute 7 months ago
Clone

Blame SOURCES/0077-ss-Don-t-leak-fd-in-tcp_show_netlink_file.patch

36cfb7
From fa8b9f8fa8a6762bb0151e65a11eca9dca7aca83 Mon Sep 17 00:00:00 2001
36cfb7
From: Andrea Claudi <aclaudi@redhat.com>
36cfb7
Date: Mon, 29 Apr 2019 20:07:22 +0200
36cfb7
Subject: [PATCH] ss: Don't leak fd in tcp_show_netlink_file()
36cfb7
36cfb7
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1465646
36cfb7
Upstream Status: iproute2.git commit 4b45ae221e949
36cfb7
36cfb7
commit 4b45ae221e949b604d968a10d5d996c7c7cec1a6
36cfb7
Author: Phil Sutter <phil@nwl.cc>
36cfb7
Date:   Thu Aug 17 19:09:30 2017 +0200
36cfb7
36cfb7
    ss: Don't leak fd in tcp_show_netlink_file()
36cfb7
36cfb7
    Signed-off-by: Phil Sutter <phil@nwl.cc>
36cfb7
---
36cfb7
 misc/ss.c | 32 ++++++++++++++++++++------------
36cfb7
 1 file changed, 20 insertions(+), 12 deletions(-)
36cfb7
36cfb7
diff --git a/misc/ss.c b/misc/ss.c
36cfb7
index 86defc71fabc4..eb46e0c4b95fb 100644
36cfb7
--- a/misc/ss.c
36cfb7
+++ b/misc/ss.c
36cfb7
@@ -2764,41 +2764,44 @@ static int tcp_show_netlink_file(struct filter *f)
36cfb7
 {
36cfb7
 	FILE	*fp;
36cfb7
 	char	buf[16384];
36cfb7
+	int	err = -1;
36cfb7
 
36cfb7
 	if ((fp = fopen(getenv("TCPDIAG_FILE"), "r")) == NULL) {
36cfb7
 		perror("fopen($TCPDIAG_FILE)");
36cfb7
-		return -1;
36cfb7
+		return err;
36cfb7
 	}
36cfb7
 
36cfb7
 	while (1) {
36cfb7
-		int status, err;
36cfb7
+		int status, err2;
36cfb7
 		struct nlmsghdr *h = (struct nlmsghdr *)buf;
36cfb7
 		struct sockstat s = {};
36cfb7
 
36cfb7
 		status = fread(buf, 1, sizeof(*h), fp);
36cfb7
 		if (status < 0) {
36cfb7
 			perror("Reading header from $TCPDIAG_FILE");
36cfb7
-			return -1;
36cfb7
+			break;
36cfb7
 		}
36cfb7
 		if (status != sizeof(*h)) {
36cfb7
 			perror("Unexpected EOF reading $TCPDIAG_FILE");
36cfb7
-			return -1;
36cfb7
+			break;
36cfb7
 		}
36cfb7
 
36cfb7
 		status = fread(h+1, 1, NLMSG_ALIGN(h->nlmsg_len-sizeof(*h)), fp);
36cfb7
 
36cfb7
 		if (status < 0) {
36cfb7
 			perror("Reading $TCPDIAG_FILE");
36cfb7
-			return -1;
36cfb7
+			break;
36cfb7
 		}
36cfb7
 		if (status + sizeof(*h) < h->nlmsg_len) {
36cfb7
 			perror("Unexpected EOF reading $TCPDIAG_FILE");
36cfb7
-			return -1;
36cfb7
+			break;
36cfb7
 		}
36cfb7
 
36cfb7
 		/* The only legal exit point */
36cfb7
-		if (h->nlmsg_type == NLMSG_DONE)
36cfb7
-			return 0;
36cfb7
+		if (h->nlmsg_type == NLMSG_DONE) {
36cfb7
+			err = 0;
36cfb7
+			break;
36cfb7
+		}
36cfb7
 
36cfb7
 		if (h->nlmsg_type == NLMSG_ERROR) {
36cfb7
 			struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
36cfb7
@@ -2809,7 +2812,7 @@ static int tcp_show_netlink_file(struct filter *f)
36cfb7
 				errno = -err->error;
36cfb7
 				perror("TCPDIAG answered");
36cfb7
 			}
36cfb7
-			return -1;
36cfb7
+			break;
36cfb7
 		}
36cfb7
 
36cfb7
 		parse_diag_msg(h, &s);
36cfb7
@@ -2818,10 +2821,15 @@ static int tcp_show_netlink_file(struct filter *f)
36cfb7
 		if (f && f->f && run_ssfilter(f->f, &s) == 0)
36cfb7
 			continue;
36cfb7
 
36cfb7
-		err = inet_show_sock(h, &s);
36cfb7
-		if (err < 0)
36cfb7
-			return err;
36cfb7
+		err2 = inet_show_sock(h, &s);
36cfb7
+		if (err2 < 0) {
36cfb7
+			err = err2;
36cfb7
+			break;
36cfb7
+		}
36cfb7
 	}
36cfb7
+
36cfb7
+	fclose(fp);
36cfb7
+	return err;
36cfb7
 }
36cfb7
 
36cfb7
 static int tcp_show(struct filter *f)
36cfb7
-- 
e138d9
2.21.0
36cfb7