From: Warner Losh <imp@bsdimp.com>
To: qemu-devel@nongnu.org
Cc: Warner Losh <imp@bsdimp.com>, Kyle Evans <kevans@freebsd.org>,
Richard Henderson <richard.henderson@linaro.org>
Subject: [PULL 05/11] bsd-user: Fix crazy bug with mmap
Date: Tue, 22 Sep 2026 13:21:03 -0600 [thread overview]
Message-ID: <20260922192109.98467-6-imp@bsdimp.com> (raw)
In-Reply-To: <20260922192109.98467-1-imp@bsdimp.com>
So, for some crazy reason, we checked that only the SHARED bit was set
out of all these bits. The only ones that were live was MAP_ANON and
MAP_FIXED. We never called mmap with MAP_FIXED and MAP_SHARED. And the
equivalent from linux-user tested that MAP_ANON was off, but MAP_SHARED
was on. So incorporate the more direct approach from linux-user for
testing this condition. Many of these bits haven't been actually used in
FreeBSD since the very early days....
Also fix MAP_TYPE definition. The 0xf I think was copied from the Linux
headers a long time ago, since BSD doesn't define this. In FreeBSD,
there's only two bits of this low nibble that are used for the sharing
type (linux has 2 more types encoded in these low bits). The kernel
rejects bogus flags (0xc part of 0xf mask), so it's better to have just
these two bits here.
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Warner Losh <imp@bsdimp.com>
---
bsd-user/mmap.c | 17 +++++++++++------
bsd-user/syscall_defs.h | 9 ++++++++-
2 files changed, 19 insertions(+), 7 deletions(-)
diff --git a/bsd-user/mmap.c b/bsd-user/mmap.c
index 548b4824df..5068e5c3b7 100644
--- a/bsd-user/mmap.c
+++ b/bsd-user/mmap.c
@@ -215,11 +215,15 @@ static int mmap_frag(abi_ulong real_start,
prot_new = prot | prot1;
if (fd != -1) {
- /* msync() won't work here, so we return an error if write is
- possible while it is a shared mapping */
- if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
- (prot & PROT_WRITE))
+ /*
+ * msync() won't work here, so we return an error if write is
+ * possible while it is a shared mapping
+ */
+ if (!(flags & MAP_ANON)
+ && (flags & MAP_TYPE) == MAP_SHARED
+ && (prot & PROT_WRITE)) {
return -1;
+ }
/* adjust protection to be able to read */
if (!(prot1 & PROT_WRITE))
@@ -586,8 +590,9 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
* msync() won't work here, so we return an error if write is
* possible while it is a shared mapping
*/
- if ((flags & TARGET_BSD_MAP_FLAGMASK) == MAP_SHARED &&
- (prot & PROT_WRITE)) {
+ if (!(flags & MAP_ANON)
+ && (flags & MAP_TYPE) == MAP_SHARED
+ && (prot & PROT_WRITE)) {
errno = EINVAL;
goto fail;
}
diff --git a/bsd-user/syscall_defs.h b/bsd-user/syscall_defs.h
index 9fa16e0863..c4fd5f5589 100644
--- a/bsd-user/syscall_defs.h
+++ b/bsd-user/syscall_defs.h
@@ -121,7 +121,14 @@ struct target_msgbuf {
/*
* sys/mman.h
*/
-#define TARGET_BSD_MAP_FLAGMASK 0x3ff7
+/*
+ * FreeBSD doesn't define this, but Linux does. In Linux, it's only MAP_PRIVATE
+ * and MAP_SHARED and a few Linux-specific types (0xf). FreeBSD allows other
+ * types of mapping with MAP_ANON, MAP_GUARD, and MAP_STACK. The mask on Linux
+ * excludes these: They don't have MAP_GUARD, and MAP_STACK and MAP_ANON have
+ * similar flags with different names that aren't in it's 0xf MAP_TYPE.
+ */
+#define MAP_TYPE (MAP_PRIVATE | MAP_SHARED)
/*
* sys/time.h
--
2.55.0
next prev parent reply other threads:[~2026-09-22 19:22 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-22 19:20 [PULL 00/11] Bsd user 2026q3 patches Warner Losh
2026-09-22 19:20 ` [PULL 01/11] bsd-user: HOST_BIG_ENDIAN and TARGET_BIG_ENDIAN are always Warner Losh
2026-09-22 19:21 ` [PULL 02/11] bsd-user: Drop support for FreeBSD 12 Warner Losh
2026-09-22 19:21 ` [PULL 03/11] bsd-user: Flag shared memory as needing atomics Warner Losh
2026-09-22 19:21 ` [PULL 04/11] bsd-user: Initialize per-arch child registers Warner Losh
2026-09-22 19:21 ` Warner Losh [this message]
2026-09-22 19:21 ` [PULL 06/11] bsd-user: Move tb_flush stuff to begin_parallel_context Warner Losh
2026-09-22 19:21 ` [PULL 07/11] bsd-user: Implement exterrctl(2) Warner Losh
2026-09-22 19:21 ` [PULL 08/11] bsd-user: Implement sigfastblock Warner Losh
2026-09-22 19:21 ` [PULL 09/11] bsd-user: Eliminate unused regs arg in load_elf_binary Warner Losh
2026-09-22 19:21 ` [PULL 10/11] bsd-user: #ifdef sockopt that aren't in 14 Warner Losh
2026-09-22 19:21 ` [PULL 11/11] bsd-user: Remove host-os.h Warner Losh
2026-09-23 19:44 ` [PULL 00/11] Bsd user 2026q3 patches Richard Henderson
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=20260922192109.98467-6-imp@bsdimp.com \
--to=imp@bsdimp.com \
--cc=kevans@freebsd.org \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.