add overflow check for vaddr
This commit is contained in:
parent
a1ece95459
commit
767804233f
|
@ -12,6 +12,7 @@ pub enum Error {
|
|||
BadMagic,
|
||||
BadType,
|
||||
BadMachine,
|
||||
BadAddr,
|
||||
}
|
||||
|
||||
struct Phdr {
|
||||
|
@ -101,7 +102,8 @@ pub fn load(elf: Vec<u8>, mem: &mut [u8]) -> Result<u32, Error> {
|
|||
continue;
|
||||
}
|
||||
|
||||
let vaddr = (phdr.vaddr - RAM_BASE) as usize;
|
||||
if let Some(vaddr) = phdr.vaddr.checked_sub(RAM_BASE) {
|
||||
let vaddr = vaddr as usize;
|
||||
let memsz = phdr.memsz as usize;
|
||||
|
||||
let pos = rdr.position();
|
||||
|
@ -110,6 +112,9 @@ pub fn load(elf: Vec<u8>, mem: &mut [u8]) -> Result<u32, Error> {
|
|||
return Err(Error::Eof);
|
||||
}
|
||||
rdr.set_position(pos);
|
||||
} else {
|
||||
return Err(Error::BadAddr);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(entry)
|
||||
|
|
|
@ -336,6 +336,7 @@ pub fn run(path: String) -> Result<()> {
|
|||
elf::Error::BadMagic => bail!("bad magic"),
|
||||
elf::Error::BadType => bail!("invalid executable"),
|
||||
elf::Error::BadMachine => bail!("foreign elf architecture"),
|
||||
elf::Error::BadAddr => bail!("bad virt address offset"),
|
||||
},
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue