qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Matthew Lugg <mlugg@mlugg.co.uk>
To: qemu-devel@nongnu.org
Cc: laurent@vivier.eu, peter.maydell@linaro.org,
	Matthew Lugg <mlugg@mlugg.co.uk>
Subject: [PATCH v2 3/4] linux-user: fix reserved_va page leak in do_munmap
Date: Mon, 17 Nov 2025 17:09:53 +0000	[thread overview]
Message-ID: <20251117170954.31451-4-mlugg@mlugg.co.uk> (raw)
In-Reply-To: <20251117170954.31451-1-mlugg@mlugg.co.uk>

The old logic had an off-by-one bug. For instance, assuming 4k pages on
host and guest, if 'len' is '4097' (indicating to unmap 2 pages), then
'last = start + 4096', so 'real_last = start + 4095', so ultimately
'real_len = 4096'. I do not believe this could cause any observable bugs
in guests, because `target_munmap` page-aligns the length it passes in.
However, calls to this function in `target_mremap` do not page-align the
length, so those calls could "drop" pages, leading to a part of the
reserved region becoming unmapped. At worst, a host allocation could get
mapped into that hole, then clobbered by a new guest mapping.

Signed-off-by: Matthew Lugg <mlugg@mlugg.co.uk>
---
 linux-user/mmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

The refactor in the last revision has been dropped in favour of a simple
two-line patch. The added 'ROUND_UP' on the first line of the diff is necessary
to prevent the last 'for (prot = 0, ...' in the function from considering a page
which 'len' partially covers.

diff --git a/linux-user/mmap.c b/linux-user/mmap.c
index 6163f1a0d1..4bcfaf7894 100644
--- a/linux-user/mmap.c
+++ b/linux-user/mmap.c
@@ -1029,9 +1029,9 @@ static int mmap_reserve_or_unmap(abi_ulong start, abi_ulong len)
     void *host_start;
     int prot;
 
-    last = start + len - 1;
+    last = ROUND_UP(start + len, TARGET_PAGE_SIZE) - 1;
     real_start = start & -host_page_size;
-    real_last = ROUND_UP(last, host_page_size) - 1;
+    real_last = ROUND_UP(last + 1, host_page_size) - 1;
 
     /*
      * If guest pages remain on the first or last host pages,
-- 
2.51.2



  parent reply	other threads:[~2025-11-17 17:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-17 17:09 [PATCH v2 0/4] linux-user: fix several mremap bugs Matthew Lugg
2025-11-17 17:09 ` [PATCH v2 1/4] linux-user: fix mremap unmapping adjacent region Matthew Lugg
2025-11-19 11:20   ` Richard Henderson
2025-11-17 17:09 ` [PATCH v2 2/4] linux-user: fix mremap errors for invalid ranges Matthew Lugg
2025-11-17 17:09 ` Matthew Lugg [this message]
2025-11-19 11:21   ` [PATCH v2 3/4] linux-user: fix reserved_va page leak in do_munmap Richard Henderson
2025-11-17 17:09 ` [PATCH v2 4/4] tests: add tcg coverage for fixed mremap bugs Matthew Lugg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251117170954.31451-4-mlugg@mlugg.co.uk \
    --to=mlugg@mlugg.co.uk \
    --cc=laurent@vivier.eu \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).