cosca 0.4.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! Windows raw `CreateProcessW` spawn backend.
//!
//! [`spawn_raw`] is the sync entry point: it loads an `executable()` file
//! independently of argv[0] (the case std cannot express on Windows), wiring the
//! child's std handles via `STARTUPINFOEXW` + a scoped `PROC_THREAD_ATTRIBUTE_HANDLE_LIST`.
//! The process-handle + FFI primitives live in [`proc`]; program/env/NUL
//! resolution in [`resolve`]; the MSVCRT fd-table encoder (for fd >= 3)
//! in [`crt_fds`].

#[path = "windows_raw/crt_fds.rs"]
mod crt_fds;

// `pub(crate)`: the async raw backend (`crate::tokio::spawn::windows_raw`) reuses program/env/NUL
// resolution verbatim.
#[path = "windows_raw/resolve.rs"]
pub(crate) mod resolve;

#[path = "windows_raw/proc.rs"]
mod proc;

pub(crate) use proc::RawChild;
// Additional seams the async raw backend reuses: the cancellable handle wait + its
// outcome, and the exit-status reader. The sync path uses these only inside `proc`, so the
// re-export is tokio-only. (`create_process` is reached through the shared `spawn_step`, so it
// needs no re-export.)
#[cfg(feature = "tokio")]
pub(crate) use proc::{exit_status, wait_handle_or_cancel, WaitOutcome};

use std::collections::BTreeMap;
use std::ffi::{OsStr, OsString};
use std::os::windows::ffi::{OsStrExt, OsStringExt};
use std::os::windows::io::{AsRawHandle, OwnedHandle};
use std::path::PathBuf;

use windows::Win32::Foundation::{CloseHandle, SetHandleInformation, HANDLE, HANDLE_FLAG_INHERIT};
use windows::Win32::System::Threading::{
    DeleteProcThreadAttributeList, InitializeProcThreadAttributeList, OpenProcess, UpdateProcThreadAttribute,
    LPPROC_THREAD_ATTRIBUTE_LIST, PROCESS_TERMINATE, PROC_THREAD_ATTRIBUTE_HANDLE_LIST, STARTF_USESTDHANDLES,
    STARTUPINFOEXW,
};

use crate::child::proc_handle::ProcHandle;
use crate::child::spawn::{
    attach_or_fault, reject_batch_path, resolve_identity, resolve_stdio, spawn_lock, ChildEnd, PipeOwnership,
};
use crate::child::Child;
use crate::command::{Command, CommandInput, EnvOp};
use crate::error::Error;
use crate::stdio::{Fd, ResolvedStdio};

/// Spawn `cmd` via raw `CreateProcessW`. Handles descriptors 0/1/2 plus arbitrary fd >= 3 (wired
/// through the MSVCRT `lpReserved2` table), contained (Job Object / TreeWalk) or uncontained.
pub(crate) fn spawn_raw(cmd: &Command, fds: BTreeMap<Fd, ResolvedStdio>, kill_on_drop: bool) -> Result<Child, Error> {
    // .bat/.cmd rejected on the raw program token BEFORE resolution, so a bad/nonexistent batch
    // path still errors loudly (CVE-2024-24576) rather than surfacing as a spawn failure.
    reject_batch_program(cmd)?;

    let image: Option<PathBuf> = cmd.executable_path().map(resolve::resolve_executable).transpose()?;
    if let Some(p) = &image {
        resolve::ensure_no_nul_wide(p.as_os_str())?;
    }
    if let Some(c) = cmd.cwd() {
        resolve::ensure_no_nul_wide(c.as_os_str())?;
    }
    let app_name: Option<Vec<u16>> = image.as_ref().map(|p| to_wide_nul(p.as_os_str()));
    let mut cmdline = raw_program_and_line(cmd)?; // each token NUL-checked
    cmdline.push(0);

    // Containment: mirror `prepare`'s pre-spawn decision on the raw path. An uncontained spawn keeps
    // the defaults (`contain_flags` 0, a `mode: None`/`is_root: false` `Prepared`); a Strongest root
    // spawns CREATE_SUSPENDED and is job-assigned + resumed in `attach_or_fault`.
    let req = cmd.contain_request();
    let marker_present = std::env::var_os(crate::containment::NESTED_ENV).is_some();
    let is_root = !crate::containment::dispatch::is_nested(marker_present);
    // Composed and validated BEFORE `clear_std_handle_inheritance`, which is a process-global
    // `SetHandleInformation` on THIS process's std handles that nothing undoes: a refused spawn
    // must not have mutated the parent.
    let plan = crate::command::flags::windows_spawn(
        &req,
        *cmd.flags_request(),
        is_root,
        crate::command::flags::SpawnBackend::Raw,
    )?;
    let marker_env = plan.marker_env;
    if req.mode.is_some() {
        crate::containment::windows::clear_std_handle_inheritance();
    }

    // Append the inherited root marker AFTER the user's env ops so it survives a user `env_clear()`
    // (mirrors the std path setting the marker after the user's env).
    let env_block = if marker_env {
        let mut ops = cmd.env_ops().to_vec();
        ops.push(EnvOp::Set(
            OsString::from(crate::containment::NESTED_ENV),
            OsString::from("1"),
        ));
        resolve::build_env_block(&ops)?
    } else {
        resolve::build_env_block(cmd.env_ops())?
    };
    let cwd_w = cmd.cwd().map(|c| to_wide_nul(c.as_os_str()));

    // Cap the MSVCRT fd-table to the WORD-sized `cbReserved2` field BEFORE allocating anything.
    ensure_fd_table_fits(&fds)?;

    // Resolve 0/1/2 (always) plus any configured fd >= 3. `resolve_stdio` rejects inherit on fd >= 3.
    let slots: Vec<Fd> = {
        let mut v = vec![Fd::STDIN, Fd::STDOUT, Fd::STDERR];
        v.extend(fds.keys().copied().filter(|f| f.raw() >= 3));
        v
    };
    let (child_ends, parent_ends) = resolve_stdio(&fds, &slots, PipeOwnership::Owned)?;

    // Classify each resolved child end (0/1/2 + fd >= 3) and encode the dense 0..=maxfd MSVCRT
    // fd-table the child CRT reads back from `lpReserved2`.
    let table = build_fd_table(&child_ends)?;

    // STARTUPINFOEXW: STARTF_USESTDHANDLES + hStd* for 0/1/2; `lpReserved2` carries the fd-table so
    // the child CRT recovers fd >= 3; the HANDLE_LIST scopes inheritance AND backs
    // EXTENDED_STARTUPINFO_PRESENT. The table (`bytes`) is kept alive until after CreateProcessW.
    let mut si = STARTUPINFOEXW::default();
    si.StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
    si.StartupInfo.hStdInput = child_handle(&child_ends, Fd::STDIN);
    si.StartupInfo.hStdOutput = child_handle(&child_ends, Fd::STDOUT);
    si.StartupInfo.hStdError = child_handle(&child_ends, Fd::STDERR);
    si.StartupInfo.cbReserved2 = table.bytes.len() as u16; // fits: capped above
    si.StartupInfo.lpReserved2 = table.bytes.as_ptr() as *mut u8;
    // SINGLE handle source: `table.handles` is 0/1/2 + fd >= 3, each a distinct fresh dup from
    // `resolve_stdio` (its 0/1/2 entries ARE the hStd* handles), so no duplicate reaches the list.
    let all_handles: &[HANDLE] = &table.handles;
    let attr = AttributeList::build(all_handles)?;
    si.lpAttributeList = attr.as_ptr();
    // The complete word, composed above by the ONE function all three backends share — the two
    // structural bits this backend cannot spawn without included. Nothing ORs into it here.
    let flags = plan.creation_flags;

    // UNDER THE LOCK: mark the listed child ends inheritable, spawn, then CLOSE the child ends and
    // the attribute list BEFORE the guard releases on EVERY path. An early `?` here would drop the
    // inner-scope guard before `child_ends`/`attr` (Rust drops inner locals first), leaving
    // inheritable handles exposed to a concurrent spawn — so compute a Result and drop explicitly.
    let spawned = {
        let _guard = spawn_lock();
        let r = spawn_step(
            all_handles,
            app_name.as_deref(),
            &mut cmdline,
            &mut si,
            &env_block,
            &cwd_w,
            flags,
            *cmd.flags_request(),
        );
        drop(child_ends); // close the child ends inside the lock, on success AND error
        drop(attr); // DeleteProcThreadAttributeList before the guard releases
        r
    };
    let (proc, pid) = spawned?;

    // Identity read + attach BEFORE building `Child`, with the SAME kill+reap teardown as the std
    // path (dropping the OwnedHandle alone neither kills nor reaps on Windows). The `Prepared`
    // carries the REAL mode + is_root computed above, so `attach_or_fault` assigns the Job Object
    // (Strongest root) or the TreeWalk/Delegated mechanism exactly as the std path does.
    let prepared = crate::containment::Prepared {
        mode: req.mode,
        is_root,
        // From the COMPOSED word, which carries the caller's flags as well as the containment
        // decision: a derivation reading only the containment half would report an in-process
        // route for a child this spawn deliberately put in another console.
        graceful: crate::containment::windows::mechanism_from_flags(flags),
    };
    let raw_handle = proc.as_raw_handle();
    let attachment = match attach_or_fault(pid, raw_handle, prepared) {
        Ok(v) => v,
        Err(e) => {
            raw_spawn_teardown(proc, pid);
            return Err(e);
        }
    };
    let id = match resolve_identity(pid) {
        crate::identity::Resolved::Found(id) => id,
        other => {
            raw_spawn_teardown(proc, pid);
            return Err(crate::child::spawn::spawn_identity_error(other));
        }
    };

    Ok(Child::from_parts(
        ProcHandle::Raw(RawChild::new(proc, pid)),
        id,
        parent_ends,
        kill_on_drop,
        attachment,
    ))
}

/// Reject a descriptor set whose dense MSVCRT fd-table would exceed the WORD-sized `cbReserved2`
/// field, BEFORE any allocation (`encoded_len` is overflow-safe). `maxfd` is the largest configured
/// slot; 0/1/2 always resolve, so the `2` floor covers an empty/low map. `pub(crate)`: shared with
/// the async raw backend, which caps the same table.
pub(crate) fn ensure_fd_table_fits(fds: &BTreeMap<Fd, ResolvedStdio>) -> Result<(), Error> {
    let maxfd = fds.keys().map(|f| f.raw()).max().unwrap_or(2);
    if !crt_fds::table_fits(crt_fds::encoded_len(maxfd)) {
        return Err(Error::Unsupported {
            op: format!("fd {maxfd}"),
            platform: "windows",
            detail: "descriptor table exceeds the 64KiB cbReserved2 limit".into(),
        });
    }
    Ok(())
}

/// Classify each resolved child end (0/1/2 + fd >= 3) for its CRT device flags, then encode the
/// dense `0..=maxfd` MSVCRT fd-table the child CRT reads back from `lpReserved2`. The returned
/// `handles` is the SINGLE inheritance source (0/1/2 + fd >= 3, each a distinct fresh dup), so no
/// duplicate reaches the HANDLE_LIST. `pub(crate)`: shared with the async raw backend, which builds
/// the identical table. Gate the caller on [`ensure_fd_table_fits`] first.
pub(crate) fn build_fd_table(child_ends: &BTreeMap<Fd, ChildEnd>) -> Result<crt_fds::FdTable, Error> {
    let mut entries: BTreeMap<Fd, (HANDLE, crt_fds::FdKind)> = BTreeMap::new();
    for (&slot, end) in child_ends {
        let h = HANDLE(end.as_raw_handle());
        entries.insert(slot, (h, crt_fds::classify(h)?));
    }
    Ok(crt_fds::encode(&entries))
}

/// Mark each listed handle inheritable, then spawn. Returns a Result WITHOUT `?`-ing so the caller
/// can close the child ends + attribute list before releasing the spawn lock on either arm.
/// `pub(crate)`: the async raw backend reuses the inheritable-mark + `create_process` window.
// `CreateProcessW`'s own parameter list, plus the request its failure is classified against.
// Bundling them would only rename the same values one call site deep.
#[allow(clippy::too_many_arguments)]
pub(crate) fn spawn_step(
    handles: &[HANDLE],
    app: Option<&[u16]>,
    cmdline: &mut [u16],
    si: &mut STARTUPINFOEXW,
    env: &Option<Vec<u16>>,
    cwd: &Option<Vec<u16>>,
    flags: u32,
    request: crate::command::flags::FlagsRequest,
) -> Result<(OwnedHandle, u32), Error> {
    for &h in handles {
        set_inherit(h)?;
    }
    // Only the syscall's own Result is classified — the inherit loop above stays outside, since
    // an access-denied from `SetHandleInformation` is not a breakaway denial.
    proc::create_process(app, cmdline, si, env, cwd, flags)
        .map_err(|e| crate::command::flags::classify_spawn_syscall_error(e, request))
}

/// Kill + reap a just-spawned child whose post-spawn attach/identity read failed, so a failed spawn
/// never leaks a running/zombie process (mirrors the std path's teardown). `pub(crate)`: the async
/// raw backend shares the identical error-teardown.
pub(crate) fn raw_spawn_teardown(proc: OwnedHandle, pid: u32) {
    let rc = RawChild::new(proc, pid);
    // Windows only, so the std path-s invariant does NOT carry: there is no zombie to reap
    // and `rc.wait()` is a bare `WaitForSingleObject(handle, INFINITE)`. If the kill failed,
    // nothing asked the child to exit and that wait would park forever - a logged, leaked
    // child is strictly better.
    if let Err(e) = rc.kill() {
        log::warn!("raw spawn teardown: kill of pid {pid} failed: {e}; not waiting");
        return;
    }
    if let Err(e) = rc.wait() {
        log::warn!("raw spawn teardown failed to reap pid {pid}: {e}");
        debug_assert!(false, "raw spawn teardown failed to reap child: {e}");
    }
}

/// Does the caller hold `PROCESS_TERMINATE` on `pid`? A STATIC permission answer (a second
/// `OpenProcess`), used to separate a genuine higher-integrity runas denial from the OS
/// teardown-window `ACCESS_DENIED` WITHOUT racing a `try_wait`. Pid-reuse-safe when the caller
/// still holds a handle pinning the process object. Shared by the sync `RawChild` and the async
/// `RawAsyncChild` runas kill paths so both surface the same typed `Unkillable`.
pub(crate) fn can_terminate(pid: u32) -> bool {
    // SAFETY: the caller holds a live owned handle pinning the process object, so `pid` still
    // names THIS process; OpenProcess tolerates failure (returns Err).
    match unsafe { OpenProcess(PROCESS_TERMINATE, false, pid) } {
        Ok(h) => {
            // SAFETY: `h` is an owned handle from a successful OpenProcess; close it once.
            let closed = unsafe { CloseHandle(h) };
            debug_assert!(closed.is_ok(), "CloseHandle of an owned probe handle should not fail");
            true
        }
        Err(_) => false,
    }
}

fn child_handle(ends: &BTreeMap<Fd, ChildEnd>, slot: Fd) -> HANDLE {
    // resolve_stdio with the 0/1/2 slot list always resolves all three (None -> inherit).
    HANDLE(ends[&slot].as_raw_handle())
}

/// Mark `h` inheritable (`bInheritHandles` + the HANDLE_LIST require it).
fn set_inherit(h: HANDLE) -> Result<(), Error> {
    // SAFETY: `h` is a live child-end handle we own; SetHandleInformation only toggles its flags.
    unsafe { SetHandleInformation(h, HANDLE_FLAG_INHERIT.0, HANDLE_FLAG_INHERIT) }.map_err(|e| Error::Io(e.into()))
}

pub(crate) fn to_wide_nul(s: &OsStr) -> Vec<u16> {
    s.encode_wide().chain(std::iter::once(0)).collect()
}

/// Reject a `.bat`/`.cmd` program by the token that determines the loaded image: `executable()` if
/// set, else the argv[0] / command-line first token. Runs before resolution. `pub(crate)`: shared
/// with the async raw backend.
pub(crate) fn reject_batch_program(cmd: &Command) -> Result<(), Error> {
    let token = cmd.executable_path().map(PathBuf::from).or_else(|| program_token(cmd));
    if let Some(prog) = token {
        reject_batch_path(&prog)?;
    }
    Ok(())
}

/// The program token (argv[0] / command-line first token) when `executable()` is unset.
fn program_token(cmd: &Command) -> Option<PathBuf> {
    match cmd.input() {
        CommandInput::Empty => None,
        CommandInput::Argv(argv) => argv.first().map(PathBuf::from),
        CommandInput::CommandLine(line) => {
            let wide: Vec<u16> = line.encode_wide().collect();
            crate::quote::windows::first_token_wide(&wide).map(|t| PathBuf::from(OsString::from_wide(&t)))
        }
    }
}

/// Build the child's command line. argv[0] is always the user's name (independent of the loaded
/// `executable()`); each token is NUL-checked. `commandline()` is passed through verbatim (the OS
/// parses argv[0] as its first token); `argv` is joined via the MSVCRT quoter. `pub(crate)`: shared
/// with the async raw backend.
pub(crate) fn raw_program_and_line(cmd: &Command) -> Result<Vec<u16>, Error> {
    match cmd.input() {
        CommandInput::Empty => {
            // executable() alone, no argv/commandline: the OS uses lpApplicationName as argv[0].
            if cmd.executable_path().is_some() {
                Ok(Vec::new())
            } else {
                Err(Error::Io(std::io::Error::other("no program specified")))
            }
        }
        CommandInput::Argv(argv) => {
            if argv.is_empty() && cmd.executable_path().is_none() {
                return Err(Error::Io(std::io::Error::other("empty argv")));
            }
            let mut wides: Vec<Vec<u16>> = Vec::with_capacity(argv.len());
            for a in argv {
                resolve::ensure_no_nul_wide(a)?;
                wides.push(a.encode_wide().collect());
            }
            let refs: Vec<&[u16]> = wides.iter().map(Vec::as_slice).collect();
            Ok(crate::quote::windows::join_wide(&refs))
        }
        CommandInput::CommandLine(line) => {
            resolve::ensure_no_nul_wide(line)?;
            Ok(line.encode_wide().collect())
        }
    }
}

/// RAII owner of a `PROC_THREAD_ATTRIBUTE_LIST` carrying a HANDLE_LIST. Deletes the list on drop.
/// The handle array it references must outlive the list (per `UpdateProcThreadAttribute`): callers
/// keep `all_handles` alive across both `CreateProcessW` and this drop. `pub(crate)`: the async raw
/// backend builds the identical scoped inheritance list.
pub(crate) struct AttributeList {
    _buf: Vec<u8>,
    list: LPPROC_THREAD_ATTRIBUTE_LIST,
}

impl AttributeList {
    pub(crate) fn build(handles: &[HANDLE]) -> Result<AttributeList, Error> {
        let mut size: usize = 0;
        // Sizing call: returns ERROR_INSUFFICIENT_BUFFER and writes the required byte count.
        // SAFETY: the null-list form is the documented way to query the buffer size.
        let _ = unsafe { InitializeProcThreadAttributeList(None, 1, None, &mut size) };
        let mut buf: Vec<u8> = vec![0u8; size];
        let list = LPPROC_THREAD_ATTRIBUTE_LIST(buf.as_mut_ptr().cast());
        // SAFETY: `buf` is `size` bytes, matching the queried requirement; count = 1 (one attribute).
        unsafe { InitializeProcThreadAttributeList(Some(list), 1, None, &mut size) }
            .map_err(|e| Error::Io(e.into()))?;
        // SAFETY: `list` is initialized; the HANDLE_LIST attribute takes an array of `handles.len()`
        // HANDLEs by pointer (not copied), which the caller keeps alive until this list is deleted.
        let update = unsafe {
            UpdateProcThreadAttribute(
                list,
                0,
                PROC_THREAD_ATTRIBUTE_HANDLE_LIST as usize,
                Some(handles.as_ptr().cast()),
                std::mem::size_of_val(handles),
                None,
                None,
            )
        };
        if let Err(e) = update {
            // SAFETY: `list` was initialized above; delete it before `buf` drops.
            unsafe { DeleteProcThreadAttributeList(list) };
            return Err(Error::Io(e.into()));
        }
        Ok(AttributeList { _buf: buf, list })
    }

    pub(crate) fn as_ptr(&self) -> LPPROC_THREAD_ATTRIBUTE_LIST {
        self.list
    }
}

impl Drop for AttributeList {
    fn drop(&mut self) {
        // SAFETY: `list` was initialized in `build`; Delete is its paired teardown.
        unsafe { DeleteProcThreadAttributeList(self.list) };
    }
}