From patchwork Sat Jul 1 06:55:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 698314 Delivered-To: patch@linaro.org Received: by 2002:adf:fcc5:0:0:0:0:0 with SMTP id f5csp813283wrs; Fri, 30 Jun 2023 23:57:04 -0700 (PDT) X-Google-Smtp-Source: APBJJlHyQScGn8aY9aq7HLa3OfLaCMiVzCjczkYPtgzGNqmw6c2Pf3wgXpSiJEXVel5swf1kxSqo X-Received: by 2002:a05:620a:4096:b0:765:a6a1:94b3 with SMTP id f22-20020a05620a409600b00765a6a194b3mr7066937qko.73.1688194624523; Fri, 30 Jun 2023 23:57:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688194624; cv=none; d=google.com; s=arc-20160816; b=HvLsRMnIKf4irh52vPxoVP1xQhXPuGqBhGl5Npa3OSn1EdbhLe4FUXwpwRo+aA3Gh8 q1sxOEc2TMF6bzxLnMwMKoK4MnpZP8YMH2YoFNfcNzhGHclp8tgfycdXvba+qWai9JB+ cG0CSMUpHSUP6oFrF7R3R/Ry6fpSFWw5yWSWSWM5psL7FxJOj+EOhHqbVGIYu9DaBooa tUXTx6bKnCq755S+KM2MZCbKjr4zettiImM+N3rd+DKHyLhT9tZyV1in6VIIlEA/hiCf vrInwTVt8dstuFX8/lUDqgMQUkyhgCb2QjFh2GodrlfGgbkP45cYdvJK0bR/1JHrZX0J uhjg== 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=H+qtKArYQsrn/b+NQpVzVpVv88M5134YWuGW3InQxAY=; fh=L7empYteKoZwGFCGMlp9qDTgHTnX02hHuViDUGZLQvE=; b=xDwhWc3UIIwszrCnZhhQBeYxzsaTKEY8EiFlsyKVzsp+mj9s3GbQZNEk1BDn61GRUd oT+OelEZTr8+14YsSpSzaorNziy7MqrTfxLVk7okKLr1RGr1Ag7b0zkReemViAGxgunP MChyMqxTBkm/wxeo54aQcDOg0J/7TUujclH6++O5E9kAGdp7FlH8OHbVkmiHAe2DAm4F V1WNuAh8RtdgMcTV+iC2eie9JUoApIZAjQUXKZUEigIytyUYXCRcvL28Yu/jn0ePSXEF J6CmAgvEISI7RoJKEMlAR7zTay9f3R2BfJTz09z5eq0HnuUZo5yeXplEVJ7z3ytDxMrs w9SQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=cAZnbS3B; 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 tn8-20020a05620a3c0800b007655a4d2ab0si7083334qkn.85.2023.06.30.23.57.04 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 30 Jun 2023 23:57:04 -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=cAZnbS3B; 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 1qFUVx-0007YD-Bz; Sat, 01 Jul 2023 02:55:25 -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 1qFUVv-0007Wa-Dm for qemu-devel@nongnu.org; Sat, 01 Jul 2023 02:55:23 -0400 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qFUVt-0007GD-Hb for qemu-devel@nongnu.org; Sat, 01 Jul 2023 02:55:23 -0400 Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-3fbc0981756so24377305e9.0 for ; Fri, 30 Jun 2023 23:55:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1688194520; x=1690786520; 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=H+qtKArYQsrn/b+NQpVzVpVv88M5134YWuGW3InQxAY=; b=cAZnbS3BQNJwY0a1ELCkEpvhj8/hYssPgwQk1GF1cuHbY1XVNqKzy0H6kO7ZyaGDRs tY8le5Ov4apcusKs9G4349GneSyuJ4h7spledhwUMujROPtQI/HQXTN3n0Zk+8THyRlD 5JiPbPK3+btqICAnxTKCt5kUpLUvooJ64G2sPvluAF+gIeWhIhDBZDo5HGunGXbxEylV P0Z8Ti9x6lfbW8eticMphotCT7CP9wHMkfLDfPgpD9MRod7yuQzFg5G8H1Q4012fZk3P du6o0oSPD4+NOo//PvN8z0PiGVQTYjXMVHmS9kp90ZH8ckGO63iT2kGRjqZqwZGh8pUL i1cA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688194520; x=1690786520; 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=H+qtKArYQsrn/b+NQpVzVpVv88M5134YWuGW3InQxAY=; b=Diq47pCWTDzuK7NNpQIPbUn9ML+AozOB83H5z/yA7QrvzaLGTLZJVUrvIUcrPsVBT2 XsbhwTeLTR206qLlB7iV3OXXObz5+wRL28Wjay53U4CeSsioVj9vOuQu49gcWiax3ANB jsu2Wxbx+35QA5cTx5QxMHWTajZlPq6DkpTlSQI/mXpNgTAwaUrQsDxwRL/WbyV7u4Uy SiFYjDDPaPP0n5OIwmcjvibv/09zrdD/aeV3AlzXsl/qZLUm3qJ1fIJSXaueL7AUaL8I nZpRL6kGL8xjXg4QZdoPVCNp++4UWA1nqD4Q4aZOiLWww3M0453XYfBbN4laAiOxxfcO pwJQ== X-Gm-Message-State: AC+VfDwZgz5h8mpL+LJPGPk5Qn1BaoWKWMrUVcNguaJAa4UE1PZ1J+aW gbN2Ova6oetr7+42IJ3ecDeMVSxDTXxN4voG7EI1tg== X-Received: by 2002:a1c:7908:0:b0:3f8:fb7c:6793 with SMTP id l8-20020a1c7908000000b003f8fb7c6793mr3248980wme.24.1688194520279; Fri, 30 Jun 2023 23:55:20 -0700 (PDT) Received: from localhost.localdomain ([139.47.41.96]) by smtp.gmail.com with ESMTPSA id w10-20020a1cf60a000000b003f9bd9e3226sm20874918wmc.7.2023.06.30.23.55.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jun 2023 23:55:20 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: qemu-stable@nongnu.org, Michael Tokarev Subject: [PULL 11/11] linux-user: Avoid mmap of the last byte of the reserved_va Date: Sat, 1 Jul 2023 08:55:10 +0200 Message-Id: <20230701065510.514743-12-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230701065510.514743-1-richard.henderson@linaro.org> References: <20230701065510.514743-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::32e; envelope-from=richard.henderson@linaro.org; helo=mail-wm1-x32e.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 There is an overflow problem in mmap_find_vma_reserved: when reserved_va == UINT32_MAX, end may overflow to 0. Rather than a larger rewrite at this time, simply avoid the final byte of the VA, which avoids searching the final page, which avoids the overflow. Cc: qemu-stable@nongnu.org Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1741 Fixes: 95059f9c ("include/exec: Change reserved_va semantics to last byte") Signed-off-by: Richard Henderson Tested-by: Michael Tokarev Reviewed-by: Michael Tokarev Message-Id: <20230629080835.71371-1-richard.henderson@linaro.org> --- linux-user/mmap.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 0aa8ae7356..2692936773 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -281,9 +281,15 @@ static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size, /* Note that start and size have already been aligned by mmap_find_vma. */ end_addr = start + size; + /* + * Start at the top of the address space, ignoring the last page. + * If reserved_va == UINT32_MAX, then end_addr wraps to 0, + * throwing the rest of the calculations off. + * TODO: rewrite using last_addr instead. + * TODO: use the interval tree instead of probing every page. + */ if (start > reserved_va - size) { - /* Start at the top of the address space. */ - end_addr = ((reserved_va + 1 - size) & -align) + size; + end_addr = ((reserved_va - size) & -align) + size; looped = true; } @@ -296,8 +302,8 @@ static abi_ulong mmap_find_vma_reserved(abi_ulong start, abi_ulong size, /* Failure. The entire address space has been searched. */ return (abi_ulong)-1; } - /* Re-start at the top of the address space. */ - addr = end_addr = ((reserved_va + 1 - size) & -align) + size; + /* Re-start at the top of the address space (see above). */ + addr = end_addr = ((reserved_va - size) & -align) + size; looped = true; } else { prot = page_get_flags(addr);