From 824debfd2ef5f997c01d355afa9b4b016c2db3aa Mon Sep 17 00:00:00 2001 From: Ben Hutchings Date: Tue, 11 Sep 2018 03:20:07 +0100 Subject: [PATCH] tools: turbostat: Add checks for failure of fgets() and fscanf() --- debian/changelog | 1 + ...Add-checks-for-failure-of-fgets-and-.patch | 113 ++++++++++++++++++ debian/patches/series | 1 + 3 files changed, 115 insertions(+) create mode 100644 debian/patches/bugfix/x86/tools-turbostat-Add-checks-for-failure-of-fgets-and-.patch diff --git a/debian/changelog b/debian/changelog index 50d48c1fd..04fa89737 100644 --- a/debian/changelog +++ b/debian/changelog @@ -20,6 +20,7 @@ linux (4.19~rc3-1~exp2) UNRELEASED; urgency=medium * debian/rules.d/tools/power/linux-cpupower: Add "+" to recursive make commands * tools: x86_energy_perf_policy: Fix "uninitialized variable" warnings at -O2 + * tools: turbostat: Add checks for failure of fgets() and fscanf() -- Ben Hutchings Mon, 10 Sep 2018 22:25:53 +0100 diff --git a/debian/patches/bugfix/x86/tools-turbostat-Add-checks-for-failure-of-fgets-and-.patch b/debian/patches/bugfix/x86/tools-turbostat-Add-checks-for-failure-of-fgets-and-.patch new file mode 100644 index 000000000..f33710e58 --- /dev/null +++ b/debian/patches/bugfix/x86/tools-turbostat-Add-checks-for-failure-of-fgets-and-.patch @@ -0,0 +1,113 @@ +From: Ben Hutchings +Date: Tue, 11 Sep 2018 03:07:28 +0100 +Subject: tools: turbostat: Add checks for failure of fgets() and fscanf() + +Most calls to fgets() and fscanf() are followed by error checks. +Add an exit-on-error in the remaining cases. + +Signed-off-by: Ben Hutchings +--- + tools/power/x86/turbostat/turbostat.c | 28 +++++++++++++++++---------- + 1 file changed, 18 insertions(+), 10 deletions(-) + +diff --git a/tools/power/x86/turbostat/turbostat.c b/tools/power/x86/turbostat/turbostat.c +index 980bd9d20646..b4f32dd37c67 100644 +--- a/tools/power/x86/turbostat/turbostat.c ++++ b/tools/power/x86/turbostat/turbostat.c +@@ -2554,7 +2554,8 @@ int get_thread_siblings(struct cpu_topology *thiscpu) + filep = fopen_or_die(path, "r"); + do { + offset -= BITMASK_SIZE; +- fscanf(filep, "%lx%c", &map, &character); ++ if (fscanf(filep, "%lx%c", &map, &character) != 2) ++ err(1, "%s: failed to parse file", path); + for (shift = 0; shift < BITMASK_SIZE; shift++) { + if ((map >> shift) & 0x1) { + so = shift + offset; +@@ -3407,14 +3408,14 @@ dump_sysfs_cstate_config(void) + input = fopen(path, "r"); + if (input == NULL) + continue; +- fgets(name_buf, sizeof(name_buf), input); ++ if (!fgets(name_buf, sizeof(name_buf), input)) ++ err(1, "%s: failed to read file", path); + + /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ + sp = strchr(name_buf, '-'); + if (!sp) + sp = strchrnul(name_buf, '\n'); + *sp = '\0'; +- + fclose(input); + + sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc", +@@ -3422,7 +3423,8 @@ dump_sysfs_cstate_config(void) + input = fopen(path, "r"); + if (input == NULL) + continue; +- fgets(desc, sizeof(desc), input); ++ if (!fgets(desc, sizeof(desc), input)) ++ err(1, "%s: failed to read file", path); + + fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc); + fclose(input); +@@ -3444,7 +3446,8 @@ dump_sysfs_pstate_config(void) + fprintf(stderr, "NSFOD %s\n", path); + return; + } +- fgets(driver_buf, sizeof(driver_buf), input); ++ if (!fgets(driver_buf, sizeof(driver_buf), input)) ++ err(1, "%s: failed to read file", path); + fclose(input); + + sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", +@@ -3454,7 +3457,8 @@ dump_sysfs_pstate_config(void) + fprintf(stderr, "NSFOD %s\n", path); + return; + } +- fgets(governor_buf, sizeof(governor_buf), input); ++ if (!fgets(governor_buf, sizeof(governor_buf), input)) ++ err(1, "%s: failed to read file", path); + fclose(input); + + fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf); +@@ -3463,7 +3467,8 @@ dump_sysfs_pstate_config(void) + sprintf(path, "/sys/devices/system/cpu/cpufreq/boost"); + input = fopen(path, "r"); + if (input != NULL) { +- fscanf(input, "%d", &turbo); ++ if (fscanf(input, "%d", &turbo) != 1) ++ err(1, "%s: failed to parse number from file", path); + fprintf(outf, "cpufreq boost: %d\n", turbo); + fclose(input); + } +@@ -3471,7 +3476,8 @@ dump_sysfs_pstate_config(void) + sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo"); + input = fopen(path, "r"); + if (input != NULL) { +- fscanf(input, "%d", &turbo); ++ if (fscanf(input, "%d", &turbo) != 1) ++ err(1, "%s: failed to parse number from file", path); + fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo); + fclose(input); + } +@@ -5293,7 +5299,8 @@ void probe_sysfs(void) + input = fopen(path, "r"); + if (input == NULL) + continue; +- fgets(name_buf, sizeof(name_buf), input); ++ if (!fgets(name_buf, sizeof(name_buf), input)) ++ err(1, "%s: failed to read file", path); + + /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ + sp = strchr(name_buf, '-'); +@@ -5320,7 +5327,8 @@ void probe_sysfs(void) + input = fopen(path, "r"); + if (input == NULL) + continue; +- fgets(name_buf, sizeof(name_buf), input); ++ if (!fgets(name_buf, sizeof(name_buf), input)) ++ err(1, "%s: failed to read file", path); + /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */ + sp = strchr(name_buf, '-'); + if (!sp) diff --git a/debian/patches/series b/debian/patches/series index 5d839e52c..1bec250d3 100644 --- a/debian/patches/series +++ b/debian/patches/series @@ -153,6 +153,7 @@ bugfix/all/locking-lockdep-delete-unnecesary-include.patch bugfix/all/tools-lib-api-fs-fs.c-fix-misuse-of-strncpy.patch bugfix/all/usbip-fix-misuse-of-strncpy.patch bugfix/x86/tools-x86_energy_perf_policy-fix-uninitialized-varia.patch +bugfix/x86/tools-turbostat-Add-checks-for-failure-of-fgets-and-.patch # wireless: Disable regulatory.db direct loading (until we sort out signing) debian/wireless-disable-regulatory.db-direct-loading.patch