From nobody Tue Apr 7 09:06:50 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; dmarc=pass(p=quarantine dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1773426693; cv=none; d=zohomail.com; s=zohoarc; b=AD6FPq68rv61notz96LMdEcOGtN+nFV2X7+hM99uXvJ9smYG/i8oAc/4uvdulMBXsW/4QIMlfbDNkSAE8LJ9KB+VdTw67RPjH8PFPyjuzjkc6n4p27RBwIUtco1oU7e4ccUHcAvg3Tz628vmy8uR7CQGX86GCKt5ajPC12WXmtc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1773426693; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=wKfC3INVkwCJbTe2IHZRLbg0+sc92Iz8FWaY26hJnHE=; b=nWJ9EWYE28G69PNCJByNqpOhW/suM1YD18yvqbbcuSFbZFGizptWK4P8onr+BlZtuv/wKAOTZU7HGJj3/F19uEQirB1pU2/Qw0ne8OUh5/Q1w4VpSLJ7isZQ2tRVmXQV/b19yBj9u95EAiw0Ae8rqwAoZjHpmSr0iNUfMRBokfA= 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; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1773426693948593.9885153705186; Fri, 13 Mar 2026 11:31:33 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1w17IJ-0008LQ-TP; Fri, 13 Mar 2026 14:31:31 -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 1w17H8-0007c8-0W for qemu-devel@nongnu.org; Fri, 13 Mar 2026 14:30:22 -0400 Received: from tor.source.kernel.org ([2600:3c04:e001:324:0:1991:8:25]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1w17H6-0005SK-27 for qemu-devel@nongnu.org; Fri, 13 Mar 2026 14:30:17 -0400 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by tor.source.kernel.org (Postfix) with ESMTP id 7F2CE6013C; Fri, 13 Mar 2026 18:30:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2D6B4C19421; Fri, 13 Mar 2026 18:30:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773426614; bh=V3pKuIKReYWC1E9/VW0Lj3lZ20wq73KCxdtWeUT2TXQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Wq4wXKdGRhDQTMTFGly+hNSpnw6IhfNl3dtkIGnOeCd48rXE0yvd3cKuqkD1MQ5in Vcbj/gH/rfLSQVYDnfsyowPp8dQChL7BI2kNPu1cWHjdUXvG/bM0XP8nXAM6nKp129 dB9ETJIVzA7oLebRY7bU/8qiwDELDr8HJQpQUQCexg00DefUMionUw+nCejsBhhQvM FHuJsIOiU1hBf2w/MmWG1KKiU4oHl3wF8QUP5bqc0N6YwsRLRw07lmhYmDiAW4ShVB hMtcWQsHVQD6FP3x2JjxavZlnHV2eXus8seGi3WZA5+7prT4vgyNPULeIfjB/F+8rT p0Vvs5g0qMRJg== From: Helge Deller To: qemu-devel@nongnu.org Cc: Helge Deller , Razvan Ghiorghe , Laurent Vivier Subject: [PULL 1/2] linux-user: Fix zero_bss for RX PT_LOAD segments Date: Fri, 13 Mar 2026 19:30:08 +0100 Message-ID: <20260313183009.34221-2-deller@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260313183009.34221-1-deller@kernel.org> References: <20260313183009.34221-1-deller@kernel.org> 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: pass client-ip=2600:3c04:e001:324:0:1991:8:25; envelope-from=deller@kernel.org; helo=tor.source.kernel.org 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, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, 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: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @kernel.org) X-ZM-MESSAGEID: 1773426695526158500 Content-Type: text/plain; charset="utf-8" From: Razvan Ghiorghe zero_bss() incorrectly assumed that any PT_LOAD containing .bss must be writable, rejecting valid ELF binaries where .bss overlaps the tail of an RX file-backed page. Instead of failing, temporarily enable write access on the overlapping page to zero the fractional bss range, then restore the original page permissions once initialization is complete. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3179 Signed-off-by: Razvan Ghiorghe Reviewed-by: Helge Deller Signed-off-by: Helge Deller --- linux-user/elfload.c | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 35471c0c9a..59b543f740 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -449,12 +449,6 @@ static bool zero_bss(abi_ulong start_bss, abi_ulong en= d_bss, { abi_ulong align_bss; =20 - /* We only expect writable bss; the code segment shouldn't need this. = */ - if (!(prot & PROT_WRITE)) { - error_setg(errp, "PT_LOAD with non-writable bss"); - return false; - } - align_bss =3D TARGET_PAGE_ALIGN(start_bss); end_bss =3D TARGET_PAGE_ALIGN(end_bss); =20 @@ -472,20 +466,35 @@ static bool zero_bss(abi_ulong start_bss, abi_ulong e= nd_bss, */ align_bss -=3D TARGET_PAGE_SIZE; } else { + abi_ulong start_page_aligned =3D start_bss & TARGET_PAGE_MASK; /* - * The start of the bss shares a page with something. - * The only thing that we expect is the data section, - * which would already be marked writable. - * Overlapping the RX code segment seems malformed. + * The logical OR between flags and PAGE_WRITE works because + * in include/exec/page-protection.h they are defined as PROT_* + * values, matching mprotect(). + * Temporarily enable write access to zero the fractional bss. + * target_mprotect() handles TB invalidation if needed. */ if (!(flags & PAGE_WRITE)) { - error_setg(errp, "PT_LOAD with bss overlapping " - "non-writable page"); - return false; + if (target_mprotect(start_page_aligned, + TARGET_PAGE_SIZE, + prot | PAGE_WRITE) =3D=3D -1) { + error_setg_errno(errp, errno, + "Error enabling write access for bss"); + return false; + } } =20 - /* The page is already mapped and writable. */ + /* The page is already mapped and now guaranteed writable. */ memset(g2h_untagged(start_bss), 0, align_bss - start_bss); + + if (!(flags & PAGE_WRITE)) { + if (target_mprotect(start_page_aligned, + TARGET_PAGE_SIZE, prot) =3D=3D -1) { + error_setg_errno(errp, errno, + "Error restoring bss first permissions= "); + return false; + } + } } } =20 --=20 2.53.0 From nobody Tue Apr 7 09:06:50 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; dmarc=pass(p=quarantine dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1773426677; cv=none; d=zohomail.com; s=zohoarc; b=Qi+oY8UJZUGY2x86XysiyrqRcQhraIYpxTj3Kyt8pCxb6caD6XDfygc36W610F7fqfJX8WfU0BJ/jVZ5bRclNDiDKbtQGhTpNV6qyGcmYyNmTn/Ho/4yf5OccqBjuIbq3zc7XIUqhNpVt89+sqFqIy0wiwfADn+VlIOmsjcTVeo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1773426677; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=fpzPZvf4lGg/YV04gcwA1Ds3uYOT4KMbk4tjGAtyFro=; b=cLIfNXtG8zb/pyjOEXLW77nOglOKb7C3YGGm1cbCPGSS6eoxbzUeV6ramVu2It/c/8EKMDHeVoZbv1FmvhAyUdSul5gaQUY/U6L6wgTveKnJ1o5eWbkQoh8nNN30r09l4/K//+c9zAQz0J+3heauLBIguPizV1vLgEm8lQv+6f0= 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; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1773426677694428.35393985456574; Fri, 13 Mar 2026 11:31:17 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1w17Hg-0007f7-AZ; Fri, 13 Mar 2026 14:30:55 -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 1w17HA-0007cF-CH for qemu-devel@nongnu.org; Fri, 13 Mar 2026 14:30:23 -0400 Received: from tor.source.kernel.org ([172.105.4.254]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1w17H7-0005SV-4y for qemu-devel@nongnu.org; Fri, 13 Mar 2026 14:30:19 -0400 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by tor.source.kernel.org (Postfix) with ESMTP id F2B2D6183E; Fri, 13 Mar 2026 18:30:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9F9EBC19424; Fri, 13 Mar 2026 18:30:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773426615; bh=IoVriGbsFQSGgBcb5APt3WGdcnQHDFn1QXcp46ncTuE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kQ/5aH8q6kv8OqBqiLEIRDPaOnCn0icVyASenP9VXNaz7Qev5SGWK8ZpTt43rk6LW D3WlW9oJ2qD43ymyMBmp0aIaz+8TfAMilrCVDFmU1AWF3Eowezb5rf1cC9lCCC2HJh 3+aDsHliC8Zahi5uNyXLT8/Lc3CAbNMQ5xa3sSFzq00c+C6TOC6ek8ohw48ajFSxrs flIWFiUIT0vKO5lN8caCWzS0MaX0f7az9vPj0GquVhHVvgVvvBkxa/yI6sj7Hu6LEo 7mYdHbWS7mk1/iOzy9pgUnIjvznHa2V4KgEo9hYUtJVVTevufBdZXUBBIcwLvq2wXd aUp4CQSiGpsuw== From: Helge Deller To: qemu-devel@nongnu.org Cc: Helge Deller , Razvan Ghiorghe , Laurent Vivier Subject: [PULL 2/2] linux-user: fix mremap with old_size=0 for shared mappings Date: Fri, 13 Mar 2026 19:30:09 +0100 Message-ID: <20260313183009.34221-3-deller@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260313183009.34221-1-deller@kernel.org> References: <20260313183009.34221-1-deller@kernel.org> 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: pass client-ip=172.105.4.254; envelope-from=deller@kernel.org; helo=tor.source.kernel.org X-Spam_score_int: -3 X-Spam_score: -0.4 X-Spam_bar: / X-Spam_report: (-0.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.819, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.903, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @kernel.org) X-ZM-MESSAGEID: 1773426679347158500 Content-Type: text/plain; charset="utf-8" From: Razvan Ghiorghe When old_size is zero and old_address refers to a shareable mapping, mremap() should create a new mapping of the same pages according to the mremap(2) man page. The MREMAP_MAYMOVE flag must be specified in this case. Previously, QEMU's target_mremap() rejected this valid case with EFAULT during the initial validation, before checking for the special old_size =3D=3D 0 behaviour. This patch adds proper handling for old_size =3D=3D 0: - Validates that MREMAP_MAYMOVE flag is set (required by man spec) - Passes the call through to the host mremap() - Creates a new mapping without invalidating the original, with both being valid and sharing the same physical memory frames. - Ensures the new mapping address falls within the valid guest address region before returning it to the guest. Tested with the reproducer from the issue on qemu-riscv64, qemu-hppa, and qemu-aarch64. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3105 Signed-off-by: Razvan Ghiorghe Tested-by: Helge Deller Reviewed-by: Helge Deller Signed-off-by: Helge Deller --- linux-user/mmap.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 76978a56a8..b635b6a21c 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -1120,6 +1120,58 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong= old_size, errno =3D EINVAL; return -1; } + + if (!old_size) { + if (!(flags & MREMAP_MAYMOVE)) { + errno =3D EINVAL; + return -1; + } + mmap_lock(); + if (flags & MREMAP_FIXED) { + host_addr =3D mremap(g2h_untagged(old_addr), old_size, new_siz= e, + flags, g2h_untagged(new_addr)); + } else { + /* + * We ensure that the new mapping stands in the + * region of guest mappable addresses. + */ + abi_ulong mmap_start; + + mmap_start =3D mmap_find_vma(0, new_size, TARGET_PAGE_SIZE); + + if (mmap_start =3D=3D -1) { + errno =3D ENOMEM; + mmap_unlock(); + return -1; + } + + host_addr =3D mremap(g2h_untagged(old_addr), old_size, new_siz= e, + flags | MREMAP_FIXED, g2h_untagged(mmap_start= )); + + new_addr =3D mmap_start; + } + + if (host_addr =3D=3D MAP_FAILED) { + mmap_unlock(); + return -1; + } + + if (flags & MREMAP_FIXED) { + new_addr =3D h2g(host_addr); + } + + prot =3D page_get_flags(old_addr); + /* + * For old_size zero, there is nothing to clear at old_addr. + * Only set the flags for the new mapping. They both are valid. + */ + page_set_flags(new_addr, new_addr + new_size - 1, + prot | PAGE_VALID, PAGE_VALID); + shm_region_rm_complete(new_addr, new_addr + new_size - 1); + mmap_unlock(); + return new_addr; + } + if (!guest_range_valid_untagged(old_addr, old_size)) { errno =3D EFAULT; return -1; --=20 2.53.0