aboutsummaryrefslogtreecommitdiff
path: root/linux-user/syscall.c
diff options
context:
space:
mode:
authorHelge Deller <deller@gmx.de>2023-07-17 12:27:13 +0200
committerHelge Deller <deller@gmx.de>2023-07-18 20:42:05 +0200
commitdfe49864afb06e7e452a4366051697bc4fcfc1a5 (patch)
treeec22a25082c603fd2b77351f42f4f85face4f4de /linux-user/syscall.c
parent15ad98536ad9410fb32ddf1ff09389b677643faa (diff)
downloadqemu-dfe49864afb06e7e452a4366051697bc4fcfc1a5.zip
qemu-dfe49864afb06e7e452a4366051697bc4fcfc1a5.tar.gz
qemu-dfe49864afb06e7e452a4366051697bc4fcfc1a5.tar.bz2
linux-user: Prohibit brk() to to shrink below initial heap address
Since commit 86f04735ac ("linux-user: Fix brk() to release pages") it's possible for userspace applications to reduce their memory footprint by calling brk() with a lower address and free up memory. Before that commit guest heap memory was never unmapped. But the Linux kernel prohibits to reduce brk() below the initial memory address which is set at startup by the set_brk() function in binfmt_elf.c. Such a range check was missed in commit 86f04735ac. This patch adds the missing check by storing the initial brk value in initial_target_brk and verify any new brk addresses against that value. Tested with the i386 upx binary from https://github.com/upx/upx/releases/download/v4.0.2/upx-4.0.2-i386_linux.tar.xz Signed-off-by: Helge Deller <deller@gmx.de> Tested-by: "Markus F.X.J. Oberhumer" <markus@oberhumer.com> Fixes: 86f04735ac ("linux-user: Fix brk() to release pages") Cc: qemu-stable@nongnu.org Buglink: https://github.com/upx/upx/issues/683
Diffstat (limited to 'linux-user/syscall.c')
-rw-r--r--linux-user/syscall.c8
1 files changed, 7 insertions, 1 deletions
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index ee54eed..125fcbe 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -801,12 +801,13 @@ static inline int host_to_target_sock_type(int host_type)
return target_type;
}
-static abi_ulong target_brk;
+static abi_ulong target_brk, initial_target_brk;
static abi_ulong brk_page;
void target_set_brk(abi_ulong new_brk)
{
target_brk = TARGET_PAGE_ALIGN(new_brk);
+ initial_target_brk = target_brk;
brk_page = HOST_PAGE_ALIGN(target_brk);
}
@@ -824,6 +825,11 @@ abi_long do_brk(abi_ulong brk_val)
return target_brk;
}
+ /* do not allow to shrink below initial brk value */
+ if (brk_val < initial_target_brk) {
+ brk_val = initial_target_brk;
+ }
+
new_brk = TARGET_PAGE_ALIGN(brk_val);
new_host_brk_page = HOST_PAGE_ALIGN(brk_val);