selftests/nolibc: avoid buffer underrun in space printing
authorThomas Weißschuh <linux@weissschuh.net>
Wed, 26 Jul 2023 06:08:13 +0000 (08:08 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 23 Aug 2023 03:17:07 +0000 (05:17 +0200)
If the test description is longer than the status alignment the
parameter 'n' to putcharn() would lead to a signed underflow that then
gets converted to a very large unsigned value.
This in turn leads out-of-bound writes in memset() crashing the
application.

The failure case of EXPECT_PTRER() used in "mmap_bad" exhibits this
exact behavior.

Fixes: 29f5540be392 ("selftests/nolibc: add EXPECT_PTREQ, EXPECT_PTRNE and EXPECT_PTRER")
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Signed-off-by: Willy Tarreau <w@1wt.eu>
tools/testing/selftests/nolibc/nolibc-test.c

index 7372057..3f5a256 100644 (file)
@@ -151,7 +151,8 @@ static void result(int llen, enum RESULT r)
        else
                msg = "[FAIL]";
 
-       putcharn(' ', 64 - llen);
+       if (llen < 64)
+               putcharn(' ', 64 - llen);
        puts(msg);
 }