1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//! Windows fixtures for the frozen-spawn tests: a suspended child, its initial thread's suspend
//! count, and a job's limit flags.
//!
//! The suspend count is a kernel fact, so "this child ran nothing yet" is asserted from it and never
//! from the absence of an event.
use std::os::windows::io::AsRawHandle;
use std::os::windows::process::CommandExt;
use windows::Win32::Foundation::{CloseHandle, HANDLE};
use windows::Win32::System::Diagnostics::Debug::WriteProcessMemory;
use windows::Win32::System::Diagnostics::ToolHelp::{
CreateToolhelp32Snapshot, Thread32First, Thread32Next, TH32CS_SNAPTHREAD, THREADENTRY32,
};
use windows::Win32::System::JobObjects::{
JobObjectBasicLimitInformation, QueryInformationJobObject, JOBOBJECT_BASIC_LIMIT_INFORMATION, JOB_OBJECT_LIMIT,
};
use windows::Win32::System::LibraryLoader::{GetModuleHandleA, GetProcAddress};
use windows::Win32::System::Memory::{VirtualAllocEx, MEM_COMMIT, MEM_RESERVE, PAGE_READWRITE};
use windows::Win32::System::Threading::{
CreateRemoteThread, GetThreadId, GetThreadTimes, OpenProcess, OpenThread, ResumeThread, SuspendThread,
TerminateProcess, WaitForSingleObject, CREATE_NO_WINDOW, CREATE_SUSPENDED, INFINITE, LPTHREAD_START_ROUTINE,
PROCESS_SYNCHRONIZE, PROCESS_TERMINATE, THREAD_QUERY_LIMITED_INFORMATION, THREAD_SUSPEND_RESUME,
};
use crate::containment::windows::JobHandle;
/// A `findstr x` blocker created `CREATE_SUSPENDED` through std, with piped stdin; closing it lets the
/// child exit once it runs.
pub(crate) fn spawn_suspended_blocker() -> std::process::Child {
let mut cmd = std::process::Command::new(crate::test_child::BLOCKER_ARGV[0]);
cmd.args(&crate::test_child::BLOCKER_ARGV[1..])
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.creation_flags(CREATE_SUSPENDED.0 | CREATE_NO_WINDOW.0);
crate::test_spawn::spawn(&mut cmd).expect("spawn a suspended blocker")
}
/// The initial thread of a process that is still alive, held by handle: its earliest-created
/// thread, the product's rule. Opened while the caller's own handle on the process pins the pid.
/// The handle is live, owned and closed once, with suspend rights, for as long as the value exists.
pub(crate) struct InitialThread(HANDLE);
impl InitialThread {
pub(crate) fn of(pid: u32) -> InitialThread {
let mut earliest: Option<(u64, HANDLE)> = None;
// SAFETY: standard Win32 snapshot walk; the snapshot handle is closed before return, and
// every thread handle is either kept in `earliest` or closed.
unsafe {
let snap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0).expect("thread snapshot");
let mut entry = THREADENTRY32 {
dwSize: std::mem::size_of::<THREADENTRY32>() as u32,
..Default::default()
};
let mut step = Thread32First(snap, &mut entry);
while step.is_ok() {
if entry.th32OwnerProcessID == pid {
// A thread that has exited since the snapshot cannot be the initial one of a live child.
if let Ok(thread) = OpenThread(
THREAD_SUSPEND_RESUME | THREAD_QUERY_LIMITED_INFORMATION,
false,
entry.th32ThreadID,
) {
let (mut created, mut exited, mut kernel, mut user) = Default::default();
GetThreadTimes(thread, &mut created, &mut exited, &mut kernel, &mut user)
.expect("read the thread's creation time");
let created = (u64::from(created.dwHighDateTime) << 32) | u64::from(created.dwLowDateTime);
match earliest {
Some((best, _)) if best <= created => _ = CloseHandle(thread),
_ => {
if let Some((_, older)) = earliest.replace((created, thread)) {
_ = CloseHandle(older);
}
}
}
}
}
step = Thread32Next(snap, &mut entry);
}
_ = CloseHandle(snap);
}
let (_, thread) = earliest.unwrap_or_else(|| panic!("pid {pid} has no openable thread in the snapshot"));
InitialThread(thread)
}
/// The thread id.
pub(crate) fn tid(&self) -> u32 {
// SAFETY: `self.0` is a live thread handle.
unsafe { GetThreadId(self.0) }
}
/// The suspend count, read by suspending and resuming once. Only meaningful while the thread
/// is not running anything that matters (a frozen child), or when the count is the question.
pub(crate) fn suspend_count(&self) -> u32 {
let previous = self.suspend_once();
self.release_one();
previous
}
/// `SuspendThread`: returns the previous count.
pub(crate) fn suspend_once(&self) -> u32 {
let previous = unsafe { SuspendThread(self.0) };
assert_ne!(
previous,
u32::MAX,
"SuspendThread failed: {}",
std::io::Error::last_os_error()
);
previous
}
/// `ResumeThread`: returns the previous count.
pub(crate) fn release_one(&self) -> u32 {
let previous = unsafe { ResumeThread(self.0) };
assert_ne!(
previous,
u32::MAX,
"ResumeThread failed: {}",
std::io::Error::last_os_error()
);
previous
}
}
impl Drop for InitialThread {
fn drop(&mut self) {
unsafe { _ = CloseHandle(self.0) };
}
}
/// A thread another party started in a child, as an EDR agent or a debugger does: held by handle,
/// closed on drop. It is not the child's initial thread.
pub(crate) struct InjectedThread(HANDLE);
impl InjectedThread {
/// The thread id.
pub(crate) fn tid(&self) -> u32 {
// SAFETY: `self.0` is a live thread handle.
unsafe { GetThreadId(self.0) }
}
/// The suspend count, read by suspending and resuming once; meaningful because the thread is
/// blocked for good (see [`inject_running_thread`]) and so never changes the count itself.
pub(crate) fn suspend_count(&self) -> u32 {
// SAFETY: `self.0` is a live thread handle with suspend/resume rights (the creator's).
let previous = unsafe { SuspendThread(self.0) };
assert_ne!(
previous,
u32::MAX,
"SuspendThread failed: {}",
std::io::Error::last_os_error()
);
// SAFETY: as above.
let resumed = unsafe { ResumeThread(self.0) };
assert_eq!(resumed, previous + 1, "the thread must be back at the count it had");
previous
}
}
impl Drop for InjectedThread {
fn drop(&mut self) {
// SAFETY: the handle is owned and closed once.
unsafe { _ = CloseHandle(self.0) };
}
}
/// Start a second thread in the process behind `process` (a handle with thread-creation, memory
/// and query rights), released at once: its suspend count is 0 from
/// the moment this returns, and it stays alive, blocked for good on an SRW lock this function
/// writes into the child as already held. It starts in ntdll's `RtlAcquireSRWLockExclusive`,
/// whose address is the same in every process of this boot.
pub(crate) fn inject_running_thread(process: HANDLE) -> InjectedThread {
let thread = inject_suspended_thread(process);
// SAFETY: `thread` is a live handle; the new thread was created suspended once.
assert_eq!(
unsafe { ResumeThread(thread.0) },
1,
"the new thread was created suspended once"
);
thread
}
/// Like [`inject_running_thread`], but the thread is left at suspend count 1, as a debugger or an
/// EDR agent that holds a thread of the child suspended leaves it.
pub(crate) fn inject_suspended_thread(process: HANDLE) -> InjectedThread {
// SAFETY: `process` is a live process handle with the rights above. The lock word is data in
// memory this function allocates in the child; ntdll is mapped in every process, and
// `RtlAcquireSRWLockExclusive` takes one pointer, which the thread gets as its argument. The
// thread handle is owned by the returned value.
unsafe {
let lock = VirtualAllocEx(process, None, 4096, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
assert!(
!lock.is_null(),
"allocate the lock in the child: {}",
std::io::Error::last_os_error()
);
// An SRW lock is held exclusively, with no waiters, when its word is 1.
let held: usize = 1;
WriteProcessMemory(process, lock, std::ptr::addr_of!(held).cast(), size_of::<usize>(), None)
.expect("write the held lock into the child");
let ntdll = GetModuleHandleA(windows::core::s!("ntdll.dll")).expect("ntdll is loaded");
let start =
GetProcAddress(ntdll, windows::core::s!("RtlAcquireSRWLockExclusive")).expect("RtlAcquireSRWLockExclusive");
let thread = CreateRemoteThread(
process,
None,
0,
LPTHREAD_START_ROUTINE::Some(std::mem::transmute::<
unsafe extern "system" fn() -> isize,
unsafe extern "system" fn(*mut std::ffi::c_void) -> u32,
>(start)),
Some(lock),
CREATE_SUSPENDED.0,
None,
)
.expect("start a thread in the child");
InjectedThread(thread)
}
}
/// [`inject_suspended_thread`] into a std child, whose handle has full access.
pub(crate) fn inject_suspended_thread_into_std_child(child: &std::process::Child) -> InjectedThread {
inject_suspended_thread(HANDLE(raw(child)))
}
/// [`inject_running_thread`] into a std child, whose handle has full access.
pub(crate) fn inject_running_thread_into_std_child(child: &std::process::Child) -> InjectedThread {
inject_running_thread(HANDLE(raw(child)))
}
/// [`inject_running_thread`] into the live process `pid`, which the caller keeps from being reaped.
pub(crate) fn inject_running_thread_into_pid(pid: u32) -> InjectedThread {
use windows::Win32::System::Threading::{
OpenProcess, PROCESS_CREATE_THREAD, PROCESS_QUERY_INFORMATION, PROCESS_VM_OPERATION, PROCESS_VM_READ,
PROCESS_VM_WRITE,
};
// SAFETY: plain Win32 call; the handle is closed below, after the thread exists.
let process = unsafe {
OpenProcess(
PROCESS_CREATE_THREAD
| PROCESS_QUERY_INFORMATION
| PROCESS_VM_OPERATION
| PROCESS_VM_READ
| PROCESS_VM_WRITE,
false,
pid,
)
}
.expect("open the frozen process");
let thread = inject_running_thread(process);
// SAFETY: owned and closed once; the thread does not depend on this handle.
unsafe { _ = CloseHandle(process) };
thread
}
/// The limit flags `job` holds now.
pub(crate) fn limit_flags(job: &JobHandle) -> JOB_OBJECT_LIMIT {
let mut info = JOBOBJECT_BASIC_LIMIT_INFORMATION::default();
job.with_handle(|handle| {
// SAFETY: `handle` is live for the call, and `info` is a correctly sized out-parameter.
unsafe {
QueryInformationJobObject(
Some(handle),
JobObjectBasicLimitInformation,
std::ptr::addr_of_mut!(info).cast(),
std::mem::size_of::<JOBOBJECT_BASIC_LIMIT_INFORMATION>() as u32,
None,
)
}
.expect("query the job's limits");
})
.expect("the job handle is live");
info.LimitFlags
}
/// The raw process handle of a std child, as `attach` takes it.
pub(crate) fn raw(child: &std::process::Child) -> std::os::windows::io::RawHandle {
child.as_raw_handle()
}
thread_local! {
static ROOT_TEARDOWNS: std::cell::Cell<Option<u32>> = const { std::cell::Cell::new(None) };
}
/// Called by `ProcHandle::teardown_on_drop`; counts while a recorder lives on this thread.
pub(crate) fn note_root_teardown() {
ROOT_TEARDOWNS.with(|c| {
if let Some(n) = c.get() {
c.set(Some(n + 1));
}
});
}
/// Count every root teardown a drop or `tear_down_now` starts on THIS thread until the recorder
/// drops. The teardown still runs.
pub(crate) fn record_root_teardowns() -> RootTeardownRecorder {
ROOT_TEARDOWNS.with(|c| c.set(Some(0)));
RootTeardownRecorder(())
}
#[must_use = "recording stops as soon as the recorder is dropped"]
pub(crate) struct RootTeardownRecorder(());
impl RootTeardownRecorder {
pub(crate) fn count(&self) -> u32 {
ROOT_TEARDOWNS.with(|c| c.get().expect("the recorder is live"))
}
}
impl Drop for RootTeardownRecorder {
fn drop(&mut self) {
ROOT_TEARDOWNS.with(|c| c.set(None));
}
}
/// An owned handle on a process that can wait for it and end it, opened while the caller still
/// holds the process, so the pid names nothing else.
pub(crate) struct Remote(HANDLE);
impl Remote {
pub(crate) fn open(pid: u32) -> Remote {
// SAFETY: plain Win32 call; the handle is closed on drop.
let handle =
unsafe { OpenProcess(PROCESS_TERMINATE | PROCESS_SYNCHRONIZE, false, pid) }.expect("open the child");
Remote(handle)
}
/// Block until the process is gone; the process object signals, nothing here is a timer.
pub(crate) fn wait_for_death(&self) {
// SAFETY: `self.0` is live.
unsafe { WaitForSingleObject(self.0, INFINITE) };
}
/// Whether the process is gone now. A single non-blocking look, used only after a drop that
/// kills synchronously, so it is not a bet that enough time has passed.
pub(crate) fn is_dead_now(&self) -> bool {
// SAFETY: `self.0` is live.
unsafe { WaitForSingleObject(self.0, 0) }.0 == 0
}
pub(crate) fn end(&self) {
// SAFETY: `self.0` is live with `PROCESS_TERMINATE`.
unsafe { TerminateProcess(self.0, 1) }.expect("end the child");
self.wait_for_death();
}
}
impl Drop for Remote {
fn drop(&mut self) {
// SAFETY: owned, closed once.
unsafe { _ = CloseHandle(self.0) };
}
}