selftests/nolibc: prevent out of bounds access in expect_vfprintf

[ Upstream commit 9c5e490093 ]

If read() fails and returns -1 (or returns garbage for some other
reason) buf would be accessed out of bounds.
Only use the return value of read() after it has been validated.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
Thomas Weißschuh 2023-08-03 09:28:56 +02:00 committed by Greg Kroah-Hartman
parent 21c634eb69
commit 886c7f95e7

View file

@ -769,7 +769,6 @@ static int expect_vfprintf(int llen, size_t c, const char *expected, const char
lseek(fd, 0, SEEK_SET);
r = read(fd, buf, sizeof(buf) - 1);
buf[r] = '\0';
fclose(memfile);
@ -779,6 +778,7 @@ static int expect_vfprintf(int llen, size_t c, const char *expected, const char
return 1;
}
buf[r] = '\0';
llen += printf(" \"%s\" = \"%s\"", expected, buf);
ret = strncmp(expected, buf, c);