Skip to content

Commit 8b67529

Browse files
rchatregregkh
authored andcommitted
selftests/resctrl: Protect against array overflow when reading strings
[ Upstream commit 4605843 ] 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 <[email protected]> Reviewed-by: Ilpo Järvinen <[email protected]> Signed-off-by: Shuah Khan <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 0380da1 commit 8b67529

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

tools/testing/selftests/resctrl/resctrl_val.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ static int read_from_imc_dir(char *imc_dir, int count)
159159

160160
return -1;
161161
}
162-
if (fscanf(fp, "%s", cas_count_cfg) <= 0) {
162+
if (fscanf(fp, "%1023s", cas_count_cfg) <= 0) {
163163
ksft_perror("Could not get iMC cas count read");
164164
fclose(fp);
165165

@@ -177,7 +177,7 @@ static int read_from_imc_dir(char *imc_dir, int count)
177177

178178
return -1;
179179
}
180-
if (fscanf(fp, "%s", cas_count_cfg) <= 0) {
180+
if (fscanf(fp, "%1023s", cas_count_cfg) <= 0) {
181181
ksft_perror("Could not get iMC cas count write");
182182
fclose(fp);
183183

tools/testing/selftests/resctrl/resctrlfs.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ int get_cache_size(int cpu_no, const char *cache_type, unsigned long *cache_size
182182

183183
return -1;
184184
}
185-
if (fscanf(fp, "%s", cache_str) <= 0) {
185+
if (fscanf(fp, "%63s", cache_str) <= 0) {
186186
ksft_perror("Could not get cache_size");
187187
fclose(fp);
188188

0 commit comments

Comments
 (0)