selftests/bpf: Prevent out-of-bounds stack access in test_bpffs

Buf can be not zero-terminated leading to strstr() to access data beyond
the intended buf[] array. Fix by forcing zero termination.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20211124002325.1737739-12-andrii@kernel.org
This commit is contained in:
Andrii Nakryiko 2021-11-23 16:23:23 -08:00 committed by Daniel Borkmann
parent e2e0d90c55
commit 57428298b5
1 changed files with 3 additions and 1 deletions

View File

@ -19,11 +19,13 @@ static int read_iter(char *file)
fd = open(file, 0);
if (fd < 0)
return -1;
while ((len = read(fd, buf, sizeof(buf))) > 0)
while ((len = read(fd, buf, sizeof(buf))) > 0) {
buf[sizeof(buf) - 1] = '\0';
if (strstr(buf, "iter")) {
close(fd);
return 0;
}
}
close(fd);
return -1;
}