[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 04/18] linux-user: Don't use MAP_FIXED in do_brk()
From: |
riku . voipio |
Subject: |
[Qemu-devel] [PATCH 04/18] linux-user: Don't use MAP_FIXED in do_brk() |
Date: |
Mon, 20 Jun 2011 19:20:09 +0300 |
From: Peter Maydell <address@hidden>
Since mmap() with MAP_FIXED will map over the top of existing mappings,
it's a bad idea to use it to implement brk(), because brk() with a
large size is likely to overwrite important things like qemu itself
or the host libc. So we drop MAP_FIXED and handle "mapped but at
different address" as an error case instead.
Signed-off-by: Peter Maydell <address@hidden>
Signed-off-by: Riku Voipio <address@hidden>
---
linux-user/syscall.c | 29 ++++++++++++++++++++---------
1 files changed, 20 insertions(+), 9 deletions(-)
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 5cb27c7..b975730 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -735,23 +735,34 @@ abi_long do_brk(abi_ulong new_brk)
return target_brk;
}
- /* We need to allocate more memory after the brk... */
+ /* We need to allocate more memory after the brk... Note that
+ * we don't use MAP_FIXED because that will map over the top of
+ * any existing mapping (like the one with the host libc or qemu
+ * itself); instead we treat "mapped but at wrong address" as
+ * a failure and unmap again.
+ */
new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
PROT_READ|PROT_WRITE,
- MAP_ANON|MAP_FIXED|MAP_PRIVATE, 0, 0));
+ MAP_ANON|MAP_PRIVATE, 0, 0));
+
+ if (mapped_addr == brk_page) {
+ target_brk = new_brk;
+ return target_brk;
+ } else if (mapped_addr != -1) {
+ /* Mapped but at wrong address, meaning there wasn't actually
+ * enough space for this brk.
+ */
+ target_munmap(mapped_addr, new_alloc_size);
+ mapped_addr = -1;
+ }
#if defined(TARGET_ALPHA)
/* We (partially) emulate OSF/1 on Alpha, which requires we
return a proper errno, not an unchanged brk value. */
- if (is_error(mapped_addr)) {
- return -TARGET_ENOMEM;
- }
+ return -TARGET_ENOMEM;
#endif
-
- if (!is_error(mapped_addr)) {
- target_brk = new_brk;
- }
+ /* For everything else, return the previous break. */
return target_brk;
}
--
1.7.4.1
- [Qemu-devel] [PATCH 00/18] pending linux-user patches, riku . voipio, 2011/06/20
- [Qemu-devel] [PATCH 01/18] Don't translate pointer when in restore_sigcontext, riku . voipio, 2011/06/20
- [Qemu-devel] [PATCH 05/18] arm-semi.c: Use correct check for failure of do_brk(), riku . voipio, 2011/06/20
- [Qemu-devel] [PATCH 04/18] linux-user: Don't use MAP_FIXED in do_brk(),
riku . voipio <=
- [Qemu-devel] [PATCH 02/18] linux-user: Fix the load of ELF files that have no "useful" symbol, riku . voipio, 2011/06/20
- [Qemu-devel] [PATCH 07/18] linux-user: Fix the computation of the requested heap size, riku . voipio, 2011/06/20
- [Qemu-devel] [PATCH 03/18] linux-user: Handle images where lowest vaddr is not page aligned, riku . voipio, 2011/06/20
- [Qemu-devel] [PATCH 06/18] m68k-semi.c: Use correct check for failure of do_brk(), riku . voipio, 2011/06/20
- [Qemu-devel] [PATCH 08/18] linux-user: add pselect6 syscall support, riku . voipio, 2011/06/20
- [Qemu-devel] [PATCH 09/18] linux-user: Define AT_RANDOM to support target stack protection mechanism., riku . voipio, 2011/06/20