Skip to content

Commit

Permalink
selftests/bpf: Prevent out-of-bounds stack access in test_bpffs
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Daniel Borkmann <[email protected]>
Link: https://lore.kernel.org/bpf/[email protected]
  • Loading branch information
anakryiko authored and borkmann committed Nov 25, 2021
1 parent e2e0d90 commit 5742829
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion tools/testing/selftests/bpf/prog_tests/test_bpffs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 5742829

Please sign in to comment.