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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
//! POSIX elevation effect layer (`cfg(unix)`): backend detection, pure argv
//! construction, non-destructive command rewrite, and the controlling-terminal probe.

use std::ffi::{OsStr, OsString};
use std::fs::File;
use std::os::fd::OwnedFd;
use std::path::{Path, PathBuf};

use zeroize::Zeroize;

use super::plan::{BackendSet, Host, Os, Transition};
use super::{Auth, Backend, ElevatedStdio, ElevatedVia, ElevationReport, Privilege, Secret};
use crate::command::{Command, CommandInput, EnvOp};
use crate::error::{ElevationErrorKind, Error};
use crate::stdio::{Fd, Stdio};

/// A valid environment variable name: `[A-Za-z_][A-Za-z0-9_]*`, ASCII only. A name
/// with a comma / `=` / non-ASCII byte has no lossless place in `--preserve-env`'s
/// comma-joined list or `--setenv=NAME=VALUE`.
fn valid_env_name(k: &OsStr) -> bool {
    use std::os::unix::ffi::OsStrExt;
    let b = k.as_bytes();
    match b.first() {
        Some(&c) if c == b'_' || c.is_ascii_alphabetic() => {}
        _ => return false,
    }
    b.iter().all(|&c| c == b'_' || c.is_ascii_alphanumeric())
}

fn unsupported_env_name(k: &OsStr) -> Error {
    Error::Unsupported {
        op: "forwarding an env var with an unusual name across elevation".into(),
        platform: "unix",
        detail: format!("env var name {k:?} is not [A-Za-z_][A-Za-z0-9_]*; it cannot be forwarded losslessly"),
    }
}

/// `--preserve-env=A,B,…` (names validated; values are set in the backend's own env).
fn preserve_env_flag(env: &[(OsString, OsString)]) -> Result<OsString, Error> {
    let mut flag = OsString::from("--preserve-env=");
    for (i, (k, _)) in env.iter().enumerate() {
        if !valid_env_name(k) {
            return Err(unsupported_env_name(k));
        }
        if i > 0 {
            flag.push(",");
        }
        flag.push(k);
    }
    Ok(flag)
}

/// Build the full elevated argv. argv[0] is the injected ABSOLUTE `backend_path`.
/// `env` MUST be pre-sanitized and sorted (see [`super::sanitize::EnvSanitizer::apply`]).
/// Pure — no installed backend required.
pub(crate) fn build_argv(
    backend: Backend,
    backend_path: &OsStr,
    auth: &Auth,
    program: &OsStr,
    args: &[OsString],
    env: &[(OsString, OsString)],
) -> Result<Vec<OsString>, Error> {
    let mut argv: Vec<OsString> = vec![backend_path.to_os_string()];
    match backend {
        Backend::Sudo => {
            match auth {
                Auth::NonInteractive => argv.push("-n".into()),
                Auth::Stdin(_) => argv.push("-S".into()),
                Auth::Askpass(_) => argv.push("-A".into()),
                Auth::Interactive | Auth::Gui => {}
            }
            if !env.is_empty() {
                argv.push(preserve_env_flag(env)?);
            }
        }
        Backend::Doas => {
            debug_assert!(
                env.is_empty(),
                "doas forwards no env; the rewrite rejects .env() for doas"
            );
            if matches!(auth, Auth::NonInteractive) {
                argv.push("-n".into());
            }
        }
        Backend::Pkexec => {
            debug_assert!(
                env.is_empty(),
                "pkexec forwards no env; the rewrite rejects .env() for pkexec"
            );
            // Fail loud if the graphical agent is missing, instead of a blocking text prompt.
            argv.push("--disable-internal-agent".into());
            // pkexec has no `--` terminator, so a leading-dash program cannot be shielded.
            if program_starts_with_dash(program) {
                return Err(Error::Unsupported {
                    op: "elevating a leading-dash program under pkexec".into(),
                    platform: "unix",
                    detail: "pkexec cannot parse a `--` terminator, so a program starting with `-` would be taken as a pkexec option; use sudo/doas/run0, or a non-dash program path".into(),
                });
            }
        }
        Backend::Run0 => {
            argv.push("--pipe".into());
            if matches!(auth, Auth::NonInteractive) {
                argv.push("--no-ask-password".into());
            }
            for (k, v) in env {
                if !valid_env_name(k) {
                    return Err(unsupported_env_name(k));
                }
                let mut a = OsString::from("--setenv=");
                a.push(k);
                a.push("=");
                a.push(v);
                argv.push(a);
            }
        }
        Backend::Auto => unreachable!("build_argv received unresolved Backend::Auto; the planner resolves Auto"),
    }
    // Terminate option/assignment parsing before the program — every backend EXCEPT
    // pkexec, whose option loop mis-parses `--` (a leading-dash pkexec program is
    // rejected above instead).
    if backend != Backend::Pkexec {
        argv.push("--".into());
    }
    argv.push(program.to_os_string());
    argv.extend(args.iter().cloned());
    Ok(argv)
}

/// Does `program` begin with `-`? (Only pkexec, which has no `--` shield, cares.)
fn program_starts_with_dash(program: &OsStr) -> bool {
    use std::os::unix::ffi::OsStrExt;
    program.as_bytes().first() == Some(&b'-')
}

pub(super) fn is_elevated() -> bool {
    // SAFETY: geteuid has no preconditions and never fails.
    unsafe { libc::geteuid() == 0 }
}

/// Does this session have a controlling terminal? Probes `/dev/tty` directly —
/// which resolves to the controlling terminal regardless of stdin redirection and
/// fails once a process has none (e.g. after `setsid`). `O_NONBLOCK` avoids
/// blocking on a carrier-less serial console; the probe only needs the open to
/// succeed. `isatty(stdin)` answers a different question and is wrong for both cases.
#[doc(hidden)]
pub fn controlling_terminal_present() -> bool {
    // SAFETY: open/close of a fixed path; the fd is closed on the success path.
    unsafe {
        let fd = libc::open(c"/dev/tty".as_ptr(), libc::O_RDWR | libc::O_CLOEXEC | libc::O_NONBLOCK);
        if fd < 0 {
            return false;
        }
        libc::close(fd);
        true
    }
}

/// A best-effort HINT that `path` is an executable file for the EFFECTIVE ids.
/// `faccessat(AT_EACCESS)` answers for the ids that will actually exec (unlike
/// `access`, which uses the real ids); a real exec failure is still surfaced as
/// `BackendUnavailable` at spawn time.
fn is_executable(path: &Path) -> bool {
    use std::os::unix::ffi::OsStrExt;
    let Ok(c) = std::ffi::CString::new(path.as_os_str().as_bytes()) else {
        return false;
    };
    // SAFETY: faccessat with a valid NUL-terminated path; a read-only permission query.
    path.is_file() && unsafe { libc::faccessat(libc::AT_FDCWD, c.as_ptr(), libc::X_OK, libc::AT_EACCESS) == 0 }
}

/// Resolve `program` to its ABSOLUTE path on `$PATH`.
pub(super) fn resolve_on_path(program: &str) -> Option<PathBuf> {
    resolve_in_path_var(&std::env::var_os("PATH")?, program)
}

/// PURE path resolution over an explicit PATH value: check the exec bit and SKIP
/// empty elements (an empty element is CWD — never resolve a backend there).
pub(super) fn resolve_in_path_var(path_var: &OsStr, program: &str) -> Option<PathBuf> {
    std::env::split_paths(path_var).find_map(|dir| {
        if dir.as_os_str().is_empty() {
            return None; // empty element = CWD; never resolve here
        }
        let cand = dir.join(program);
        is_executable(&cand).then_some(cand)
    })
}

/// Reads `sysctl kern.argmax` for [`Host::arg_max`]; `None` if the query fails.
#[cfg(target_os = "macos")]
fn kern_argmax() -> Option<usize> {
    let mut mib = [libc::CTL_KERN, libc::KERN_ARGMAX];
    let mut value: libc::c_int = 0;
    let mut len = std::mem::size_of::<libc::c_int>();
    // SAFETY: a two-element MIB with a matching `c_int` out-buffer and its exact
    // size; a read-only sysctl (null new-value, zero new-length).
    let rc = unsafe {
        libc::sysctl(
            mib.as_mut_ptr(),
            mib.len() as libc::c_uint,
            &mut value as *mut libc::c_int as *mut libc::c_void,
            &mut len,
            std::ptr::null_mut(),
            0,
        )
    };
    if rc != 0 || value <= 0 {
        log::debug!("could not read kern.argmax; the elevation length guard is disabled");
        return None;
    }
    Some(value as usize)
}

pub(super) fn detect() -> Host {
    Host {
        elevated: is_elevated(),
        has_tty: controlling_terminal_present(),
        available: BackendSet {
            run0: resolve_on_path("run0"),
            sudo: resolve_on_path("sudo"),
            doas: resolve_on_path("doas"),
            pkexec: resolve_on_path("pkexec"),
            // A fixed system path, never PATH-resolved: a PATH lookup would let a
            // shadowing `osascript` earlier on PATH receive an elevation request.
            osascript: {
                #[cfg(target_os = "macos")]
                {
                    let p = Path::new("/usr/bin/osascript");
                    is_executable(p).then(|| p.to_path_buf())
                }
                #[cfg(not(target_os = "macos"))]
                {
                    None
                }
            },
        },
        os: if cfg!(target_os = "macos") { Os::MacOs } else { Os::Unix },
        arg_max: {
            #[cfg(target_os = "macos")]
            {
                kern_argmax()
            }
            #[cfg(not(target_os = "macos"))]
            {
                None
            }
        },
    }
}

// ===== Non-destructive rewrite + deferred password channel =====
//
// The deferred-password chain (`PendingPassword`/`password_line`/`write_*`) and the
// `PosixRewrite` fields are consumed by the sync and async POSIX spawn arms.

/// The `Auth::Stdin` password channel: the pipe write-end plus the secret, written
/// AFTER spawn (the child is then draining via `sudo -S`).
pub(crate) struct PendingPassword {
    writer: std::io::PipeWriter,
    secret: Secret,
}

/// The password line to feed `sudo -S`: the secret plus a trailing newline, in a buffer
/// pre-sized to `secret.len() + 1` so the `push` never reallocates. A realloc would leave
/// an un-zeroized plaintext copy in the freed allocation. Zeroize the returned buffer
/// after use.
fn password_line(secret: &[u8]) -> Vec<u8> {
    let mut bytes = Vec::with_capacity(secret.len() + 1);
    bytes.extend_from_slice(secret);
    bytes.push(b'\n');
    bytes
}

/// Put `writer`'s underlying fd into non-blocking mode so a write cannot block when the
/// backend never reads fd0 (a cached-credential / NOPASSWD sudo). The non-blocking
/// invariant is load-bearing (`write_after_spawn` relies on `WouldBlock`), so an fcntl
/// failure is surfaced via `log::warn!`, never silently swallowed.
fn set_writer_nonblocking(writer: &std::io::PipeWriter) {
    use std::os::fd::AsRawFd;
    let fd = writer.as_raw_fd();
    // SAFETY: fcntl on a live owned fd; F_GETFL/F_SETFL take/return the flag word.
    unsafe {
        let flags = libc::fcntl(fd, libc::F_GETFL);
        if flags < 0 {
            log::warn!(
                "could not read the password channel's flags (F_GETFL): {}; leaving it blocking",
                std::io::Error::last_os_error()
            );
            return;
        }
        if libc::fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK) < 0 {
            log::warn!(
                "could not set the password channel non-blocking (F_SETFL): {}; leaving it blocking",
                std::io::Error::last_os_error()
            );
        }
    }
}

/// Block until `fd` is writable, or report that the reader hung up. The `-1` timeout is a
/// real readiness wait on a genuine fd event (no time-based polling). `Ok(true)` =
/// writable, `Ok(false)` = the reader closed / errored (`POLLHUP`/`POLLERR`).
fn wait_writable(fd: std::os::fd::RawFd) -> Result<bool, std::io::Error> {
    loop {
        let mut pfd = libc::pollfd {
            fd,
            events: libc::POLLOUT,
            revents: 0,
        };
        // SAFETY: one initialized pollfd; `-1` blocks until a readiness event.
        let rc = unsafe { libc::poll(&mut pfd as *mut libc::pollfd, 1, -1) };
        if rc < 0 {
            let err = std::io::Error::last_os_error();
            if err.kind() == std::io::ErrorKind::Interrupted {
                continue; // EINTR: reissue the wait (no arbitrary retry bound)
            }
            return Err(err);
        }
        if pfd.revents & (libc::POLLHUP | libc::POLLERR) != 0 {
            return Ok(false);
        }
        if pfd.revents & libc::POLLOUT != 0 {
            return Ok(true);
        }
    }
}

fn auth_failed(detail: String) -> Error {
    Error::Elevation {
        kind: ElevationErrorKind::AuthFailed,
        detail,
    }
}

/// Write `bytes` to the non-blocking `writer` with a `write()` LOOP (never `write_all`,
/// which can return `WouldBlock` AFTER a partial write and truncate the password). A
/// WouldBlock/BrokenPipe with ZERO bytes written means the backend never read fd0
/// (cached credentials) → success; the SAME error after a partial write is a real
/// truncation → `AuthFailed`.
fn write_password_bytes(writer: &mut std::io::PipeWriter, fd: std::os::fd::RawFd, bytes: &[u8]) -> Result<(), Error> {
    use std::io::Write;
    let mut written = 0usize;
    while written < bytes.len() {
        match writer.write(&bytes[written..]) {
            Ok(0) => {
                return if written == 0 {
                    log::debug!("elevation backend did not consume the password (fd0 accepted nothing)");
                    Ok(())
                } else {
                    Err(auth_failed(
                        "elevation backend closed the password channel after a partial write".into(),
                    ))
                };
            }
            Ok(n) => written += n,
            Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
            Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => {
                if written == 0 {
                    log::debug!("elevation backend did not consume the password (fd0 would block, no read): {e}");
                    return Ok(());
                }
                match wait_writable(fd) {
                    Ok(true) => continue,
                    Ok(false) => {
                        return Err(auth_failed(
                            "elevation backend closed fd0 after a partial password write".into(),
                        ))
                    }
                    Err(pe) => {
                        return Err(auth_failed(format!(
                            "waiting to deliver the sudo -S password failed: {pe}"
                        )))
                    }
                }
            }
            Err(e) if e.kind() == std::io::ErrorKind::BrokenPipe => {
                if written == 0 {
                    log::debug!("elevation backend did not consume the password (fd0 closed / EPIPE): {e}");
                    return Ok(());
                }
                return Err(auth_failed(format!(
                    "elevation backend closed fd0 after a partial password write: {e}"
                )));
            }
            Err(e) => return Err(auth_failed(format!("could not deliver the sudo -S password: {e}"))),
        }
    }
    Ok(())
}

impl PendingPassword {
    /// Deliver the password + newline, then EOF. RACE-HARDENED: a cached-credential /
    /// NOPASSWD sudo never reads fd0, so the writer is non-blocking and a `BrokenPipe`
    /// (`EPIPE`) or `WouldBlock` with nothing yet written means "the backend did not need
    /// the password" → `log::debug!` + `Ok`, NOT `AuthFailed`. The buffer is zeroized and
    /// the writer dropped (EOF) on EVERY path. On a genuine failure the CALLER (the spawn
    /// arm) kills and reaps the running child.
    pub(crate) fn write_after_spawn(mut self) -> Result<(), Error> {
        use std::os::fd::AsRawFd;
        let mut bytes = password_line(self.secret.expose());
        set_writer_nonblocking(&self.writer);
        let fd = self.writer.as_raw_fd();
        let result = write_password_bytes(&mut self.writer, fd, &bytes);
        bytes.zeroize();
        drop(self.writer); // EOF after the password line
        result
    }
}

/// Outcome of a POSIX rewrite. `derived` is the command to spawn (the backend wrapper, or
/// — when already elevated — the sanitized original). `report` is attached to the
/// resulting `Child`. `password_write` is delivered after spawn. `backend_path` is the
/// resolved argv[0] the spawn arm passes to `remap_derived_spawn_error` (`None` when no
/// backend wraps the child, i.e. already elevated).
pub(crate) struct PosixRewrite {
    pub derived: Option<Command>,
    pub report: Option<ElevationReport>,
    pub password_write: Option<PendingPassword>,
    pub backend_path: Option<PathBuf>,
}

/// Collect the explicitly-`Set` env into an ordered (k,v) list (later `Set`s win).
/// `Remove`/`Clear` are rejected before this runs, so only `Set` survives.
fn explicit_set_env(ops: &[EnvOp]) -> Vec<(OsString, OsString)> {
    let mut map: std::collections::BTreeMap<OsString, OsString> = std::collections::BTreeMap::new();
    for op in ops {
        if let EnvOp::Set(k, v) = op {
            map.insert(k.clone(), v.clone());
        }
    }
    map.into_iter().collect()
}

/// Program + args, honoring `executable()`. An argv[0] distinct from a set
/// `executable()` cannot survive the backend wrapper → `Unsupported`.
fn program_and_args(cmd: &Command) -> Result<(OsString, Vec<OsString>), Error> {
    // `Empty` is matched FIRST. A fresh `Command` is `CommandInput::Empty`, not
    // `Argv(vec![])`, so folding it into the commandline arm would answer "no
    // program set" with a message about re-quoting a command line that was never set.
    let empty = || Error::Unsupported {
        op: "elevation of an empty command".into(),
        platform: "unix",
        detail: "set a program via .args([...]) before .elevate()".into(),
    };
    let argv =
        match cmd.input() {
            CommandInput::Argv(argv) => argv,
            CommandInput::Empty => return Err(empty()),
            CommandInput::CommandLine(_) => return Err(Error::Unsupported {
                op: "elevation of a commandline() command".into(),
                platform: "unix",
                detail:
                    "elevation requires an argv command (set .args([...])); a raw command line cannot be safely wrapped"
                        .into(),
            }),
        };
    if argv.is_empty() {
        return Err(empty());
    }
    match cmd.executable_path() {
        Some(exe) => {
            if argv[0].as_os_str() != exe.as_os_str() {
                return Err(Error::Unsupported {
                    op: "elevation with an argv[0] distinct from executable()".into(),
                    platform: "unix",
                    detail: "the backend runs the loaded file with argv[0] = its path; a separate argv[0] cannot survive elevation".into(),
                });
            }
            Ok((exe.as_os_str().to_os_string(), argv[1..].to_vec()))
        }
        None => Ok((argv[0].clone(), argv[1..].to_vec())),
    }
}

/// Structural request-validation, evaluated against the REQUESTED backend so the verdict
/// is privilege-independent. Run BEFORE the already-elevated short-circuit, so an
/// already-elevated caller gets the same rejection. (Backend availability + NoTty are
/// environmental and stay in the planner, after the short-circuit.)
fn reject_structural_posix_config(cmd: &Command, backend: Backend, auth: &Auth) -> Result<(), Error> {
    // commandline() / empty / distinct-argv0.
    program_and_args(cmd)?;
    if cmd.fds().keys().any(|f| f.raw() >= 3) {
        return Err(Error::Unsupported {
            op: "fd >= 3 on an elevated POSIX child".into(),
            platform: "unix",
            detail: "sudo/pkexec closefrom and run0's PID-1 reparent drop fds > 2; fd >= 3 needs the (deferred) broker"
                .into(),
        });
    }
    let ops = cmd.env_ops();
    if ops.iter().any(|o| matches!(o, EnvOp::Remove(_) | EnvOp::Clear)) {
        return Err(Error::Unsupported {
            op: ".env_remove()/.env_clear() + elevate".into(),
            platform: "unix",
            detail: "the backend builds the elevated base environment; the crate can add but not subtract from it"
                .into(),
        });
    }
    if ops.iter().any(|o| matches!(o, EnvOp::Set(..))) && matches!(backend, Backend::Doas | Backend::Pkexec) {
        return Err(Error::Unsupported {
            op: format!(".env() + Backend::{backend:?}"),
            platform: "unix",
            detail: "doas and pkexec expose no environment-forwarding mechanism; .env()/.envs() cannot cross them"
                .into(),
        });
    }
    if backend == Backend::Run0 && cmd.contain_request().mode.is_some() {
        return Err(Error::Unsupported {
            op: ".contain() + Backend::Run0".into(),
            platform: "unix",
            detail:
                "run0 runs the target as a PID 1-parented transient unit outside our cgroup; containment cannot span it"
                    .into(),
        });
    }
    if matches!(auth, Auth::Stdin(_)) && cmd.fds().contains_key(&Fd::STDIN) {
        return Err(Error::Unsupported {
            op: "Auth::Stdin with a caller-configured stdin".into(),
            platform: "unix",
            detail: "Auth::Stdin consumes fd0 to feed sudo -S the password; do not also configure stdin".into(),
        });
    }
    Ok(())
}

/// Transfer the caller's cwd / containment / kill-on-drop onto the derived command.
/// Does NOT suppress the fd marker — that is only correct for a real wrapper spawn
/// (`ElevatePosix`), whose `closefrom` destroys it; `RunAsIs`'s derived command spawns the
/// original program directly, with no wrapper to destroy anything, so its caller must
/// suppress explicitly if that arm ever needs to.
fn transfer_process_attrs(derived: &mut Command, cmd: &Command) {
    if let Some(d) = cmd.cwd() {
        derived.current_dir(d);
    }
    derived.set_contain(cmd.contain_request());
    derived.kill_on_drop(cmd.kill_on_drop_flag());
}

/// Detect-then-plan-then-rewrite. Thin wrapper over the pure form.
// Consumed by the sync POSIX spawn arm (`crate::child::spawn::spawn`); the pure
// `rewrite_with_host` is what the tests drive directly.
pub(crate) fn rewrite(cmd: &mut Command) -> Result<PosixRewrite, Error> {
    rewrite_with_host(cmd, &Host::detect())
}

/// PURE given `host`: gate + plan + sanitize + build a DERIVED command. The caller's
/// `Command` `input`/`env_ops` are left untouched (non-destructive): the caller's fd 0-2
/// stdio is MOVED into the derived command (`ResolvedStdio::File` is not `Clone`).
pub(crate) fn rewrite_with_host(cmd: &mut Command, host: &Host) -> Result<PosixRewrite, Error> {
    let requested_backend = cmd.elevation_request().backend;
    let requested_auth = cmd.elevation_request().auth.clone();

    // Structural config gates FIRST — privilege-independent (before the short-circuit).
    //
    // Which gate applies is a property of the REQUEST, not the run — see
    // `is_macos_gui_auto`'s doc and the ambient-privilege invariant
    // `structural_posix` documents. Keying on the resolved `Transition` instead
    // would break it: `plan()` yields `RunAsIs` under root, so the same request
    // would be accepted as root and rejected as a normal user.
    let macos_gui = super::plan::is_macos_gui_auto(host.os, requested_backend, &requested_auth);
    if macos_gui {
        super::macos::reject_structural_gui_config(cmd)?;
    } else {
        reject_structural_posix_config(cmd, requested_backend, &requested_auth)?;
    }

    match host.plan(Privilege::Elevated, requested_backend, requested_auth) {
        Transition::Reject { error } => Err(error),
        Transition::ElevateWindows { .. } => unreachable!("planner never yields ElevateWindows on a unix host"),
        Transition::ElevateMacosGui { osascript, arg_max } => {
            let (derived, report) = super::macos::build_rewrite(cmd, &osascript, arg_max)?;
            Ok(PosixRewrite {
                derived: Some(derived),
                report: Some(report),
                password_write: None,
                // The derived program IS osascript, so an exec failure remaps to
                // BackendUnavailable exactly as it does for the POSIX backends.
                backend_path: Some(osascript),
            })
        }
        Transition::RunAsIs => {
            // Already elevated: no wrapper, but the sanitizer STILL runs so a dangerous
            // forwarded var never reaches the root child. Build a non-destructive derived
            // command (the ORIGINAL program + args, sanitized env, fds MOVED).
            let (kept, stripped) = cmd.elevation_request().sanitizer.apply(explicit_set_env(cmd.env_ops()));
            let (program, args) = program_and_args(cmd)?;
            let mut argv = Vec::with_capacity(args.len() + 1);
            argv.push(program);
            argv.extend(args);
            let env_ops: Vec<EnvOp> = kept.iter().map(|(k, v)| EnvOp::Set(k.clone(), v.clone())).collect();
            let mut derived = Command::new();
            derived.set_input_argv(argv);
            derived.set_env_ops(env_ops);
            transfer_process_attrs(&mut derived, cmd);
            for (slot, resolved) in std::mem::take(cmd.fds_mut()) {
                derived.fds_mut().insert(slot, resolved);
            }
            Ok(PosixRewrite {
                derived: Some(derived),
                report: Some(ElevationReport {
                    via: ElevatedVia::AlreadyElevated,
                    stripped_env: stripped,
                    stdio: ElevatedStdio::Passthrough,
                }),
                password_write: None,
                backend_path: None,
            })
        }
        Transition::ElevatePosix { backend, path, auth } => {
            let (kept, stripped) = cmd.elevation_request().sanitizer.apply(explicit_set_env(cmd.env_ops()));
            // Backend resolved via Auto may land on a non-sudo target that cannot forward
            // env (the requested-backend gate only catches the EXPLICIT doas/pkexec case).
            if !kept.is_empty() && matches!(backend, Backend::Doas | Backend::Pkexec) {
                return Err(Error::Unsupported {
                    op: format!(".env() + Backend::{backend:?} (resolved via Auto)"),
                    platform: "unix",
                    detail:
                        "doas and pkexec expose no environment-forwarding mechanism; .env()/.envs() cannot cross them"
                            .into(),
                });
            }
            let (program, args) = program_and_args(cmd)?;
            let argv = build_argv(backend, path.as_os_str(), &auth, &program, &args, &kept)?;

            // --- build the DERIVED command (the caller's Command stays intact) ---
            let mut new_ops: Vec<EnvOp> = Vec::new();
            if backend == Backend::Sudo {
                // sudo preserves these from its OWN env (named in --preserve-env); run0
                // carried them in argv already; doas/pkexec were rejected above.
                for (k, v) in &kept {
                    new_ops.push(EnvOp::Set(k.clone(), v.clone()));
                }
            }
            if let Auth::Askpass(p) = &auth {
                new_ops.push(EnvOp::Set(OsString::from("SUDO_ASKPASS"), p.as_os_str().to_os_string()));
            }
            let mut derived = Command::new();
            derived.set_input_argv(argv);
            derived.set_env_ops(new_ops);
            transfer_process_attrs(&mut derived, cmd);
            // Only THIS arm's derived command is a real wrapper spawn (`sudo`/`doas`/`pkexec`
            // …): its `closefrom` destroys an installed marker, so the marker must not be
            // installed on it at all. `RunAsIs` spawns the original program with no wrapper —
            // suppressing there would claim a false justification (see `transfer_process_attrs`).
            derived.suppress_fd_marker();

            // Auth::Stdin: wire the derived fd0 to a fresh pipe's read end; the password is
            // written after spawn (the fd0 conflict was rejected in the structural gate).
            let mut password_write = None;
            if let Auth::Stdin(secret) = &auth {
                let (reader, writer) = std::io::pipe().map_err(Error::Io)?;
                let reader_file = File::from(OwnedFd::from(reader));
                derived.stdin(Stdio::from_file(reader_file))?;
                password_write = Some(PendingPassword {
                    writer,
                    secret: secret.clone(),
                });
            }

            // Move the caller's fd 0-2 stdio into the derived command (File is not Clone).
            // Skip fd0 when Auth::Stdin already wired it to the pipe read end.
            for (slot, resolved) in std::mem::take(cmd.fds_mut()) {
                if password_write.is_some() && slot == Fd::STDIN {
                    continue;
                }
                derived.fds_mut().insert(slot, resolved);
            }

            let stdio = if matches!(auth, Auth::Stdin(_)) {
                ElevatedStdio::StdinConsumed
            } else {
                ElevatedStdio::Passthrough
            };
            Ok(PosixRewrite {
                derived: Some(derived),
                report: Some(ElevationReport {
                    via: ElevatedVia::Wrapped(backend),
                    stripped_env: stripped,
                    stdio,
                }),
                password_write,
                backend_path: Some(path),
            })
        }
    }
}

#[cfg(test)]
#[path = "posix_tests.rs"]
mod posix_tests;