Blame SOURCES/0101-ifstat-nstat-Check-fdopen-return-value.patch

99be8f
From 5ae0f31d9c5d40dbf9eaf00435b9df1968109f5e Mon Sep 17 00:00:00 2001
99be8f
From: Andrea Claudi <aclaudi@redhat.com>
99be8f
Date: Mon, 29 Apr 2019 20:08:07 +0200
99be8f
Subject: [PATCH] ifstat, nstat: Check fdopen() return value
99be8f
99be8f
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1465646
99be8f
Upstream Status: iproute2.git commit 6d02518fdc37e
99be8f
99be8f
commit 6d02518fdc37eb12abff67b6f8c741fbd81dce72
99be8f
Author: Phil Sutter <phil@nwl.cc>
99be8f
Date:   Thu Aug 24 11:46:31 2017 +0200
99be8f
99be8f
    ifstat, nstat: Check fdopen() return value
99be8f
99be8f
    Prevent passing NULL FILE pointer to fgets() later.
99be8f
99be8f
    Fix both tools in a single patch since the code changes are basically
99be8f
    identical.
99be8f
99be8f
    Signed-off-by: Phil Sutter <phil@nwl.cc>
99be8f
---
99be8f
 misc/ifstat.c | 16 +++++++++++-----
99be8f
 misc/nstat.c  | 16 +++++++++++-----
99be8f
 2 files changed, 22 insertions(+), 10 deletions(-)
99be8f
99be8f
diff --git a/misc/ifstat.c b/misc/ifstat.c
99be8f
index 1be21703bf14c..ac3eff6b870a9 100644
99be8f
--- a/misc/ifstat.c
99be8f
+++ b/misc/ifstat.c
99be8f
@@ -992,12 +992,18 @@ int main(int argc, char *argv[])
99be8f
 	    && verify_forging(fd) == 0) {
99be8f
 		FILE *sfp = fdopen(fd, "r");
99be8f
 
99be8f
-		load_raw_table(sfp);
99be8f
-		if (hist_db && source_mismatch) {
99be8f
-			fprintf(stderr, "ifstat: history is stale, ignoring it.\n");
99be8f
-			hist_db = NULL;
99be8f
+		if (!sfp) {
99be8f
+			fprintf(stderr, "ifstat: fdopen failed: %s\n",
99be8f
+				strerror(errno));
99be8f
+			close(fd);
99be8f
+		} else  {
99be8f
+			load_raw_table(sfp);
99be8f
+			if (hist_db && source_mismatch) {
99be8f
+				fprintf(stderr, "ifstat: history is stale, ignoring it.\n");
99be8f
+				hist_db = NULL;
99be8f
+			}
99be8f
+			fclose(sfp);
99be8f
 		}
99be8f
-		fclose(sfp);
99be8f
 	} else {
99be8f
 		if (fd >= 0)
99be8f
 			close(fd);
99be8f
diff --git a/misc/nstat.c b/misc/nstat.c
99be8f
index 1212b1f2c8128..a4dd405d43a93 100644
99be8f
--- a/misc/nstat.c
99be8f
+++ b/misc/nstat.c
99be8f
@@ -706,12 +706,18 @@ int main(int argc, char *argv[])
99be8f
 	    && verify_forging(fd) == 0) {
99be8f
 		FILE *sfp = fdopen(fd, "r");
99be8f
 
99be8f
-		load_good_table(sfp);
99be8f
-		if (hist_db && source_mismatch) {
99be8f
-			fprintf(stderr, "nstat: history is stale, ignoring it.\n");
99be8f
-			hist_db = NULL;
99be8f
+		if (!sfp) {
99be8f
+			fprintf(stderr, "nstat: fdopen failed: %s\n",
99be8f
+				strerror(errno));
99be8f
+			close(fd);
99be8f
+		} else {
99be8f
+			load_good_table(sfp);
99be8f
+			if (hist_db && source_mismatch) {
99be8f
+				fprintf(stderr, "nstat: history is stale, ignoring it.\n");
99be8f
+				hist_db = NULL;
99be8f
+			}
99be8f
+			fclose(sfp);
99be8f
 		}
99be8f
-		fclose(sfp);
99be8f
 	} else {
99be8f
 		if (fd >= 0)
99be8f
 			close(fd);
99be8f
-- 
99be8f
2.20.1
99be8f