[PATCH] linux-user: check for NULL before using interval_tree_iter_first result

gerben@altlinux.org posted 1 patch 3 months, 3 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20250722101544.16366-1-gerben@altlinux.org
Maintainers: Laurent Vivier <laurent@vivier.eu>
linux-user/syscall.c | 3 +++
1 file changed, 3 insertions(+)
[PATCH] linux-user: check for NULL before using interval_tree_iter_first result
Posted by gerben@altlinux.org 3 months, 3 weeks ago
From: Denis Rastyogin <gerben@altlinux.org>

interval_tree_iter_first() may return NULL if the interval tree is empty or invalid.
Add a check for NULL before dereferencing the pointer to avoid potential crashes
due to null pointer dereference in open_self_maps_2().

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Signed-off-by: Denis Rastyogin <gerben@altlinux.org>
---
 linux-user/syscall.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 91360a072c..d5d5912c96 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -8172,6 +8172,9 @@ static int open_self_maps_2(void *opaque, vaddr guest_start,
     while (1) {
         IntervalTreeNode *n =
             interval_tree_iter_first(d->host_maps, host_start, host_start);
+        if (n == NULL) {
+            return -1;
+        }
         MapInfo *mi = container_of(n, MapInfo, itree);
         uintptr_t this_hlast = MIN(host_last, n->last);
         target_ulong this_gend = h2g(this_hlast) + 1;
-- 
2.42.2
Re: [PATCH] linux-user: check for NULL before using interval_tree_iter_first result
Posted by Richard Henderson 3 months, 3 weeks ago
On 7/22/25 03:15, gerben@altlinux.org wrote:
> From: Denis Rastyogin <gerben@altlinux.org>
> 
> interval_tree_iter_first() may return NULL if the interval tree is empty or invalid.
> Add a check for NULL before dereferencing the pointer to avoid potential crashes
> due to null pointer dereference in open_self_maps_2().
> 
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
> 
> Signed-off-by: Denis Rastyogin <gerben@altlinux.org>
> ---
>   linux-user/syscall.c | 3 +++
>   1 file changed, 3 insertions(+)
> 
> diff --git a/linux-user/syscall.c b/linux-user/syscall.c
> index 91360a072c..d5d5912c96 100644
> --- a/linux-user/syscall.c
> +++ b/linux-user/syscall.c
> @@ -8172,6 +8172,9 @@ static int open_self_maps_2(void *opaque, vaddr guest_start,
>       while (1) {
>           IntervalTreeNode *n =
>               interval_tree_iter_first(d->host_maps, host_start, host_start);
> +        if (n == NULL) {
> +            return -1;
> +        }
>           MapInfo *mi = container_of(n, MapInfo, itree);
>           uintptr_t this_hlast = MIN(host_last, n->last);
>           target_ulong this_gend = h2g(this_hlast) + 1;

This will not return null.  Via our caller, we have already verified that there is guest 
memory present for the virtual address.  There *must* be host memory present to back it.


r~