From patchwork Thu Jun 29 08:08:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 697681 Delivered-To: patch@linaro.org Received: by 2002:adf:e885:0:0:0:0:0 with SMTP id d5csp4761785wrm; Thu, 29 Jun 2023 01:09:17 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7maXrMjDRjnRDuaDS+Z6iee+XBUyWezsl7sV3e0xurlACvG2GihwbxuQrqtcdOnLOimnrn X-Received: by 2002:a05:620a:d8c:b0:75b:23a1:8e68 with SMTP id q12-20020a05620a0d8c00b0075b23a18e68mr24060704qkl.57.1688026157063; Thu, 29 Jun 2023 01:09:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688026157; cv=none; d=google.com; s=arc-20160816; b=OHSOoyEyxMNTP48i/7wSYzUgcuUf0RHq2CMFbHfxigc01AatJylW8EKrtxaClc4c+7 ibDeLX+H+qkA7qNLFYvBstWhEwLpVykm3haZRsv9M6/k5IKIbagN/Vrihy570WjExcTP 6bm13HDcfsd4/skBB8+wviWbG5cQ3Ag5pofnoXEo21XVDTPj4/IpKVfxlXvsgsCCfmwi lBb7O9rpXuIa5OBL1MeTeIj0NMhUu583UEA39op+FbaLIS/sB76oOWbeUL3QeRj4vzg9 Q4S7ddat52Likq+K47488ET9gtAQ2xVFsJqrgky2szNqkP2aGdBP3NPvUXy8IFf1sB04 7W6Q== 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=9eMAB43Rqt4O7Kkto7G7nYb0ls8Z9zu/GUhcxBLdkTs=; fh=xPhpA0GFN7P/wg2DtehxizlVkg1wgALdMI7ZhnCl49o=; b=QTQyANLWmPYy7bPghtsctG7/DyuFtApltDFcxVcjjbnH4cCxwPEb/pYiaZcv3rXgpR 7k32TVo8K+UVkZBtaHDOMzMuLMq9dT5JXRYryNnW5fIWnaLbaeGwBh1MynM9HdQe9fSk 6xg7MzmCBt8MuJXKl512LK77EerlJr2xLMRfPNErHJT4b3ddB1uBhFhmgysGtxfkAPA3 V7NsS03z0n/wsQ+nb9jfeNHVR1xI9IibnSjIxNPgYylJLPCeXnCBCju0i5efok54UjKo uMuEgrfVe0LdLQ4PCnVhOxX8BBC3fACd7DQhhCz2UfR0QwKmmuJ0NUbdiWrvr+37+dI5 ibjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Bm6SjvWz; 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 f25-20020a37ad19000000b00765bfc4a6fdsi4329708qkm.652.2023.06.29.01.09.16 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 29 Jun 2023 01:09:17 -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=Bm6SjvWz; 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 1qEmhl-0004af-I0; Thu, 29 Jun 2023 04:08:41 -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 1qEmhk-0004aA-65 for qemu-devel@nongnu.org; Thu, 29 Jun 2023 04:08:40 -0400 Received: from mail-wm1-x335.google.com ([2a00:1450:4864:20::335]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qEmhi-0005yI-C4 for qemu-devel@nongnu.org; Thu, 29 Jun 2023 04:08:39 -0400 Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-3fbc060a4d4so964595e9.2 for ; Thu, 29 Jun 2023 01:08:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1688026116; x=1690618116; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=9eMAB43Rqt4O7Kkto7G7nYb0ls8Z9zu/GUhcxBLdkTs=; b=Bm6SjvWzv4K7j+CYyFpBxSWfvx51jHCc75c4YCJSvwJvWrpMBAYYMdddbSu3U7l2Rb FsmyxmSmmWITfWULMZG3Wm3Z45nsmPudtcG3yXq/vErqu96pdB/WeABcXHXHPSpnQeHV VZ9pG0aw74sD7hsfpVyQXT/eu3uSesM2pvQwiqT4k9fCShOFKZ6729OBROjoi/FumRBD Hh9zxx0INbPC9LNwDY6o7ZHb01P/ujFfhnp+HdBndlm1oH6ILr+Zt0J9q+SSz/GCRvUR DRg3UwFdKW90uSN46ZNhNCPrIDPxJ3pMEh/WI5FDxOCHtfEBjlAOT+3x2OLcPgCDC5hH s87A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688026116; x=1690618116; 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=9eMAB43Rqt4O7Kkto7G7nYb0ls8Z9zu/GUhcxBLdkTs=; b=O4i3BeLC7DZsqI6PIshXmaPE4iZrQiPASGyxUDr/gSemk76CRqM6sUrmXvpN8WTuDA IhL/y5JZ1G2lvcZv3uaDOCozjDZsy/Ql6WayPJVkbpVyTLfhBuOsavNb3FC0Q8Rsm21Q c7u/ToItEAaKHCXqRFdMer6QmShExMvkW2WSBRoS8xShKY4iLodHQssnQ6C/2IhYcAPB Tjo5LeBcxqZNF4YDFEjEOog38T/3rqJs0KuE48osZBvOcF1vEpJvkKTvFbvJeZITz3mB umDGFAZqFcNMJYS9WNXOPTcn+olDSthrUk8XdyBjSwId8ctHnLtYVfihsjIbdXCFihoo jK0g== X-Gm-Message-State: AC+VfDwd9uUKWGvf+Xdw+I9jk0Rw/TPWiikpSl8IaqYKUGFSq2/4c1bW c+sFxGP7Oh6o6P6qlTY6/xS2YozINaTSzNiDXLTFC+q4 X-Received: by 2002:a05:600c:22ca:b0:3fb:a917:b769 with SMTP id 10-20020a05600c22ca00b003fba917b769mr4945398wmg.21.1688026116363; Thu, 29 Jun 2023 01:08:36 -0700 (PDT) Received: from localhost.localdomain ([139.47.41.96]) by smtp.gmail.com with ESMTPSA id i18-20020adff312000000b0030fb4b55c13sm68976wro.96.2023.06.29.01.08.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Jun 2023 01:08:36 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: mjt@tls.msk.ru, qemu-stable@nongnu.org Subject: [PATCH] linux-user: Avoid mmap of the last byte of the reserved_va Date: Thu, 29 Jun 2023 10:08:35 +0200 Message-Id: <20230629080835.71371-1-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::335; envelope-from=richard.henderson@linaro.org; helo=mail-wm1-x335.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 --- 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);