From patchwork Wed Aug 30 20:34:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 718592 Delivered-To: patch@linaro.org Received: by 2002:adf:e5c2:0:b0:31d:da82:a3b4 with SMTP id a2csp121755wrn; Wed, 30 Aug 2023 13:35:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGU+rLqPRgc0OBFm+eLdHgan241IoiM8A/efy7ZnFiSDPqTgQ0v4izv3nkSh6jdY0ZMQPkC X-Received: by 2002:a05:620a:7fb:b0:76f:1b38:e748 with SMTP id k27-20020a05620a07fb00b0076f1b38e748mr852368qkk.2.1693427722788; Wed, 30 Aug 2023 13:35:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693427722; cv=none; d=google.com; s=arc-20160816; b=Z5xNeq4dG3uytZWxY98GSSXyQq0OnJMEWh7OcnnSmBH6UfBgu3voGZ0Ut/Sc8W4sU2 ZTgK0JQlc6rrcMDnXhUlSoXnWyWFCrfWryjwRe0Ebxnj00DRVonTytT2XDRMuoM5dsei 9mL0rCkDWCxLRTXPn7GNr3xiSav+V4yrKm55HHJSnRVbEZjbKNffHZnsggu96cuuT9IT Aepw3VrKQ/Z8LlRxS83f30ysbgHXfzNBk+VZ1lysSkrgn9PtSnDpMKyv5I8HejzklvUk PcopsrwlyzS7apvGAZ2WDjly9yycAyHvDlDAr/Fwq8FXVy6usiF4xp5EJfAOX3H3POMn vBIw== 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:message-id:date:subject:cc:to:from:dkim-signature; bh=WWoJcjMrli/UBoxVP+bc1AqaQlcf1mCsWLbenvoycVs=; fh=nF/kdnqdCQq/OSSMEyRQHDEjJh39CWpuaGy0RYtinc4=; b=wEClUhw4Ly4Q4vIMHWRcnECaL82fbbg9JoaGPERQ/TnR3w757gOrp2v5P4nkLX1TGV Syn9JwHR1ujJ4IpXdiCj25JKHLYfaUViqqkZ0Q9fob1N+4wEIF+4OdxJKt7+CAyNOj46 bBjBXiaMlgRRH4wOUAITijnzjV6EkJ2bnsE2g/Y29BeHmmaL7rk/G6lnNuzY7yhOo/5a eSM4J+Ka4yaFFIRL6BgSjIG4qVCsEe0eXEigLhkMwPtP2LeiFprEqiG6AV4SCAp3WF3Y fb3NW2hEUSmNGlF6KiyPb5XhSFhozP7mfwX5Cs0y1ENR+GthL+Ni1muY6YBtaIwRDRt3 mfbQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=NnBz6OPF; 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 s10-20020a05620a0bca00b0076c7efa5c38si8294797qki.616.2023.08.30.13.35.22 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 30 Aug 2023 13:35:22 -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=NnBz6OPF; 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 1qbRtr-0002p2-Ce; Wed, 30 Aug 2023 16:34:51 -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 1qbRtp-0002oJ-7B for qemu-devel@nongnu.org; Wed, 30 Aug 2023 16:34:49 -0400 Received: from mail-pf1-x435.google.com ([2607:f8b0:4864:20::435]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qbRtm-0000fQ-Nb for qemu-devel@nongnu.org; Wed, 30 Aug 2023 16:34:48 -0400 Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-68a56401b9aso67037b3a.1 for ; Wed, 30 Aug 2023 13:34:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1693427685; x=1694032485; darn=nongnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=WWoJcjMrli/UBoxVP+bc1AqaQlcf1mCsWLbenvoycVs=; b=NnBz6OPFKkfXv8Qgzbdblc7xsXWTtIw61aWPf4FqZ+S4YpJMN20z/r1UU/66+kJsNP bXEVyPKTDtps0UjqpyAbvbUFyj7afne+Oyz45SOk9v6CS106FuptmiN8o34upaw7/ffb YvbALtGZYKs1gJTZrc22KcGqX7qfSI+ZlCmuvt26Z+1LdUfKaXnPlG0leo+2PWmyx/aK FpoeUsslN1D0Y1ZyB0TD7Qmf60C0q4qvBLO/vX0keAEBU59C/NVeypzuEjj/Wlq8XOUA H32vow5+fKemYDwj9uOJcgO38XldIMlBrjd4up4NxnS1NWUCDbj5nxmuZm2c5OdPAhC0 slGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693427685; x=1694032485; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=WWoJcjMrli/UBoxVP+bc1AqaQlcf1mCsWLbenvoycVs=; b=bPgToWn86UGtMTLxH1aWGRPBfcSqMVPEhM9qxauE6WNt4xAEfbz1K91yx9B7hHHifU kAQJzUxAexVWBlil/Vn5gJaKUfAJFTKSTiKmhLZTiqsdDRWqHguKdVt44Z0WrK0ciMER zE0O7eO9M8MjP6rrWKXkOzOD+btim88/68zIhJBofGCsaFHZ5YIe+is+6e3Lnd9N4ZPv y04RC8nSfp6mDS8z1ZNwvnqe6oQZRbUZQQictF5Hs1PsAbgKR8Nx/1eOK9ethBtRuVE2 2RnThC+hXEJohuu1tXgHzm3u7RILPXia7KSaVGtek+537iEImk6/KdGAjSLKxSU0y6l6 Qu3Q== X-Gm-Message-State: AOJu0YxdHCBcQy2Om0xpLOWbYfiui+2dIDyaVHdNros/IH+Dt9TuPhBE WzkHv1FvAaiUS6s863hYF1bCeLiVHvkb97iatxY= X-Received: by 2002:a05:6a00:b45:b0:68a:4b8c:4e91 with SMTP id p5-20020a056a000b4500b0068a4b8c4e91mr3619789pfo.19.1693427684930; Wed, 30 Aug 2023 13:34:44 -0700 (PDT) Received: from stoup.. ([71.212.131.115]) by smtp.gmail.com with ESMTPSA id y17-20020aa78051000000b00688c733fe92sm10387544pfm.215.2023.08.30.13.34.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Aug 2023 13:34:44 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: thuth@redhat.com, qemu-stable@nongnu.org Subject: [PATCH] linux-user: Fixes for zero_bss Date: Wed, 30 Aug 2023 13:34:43 -0700 Message-Id: <20230830203443.1126914-1-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::435; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x435.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 autolearn=unavailable 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 The previous change, 2d385be6152, assumed !PAGE_VALID meant that the page would be unmapped by the elf image. However, since we reserved the entire image space via mmap, PAGE_VALID will always be set. Instead, assume PROT_NONE for the same condition. Furthermore, assume bss is only ever present for writable segments, and that there is no page overlap between PT_LOAD segments. Instead of an assert, return false to indicate failure. Cc: qemu-stable@nongnu.org Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1854 Fixes: 2d385be6152 ("linux-user: Do not adjust zero_bss for host page size") Signed-off-by: Richard Henderson --- linux-user/elfload.c | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index a5b28fa3e7..7bc7bcec19 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2308,21 +2308,40 @@ static bool zero_bss(abi_ulong start_bss, abi_ulong end_bss, int prot) { abi_ulong align_bss; + /* We only expect writable bss; the code segment shouldn't need this. */ + if (!(prot & PROT_WRITE)) { + return false; + } + align_bss = TARGET_PAGE_ALIGN(start_bss); end_bss = TARGET_PAGE_ALIGN(end_bss); if (start_bss < align_bss) { int flags = page_get_flags(start_bss); - if (!(flags & PAGE_VALID)) { - /* Map the start of the bss. */ + if (!(flags & PAGE_BITS)) { + /* + * The whole address space of the executable was reserved + * at the start, therefore all pages will be VALID. + * But assuming there are no PROT_NONE PT_LOAD segments, + * a PROT_NONE page means no data all bss, and we can + * simply extend the new anon mapping back to the start + * of the page of bss. + */ align_bss -= TARGET_PAGE_SIZE; - } else if (flags & PAGE_WRITE) { - /* The page is already mapped writable. */ - memset(g2h_untagged(start_bss), 0, align_bss - start_bss); } else { - /* Read-only zeros? */ - g_assert_not_reached(); + /* + * The start of the bss shares a page with something. + * The only thing that we expect is the data section, + * which would already be marked writable. + * Overlapping the RX code segment seems malformed. + */ + if (!(flags & PAGE_WRITE)) { + return false; + } + + /* The page is already mapped and writable. */ + memset(g2h_untagged(start_bss), 0, align_bss - start_bss); } }