linux-user/x86_64: modify_ldt refuses Linux's 16-byte struct user_desc, and honours its lm bit
Host environment
- Operating system: Ubuntu, Linux 7.0, x86-64 (AMD Ryzen 9 9950X)
- QEMU: master at a1c27ca0 (11.1.50),
configured with
--target-list=x86_64-linux-user; also 10.2.1 (Debian 1:10.2.1+ds-1ubuntu3.2) - QEMU flags: none (
qemu-x86_64 ./ldt)
Emulated CPU
Default (qemu64), TCG.
Description of problem
modify_ldt(func, ptr, bytecount) writes an LDT entry from a
struct user_desc, and Linux refuses a bytecount other than
sizeof(struct user_desc) with EINVAL. On x86-64 that struct is 16 bytes,
as on i386: base_addr is an unsigned int
(arch/x86/include/uapi/asm/ldt.h). So a 64-bit program calls
modify_ldt(0x11, &desc, 16), which Linux accepts.
qemu-x86_64 refuses it with EINVAL. Its struct target_modify_ldt_ldt_s
in linux-user/x86_64/target_syscall.h declares base_addr as an
abi_ulong, 8 bytes for x86-64, which makes the struct 24 bytes:
struct target_modify_ldt_ldt_s {
unsigned int entry_number;
abi_ulong base_addr;
unsigned int limit;
unsigned int flags;
};and write_ldt in linux-user/syscall.c checks the size against it:
if (bytecount != sizeof(ldt_info))
return -TARGET_EINVAL;A program can only install an LDT entry under qemu-x86_64 by passing a
24-byte struct, laid out with padding after entry_number and an 8-byte
base, which Linux refuses.
A second difference in the same function: for x86-64, write_ldt takes the
descriptor's L bit from the lm flag (bit 7 of the flags word), so a
program can install a 64-bit code segment in its LDT. Linux ignores lm.
fill_ldt in arch/x86/include/asm/desc.h always clears L ("Don't allow
setting of the lm bit. It would confuse user_64bit_mode and would get
overridden by sysret anyway."). This cannot be seen from the reproducer
until the size is fixed.
Steps to reproduce
- Save the program below as
ldt.cand build it on an x86-64 host:gcc -O2 -static -o ldt ldt.c - Run
./ldtnatively: both lines saycorrect, exit status 0. - Run
qemu-x86_64 ./ldt:
modify_ldt(0x11, &user_desc, 16) = -1 (Invalid argument): wrong
modify_ldt(0x11, &user_desc, 16) = -1 (Invalid argument): wrongWith only the size fixed, the second line shows the lm difference: the
entry reads back with bit 53, L, set.
/* modify_ldt takes Linux's 16-byte struct user_desc, and ignores its lm bit.
* Build: gcc -O2 -static -o ldt ldt.c
* Run: ./ldt, natively and under qemu-x86_64. */
#include <asm/ldt.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/syscall.h>
#include <unistd.h>
/* Installs a writable 32-bit data segment as LDT entry `n` (with `lm` as
* given) and returns the descriptor read back, or 0 when modify_ldt fails. */
static uint64_t install(unsigned n, unsigned lm)
{
struct user_desc d;
memset(&d, 0, sizeof d);
d.entry_number = n;
d.base_addr = 0x12345678;
d.limit = 0xabcd;
d.seg_32bit = 1;
d.lm = lm;
long r = syscall(SYS_modify_ldt, 0x11, &d, sizeof d);
if (r != 0) {
printf("modify_ldt(0x11, &user_desc, %zu) = %ld (%s): wrong\n", sizeof d, r, strerror(errno));
return 0;
}
uint64_t ldt[2] = {0, 0};
syscall(SYS_modify_ldt, 0, ldt, sizeof ldt);
/* Linux sets the accessed bit (bit 40) of the entries it installs. */
return ldt[n] & ~(1ull << 40);
}
int main(void)
{
/* Base 0x12345678, limit 0xabcd, present, DPL 3, writable data, D=1. */
const uint64_t want = 0x1240f2345678abcdull;
int bad = 0;
uint64_t got = install(0, 0);
if (got) {
printf("entry 0: %#018llx, expected %#018llx: %s\n", (unsigned long long)got,
(unsigned long long)want, got == want ? "correct" : "wrong");
bad |= got != want;
} else {
bad = 1;
}
/* Linux never sets a descriptor's L bit (bit 53) from lm. */
got = install(1, 1);
if (got) {
printf("entry 1 (lm = 1): %#018llx, expected %#018llx: %s\n", (unsigned long long)got,
(unsigned long long)want, got == want ? "correct" : "wrong");
bad |= got != want;
} else {
bad = 1;
}
return bad;
}Additional information
A possible fix: declare base_addr as an abi_uint, read it with
tswap32, and drop lm as Linux does. On i386 abi_ulong is already 32
bits, so nothing changes there. do_set_thread_area and
do_get_thread_area are i386-only (TARGET_ABI32), where lm is already 0.
--- a/linux-user/x86_64/target_syscall.h
+++ b/linux-user/x86_64/target_syscall.h
struct target_modify_ldt_ldt_s {
unsigned int entry_number;
- abi_ulong base_addr;
+ abi_uint base_addr;
unsigned int limit;
unsigned int flags;
};
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ static abi_long write_ldt(CPUX86State *env,
- ldt_info.base_addr = tswapal(target_ldt_info->base_addr);
+ ldt_info.base_addr = tswap32(target_ldt_info->base_addr);
@@
-#ifdef TARGET_ABI32
- lm = 0;
-#else
- lm = (ldt_info.flags >> 7) & 1;
-#endif
+ /* Like Linux, ignore lm: an LDT entry is never a 64-bit code segment. */
@@
- (lm << 21) |
0x7000;(The full patch is attached; it also changes the #if 0 copy of the struct
in the same header. checkpatch.pl reports no problems.)
With this change on master, the reproducer prints correct for both
entries under qemu-x86_64. Further checks:
qemu-i386builds, and installs LDT entries as before.- A randomized comparison of segment loads, far branches and IRET through
a nine-entry LDT installed with 16-byte structs agrees with the semantics
model, in 64-bit code under
qemu-x86_64and 32-bit code underqemu-i386. That is about a million tests per mode across all integer forms, and 5000 per form for the segment forms. This was with the fixes for two other bugs found in the same comparison, reported separately: a 16-bit SS abortsqemu-x86_64, and far CALL, far RET and IRET with a 16- or 32-bit operand size in 64-bit mode use SS's base and B bit.
Found while validating an x86 semantics model against QEMU and real AMD hardware.