Skip to content

Commit a25a94d

Browse files
committed
intercept-preload is safe to build on non linux
1 parent 0284d98 commit a25a94d

File tree

3 files changed

+62
-49
lines changed

3 files changed

+62
-49
lines changed

intercept-preload/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ edition.workspace = true
1313
[lib]
1414
name = "exec"
1515
path = "src/lib.rs"
16-
crate-type = ["cdylib", "rlib"]
16+
crate-type = ["rlib", "cdylib"]
1717

1818
[dependencies]
1919
bear = { path = "../bear" }
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// SPDX-License-Identifier: GPL-3.0-or-later
2+
3+
use std::ffi::CStr;
4+
use std::os::raw::{c_char, c_int};
5+
6+
/// Intercepted open function
7+
///
8+
/// # Safety
9+
///
10+
/// This is an FFI function intended for LD_PRELOAD interception.
11+
#[no_mangle]
12+
pub unsafe extern "C" fn open(path: *const c_char, flags: c_int, mode: libc::mode_t) -> c_int {
13+
// Log the file being opened if logging is initialized
14+
if !path.is_null() {
15+
if let Ok(path_str) = CStr::from_ptr(path).to_str() {
16+
log::debug!("libear: open called for path: {}", path_str);
17+
18+
// Here we could implement additional logic for interception
19+
// For example, recording file access for compilation database
20+
}
21+
}
22+
23+
// Call the real open function
24+
libc::open(path, flags, mode)
25+
}
26+
27+
/// Intercepted execve function
28+
///
29+
/// # Safety
30+
///
31+
/// This is an FFI function intended for LD_PRELOAD interception.
32+
#[no_mangle]
33+
pub unsafe extern "C" fn execve(
34+
path: *const c_char,
35+
argv: *const *const c_char,
36+
envp: *const *const c_char,
37+
) -> c_int {
38+
// Log the process being executed
39+
if !path.is_null() {
40+
if let Ok(path_str) = CStr::from_ptr(path).to_str() {
41+
log::info!("libear: intercepted execution of: {}", path_str);
42+
43+
// Here we could implement command interception logic
44+
// For example, recording compiler invocations
45+
}
46+
}
47+
48+
// Call the real execve function
49+
libc::execve(path, argv, envp)
50+
}

intercept-preload/src/lib.rs

Lines changed: 11 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// SPDX-License-Identifier: GPL-3.0-or-later
2+
13
//! Library for Bear interception via LD_PRELOAD
24
//!
35
//! This library provides system capability checks at build time.
@@ -19,62 +21,23 @@
1921
//! fn fallback_exec() { /* fallback implementation */ }
2022
//! ```
2123
22-
use std::ffi::CStr;
23-
use std::os::raw::{c_char, c_int};
24+
// Only include Linux implementation when building for Linux
25+
#[cfg(target_os = "linux")]
26+
mod implementation;
27+
28+
// Re-export Linux implementations when on Linux
29+
#[cfg(target_os = "linux")]
30+
pub use implementation::*;
2431

2532
/// Version information for the library
2633
#[no_mangle]
2734
pub static LIBEAR_VERSION: &[u8; 6] = b"4.0.0\0";
2835

29-
/// Intercepted open function
30-
///
31-
/// # Safety
32-
///
33-
/// This is an FFI function intended for LD_PRELOAD interception.
34-
#[no_mangle]
35-
pub unsafe extern "C" fn open(path: *const c_char, flags: c_int, mode: libc::mode_t) -> c_int {
36-
// Log the file being opened if logging is initialized
37-
if !path.is_null() {
38-
if let Ok(path_str) = CStr::from_ptr(path).to_str() {
39-
log::debug!("libear: open called for path: {}", path_str);
40-
41-
// Here we could implement additional logic for interception
42-
// For example, recording file access for compilation database
43-
}
44-
}
45-
46-
// Call the real open function
47-
libc::open(path, flags, mode)
48-
}
49-
50-
/// Intercepted execve function
51-
///
52-
/// # Safety
53-
///
54-
/// This is an FFI function intended for LD_PRELOAD interception.
55-
#[no_mangle]
56-
pub unsafe extern "C" fn execve(
57-
path: *const c_char,
58-
argv: *const *const c_char,
59-
envp: *const *const c_char,
60-
) -> c_int {
61-
// Log the process being executed
62-
if !path.is_null() {
63-
if let Ok(path_str) = CStr::from_ptr(path).to_str() {
64-
log::info!("libear: intercepted execution of: {}", path_str);
65-
66-
// Here we could implement command interception logic
67-
// For example, recording compiler invocations
68-
}
69-
}
70-
71-
// Call the real execve function
72-
libc::execve(path, argv, envp)
73-
}
74-
7536
#[cfg(test)]
7637
mod tests {
7738
use super::*;
39+
use std::ffi::CStr;
40+
use std::os::raw::c_char;
7841

7942
#[test]
8043
fn test_version() {

0 commit comments

Comments
 (0)