Skip to content

Commit

Permalink
updated output for elf-verify
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotaku committed Sep 26, 2023
1 parent 178aad7 commit a3db8b2
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 18 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 13 additions & 9 deletions common/bin/src/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use elf::{abi, ElfStream};
use crate::BinaryInfo;

impl BinaryInfo {
pub fn parse<S, N>(source: S, name: N) -> Result<Self, elf::ParseError>
pub fn parse<S, N>(source: S, name: N, with_rpath: bool) -> Result<Self, elf::ParseError>
where
S: std::io::Read + std::io::Seek,
N: AsRef<str>,
Expand All @@ -29,13 +29,17 @@ impl BinaryInfo {
dynstr_table.get(entry.d_val() as usize).unwrap(),
));
}
abi::DT_RPATH | abi::DT_RUNPATH => rpath.extend(
dynstr_table
.get(entry.d_val() as usize)
.unwrap()
.split(":")
.map(|s| String::from(s)),
),
abi::DT_RPATH | abi::DT_RUNPATH => {
if with_rpath {
rpath.extend(
dynstr_table
.get(entry.d_val() as usize)
.unwrap()
.split(":")
.map(|s| String::from(s)),
)
}
}
_ => {}
}
}
Expand Down Expand Up @@ -89,7 +93,7 @@ mod tests {
fn test_parse() {
let mut content = Cursor::new(include_bytes!("fixtures/sample.bin"));
let info =
BinaryInfo::parse(&mut content, "sample.bin").expect("should not have any error");
BinaryInfo::parse(&mut content, "sample.bin", true).expect("should not have any error");
assert_eq!(info.needed[0], "libc.so.6");
}
}
2 changes: 2 additions & 0 deletions common/ipk/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl Component<AppInfo> {
)
})?,
exe_path.file_name().unwrap().to_string_lossy(),
true,
)
.map_err(|e| {
Error::new(
Expand Down Expand Up @@ -94,6 +95,7 @@ impl Component<ServiceInfo> {
let bin_info = BinaryInfo::parse(
File::open(dir.join(&exe_path))?,
exe_path.file_name().unwrap().to_string_lossy(),
true,
)
.map_err(|e| {
Error::new(
Expand Down
1 change: 1 addition & 0 deletions packages/elf-verify/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ license = "Apache-2.0"
[dependencies]
serde = "1.0.183"
serde_json = "1.0.96"
semver = "1.0.18"

[dependencies.clap]
version = "4.3.23"
Expand Down
51 changes: 42 additions & 9 deletions packages/elf-verify/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::fs::File;
use std::path::PathBuf;

use bin_lib::BinaryInfo;
use clap::Parser;
use semver::VersionReq;

use bin_lib::BinaryInfo;
use fw_lib::Firmware;
use verify_lib::Verify;

Expand All @@ -12,26 +14,57 @@ struct Args {
executables: Vec<PathBuf>,
#[arg(short, long, num_args(1..))]
lib_paths: Vec<String>,
#[arg(short = 'R', long, default_value = "false")]
skip_rpath: bool,
#[arg(short = 'r', long)]
fw_releases: Option<VersionReq>,
#[arg(short, long, action = clap::ArgAction::Count)]
debug: u8,
}

fn main() {
let args = Args::parse();
let firmwares: Vec<Firmware> = Firmware::list(Firmware::data_path())
.unwrap()
.into_iter()
.filter(|fw| {
if let Some(fw_releases) = &args.fw_releases {
return fw_releases.matches(&fw.info.release);
}
return true;
})
.collect();
if firmwares.is_empty() {
eprintln!("No firmware found");
return;
}
for executable in args.executables {
let Ok(file) = File::open(&executable) else {
eprintln!("Failed to open file {}", executable.to_string_lossy());
continue;
};
let mut info = BinaryInfo::parse(file, executable.file_name().unwrap().to_string_lossy())
.expect("parse error");
let mut info = BinaryInfo::parse(
file,
executable.file_name().unwrap().to_string_lossy(),
!args.skip_rpath,
)
.expect("parse error");
info.rpath.extend(args.lib_paths.clone());
for firmware in Firmware::list(Firmware::data_path()).unwrap() {
println!(
"Verify result for firmware {} {:?}",
firmware.info,
info.verify(&|name| firmware.find_library(name))
);
let mut all_ok = true;
for firmware in &firmwares {
let result = info.verify(&|name| firmware.find_library(name));
println!("Verify result for firmware {}:", firmware.info);
for lib in result.missing_lib {
println!("Missing library: {}", lib);
all_ok = false;
}
for sym in result.undefined_sym {
println!("Missing symbol: {}", sym);
all_ok = false;
}
}
if all_ok {
println!("All OK.");
}
}
}

0 comments on commit a3db8b2

Please sign in to comment.