From: Warner Losh <imp@bsdimp.com>
To: qemu-devel@nongnu.org
Cc: Kyle Evans <kevans@freebsd.org>, Warner Losh <imp@bsdimp.com>,
Maksym Sobolyev <sobomax@sippysoft.com>
Subject: [PATCH 13/14] bsd-user: Fix non-PIE binaries
Date: Sat, 19 Sep 2026 23:20:49 -0600 [thread overview]
Message-ID: <20260919-misc-2025q4-v1-13-ebfa61049bd7@bsdimp.com> (raw)
In-Reply-To: <20260919-misc-2025q4-v1-0-ebfa61049bd7@bsdimp.com>
From: Maksym Sobolyev <sobomax@sippysoft.com>
When loading non-PIE binary (could be either static or dymanic),
make sure to set MAP_EXCL while mapping the program text. Otherwise
if qemu is also non-PIE it might be loaded at the very same
address, effectively self-destructing after mapping target binary
over its own text area.
It is not a problem for fully dynamic binaries loading, as in
that case the initial mapping is non-fixed, so the kernel would
pick some non-conflicting area for us.
32625 qemu-x86_64 CALL openat(AT_FDCWD,0x7fffffffe240,0<O_RDONLY>)
32625 qemu-x86_64 NAMI "/usr/bin/cc"
32625 qemu-x86_64 RET openat 3
[...]
32625 qemu-x86_64 CALL mmap(0x809222000,0x20001000,0x7<PROT_READ|PROT_WRITE|PROT_EXEC>,0x1012<MAP_PRIVATE|MAP_FIXED|MAP_ANON>,0xffffffff,0)
32625 qemu-x86_64 RET mmap 34512969728/0x809222000
32625 qemu-x86_64 CALL mprotect(0x809222000,0x1000,0<PROT_NONE>)
32625 qemu-x86_64 RET mprotect 0
32625 qemu-x86_64 CALL mmap(0,0x458,0x3<PROT_READ|PROT_WRITE>,0x1002<MAP_PRIVATE|MAP_ANON>,0xffffffff,0)
32625 qemu-x86_64 RET mmap 35049844736/0x829223000
32625 qemu-x86_64 CALL minherit(0x829223000,0x458,INHERIT_ZERO)
32625 qemu-x86_64 RET minherit 0
32625 qemu-x86_64 CALL getrandom(0x7fffffffde10,0x28,0)
32625 qemu-x86_64 RET getrandom 40/0x28
32625 qemu-x86_64 CALL mmap(0x200000,0x2053000,0x1<PROT_READ>,0x12<MAP_PRIVATE|MAP_FIXED>,0x3,0)
32625 qemu-x86_64 RET mmap 2097152/0x200000
32625 qemu-x86_64 PSIG SIGSEGV SIG_DFL code=SEGV_ACCERR
32625 qemu-x86_64 RET nanosleep -1 errno 4 Interrupted system call
Signed-off-by: Maksym Sobolyev <sobomax@sippysoft.com>
Reviewed-by: Warner Losh <imp@bsdimp.com>
Signed-off-by: Warner Losh <imp@bsdimp.com>
---
bsd-user/elfload.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/bsd-user/elfload.c b/bsd-user/elfload.c
index 295dad4643..eaa8676f5a 100644
--- a/bsd-user/elfload.c
+++ b/bsd-user/elfload.c
@@ -561,12 +561,14 @@ load_elf_sections(const struct elfhdr *hdr, struct elf_phdr *phdr, int fd,
elf_prot |= PROT_EXEC;
}
+ int flags = MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE;
+ if (rbase == 0) {
+ flags |= MAP_EXCL;
+ }
error = target_mmap(TARGET_ELF_PAGESTART(rbase + elf_ppnt->p_vaddr),
(elf_ppnt->p_filesz +
TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
- elf_prot,
- (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
- fd,
+ elf_prot, flags, fd,
(elf_ppnt->p_offset -
TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)));
if (error == -1) {
--
2.55.0
next prev parent reply other threads:[~2026-09-20 5:23 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-20 5:20 [PATCH 00/14] bsd-user: Upstream a few fixes Warner Losh
2026-09-20 5:20 ` [PATCH 01/14] bsd-user: HOST_BIG_ENDIAN and TARGET_BIG_ENDIAN are always Warner Losh
2026-09-20 21:57 ` Richard Henderson
2026-09-20 5:20 ` [PATCH 02/14] bsd-user: Drop support for FreeBSD 12 Warner Losh
2026-09-21 18:35 ` Pierrick Bouvier
2026-09-20 5:20 ` [PATCH 03/14] bsd-user: Flag shared memory as needing atomics Warner Losh
2026-09-20 21:59 ` Richard Henderson
2026-09-20 5:20 ` [PATCH 04/14] bsd-user: Add gdb for system calls Warner Losh
2026-09-20 22:02 ` Richard Henderson
2026-09-21 10:06 ` Warner Losh
2026-09-20 5:20 ` [PATCH 05/14] bsd-user: Initialize per-arch child registers Warner Losh
2026-09-20 22:07 ` Richard Henderson
2026-09-20 5:20 ` [PATCH 06/14] bsd-user: Fix crazy bug with mmap Warner Losh
2026-09-20 22:09 ` Richard Henderson
2026-09-21 2:38 ` Warner Losh
2026-09-20 5:20 ` [PATCH 07/14] bsd-user: Move tb_flush stuff to begin_parallel_context Warner Losh
2026-09-20 22:11 ` Richard Henderson
2026-09-20 5:20 ` [PATCH 08/14] bsd-user: Fix MAP_TYPE definition Warner Losh
2026-09-20 22:12 ` Richard Henderson
2026-09-20 5:20 ` [PATCH 09/14] bsd-user: Implement exterrctl(2) Warner Losh
2026-09-21 18:34 ` Pierrick Bouvier
2026-09-20 5:20 ` [PATCH 10/14] bsd-user: Implement sigfastblock Warner Losh
2026-09-21 18:34 ` Pierrick Bouvier
2026-09-20 5:20 ` [PATCH 11/14] bsd-user: Eliminate unused regs arg in load_elf_binary Warner Losh
2026-09-20 22:14 ` Richard Henderson
2026-09-20 5:20 ` [PATCH 12/14] bsd-user: #ifdef sockopt that aren't in 14 Warner Losh
2026-09-21 18:32 ` Pierrick Bouvier
2026-09-20 5:20 ` Warner Losh [this message]
2026-09-20 22:33 ` [PATCH 13/14] bsd-user: Fix non-PIE binaries Richard Henderson
2026-09-21 1:06 ` Warner Losh
2026-09-20 5:20 ` [PATCH 14/14] bsd-user: Remove host-os.h Warner Losh
2026-09-20 22:34 ` 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=20260919-misc-2025q4-v1-13-ebfa61049bd7@bsdimp.com \
--to=imp@bsdimp.com \
--cc=kevans@freebsd.org \
--cc=qemu-devel@nongnu.org \
--cc=sobomax@sippysoft.com \
/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.