]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests/resctrl: Protect against array overflow when reading strings
authorReinette Chatre <reinette.chatre@intel.com>
Thu, 24 Oct 2024 21:18:42 +0000 (14:18 -0700)
committerShuah Khan <skhan@linuxfoundation.org>
Tue, 5 Nov 2024 00:02:02 +0000 (17:02 -0700)
resctrl selftests discover system properties via a variety of sysfs files.
The MBM and MBA tests need to discover the event and umask with which to
configure the performance event used to measure read memory bandwidth.
This is done by parsing the contents of
/sys/bus/event_source/devices/uncore_imc_<imc instance>/events/cas_count_read
Similarly, the resctrl selftests discover the cache size via
/sys/bus/cpu/devices/cpu<id>/cache/index<index>/size.

Take care to do bounds checking when using fscanf() to read the
contents of files into a string buffer because by default fscanf() assumes
arbitrarily long strings. If the file contains more bytes than the array
can accommodate then an overflow will occur.

Provide a maximum field width to the conversion specifier to protect
against array overflow. The maximum is one less than the array size because
string input stores a terminating null byte that is not covered by the
maximum field width.

Signed-off-by: Reinette Chatre <reinette.chatre@intel.com>
Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
tools/testing/selftests/resctrl/resctrl_val.c
tools/testing/selftests/resctrl/resctrlfs.c

index e88d5ca3051731761154f1db0f0576532f67e942..c9dd70ce3ea87c7c089a4bc19d194f674c0468bc 100644 (file)
@@ -159,7 +159,7 @@ static int read_from_imc_dir(char *imc_dir, int count)
 
                return -1;
        }
-       if (fscanf(fp, "%s", cas_count_cfg) <= 0) {
+       if (fscanf(fp, "%1023s", cas_count_cfg) <= 0) {
                ksft_perror("Could not get iMC cas count read");
                fclose(fp);
 
@@ -177,7 +177,7 @@ static int read_from_imc_dir(char *imc_dir, int count)
 
                return -1;
        }
-       if  (fscanf(fp, "%s", cas_count_cfg) <= 0) {
+       if  (fscanf(fp, "%1023s", cas_count_cfg) <= 0) {
                ksft_perror("Could not get iMC cas count write");
                fclose(fp);
 
index 250c320349a785c3b1de96fea59831471e79b2b7..a53cd1cb6e0c64a140bbe0e51294201876b1afd9 100644 (file)
@@ -182,7 +182,7 @@ int get_cache_size(int cpu_no, const char *cache_type, unsigned long *cache_size
 
                return -1;
        }
-       if (fscanf(fp, "%s", cache_str) <= 0) {
+       if (fscanf(fp, "%63s", cache_str) <= 0) {
                ksft_perror("Could not get cache_size");
                fclose(fp);