vtcode-bash-runner 0.181.0

Cross-platform shell execution helpers extracted from VT Code
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
//! Native command lifetime support for a binary-owned task cleanup endpoint.
//!
//! This is cleanup, not command admission: callers must validate the original
//! command and select its sandbox launcher before applying this helper.

use std::ffi::{OsStr, OsString};
use std::io::{self, Read};
use std::os::fd::AsRawFd;
use std::os::unix::net::UnixStream;
use std::os::unix::process::CommandExt;
use std::path::{Path, PathBuf};
use std::time::Duration;

/// Private task endpoint inherited by VT Code's managed command launchers.
pub const OWNER_SOCKET_ENV: &str = "VTCODE_COMPANION_OWNER_SOCKET";
/// Private native entry point shared with the binary-owned task launcher.
pub const HELPER_MODE: &str = "--vtcode-owned-exec";
/// Prefix of the private directory that contains the task owner endpoint.
pub const OWNER_DIRECTORY_PREFIX: &str = "vtc-";
/// Basename of the task owner endpoint inside its private directory.
pub const OWNER_SOCKET_FILE: &str = "owner";
const READY: u8 = 1;

/// Exact argv for the shipped native helper. Original argv is never interpreted
/// as shell syntax; `arg0` is restored when replacing the helper with the target.
pub struct OwnedCommand {
    pub executable: PathBuf,
    pub arguments: Vec<OsString>,
    pub owner_socket: PathBuf,
}

impl OwnedCommand {
    pub fn new(
        executable: &Path,
        owner_socket: &Path,
        program: &OsStr,
        args: impl IntoIterator<Item = impl AsRef<OsStr>>,
        arg0: Option<&OsStr>,
    ) -> Self {
        let mut arguments = vec![
            OsString::from(HELPER_MODE),
            arg0.unwrap_or(program).to_owned(),
            program.to_owned(),
        ];
        arguments.extend(args.into_iter().map(|arg| arg.as_ref().to_owned()));
        Self {
            executable: executable.to_owned(),
            arguments,
            owner_socket: owner_socket.to_owned(),
        }
    }

    /// Wrap `program` when this process is itself a managed command whose
    /// private task endpoint is still present.
    ///
    /// [`OWNER_SOCKET_ENV`] is inherited from the process environment, so a
    /// spurious, stale, or forged value must not be honored: wrapping an
    /// endpoint this process does not own fails closed inside the helper and
    /// would disable every managed command. A value that is not a live
    /// [`OWNER_SOCKET_ENV`] endpoint created by this user's task supervisor is
    /// therefore ignored, and the command spawns normally.
    pub fn from_environment(
        program: &OsStr,
        args: impl IntoIterator<Item = impl AsRef<OsStr>>,
        arg0: Option<&OsStr>,
    ) -> io::Result<Option<Self>> {
        let Some(socket) = std::env::var_os(OWNER_SOCKET_ENV) else {
            return Ok(None);
        };
        let socket = PathBuf::from(socket);
        if !is_owned_endpoint(&socket) {
            return Ok(None);
        }
        Ok(Some(Self::new(&std::env::current_exe()?, &socket, program, args, arg0)))
    }
}

/// Whether `socket` belongs to a live task owner created by this user.
///
/// Provenance is structural, matching how the supervisor creates the endpoint:
/// a Unix socket named [`OWNER_SOCKET_FILE`] directly inside a directory that
/// is prefixed [`OWNER_DIRECTORY_PREFIX`], owned by the current user, granted
/// owner-only permissions, and located under the system temp root. Every part
/// must hold, so a path the supervisor could not have produced is rejected.
///
/// Exposed so the binary-owned supervisor can assert that the endpoint it
/// publishes satisfies the launcher's provenance contract.
pub fn is_owned_endpoint(socket: &Path) -> bool {
    is_owned_endpoint_under(socket, &std::env::temp_dir())
}

/// [`is_owned_endpoint`] with an explicit temp root, so the containment gate is
/// testable without mutating the process environment.
fn is_owned_endpoint_under(socket: &Path, temp_root: &Path) -> bool {
    use std::os::unix::fs::{FileTypeExt, MetadataExt, PermissionsExt};

    if !socket.is_absolute() || socket.file_name() != Some(OsStr::new(OWNER_SOCKET_FILE)) {
        return false;
    }
    // `symlink_metadata` keeps a symlink from standing in for a real endpoint.
    let Ok(endpoint) = std::fs::symlink_metadata(socket) else {
        return false;
    };
    if !endpoint.file_type().is_socket() {
        return false;
    }
    let Some(directory) = socket.parent() else {
        return false;
    };
    let Ok(directory_metadata) = std::fs::metadata(directory) else {
        return false;
    };
    let owner_only = directory_metadata.permissions().mode() & 0o077 == 0;
    let owned_by_self = directory_metadata.is_dir() && directory_metadata.uid() == nix::unistd::geteuid().as_raw();
    if !owner_only || !owned_by_self {
        return false;
    }
    if !directory
        .file_name()
        .and_then(OsStr::to_str)
        .is_some_and(|name| name.starts_with(OWNER_DIRECTORY_PREFIX))
    {
        return false;
    }
    let Ok(directory) = vtcode_commons::canonicalize(directory) else {
        return false;
    };
    let temp_root = vtcode_commons::canonicalize(temp_root).unwrap_or_else(|_| temp_root.to_path_buf());
    directory.starts_with(temp_root)
}

/// Dispatch before agent environment hardening, worker threads, tracing, or the async runtime.
/// A successful helper replaces itself with the admitted program and does not
/// return. A missing/closed endpoint fails before the target can execute.
pub fn maybe_run_owned_command() -> io::Result<bool> {
    let args: Vec<_> = std::env::args_os().skip(1).collect();
    if args.first().is_none_or(|arg| arg != HELPER_MODE) {
        return Ok(false);
    }
    let arg0 = args
        .get(1)
        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "missing target argv[0]"))?;
    let program = args
        .get(2)
        .filter(|p| !p.is_empty())
        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "missing target program"))?;
    if nix::unistd::getpgrp() != nix::unistd::getpid() {
        return Err(io::Error::new(io::ErrorKind::InvalidInput, "owned command requires an isolated process group"));
    }
    let socket = std::env::var_os(OWNER_SOCKET_ENV)
        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "missing task owner endpoint"))?;
    let mut owner = UnixStream::connect(socket)?;
    owner.set_read_timeout(Some(Duration::from_secs(2)))?;
    let mut acknowledgement = [0];
    owner.read_exact(&mut acknowledgement)?;
    if acknowledgement != [READY] {
        return Err(io::Error::new(io::ErrorKind::InvalidData, "invalid task owner acknowledgement"));
    }
    // The monitor has no readiness timeout: it waits for EOF throughout the
    // command's lifetime. The two-second timeout only bounds startup above.
    owner.set_read_timeout(None)?;
    spawn_watchdog(&owner)?;
    drop(owner);
    // Ancestors may hold descriptors without CLOEXEC (observed: a system
    // kernel-control socket inherited at fd 24 flaking lifetime tests), so
    // the helper enforces a clean table itself instead of trusting ambient
    // CLOEXEC hygiene. Only stdio must survive: owner and watchdog channels
    // are already dropped above.
    close_all_above_stdio();
    let error = std::process::Command::new(program).arg0(arg0).args(args.iter().skip(3)).exec();
    Err(error)
}

/// Close every inherited descriptor above stdio, preserving 0, 1, and 2.
///
/// Call only when no Rust-owned descriptor above stdio may still be live;
/// the helper drops its owner and watchdog channels before exec, so by that
/// point every surviving fd is inherited junk. Sweeps to the process file
/// limit (floored at 256 so the checked range is always covered, capped at
/// 65536 to bound worst-case time); descriptors above a lowered limit are a
/// pathological case this best-effort sweep does not chase.
#[expect(
    unsafe_code,
    reason = "getrlimit/close take validated arguments; no Rust-owned fds above stdio are live here"
)]
fn close_all_above_stdio() {
    let mut limit = libc::rlimit { rlim_cur: 0, rlim_max: 0 };
    // SAFETY: `getrlimit` writes only to the caller-owned `limit` out-param.
    let ceiling = if unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, &mut limit) } == 0 {
        limit.rlim_cur.clamp(256, 65536) as libc::c_int
    } else {
        8192
    };
    for fd in 3..ceiling {
        // SAFETY: `close` on an arbitrary small fd number is always sound;
        // invalid numbers fail with EBADF and stdio is out of range.
        unsafe {
            libc::close(fd);
        }
    }
}

#[expect(
    unsafe_code,
    reason = "Fork children execute only async-signal-safe syscalls and exit without Rust destructors"
)]
fn spawn_watchdog(owner: &UnixStream) -> io::Result<()> {
    let (mut ready_reader, ready_writer) = UnixStream::pair()?;
    ready_reader.set_read_timeout(Some(Duration::from_secs(2)))?;
    let owner_fd = owner.as_raw_fd();
    let ready_fd = ready_writer.as_raw_fd();
    if owner_fd <= 2 || ready_fd <= 2 || ready_reader.as_raw_fd() <= 2 {
        return Err(io::Error::new(io::ErrorKind::InvalidInput, "owned command requires open standard streams"));
    }
    let group = nix::unistd::getpgrp().as_raw();
    // SAFETY: The child branches use only fork, close, sigaction, read, write,
    // kill, and _exit. They never allocate, lock, unwind, or run Rust drops.
    let intermediate = unsafe { libc::fork() };
    if intermediate == -1 {
        return Err(io::Error::last_os_error());
    }
    if intermediate == 0 {
        // SAFETY: The child branches below use only async-signal-safe calls
        // and never return to Rust. Double-forking keeps shell wait independent.
        let monitor = unsafe { libc::fork() };
        if monitor == -1 {
            // SAFETY: Exit immediately without running fork-inherited drops.
            unsafe { libc::_exit(1) };
        }
        if monitor != 0 {
            // SAFETY: The intermediate child has no further work or cleanup.
            unsafe { libc::_exit(0) };
        }
        // SAFETY: Close only this child's copy of the validated socket fd.
        unsafe { libc::close(ready_reader.as_raw_fd()) };
        // SAFETY: Valid inherited fds and the isolated positive group ID were
        // established before fork. The monitor cannot return or run drops.
        unsafe { run_watchdog(owner_fd, ready_fd, group) };
    }
    drop(ready_writer);
    let mut status = 0;
    loop {
        // SAFETY: `status` is writable and this waits only for our intermediate
        // child. The monitor is reparented and never belongs to the command's
        // child-wait relationship.
        let waited = unsafe { libc::waitpid(intermediate, &mut status, 0) };
        if waited == intermediate {
            break;
        }
        let error = io::Error::last_os_error();
        if error.kind() != io::ErrorKind::Interrupted {
            return Err(error);
        }
    }
    if !libc::WIFEXITED(status) || libc::WEXITSTATUS(status) != 0 {
        return Err(io::Error::other("failed to start command lifetime watchdog"));
    }
    let mut ready = [0];
    ready_reader.read_exact(&mut ready)?;
    if ready != [READY] {
        return Err(io::Error::other("command lifetime watchdog did not initialize"));
    }
    Ok(())
}

/// # Safety
/// Called only in the double-forked child with valid borrowed socket fds above
/// stdio and a positive, isolated group ID. Never returns or drops Rust values.
#[expect(
    unsafe_code,
    reason = "Minimal fork-child watchdog uses only async-signal-safe POSIX calls"
)]
unsafe fn run_watchdog(owner_fd: libc::c_int, ready_fd: libc::c_int, group: libc::pid_t) -> ! {
    // SAFETY: POSIX sigaction has a valid all-zero representation before its
    // handler and mask are initialized below.
    let mut ignore: libc::sigaction = unsafe { std::mem::zeroed() };
    ignore.sa_sigaction = libc::SIG_IGN;
    // SAFETY: The pointer addresses a writable stack-owned signal mask.
    unsafe { libc::sigemptyset(&mut ignore.sa_mask) };
    for signal in [libc::SIGINT, libc::SIGTERM, libc::SIGHUP, libc::SIGPIPE] {
        // SAFETY: Supported signal numbers and a fully initialized action;
        // ignoring cancellation pins group identity until SIGKILL or owner EOF.
        if unsafe { libc::sigaction(signal, &ignore, std::ptr::null_mut()) } == -1 {
            // SAFETY: Failed monitor initialization must not run Rust drops.
            unsafe { libc::_exit(1) };
        }
    }
    for fd in 0..=2 {
        // SAFETY: Close only this child's standard descriptors, never the
        // validated lifetime socket fds above stdio.
        unsafe { libc::close(fd) };
    }
    let ready = READY;
    // SAFETY: The borrowed socket fd is valid and the pointer addresses one
    // initialized byte on this child's stack.
    if unsafe { libc::write(ready_fd, std::ptr::addr_of!(ready).cast(), 1) } != 1 {
        // SAFETY: Failed readiness must exit without Rust drops.
        unsafe { libc::_exit(1) };
    }
    // SAFETY: Only this child's readiness descriptor copy is closed.
    unsafe { libc::close(ready_fd) };
    let mut byte = 0u8;
    loop {
        // SAFETY: The owner fd remains valid; the pointer is a writable byte.
        let read = unsafe { libc::read(owner_fd, std::ptr::addr_of_mut!(byte).cast(), 1) };
        if read == -1 && nix::errno::Errno::last() == nix::errno::Errno::EINTR {
            continue;
        }
        // EOF, invalid protocol data, and I/O errors all fail closed.
        // SAFETY: This monitor remains in the positive isolated group it pins;
        // negating that group targets only its own managed command group.
        unsafe { libc::kill(-group, libc::SIGKILL) };
        // SAFETY: Even if kill fails, never return into fork-inherited Rust.
        unsafe { libc::_exit(1) };
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use assert_fs::TempDir;
    use std::os::unix::fs::{PermissionsExt, symlink};
    use std::os::unix::net::UnixListener;

    fn private_directory(root: &Path, name: &str) -> PathBuf {
        let path = root.join(name);
        std::fs::create_dir(&path).expect("create private directory");
        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o700)).expect("owner-only directory");
        path
    }

    fn bind_endpoint(directory: &Path, name: &str) -> PathBuf {
        let socket = directory.join(name);
        // The bound path stays a socket file after the listener is dropped.
        drop(UnixListener::bind(&socket).expect("bind endpoint"));
        socket
    }

    #[test]
    fn accepts_a_supervisor_shaped_endpoint() {
        let root = TempDir::new().expect("temp root");
        let directory = private_directory(root.path(), "vtc-accepted");
        let socket = bind_endpoint(&directory, OWNER_SOCKET_FILE);

        assert!(is_owned_endpoint(&socket));
        assert!(is_owned_endpoint_under(&socket, &std::env::temp_dir()));
    }

    #[test]
    fn ignores_a_stale_or_invalid_endpoint() {
        let root = TempDir::new().expect("temp root");
        let missing = root.path().join("vtc-stale").join(OWNER_SOCKET_FILE);
        // A stale value points at an endpoint the supervisor has removed.
        assert!(!is_owned_endpoint(&missing));

        let directory = private_directory(root.path(), "vtc-stale");
        std::fs::write(&missing, b"not a socket").expect("regular file");
        assert!(!is_owned_endpoint(&missing));

        let _foreign = bind_endpoint(&directory, "endpoint");
        assert!(!is_owned_endpoint(&directory.join("endpoint")));
        assert!(!is_owned_endpoint(Path::new(OWNER_SOCKET_FILE)));
    }

    #[test]
    fn ignores_an_endpoint_outside_a_private_directory() {
        let root = TempDir::new().expect("temp root");
        let directory = root.path().join("vtc-public");
        std::fs::create_dir(&directory).expect("public directory");
        std::fs::set_permissions(&directory, std::fs::Permissions::from_mode(0o755)).expect("world-accessible");
        let socket = bind_endpoint(&directory, OWNER_SOCKET_FILE);

        assert!(!is_owned_endpoint(&socket));
    }

    #[test]
    fn ignores_a_directory_the_supervisor_did_not_name() {
        let root = TempDir::new().expect("temp root");
        let directory = private_directory(root.path(), "other-owner");
        let socket = bind_endpoint(&directory, OWNER_SOCKET_FILE);

        assert!(!is_owned_endpoint(&socket));
    }

    #[test]
    fn ignores_a_symlinked_endpoint() {
        let root = TempDir::new().expect("temp root");
        let target = bind_endpoint(root.path(), "target.sock");
        let directory = private_directory(root.path(), "vtc-symlinked");
        let socket = directory.join(OWNER_SOCKET_FILE);
        symlink(&target, &socket).expect("symlink endpoint");

        assert!(!is_owned_endpoint(&socket));
    }

    #[test]
    fn ignores_an_endpoint_outside_the_temp_root() {
        let root = TempDir::new().expect("temp root");
        let other_root = TempDir::new().expect("other root");
        let directory = private_directory(root.path(), "vtc-containment");
        let socket = bind_endpoint(&directory, OWNER_SOCKET_FILE);

        assert!(is_owned_endpoint(&socket));
        assert!(!is_owned_endpoint_under(&socket, other_root.path()));
    }
}