Skip to contentSkip to conversation

linux-user/x86_64: fix modify_ldt ABI and lm handling

PATCH In review Artemii Mashanov 1 participant 2 messages Oct 27 – Nov 17 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user/x86_64: fix modify_ldt ABI and lm handling

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

---
 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
linux-user/syscall.c +2 -8
index 0380691..a5d827b 100644
@@ -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;
 
     if (bytecount != 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 = tswap32(target_ldt_info->entry_number);
-    ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
+    ldt_info.base_addr = tswap32(target_ldt_info->base_addr);
     ldt_info.limit = tswap32(target_ldt_info->limit);
     ldt_info.flags = 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 = (ldt_info.flags >> 4) & 1;
     seg_not_present = (ldt_info.flags >> 5) & 1;
     useable = (ldt_info.flags >> 6) & 1;
-#ifdef TARGET_ABI32
-    lm = 0;
-#else
-    lm = (ldt_info.flags >> 7) & 1;
-#endif
     if (contents == 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 |= (useable << 20);
linux-user/x86_64/target_syscall.h +2 -2
index 68f55f8..06f3af5 100644
@@ -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;
 };
tests/tcg/i386/meson.build +1 -0
index e7c3095..b68b219 100644
@@ -8,6 +8,7 @@ tests += {
   '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']},
tests/tcg/i386/modify-ldt.c new file +72 -0

72 lines not on this page · raw message

tests/tcg/x86_64/meson.build +1 -0
index ad4d1f5..56f4e8a 100644
@@ -25,6 +25,7 @@ test_mmx_h = custom_target('test_mmx_h',
                            output: 'test-mmx.h')
 
 tests += {
+  '../i386/modify-ldt.c': {},
   '../i386/test-i386-ssse3.c': {},
   '../i386/test-3dnow.c': {
     'cflags': ['-I', meson.current_build_dir(),
Signature · 1 line
2.55.0
#1 lore raw

Hi,

Ping on this patch. Could someone please take a look when time permits?

Please let me know if any changes or additional testing are needed.

Thanks,
Artemii

вт, 27 окт. 2026 г. в 16:07, Artemii Mashanov <[email protected]>:

Quoted 190 lines

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

---
 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
linux-user/syscall.c +2 -8
index 0380691..a5d827b 100644
@@ -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;
 
     if (bytecount != 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 = tswap32(target_ldt_info->entry_number);
-    ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
+    ldt_info.base_addr = tswap32(target_ldt_info->base_addr);
     ldt_info.limit = tswap32(target_ldt_info->limit);
     ldt_info.flags = 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 = (ldt_info.flags >> 4) & 1;
     seg_not_present = (ldt_info.flags >> 5) & 1;
     useable = (ldt_info.flags >> 6) & 1;
-#ifdef TARGET_ABI32
-    lm = 0;
-#else
-    lm = (ldt_info.flags >> 7) & 1;
-#endif
     if (contents == 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 |= (useable << 20);
linux-user/x86_64/target_syscall.h +2 -2
index 68f55f8..06f3af5 100644
@@ -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;
 };
tests/tcg/i386/meson.build +1 -0
index e7c3095..b68b219 100644
@@ -8,6 +8,7 @@ tests += {
   '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']},
tests/tcg/i386/modify-ldt.c new file +72 -0
new file mode 100644
index 0000000..4bfa4ea
@@ -0,0 +1,72 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#define _GNU_SOURCE
+#include <asm/ldt.h>
+#include <assert.h>
+#include <errno.h>
+#include <stdint.h>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+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)) == 0);
+    assert(syscall(SYS_modify_ldt, 0, entries, sizeof(entries)) ==
+           sizeof(entries));
+    /* Linux sets the accessed bit when installing a descriptor. */
+    assert((entries[desc->entry_number] & ~(UINT64_C(1) << 40)) == expected);
+}
+
+int main(void)
+{
+    struct user_desc desc = {
+        .entry_number = 1,
+        .base_addr = 0x89abcdef,
+        .limit = 0x54321,
+        .seg_32bit = 1,
+        .limit_in_pages = 1,
+        .useable = 1,
+    };
+    const unsigned int invalid_sizes[] = {0, 15, 17, 24};
+
+    _Static_assert(sizeof(desc) == 16, "modify_ldt user_desc must be 16 bytes");
+    for (unsigned int i = 0;
+         i < sizeof(invalid_sizes) / sizeof(invalid_sizes[0]); i++) {
+        long ret = syscall(SYS_modify_ldt, 0x11, &desc, invalid_sizes[i]);
+
+        /* Linux returns int-sized errors; libc may have translated them. */
+        assert((int)ret == -EINVAL || (ret == -1 && errno == EINVAL));
+    }
+    for (int func = 1; func <= 0x11; func += 0x10) {
+        for (int contents = MODIFY_LDT_CONTENTS_DATA;
+             contents <= MODIFY_LDT_CONTENTS_CODE; contents += 2) {
+            uint64_t expected = UINT64_C(0x89c5f2abcdef4321);
+
+            desc.contents = contents;
+            if (contents == MODIFY_LDT_CONTENTS_CODE) {
+                expected |= UINT64_C(1) << 43;
+            }
+            if (func == 0x11) {
+                expected |= UINT64_C(1) << 52;
+            }
+            check_entry(&desc, func, expected);
+#ifdef __x86_64__
+            desc.lm = 1;
+            check_entry(&desc, func, expected);
+            desc.lm = 0;
+#endif
+        }
+    }
+    desc = (struct user_desc) {
+        .entry_number = 1,
+        .read_exec_only = 1,
+        .seg_not_present = 1,
+    };
+#ifdef __x86_64__
+    desc.lm = 1;
+#endif
+    check_entry(&desc, 0x11, 0);
+    return 0;
+}
tests/tcg/x86_64/meson.build +1 -1
index ad4d1f5..56f4e8a 100644
@@ -25,6 +25,7 @@ test_mmx_h = custom_target('test_mmx_h',
                            output: 'test-mmx.h')
 
 tests += {
+  '../i386/modify-ldt.c': {},
   '../i386/test-i386-ssse3.c': {},
   '../i386/test-3dnow.c': {
     'cflags': ['-I', meson.current_build_dir(),
--

2.55.0