Skip to content

Commit e3b3c21

Browse files
committed
rust: validate machine architecture of ELF binaries
This helps ensure we're shipping properly targeted binaries.
1 parent 55a598b commit e3b3c21

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/main.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,25 @@ fn validate_elf(
312312
) -> Result<Vec<String>> {
313313
let mut errors = vec![];
314314

315+
let wanted_cpu_type = match target_triple {
316+
"aarch64-unknown-linux-gnu" => goblin::elf::header::EM_AARCH64,
317+
"armv7-unknown-linux-gnueabi" => goblin::elf::header::EM_ARM,
318+
"armv7-unknown-linux-gnueabihf" => goblin::elf::header::EM_ARM,
319+
"i686-unknown-linux-gnu" => goblin::elf::header::EM_386,
320+
"x86_64-unknown-linux-gnu" => goblin::elf::header::EM_X86_64,
321+
"x86_64-unknown-linux-musl" => goblin::elf::header::EM_X86_64,
322+
_ => panic!("unhandled target triple: {}", target_triple),
323+
};
324+
325+
if elf.header.e_machine != wanted_cpu_type {
326+
errors.push(format!(
327+
"invalid ELF machine type in {}; wanted {}, got {}",
328+
path.display(),
329+
wanted_cpu_type,
330+
elf.header.e_machine
331+
));
332+
}
333+
315334
let mut allowed_libraries = ELF_ALLOWED_LIBRARIES.to_vec();
316335
if let Some(extra) = ELF_ALLOWED_LIBRARIES_BY_TRIPLE.get(target_triple) {
317336
allowed_libraries.extend(extra.iter());

0 commit comments

Comments
 (0)