Skip to content

Commit

Permalink
got rid of check_vm_instruction related allocations.
Browse files Browse the repository at this point in the history
  • Loading branch information
francis-starlab committed Jan 22, 2021
1 parent b0bfd75 commit c9fa2f4
Show file tree
Hide file tree
Showing 22 changed files with 160 additions and 167 deletions.
44 changes: 23 additions & 21 deletions mythril/src/acpi/madt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use num_enum::TryFromPrimitive;
/// SDT (the end of the Creator Revision at offset 36).
mod offsets {
use super::*;

/// 32-bit physical address at which each processor can access its
/// local APIC.
pub const LOCAL_INT_CTRL_ADDR: Range<usize> = 0..4;
Expand Down Expand Up @@ -81,8 +80,7 @@ impl IcsType {
if length == self.expected_len() as usize - 2 {
Ok(())
} else {
error!("Invalid length={} for type=0x{:x}",
*self as u8, length);
error!("Invalid length={} for type=0x{:x}", *self as u8, length);
Err(Error::InvalidValue)
}
}
Expand Down Expand Up @@ -255,16 +253,18 @@ impl Ics {
apic_proc_uid: NativeEndian::read_u32(&bytes[10..14]),
}),
_ => {
error!("type=0x{:x} length={} not implemented",
ty as u8,
bytes.len());
error!(
"type=0x{:x} length={} not implemented",
ty as u8,
bytes.len()
);
Err(Error::NotImplemented)
}
}
}

/// Encode into the byte sequence
pub fn encode<T: Array<Item=u8>>(
pub fn encode<T: Array<Item = u8>>(
&self,
buffer: &mut ArrayVec<T>,
) -> Result<()> {
Expand Down Expand Up @@ -295,8 +295,10 @@ impl Ics {
NativeEndian::write_u32(&mut tmp_buf[8..12], gsi_base);
}
_ => {
error!("The ICS Type {:?} has not been implemented",
self.ics_type());
error!(
"The ICS Type {:?} has not been implemented",
self.ics_type()
);
return Err(Error::NotImplemented);
}
}
Expand Down Expand Up @@ -393,22 +395,22 @@ impl<'a> Iterator for IcsIterator<'a> {
let ty = match IcsType::try_from(self.bytes[0]) {
Ok(ty) => ty,
_ => {
error!("Invalid ICS type: {}",
self.bytes[0]);
error!("Invalid ICS type: {}", self.bytes[0]);
return Some(Err(Error::InvalidValue));
}
};
let len = self.bytes[1] as usize;

if len > self.bytes.len() {
error!("Payload for type=0x{:x} and len={} to big for buffer len={}",
ty as u8,
len,
self.bytes.len());
error!(
"Payload for type=0x{:x} and len={} to big for buffer len={}",
ty as u8,
len,
self.bytes.len()
);
return Some(Err(Error::InvalidValue));
} else if len < 3 {
error!("length `{}` provided is too small",
len);
error!("length `{}` provided is too small", len);
return Some(Err(Error::InvalidValue));
}

Expand All @@ -434,7 +436,7 @@ pub struct MADTBuilder<T: Array> {
structures: ArrayVec<T>,
}

impl<T: Array<Item=Ics>> MADTBuilder<T> {
impl<T: Array<Item = Ics>> MADTBuilder<T> {
/// Create a new builder for the MADT SDT.
pub fn new() -> MADTBuilder<T> {
MADTBuilder {
Expand Down Expand Up @@ -462,8 +464,8 @@ impl<T: Array<Item=Ics>> MADTBuilder<T> {
}

impl<U> SDTBuilder for MADTBuilder<U>
where
U: Array<Item=Ics>,
where
U: Array<Item = Ics>,
{
const SIGNATURE: [u8; 4] = [b'A', b'P', b'I', b'C'];

Expand All @@ -472,7 +474,7 @@ impl<U> SDTBuilder for MADTBuilder<U>
5u8
}

fn encode_table<T: Array<Item=u8>>(
fn encode_table<T: Array<Item = u8>>(
&mut self,
buffer: &mut ArrayVec<T>,
) -> Result<()> {
Expand Down
6 changes: 5 additions & 1 deletion mythril/src/acpi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ pub(self) fn verify_checksum(bytes: &[u8], cksum_idx: usize) -> Result<()> {
if (result & 0xff) == 0x00 {
Ok(())
} else {
error!("Checksum mismatch checksum={:x} {:x} != 0x00", bytes[cksum_idx], result & 0xff);
error!(
"Checksum mismatch checksum={:x} {:x} != 0x00",
bytes[cksum_idx],
result & 0xff
);
Err(Error::InvalidValue)
}
}
Expand Down
21 changes: 10 additions & 11 deletions mythril/src/acpi/rsdp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,8 @@ impl RSDP {
xsdt_addr: NativeEndian::read_u64(&bytes[offsets::XSDT_ADDR]),
},
_ => {
error!("Invalid RSDP revision: {}",
bytes[offsets::REVISION]);
return Err(Error::InvalidValue)
.into();
error!("Invalid RSDP revision: {}", bytes[offsets::REVISION]);
return Err(Error::InvalidValue).into();
}
};

Expand Down Expand Up @@ -179,9 +177,11 @@ impl RSDP {
Ok(&range[i..rsdp_v2_end])
}
_ => {
error!("Invalid RSDP revision: {} at {:p}",
candidate[offsets::REVISION],
candidate.as_ptr());
error!(
"Invalid RSDP revision: {} at {:p}",
candidate[offsets::REVISION],
candidate.as_ptr()
);
Err(Error::InvalidValue)
}
};
Expand All @@ -205,8 +205,7 @@ impl RSDP {
offsets::EXT_CHECKSUM,
),
_ => {
error!("Invalid RSDP revision: {}",
bytes[offsets::REVISION]);
error!("Invalid RSDP revision: {}", bytes[offsets::REVISION]);
Err(Error::InvalidValue)
}
}
Expand All @@ -222,11 +221,11 @@ impl RSDP {
}

/// Builder structure for the RSDP
pub struct RSDPBuilder<'a, T: Array<Item=u8>> {
pub struct RSDPBuilder<'a, T: Array<Item = u8>> {
builder: RSDTBuilder<'a, T>,
}

impl<'a, T: Array<Item=u8>> RSDPBuilder<'a, T> {
impl<'a, T: Array<Item = u8>> RSDPBuilder<'a, T> {
/// Create a new RSDP Builder.
pub fn new(
map: ManagedMap<'a, [u8; 4], (ArrayVec<T>, usize)>,
Expand Down
21 changes: 13 additions & 8 deletions mythril/src/acpi/rsdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,11 @@ fn write_sdt_header(
// The SDT length value is the value of the entire SDT including
// the header.
if buffer.len() < sdt_len {
error!("Buffer length should be at least `{}` but was `{}`",
sdt_len,
buffer.len());
error!(
"Buffer length should be at least `{}` but was `{}`",
sdt_len,
buffer.len()
);
return Err(Error::InvalidValue);
}
// Fill in the SDT header with the implementations values
Expand Down Expand Up @@ -338,8 +340,10 @@ impl<'a, T: Array<Item = u8>> RSDTBuilder<'a, T> {
Ok(())
}
} else {
error!("The key `{}` already exists",
str::from_utf8(&U::SIGNATURE).unwrap());
error!(
"The key `{}` already exists",
str::from_utf8(&U::SIGNATURE).unwrap()
);
Err(Error::InvalidValue)
}
}
Expand Down Expand Up @@ -371,10 +375,11 @@ impl<'a, T: Array<Item = u8>> RSDTBuilder<'a, T> {
})?;

for (i, (name, (sdt, size))) in self.map.iter().enumerate() {
const LEN_OF_ETC_MYTHRIL :usize = 12;
const LEN_OF_ETC_MYTHRIL: usize = 12;
const LEN_OF_NAME: usize = 4;
let mut table_name_bytes = [0u8;LEN_OF_ETC_MYTHRIL + LEN_OF_NAME];
table_name_bytes[0..LEN_OF_ETC_MYTHRIL].copy_from_slice("etc/mythril/".as_bytes());
let mut table_name_bytes = [0u8; LEN_OF_ETC_MYTHRIL + LEN_OF_NAME];
table_name_bytes[0..LEN_OF_ETC_MYTHRIL]
.copy_from_slice("etc/mythril/".as_bytes());
table_name_bytes[LEN_OF_ETC_MYTHRIL..].copy_from_slice(name);
let table_name = str::from_utf8(&table_name_bytes)?;

Expand Down
2 changes: 1 addition & 1 deletion mythril/src/emulate/controlreg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn emulate_access(
},
_ => {
error!("Unsupported CR number access");
return Err(Error::InvalidValue)
return Err(Error::InvalidValue);
}
}
Ok(())
Expand Down
17 changes: 9 additions & 8 deletions mythril/src/emulate/memio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ fn read_register_value(

_ => {
error!("Invalid register '{:?}'", register);
return Err(Error::InvalidValue)
return Err(Error::InvalidValue);
}
}

Expand Down Expand Up @@ -351,9 +351,8 @@ fn do_mmio_read(
}

register => {
error!("mmio read into invalid register '{:?}'",
register);
return Err(Error::InvalidValue)
error!("mmio read into invalid register '{:?}'", register);
return Err(Error::InvalidValue);
}
},
_ => return Err(Error::NotSupported),
Expand Down Expand Up @@ -431,10 +430,12 @@ fn process_memio_op(
{
do_mmio_read(addr, vcpu, guest_cpu, responses, instr, on_read)?;
} else {
error!("Unsupported mmio instruction: {:?} (rip=0x{:x}, bytes={:?})",
instr.code(),
ip,
bytes);
error!(
"Unsupported mmio instruction: {:?} (rip=0x{:x}, bytes={:?})",
instr.code(),
ip,
bytes
);
return Err(Error::InvalidValue);
}
Ok(())
Expand Down
15 changes: 7 additions & 8 deletions mythril/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::vmcs;
use alloc::string::String;
use arrayvec::CapacityError;
use core::convert::TryFrom;
use core::num::TryFromIntError;
Expand Down Expand Up @@ -44,11 +43,11 @@ pub enum VmInstructionError {
InvalidOperandToInveptInvvpid = 28,
}

pub fn check_vm_insruction(rflags: u64, error: String) -> Result<()> {
pub fn check_vm_instruction(rflags: u64, log_error: impl Fn()) -> Result<()> {
let rflags = rflags::RFlags::from_bits_truncate(rflags);

if rflags.contains(RFlags::FLAGS_CF) {
error!("{}",error);
log_error();
Err(Error::VmFailInvalid)
} else if rflags.contains(RFlags::FLAGS_ZF) {
let errno = unsafe {
Expand All @@ -63,8 +62,8 @@ pub fn check_vm_insruction(rflags: u64, error: String) -> Result<()> {
let vm_error = VmInstructionError::try_from(errno)
.unwrap_or(VmInstructionError::UnknownError);

error!("{:?}",vm_error);
error!("{}",error);
error!("{:?}", vm_error);
log_error();
Err(Error::VmFailValid)
} else {
Ok(())
Expand All @@ -88,21 +87,21 @@ pub enum Error {

impl<T: TryFromPrimitive> From<TryFromPrimitiveError<T>> for Error {
fn from(error: TryFromPrimitiveError<T>) -> Error {
error!("{}",error);
error!("{}", error);
Error::InvalidValue
}
}

impl From<TryFromIntError> for Error {
fn from(error: TryFromIntError) -> Error {
error!("{}",error);
error!("{}", error);
Error::InvalidValue
}
}

impl From<core::str::Utf8Error> for Error {
fn from(error: core::str::Utf8Error) -> Error {
error!("{}",error);
error!("{}", error);
Error::InvalidValue
}
}
Expand Down
Loading

0 comments on commit c9fa2f4

Please sign in to comment.