Skip to content

Commit e1d4633

Browse files
committed
zero out stack, etc
1 parent 083d635 commit e1d4633

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/shims/trace/child.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ pub unsafe fn init_sv() -> Result<(), SvInitError> {
179179
// SAFETY: Calling sysconf(_SC_PAGESIZE) is always safe and cannot error.
180180
let page_size = unsafe { libc::sysconf(libc::_SC_PAGESIZE) }.try_into().unwrap();
181181

182+
// Set up the pagesize used in the memory protection functions.
183+
// SAFETY: sysconf(_SC_PAGESIZE) is always safe and doesn't error
184+
super::parent::PAGE_SIZE.store(page_size, std::sync::atomic::Ordering::Relaxed);
185+
182186
unsafe {
183187
// TODO: Maybe use clone3() instead for better signalling of when the child exits?
184188
// SAFETY: Caller upholds that only one thread exists.
@@ -200,8 +204,7 @@ pub unsafe fn init_sv() -> Result<(), SvInitError> {
200204
match ptrace::seize(child, options) {
201205
// Ptrace works :D
202206
Ok(_) => {
203-
let code = sv_loop(listener, child, event_tx, confirm_tx, page_size)
204-
.unwrap_err();
207+
let code = sv_loop(listener, child, event_tx, confirm_tx).unwrap_err();
205208
// If a return code of 0 is not explicitly given, assume something went
206209
// wrong and return 1.
207210
std::process::exit(code.unwrap_or(1))
@@ -228,12 +231,6 @@ pub unsafe fn init_sv() -> Result<(), SvInitError> {
228231
// SAFETY: prctl PR_SET_PDEATHSIG is always safe to call.
229232
let ret = libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGTERM);
230233
assert_eq!(ret, 0);
231-
// Set up the pagesize used in the memory protection functions.
232-
// SAFETY: sysconf(_SC_PAGESIZE) is always safe and doesn't error
233-
super::parent::PAGE_SIZE.store(
234-
libc::sysconf(libc::_SC_PAGESIZE).try_into().unwrap(),
235-
std::sync::atomic::Ordering::Relaxed,
236-
);
237234
// First make sure the parent succeeded with ptracing us!
238235
signal::raise(signal::SIGSTOP).unwrap();
239236
// If we're the child process, save the supervisor info.

src/shims/trace/parent.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,10 @@ pub fn sv_loop(
238238
init_pid: unistd::Pid,
239239
event_tx: ipc::IpcSender<MemEvents>,
240240
confirm_tx: ipc::IpcSender<Confirmation>,
241-
page_size: usize,
242241
) -> Result<!, Option<i32>> {
242+
let page_size = PAGE_SIZE.load(std::sync::atomic::Ordering::Relaxed);
243+
assert_ne!(page_size, 0);
244+
243245
// Things that we return to the child process.
244246
let mut acc_events = Vec::new();
245247

@@ -289,6 +291,7 @@ pub fn sv_loop(
289291
event_tx.send(MemEvents { acc_events }).unwrap();
290292
// And reset our values.
291293
acc_events = Vec::new();
294+
ch_pages = Vec::new();
292295
ch_stack = None;
293296

294297
// No need to monitor syscalls anymore, they'd just be ignored.
@@ -550,6 +553,12 @@ fn handle_segfault(
550553
// - Parse executed code to estimate size & type of access
551554
// - Reprotect the memory
552555
// - Continue
556+
557+
// Zero out the stack
558+
for a in (ch_stack..ch_stack.strict_add(FAKE_STACK_SIZE)).step_by(ARCH_WORD_SIZE) {
559+
ptrace::write(pid, std::ptr::with_exposed_provenance_mut(a), 0).unwrap();
560+
}
561+
553562
let stack_ptr = ch_stack.strict_add(FAKE_STACK_SIZE / 2);
554563
let regs_bak = ptrace::getregs(pid).unwrap();
555564
let mut new_regs = regs_bak;
@@ -591,6 +600,11 @@ fn handle_segfault(
591600
// Also, don't let it continue with unprotected memory if something errors!
592601
let _ = wait::waitid(wait::Id::Pid(pid), WAIT_FLAGS).map_err(|_| ExecError::Died(None))?;
593602

603+
// Zero it out again to be safe
604+
for a in (ch_stack..ch_stack.strict_add(FAKE_STACK_SIZE)).step_by(ARCH_WORD_SIZE) {
605+
ptrace::write(pid, std::ptr::with_exposed_provenance_mut(a), 0).unwrap();
606+
}
607+
594608
// Save registers and grab the bytes that were executed. This would
595609
// be really nasty if it was a jump or similar but those thankfully
596610
// won't do memory accesses and so can't trigger this!

0 commit comments

Comments
 (0)