Skip to content

Commit af793ce

Browse files
committed
virt_mshv_vtl: fix TDX legacy LGDT/LIDT descriptor decode panic
Legacy-mode LGDT/LIDT descriptors carry a 4-byte base, but the decoder always converted the base bytes to a u64, panicking on valid input. Zero-extend the legacy 32-bit base and validate the descriptor length. Test changes: - Verify a legacy 6-byte descriptor decodes without panicking. - Verify a long-mode 10-byte descriptor keeps its full 64-bit base.
1 parent 35dfc23 commit af793ce

3 files changed

Lines changed: 49 additions & 4 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10294,6 +10294,7 @@ dependencies = [
1029410294
"safe_intrinsics",
1029510295
"safeatomic",
1029610296
"sidecar_client",
10297+
"test_with_tracing",
1029710298
"thiserror 2.0.16",
1029810299
"tracelimit",
1029910300
"tracing",

‎openhcl/virt_mshv_vtl/Cargo.toml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,8 @@ virt_support_aarch64emu.workspace = true
6060
[build-dependencies]
6161
build_rs_guest_arch.workspace = true
6262

63+
[dev-dependencies]
64+
test_with_tracing.workspace = true
65+
6366
[lints]
6467
workspace = true

‎openhcl/virt_mshv_vtl/src/processor/tdx/mod.rs‎

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3254,6 +3254,20 @@ impl TdxSegmentReg {
32543254
}
32553255
}
32563256

3257+
fn decode_table_register(buf: &[u8]) -> TableRegister {
3258+
let base_bytes = match buf.len() {
3259+
6 | 10 => &buf[2..],
3260+
len => panic!("invalid descriptor length {len}"),
3261+
};
3262+
// Zero-padding the little-endian base zero-extends a legacy 32-bit base.
3263+
let mut base = [0u8; 8];
3264+
base[..base_bytes.len()].copy_from_slice(base_bytes);
3265+
TableRegister {
3266+
limit: u16::from_le_bytes([buf[0], buf[1]]),
3267+
base: u64::from_le_bytes(base),
3268+
}
3269+
}
3270+
32573271
#[derive(Debug)]
32583272
enum TdxTableReg {
32593273
Idtr,
@@ -3456,10 +3470,7 @@ impl UhProcessor<'_, TdxBacked> {
34563470
EmulatedMemoryOperation::Read(&mut buf[..len]),
34573471
)
34583472
.await?;
3459-
let table = TableRegister {
3460-
limit: u16::from_le_bytes(buf[..2].try_into().unwrap()),
3461-
base: u64::from_le_bytes(buf[2..len].try_into().unwrap()),
3462-
};
3473+
let table = decode_table_register(&buf[..len]);
34633474
self.write_table_register(
34643475
vtl,
34653476
if matches!(instr_info.instruction(), GdtrOrIdtrInstruction::Lidt) {
@@ -4616,6 +4627,36 @@ impl TlbFlushLockAccess for TdxTlbLockFlushAccess<'_> {
46164627
}
46174628
}
46184629

4630+
#[cfg(test)]
4631+
mod tests {
4632+
use super::decode_table_register;
4633+
use test_with_tracing::test;
4634+
4635+
// Test: Decode a valid six-byte descriptor used by legacy LGDT/LIDT.
4636+
// Expected: Return its limit and zero-extended 32-bit base without panicking.
4637+
#[test]
4638+
fn legacy_descriptor_load_decodes_limit_and_base() {
4639+
let descriptor = [0x3f, 0x00, 0x78, 0x56, 0x34, 0x12];
4640+
4641+
let table = decode_table_register(&descriptor);
4642+
4643+
assert_eq!(table.limit, 0x003f);
4644+
assert_eq!(table.base, 0x0000_0000_1234_5678);
4645+
}
4646+
4647+
// Test: Decode a valid ten-byte descriptor used by long-mode LGDT/LIDT.
4648+
// Expected: Return its limit and preserve all 64 bits of its base.
4649+
#[test]
4650+
fn long_mode_descriptor_load_decodes_limit_and_base() {
4651+
let descriptor = [0xff, 0x01, 0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01];
4652+
4653+
let table = decode_table_register(&descriptor);
4654+
4655+
assert_eq!(table.limit, 0x01ff);
4656+
assert_eq!(table.base, 0x0123_4567_89ab_cdef);
4657+
}
4658+
}
4659+
46194660
mod save_restore {
46204661
use super::TdxBacked;
46214662
use super::UhProcessor;

0 commit comments

Comments
 (0)