From nobody Sun Feb 8 23:57:56 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1665159207; cv=none; d=zohomail.com; s=zohoarc; b=GtJA2/cWwqfHBr8mQa9k1BLBWawyGA1mbvIo7PMumgNWqU0/OC/BoQNDnHwd9tSYm0d+FDWW2zzu/fCK0ufFmjLeUS2URyFookb5keuetyqkqsMzeWZmg1HqP3hMN/QBYM20U4PkTd4nzGsl84reuTsIEenvreGIXrxXFdLRHvE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1665159207; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=myO3ktHeJT6MCrgSHvLKUCdHXWktxN7tncXD07KwBmU=; b=iNM+bo15ku47Bisxj+S56WSAafb5F19Ke0DJwy7XbrYjoopsS56yZxh/FLdxcqP47mz34a8RdYdt2tlzy/KcZHeoH1BvO3Ba5RbBnOeZcQlLiy63WXqD3ducXyURZ3LdM+GZyBnG36NrWcAH9xjxmBJ0nDGQ+Vur//gP9x1ScWk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1665159207709312.70993106950516; Fri, 7 Oct 2022 09:13:27 -0700 (PDT) Received: from localhost ([::1]:47912 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ogpyY-0004LZ-Cl for importer@patchew.org; Fri, 07 Oct 2022 12:13:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57126) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ogpTw-0003LN-AS for qemu-devel@nongnu.org; Fri, 07 Oct 2022 11:41:48 -0400 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]:43764) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ogpTo-0000sD-S5 for qemu-devel@nongnu.org; Fri, 07 Oct 2022 11:41:47 -0400 Received: by mail-pl1-x632.google.com with SMTP id z20so4861102plb.10 for ; Fri, 07 Oct 2022 08:41:39 -0700 (PDT) Received: from DESKTOP-8TCEJFK.localdomain (215.134.178.217.shared.user.transix.jp. [217.178.134.215]) by smtp.gmail.com with ESMTPSA id gm7-20020a17090b100700b0020669c8bd87sm1668482pjb.36.2022.10.07.08.41.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Oct 2022 08:41:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isshiki-clinic-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=myO3ktHeJT6MCrgSHvLKUCdHXWktxN7tncXD07KwBmU=; b=ZVCB9kCTOQtpyK0zpD4ysqoq95hu/l1TWqJDZzx9RFwAnWDG1NTf5qHPSXzjx3PsC6 LerGQ6fA9EesxMec8f4/4Gy0e7SDfI1CtLKQquDAx1XdN9aPrHUm5iawUV6caMvDZfV1 CZeBi/fQIXYfbrBFKO5iEBJ7eGvRqKGLdohpY2trVexHL4HXzCyi4+Gf1n5nvtyBjJAw CjHOPvZFKGZkuPllB3sv8wB1xv9hn29oBCAUeeNW6phl1P+Ll3tOKWghRfYtqoatVfJe J3L2Xur8yy4IqpbLCgo6jfKzwe/5T8hOR4aSDOQA/rQZ1SqAAGYHmfwwzLfAGbqw76GW FUSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=myO3ktHeJT6MCrgSHvLKUCdHXWktxN7tncXD07KwBmU=; b=wKCkv5YhtVYUERzwaTbDL89uKxnh+ELfBe4qMXk9ZSwFqMHP7Hgpd9ct2ChG065TFm /i6/oAvn7Zox4aLCS2xKOaPUtgdZkfKlI80IL5LZJxohDgAgnRLYRitEAPW4PQvu0cGk 8Eelp0ZrSScs0AnjjIXKcVzZFgA0rQJDfxsLb8+UjoRP22itLXmAy7YsveQkU7LGqJcF Ojl8SrZqRaZ8yvaaQ7k/B3w08bUJHxd8QWrRMswo01Enhg33GaMK7EBwRuwka1XrEYfv 8LMpJkkdVLcDrXrgq3mHnEOZ/CNOBATK/lsdfU0NgjJkccIXsAJxCZOv2qXzpokH4kM7 IwEg== X-Gm-Message-State: ACrzQf2Exm3B4Nv6U9vWe+M5w7GYqt7AVUcmUrDvmT+r5bDFzj4HpKGk Nz7r4K3BUXSMdzXB8s7HrqeopQ== X-Google-Smtp-Source: AMsMyM7Q3gmqye+5MAd+eFw+NrKANIS8IS1sH6Y+4snsm3WbLvZLcBj0Lo64OgnU24viJf9+0f6tIA== X-Received: by 2002:a17:902:d48e:b0:178:b5d:ab3 with SMTP id c14-20020a170902d48e00b001780b5d0ab3mr5660641plg.22.1665157298350; Fri, 07 Oct 2022 08:41:38 -0700 (PDT) From: Soichiro Isshiki X-Google-Original-From: Soichiro Isshiki To: Richard Henderson , Peter Maydell , qemu-devel@nongnu.org, qemu-trivial@nongnu.org Cc: sisshiki1969 Subject: [PATCH v2] linux-user: mprotect() should returns 0 when len is 0. Date: Sat, 8 Oct 2022 00:40:40 +0900 Message-Id: <20221007154039.10239-1-sisshiki@mac.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=2607:f8b0:4864:20::632; envelope-from=sisshiki@isshiki-clinic.com; helo=mail-pl1-x632.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @isshiki-clinic-com.20210112.gappssmtp.com) X-ZM-MESSAGEID: 1665159209344100001 Content-Type: text/plain; charset="utf-8" From: sisshiki1969 On Fri, Oct 7, 2022 at 9:38 AM Richard Henderson wrote: | Although, sorta, this smells like a kernel bug. | Why should mprotect(-4096, 0, 0) succeed while mprotect(-4096, 4096, 0) f= ails? This may be kinda bug compatibility... | But anyway, if we're going to fix len =3D=3D 0 to match, we might as well= fix all 3 test | ordering bugs at the same time. Yes, I agree, and made another patch. A validation for wrap-around was added, I think it is neccesory. A tiny test code was shown below. ```sh > cat test.c #include #include #include int main(int argc, char *argv[]) { char *addr; int prot =3D PROT_READ | PROT_EXEC; int map =3D MAP_SHARED | MAP_ANONYMOUS; addr =3D mmap(NULL, 4096, prot, map, -1, 0); if (addr =3D=3D 0) { perror("mmap"); exit(EXIT_FAILURE); } =20 void *addrs[] =3D { (void *)77, NULL, addr }; for (int i =3D 0; i < 3; i++) { if (mprotect(addrs[i], 0, PROT_READ) =3D=3D -1) { perror("mprotect"); } else { printf("OK\n"); } } // invalid prot if (mprotect(addr, 2048, PROT_READ | 0x20) =3D=3D -1) { perror("mprotect"); } else { printf("OK\n"); } } > cc test.c -o test > ./test mprotect: Invalid argument OK OK mprotect: Invalid argument > qemu-x86_64 test # current master mprotect: Invalid argument OK mprotect: Cannot allocate memory mprotect: Invalid argument > build/qemu-x86_64 test # after the patch applied mprotect: Invalid argument OK OK mprotect: Invalid argument ``` seems good. Soichiro Isshiki Signed-off-by: sisshiki1969 --- linux-user/mmap.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 28f3bc85ed..757709eeba 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -124,17 +124,20 @@ int target_mprotect(abi_ulong start, abi_ulong len, i= nt target_prot) if ((start & ~TARGET_PAGE_MASK) !=3D 0) { return -TARGET_EINVAL; } - page_flags =3D validate_prot_to_pageflags(&host_prot, target_prot); - if (!page_flags) { - return -TARGET_EINVAL; + if (len =3D=3D 0) { + return 0; } len =3D TARGET_PAGE_ALIGN(len); end =3D start + len; + if (end <=3D start) { + return -TARGET_ENOMEM; + } if (!guest_range_valid_untagged(start, len)) { return -TARGET_ENOMEM; } - if (len =3D=3D 0) { - return 0; + page_flags =3D validate_prot_to_pageflags(&host_prot, target_prot); + if (!page_flags) { + return -TARGET_EINVAL; } =20 mmap_lock(); --=20 2.25.1