From c64f97e541a712b9c6af4bd3981d573db553cd5b Mon Sep 17 00:00:00 2001 From: saviourmk Date: Fri, 27 Nov 2026 13:09:03 +0900 Subject: [PATCH] vdso: shift time() under time_start on x86_64 --- crates/sandlock-core/src/vdso.rs | 65 ++++++++++++++++++- .../tests/integration/test_determinism.rs | 33 ++++++++++ 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/crates/sandlock-core/src/vdso.rs b/crates/sandlock-core/src/vdso.rs index 0dc9e172a..b4ea7bf7b 100644 --- a/crates/sandlock-core/src/vdso.rs +++ b/crates/sandlock-core/src/vdso.rs @@ -232,6 +232,33 @@ fn offset_stub_gettimeofday(offset_secs: i64) -> Vec { stub } +/// Generate an offset stub for time(tloc) that forces a real syscall, then adds +/// the time offset to the returned seconds and, when `tloc` is non-NULL, stores +/// them over the unshifted value the kernel wrote there. The syscall preserves +/// rdi, so `tloc` is still in place afterwards. +/// +/// Layout (x86-64), 29 bytes: +/// mov eax, 201 / syscall ; real time(tloc) +/// movabs rcx, offset_secs +/// add rax, rcx ; shifted seconds +/// test rdi, rdi / je +3 ; tloc == NULL -> ret +/// mov [rdi], rax +/// ret +#[cfg(target_arch = "x86_64")] +fn offset_stub_time(offset_secs: i64) -> Vec { + let mut stub = Vec::new(); + stub.extend_from_slice(&[0xB8, 0xC9, 0x00, 0x00, 0x00]); // mov eax, 201 + stub.extend_from_slice(&[0x0F, 0x05]); // syscall + stub.extend_from_slice(&[0x48, 0xB9]); // movabs rcx, imm64 + stub.extend_from_slice(&offset_secs.to_le_bytes()); + stub.extend_from_slice(&[0x48, 0x01, 0xC8]); // add rax, rcx + stub.extend_from_slice(&[0x48, 0x85, 0xFF]); // test rdi, rdi (tloc == NULL?) + stub.extend_from_slice(&[0x74, 0x03]); // je +3 -> ret (skip the store) + stub.extend_from_slice(&[0x48, 0x89, 0x07]); // mov [rdi], rax + stub.push(0xC3); // ret + stub +} + #[cfg(target_arch = "aarch64")] fn offset_stub_gettimeofday(offset_secs: i64) -> Vec { let mut stub = Vec::new(); @@ -500,8 +527,8 @@ fn vdso_targets() -> Vec<(&'static str, &'static str, u32)> { } /// Patch the vDSO of a target process to force real syscalls (interceptable by seccomp). -/// If `time_offset_secs` is provided, clock_gettime and gettimeofday stubs will add -/// the offset to the returned time. +/// If `time_offset_secs` is provided, the clock_gettime, gettimeofday and (on +/// x86_64) time stubs will add the offset to the returned time. pub(crate) fn patch( pid: i32, time_offset_secs: Option, @@ -537,6 +564,8 @@ pub(crate) fn patch( let stub = match (time_offset_secs, name) { (Some(off), "clock_gettime") => offset_stub_clock_gettime(off), (Some(off), "gettimeofday") => offset_stub_gettimeofday(off), + #[cfg(target_arch = "x86_64")] + (Some(off), "time") => offset_stub_time(off), _ => simple_stub(syscall_nr), }; @@ -828,6 +857,38 @@ mod tests { } } + /// Execute the generated `time` offset stub: the seconds it returns and the + /// seconds it stores through `tloc` are both shifted by the embedded offset, + /// and a NULL `tloc` takes the stub's NULL branch without a store. + #[test] + #[cfg(target_arch = "x86_64")] + fn offset_stub_time_executes_and_shifts_seconds() { + const OFFSET: i64 = -86_400; // one day back + let page = map_executable(&offset_stub_time(OFFSET)); + let stub_fn: extern "C" fn(*mut libc::time_t) -> libc::time_t = + unsafe { std::mem::transmute(page) }; + + let real = unsafe { libc::time(std::ptr::null_mut()) }; + let mut stored: libc::time_t = 0; + let returned = stub_fn(&mut stored); + assert!( + (real - returned - (-OFFSET)).abs() <= 2, + "time() should be shifted by {OFFSET}s, real={real} stub={returned}" + ); + assert_eq!(stored, returned, "tloc receives the shifted seconds"); + assert_eq!(offset_stub_time(OFFSET).len(), 29); + + let returned = stub_fn(std::ptr::null_mut()); + assert!( + (real - returned - (-OFFSET)).abs() <= 2, + "NULL tloc handled without fault, real={real} stub={returned}" + ); + + unsafe { + libc::munmap(page, EXEC_PAGE); + } + } + #[test] #[cfg(target_arch = "x86_64")] fn test_offset_stub_contains_offset() { diff --git a/crates/sandlock-core/tests/integration/test_determinism.rs b/crates/sandlock-core/tests/integration/test_determinism.rs index c0a118e9e..cfb832ac9 100644 --- a/crates/sandlock-core/tests/integration/test_determinism.rs +++ b/crates/sandlock-core/tests/integration/test_determinism.rs @@ -113,6 +113,39 @@ async fn test_time_start_frozen() { ); } +/// time(2) is a vDSO call on x86_64, and libc's time(), Python's argument-less +/// time.gmtime() and many C programs read the clock through it. It has to +/// report the shifted wall clock like clock_gettime and gettimeofday do. +#[tokio::test] +async fn test_time_start_shifts_time_call() { + 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) +libc.time.restype = ctypes.c_long +stored = ctypes.c_long() +returned = libc.time(ctypes.byref(stored)) +print(time.gmtime(returned).tm_year, time.gmtime(stored.value).tm_year, time.gmtime().tm_year) +"#; + let result = policy + .clone() + .run(&["python3", "-c", script]) + .await + .unwrap(); + assert!(result.success(), "{}", result.stderr_str().unwrap_or("")); + assert_eq!(result.stdout_str().unwrap_or("").trim(), "2000 2000 2000"); +} + /// Test that time_start doesn't break basic command execution. #[tokio::test] async fn test_time_start_basic_commands_work() {