Skip to content

Commit 53ab50f

Browse files
committed
ensure static executable using fspy_shared_unix::is_dynamically_linked_to_lib
1 parent a502611 commit 53ab50f

4 files changed

Lines changed: 16 additions & 15 deletions

File tree

crates/fspy/tests/static_executable.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,32 @@ use std::{
44
fs::{self, Permissions},
55
os::unix::fs::PermissionsExt as _,
66
path::{Path, PathBuf},
7-
str::from_utf8,
87
sync::LazyLock,
98
};
109

1110
use fspy::PathAccessIterable;
11+
use fspy_shared_unix::is_dynamically_linked_to_libc;
1212

1313
use crate::test_utils::assert_contains;
1414

1515
mod test_utils;
1616

17-
const PRELOAD_CDYLIB_BINARY: &[u8] = include_bytes!(env!("CARGO_BIN_FILE_FSPY_TEST_BIN"));
17+
const TEST_BIN_CONTENT: &[u8] = include_bytes!(env!("CARGO_BIN_FILE_FSPY_TEST_BIN"));
1818

1919
fn test_bin_path() -> &'static Path {
2020
static TEST_BIN_PATH: LazyLock<PathBuf> = LazyLock::new(|| {
21+
assert_eq!(
22+
is_dynamically_linked_to_libc(&TEST_BIN_CONTENT),
23+
Ok(false),
24+
"Test binary is not a static executable"
25+
);
26+
2127
let tmp_dir = env!("CARGO_TARGET_TMPDIR");
2228
let test_bin_path = PathBuf::from(tmp_dir).join("fspy-test-bin");
23-
fs::write(&test_bin_path, PRELOAD_CDYLIB_BINARY).expect("failed to write test binary");
29+
fs::write(&test_bin_path, TEST_BIN_CONTENT).expect("failed to write test binary");
2430
fs::set_permissions(&test_bin_path, Permissions::from_mode(0o755))
2531
.expect("failed to set permissions on test binary");
2632

27-
// Verify that the test binary is indeed a static executable
28-
let output = std::process::Command::new("ldd").arg(&test_bin_path).output().unwrap();
29-
assert_eq!(
30-
output.status.code(),
31-
Some(1),
32-
"ldd should fail on static executables. Stdout: {}. Stderr: {}",
33-
from_utf8(&output.stdout).unwrap(),
34-
from_utf8(&output.stderr).unwrap()
35-
);
36-
3733
test_bin_path
3834
});
3935
TEST_BIN_PATH.as_path()
File renamed without changes.

crates/fspy_shared_unix/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ pub mod exec;
44
pub(crate) mod open_exec;
55
pub mod payload;
66
pub mod spawn;
7+
8+
#[cfg(target_os = "linux")]
9+
mod elf;
10+
11+
#[cfg(target_os = "linux")] // exposed for verifying static executables in fspy tests
12+
pub use elf::is_dynamically_linked_to_libc;

crates/fspy_shared_unix/src/spawn/linux/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
mod elf;
2-
31
use std::{ffi::OsStr, os::unix::ffi::OsStrExt as _, path::Path};
42

53
use fspy_seccomp_unotify::{payload::SeccompPayload, target::install_target};
64
use memmap2::Mmap;
75

86
use crate::{
7+
elf,
98
exec::{Exec, ensure_env},
109
open_exec::open_executable,
1110
payload::{EncodedPayload, PAYLOAD_ENV_NAME},

0 commit comments

Comments
 (0)