Skip to contentSkip to conversation

cpupower: Fix cpuidle_set to accept only numeric values for idle-set operation.

PATCH Superseded Korrapati Likhitha 2 participants 3 messages Apr 10, 2023 – Jul 14, 2023 CPU power monitoring

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 cpupower: Fix cpuidle_set to accept only numeric values for idle-set operation.

From: Likhitha Korrapati <[email protected]>

For both the d and e options in 'cpupower idle_set' command, an atoi() conversion is done without checking if the input argument is all numeric. So, an atoi conversion is done on any character provided as input and the CPU idle_set operation continues with that integer value, which may not be what is intended or entirely correct.

The output of cpuidle-set before patch is as follows:

[root@xxx cpupower]# cpupower idle-set -e 1$
Idlestate 1 enabled on CPU 0
[snip]
Idlestate 1 enabled on CPU 47

[root@xxx cpupower]# cpupower idle-set -e 11
Idlestate 11 not available on CPU 0
[snip]
Idlestate 11 not available on CPU 47

[root@xxx cpupower]# cpupower idle-set -d 12
Idlestate 12 not available on CPU 0
[snip]
Idlestate 12 not available on CPU 47

[root@xxx cpupower]# cpupower idle-set -d qw
Idlestate 0 disabled on CPU 0
[snip]
Idlestate 0 disabled on CPU 47

This patch adds a check for both d and e options in cpuidle-set.c to see that the idle_set value is all numeric before doing a string-to-int conversion.

The output of cpuidle-set after the patch is as below:

[root@xxx cpupower]# ./cpupower idle-set -e 1$
Bad idle_set value: 1$. Integer expected

[root@xxx cpupower]# ./cpupower idle-set -e 11
Idlestate 11 not available on CPU 0
[snip]
Idlestate 11 not available on CPU 47

[root@xxx cpupower]# ./cpupower idle-set -d 12
Idlestate 12 not available on CPU 0
[snip]
Idlestate 12 not available on CPU 47

[root@xxx cpupower]# ./cpupower idle-set -d qw
Bad idle_set value: qw. Integer expected

---

** changes since v1 [1] **

- Addressed reviewed comments from v1.
- Slightly reworded the commit for clarity.

[1] https://lore.kernel.org/all/[email protected]/

 tools/power/cpupower/utils/cpuidle-set.c     | 25 ++++++++++++++++----
 tools/power/cpupower/utils/helpers/helpers.h |  8 +++++++
 tools/power/cpupower/utils/helpers/misc.c    | 17 +++++++++++++
 3 files changed, 45 insertions(+), 5 deletions(-)
tools/power/cpupower/utils/cpuidle-set.c +20 -5
index 46158928f9ad..1bfe16d27c2d 100644
@@ -47,7 +47,12 @@ int cmd_idle_set(int argc, char **argv)
 				break;
 			}
 			param = ret;
-			idlestate = atoi(optarg);
+			if (is_stringnumeric(optarg))
+				idlestate = atoi(optarg);
+			else {
+				printf(_("Bad idle_set value: %s. Integer expected\n"), optarg);
+				exit(EXIT_FAILURE);
+			}
 			break;
 		case 'e':
 			if (param) {
@@ -56,7 +61,12 @@ int cmd_idle_set(int argc, char **argv)
 				break;
 			}
 			param = ret;
-			idlestate = atoi(optarg);
+			if (is_stringnumeric(optarg))
+				idlestate = atoi(optarg);
+			else {
+				printf(_("Bad idle_set value: %s. Integer expected\n"), optarg);
+				exit(EXIT_FAILURE);
+			}
 			break;
 		case 'D':
 			if (param) {
@@ -65,9 +75,14 @@ int cmd_idle_set(int argc, char **argv)
 				break;
 			}
 			param = ret;
-			latency = strtoull(optarg, &endptr, 10);
-			if (*endptr != '\0') {
-				printf(_("Bad latency value: %s\n"), optarg);
+			if (is_stringnumeric(optarg)) {
+				latency = strtoull(optarg, &endptr, 10);
+				if (*endptr != '\0') {
+					printf(_("Bad latency value: %s\n"), optarg);
+					exit(EXIT_FAILURE);
+				}
+			} else {
+				printf(_("Bad idle_set value: %s. Integer expected\n"), optarg);
 				exit(EXIT_FAILURE);
 			}
 			break;
tools/power/cpupower/utils/helpers/helpers.h +8 -0
index 96e4bede078b..9977f0773986 100644
@@ -208,3 +208,11 @@ void print_offline_cpus(void);
 void print_speed(unsigned long speed, int no_rounding);
 
 #endif /* __CPUPOWERUTILS_HELPERS__ */
+
+/*
+ * CPU idle-set
+ */
+int is_stringnumeric(char *arg);
+/*
+ * CPU idle-set
+ */
tools/power/cpupower/utils/helpers/misc.c +17 -0
index 9547b29254a7..8ec47c3c138e 100644
@@ -4,6 +4,7 @@
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
+#include <ctype.h>
 
 #include "helpers/helpers.h"
 #include "helpers/sysfs.h"
@@ -204,3 +205,19 @@ void print_speed(unsigned long speed, int no_rounding)
 		}
 	}
 }
+
+/*
+ * is_stringnumeric
+ *
+ * To check if the given string has all numericals
+ */
+int is_stringnumeric(char *arg)
+{
+	size_t i = 0;
+
+	for (i = 0; arg[i] ; i++) {
+		if (!isdigit(arg[i]))
+			return 0;
+	}
+	return 1;
+}
Signature · 1 line
2.31.1
maintainer
#1 lore raw
Quoted 76 lines · Korrapati Likhitha

From: Likhitha Korrapati <[email protected]>

For both the d and e options in 'cpupower idle_set' command, an atoi() conversion is done without checking if the input argument is all numeric. So, an atoi conversion is done on any character provided as input and the CPU idle_set operation continues with that integer value, which may not be what is intended or entirely correct.

The output of cpuidle-set before patch is as follows:

[root@xxx cpupower]# cpupower idle-set -e 1$
Idlestate 1 enabled on CPU 0
[snip]
Idlestate 1 enabled on CPU 47

[root@xxx cpupower]# cpupower idle-set -e 11
Idlestate 11 not available on CPU 0
[snip]
Idlestate 11 not available on CPU 47

[root@xxx cpupower]# cpupower idle-set -d 12
Idlestate 12 not available on CPU 0
[snip]
Idlestate 12 not available on CPU 47

[root@xxx cpupower]# cpupower idle-set -d qw
Idlestate 0 disabled on CPU 0
[snip]
Idlestate 0 disabled on CPU 47

This patch adds a check for both d and e options in cpuidle-set.c to see that the idle_set value is all numeric before doing a string-to-int conversion.

The output of cpuidle-set after the patch is as below:

[root@xxx cpupower]# ./cpupower idle-set -e 1$
Bad idle_set value: 1$. Integer expected

[root@xxx cpupower]# ./cpupower idle-set -e 11
Idlestate 11 not available on CPU 0
[snip]
Idlestate 11 not available on CPU 47

[root@xxx cpupower]# ./cpupower idle-set -d 12
Idlestate 12 not available on CPU 0
[snip]
Idlestate 12 not available on CPU 47

[root@xxx cpupower]# ./cpupower idle-set -d qw
Bad idle_set value: qw. Integer expected

---

** changes since v1 [1] **

- Addressed reviewed comments from v1.
- Slightly reworded the commit for clarity.

[1] https://lore.kernel.org/all/[email protected]/

  tools/power/cpupower/utils/cpuidle-set.c     | 25 ++++++++++++++++----
  tools/power/cpupower/utils/helpers/helpers.h |  8 +++++++
  tools/power/cpupower/utils/helpers/misc.c    | 17 +++++++++++++
  3 files changed, 45 insertions(+), 5 deletions(-)
tools/power/cpupower/utils/cpuidle-set.c +0 -0
index 46158928f9ad..1bfe16d27c2d 100644

tools/power/cpupower/utils/cpuidle-set.c int cmd_idle_set(int argc, char **argv) · line 55

Quoted 10 lines
tools/power/cpupower/utils/cpuidle-set.c +6 -1
@@ -47,7 +47,12 @@ int cmd_idle_set(int argc, char **argv)
  				break;
  			}
  			param = ret;
-			idlestate = atoi(optarg);
+			if (is_stringnumeric(optarg))
+				idlestate = atoi(optarg);
+			else {
+				printf(_("Bad idle_set value: %s. Integer expected\n"), optarg);
+				exit(EXIT_FAILURE);
+			}

Why can't we do this once instead of duplicating the code under 'd' and 'e'

Also have you tried using isdigit(idlestate) - works just fine for me.

tools/power/cpupower/utils/cpuidle-set.c +5 -0
index 46158928f9ad..01b344efc1b1 100644
@@ -95,6 +95,11 @@ int cmd_idle_set(int argc, char **argv)
 		exit(EXIT_FAILURE);
 	}
 
+	if(!isdigit(idlestate)) {
+		printf("invalid idlestate specified\n");
+		exit(EXIT_FAILURE);
+	}
+
 	get_cpustate();
 
 	/* Default is: set all CPUs */

thanks,
-- Shuah

#2 lore raw

Hi Shuah,

Thank you for reviewing.

Quoted 92 lines · Shuah Khan
Quoted 77 lines · Korrapati Likhitha

From: Likhitha Korrapati <[email protected]>

For both the d and e options in 'cpupower idle_set' command, an atoi() conversion is done without checking if the input argument is all numeric. So, an atoi conversion is done on any character provided as input and the CPU idle_set operation continues with that integer value, which may not be what is intended or entirely correct.

The output of cpuidle-set before patch is as follows:

[root@xxx cpupower]# cpupower idle-set -e 1$
Idlestate 1 enabled on CPU 0
[snip]
Idlestate 1 enabled on CPU 47

[root@xxx cpupower]# cpupower idle-set -e 11
Idlestate 11 not available on CPU 0
[snip]
Idlestate 11 not available on CPU 47

[root@xxx cpupower]# cpupower idle-set -d 12
Idlestate 12 not available on CPU 0
[snip]
Idlestate 12 not available on CPU 47

[root@xxx cpupower]# cpupower idle-set -d qw
Idlestate 0 disabled on CPU 0
[snip]
Idlestate 0 disabled on CPU 47

This patch adds a check for both d and e options in cpuidle-set.c to see that the idle_set value is all numeric before doing a string-to-int conversion.

The output of cpuidle-set after the patch is as below:

[root@xxx cpupower]# ./cpupower idle-set -e 1$
Bad idle_set value: 1$. Integer expected

[root@xxx cpupower]# ./cpupower idle-set -e 11
Idlestate 11 not available on CPU 0
[snip]
Idlestate 11 not available on CPU 47

[root@xxx cpupower]# ./cpupower idle-set -d 12
Idlestate 12 not available on CPU 0
[snip]
Idlestate 12 not available on CPU 47

[root@xxx cpupower]# ./cpupower idle-set -d qw
Bad idle_set value: qw. Integer expected

---

** changes since v1 [1] **

- Addressed reviewed comments from v1.
- Slightly reworded the commit for clarity.

[1] https://lore.kernel.org/all/[email protected]/

  tools/power/cpupower/utils/cpuidle-set.c     | 25 ++++++++++++++++---- tools/power/cpupower/utils/helpers/helpers.h |  8 +++++++ tools/power/cpupower/utils/helpers/misc.c    | 17 +++++++++++++

  3 files changed, 45 insertions(+), 5 deletions(-)
tools/power/cpupower/utils/cpuidle-set.c +0 -0
index 46158928f9ad..1bfe16d27c2d 100644
@@ -47,7 +47,12 @@ int cmd_idle_set(int argc, char **argv)
                   break;

tools/power/cpupower/utils/cpuidle-set.c int cmd_idle_set(int argc, char **argv) · line 56

Quoted 10 lines
tools/power/cpupower/utils/cpuidle-set.c +6 -1
               }
               param = ret;
-            idlestate = atoi(optarg);
+            if (is_stringnumeric(optarg))
+                idlestate = atoi(optarg);
+            else {
+                printf(_("Bad idle_set value: %s. Integer 
 expected\n"), optarg);
+                exit(EXIT_FAILURE);
+            }

Why can't we do this once instead of duplicating the code under 'd' and 'e'

Quoted 2 lines

Also have you tried using isdigit(idlestate) - works just fine for me.

idlestate = atoi(optarg)
The function atoi() is used to convert optarg to an integer. However, optarg can potentially be a special character, alphanumeric, or an alphabet, such as "q1", "1!", "qw", etc. In such cases, atoi converts them to 0, which is an integer. Consequently, the value of idlestate becomes 0. This is incorrect because the actual input provided by the user is an invalid input like "q1", "1!", "qw", and idlestate gets set to 0 in an invalid error condition. isdigit() on this unintended 0 will be treated as a good case and not error out as invalid input. and also idlestate is already an integer because it is from atoi() ouput. so isdigit(idlestate) will be a redundant check.

To handle all these scenarios, we used strtol, which is a string to long integer converter. This function is similar to strtoull used in case D for latency. This approach allows us to properly handle invalid inputs and avoid relying on the isdigit function to determine the validity of idlestate, as the value obtained from atoi(optarg) may not be a valid idlestate.

I will address the duplication of code under 'd' and 'e' as part of v3.

Thanks,
Likhitha