Skip to content

Commit 5dc9ab1

Browse files
rchatregregkh
authored andcommitted
selftests/resctrl: Protect against array overrun during iMC config parsing
[ Upstream commit 48ed4e7 ] 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 /sys/bus/event_source/devices/uncore_imc_<imc instance>/events/cas_count_read file for each iMC instance that contains the formatted output: "event=<event>,umask=<umask>" Parsing of cas_count_read contents is done by initializing an array of MAX_TOKENS elements with tokens (deliminated by "=,") from this file. Remove the unnecessary append of a delimiter to the string needing to be parsed. Per the strtok() man page: "delimiter bytes at the start or end of the string are ignored". This has no impact on the token placement within the array. After initialization, the actual event and umask is determined by parsing the tokens directly following the "event" and "umask" tokens respectively. Iterating through the array up to index "i < MAX_TOKENS" but then accessing index "i + 1" risks array overrun during the final iteration. Avoid array overrun by ensuring that the index used within for loop will always be valid. Fixes: 1d3f086 ("selftests/resctrl: Read memory bandwidth from perf IMC counter and from resctrl file system") 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> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 9d46744 commit 5dc9ab1

1 file changed

Lines changed: 1 addition & 2 deletions

File tree

tools/testing/selftests/resctrl/resctrl_val.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,12 @@ void get_event_and_umask(char *cas_count_cfg, int count, bool op)
8383
char *token[MAX_TOKENS];
8484
int i = 0;
8585

86-
strcat(cas_count_cfg, ",");
8786
token[0] = strtok(cas_count_cfg, "=,");
8887

8988
for (i = 1; i < MAX_TOKENS; i++)
9089
token[i] = strtok(NULL, "=,");
9190

92-
for (i = 0; i < MAX_TOKENS; i++) {
91+
for (i = 0; i < MAX_TOKENS - 1; i++) {
9392
if (!token[i])
9493
break;
9594
if (strcmp(token[i], "event") == 0) {

0 commit comments

Comments
 (0)