From patchwork Fri Aug 4 01:45:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 710222 Delivered-To: patch@linaro.org Received: by 2002:a05:6359:606:b0:129:c516:61db with SMTP id eh6csp233264rwb; Thu, 3 Aug 2023 18:47:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHLBEyov7NEn7P2we1lmMI8DeK7RPOD2hO2qPdB227e1VMwVira2AQczkUEcV5O869a+0Kq X-Received: by 2002:a05:620a:2a11:b0:76c:7d2a:2dbf with SMTP id o17-20020a05620a2a1100b0076c7d2a2dbfmr743100qkp.57.1691113677334; Thu, 03 Aug 2023 18:47:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691113677; cv=none; d=google.com; s=arc-20160816; b=W3GBzgihx8qiElbYbVXuQE4A4qRliJJyTQIevpj6u1U1wce6XAzivWoRFtbL70v/pK WTzeDn6+wgs+MER7gY7hDa5dsFopp1Nu3PXpEfhjNeJibOiEQYaLz1G+Kitm5DGEwFMi Kush4JsXMu79O5TKS+f1Y6g0JJkEFLbi/v9DLflKkaXT2U5cFhPY4sjw6IK1I8gsoQJO iF5iV3zPWvrAC+Ddm8E+EL4O50lSKR91aUUSkLayOZGy4kv0PycyVM18HErBydFKBsp2 4z1JLldIVRAE6c2TO1YeHTW2oBx8aqpARBMxaLDImRwWRRQRcb5/KCRJro2PgHp1jmcV sQUg== 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=/eOGiWVqZ8Y4POmSjtXETDABSVmvc7JlojTPUk6j1vw=; fh=oY0Hqs0mb1Sbw8XbQT7Frqsb6/GXc1LASz4nvWEMVJQ=; b=sQ30a9YYxiHaM0QOv+LmumOR7yCaMdykknEFnYLBkQ4gC4kc8w/py1+Hqs+BrAZRMu AadGs+XNeRfn2JQivMmcuX9ZB6zBOl6jrvW4nfX7U6LaLa4pxd0SxWUVc3tplXLbQunL hRbpzu3F9gau6Uqb9sskq5Lq+e9b+umF/b2zN0+1i/mgbesYYPRz9Rn7bcVr86enw4ml eByEyEzPph9lP/1+M+r/aUs+c7uY7KzxnKdAOf5h/R+cCDK94TR0ajn7ChKqGMrcGOOz kslK6T5oC2+5oQqKOgYC7Jam10enM2J0Ei7uSmMHWUNYR23z9H9D69GGDhYhmFwAZ5fB VCNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=kvL3PYYC; 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 r18-20020a05620a299200b0076ca010259asi655495qkp.263.2023.08.03.18.47.57 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 03 Aug 2023 18:47:57 -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=kvL3PYYC; 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 1qRjsy-0002jk-Ow; Thu, 03 Aug 2023 21:45:48 -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 1qRjsZ-0002cc-PO for qemu-devel@nongnu.org; Thu, 03 Aug 2023 21:45:30 -0400 Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qRjsX-000570-Fd for qemu-devel@nongnu.org; Thu, 03 Aug 2023 21:45:23 -0400 Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-686fc0d3c92so1164024b3a.0 for ; Thu, 03 Aug 2023 18:45:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1691113520; x=1691718320; 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=/eOGiWVqZ8Y4POmSjtXETDABSVmvc7JlojTPUk6j1vw=; b=kvL3PYYCNqPm2kdtbooz9RzzOkPGr5fnNUK05uskyW30BoIPIRc0KjNoY/I/l6vIsZ +8z7v/QcuomaO3/jGb8LiKkhXwId6j5GmHVZjIEPpktD6Jx8YB9xZ2d+bNj5PfSNysw5 25LeZeBdPRnjIsmTTFbH+VjPknyB+u33dxS51ORqY7AxzXxHAOlo3V5dm5PI7w9KB499 9702KVBD7hTvjrCqDp8he3ienJXCMJ9x7GNgsibtsNXsk6RH1FhEE669o/4LBbXFVMPZ y6hG6UMd6wkgrWJVdkMRfy66h6aNE+Wbgw/L3z3G/RNR8HfUNa3X76eoVmtBTs2jJL9k k8og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691113520; x=1691718320; 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=/eOGiWVqZ8Y4POmSjtXETDABSVmvc7JlojTPUk6j1vw=; b=QTbSapqu89jF/mr2DtE9+0uWK2y11gNlaADLw4UpnYs06gM9fIoGD7NObJpMbEi/68 MFsmWkdhWSt4IZHw3io2QL7YSKMj86BVChsOxFCin/Z+5f+yHugmih8KuDwKm+jfrTeS E0Vy9AjyhDunsL/6ZqnIOaQudJqbtBDOtX0J34VSq+0aY2BnC+qzur4AkVCY81VBkDo7 aqRr47Vfznlp/cJqkvMUWOgallg+KVO/RHC85/6eGwNp7osFautUay2qDg0XViqQk5MT BnREJu3+0KNz9n4k+xy6t+jGo07LejkBHyL/CYTuU9C+m8wA0TpXmxDRsB3mTPcLqM1K l46A== X-Gm-Message-State: AOJu0YzfwDDqTDMPtidj1iiG7bHJ6UHo9QAHdGx7NErFUtQMwkHy2euv azY93Pt5hrNkRU8O8+DQfRrWGeCsX8RwKx5lEmw= X-Received: by 2002:a05:6a21:4889:b0:137:9622:17d0 with SMTP id av9-20020a056a21488900b00137962217d0mr231190pzc.58.1691113520189; Thu, 03 Aug 2023 18:45:20 -0700 (PDT) Received: from stoup.. ([2602:47:d490:6901:9d52:4fe8:10ce:5c3]) by smtp.gmail.com with ESMTPSA id c5-20020aa78c05000000b0066f37665a63sm456138pfd.73.2023.08.03.18.45.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Aug 2023 18:45:19 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: joel@jms.id.au, akihiko.odaki@daynix.com, laurent@vivier.eu, deller@gmx.de Subject: [PATCH v8 02/17] linux-user: Fix MAP_FIXED_NOREPLACE on old kernels Date: Thu, 3 Aug 2023 18:45:02 -0700 Message-Id: <20230804014517.6361-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230804014517.6361-1-richard.henderson@linaro.org> References: <20230804014517.6361-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::42e; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x42e.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 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> 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;