From 97fa33f3abb3a8676d897f4f39affbd67ded5d05 Mon Sep 17 00:00:00 2001 From: Ningyuan Li Date: Mon, 21 Aug 2023 13:54:27 +0900 Subject: [PATCH] binary rpath improvement wip --- Cargo.lock | 2 +- common/bin/src/binary.rs | 10 +++++++--- common/bin/src/lib.rs | 2 ++ common/ipk/src/component.rs | 2 ++ common/verify/src/ipk/component.rs | 1 + 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6de2e21..3abbd97 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -523,7 +523,7 @@ dependencies = [ [[package]] name = "ipk-verify" -version = "0.1.0" +version = "0.1.1" dependencies = [ "bin-lib", "clap", diff --git a/common/bin/src/binary.rs b/common/bin/src/binary.rs index 8611737..3ba29ff 100644 --- a/common/bin/src/binary.rs +++ b/common/bin/src/binary.rs @@ -12,7 +12,9 @@ use crate::{BinaryInfo, LibraryInfo}; impl BinaryInfo { pub fn find_library(&self, name: &str) -> Option { for rpath in &self.rpath { - let path = Path::new(rpath).join(name); + let path = + Path::new(&rpath.replace("$ORIGIN", &self.dir.as_ref().unwrap().to_string_lossy())) + .join(name); if let Ok(f) = File::open(&path) { return LibraryInfo::parse(f, false, path.file_name().unwrap().to_string_lossy()) .map_err(|e| { @@ -24,10 +26,11 @@ impl BinaryInfo { return None; } - pub fn parse(source: S, name: N) -> Result + pub fn parse(source: S, name: N, dir: Option) -> Result where S: std::io::Read + std::io::Seek, N: AsRef, + D: AsRef, { let mut rpath = Vec::::new(); let mut needed = Vec::::new(); @@ -89,6 +92,7 @@ impl BinaryInfo { return Ok(Self { name: String::from(name.as_ref()), + dir: dir.map(|d| d.as_ref().to_path_buf()), rpath, needed, undefined, @@ -106,7 +110,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", None).expect("should not have any error"); assert_eq!(info.needed[0], "libc.so.6"); } } diff --git a/common/bin/src/lib.rs b/common/bin/src/lib.rs index c02620e..f0e3847 100644 --- a/common/bin/src/lib.rs +++ b/common/bin/src/lib.rs @@ -1,3 +1,4 @@ +use std::path::PathBuf; use serde::{Deserialize, Serialize}; pub mod binary; @@ -6,6 +7,7 @@ pub mod library; #[derive(Debug, Serialize, Deserialize)] pub struct BinaryInfo { pub name: String, + pub dir: Option, pub rpath: Vec, pub needed: Vec, pub undefined: Vec, diff --git a/common/ipk/src/component.rs b/common/ipk/src/component.rs index dd446d6..9d7d301 100644 --- a/common/ipk/src/component.rs +++ b/common/ipk/src/component.rs @@ -61,6 +61,7 @@ impl Component { ) })?, exe_path.file_name().unwrap().to_string_lossy(), + exe_path.parent() ) .map_err(|e| { Error::new( @@ -96,6 +97,7 @@ impl Component { BinaryInfo::parse( File::open(dir.join(&exe_path))?, exe_path.file_name().unwrap().to_string_lossy(), + exe_path.parent() ) .map_err(|e| { Error::new( diff --git a/common/verify/src/ipk/component.rs b/common/verify/src/ipk/component.rs index fbe551c..bddfa9d 100644 --- a/common/verify/src/ipk/component.rs +++ b/common/verify/src/ipk/component.rs @@ -85,6 +85,7 @@ impl VerifyWithFirmware for Component { let verify_result = self.verify_bin( &BinaryInfo { name: lib.name.clone(), + dir: Default::default(), rpath: Default::default(), needed: lib.needed.clone(), undefined: lib.undefined.clone(),