Lukáš Zaoral ef63c0
From 398585bfe7b1340d41143f50dfc868ef8ab9a5e4 Mon Sep 17 00:00:00 2001
Lukáš Zaoral ef63c0
From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Zaoral?= <lzaoral@redhat.com>
Lukáš Zaoral ef63c0
Date: Tue, 21 Feb 2023 12:43:42 +0100
Lukáš Zaoral ef63c0
Subject: [PATCH] Tools that take --dec=X option should only accept digits
Lukáš Zaoral ef63c0
MIME-Version: 1.0
Lukáš Zaoral ef63c0
Content-Type: text/plain; charset=UTF-8
Lukáš Zaoral ef63c0
Content-Transfer-Encoding: 8bit
Lukáš Zaoral ef63c0
Lukáš Zaoral ef63c0
Right now the argument of --dec is passed to atoi(3) which returns 0
Lukáš Zaoral ef63c0
on conversion error.  Therefore, --dec=A was not rejected and was
Lukáš Zaoral ef63c0
equivalent to --dec=0 by mistake.
Lukáš Zaoral ef63c0
Lukáš Zaoral ef63c0
Signed-off-by: Lukáš Zaoral <lzaoral@redhat.com>
Lukáš Zaoral ef63c0
---
Lukáš Zaoral ef63c0
 cifsiostat.c | 5 +++++
Lukáš Zaoral ef63c0
 iostat.c     | 5 +++++
Lukáš Zaoral ef63c0
 mpstat.c     | 5 +++++
Lukáš Zaoral ef63c0
 pidstat.c    | 5 +++++
Lukáš Zaoral ef63c0
 sar.c        | 6 ++++++
Lukáš Zaoral ef63c0
 5 files changed, 26 insertions(+)
Lukáš Zaoral ef63c0
Lukáš Zaoral ef63c0
diff --git a/cifsiostat.c b/cifsiostat.c
Lukáš Zaoral ef63c0
index 375b1ff..849583b 100644
Lukáš Zaoral ef63c0
--- a/cifsiostat.c
Lukáš Zaoral ef63c0
+++ b/cifsiostat.c
Lukáš Zaoral ef63c0
@@ -522,6 +522,11 @@ int main(int argc, char **argv)
Lukáš Zaoral ef63c0
 		}
Lukáš Zaoral ef63c0
 
Lukáš Zaoral ef63c0
 		else if (!strncmp(argv[opt], "--dec=", 6) && (strlen(argv[opt]) == 7)) {
Lukáš Zaoral ef63c0
+			/* Check that the argument is a digit */
Lukáš Zaoral ef63c0
+			if (!isdigit(argv[opt][6])) {
Lukáš Zaoral ef63c0
+				usage(argv[0]);
Lukáš Zaoral ef63c0
+			}
Lukáš Zaoral ef63c0
+
Lukáš Zaoral ef63c0
 			/* Get number of decimal places */
Lukáš Zaoral ef63c0
 			dplaces_nr = atoi(argv[opt] + 6);
Lukáš Zaoral ef63c0
 			if ((dplaces_nr < 0) || (dplaces_nr > 2)) {
Lukáš Zaoral ef63c0
diff --git a/iostat.c b/iostat.c
Lukáš Zaoral ef63c0
index 1d7ea3c..7ac56ef 100644
Lukáš Zaoral ef63c0
--- a/iostat.c
Lukáš Zaoral ef63c0
+++ b/iostat.c
Lukáš Zaoral ef63c0
@@ -2142,6 +2142,11 @@ int main(int argc, char **argv)
Lukáš Zaoral ef63c0
 #endif
Lukáš Zaoral ef63c0
 
Lukáš Zaoral ef63c0
 		else if (!strncmp(argv[opt], "--dec=", 6) && (strlen(argv[opt]) == 7)) {
Lukáš Zaoral ef63c0
+			/* Check that the argument is a digit */
Lukáš Zaoral ef63c0
+			if (!isdigit(argv[opt][6])) {
Lukáš Zaoral ef63c0
+				usage(argv[0]);
Lukáš Zaoral ef63c0
+			}
Lukáš Zaoral ef63c0
+
Lukáš Zaoral ef63c0
 			/* Get number of decimal places */
Lukáš Zaoral ef63c0
 			dplaces_nr = atoi(argv[opt] + 6);
Lukáš Zaoral ef63c0
 			if ((dplaces_nr < 0) || (dplaces_nr > 2)) {
Lukáš Zaoral ef63c0
diff --git a/mpstat.c b/mpstat.c
Lukáš Zaoral ef63c0
index 90d6226..5045e45 100644
Lukáš Zaoral ef63c0
--- a/mpstat.c
Lukáš Zaoral ef63c0
+++ b/mpstat.c
Lukáš Zaoral ef63c0
@@ -2221,6 +2221,11 @@ int main(int argc, char **argv)
Lukáš Zaoral ef63c0
 	while (++opt < argc) {
Lukáš Zaoral ef63c0
 
Lukáš Zaoral ef63c0
 		if (!strncmp(argv[opt], "--dec=", 6) && (strlen(argv[opt]) == 7)) {
Lukáš Zaoral ef63c0
+			/* Check that the argument is a digit */
Lukáš Zaoral ef63c0
+			if (!isdigit(argv[opt][6])) {
Lukáš Zaoral ef63c0
+				usage(argv[0]);
Lukáš Zaoral ef63c0
+			}
Lukáš Zaoral ef63c0
+
Lukáš Zaoral ef63c0
 			/* Get number of decimal places */
Lukáš Zaoral ef63c0
 			dplaces_nr = atoi(argv[opt] + 6);
Lukáš Zaoral ef63c0
 			if ((dplaces_nr < 0) || (dplaces_nr > 2)) {
Lukáš Zaoral ef63c0
diff --git a/pidstat.c b/pidstat.c
Lukáš Zaoral ef63c0
index 21fed6c..d550605 100644
Lukáš Zaoral ef63c0
--- a/pidstat.c
Lukáš Zaoral ef63c0
+++ b/pidstat.c
Lukáš Zaoral ef63c0
@@ -2633,6 +2633,11 @@ int main(int argc, char **argv)
Lukáš Zaoral ef63c0
 		}
Lukáš Zaoral ef63c0
 
Lukáš Zaoral ef63c0
 		else if (!strncmp(argv[opt], "--dec=", 6) && (strlen(argv[opt]) == 7)) {
Lukáš Zaoral ef63c0
+			/* Check that the argument is a digit */
Lukáš Zaoral ef63c0
+			if (!isdigit(argv[opt][6])) {
Lukáš Zaoral ef63c0
+				usage(argv[0]);
Lukáš Zaoral ef63c0
+			}
Lukáš Zaoral ef63c0
+
Lukáš Zaoral ef63c0
 			/* Get number of decimal places */
Lukáš Zaoral ef63c0
 			dplaces_nr = atoi(argv[opt] + 6);
Lukáš Zaoral ef63c0
 			if ((dplaces_nr < 0) || (dplaces_nr > 2)) {
Lukáš Zaoral ef63c0
diff --git a/sar.c b/sar.c
Lukáš Zaoral ef63c0
index 4f06172..7691793 100644
Lukáš Zaoral ef63c0
--- a/sar.c
Lukáš Zaoral ef63c0
+++ b/sar.c
Lukáš Zaoral ef63c0
@@ -28,6 +28,7 @@
Lukáš Zaoral ef63c0
 #include <errno.h>
Lukáš Zaoral ef63c0
 #include <signal.h>
Lukáš Zaoral ef63c0
 #include <sys/stat.h>
Lukáš Zaoral ef63c0
+#include <ctype.h>
Lukáš Zaoral ef63c0
 
Lukáš Zaoral ef63c0
 #include "version.h"
Lukáš Zaoral ef63c0
 #include "sa.h"
Lukáš Zaoral ef63c0
@@ -1372,6 +1373,11 @@ int main(int argc, char **argv)
Lukáš Zaoral ef63c0
 		}
Lukáš Zaoral ef63c0
 
Lukáš Zaoral ef63c0
 		else if (!strncmp(argv[opt], "--dec=", 6) && (strlen(argv[opt]) == 7)) {
Lukáš Zaoral ef63c0
+			/* Check that the argument is a digit */
Lukáš Zaoral ef63c0
+			if (!isdigit(argv[opt][6])) {
Lukáš Zaoral ef63c0
+				usage(argv[0]);
Lukáš Zaoral ef63c0
+			}
Lukáš Zaoral ef63c0
+
Lukáš Zaoral ef63c0
 			/* Get number of decimal places */
Lukáš Zaoral ef63c0
 			dplaces_nr = atoi(argv[opt] + 6);
Lukáš Zaoral ef63c0
 			if ((dplaces_nr < 0) || (dplaces_nr > 2)) {
Lukáš Zaoral ef63c0
-- 
Lukáš Zaoral ef63c0
2.39.2
Lukáš Zaoral ef63c0