aboutsummaryrefslogtreecommitdiff
path: root/linux-user
diff options
context:
space:
mode:
authorPeter Maydell <peter.maydell@linaro.org>2011-04-18 16:34:24 +0100
committerRiku Voipio <riku.voipio@iki.fi>2011-06-21 20:29:01 +0300
commit00faf08c951fcc351467faac5697307a86edb077 (patch)
tree3a85f8adb1b674999861b2cee5ff658e2d41d86b /linux-user
parentf3ed1f5d476bfc18c4b2e27bf2386f54c98561f1 (diff)
linux-user: Don't use MAP_FIXED in do_brk()
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 <peter.maydell@linaro.org> Signed-off-by: Riku Voipio <riku.voipio@iki.fi>
Diffstat (limited to 'linux-user')
-rw-r--r--linux-user/syscall.c29
1 files changed, 20 insertions, 9 deletions
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 5cb27c7f9f..b9757306ac 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;
}