From patchwork Sun Jul 2 18:04:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Tokarev X-Patchwork-Id: 698427 Delivered-To: patch@linaro.org Received: by 2002:adf:fcc5:0:0:0:0:0 with SMTP id f5csp1409611wrs; Sun, 2 Jul 2023 11:07:25 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ71T3J/3oOaC3MByLzF1bQ6yCwglGSzLCGZFlRKSnUmNQz79lKuh7ZXEidhtUB+7DjQDWON X-Received: by 2002:a05:620a:4485:b0:765:a8dc:f5c8 with SMTP id x5-20020a05620a448500b00765a8dcf5c8mr17394146qkp.35.1688321245072; Sun, 02 Jul 2023 11:07:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688321245; cv=none; d=google.com; s=arc-20160816; b=Wq68hKr7KG00memuaARq0h3RmIfhBzuiN/2KbJXZ6CWm2wjNr26TcJ+tRk8zx7vWC6 lXk5K4d449oZvDK+iSB45BJ4wIiBszYJQW0J7d9yabEDMLa/h/rpgqYpLltk6VRl0LcD 7yc65UTY/PPuoDd9quk7X5ZZhmsiIJUsqwuh012ExxosS2wZdOPl1jCNDxLvlo3fVj50 4bgsbDkH/D4RK/XgLRBASCX/U2aYpUbFAmjpcbRYrhD61ck+LGXKzkvom7uGMK9aKRQb yf8FwOaQPzzieaoNsDGfGs6x5clTZtcMOn2CUXyqjDTW7/dALSJT0SBQhGrNdP+8+h+p WfaA== 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; bh=0z9zfzX8bzVKsYQquAYjejnRcWyDzll+n9ZgK9kuH2k=; fh=kJtboUZ4qY37EseMX+JGdtKOjera+arftgaffDg8FNU=; b=sU1NxL917xCecN0X1yQvBj972w6cuYZUn0C+jZd0buTU0KiGI6uVh8iPyC7ZfWCgy+ sKCQEzDX0tm4+8/QZ6Z8PXxIYnTEtqeBtSrOmyaI9icqRc03kSfp90qBbNFhqudpDTTT 2P4YV3P739TB4n7AQSdGDwbNS0oak/tW9d0tSHv5dcFhFfrCCjSCA0i25mYPVHOTffxk wMzRWwMZELr9O/Dz8Sox7IAre6E2vS335FGJB2AU7tcS9qhSnGLjufeZRf/HXXaeYPY6 b/QLXH9fXN/WsvranzYrxgcKfDLK5pXHb25O5FyIFW9UYSiCyF6sPNFlZu9XTlrasaN+ EweQ== ARC-Authentication-Results: i=1; mx.google.com; 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" Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id py21-20020a05620a879500b0076718c3bb2csi7392963qkn.274.2023.07.02.11.07.24 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 02 Jul 2023 11:07:25 -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; 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" Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qG1SH-0000kQ-Ch; Sun, 02 Jul 2023 14:05:49 -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 1qG1Rt-0000dQ-E0; Sun, 02 Jul 2023 14:05:27 -0400 Received: from isrv.corpit.ru ([86.62.121.231]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qG1Rr-0005JX-5i; Sun, 02 Jul 2023 14:05:24 -0400 Received: from tsrv.corpit.ru (tsrv.tls.msk.ru [192.168.177.2]) by isrv.corpit.ru (Postfix) with ESMTP id EBBE310DF3; Sun, 2 Jul 2023 21:04:54 +0300 (MSK) Received: from tls.msk.ru (mjt.wg.tls.msk.ru [192.168.177.130]) by tsrv.corpit.ru (Postfix) with SMTP id 308BE11207; Sun, 2 Jul 2023 21:04:53 +0300 (MSK) Received: (nullmailer pid 2122381 invoked by uid 1000); Sun, 02 Jul 2023 18:04:51 -0000 From: Michael Tokarev To: qemu-devel@nongnu.org, qemu-stable@nongnu.org Cc: Richard Henderson , Michael Tokarev Subject: [Stable-8.0.3 64/65] linux-user: Avoid mmap of the last byte of the reserved_va Date: Sun, 2 Jul 2023 21:04:37 +0300 Message-Id: <20230702180439.2122316-10-mjt@tls.msk.ru> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=86.62.121.231; envelope-from=mjt@tls.msk.ru; helo=isrv.corpit.ru X-Spam_score_int: -68 X-Spam_score: -6.9 X-Spam_bar: ------ X-Spam_report: (-6.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_HI=-5, 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 From: Richard Henderson 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> (cherry picked from commit 605a8b5491a119a2a6efbf61e5a38f9374645990) Signed-off-by: Michael Tokarev 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);