From patchwork Thu Aug 3 01:53:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 709589 Delivered-To: patch@linaro.org Received: by 2002:ab3:5b92:0:b0:238:c968:2573 with SMTP id j18csp1354405ltf; Wed, 2 Aug 2023 18:55:39 -0700 (PDT) X-Google-Smtp-Source: APBJJlEDdqMuKKs1OzBMKwgyrgP4AJLqT3VnCR88AtQLZsLuvsmvU34dv5MZf5Uud/8BNTi3mmzp X-Received: by 2002:ac8:4e8c:0:b0:405:4393:a001 with SMTP id 12-20020ac84e8c000000b004054393a001mr24344240qtp.2.1691027738836; Wed, 02 Aug 2023 18:55:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691027738; cv=none; d=google.com; s=arc-20160816; b=hPK4ERfwRKVl2tJYwUvWKrGPnTk1Al5cWS3VE2w8yBA/Xa7YjDgEy9PgjqPNjBTitO awd0Nusf/COGP5IUYhg5usQo0a9OoRTo3RAlHOgrX2rpktu8WbKaOm8mj+T+nlfQvxtI 1c6Z4lFJIcRF+KI/mMYyJ/T+wIDqw+a+l4VSUrElqzDSyxobeQMlRmfkuloy9aml4sHv nmtcZcZwQA42U59k5P4147rNQwAoQ6rlVIFIu2zLn8EznYczjCe3TNGsA0a3trhLZ4+s 2c6NEA2kmnEgtS59EoCI361YFrWsbY/BHLaZYKvfVAt8vXHYu+TtSQoWYF+p1GsivVCW TxRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ChEUXUgAPSYhXU4GN1bgmHxBnAzewX2kPCgTDo1jgOA=; fh=fmVFRXV+XPDbp8+eafcQB359/x6dDRQDsVzPMZJ0Hao=; b=zA6NGtogdZ9OUvnh4UmYIgcDJwg73GA0JuX/uC/ZPCa4hQY9p4aINHb16Yaq3a81mp /sPJNNPaNOfKs9CkojjRq+i2zN3Oxp/NDnLEgrjH07MXuO+k846ZUOtUdAnqclBIkHIN qCTaCX+soAbOJESsAsPXxpDaoLrLsnBBXzR/t+7XiaVlwURnuh0IvPP4rIIsAQbe042r Cm4btPmXORbiycC5OkFTFfiDYMlGqhwy8FJcd1YLE/ux89L01kUOxPixmJVpSBgq2kh8 pee6MSw/uTWpmxrxMf4wwVEJTwOUrBeGDvxUocPC8reFD6up/koG7dtPY0WsHQQgDe4W 6zdw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="buVzk17/"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id t21-20020ac85895000000b00403df34987asi9252133qta.376.2023.08.02.18.55.38 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 02 Aug 2023 18:55:38 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="buVzk17/"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qRNWi-0000iv-5K; Wed, 02 Aug 2023 21:53:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qRNWg-0000i6-6K for qemu-devel@nongnu.org; Wed, 02 Aug 2023 21:53:18 -0400 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qRNWd-0000oN-LM for qemu-devel@nongnu.org; Wed, 02 Aug 2023 21:53:17 -0400 Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-686be3cbea0so1083315b3a.0 for ; Wed, 02 Aug 2023 18:53:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691027594; x=1691632394; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ChEUXUgAPSYhXU4GN1bgmHxBnAzewX2kPCgTDo1jgOA=; b=buVzk17/UzMaI0kKJj0C9HpUwQbWclgya3adJJYiPRubCo2UM0hIZzLQd5f8aDJ7GR CCQCGEyNyEVYJrIK2mhlm/Op0Or1nah3m5Lw0Ica3kfORuqXEgr2Zap9SfLOefBIKCE7 bpgycsabfJMzwJm0iEg4gYMlTz4LNbTRkczu7alcPrg2iyXd4W5VEmbySEOTjfx0W6E3 iCvkgYpmY37W5ellBLXQAC2UmuhMgLY/ZkdR5NjSkjfMpAkzV4DFdvkcA5CdT9J6fNaH hYZyj6xqloIljZSGDkgG+sXSU56lWsYl7RxcmQZaRSV87sahqnOzGQTscK25KerH/7jX eV2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691027594; x=1691632394; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ChEUXUgAPSYhXU4GN1bgmHxBnAzewX2kPCgTDo1jgOA=; b=V/hT223VrcixJqq9Y5Z+GCTgjUdMgbKyDxFjbNONhUnA8P3KKwe1Pg/bjPdvTicKEf smWOxePKMBeY+VyGe8zwe19GTHCdPH0rpAe24XQ/dqXgcn03/JYf61ys80Zan9XeT6RH LBaZtONISS348wiBuqirZOvNxvTeltoNwZ+sliTb+XLO87wzn7NfGVOisViJDvyE/+ZO abQV+Uu39IS3ETiBQ8JL5VzlQzNxk3FTHdgRrx6JNXlU1USiQ2N9F7nQ+XH2LBvUHQ0l RC9QGIrJiwGkU4btWTvvUasSLcCk3eLn3Ya6ApKcSL2qNx+vGggPj0LbhRLEyMZQl/yd TGZw== X-Gm-Message-State: ABy/qLZhnxJ1M+F2KYVnoINjUDpUlDu2d7iPA95NGITPqZgeHhKZbbf8 9s/613lNB5PUMiUbyE7agHTGZ2NGQs8GWok3aT8= X-Received: by 2002:a05:6a20:72a5:b0:132:d09f:1716 with SMTP id o37-20020a056a2072a500b00132d09f1716mr18620596pzk.2.1691027594197; Wed, 02 Aug 2023 18:53:14 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:b659:bbad:1a22:7ef9]) by smtp.gmail.com with ESMTPSA id s25-20020a63a319000000b005649cee408fsm157123pge.0.2023.08.02.18.53.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Aug 2023 18:53:13 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: deller@gmx.de, laurent@vivier.eu, akihiko.odaki@daynix.com, joel@jms.id.au Subject: [PATCH v7 12/14] linux-user: Use elf_et_dyn_base for ET_DYN with interpreter Date: Wed, 2 Aug 2023 18:53:00 -0700 Message-Id: <20230803015302.407219-13-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230803015302.407219-1-richard.henderson@linaro.org> References: <20230803015302.407219-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::430; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x430.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Follow the lead of the linux kernel in fs/binfmt_elf.c, in which an ET_DYN executable which uses an interpreter (usually a PIE executable) is loaded away from where the interpreter itself will be loaded. Signed-off-by: Richard Henderson --- linux-user/elfload.c | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 2aee2298ec..fef9a0bc8f 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -3021,7 +3021,7 @@ static void load_elf_image(const char *image_name, int image_fd, struct elfhdr *ehdr = (struct elfhdr *)bprm_buf; struct elf_phdr *phdr; abi_ulong load_addr, load_bias, loaddr, hiaddr, error; - int i, retval, prot_exec; + int i, retval, prot_exec, load_map_flags; Error *err = NULL; /* First of all, some simple consistency checks */ @@ -3106,6 +3106,19 @@ static void load_elf_image(const char *image_name, int image_fd, } } + load_addr = loaddr; + + /* + * For ET_EXEC, load_addr is required. Use MAP_FIXED_NOREPLACE instead + * of MAP_FIXED on the off-chance that the guest address overlaps a + * host address. There should be no other conflict this early in the + * loading process. + */ + if (ehdr->e_type == ET_EXEC) { + load_map_flags = MAP_FIXED_NOREPLACE; + } else { + load_map_flags = 0; + } if (pinterp_name != NULL) { /* * This is the main executable. @@ -3135,11 +3148,34 @@ static void load_elf_image(const char *image_name, int image_fd, */ probe_guest_base(image_name, loaddr, hiaddr); } else { + abi_ulong align; + /* * The binary is dynamic, but we still need to * select guest_base. In this case we pass a size. */ probe_guest_base(image_name, 0, hiaddr - loaddr); + + /* + * Avoid collision with the loader by providing a different + * default load address. + */ + load_addr = loaddr + elf_et_dyn_base; + + /* + * TODO: Better support for mmap alignment is desirable. + * Without reserved_va we would prefer any host conflict be + * resolved by choosing a different address, therefore we + * don't want to use MAP_FIXED. But without that we cannot + * cannot guarantee alignment, only suggest it. + */ + align = pow2ceil(info->alignment); + if (align) { + load_addr &= -align; + } + if (reserved_va) { + load_map_flags = MAP_FIXED_NOREPLACE; + } } } @@ -3157,10 +3193,9 @@ static void load_elf_image(const char *image_name, int image_fd, * In both cases, we will overwrite pages in this range with mappings * from the executable. */ - load_addr = target_mmap(loaddr, (size_t)hiaddr - loaddr + 1, PROT_NONE, + load_addr = target_mmap(load_addr, (size_t)hiaddr - loaddr + 1, PROT_NONE, MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | - (ehdr->e_type == ET_EXEC ? MAP_FIXED : 0), - -1, 0); + load_map_flags, -1, 0); if (load_addr == -1) { goto exit_mmap; }