From 1212468152cc6438842da673cba9b35ad9b6adfe Mon Sep 17 00:00:00 2001 From: Changyuan Lyu Date: Wed, 2 Dec 2026 09:41:02 -0800 Subject: [PATCH 1/5] fix(fw_cfg): advance offset and return EOF in FwCfgContentAccess::read FwCfgContentAccess::read() previously never advanced self.offset across calls and returned Err(ErrorKind::UnexpectedEof) when reading at or past EOF. If a guest memory write spanned multiple slices (causing multiple Read::read() calls), subsequent calls re-read from the original offset. Advance self.offset by the bytes read, return Ok(0) at EOF per the std::io::Read contract, and use FileExt::read_at() for file-backed items. Fixes: 01ff8cf33f50 ("feat(dev): implement QEMU's fw_cfg device") Signed-off-by: Changyuan Lyu --- alioth/src/device/fw_cfg/fw_cfg.rs | 39 +++++++++---------------- alioth/src/device/fw_cfg/fw_cfg_test.rs | 17 +++++++++++ 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/alioth/src/device/fw_cfg/fw_cfg.rs b/alioth/src/device/fw_cfg/fw_cfg.rs index 558e006e..a38d46f1 100644 --- a/alioth/src/device/fw_cfg/fw_cfg.rs +++ b/alioth/src/device/fw_cfg/fw_cfg.rs @@ -17,7 +17,7 @@ pub mod acpi; use std::ffi::CString; use std::fmt; use std::fs::File; -use std::io::{ErrorKind, Read, Result, Seek, SeekFrom}; +use std::io::{ErrorKind, Read, Result}; use std::mem::{size_of, size_of_val}; use std::os::unix::fs::FileExt; use std::path::Path; @@ -108,32 +108,19 @@ struct FwCfgContentAccess<'a> { impl Read for FwCfgContentAccess<'_> { fn read(&mut self, buf: &mut [u8]) -> Result { - match self.content { - FwCfgContent::File(offset, f) => { - Seek::seek(&mut (&*f), SeekFrom::Start(offset + self.offset as u64))?; - Read::read(&mut (&*f), buf) - } - FwCfgContent::Bytes(b) => match b.get(self.offset as usize..) { - Some(mut s) => s.read(buf), - None => Err(ErrorKind::UnexpectedEof)?, - }, - FwCfgContent::Slice(b) => match b.get(self.offset as usize..) { - Some(mut s) => s.read(buf), - None => Err(ErrorKind::UnexpectedEof)?, - }, - FwCfgContent::Lu16(n) => match n.as_bytes().get(self.offset as usize..) { - Some(mut s) => s.read(buf), - None => Err(ErrorKind::UnexpectedEof)?, - }, - FwCfgContent::Lu32(n) => match n.as_bytes().get(self.offset as usize..) { - Some(mut s) => s.read(buf), - None => Err(ErrorKind::UnexpectedEof)?, - }, - FwCfgContent::Lu64(n) => match n.as_bytes().get(self.offset as usize..) { - Some(mut s) => s.read(buf), - None => Err(ErrorKind::UnexpectedEof)?, - }, + fn read_from_bytes(bytes: &[u8], offset: u32, buf: &mut [u8]) -> Result { + bytes.get(offset as usize..).unwrap_or(&[]).read(buf) } + let n = match self.content { + FwCfgContent::File(offset, f) => f.read_at(buf, offset + self.offset as u64)?, + FwCfgContent::Bytes(b) => read_from_bytes(b, self.offset, buf)?, + FwCfgContent::Slice(b) => read_from_bytes(b, self.offset, buf)?, + FwCfgContent::Lu16(n) => read_from_bytes(n.as_bytes(), self.offset, buf)?, + FwCfgContent::Lu32(n) => read_from_bytes(n.as_bytes(), self.offset, buf)?, + FwCfgContent::Lu64(n) => read_from_bytes(n.as_bytes(), self.offset, buf)?, + }; + self.offset = self.offset.saturating_add(n as u32); + Ok(n) } } diff --git a/alioth/src/device/fw_cfg/fw_cfg_test.rs b/alioth/src/device/fw_cfg/fw_cfg_test.rs index 0c0afa20..16ddd6bf 100644 --- a/alioth/src/device/fw_cfg/fw_cfg_test.rs +++ b/alioth/src/device/fw_cfg/fw_cfg_test.rs @@ -109,3 +109,20 @@ fn test_fw_cfg_content_file_access() { let _ = fw_cfg_content.access(120).read(&mut buf); assert_eq!(&buf[..4], [0, 0, 0, 0]); } + +#[test] +fn test_fw_cfg_content_access_advances_offset_and_eof() { + let content = FwCfgContent::Slice(b"abcdef"); + let mut access = content.access(1); + + let mut first = [0u8; 2]; + assert_eq!(access.read(&mut first).unwrap(), 2); + assert_eq!(&first, b"bc"); + + let mut second = [0u8; 4]; + assert_eq!(access.read(&mut second).unwrap(), 3); + assert_eq!(&second[..3], b"def"); + + let mut eof = [0xffu8; 4]; + assert_eq!(access.read(&mut eof).unwrap(), 0); +} From de03d17a1b50f98325814ea4d5f88c8c3e79cafd Mon Sep 17 00:00:00 2001 From: Changyuan Lyu Date: Wed, 2 Dec 2026 09:41:37 -0800 Subject: [PATCH 2/5] fix(fw_cfg): zero-fill guest buffer on DMA read past EOF FwCfg::dma_read_content() only wrote min(content_size, len) bytes to guest memory and left the remainder untouched. When a guest reads an unset (empty) item such as FW_CFG_KERNEL_SIZE or FW_CFG_INITRD_SIZE via DMA into an uninitialized stack variable (as EDK2 QemuFwCfgRead32() does), the guest observes stale stack contents instead of zeros. Chain io::repeat(0) onto the item reader and write the full requested length, matching QEMU's fw_cfg_dma_transfer() behavior. Fixes: 01ff8cf33f50 ("feat(dev): implement QEMU's fw_cfg device") Signed-off-by: Changyuan Lyu --- alioth/src/device/fw_cfg/fw_cfg.rs | 16 +++--- alioth/src/device/fw_cfg/fw_cfg_test.rs | 72 ++++++++++++++++++++++++- 2 files changed, 80 insertions(+), 8 deletions(-) diff --git a/alioth/src/device/fw_cfg/fw_cfg.rs b/alioth/src/device/fw_cfg/fw_cfg.rs index a38d46f1..3a2c0e0b 100644 --- a/alioth/src/device/fw_cfg/fw_cfg.rs +++ b/alioth/src/device/fw_cfg/fw_cfg.rs @@ -15,13 +15,13 @@ pub mod acpi; use std::ffi::CString; -use std::fmt; use std::fs::File; -use std::io::{ErrorKind, Read, Result}; +use std::io::{self, ErrorKind, Read, Result}; use std::mem::{size_of, size_of_val}; use std::os::unix::fs::FileExt; use std::path::Path; use std::sync::Arc; +use std::{cmp, fmt}; use alioth_macros::Layout; use bitfield::bitfield; @@ -362,13 +362,15 @@ impl FwCfg { address: u64, ) -> Result { let content_size = content.size()?.saturating_sub(offset); - let op_size = std::cmp::min(content_size, len); - let r = self - .memory - .write_range(address, op_size as u64, content.access(offset)); + let op_size = cmp::min(content_size, len); + let r = self.memory.write_range( + address, + len as u64, + content.access(offset).chain(io::repeat(0)), + ); match r { Err(e) => { - log::error!("fw_cfg: dam read error: {e:x?}"); + log::error!("fw_cfg: dma read error: {e:x?}"); Err(ErrorKind::InvalidInput.into()) } Ok(()) => Ok(op_size), diff --git a/alioth/src/device/fw_cfg/fw_cfg_test.rs b/alioth/src/device/fw_cfg/fw_cfg_test.rs index 16ddd6bf..7472b4f8 100644 --- a/alioth/src/device/fw_cfg/fw_cfg_test.rs +++ b/alioth/src/device/fw_cfg/fw_cfg_test.rs @@ -14,12 +14,20 @@ use std::fs::{self, File}; use std::io::{self, Read}; +use std::sync::Arc; use assert_matches::assert_matches; +use libc::{PROT_READ, PROT_WRITE}; +use parking_lot::Mutex; use rstest::rstest; use tempfile::TempDir; -use crate::device::fw_cfg::FwCfgContent; +use crate::arch::layout::{PORT_FW_CFG_DMA_HI, PORT_FW_CFG_DMA_LO, PORT_FW_CFG_SELECTOR}; +use crate::device::fw_cfg::{ + FW_CFG_FILE_FIRST, FW_CFG_INITRD_SIZE, FwCfg, FwCfgContent, FwCfgDmaAccess, FwCfgItem, +}; +use crate::mem::emulated::Mmio; +use crate::mem::mapped::{ArcMemPages, RamBus}; fn create_file_with_content(content: &str) -> io::Result { let tmp_dir = TempDir::new()?; @@ -126,3 +134,65 @@ fn test_fw_cfg_content_access_advances_offset_and_eof() { let mut eof = [0xffu8; 4]; assert_eq!(access.read(&mut eof).unwrap(), 0); } + +fn create_test_ram_bus() -> Arc { + const PAGE_SIZE: usize = 0x1000; + let bus = Arc::new(RamBus::new()); + let prot = PROT_READ | PROT_WRITE; + let low_mem = ArcMemPages::from_anonymous(PAGE_SIZE, Some(prot), None).unwrap(); + let high_mem = ArcMemPages::from_anonymous(PAGE_SIZE, Some(prot), None).unwrap(); + bus.update(|ram| ram.add(0x1000, low_mem)).unwrap(); + bus.update(|ram| ram.add(0x2_0000_0000, high_mem)).unwrap(); + bus +} + +fn run_dma(dev: &Mutex, bus: &RamBus, dma_gpa: u64, access: &FwCfgDmaAccess) -> u32 { + bus.write_t(dma_gpa, access).unwrap(); + let dma_hi_off = (PORT_FW_CFG_DMA_HI - PORT_FW_CFG_SELECTOR) as u64; + let dma_lo_off = (PORT_FW_CFG_DMA_LO - PORT_FW_CFG_SELECTOR) as u64; + let hi = (dma_gpa >> 32) as u32; + let lo = (dma_gpa & 0xffff_ffff) as u32; + if hi != 0 { + dev.write(dma_hi_off, 4, hi.to_be() as u64).unwrap(); + } + dev.write(dma_lo_off, 4, lo.to_be() as u64).unwrap(); + let resp: FwCfgDmaAccess = bus.read_t(dma_gpa).unwrap(); + resp.control.into() +} + +#[test] +fn test_fw_cfg_dma_read_zero_fills_past_eof() { + let bus = create_test_ram_bus(); + let item = FwCfgItem { + name: "test/short".to_owned(), + content: FwCfgContent::Slice(b"hi"), + }; + let dev = Mutex::new(FwCfg::new(bus.clone(), vec![item]).unwrap()); + let dma_gpa = 0x1000; + let buf_gpa = 0x1100; + + // Unset known item (empty slice): destination buffer must be zero-filled. + bus.write_t(buf_gpa, &0xdead_beef_u32).unwrap(); + dev.write(0, 2, FW_CFG_INITRD_SIZE as u64).unwrap(); + let access = FwCfgDmaAccess { + control: (1u32 << 1).into(), // READ + length: 4u32.into(), + address: buf_gpa.into(), + }; + assert_eq!(run_dma(&dev, &bus, dma_gpa, &access), 0); + assert_eq!(bus.read_t::(buf_gpa).unwrap(), 0); + + // Partial read past EOF: copies item bytes and zero-fills remainder. + bus.write_t(buf_gpa, &[0xaa_u8; 6]).unwrap(); + dev.write(0, 2, FW_CFG_FILE_FIRST as u64).unwrap(); + let access = FwCfgDmaAccess { + control: (1u32 << 1).into(), // READ + length: 6u32.into(), + address: buf_gpa.into(), + }; + assert_eq!(run_dma(&dev, &bus, dma_gpa, &access), 0); + assert_eq!( + bus.read_t::<[u8; 6]>(buf_gpa).unwrap(), + [b'h', b'i', 0, 0, 0, 0] + ); +} From 681f5d00845565a8f731c84667b344b302695261 Mon Sep 17 00:00:00 2001 From: Changyuan Lyu Date: Wed, 2 Dec 2026 09:42:03 -0800 Subject: [PATCH 3/5] fix(fw_cfg): reset dma_address after DMA transfer Per the QEMU fw_cfg specification, a guest submitting a 32-bit DMA descriptor address only needs to write the low 32 bits (DMA_LO) and may omit writing DMA_HI. Because FwCfg::do_dma() never cleared self.dma_address, a 32-bit DMA transfer following a >4 GiB transfer inherited the stale upper 32 bits. Reset self.dma_address to 0 at the start of do_dma(). Fixes: 01ff8cf33f50 ("feat(dev): implement QEMU's fw_cfg device") Signed-off-by: Changyuan Lyu --- alioth/src/device/fw_cfg/fw_cfg.rs | 1 + alioth/src/device/fw_cfg/fw_cfg_test.rs | 33 ++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/alioth/src/device/fw_cfg/fw_cfg.rs b/alioth/src/device/fw_cfg/fw_cfg.rs index 3a2c0e0b..7fc9008f 100644 --- a/alioth/src/device/fw_cfg/fw_cfg.rs +++ b/alioth/src/device/fw_cfg/fw_cfg.rs @@ -396,6 +396,7 @@ impl FwCfg { fn do_dma(&mut self) { let dma_address = self.dma_address; + self.dma_address = 0; let dma_access: FwCfgDmaAccess = match self.memory.read_t(dma_address) { Ok(access) => access, Err(e) => { diff --git a/alioth/src/device/fw_cfg/fw_cfg_test.rs b/alioth/src/device/fw_cfg/fw_cfg_test.rs index 7472b4f8..8a9d3d5c 100644 --- a/alioth/src/device/fw_cfg/fw_cfg_test.rs +++ b/alioth/src/device/fw_cfg/fw_cfg_test.rs @@ -24,7 +24,8 @@ use tempfile::TempDir; use crate::arch::layout::{PORT_FW_CFG_DMA_HI, PORT_FW_CFG_DMA_LO, PORT_FW_CFG_SELECTOR}; use crate::device::fw_cfg::{ - FW_CFG_FILE_FIRST, FW_CFG_INITRD_SIZE, FwCfg, FwCfgContent, FwCfgDmaAccess, FwCfgItem, + FW_CFG_FILE_FIRST, FW_CFG_INITRD_SIZE, FW_CFG_SIGNATURE, FwCfg, FwCfgContent, FwCfgDmaAccess, + FwCfgItem, }; use crate::mem::emulated::Mmio; use crate::mem::mapped::{ArcMemPages, RamBus}; @@ -196,3 +197,33 @@ fn test_fw_cfg_dma_read_zero_fills_past_eof() { [b'h', b'i', 0, 0, 0, 0] ); } + +#[test] +fn test_fw_cfg_dma_resets_address_after_transfer() { + let bus = create_test_ram_bus(); + let dev = Mutex::new(FwCfg::new(bus.clone(), vec![]).unwrap()); + + // Perform a >4 GiB DMA transfer (writes both DMA_HI and DMA_LO). + let high_dma_gpa = 0x2_0000_0000; + let high_buf_gpa = 0x2_0000_0100; + dev.write(0, 2, FW_CFG_SIGNATURE as u64).unwrap(); + let high_access = FwCfgDmaAccess { + control: (1u32 << 1).into(), // READ + length: 4u32.into(), + address: high_buf_gpa.into(), + }; + assert_eq!(run_dma(&dev, &bus, high_dma_gpa, &high_access), 0); + assert_eq!(bus.read_t::<[u8; 4]>(high_buf_gpa).unwrap(), *b"QEMU"); + + // Follow with a 32-bit DMA transfer (writes only DMA_LO); upper bits must be 0. + let low_dma_gpa = 0x1000; + let low_buf_gpa = 0x1100; + dev.write(0, 2, FW_CFG_SIGNATURE as u64).unwrap(); + let low_access = FwCfgDmaAccess { + control: (1u32 << 1).into(), // READ + length: 4u32.into(), + address: low_buf_gpa.into(), + }; + assert_eq!(run_dma(&dev, &bus, low_dma_gpa, &low_access), 0); + assert_eq!(bus.read_t::<[u8; 4]>(low_buf_gpa).unwrap(), *b"QEMU"); +} From 343629864ff3a1f76c17618460703c2aabac78d3 Mon Sep 17 00:00:00 2001 From: Changyuan Lyu Date: Wed, 2 Dec 2026 09:42:24 -0800 Subject: [PATCH 4/5] fix(fw_cfg): fix selector and offset handling on DMA select In FwCfg::do_dma(), handling the SELECT control bit assigned control.select() as u16 (always 1) instead of control.selector() as u16, did not reset self.data_offset to 0, and rejected select-only DMA descriptors with an error. Read the selector from control.selector(), reset self.data_offset to 0, and treat select-only DMA descriptors as valid no-op transfers. Fixes: 01ff8cf33f50 ("feat(dev): implement QEMU's fw_cfg device") Signed-off-by: Changyuan Lyu --- alioth/src/device/fw_cfg/fw_cfg.rs | 5 ++- alioth/src/device/fw_cfg/fw_cfg_test.rs | 45 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/alioth/src/device/fw_cfg/fw_cfg.rs b/alioth/src/device/fw_cfg/fw_cfg.rs index 7fc9008f..c4b240ab 100644 --- a/alioth/src/device/fw_cfg/fw_cfg.rs +++ b/alioth/src/device/fw_cfg/fw_cfg.rs @@ -406,7 +406,8 @@ impl FwCfg { }; let control = AccessControl(dma_access.control.into()); if control.select() { - self.selector = control.select() as u16; + self.selector = control.selector() as u16; + self.data_offset = 0; } let len = dma_access.length.to_ne(); let addr = dma_access.address.to_ne(); @@ -417,6 +418,8 @@ impl FwCfg { } else if control.skip() { self.data_offset += len; Ok(()) + } else if control.select() { + Ok(()) } else { Err(ErrorKind::InvalidData.into()) }; diff --git a/alioth/src/device/fw_cfg/fw_cfg_test.rs b/alioth/src/device/fw_cfg/fw_cfg_test.rs index 8a9d3d5c..dfda3952 100644 --- a/alioth/src/device/fw_cfg/fw_cfg_test.rs +++ b/alioth/src/device/fw_cfg/fw_cfg_test.rs @@ -227,3 +227,48 @@ fn test_fw_cfg_dma_resets_address_after_transfer() { assert_eq!(run_dma(&dev, &bus, low_dma_gpa, &low_access), 0); assert_eq!(bus.read_t::<[u8; 4]>(low_buf_gpa).unwrap(), *b"QEMU"); } + +#[test] +fn test_fw_cfg_dma_select() { + let bus = create_test_ram_bus(); + let item = FwCfgItem { + name: "test/item".to_owned(), + content: FwCfgContent::Slice(b"abcd"), + }; + let dev = Mutex::new(FwCfg::new(bus.clone(), vec![item]).unwrap()); + let dma_gpa = 0x1000; + let buf_gpa = 0x1100; + + // Advance offset on FW_CFG_FILE_FIRST via SKIP, then re-select via DMA SELECT | READ. + dev.write(0, 2, FW_CFG_FILE_FIRST as u64).unwrap(); + let skip_access = FwCfgDmaAccess { + control: (1u32 << 2).into(), // SKIP + length: 2u32.into(), + address: 0u64.into(), + }; + assert_eq!(run_dma(&dev, &bus, dma_gpa, &skip_access), 0); + + let select_read = FwCfgDmaAccess { + control: (((FW_CFG_FILE_FIRST as u32) << 16) | (1 << 3) | (1 << 1)).into(), + length: 4u32.into(), + address: buf_gpa.into(), + }; + assert_eq!(run_dma(&dev, &bus, dma_gpa, &select_read), 0); + assert_eq!(bus.read_t::<[u8; 4]>(buf_gpa).unwrap(), *b"abcd"); + + // Select-only DMA succeeds and resets offset. + let select_only = FwCfgDmaAccess { + control: (((FW_CFG_SIGNATURE as u32) << 16) | (1 << 3)).into(), + length: 0u32.into(), + address: 0u64.into(), + }; + assert_eq!(run_dma(&dev, &bus, dma_gpa, &select_only), 0); + + let read_after_select = FwCfgDmaAccess { + control: (1u32 << 1).into(), // READ + length: 4u32.into(), + address: buf_gpa.into(), + }; + assert_eq!(run_dma(&dev, &bus, dma_gpa, &read_after_select), 0); + assert_eq!(bus.read_t::<[u8; 4]>(buf_gpa).unwrap(), *b"QEMU"); +} From 9f64eb97cf2b536cb666ff90f3e2ef19ea116f9d Mon Sep 17 00:00:00 2001 From: Changyuan Lyu Date: Wed, 2 Dec 2026 09:42:45 -0800 Subject: [PATCH 5/5] fix(fw_cfg): avoid VMM panic on DMA write and skip overflow A malicious or misbehaving guest could crash the VMM via fw_cfg DMA by either issuing a WRITE request (hitting unimplemented!() in dma_write()) or issuing SKIP requests whose lengths overflow data_offset. Return ErrorKind::Unsupported from dma_write() so do_dma() reports the DMA error bit to the guest, and use saturating_add() for DMA SKIP. Fixes: 01ff8cf33f50 ("feat(dev): implement QEMU's fw_cfg device") Signed-off-by: Changyuan Lyu --- alioth/src/device/fw_cfg/fw_cfg.rs | 4 ++-- alioth/src/device/fw_cfg/fw_cfg_test.rs | 26 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/alioth/src/device/fw_cfg/fw_cfg.rs b/alioth/src/device/fw_cfg/fw_cfg.rs index c4b240ab..e1f96234 100644 --- a/alioth/src/device/fw_cfg/fw_cfg.rs +++ b/alioth/src/device/fw_cfg/fw_cfg.rs @@ -391,7 +391,7 @@ impl FwCfg { } fn dma_write(&self, _selector: u16, _len: u32, _address: u64) -> Result<()> { - unimplemented!() + Err(ErrorKind::Unsupported.into()) } fn do_dma(&mut self) { @@ -416,7 +416,7 @@ impl FwCfg { } else if control.write() { self.dma_write(self.selector, len, addr) } else if control.skip() { - self.data_offset += len; + self.data_offset = self.data_offset.saturating_add(len); Ok(()) } else if control.select() { Ok(()) diff --git a/alioth/src/device/fw_cfg/fw_cfg_test.rs b/alioth/src/device/fw_cfg/fw_cfg_test.rs index dfda3952..00a61509 100644 --- a/alioth/src/device/fw_cfg/fw_cfg_test.rs +++ b/alioth/src/device/fw_cfg/fw_cfg_test.rs @@ -272,3 +272,29 @@ fn test_fw_cfg_dma_select() { assert_eq!(run_dma(&dev, &bus, dma_gpa, &read_after_select), 0); assert_eq!(bus.read_t::<[u8; 4]>(buf_gpa).unwrap(), *b"QEMU"); } + +#[test] +fn test_fw_cfg_dma_write_unsupported_and_skip_saturates() { + let bus = create_test_ram_bus(); + let dev = Mutex::new(FwCfg::new(bus.clone(), vec![]).unwrap()); + let dma_gpa = 0x1000; + let buf_gpa = 0x1100; + + // Unsupported DMA WRITE sets error bit instead of panicking. + let write_access = FwCfgDmaAccess { + control: (1u32 << 4).into(), // WRITE + length: 4u32.into(), + address: buf_gpa.into(), + }; + assert_eq!(run_dma(&dev, &bus, dma_gpa, &write_access), 1); + + // Large DMA SKIP saturates data_offset instead of overflowing. + dev.write(0, 2, FW_CFG_SIGNATURE as u64).unwrap(); + let skip_access = FwCfgDmaAccess { + control: (1u32 << 2).into(), // SKIP + length: u32::MAX.into(), + address: 0u64.into(), + }; + assert_eq!(run_dma(&dev, &bus, dma_gpa, &skip_access), 0); + assert_eq!(run_dma(&dev, &bus, dma_gpa, &skip_access), 0); +}