Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

feat(cua-driver-rs): become own responsible process for true TCC status (#1956)

Francesco Bonaccid61ce0197ee4
diff --git a/libs/cua-driver/rust/crates/cua-driver-core/src/lib.rs b/libs/cua-driver/rust/crates/cua-driver-core/src/lib.rs
index db9c2e8e37..515773bffb 100644
--- a/libs/cua-driver/rust/crates/cua-driver-core/src/lib.rs
+++ b/libs/cua-driver/rust/crates/cua-driver-core/src/lib.rs
@@ -10,6 +10,8 @@
//! - Each request has `jsonrpc: "2.0"`, `id` (any), `method`, optional `params`
//! - Notifications (no `id`) are silently ignored
+pub const RESPONSIBILITY_DISCLAIMED_ENV: &str = "CUA_DRIVER_RS_RESPONSIBILITY_DISCLAIMED";
+
pub mod cdp;
pub mod element_cache;
pub mod ffmpeg_install;
diff --git a/libs/cua-driver/rust/crates/cua-driver/src/cli.rs b/libs/cua-driver/rust/crates/cua-driver/src/cli.rs
index fb74f57ce6..6b053015be 100644
--- a/libs/cua-driver/rust/crates/cua-driver/src/cli.rs
+++ b/libs/cua-driver/rust/crates/cua-driver/src/cli.rs
@@ -1533,8 +1533,8 @@ pub fn run_permissions_cmd(
///
/// macOS attributes Accessibility / Screen-Recording to the *responsible
/// process*, so the ONLY process that can read `com.trycua.driver`'s real
-/// grants is the bundle daemon (launched via LaunchServices, reparented to
-/// launchd). When the daemon is up we query it and report its
+/// grants is the daemon running as its own responsible process. When the
+/// daemon is up we query it and report its
/// `driver-daemon`-attributed answer. When it is NOT up we deliberately
/// report `unknown` rather than fall back to an in-process check — that
/// fallback would report the *calling terminal's* grants and could print
@@ -1559,12 +1559,11 @@ fn run_permissions_status(json: bool) {
.filter(|r| r.ok)
.and_then(|r| r.result)
.and_then(|res| res.get("structuredContent").cloned())
- // Trust the booleans ONLY when the answering process is the launchd
- // bundle daemon (`driver-daemon`). A daemon spawned from a terminal
- // (`cua-driver serve` by hand) answers with `caller` attribution —
- // those are the terminal's grants, not the driver's, so we discard
- // them and fall through to `unknown` rather than report a false
- // `granted`. A missing `source` (non-macOS, no TCC) is trusted as-is.
+ // Trust the booleans ONLY when the answering daemon is its own
+ // responsible process (`driver-daemon`). Otherwise those grants
+ // belong to the launching app, so we discard them and fall through
+ // to `unknown`. A missing `source` (non-macOS, no TCC) is trusted
+ // as-is.
.filter(|s| {
s.get("source")
.and_then(|src| src.get("attribution"))
diff --git a/libs/cua-driver/rust/crates/cua-driver/src/main.rs b/libs/cua-driver/rust/crates/cua-driver/src/main.rs
index bbc0b77375..0af9eca6fa 100644
--- a/libs/cua-driver/rust/crates/cua-driver/src/main.rs
+++ b/libs/cua-driver/rust/crates/cua-driver/src/main.rs
@@ -30,6 +30,7 @@ mod cli;
mod doctor;
mod mcp_http;
mod proxy;
+mod responsibility;
mod serve;
mod skills;
mod telemetry;
@@ -226,6 +227,7 @@ fn main() {
return;
}
cli::Command::Serve { socket, no_permissions_gate, claude_code_compat } => {
+ responsibility::reexec_disclaimed_if_needed();
// Long-running daemon — kick off the background update check
// before any blocking work so the banner can land on stderr
// early in the serve lifecycle.
@@ -571,6 +573,7 @@ fn main() -> anyhow::Result<()> {
return Ok(());
}
cli::Command::Serve { socket, no_permissions_gate, claude_code_compat } => {
+ responsibility::reexec_disclaimed_if_needed();
// Long-running daemon — kick off the background update check
// before any blocking work so the banner can land on stderr.
version_check::maybe_announce_update();
diff --git a/libs/cua-driver/rust/crates/cua-driver/src/responsibility.rs b/libs/cua-driver/rust/crates/cua-driver/src/responsibility.rs
new file mode 100644
index 0000000000..2ab7ac0435
--- /dev/null
+++ b/libs/cua-driver/rust/crates/cua-driver/src/responsibility.rs
@@ -0,0 +1,183 @@
+#[cfg(target_os = "macos")]
+use std::ffi::CString;
+
+#[cfg(target_os = "macos")]
+use std::os::unix::ffi::{OsStrExt, OsStringExt};
+
+#[cfg(target_os = "macos")]
+unsafe extern "C" {
+ fn responsibility_spawnattrs_setdisclaim(
+ attr: *mut libc::posix_spawnattr_t,
+ disclaim: libc::c_int,
+ ) -> libc::c_int;
+}
+
+#[cfg(target_os = "macos")]
+pub fn already_disclaimed() -> bool {
+ std::env::var_os(cua_driver_core::RESPONSIBILITY_DISCLAIMED_ENV).is_some()
+}
+
+#[cfg(target_os = "macos")]
+pub fn reexec_disclaimed_if_needed() {
+ if already_disclaimed() {
+ return;
+ }
+ if crate::bundle::is_executable_inside_cuadriver_app() {
+ return;
+ }
+
+ let exe = match std::env::current_exe() {
+ Ok(exe) => exe,
+ Err(e) => {
+ tracing::debug!("responsibility disclaim skipped: current_exe failed: {e}");
+ return;
+ }
+ };
+
+ let exe_c = match CString::new(exe.as_os_str().as_bytes()) {
+ Ok(exe) => exe,
+ Err(e) => {
+ tracing::debug!("responsibility disclaim skipped: executable path contains NUL: {e}");
+ return;
+ }
+ };
+
+ let argv: Vec<CString> = match std::env::args_os()
+ .map(|arg| CString::new(arg.as_bytes()))
+ .collect::<Result<_, _>>()
+ {
+ Ok(argv) => argv,
+ Err(e) => {
+ tracing::debug!("responsibility disclaim skipped: argv contains NUL: {e}");
+ return;
+ }
+ };
+ let mut argv_ptrs: Vec<*mut libc::c_char> = argv
+ .iter()
+ .map(|arg| arg.as_ptr() as *mut libc::c_char)
+ .collect();
+ argv_ptrs.push(std::ptr::null_mut());
+
+ let env: Vec<CString> = match std::env::vars_os()
+ .map(|(key, value)| {
+ let mut entry = key.into_vec();
+ entry.push(b'=');
+ entry.extend(value.into_vec());
+ CString::new(entry)
+ })
+ .chain(std::iter::once(CString::new(format!(
+ "{}=1",
+ cua_driver_core::RESPONSIBILITY_DISCLAIMED_ENV
+ ))))
+ .collect::<Result<_, _>>()
+ {
+ Ok(env) => env,
+ Err(e) => {
+ tracing::debug!("responsibility disclaim skipped: environment contains NUL: {e}");
+ return;
+ }
+ };
+ let mut env_ptrs: Vec<*mut libc::c_char> = env
+ .iter()
+ .map(|entry| entry.as_ptr() as *mut libc::c_char)
+ .collect();
+ env_ptrs.push(std::ptr::null_mut());
+
+ let mut attr = std::mem::MaybeUninit::<libc::posix_spawnattr_t>::uninit();
+ let init_result = unsafe { libc::posix_spawnattr_init(attr.as_mut_ptr()) };
+ if init_result != 0 {
+ tracing::debug!(
+ "responsibility disclaim skipped: posix_spawnattr_init failed: {init_result}"
+ );
+ return;
+ }
+ let mut attr = unsafe { attr.assume_init() };
+
+ let disclaim_result = unsafe { responsibility_spawnattrs_setdisclaim(&mut attr, 1) };
+ if disclaim_result != 0 {
+ tracing::debug!(
+ "responsibility disclaim skipped: responsibility_spawnattrs_setdisclaim failed: {disclaim_result}"
+ );
+ unsafe {
+ libc::posix_spawnattr_destroy(&mut attr);
+ }
+ return;
+ }
+
+ let mut child_pid: libc::pid_t = 0;
+ let spawn_result = unsafe {
+ libc::posix_spawn(
+ &mut child_pid,
+ exe_c.as_ptr(),
+ std::ptr::null(),
+ &attr,
+ argv_ptrs.as_ptr(),
+ env_ptrs.as_ptr(),
+ )
+ };
+ unsafe {
+ libc::posix_spawnattr_destroy(&mut attr);
+ }
+
+ if spawn_result != 0 {
+ tracing::debug!("responsibility disclaim skipped: posix_spawn failed: {spawn_result}");
+ return;
+ }
+
+ // The disclaimed child is the real `serve` process. Block on it and mirror
+ // its exit status so the launch keeps its original foreground semantics:
+ // `serve` ran in-process before, so callers that wait on it, forward
+ // terminal signals to the foreground process group, or read `$?` still see
+ // the same behavior. The child shares our process group (no
+ // POSIX_SPAWN_SETPGROUP), so Ctrl-C reaches it directly.
+ let mut status: libc::c_int = 0;
+ loop {
+ let waited = unsafe { libc::waitpid(child_pid, &mut status, 0) };
+ if waited == -1 {
+ let err = std::io::Error::last_os_error();
+ if err.raw_os_error() == Some(libc::EINTR) {
+ continue;
+ }
+ // Can't reap the child (already reaped, or ECHILD). The child owns
+ // the daemon either way; exit cleanly rather than fall through and
+ // double-run `serve` in this process.
+ tracing::debug!("responsibility disclaim: waitpid failed: {err}");
+ std::process::exit(0);
+ }
+ break;
+ }
+
+ let code = if libc::WIFEXITED(status) {
+ libc::WEXITSTATUS(status)
+ } else if libc::WIFSIGNALED(status) {
+ 128 + libc::WTERMSIG(status)
+ } else {
+ 0
+ };
+ std::process::exit(code);
+}
+
+#[cfg(not(target_os = "macos"))]
+pub fn reexec_disclaimed_if_needed() {}
+
+#[cfg(all(test, target_os = "macos"))]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn already_disclaimed_reflects_env_var() {
+ let name = cua_driver_core::RESPONSIBILITY_DISCLAIMED_ENV;
+ let original = std::env::var_os(name);
+
+ std::env::remove_var(name);
+ assert!(!already_disclaimed());
+
+ std::env::set_var(name, "1");
+ assert!(already_disclaimed());
+
+ match original {
+ Some(value) => std::env::set_var(name, value),
+ None => std::env::remove_var(name),
+ }
+ }
+}
diff --git a/libs/cua-driver/rust/crates/platform-macos/src/tools/check_permissions.rs b/libs/cua-driver/rust/crates/platform-macos/src/tools/check_permissions.rs
index ec0d0a4a8b..51ad0a53e2 100644
--- a/libs/cua-driver/rust/crates/platform-macos/src/tools/check_permissions.rs
+++ b/libs/cua-driver/rust/crates/platform-macos/src/tools/check_permissions.rs
@@ -30,9 +30,8 @@ fn screen_recording_capturable() -> bool {
/// macOS attributes Accessibility / Screen-Recording to the *responsible
/// process* (the LaunchServices launching app), not the executable path.
/// So `check_permissions` answered in-process reflects:
-/// - the **CuaDriver daemon** (`com.trycua.driver`) when we're the bundle
-/// binary reparented to launchd (ppid == 1) — the real driver status;
-/// this is the forced bundle/daemon path.
+/// - the **CuaDriver daemon** (`com.trycua.driver`) when this process is
+/// its own responsible process — the real driver status.
/// - the **calling app** otherwise — e.g. the terminal/IDE that spawned
/// `cua-driver call …`. That grant is NOT the driver's, which is why a
/// standalone check can read `true` while `tccutil … com.trycua.driver`
@@ -45,14 +44,26 @@ fn permission_source() -> serde_json::Value {
.and_then(|p| std::fs::canonicalize(p).ok())
.and_then(|p| p.to_str().map(str::to_owned))
.unwrap_or_default();
- let is_driver_daemon =
- exe.contains("/CuaDriver.app/Contents/MacOS/") && ppid == 1;
+ // The trustworthy, non-spoofable signal is the executable path: a caller
+ // can't run from inside the code-signed `CuaDriver.app` bundle without
+ // controlling that install. The disclaim env var is caller-controlled, so
+ // it is treated only as a corroborating signal that explains why a
+ // bundle-resident daemon has `ppid != 1` (it re-exec'd itself with
+ // responsibility disclaim, so launchd is no longer its parent). On its own
+ // — outside the bundle — the env var must NOT grant daemon attribution, or
+ // a caller could pre-set it and spoof the TCC source. Fail closed to
+ // "caller" whenever the bundle signal is absent.
+ let inside_bundle = exe.contains("/CuaDriver.app/Contents/MacOS/");
+ let disclaimed =
+ std::env::var_os(cua_driver_core::RESPONSIBILITY_DISCLAIMED_ENV).is_some();
+ let is_driver_daemon = inside_bundle && (ppid == 1 || disclaimed);
let (attribution, note) = if is_driver_daemon {
(
"driver-daemon",
"These booleans reflect the CuaDriver daemon's own TCC identity \
- (com.trycua.driver) — the process that does the work.",
+ (com.trycua.driver) because this process is its own responsible \
+ process.",
)
} else {
(
@@ -164,3 +175,32 @@ impl Tool for CheckPermissionsTool {
}))
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn disclaim_env_var_alone_does_not_grant_daemon_attribution() {
+ // The disclaim env var is caller-controlled, so on its own it must not
+ // make `check_permissions` claim the booleans reflect the daemon's TCC
+ // identity. Daemon attribution additionally requires the binary to live
+ // inside the code-signed `CuaDriver.app` bundle — the test runner does
+ // not, so even with the env var present we must fail closed to "caller".
+ let name = cua_driver_core::RESPONSIBILITY_DISCLAIMED_ENV;
+ let original = std::env::var_os(name);
+
+ std::env::set_var(name, "1");
+ let source = permission_source();
+ assert_eq!(
+ source.get("attribution").and_then(|v| v.as_str()),
+ Some("caller"),
+ "env-var presence alone must not yield daemon attribution"
+ );
+
+ match original {
+ Some(value) => std::env::set_var(name, value),
+ None => std::env::remove_var(name),
+ }
+ }
+}