From patchwork Fri Aug 4 22:00:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 710246 Delivered-To: patch@linaro.org Received: by 2002:a05:6359:d30:b0:129:c516:61db with SMTP id gp48csp228769rwb; Fri, 4 Aug 2023 15:01:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGZSQlHCfha+00hcXBkds1givAS698P1WT8uH72lG/VN/almHj+/XfLIQMQBB6dC8vWstfo X-Received: by 2002:a05:620a:2415:b0:76c:cce2:e7d5 with SMTP id d21-20020a05620a241500b0076ccce2e7d5mr3234708qkn.62.1691186490302; Fri, 04 Aug 2023 15:01:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691186490; cv=none; d=google.com; s=arc-20160816; b=VwWD8CdKs2TmPLf0hIkKzsfnlfl9vh+lCKquFPS/oZEfPuPnjY04vjXtibaCcYkWW8 9gdaaj31mQMM7bhB6yBcJpVAugNGGIA5p7Vgrl0tM5zG2BAxF1IttAA63OsGoI/B1BvP I7v1w/OdkIG41J5F3fQ2sM/AejIhKjkA0YI1FAIk/drMaI73/5fkKsim6gl/mUCMCobN MXzHubxHKaZMbDC2QOt3a+8jO0g5ymsDs0ZlRlrHYvmq1TB4RQYqLAdLwUt1ZGH4nV8t Ex4gAvmfqfOQQzlbQL//oLWXJ+QOTnplMtnpHWfX6e2IbnXZXbrW8BLThLnzCjLkBNlo eblg== 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=Y+al/bCM9dfQjUvbH6ERA5E1FapPNk6WKGGZIMvtMEA=; fh=evWk6bomlkFU5CSlcGEbB9y45J8nz+qeJ58iBBz7OL4=; b=ahiBpXaJkvpVfyBnoP3/M5OwNBcRCPNW8vg7sKFMIlPdyWjiC9VePYfLawlGsRYx+a 7/eDCqrc+AFMsStdJ8QvmfznkrY6cIm6h0HzB0F+C+NtJ7TdWBRHu8bf6S7JpC7PK7bD IUyByoabwSmE0WF1JYiR7SVrsOnOnNIagFkKxegbAgJt70w2+yNxnmAc1hZg0yNyckAR PrQ9MdS0dnV8D28c5X1jFQLYqthe1KWzc+i031viK6AQwPmTlWxTo2ppIWvAXKIiKA45 EFSgPEYb9IAIp2whlW4/ta9r8YPQtMLgLnLy/PAA9LvE7x7rPJ3mNRHXGrn2EpIbiDde Gbyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=SOpKkvzl; 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 j5-20020a37b905000000b00767c5ae5aa2si1747298qkf.224.2023.08.04.15.01.30 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 04 Aug 2023 15:01:30 -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=SOpKkvzl; 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 1qS2rF-0004sN-ON; Fri, 04 Aug 2023 18:01:18 -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 1qS2qi-0004X6-Jm for qemu-devel@nongnu.org; Fri, 04 Aug 2023 18:00:46 -0400 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qS2qe-0001jY-H1 for qemu-devel@nongnu.org; Fri, 04 Aug 2023 18:00:43 -0400 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-1bbf8cb694aso22024045ad.3 for ; Fri, 04 Aug 2023 15:00:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691186439; x=1691791239; 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=Y+al/bCM9dfQjUvbH6ERA5E1FapPNk6WKGGZIMvtMEA=; b=SOpKkvzldfmAt3LnNY3pfZO+WMTeO8e35+ej3p06mQfrHlQ5wgzGN+uKL64OgKuJLB N0LesIdg/hZQWeELVFtnk1zoL+r3mcDvPHmX/I3kcTKUgAcpBRLcW92iQlskhCQ2Quzb 33QQB9/Uw5O9qVt7MsK8CxRFfvKXQ/zEGQO8h4/fwxMhutYNjUk4PLNCxDtk0DPHJypL U8rzxYWHbELk/S0y76dnNjllwxbuLhiDQdmjq7RIliuLwHx24U0+ZpkPi76flJ8315gD ukBEoIhZXsxV87u2lcrieB5onUouLdGLGEdMByJhz7dYpJbvivSsOZfECmliQwji0/9Z IKNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691186439; x=1691791239; 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=Y+al/bCM9dfQjUvbH6ERA5E1FapPNk6WKGGZIMvtMEA=; b=NQB1Q3r6EF53dzGw0JoQRoN2x4EjGH5wMjFwg/+mxbLkzHGA8S8vda8EkKhbIQq9xl E1kVCHKuLQ0GsWvnK1FT+p2A7AgGL5wKEfyvx3wVanp/xsKKEfYgZR4KD52ExVZAJFjX LuGQaJ1lDEDLv+1ekkL0AnEqT3f2aISynyDnw/9+mg0T6aJcv2zG7eaJlBldhchBSxKH a9voBhEoiW4CUGMdV/HugQexlALPHfH7xsD6cjL3FrN+0YWhVTnLt4+VK9OYne/69v9g t9ktMjTvcLUKEhoIaNjxwwYwrGJWsVVqMjUek43C8BfwpAW/a7qih66n8CPnzBMsBYvW fYtA== X-Gm-Message-State: AOJu0Yx8RRDt/bKbq4ABspxItS7nkOC1XltlEMZ1iXDHj3RXPMy3xPqa SRijtkWo9kI6P8Q0OuvgH+T1sS2m+rOupLysO+o= X-Received: by 2002:a17:903:188:b0:1bb:3498:9caa with SMTP id z8-20020a170903018800b001bb34989caamr3765589plg.58.1691186439141; Fri, 04 Aug 2023 15:00:39 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:1eed:f77f:f320:8b14]) by smtp.gmail.com with ESMTPSA id u1-20020a17090282c100b001bbf7fd354csm2185568plz.213.2023.08.04.15.00.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Aug 2023 15:00:38 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Akihiko Odaki Subject: [PATCH v9 06/24] linux-user: Fix MAP_FIXED_NOREPLACE on old kernels Date: Fri, 4 Aug 2023 15:00:14 -0700 Message-Id: <20230804220032.295411-7-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230804220032.295411-1-richard.henderson@linaro.org> References: <20230804220032.295411-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62b; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62b.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=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: Akihiko Odaki The man page states: > Note that older kernels which do not recognize the MAP_FIXED_NOREPLACE > flag will typically (upon detecting a collision with a preexisting > mapping) fall back to a “non-MAP_FIXED” type of behavior: they will > return an address that is different from the requested address. > Therefore, backward-compatible software should check the returned > address against the requested address. https://man7.org/linux/man-pages/man2/mmap.2.html Signed-off-by: Akihiko Odaki Message-Id: <20230802071754.14876-3-akihiko.odaki@daynix.com> Reviewed-by: Richard Henderson Signed-off-by: Richard Henderson --- linux-user/mmap.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index a11c630a7b..90b3ef2140 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -263,7 +263,11 @@ static bool mmap_frag(abi_ulong real_start, abi_ulong start, abi_ulong last, void *p = mmap(host_start, qemu_host_page_size, target_to_host_prot(prot), flags | MAP_ANONYMOUS, -1, 0); - if (p == MAP_FAILED) { + if (p != host_start) { + if (p != MAP_FAILED) { + munmap(p, qemu_host_page_size); + errno = EEXIST; + } return false; } prot_old = prot; @@ -687,17 +691,25 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, /* map the middle (easier) */ if (real_start < real_last) { - void *p; + void *p, *want_p; off_t offset1; + size_t len1; if (flags & MAP_ANONYMOUS) { offset1 = 0; } else { offset1 = offset + real_start - start; } - p = mmap(g2h_untagged(real_start), real_last - real_start + 1, - target_to_host_prot(target_prot), flags, fd, offset1); - if (p == MAP_FAILED) { + len1 = real_last - real_start + 1; + want_p = g2h_untagged(real_start); + + p = mmap(want_p, len1, target_to_host_prot(target_prot), + flags, fd, offset1); + if (p != want_p) { + if (p != MAP_FAILED) { + munmap(p, len1); + errno = EEXIST; + } goto fail; } passthrough_start = real_start;