[PATCH] tools/virtio: check mmap return value in vringh_test

longlong yan posted 1 patch 3 days, 4 hours ago
There is a newer version of this series
tools/virtio/vringh_test.c | 5 +++++
1 file changed, 5 insertions(+)
[PATCH] tools/virtio: check mmap return value in vringh_test
Posted by longlong yan 3 days, 4 hours ago
In parallel_test(), the return values of mmap() for both host_map and
guest_map are not checked against MAP_FAILED. If mmap() fails, the
subsequent code will dereference the invalid pointer, leading to a
segmentation fault.

Add MAP_FAILED checks after both mmap() calls, using err() to report
the error and exit, consistent with the existing error handling style
in this file (e.g., the open() call on line 149).

Fixes: 1515c5ce26ae("tools/virtio: add vring_test.")
Signed-off-by: longlong yan <yanlonglong@kylinos.cn>
---
 tools/virtio/vringh_test.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/tools/virtio/vringh_test.c b/tools/virtio/vringh_test.c
index b9591223437a..8cc5ca6c7cca 100644
--- a/tools/virtio/vringh_test.c
+++ b/tools/virtio/vringh_test.c
@@ -159,7 +159,12 @@ static int parallel_test(u64 features,
 
 	/* Parent and child use separate addresses, to check our mapping logic! */
 	host_map = mmap(NULL, mapsize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+	if (host_map == MAP_FAILED)
+		err(1, "mmap host_map");
+	
 	guest_map = mmap(NULL, mapsize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+	if (guest_map == MAP_FAILED)
+		err(1, "mmap guest_map");
 
 	pipe_ret = pipe(to_guest);
 	assert(!pipe_ret);
-- 
2.43.0
Re: [PATCH] tools/virtio: check mmap return value in vringh_test
Posted by Michael S. Tsirkin 2 days, 20 hours ago
On Fri, Jun 05, 2026 at 10:14:45AM +0800, longlong yan wrote:
> In parallel_test(), the return values of mmap() for both host_map and
> guest_map are not checked against MAP_FAILED. If mmap() fails, the
> subsequent code will dereference the invalid pointer, leading to a
> segmentation fault.
> 
> Add MAP_FAILED checks after both mmap() calls, using err() to report
> the error and exit, consistent with the existing error handling style
> in this file (e.g., the open() call on line 149).
> 
> Fixes: 1515c5ce26ae("tools/virtio: add vring_test.")
> Signed-off-by: longlong yan <yanlonglong@kylinos.cn>
> ---
>  tools/virtio/vringh_test.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/tools/virtio/vringh_test.c b/tools/virtio/vringh_test.c
> index b9591223437a..8cc5ca6c7cca 100644
> --- a/tools/virtio/vringh_test.c
> +++ b/tools/virtio/vringh_test.c
> @@ -159,7 +159,12 @@ static int parallel_test(u64 features,
>  
>  	/* Parent and child use separate addresses, to check our mapping logic! */
>  	host_map = mmap(NULL, mapsize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
> +	if (host_map == MAP_FAILED)
> +		err(1, "mmap host_map");
> +	


trailing whitespace here.

>  	guest_map = mmap(NULL, mapsize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
> +	if (guest_map == MAP_FAILED)
> +		err(1, "mmap guest_map");
>  
>  	pipe_ret = pipe(to_guest);
>  	assert(!pipe_ret);
> -- 
> 2.43.0
[PATCH v2] tools/virtio: check mmap return value in vringh_test
Posted by longlong yan 5 hours ago
In parallel_test(), the return values of mmap() for both host_map and
guest_map are not checked against MAP_FAILED. If mmap() fails, the
subsequent code will dereference the invalid pointer, leading to a
segmentation fault.

Add MAP_FAILED checks after both mmap() calls, using err() to report
the error and exit, consistent with the existing error handling style
in this file (e.g., the open() call on line 149).

Fixes: 1515c5ce26ae("tools/virtio: add vring_test.")
Signed-off-by: longlong yan <yanlonglong@kylinos.cn>
---
 tools/virtio/vringh_test.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/tools/virtio/vringh_test.c b/tools/virtio/vringh_test.c
index b9591223437a..5ea6d29bc992 100644
--- a/tools/virtio/vringh_test.c
+++ b/tools/virtio/vringh_test.c
@@ -159,7 +159,12 @@ static int parallel_test(u64 features,
 
 	/* Parent and child use separate addresses, to check our mapping logic! */
 	host_map = mmap(NULL, mapsize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+	if (host_map == MAP_FAILED)
+		err(1, "mmap host_map");
+
 	guest_map = mmap(NULL, mapsize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+	if (guest_map == MAP_FAILED)
+		err(1, "mmap guest_map");
 
 	pipe_ret = pipe(to_guest);
 	assert(!pipe_ret);
-- 
2.43.0