From nobody Mon Nov 23 04:26:28 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=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1793106452; cv=none; d=zohomail.com; s=zohoarc; b=V1n90jhxn0cDPmbwJPTGBS7gqOu7QLJsqpNZTu2/srDIOX+qcxlkWgdk1mdtcoBq6mQ9z4ckAF8ENUm+8mfUX3EjHsXGUzssLg8YYMYOMwWJq+L+V+wE9Sg+Uft7HifsKtdmT72CFVXC67VINPR9B2kDa3js25qkrwpowxatorA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793106452; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=w8y8FcrivNxmXSl5ogfZ9P+6PPoIlTcQ8+g6aIw9qqM=; b=SvGRiJtOdId3c4KDt6gs6nh3WS3VIQ9EYw3mjDp4SPIt0q1aReZuYZraRv4CDOGbRtf3yDOTOYlBPeEQp6Bjifsup7YugNaFhDj6EguOe3HiTer68ADLEax7GSQCbCzqJG+VBp6gOhM3y/xB38mNPhdfRa1eW2TezgSw4AIpjoo= 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=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793106452470847.6718524164289; Tue, 27 Oct 2026 06:07:32 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xLgtZ-0007qg-4J; Tue, 27 Oct 2026 09:07:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xLgtX-0007qO-HV for qemu-devel@nongnu.org; Tue, 27 Oct 2026 09:07:15 -0400 Received: from mail-lf1-x12d.google.com ([2a00:1450:4864:20::12d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xLgtU-0005Vu-6J for qemu-devel@nongnu.org; Tue, 27 Oct 2026 09:07:14 -0400 Received: by mail-lf1-x12d.google.com with SMTP id 2adb3069b0e04-5ba3ac592faso2391728e87.0 for ; Tue, 27 Oct 2026 06:07:11 -0700 (PDT) Received: from qblck ([188.130.155.185]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5bfb7a75a79sm2226308e87.39.2026.10.27.06.07.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Oct 2026 06:07:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1793106430; x=1793711230; darn=nongnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=w8y8FcrivNxmXSl5ogfZ9P+6PPoIlTcQ8+g6aIw9qqM=; b=I64rlGZRBRmmrHm5pDdDCHs06b0qfEyF8LNCSE2vcDMIK13J2PS8yQLDXXqTjqKI5x EARTSqdBsdpqPmWvIS88aV5Cs5o4dMi5ApfPCDmwRJrneKm1TS+fe0d5wOhqG1UsH1QA TbxoBm5/KIVttD98gB3vmSeYF2keVM9v7ToUeTT57gN2vhD08LMQ7VTh+vIcYtfoByQA ThyeujCh622xfF+tUlellWFBKmJjayTvLP9fCpivaF193skT4xWAZfVYClHfqW+Hj1ly 8doqk8zvu10dnp0OgIATNc/tXAsHJXdX5Oi8HhCQBBtBy6UVDFpxslT4Qk9yGWpAWc5w PFzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793106430; x=1793711230; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=w8y8FcrivNxmXSl5ogfZ9P+6PPoIlTcQ8+g6aIw9qqM=; b=o/0o2NXvYSfS1MtJ3Ovo/5wkPUE2KMX2wkxdrglTIukpcAJlYdzmUT/p9RQaEVQLZO ijANs29TVIjdaKl2BBqagyuw45PQ9HSVR3s18zR07/ILUyx31AusiZ1KUchG9lErr1e8 MKOHumqkaGTxObuyc6PnMspopcjUde/Rlb5cVyqIIGP1ppUeyolXj5HD79QZy+E4RlRp TBU/GDjniS+Xv1bNV+YCW6PWGFk+SBrLnB7rZZW8jLFTrbl0QfH78TVpjmSmsAqSeR+r XxRzbyOUgg5RRSkqoCDw2NmLadbkYNZrqhnmaxWmeZyTXihUFDyFmbM0yx6/ZC7mMs84 s4Ng== X-Gm-Message-State: AFq9FYL8eeOhEZERdgqIOzYXwZVR8vygyT2gs15liy8mAcSy2lbSqG6P w91nLzaioQOxPH3/kh64FXKULH8SXytzXQLrU9qC1ECELOpQ2lA25vjAl5LZmTjlCtmarA== X-Gm-Gg: AYBFou3r3PQ7X3sVqPKNrFFoJ/KrkuYDMDiUKf846wz9ZH+mTA1soHUt7IuPLAOctDe Mq5oAJ4gjXTiDQDHlHIrrayaUUJo3AcZHr3nX8G74g9u9i5MRfx2r/M3p/p+B3tz41h/s5dvjaA c4UNqxAuRAn6olr4E6vs3NKCIPl+PMFci1optRcC62oDgPr4jagkEdDyimLf0uMkeoN2Hj2fRyE 2mhynpPQ2RdlJClUx/4aZoFqX9BVJpwPMqeJBh0TFZoMzyrxhYeMSC9GcEvxvUab8cM+R/xd73/ gI7PBo8h/Cc6r5s5LTVunZPOBZLgmtrrZ6+wErzSA8IytMZn4bZbQHv34zsn4zTB/h0pyhVxg8x d3fYMsVOlM/UPTqyO3Vr6lhA0dpBN/qoTCI5SbjnApjEouV667MXTGfU0P7UiijH2m0bAE7RlYa KaOwe9d5wC0NrNUYRHBUcgTOtm19UNTP8rflXCrj7eGIATA6yTvXssk5V4V58dAnwyN91nL/hy5 kdj1WoUUBO6wMMF9msQumeroQ0yUJ7xUcgFQtVX/0+sMNQmiRCoIqHTFcZ5ZXhaPqObmSvTx39D R3n28wqQYUjQI2Snl6ePce7nme04qf+/VhLO9s4NiFAdiQAqwQ== X-Received: by 2002:a05:6512:3b21:b0:5b8:f048:473d with SMTP id 2adb3069b0e04-5bfb716dd83mr3119476e87.7.1793106429587; Tue, 27 Oct 2026 06:07:09 -0700 (PDT) From: Artemii Mashanov To: qemu-devel@nongnu.org Cc: Artemii Mashanov , Laurent Vivier , Helge Deller , Pierrick Bouvier , Paolo Bonzini , Richard Henderson Subject: [PATCH] linux-user/x86_64: fix modify_ldt ABI and lm handling Date: Tue, 27 Oct 2026 16:06:13 +0300 Message-ID: <20261027130613.113038-1-ralerrdirsardx@gmail.com> X-Mailer: git-send-email 2.55.0 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=lists1p.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::12d; envelope-from=ralerrdirsardx@gmail.com; helo=mail-lf1-x12d.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, FREEMAIL_FROM=0.001, 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: 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 @gmail.com) X-ZM-MESSAGEID: 1793106453474158500 Content-Type: text/plain; charset="utf-8" Linux uses a 16-byte user_desc on x86_64, with a 32-bit base_addr. QEMU expects 24 bytes instead and rejects valid modify_ldt writes. Use a 32-bit base_addr and swap it accordingly. Ignore lm when building LDT entries, matching Linux. Add a shared i386/x86_64 regression test for the ABI size and descriptor contents. Based on the diagnosis and proposed fix by naehrwert in issue #4691. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4691 Signed-off-by: Artemii Mashanov --- linux-user/syscall.c | 10 +---- linux-user/x86_64/target_syscall.h | 4 +- tests/tcg/i386/meson.build | 1 + tests/tcg/i386/modify-ldt.c | 72 ++++++++++++++++++++++++++++++ tests/tcg/x86_64/meson.build | 1 + 5 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 tests/tcg/i386/modify-ldt.c diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 0380691..a5d827b 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -6237,7 +6237,7 @@ static abi_long write_ldt(CPUX86State *env, struct target_modify_ldt_ldt_s ldt_info; struct target_modify_ldt_ldt_s *target_ldt_info; int seg_32bit, contents, read_exec_only, limit_in_pages; - int seg_not_present, useable, lm; + int seg_not_present, useable; uint32_t *lp, entry_1, entry_2; =20 if (bytecount !=3D sizeof(ldt_info)) @@ -6245,7 +6245,7 @@ static abi_long write_ldt(CPUX86State *env, if (!lock_user_struct(VERIFY_READ, target_ldt_info, ptr, 1)) return -TARGET_EFAULT; ldt_info.entry_number =3D tswap32(target_ldt_info->entry_number); - ldt_info.base_addr =3D tswapal(target_ldt_info->base_addr); + ldt_info.base_addr =3D tswap32(target_ldt_info->base_addr); ldt_info.limit =3D tswap32(target_ldt_info->limit); ldt_info.flags =3D tswap32(target_ldt_info->flags); unlock_user_struct(target_ldt_info, ptr, 0); @@ -6258,11 +6258,6 @@ static abi_long write_ldt(CPUX86State *env, limit_in_pages =3D (ldt_info.flags >> 4) & 1; seg_not_present =3D (ldt_info.flags >> 5) & 1; useable =3D (ldt_info.flags >> 6) & 1; -#ifdef TARGET_ABI32 - lm =3D 0; -#else - lm =3D (ldt_info.flags >> 7) & 1; -#endif if (contents =3D=3D 3) { if (oldmode) return -TARGET_EINVAL; @@ -6309,7 +6304,6 @@ static abi_long write_ldt(CPUX86State *env, ((seg_not_present ^ 1) << 15) | (seg_32bit << 22) | (limit_in_pages << 23) | - (lm << 21) | 0x7000; if (!oldmode) entry_2 |=3D (useable << 20); diff --git a/linux-user/x86_64/target_syscall.h b/linux-user/x86_64/target_= syscall.h index 68f55f8..06f3af5 100644 --- a/linux-user/x86_64/target_syscall.h +++ b/linux-user/x86_64/target_syscall.h @@ -17,7 +17,7 @@ #if 0 // Redefine this struct target_modify_ldt_ldt_s { unsigned int entry_number; - abi_ulong base_addr; + abi_uint base_addr; unsigned int limit; unsigned int seg_32bit:1; unsigned int contents:2; @@ -30,7 +30,7 @@ struct target_modify_ldt_ldt_s { #else struct target_modify_ldt_ldt_s { unsigned int entry_number; - abi_ulong base_addr; + abi_uint base_addr; unsigned int limit; unsigned int flags; }; diff --git a/tests/tcg/i386/meson.build b/tests/tcg/i386/meson.build index e7c3095..b68b219 100644 --- a/tests/tcg/i386/meson.build +++ b/tests/tcg/i386/meson.build @@ -8,6 +8,7 @@ tests +=3D { 'hello-i386.c': { 'cflags': ['-ffreestanding', '-fno-stack-protector', '-nostdlib'], }, + 'modify-ldt.c': {}, 'test-aes.c': {'cflags': ['-O1', '-msse2', '-maes']}, 'test-i386-adcox.c': {'cflags': ['-O2']}, 'test-i386-bmi2.c': {'cflags': ['-O2', '-fwrapv']}, diff --git a/tests/tcg/i386/modify-ldt.c b/tests/tcg/i386/modify-ldt.c new file mode 100644 index 0000000..4bfa4ea --- /dev/null +++ b/tests/tcg/i386/modify-ldt.c @@ -0,0 +1,72 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +static void check_entry(const struct user_desc *desc, int func, + uint64_t expected) +{ + uint64_t entries[2]; + + assert(syscall(SYS_modify_ldt, func, desc, sizeof(*desc)) =3D=3D 0); + assert(syscall(SYS_modify_ldt, 0, entries, sizeof(entries)) =3D=3D + sizeof(entries)); + /* Linux sets the accessed bit when installing a descriptor. */ + assert((entries[desc->entry_number] & ~(UINT64_C(1) << 40)) =3D=3D exp= ected); +} + +int main(void) +{ + struct user_desc desc =3D { + .entry_number =3D 1, + .base_addr =3D 0x89abcdef, + .limit =3D 0x54321, + .seg_32bit =3D 1, + .limit_in_pages =3D 1, + .useable =3D 1, + }; + const unsigned int invalid_sizes[] =3D {0, 15, 17, 24}; + + _Static_assert(sizeof(desc) =3D=3D 16, "modify_ldt user_desc must be 1= 6 bytes"); + for (unsigned int i =3D 0; + i < sizeof(invalid_sizes) / sizeof(invalid_sizes[0]); i++) { + long ret =3D syscall(SYS_modify_ldt, 0x11, &desc, invalid_sizes[i]= ); + + /* Linux returns int-sized errors; libc may have translated them. = */ + assert((int)ret =3D=3D -EINVAL || (ret =3D=3D -1 && errno =3D=3D E= INVAL)); + } + for (int func =3D 1; func <=3D 0x11; func +=3D 0x10) { + for (int contents =3D MODIFY_LDT_CONTENTS_DATA; + contents <=3D MODIFY_LDT_CONTENTS_CODE; contents +=3D 2) { + uint64_t expected =3D UINT64_C(0x89c5f2abcdef4321); + + desc.contents =3D contents; + if (contents =3D=3D MODIFY_LDT_CONTENTS_CODE) { + expected |=3D UINT64_C(1) << 43; + } + if (func =3D=3D 0x11) { + expected |=3D UINT64_C(1) << 52; + } + check_entry(&desc, func, expected); +#ifdef __x86_64__ + desc.lm =3D 1; + check_entry(&desc, func, expected); + desc.lm =3D 0; +#endif + } + } + desc =3D (struct user_desc) { + .entry_number =3D 1, + .read_exec_only =3D 1, + .seg_not_present =3D 1, + }; +#ifdef __x86_64__ + desc.lm =3D 1; +#endif + check_entry(&desc, 0x11, 0); + return 0; +} diff --git a/tests/tcg/x86_64/meson.build b/tests/tcg/x86_64/meson.build index ad4d1f5..56f4e8a 100644 --- a/tests/tcg/x86_64/meson.build +++ b/tests/tcg/x86_64/meson.build @@ -25,6 +25,7 @@ test_mmx_h =3D custom_target('test_mmx_h', output: 'test-mmx.h') =20 tests +=3D { + '../i386/modify-ldt.c': {}, '../i386/test-i386-ssse3.c': {}, '../i386/test-3dnow.c': { 'cflags': ['-I', meson.current_build_dir(), --=20 2.55.0