feat: add functions for x25519 agent

This commit is contained in:
2024-04-22 15:06:03 +02:00
parent 35a2e4d002
commit b5b9e0319b
4 changed files with 40 additions and 19 deletions

1
Cargo.lock generated
View File

@@ -26,6 +26,7 @@ checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]] [[package]]
name = "shared_memory_heap" name = "shared_memory_heap"
version = "0.1.0" version = "0.1.0"
source = "git+https://gitea.rixxc.de/rixxc/shared_memory_heap.git#e649e2b59522f6a0ca6f96968d429fa79a1a30da"
dependencies = [ dependencies = [
"libc", "libc",
] ]

View File

@@ -8,4 +8,4 @@ edition = "2021"
[dependencies] [dependencies]
anyhow = "1.0.82" anyhow = "1.0.82"
libc = "0.2.153" libc = "0.2.153"
shared_memory_heap = { path = "../shared_memory_heap" } shared_memory_heap = { git = "https://gitea.rixxc.de/rixxc/shared_memory_heap.git" }

View File

@@ -1,7 +1,7 @@
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use libc::{ use libc::{
c_char, c_void, execve, fork, ftruncate, memfd_create, mmap, syscall, SYS_futex, FUTEX_WAIT, c_char, c_void, execve, fork, ftruncate, memfd_create, mmap, perror, syscall, SYS_futex,
FUTEX_WAKE, MAP_SHARED, PROT_READ, FUTEX_WAIT, FUTEX_WAKE, MAP_FAILED, MAP_SHARED, PROT_READ, PROT_WRITE,
}; };
use shared_memory_heap::get_shared_mem_fd; use shared_memory_heap::get_shared_mem_fd;
use std::{ffi::CString, path::Path, ptr, usize}; use std::{ffi::CString, path::Path, ptr, usize};
@@ -17,6 +17,10 @@ impl Agent {
let data_fd = get_shared_mem_fd(); let data_fd = get_shared_mem_fd();
let sync_fd = memfd_create("sync\x00".as_ptr() as *const c_char, 0); let sync_fd = memfd_create("sync\x00".as_ptr() as *const c_char, 0);
if sync_fd <= 0 {
bail!("creating memfd failed");
}
let err = ftruncate(sync_fd, 1024); let err = ftruncate(sync_fd, 1024);
if err != 0 { if err != 0 {
bail!("ftruncate failed"); bail!("ftruncate failed");
@@ -25,16 +29,18 @@ impl Agent {
let sync_mem = mmap( let sync_mem = mmap(
ptr::null_mut::<c_void>(), ptr::null_mut::<c_void>(),
1024, 1024,
PROT_READ | PROT_READ, PROT_READ | PROT_WRITE,
MAP_SHARED, MAP_SHARED,
sync_fd, sync_fd,
0, 0,
) as *mut usize; ) as *mut usize;
if sync_mem == ptr::null_mut() { if sync_mem == MAP_FAILED as *mut usize {
bail!("mmap failed"); bail!("mmap failed");
} }
*sync_mem = 0;
let child = fork(); let child = fork();
if child == 0 { if child == 0 {
@@ -42,7 +48,7 @@ impl Agent {
let data_fd = CString::new(data_fd.to_string()).unwrap(); let data_fd = CString::new(data_fd.to_string()).unwrap();
let sync_fd = CString::new(sync_fd.to_string()).unwrap(); let sync_fd = CString::new(sync_fd.to_string()).unwrap();
let args = [data_fd.as_ptr(), sync_fd.as_ptr()]; let args = [data_fd.as_ptr(), sync_fd.as_ptr(), ptr::null()];
execve( execve(
path.as_os_str().as_encoded_bytes().as_ptr() as *const c_char, path.as_os_str().as_encoded_bytes().as_ptr() as *const c_char,
@@ -50,35 +56,40 @@ impl Agent {
ptr::null(), ptr::null(),
); );
perror("execve:\x00".as_ptr() as *const c_char);
panic!("execve failed"); panic!("execve failed");
} }
// parent // parent
Ok(Agent { Ok(Agent { sync_mem })
sync_mem,
})
} }
pub(crate) unsafe fn perform_ipc_call(&mut self, call_id: usize, ptrs: &[usize]) { pub(crate) unsafe fn perform_ipc_call(&mut self, call_id: usize, ptrs: &[usize]) {
*self.sync_mem.add(1) = call_id; *self.sync_mem.add(1) = call_id;
for (i, ptr) in ptrs.iter().enumerate() { for (i, ptr) in ptrs.iter().enumerate() {
*self.sync_mem.add(i + 1) = *ptr; *self.sync_mem.add(i + 2) = *ptr;
} }
// wake agent // wake agent
syscall( println!("Client: waking agent");
SYS_futex, let mut woken_up = 0;
self.sync_mem, while woken_up == 0 {
FUTEX_WAKE, woken_up = syscall(
1, SYS_futex,
ptr::null::<u8>(), self.sync_mem,
ptr::null::<u8>(), FUTEX_WAKE,
0, 1,
); ptr::null::<u8>(),
ptr::null::<u8>(),
0,
);
}
// wait for agent to be finished // wait for agent to be finished
println!("Client: sleeping...");
syscall( syscall(
SYS_futex, SYS_futex,
self.sync_mem, self.sync_mem,
@@ -88,5 +99,6 @@ impl Agent {
ptr::null::<u8>(), ptr::null::<u8>(),
0, 0,
); );
println!("Client: agent finished work");
} }
} }

View File

@@ -16,6 +16,14 @@ static AGENT: LazyLock<Mutex<Agent>> = LazyLock::new(|| {
Mutex::new(agent) Mutex::new(agent)
}); });
pub fn x25519_keygen(keyid: &mut SharedPtr, pk: &mut SharedPtr) {
let mut agent = AGENT.lock().unwrap();
unsafe {
agent.perform_ipc_call(0, &[keyid.get_offset(), pk.get_offset()]);
}
}
pub fn x25519(out: &mut SharedPtr, pk: &SharedPtr, sk: &SharedPtr) { pub fn x25519(out: &mut SharedPtr, pk: &SharedPtr, sk: &SharedPtr) {
let mut agent = AGENT.lock().unwrap(); let mut agent = AGENT.lock().unwrap();