Skip to content

Commit bd01b6d

Browse files
Alexandra Iordacheandreeaflorescu
authored andcommitted
FDT configurator: specific FDT size check
Signed-off-by: Alexandra Iordache <aghecen@amazon.com>
1 parent 4db08d9 commit bd01b6d

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

coverage_config_aarch64.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 79.9,
2+
"coverage_score": 80.3,
33
"exclude_path": "",
44
"crate_features": ""
55
}

src/configurator/aarch64/fdt.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use crate::configurator::{BootConfigurator, BootParams, Error as BootConfigurato
1414
/// Errors specific to the device tree boot protocol configuration.
1515
#[derive(Debug, PartialEq)]
1616
pub enum Error {
17+
/// FDT does not fit in guest memory.
18+
FDTPastRamEnd,
1719
/// Error writing FDT in memory.
1820
WriteFDTToMemory,
1921
}
@@ -22,6 +24,7 @@ impl StdError for Error {
2224
fn description(&self) -> &str {
2325
use Error::*;
2426
match self {
27+
FDTPastRamEnd => "FDT does not fit in guest memory.",
2528
WriteFDTToMemory => "Error writing FDT in guest memory.",
2629
}
2730
}
@@ -57,6 +60,10 @@ impl BootConfigurator for FdtBootConfigurator {
5760
where
5861
M: GuestMemory,
5962
{
63+
guest_memory
64+
.checked_offset(params.header_start, params.header.len())
65+
.ok_or(Error::FDTPastRamEnd)?;
66+
6067
// The VMM has filled an FDT and passed it as a `ByteValued` object.
6168
guest_memory
6269
.write_slice(params.header.as_slice(), params.header_start)
@@ -86,23 +93,17 @@ mod tests {
8693
let guest_memory = create_guest_mem();
8794

8895
// Error case: FDT doesn't fit in guest memory.
89-
let fdt_addr = guest_memory
90-
.last_addr()
91-
.checked_sub(FDT_MAX_SIZE as u64 - 2)
92-
.unwrap();
96+
let fdt_addr = GuestAddress(guest_memory.last_addr().raw_value() - FDT_MAX_SIZE as u64 + 1);
9397
assert_eq!(
9498
FdtBootConfigurator::write_bootparams::<GuestMemoryMmap>(
9599
BootParams::new::<FdtPlaceholder>(&fdt, fdt_addr),
96100
&guest_memory,
97101
)
98102
.err(),
99-
Some(Error::WriteFDTToMemory.into())
103+
Some(Error::FDTPastRamEnd.into())
100104
);
101105

102-
let fdt_addr = guest_memory
103-
.last_addr()
104-
.checked_sub(FDT_MAX_SIZE as u64 - 1)
105-
.unwrap();
106+
let fdt_addr = GuestAddress(guest_memory.last_addr().raw_value() - FDT_MAX_SIZE as u64);
106107
assert!(FdtBootConfigurator::write_bootparams::<GuestMemoryMmap>(
107108
BootParams::new::<FdtPlaceholder>(&fdt, fdt_addr),
108109
&guest_memory,
@@ -112,9 +113,13 @@ mod tests {
112113

113114
#[test]
114115
fn test_error_messages() {
116+
assert_eq!(
117+
format!("{}", Error::FDTPastRamEnd),
118+
"Device Tree Boot Configurator Error: FDT does not fit in guest memory."
119+
);
115120
assert_eq!(
116121
format!("{}", Error::WriteFDTToMemory),
117122
"Device Tree Boot Configurator Error: Error writing FDT in guest memory."
118-
)
123+
);
119124
}
120125
}

0 commit comments

Comments
 (0)