[v2,09/10] selftests/nolibc: test return value of read() in test_vfprintf

Message ID 20230801-nolibc-warnings-v2-9-1ba5ca57bd9b@weissschuh.net
State New
Headers
Series tools/nolibc: enable compiler warnings |

Commit Message

Thomas Weißschuh Aug. 1, 2023, 5:30 a.m. UTC
  If read() fails and returns -1 buf would be accessed out of bounds.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 tools/testing/selftests/nolibc/nolibc-test.c | 6 ++++++
 1 file changed, 6 insertions(+)
  

Comments

Willy Tarreau Aug. 1, 2023, 6:59 a.m. UTC | #1
On Tue, Aug 01, 2023 at 07:30:16AM +0200, Thomas Weißschuh wrote:
> If read() fails and returns -1 buf would be accessed out of bounds.
> 
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
>  tools/testing/selftests/nolibc/nolibc-test.c | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
> index 82714051c72f..a334f8450a34 100644
> --- a/tools/testing/selftests/nolibc/nolibc-test.c
> +++ b/tools/testing/selftests/nolibc/nolibc-test.c
> @@ -1031,6 +1031,12 @@ static int expect_vfprintf(int llen, int c, const char *expected, const char *fm
>  	lseek(fd, 0, SEEK_SET);
>  
>  	r = read(fd, buf, sizeof(buf) - 1);
> +	if (r == -1) {
> +		llen += printf(" read() = %s", errorname(errno));
> +		result(llen, FAIL);
> +		return 1;
> +	}
> +
>  	buf[r] = '\0';

In fact given the nature of this file (test if we properly implemented
our syscalls), I think that a more conservative approach is deserved
because if we messed up on read() we can have anything on return and we
don't want to trust that. As such I would suggest that we declare r as
ssize_t and verify that it's neither negative nor larger than
sizeof(buf)-1, which becomes:

        if ((size_t)r >= sizeof(buf)) {
            ... fail ...
        }

You'll also have to turn w to ssize_t then due to the test later BTW.

Willy
  
Thomas Weißschuh Aug. 1, 2023, 7:48 a.m. UTC | #2
On 2023-08-01 08:59:17+0200, Willy Tarreau wrote:
> On Tue, Aug 01, 2023 at 07:30:16AM +0200, Thomas Weißschuh wrote:
> > If read() fails and returns -1 buf would be accessed out of bounds.
> > 
> > Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> > ---
> >  tools/testing/selftests/nolibc/nolibc-test.c | 6 ++++++
> >  1 file changed, 6 insertions(+)
> > 
> > diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
> > index 82714051c72f..a334f8450a34 100644
> > --- a/tools/testing/selftests/nolibc/nolibc-test.c
> > +++ b/tools/testing/selftests/nolibc/nolibc-test.c
> > @@ -1031,6 +1031,12 @@ static int expect_vfprintf(int llen, int c, const char *expected, const char *fm
> >  	lseek(fd, 0, SEEK_SET);
> >  
> >  	r = read(fd, buf, sizeof(buf) - 1);
> > +	if (r == -1) {
> > +		llen += printf(" read() = %s", errorname(errno));
> > +		result(llen, FAIL);
> > +		return 1;
> > +	}
> > +
> >  	buf[r] = '\0';
> 
> In fact given the nature of this file (test if we properly implemented
> our syscalls), I think that a more conservative approach is deserved
> because if we messed up on read() we can have anything on return and we
> don't want to trust that. As such I would suggest that we declare r as
> ssize_t and verify that it's neither negative nor larger than
> sizeof(buf)-1, which becomes:
> 
>         if ((size_t)r >= sizeof(buf)) {
>             ... fail ...
>         }

As r == w is validated just below anyways we could move the assignment
buf[r] = '\0' after that check and then we don't need a new block.

> You'll also have to turn w to ssize_t then due to the test later BTW.

Will do in any case.
  

Patch

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 82714051c72f..a334f8450a34 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -1031,6 +1031,12 @@  static int expect_vfprintf(int llen, int c, const char *expected, const char *fm
 	lseek(fd, 0, SEEK_SET);
 
 	r = read(fd, buf, sizeof(buf) - 1);
+	if (r == -1) {
+		llen += printf(" read() = %s", errorname(errno));
+		result(llen, FAIL);
+		return 1;
+	}
+
 	buf[r] = '\0';
 
 	fclose(memfile);