fix: 修复elf加载器在读取解释器路径时的越界问题 (#1124)

Signed-off-by: longjin <longjin@DragonOS.org>
This commit is contained in:
LoGin 2025-03-29 23:46:55 +08:00 committed by GitHub
parent b6db20c072
commit 55833537f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -558,14 +558,26 @@ impl BinaryLoader for ElfLoader {
if seg.p_filesz > 4096 || seg.p_filesz < 2 { if seg.p_filesz > 4096 || seg.p_filesz < 2 {
return Err(ExecError::NotExecutable); return Err(ExecError::NotExecutable);
} }
let mut buffer = vec![0; seg.p_filesz.try_into().unwrap()];
let interpreter_ptr = unsafe { let r = param
core::slice::from_raw_parts( .file_mut()
seg.p_offset as *const u8, .pread(
seg.p_offset.try_into().unwrap(),
seg.p_filesz.try_into().unwrap(), seg.p_filesz.try_into().unwrap(),
buffer.as_mut_slice(),
) )
}; .map_err(|e| {
let _interpreter_path = core::str::from_utf8(interpreter_ptr).map_err(|e| { log::error!("Failed to load interpreter :{:?}", e);
return ExecError::NotSupported;
})?;
if r != seg.p_filesz.try_into().unwrap() {
log::error!("Failed to load interpreter ");
return Err(ExecError::NotSupported);
}
let _interpreter_path = core::str::from_utf8(
&buffer[0..TryInto::<usize>::try_into(seg.p_filesz).unwrap() - 1], //
)
.map_err(|e| {
ExecError::Other(format!( ExecError::Other(format!(
"Failed to parse the path of dynamic linker with error {}", "Failed to parse the path of dynamic linker with error {}",
e e