| 1 |
//! All of these tests are specific to ptrace |
| 2 |
#![cfg(any(target_os = "linux", target_os = "android"))] |
| 3 |
|
| 4 |
use { |
| 5 |
error_graph::ErrorList, |
| 6 |
minidump_writer::ptrace_dumper::PtraceDumper, |
| 7 |
nix::{ |
| 8 |
sys::mman::{mmap, MapFlags, ProtFlags}, |
| 9 |
sys::signal::Signal, |
| 10 |
}, |
| 11 |
std::{ |
| 12 |
convert::TryInto, |
| 13 |
io::{BufRead, BufReader}, |
| 14 |
mem::size_of, |
| 15 |
os::unix::process::ExitStatusExt, |
| 16 |
}, |
| 17 |
}; |
| 18 |
|
| 19 |
mod common; |
| 20 |
use common::*; |
| 21 |
|
| 22 |
/// These tests generally aren't consistent in resource-deprived environments like CI runners and |
| 23 |
/// android emulators. |
| 24 |
macro_rules! disabled_on_ci_and_android { |
| 25 |
() => { |
| 26 |
if std::env::var("CI").is_ok() || cfg!(target_os = "android") { |
| 27 |
println!("disabled on CI and android, but otherwise works locally"); |
| 28 |
return; |
| 29 |
} |
| 30 |
}; |
| 31 |
} |
| 32 |
|
| 33 |
macro_rules! assert_no_soft_errors(($n: ident, $e: expr) => {{ |
| 34 |
let mut $n = ErrorList::default(); |
| 35 |
let __result = $e; |
| 36 |
assert!($n.is_empty(), "{:?}", $n); |
| 37 |
__result |
| 38 |
}}); |
| 39 |
|
| 40 |
#[test] |
| 41 |
fn test_setup() { |
| 42 |
spawn_child("setup", &[]); |
| 43 |
} |
| 44 |
|
| 45 |
#[test] |
| 46 |
fn test_thread_list_from_child() { |
| 47 |
// Child spawns and looks in the parent (== this process) for its own thread-ID |
| 48 |
|
| 49 |
let (tx, rx) = std::sync::mpsc::sync_channel(1); |
| 50 |
|
| 51 |
// // We also spawn another thread that we send a SIGHUP to to ensure that the |
| 52 |
// // ptracedumper correctly handles it |
| 53 |
let _thread = std::thread::Builder::new() |
| 54 |
.name("sighup-thread".into()) |
| 55 |
.spawn(move || { |
| 56 |
tx.send(unsafe { libc::pthread_self() as usize }).unwrap(); |
| 57 |
loop { |
| 58 |
std::thread::sleep(std::time::Duration::from_secs(1)); |
| 59 |
} |
| 60 |
}) |
| 61 |
.unwrap(); |
| 62 |
|
| 63 |
let thread_id = rx.recv().unwrap(); |
| 64 |
|
| 65 |
// Unfortunately we need to set a signal handler to ignore the SIGHUP we send |
| 66 |
// to the thread, as otherwise the default test harness fails |
| 67 |
unsafe { |
| 68 |
let mut act: libc::sigaction = std::mem::zeroed(); |
| 69 |
if libc::sigemptyset(&mut act.sa_mask) != 0 { |
| 70 |
eprintln!( |
| 71 |
"unable to clear action mask: {:?}", |
| 72 |
std::io::Error::last_os_error() |
| 73 |
); |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
unsafe extern "C" fn on_sig( |
| 78 |
_sig: libc::c_int, |
| 79 |
_info: *mut libc::siginfo_t, |
| 80 |
_uc: *mut libc::c_void, |
| 81 |
) { |
| 82 |
} |
| 83 |
|
| 84 |
act.sa_flags = libc::SA_SIGINFO; |
| 85 |
act.sa_sigaction = on_sig as usize; |
| 86 |
|
| 87 |
// Register the action with the signal handler |
| 88 |
if libc::sigaction(libc::SIGHUP, &act, std::ptr::null_mut()) != 0 { |
| 89 |
eprintln!( |
| 90 |
"unable to register signal handler: {:?}", |
| 91 |
std::io::Error::last_os_error() |
| 92 |
); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
unsafe { |
| 97 |
libc::pthread_kill(thread_id as _, libc::SIGHUP); |
| 98 |
} |
| 99 |
|
| 100 |
spawn_child("thread_list", &[]); |
| 101 |
} |
| 102 |
|
| 103 |
#[test] |
| 104 |
fn test_thread_list_from_parent() { |
| 105 |
let num_of_threads = 5; |
| 106 |
let mut child = start_child_and_wait_for_threads(num_of_threads); |
| 107 |
let pid = child.id() as i32; |
| 108 |
|
| 109 |
let mut dumper = assert_no_soft_errors!( |
| 110 |
soft_errors, |
| 111 |
PtraceDumper::new_report_soft_errors( |
| 112 |
pid, |
| 113 |
minidump_writer::minidump_writer::STOP_TIMEOUT, |
| 114 |
Default::default(), |
| 115 |
&mut soft_errors, |
| 116 |
) |
| 117 |
) |
| 118 |
.expect("Couldn't init dumper"); |
| 119 |
|
| 120 |
assert_eq!(dumper.threads.len(), num_of_threads); |
| 121 |
|
| 122 |
assert_no_soft_errors!(soft_errors, dumper.suspend_threads(&mut soft_errors)); |
| 123 |
|
| 124 |
// let mut matching_threads = 0; |
| 125 |
for (idx, curr_thread) in dumper.threads.iter().enumerate() { |
| 126 |
println!("curr_thread: {:?}", curr_thread); |
| 127 |
let info = dumper |
| 128 |
.get_thread_info_by_index(idx) |
| 129 |
.expect("Could not get thread info by index"); |
| 130 |
let (_valid_stack_ptr, stack_len) = dumper |
| 131 |
.get_stack_info(info.stack_pointer) |
| 132 |
.expect("Could not get stack_pointer"); |
| 133 |
|
| 134 |
assert!(stack_len > 0); |
| 135 |
|
| 136 |
// TODO: I currently know of no way to write the thread_id into the registers using Rust, |
| 137 |
// so this check is deactivated for now, because it always fails |
| 138 |
/* |
| 139 |
// In the helper program, we stored a pointer to the thread id in a |
| 140 |
// specific register. Check that we can recover its value. |
| 141 |
#[cfg(target_arch = "x86_64")] |
| 142 |
let process_tid_location = info.regs.rcx; |
| 143 |
#[cfg(target_arch = "x86")] |
| 144 |
let process_tid_location = info.regs.ecx; |
| 145 |
#[cfg(target_arch = "arm")] |
| 146 |
let process_tid_location = info.regs.uregs[3]; |
| 147 |
#[cfg(target_arch = "aarch64")] |
| 148 |
let process_tid_location = info.regs.regs[3]; |
| 149 |
#[cfg(target_arch = "mips")] |
| 150 |
let process_tid_location = info.mcontext.gregs[1]; |
| 151 |
|
| 152 |
let thread_id_data = PtraceDumper::copy_from_process( |
| 153 |
*curr_thread, |
| 154 |
process_tid_location as *mut libc::c_void, |
| 155 |
4, |
| 156 |
) |
| 157 |
.expect("Could not copy from process"); |
| 158 |
let found_thread_id = i32::from_ne_bytes( |
| 159 |
thread_id_data |
| 160 |
.as_slice() |
| 161 |
.try_into() |
| 162 |
.expect("couldn't parse i32 from read data"), |
| 163 |
); |
| 164 |
matching_threads += if *curr_thread == found_thread_id { |
| 165 |
1 |
| 166 |
} else { |
| 167 |
0 |
| 168 |
}; */ |
| 169 |
} |
| 170 |
assert_no_soft_errors!(soft_errors, dumper.resume_threads(&mut soft_errors)); |
| 171 |
child.kill().expect("Failed to kill process"); |
| 172 |
|
| 173 |
// Reap child |
| 174 |
let waitres = child.wait().expect("Failed to wait for child"); |
| 175 |
let status = waitres.signal().expect("Child did not die due to signal"); |
| 176 |
assert_eq!(waitres.code(), None); |
| 177 |
assert_eq!(status, Signal::SIGKILL as i32); |
| 178 |
|
| 179 |
// We clean up the child process before checking the final result |
| 180 |
// TODO: I currently know of no way to write the thread_id into the registers using Rust, |
| 181 |
// so this check is deactivated for now, because it always fails |
| 182 |
// assert_eq!(matching_threads, num_of_threads); |
| 183 |
} |
| 184 |
|
| 185 |
// #[cfg(not(any(target_arch = "mips", target_arch = "arm-eabi"))] |
| 186 |
#[cfg(not(target_arch = "mips"))] |
| 187 |
#[test] |
| 188 |
// Ensure that the linux-gate VDSO is included in the mapping list. |
| 189 |
fn test_mappings_include_linux_gate() { |
| 190 |
spawn_child("mappings_include_linux_gate", &[]); |
| 191 |
} |
| 192 |
|
| 193 |
#[test] |
| 194 |
fn test_linux_gate_mapping_id() { |
| 195 |
disabled_on_ci_and_android!(); |
| 196 |
spawn_child("linux_gate_mapping_id", &[]); |
| 197 |
} |
| 198 |
|
| 199 |
#[test] |
| 200 |
fn test_merged_mappings() { |
| 201 |
let page_size = nix::unistd::sysconf(nix::unistd::SysconfVar::PAGE_SIZE).unwrap(); |
| 202 |
let page_size = std::num::NonZeroUsize::new(page_size.unwrap() as usize).unwrap(); |
| 203 |
let map_size = std::num::NonZeroUsize::new(3 * page_size.get()).unwrap(); |
| 204 |
|
| 205 |
let path: String = if let Ok(p) = std::env::var("TEST_HELPER") { |
| 206 |
p |
| 207 |
} else { |
| 208 |
std::env!("CARGO_BIN_EXE_test").into() |
| 209 |
}; |
| 210 |
let file = std::fs::File::open(&path).unwrap(); |
| 211 |
|
| 212 |
// mmap two segments out of the helper binary, one |
| 213 |
// enclosed in the other, but with different protections. |
| 214 |
let mapped_mem = unsafe { |
| 215 |
mmap( |
| 216 |
None, |
| 217 |
map_size, |
| 218 |
ProtFlags::PROT_READ, |
| 219 |
MapFlags::MAP_SHARED, |
| 220 |
&file, |
| 221 |
0, |
| 222 |
) |
| 223 |
.unwrap() |
| 224 |
}; |
| 225 |
|
| 226 |
let mapped = mapped_mem.as_ptr() as usize; |
| 227 |
|
| 228 |
// Carve a page out of the first mapping with different permissions. |
| 229 |
let _inside_mapping = unsafe { |
| 230 |
mmap( |
| 231 |
std::num::NonZeroUsize::new(mapped + 2 * page_size.get()), |
| 232 |
page_size, |
| 233 |
ProtFlags::PROT_NONE, |
| 234 |
MapFlags::MAP_SHARED | MapFlags::MAP_FIXED, |
| 235 |
&file, |
| 236 |
// Map a different offset just to |
| 237 |
// better test real-world conditions. |
| 238 |
page_size.get().try_into().unwrap(), // try_into() in order to work for 32 and 64 bit |
| 239 |
) |
| 240 |
}; |
| 241 |
|
| 242 |
spawn_child( |
| 243 |
"merged_mappings", |
| 244 |
&[&path, &format!("{mapped}"), &format!("{map_size}")], |
| 245 |
); |
| 246 |
} |
| 247 |
|
| 248 |
#[test] |
| 249 |
// Ensure that the linux-gate VDSO is included in the mapping list. |
| 250 |
fn test_file_id() { |
| 251 |
disabled_on_ci_and_android!(); |
| 252 |
spawn_child("file_id", &[]); |
| 253 |
} |
| 254 |
|
| 255 |
#[test] |
| 256 |
fn test_find_mapping() { |
| 257 |
spawn_child( |
| 258 |
"find_mappings", |
| 259 |
&[ |
| 260 |
&format!("{}", libc::printf as *const () as usize), |
| 261 |
&format!("{}", String::new as *const () as usize), |
| 262 |
], |
| 263 |
); |
| 264 |
} |
| 265 |
|
| 266 |
#[test] |
| 267 |
fn test_copy_from_process_self() { |
| 268 |
disabled_on_ci_and_android!(); |
| 269 |
|
| 270 |
let stack_var: libc::c_long = 0x11223344; |
| 271 |
let heap_var: Box<libc::c_long> = Box::new(0x55667788); |
| 272 |
spawn_child( |
| 273 |
"copy_from_process", |
| 274 |
&[ |
| 275 |
&format!("{}", &stack_var as *const libc::c_long as usize), |
| 276 |
&format!("{}", heap_var.as_ref() as *const libc::c_long as usize), |
| 277 |
], |
| 278 |
); |
| 279 |
} |
| 280 |
|
| 281 |
#[test] |
| 282 |
fn test_sanitize_stack_copy() { |
| 283 |
let num_of_threads = 1; |
| 284 |
let mut child = start_child_and_return(&["spawn_alloc_wait"]); |
| 285 |
let pid = child.id() as i32; |
| 286 |
|
| 287 |
let mut f = BufReader::new(child.stdout.as_mut().expect("Can't open stdout")); |
| 288 |
let mut buf = String::new(); |
| 289 |
let _ = f |
| 290 |
.read_line(&mut buf) |
| 291 |
.expect("Couldn't read address provided by child"); |
| 292 |
let mut output = buf.split_whitespace(); |
| 293 |
let heap_addr = usize::from_str_radix(output.next().unwrap().trim_start_matches("0x"), 16) |
| 294 |
.expect("unable to parse mmap_addr"); |
| 295 |
|
| 296 |
let mut dumper = assert_no_soft_errors!( |
| 297 |
soft_errors, |
| 298 |
PtraceDumper::new_report_soft_errors( |
| 299 |
pid, |
| 300 |
minidump_writer::minidump_writer::STOP_TIMEOUT, |
| 301 |
Default::default(), |
| 302 |
&mut soft_errors, |
| 303 |
) |
| 304 |
) |
| 305 |
.expect("Couldn't init dumper"); |
| 306 |
assert_eq!(dumper.threads.len(), num_of_threads); |
| 307 |
|
| 308 |
assert_no_soft_errors!(soft_errors, dumper.suspend_threads(&mut soft_errors)); |
| 309 |
|
| 310 |
let thread_info = dumper |
| 311 |
.get_thread_info_by_index(0) |
| 312 |
.expect("Couldn't find thread_info"); |
| 313 |
|
| 314 |
let defaced; |
| 315 |
#[cfg(target_pointer_width = "64")] |
| 316 |
{ |
| 317 |
defaced = 0x0defaced0defacedusize.to_ne_bytes() |
| 318 |
} |
| 319 |
#[cfg(target_pointer_width = "32")] |
| 320 |
{ |
| 321 |
defaced = 0x0defacedusize.to_ne_bytes() |
| 322 |
}; |
| 323 |
|
| 324 |
let mut simulated_stack = vec![0xffu8; 2 * size_of::<usize>()]; |
| 325 |
// Pointers into the stack shouldn't be sanitized. |
| 326 |
simulated_stack[size_of::<usize>()..].copy_from_slice(&thread_info.stack_pointer.to_ne_bytes()); |
| 327 |
|
| 328 |
dumper |
| 329 |
.sanitize_stack_copy( |
| 330 |
&mut simulated_stack, |
| 331 |
thread_info.stack_pointer, |
| 332 |
size_of::<usize>(), |
| 333 |
) |
| 334 |
.expect("Could not sanitize stack"); |
| 335 |
|
| 336 |
assert!(simulated_stack[size_of::<usize>()..] != defaced); |
| 337 |
// Memory prior to the stack pointer should be cleared. |
| 338 |
assert_eq!( |
| 339 |
&simulated_stack[0..size_of::<usize>()], |
| 340 |
vec![0u8; size_of::<usize>()].as_slice() |
| 341 |
); |
| 342 |
|
| 343 |
// Small integers should not be sanitized. |
| 344 |
for ii in -4096..=4096isize { |
| 345 |
simulated_stack = vec![0u8; 2 * size_of::<usize>()]; |
| 346 |
simulated_stack[0..size_of::<usize>()].copy_from_slice(&(ii as usize).to_ne_bytes()); |
| 347 |
dumper |
| 348 |
.sanitize_stack_copy(&mut simulated_stack, thread_info.stack_pointer, 0) |
| 349 |
.expect("Failed to sanitize with small integers"); |
| 350 |
assert!(simulated_stack[size_of::<usize>()..] != defaced); |
| 351 |
} |
| 352 |
|
| 353 |
// The instruction pointer definitely should point into an executable mapping. |
| 354 |
let instr_ptr = thread_info.get_instruction_pointer(); |
| 355 |
let mapping_info = dumper |
| 356 |
.find_mapping_no_bias(instr_ptr) |
| 357 |
.expect("Failed to find mapping info"); |
| 358 |
assert!(mapping_info.is_executable()); |
| 359 |
|
| 360 |
// Pointers to code shouldn't be sanitized. |
| 361 |
simulated_stack = vec![0u8; 2 * size_of::<usize>()]; |
| 362 |
simulated_stack[size_of::<usize>()..].copy_from_slice(&instr_ptr.to_ne_bytes()); |
| 363 |
dumper |
| 364 |
.sanitize_stack_copy(&mut simulated_stack, thread_info.stack_pointer, 0) |
| 365 |
.expect("Failed to sanitize with instr_ptr"); |
| 366 |
assert!(simulated_stack[0..size_of::<usize>()] != defaced); |
| 367 |
assert!(simulated_stack[size_of::<usize>()..] != defaced); |
| 368 |
|
| 369 |
// String fragments should be sanitized. |
| 370 |
let junk = "abcdefghijklmnop".as_bytes(); |
| 371 |
simulated_stack.copy_from_slice(&junk[0..2 * size_of::<usize>()]); |
| 372 |
dumper |
| 373 |
.sanitize_stack_copy(&mut simulated_stack, thread_info.stack_pointer, 0) |
| 374 |
.expect("Failed to sanitize with junk"); |
| 375 |
assert_eq!(simulated_stack[0..size_of::<usize>()], defaced); |
| 376 |
assert_eq!(simulated_stack[size_of::<usize>()..], defaced); |
| 377 |
|
| 378 |
// Heap pointers should be sanititzed. |
| 379 |
|
| 380 |
// NOTE: The original test used the heap-address below, but here thread_info.regs.rcx |
| 381 |
// is the instruction pointer, and thus in direct conflict with the "instruction pointer" |
| 382 |
// testcase. |
| 383 |
// Instead we just allocate something on the heap in the child and pass that address to this test. |
| 384 |
// #[cfg(target_arch = "x86_64")] |
| 385 |
// let heap_addr = thread_info.regs.rcx as usize; |
| 386 |
// #[cfg(target_arch = "x86")] |
| 387 |
// let heap_addr = thread_info.regs.ecx as usize; |
| 388 |
// #[cfg(target_arch = "arm")] |
| 389 |
// let heap_addr = thread_info.regs.uregs[3] as usize; |
| 390 |
// #[cfg(target_arch = "aarch64")] |
| 391 |
// let heap_addr = thread_info.regs.regs[3] as usize; |
| 392 |
// #[cfg(target_arch = "mips")] |
| 393 |
// let heap_addr = thread_info.mcontext.gregs[1] as usize; |
| 394 |
|
| 395 |
simulated_stack = vec![0u8; 2 * size_of::<usize>()]; |
| 396 |
|
| 397 |
simulated_stack[0..size_of::<usize>()].copy_from_slice(&heap_addr.to_ne_bytes()); |
| 398 |
dumper |
| 399 |
.sanitize_stack_copy(&mut simulated_stack, thread_info.stack_pointer, 0) |
| 400 |
.expect("Failed to sanitize with heap addr"); |
| 401 |
|
| 402 |
assert_eq!(simulated_stack[0..size_of::<usize>()], defaced); |
| 403 |
|
| 404 |
assert_no_soft_errors!(soft_errors, dumper.resume_threads(&mut soft_errors)); |
| 405 |
|
| 406 |
child.kill().expect("Failed to kill process"); |
| 407 |
|
| 408 |
// Reap child |
| 409 |
let waitres = child.wait().expect("Failed to wait for child"); |
| 410 |
let status = waitres.signal().expect("Child did not die due to signal"); |
| 411 |
assert_eq!(waitres.code(), None); |
| 412 |
assert_eq!(status, Signal::SIGKILL as i32); |
| 413 |
} |