Skip to contentSkip to conversation

ACPI: tools: pfrut: Check if the input of level and type is in the right numeric range

PATCH Superseded Chen Yu 2 participants 3 messages Mar 7, 2023 – Mar 8, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Chen Yu

author
#0 lore raw

PATCH 1/1 ACPI: tools: pfrut: Check if the input of level and type is in the right numeric range

The user can provide arbitrary non-numeic value to level and type, which brings unexpected behavior:

pfrut -h
usage: pfrut [OPTIONS]
code injection:
-l, --load
-s, --stage
-a, --activate
-u, --update [stage and activate]
-q, --query
-d, --revid
update telemetry:
-G, --getloginfo
-T, --type(0:execution, 1:history)
-L, --level(0, 1, 2, 4)
-R, --read
-D, --revid log

pfrut -T A
pfrut -G
log_level:0
log_type:0
log_revid:2
max_data_size:65536
chunk1_size:0
chunk2_size:1530
rollover_cnt:0
reset_cnt:17

Fix this by restricting the input is in the expected range.

---
 tools/power/acpi/tools/pfrut/pfrut.c | 33 ++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
tools/power/acpi/tools/pfrut/pfrut.c +33 -0
index 52aa0351533c..ed672efef83b 100644
@@ -22,6 +22,7 @@
 #include <sys/ioctl.h>
 #include <sys/mman.h>
 #include <uuid/uuid.h>
+#include <ctype.h>
 #include PFRUT_HEADER
 
 char *capsule_name;
@@ -77,6 +78,18 @@ static void help(void)
 		progname);
 }
 
+static int is_digit_input(char *str)
+{
+	char *scan;
+
+	for (scan = str; *scan != '\0'; scan++) {
+		if (!isdigit(*scan))
+			return 0;
+	}
+
+	return 1;
+}
+
 char *option_string = "l:sauqd:GT:L:RD:h";
 static struct option long_options[] = {
 	{"load", required_argument, 0, 'l'},
@@ -125,11 +138,31 @@ static void parse_options(int argc, char **argv)
 			log_getinfo = 1;
 			break;
 		case 'T':
+			if (!is_digit_input(optarg)) {
+				printf("Please provide numeric value for type (0:execution, 1:history) - Exiting.\n");
+				exit(1);
+			}
+
 			log_type = atoi(optarg);
+			if (log_type != 0 && log_type != 1) {
+				printf("Please provide numeric value for type (0:execution, 1:history) - Exiting.\n");
+				exit(1);
+			}
+
 			set_log_type = 1;
 			break;
 		case 'L':
+			if (!is_digit_input(optarg)) {
+				printf("Please provide numeric value for level (0, 1, 2, 4) - Exiting.\n");
+				exit(1);
+			}
+
 			log_level = atoi(optarg);
+			if (log_level > 4) {
+				printf("Please provide numeric value for level (0, 1, 2, 4) - Exiting.\n");
+				exit(1);
+			}
+
 			set_log_level = 1;
 			break;
 		case 'R':
Signature · 1 line
2.25.1
#1 lore raw
Quoted 3 lines · Chen Yu

The user can provide arbitrary non-numeic value to level and type, which brings unexpected behavior:

So I guess the expected behavior would be to throw an error?

Quoted 67 lines

pfrut -h
usage: pfrut [OPTIONS]
code injection:

(quoted diff) +0 -6
-l, --load
-s, --stage
-a, --activate
-u, --update [stage and activate]
-q, --query
-d, --revid

update telemetry:

(quoted diff) +0 -5
-G, --getloginfo
-T, --type(0:execution, 1:history)
-L, --level(0, 1, 2, 4)
-R, --read
-D, --revid log
 
 pfrut -T A
 pfrut -G

log_level:0
log_type:0
log_revid:2
max_data_size:65536
chunk1_size:0
chunk2_size:1530
rollover_cnt:0
reset_cnt:17

Fix this by restricting the input is in the expected range.

---
 tools/power/acpi/tools/pfrut/pfrut.c | 33 ++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
tools/power/acpi/tools/pfrut/pfrut.c +13 -0
index 52aa0351533c..ed672efef83b 100644
@@ -22,6 +22,7 @@
 #include <sys/ioctl.h>
 #include <sys/mman.h>
 #include <uuid/uuid.h>
+#include <ctype.h>
 #include PFRUT_HEADER
 
 char *capsule_name;
@@ -77,6 +78,18 @@ static void help(void)
                progname);
 }
 
+static int is_digit_input(char *str)
+{
+       char *scan;
+
+       for (scan = str; *scan != '\0'; scan++) {
+               if (!isdigit(*scan))
+                       return 0;
+       }
+
+       return 1;
+}
+
 char *option_string = "l:sauqd:GT:L:RD:h";
 static struct option long_options[] = {
        {"load", required_argument, 0, 'l'},

tools/power/acpi/tools/pfrut/pfrut.c static void parse_options(int argc, char **argv) · line 145

Quoted 8 lines
tools/power/acpi/tools/pfrut/pfrut.c +5 -0
@@ -125,11 +138,31 @@ static void parse_options(int argc, char **argv)
                        log_getinfo = 1;
                        break;
                case 'T':
+                       if (!is_digit_input(optarg)) {
+                               printf("Please provide numeric value for type (0:execution, 1:history) - Exiting.\n");
+                               exit(1);
+                       }
+

Wouldn't using strtol() instead of atoi() work?

Quoted 26 lines
tools/power/acpi/tools/pfrut/pfrut.c +15 -1
                        log_type = atoi(optarg);
+                       if (log_type != 0 && log_type != 1) {
+                               printf("Please provide numeric value for type (0:execution, 1:history) - Exiting.\n");
+                               exit(1);
+                       }
+
                        set_log_type = 1;
                        break;
                case 'L':
+                       if (!is_digit_input(optarg)) {
+                               printf("Please provide numeric value for level (0, 1, 2, 4) - Exiting.\n");
+                               exit(1);
+                       }
+
                        log_level = atoi(optarg);
+                       if (log_level > 4) {
+                               printf("Please provide numeric value for level (0, 1, 2, 4) - Exiting.\n");
+                               exit(1);
+                       }
+
                        set_log_level = 1;
                        break;
                case 'R':
--

2.25.1

author
#2 lore raw

Hi Rafael,
thanks for the review,

Quoted 7 lines · Rafael J. Wysocki
Quoted 3 lines · Chen Yu

The user can provide arbitrary non-numeic value to level and type, which brings unexpected behavior:

So I guess the expected behavior would be to throw an error?

Yes.

Quoted 69 lines
Quoted 69 lines

pfrut -h
usage: pfrut [OPTIONS]
code injection:

(quoted diff) +0 -6
-l, --load
-s, --stage
-a, --activate
-u, --update [stage and activate]
-q, --query
-d, --revid

update telemetry:

(quoted diff) +0 -5
-G, --getloginfo
-T, --type(0:execution, 1:history)
-L, --level(0, 1, 2, 4)
-R, --read
-D, --revid log
 
 pfrut -T A
 pfrut -G

log_level:0
log_type:0
log_revid:2
max_data_size:65536
chunk1_size:0
chunk2_size:1530
rollover_cnt:0
reset_cnt:17

Fix this by restricting the input is in the expected range.

---
 tools/power/acpi/tools/pfrut/pfrut.c | 33 ++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
tools/power/acpi/tools/pfrut/pfrut.c +13 -0
index 52aa0351533c..ed672efef83b 100644
@@ -22,6 +22,7 @@
 #include <sys/ioctl.h>
 #include <sys/mman.h>
 #include <uuid/uuid.h>
+#include <ctype.h>
 #include PFRUT_HEADER
 
 char *capsule_name;
@@ -77,6 +78,18 @@ static void help(void)
                progname);
 }
 
+static int is_digit_input(char *str)
+{
+       char *scan;
+
+       for (scan = str; *scan != '\0'; scan++) {
+               if (!isdigit(*scan))
+                       return 0;
+       }
+
+       return 1;
+}
+
 char *option_string = "l:sauqd:GT:L:RD:h";
 static struct option long_options[] = {
        {"load", required_argument, 0, 'l'},
@@ -125,11 +138,31 @@ static void parse_options(int argc, char **argv)
                        log_getinfo = 1;
                        break;
Quoted 9 lines

tools/power/acpi/tools/pfrut/pfrut.c static void parse_options(int argc, char **argv) · line 145

Quoted 6 lines
tools/power/acpi/tools/pfrut/pfrut.c +5 -0
                case 'T':
+                       if (!is_digit_input(optarg)) {
+                               printf("Please provide numeric value for type (0:execution, 1:history) - Exiting.\n");
+                               exit(1);
+                       }
+

Wouldn't using strtol() instead of atoi() work?

Yes, strtol() would be simpler:
c = strtol(optarg, &endptr, 0)
if (*endptr)

	exit(1);

I'll change it in next version.

thanks,
Chenyu