Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 75 additions & 18 deletions crates/sandlock-core/src/vdso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,17 @@ fn read_proc_mem(pid: i32, addr: u64, len: usize) -> io::Result<Vec<u8>> {
Ok(buf)
}

/// Parse vDSO ELF bytes and return a map of symbol name -> offset from ELF base.
fn parse_vdso_symbols(vdso_bytes: &[u8]) -> HashMap<String, u64> {
/// Parse vDSO ELF bytes and return a map of symbol name -> (offset from ELF
/// base, size in bytes).
fn parse_vdso_symbols(vdso_bytes: &[u8]) -> HashMap<String, (u64, u64)> {
let mut symbols = HashMap::new();

if let Ok(elf) = goblin::elf::Elf::parse(vdso_bytes) {
for sym in elf.dynsyms.iter() {
if sym.st_value != 0 {
if let Some(name) = elf.dynstrtab.get_at(sym.st_name) {
if !name.is_empty() {
symbols.insert(name.to_string(), sym.st_value);
symbols.insert(name.to_string(), (sym.st_value, sym.st_size));
}
}
}
Expand Down Expand Up @@ -94,13 +95,26 @@ fn arm64_b_insn(from: u64, to: u64) -> Result<u32, SandlockError> {
Ok(0x14000000u32 | ((offset as u32) & 0x03FF_FFFF))
}

/// Encode an x86-64 `jmp rel32` located at `from`, reaching ±2 GiB.
#[cfg(target_arch = "x86_64")]
fn x86_jmp_insn(from: u64, to: u64) -> Result<[u8; 5], SandlockError> {
let rel = i32::try_from(to as i64 - (from as i64 + 5)).map_err(|_| {
SandlockError::MemoryProtect(format!(
"x86_64 jmp {:#x}->{:#x} out of ±2 GiB range",
from, to
))
})?;
let mut insn = [0xE9, 0, 0, 0, 0];
insn[1..].copy_from_slice(&rel.to_le_bytes());
Ok(insn)
}

/// Offset within the vDSO mapping where the trampoline area starts, rounded up
/// to a 16-byte boundary.
///
/// It must clear the whole ELF image, not just the exported symbols: the vDSO
/// keeps unexported code after them (riscv64 `__vdso_getrandom` calls a local
/// ChaCha routine placed past its own end).
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
fn vdso_tramp_start(vdso_bytes: &[u8]) -> Option<u64> {
let elf = goblin::elf::Elf::parse(vdso_bytes).ok()?;
let load_end = elf
Expand Down Expand Up @@ -525,14 +539,19 @@ pub(crate) fn patch(

// arm64/riscv64: place full stubs in slack space at the tail of the vDSO
// mapping and patch each function entry with a single 4-byte jump to its stub.
// x86_64: stubs are short and inter-symbol gaps are wide; patch inline.
// x86_64: patch inline when the stub fits inside the function. Some kernels
// export entries that are only a 5-byte jmp to shared code, and a longer
// stub there would run into the next function, so it goes to the tail
// behind a 5-byte jmp instead.
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
let mut tramp_offset = vdso_tramp_start(&vdso_bytes).ok_or_else(|| {
SandlockError::MemoryProtect("failed to locate free space in the vDSO".into())
})?;
#[cfg(target_arch = "x86_64")]
let mut tramp_offset = vdso_tramp_start(&vdso_bytes);

for (name, alt_name, syscall_nr) in vdso_targets() {
if let Some(&offset) = symbols.get(name).or_else(|| symbols.get(alt_name)) {
if let Some(&(offset, _size)) = symbols.get(name).or_else(|| symbols.get(alt_name)) {
let entry_addr = base + offset;
let stub = match (time_offset_secs, name) {
(Some(off), "clock_gettime") => offset_stub_clock_gettime(off),
Expand All @@ -542,18 +561,41 @@ pub(crate) fn patch(

#[cfg(target_arch = "x86_64")]
{
mem.seek(SeekFrom::Start(entry_addr)).map_err(|e| {
SandlockError::MemoryProtect(format!(
"failed to seek to {} at {:#x}: {}",
name, entry_addr, e
))
})?;
mem.write_all(&stub).map_err(|e| {
SandlockError::MemoryProtect(format!(
"failed to write {} stub at {:#x}: {}",
name, entry_addr, e
))
})?;
// A size of 0 means the kernel did not record one: patch
// inline as before rather than give up on the symbol.
let writes = if _size == 0 || stub.len() as u64 <= _size {
vec![(entry_addr, stub)]
} else {
let start = tramp_offset.ok_or_else(|| {
SandlockError::MemoryProtect(
"failed to locate free space in the vDSO".into(),
)
})?;
if _size < 5 || start + stub.len() as u64 > mapping_size {
return Err(SandlockError::MemoryProtect(format!(
"no room for the {} stub: entry is {} bytes, trampoline needs {} bytes at offset {:#x}, mapping ends at {:#x}",
name, _size, stub.len(), start, mapping_size
)));
}
let tramp_addr = base + start;
tramp_offset = Some((start + stub.len() as u64 + 15) & !15);
let jmp = x86_jmp_insn(entry_addr, tramp_addr)?;
vec![(tramp_addr, stub), (entry_addr, jmp.to_vec())]
};
for (addr, bytes) in writes {
mem.seek(SeekFrom::Start(addr)).map_err(|e| {
SandlockError::MemoryProtect(format!(
"failed to seek to {} at {:#x}: {}",
name, addr, e
))
})?;
mem.write_all(&bytes).map_err(|e| {
SandlockError::MemoryProtect(format!(
"failed to write {} stub at {:#x}: {}",
name, addr, e
))
})?;
}
}

#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
Expand Down Expand Up @@ -630,6 +672,21 @@ mod tests {
);
}

#[test]
#[cfg(target_arch = "x86_64")]
fn test_x86_jmp_insn() {
// rel32 counts from the end of the 5-byte instruction.
assert_eq!(
x86_jmp_insn(0x1230, 0x1f20).unwrap(),
[0xE9, 0xEB, 0x0C, 0x00, 0x00]
);
assert_eq!(
x86_jmp_insn(0x1f20, 0x1230).unwrap(),
[0xE9, 0x0B, 0xF3, 0xFF, 0xFF]
);
assert!(x86_jmp_insn(0, 1 << 32).is_err());
}

#[test]
#[cfg(target_arch = "x86_64")]
fn test_simple_stub_size() {
Expand Down
50 changes: 50 additions & 0 deletions crates/sandlock-core/tests/integration/test_determinism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,56 @@ async fn test_time_start_basic_commands_work() {
assert!(result.success());
}

/// The time_start stubs are longer than some vDSO entry points: on recent
/// kernels __vdso_gettimeofday and __vdso_clock_gettime are a bare 5-byte jmp
/// with __vdso_time and __vdso_clock_getres right behind them. Patching the
/// stubs in place ran them into those neighbours, so gettimeofday returned
/// the time(2) result and clock_getres returned garbage without filling in
/// the resolution.
#[tokio::test]
async fn test_time_start_leaves_neighbouring_vdso_calls_working() {
let y2k = SystemTime::UNIX_EPOCH + Duration::from_secs(961027200);
let policy = Sandbox::builder()
.fs_read("/usr")
.fs_read("/lib")
.fs_read_if_exists("/lib64")
.fs_read("/bin")
.fs_read("/etc")
.fs_read("/proc")
.time_start(y2k)
.build()
.unwrap();
let script = r#"
import ctypes, time
libc = ctypes.CDLL(None)
class Pair(ctypes.Structure):
_fields_ = [('sec', ctypes.c_long), ('frac', ctypes.c_long)]
tv = Pair()
ret = libc.gettimeofday(ctypes.byref(tv), None)
print('gettimeofday', ret, time.gmtime(tv.sec).tm_year)
res = Pair(-1, -1)
ret = libc.clock_getres(time.CLOCK_MONOTONIC, ctypes.byref(res))
print('clock_getres', ret, res.sec == 0 and res.frac > 0)
print('clock_gettime', time.gmtime(time.time()).tm_year)
"#;
let result = policy
.clone()
.run(&["python3", "-c", script])
.await
.unwrap();
assert!(result.success(), "{}", result.stderr_str().unwrap_or(""));
let stdout = result.stdout_str().unwrap_or("");
let lines: Vec<&str> = stdout.lines().collect();
assert_eq!(
lines,
[
"gettimeofday 0 2000",
"clock_getres 0 True",
"clock_gettime 2000"
]
);
}

/// Test combining random_seed and time_start.
#[tokio::test]
async fn test_combined_determinism() {
Expand Down
Loading