From 251604baa2541813e9077f1b524405207b55c672 Mon Sep 17 00:00:00 2001 From: Yazen Ghannam Date: Mon, 17 Sep 2018 11:57:04 -0500 Subject: [PATCH 1/2] Fail to read/write if register number is not parsed correctly It's possible that a user will provide a register number in a format that strtoul() can't parse. strtoul() will return 0 if it fails to parse a value. The msr-tools will then silently read/write to MSR 0x0 and succeed. At best this may confuse the user, or at worst this will unintentionally write bad data to the system. Check that the register number parsing has succeeded. If it fails, then inform the user, give a suggestion, and exit. Signed-off-by: Yazen Ghannam --- rdmsr.c | 6 +++++- wrmsr.c | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/rdmsr.c b/rdmsr.c index b19986a..48e70a3 100644 --- a/rdmsr.c +++ b/rdmsr.c @@ -194,7 +194,11 @@ int main(int argc, char *argv[]) exit(127); } - reg = strtoul(argv[optind], NULL, 0); + reg = strtoul(argv[optind], &endarg, 0); + if (*endarg) { + printf("Failed to parse register number. Do you need a prefix?\n"); + exit(127); + } if (cpu == -1) { doing_for_all = 1; diff --git a/wrmsr.c b/wrmsr.c index b32cc3e..0128153 100644 --- a/wrmsr.c +++ b/wrmsr.c @@ -121,7 +121,11 @@ int main(int argc, char *argv[]) exit(127); } - reg = strtoul(argv[optind++], NULL, 0); + reg = strtoul(argv[optind++], &endarg, 0); + if (*endarg) { + printf("Failed to parse register number. Do you need a prefix?\n"); + exit(127); + } if (cpu == -1) { doing_for_all = 1; From b5b27504f5c73cd3aabb53fd04e595382e4ed4ea Mon Sep 17 00:00:00 2001 From: Andres Salomon Date: Sun, 24 Nov 2019 00:43:08 -0500 Subject: [PATCH 2/2] rdmsr/wrmsr: clean up register parsing fix Yazen fixed it so that invalid register strings wouldn't be silently parsed and used, but the error message was problematic and went to the wrong place. This sends the error to stderr, and matches the output to look like other error messages in msr-tools. Signed-off-by: Andres Salomon --- rdmsr.c | 2 +- wrmsr.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rdmsr.c b/rdmsr.c index 48e70a3..57ecb26 100644 --- a/rdmsr.c +++ b/rdmsr.c @@ -196,7 +196,7 @@ int main(int argc, char *argv[]) reg = strtoul(argv[optind], &endarg, 0); if (*endarg) { - printf("Failed to parse register number. Do you need a prefix?\n"); + fprintf(stderr, "rdmsr: failed to parse register number\n"); exit(127); } diff --git a/wrmsr.c b/wrmsr.c index 0128153..2080e5f 100644 --- a/wrmsr.c +++ b/wrmsr.c @@ -123,7 +123,7 @@ int main(int argc, char *argv[]) reg = strtoul(argv[optind++], &endarg, 0); if (*endarg) { - printf("Failed to parse register number. Do you need a prefix?\n"); + fprintf(stderr, "wrmsr: failed to parse register number\n"); exit(127); }