From patchwork Mon Apr 18 15:34:24 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 91789 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id E25DD1007DD for ; Tue, 19 Apr 2011 01:35:03 +1000 (EST) Received: from localhost ([::1]:58252 helo=lists2.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QBqTb-0000LE-RI for incoming@patchwork.ozlabs.org; Mon, 18 Apr 2011 11:34:59 -0400 Received: from eggs.gnu.org ([140.186.70.92]:34713) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QBqTF-0000Gq-6n for qemu-devel@nongnu.org; Mon, 18 Apr 2011 11:34:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QBqTE-0006Mb-C6 for qemu-devel@nongnu.org; Mon, 18 Apr 2011 11:34:37 -0400 Received: from mnementh.archaic.org.uk ([81.2.115.146]:38149) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QBqTE-0006LB-4N for qemu-devel@nongnu.org; Mon, 18 Apr 2011 11:34:36 -0400 Received: from pm215 by mnementh.archaic.org.uk with local (Exim 4.72) (envelope-from ) id 1QBqT4-0007dp-UI; Mon, 18 Apr 2011 16:34:26 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Date: Mon, 18 Apr 2011 16:34:24 +0100 Message-Id: <1303140866-29348-2-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 1.7.2.5 In-Reply-To: <1303140866-29348-1-git-send-email-peter.maydell@linaro.org> References: <1303140866-29348-1-git-send-email-peter.maydell@linaro.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 81.2.115.146 Cc: Riku Voipio , patches@linaro.org Subject: [Qemu-devel] [PATCH 1/3] linux-user: Don't use MAP_FIXED in do_brk() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- 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 bb0999d..e68c5e0 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -733,23 +733,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; }