Initial commit

This commit is contained in:
2024-04-18 15:41:29 +02:00
commit 35a2e4d002
6 changed files with 162 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

31
Cargo.lock generated Normal file
View File

@@ -0,0 +1,31 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "agent_lib"
version = "0.1.0"
dependencies = [
"anyhow",
"libc",
"shared_memory_heap",
]
[[package]]
name = "anyhow"
version = "1.0.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519"
[[package]]
name = "libc"
version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]]
name = "shared_memory_heap"
version = "0.1.0"
dependencies = [
"libc",
]

11
Cargo.toml Normal file
View File

@@ -0,0 +1,11 @@
[package]
name = "agent_lib"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.82"
libc = "0.2.153"
shared_memory_heap = { path = "../shared_memory_heap" }

2
rust-toolchain.toml Normal file
View File

@@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"

92
src/agent.rs Normal file
View File

@@ -0,0 +1,92 @@
use anyhow::{bail, Result};
use libc::{
c_char, c_void, execve, fork, ftruncate, memfd_create, mmap, syscall, SYS_futex, FUTEX_WAIT,
FUTEX_WAKE, MAP_SHARED, PROT_READ,
};
use shared_memory_heap::get_shared_mem_fd;
use std::{ffi::CString, path::Path, ptr, usize};
pub struct Agent {
sync_mem: *mut usize,
}
unsafe impl Send for Agent {}
impl Agent {
pub(crate) unsafe fn new(path: &Path) -> Result<Self> {
let data_fd = get_shared_mem_fd();
let sync_fd = memfd_create("sync\x00".as_ptr() as *const c_char, 0);
let err = ftruncate(sync_fd, 1024);
if err != 0 {
bail!("ftruncate failed");
}
let sync_mem = mmap(
ptr::null_mut::<c_void>(),
1024,
PROT_READ | PROT_READ,
MAP_SHARED,
sync_fd,
0,
) as *mut usize;
if sync_mem == ptr::null_mut() {
bail!("mmap failed");
}
let child = fork();
if child == 0 {
// child
let data_fd = CString::new(data_fd.to_string()).unwrap();
let sync_fd = CString::new(sync_fd.to_string()).unwrap();
let args = [data_fd.as_ptr(), sync_fd.as_ptr()];
execve(
path.as_os_str().as_encoded_bytes().as_ptr() as *const c_char,
args.as_ptr(),
ptr::null(),
);
panic!("execve failed");
}
// parent
Ok(Agent {
sync_mem,
})
}
pub(crate) unsafe fn perform_ipc_call(&mut self, call_id: usize, ptrs: &[usize]) {
*self.sync_mem.add(1) = call_id;
for (i, ptr) in ptrs.iter().enumerate() {
*self.sync_mem.add(i + 1) = *ptr;
}
// wake agent
syscall(
SYS_futex,
self.sync_mem,
FUTEX_WAKE,
1,
ptr::null::<u8>(),
ptr::null::<u8>(),
0,
);
// wait for agent to be finished
syscall(
SYS_futex,
self.sync_mem,
FUTEX_WAIT,
0,
ptr::null::<u8>(),
ptr::null::<u8>(),
0,
);
}
}

25
src/lib.rs Normal file
View File

@@ -0,0 +1,25 @@
#![feature(lazy_cell)]
mod agent;
pub use shared_memory_heap::sharedptr::SharedPtr;
use agent::Agent;
use std::{
path::Path,
sync::{LazyLock, Mutex},
};
static AGENT: LazyLock<Mutex<Agent>> = LazyLock::new(|| {
let agent_path = std::env::var("AGENT_PATH").expect("AGENT_PATH environment variable missing");
let agent = unsafe { Agent::new(Path::new(&agent_path)).expect("Agent failed to start") };
Mutex::new(agent)
});
pub fn x25519(out: &mut SharedPtr, pk: &SharedPtr, sk: &SharedPtr) {
let mut agent = AGENT.lock().unwrap();
unsafe {
agent.perform_ipc_call(1, &[out.get_offset(), pk.get_offset(), sk.get_offset()]);
}
}