Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

mux-server: give the daemon its own macOS privacy identity

RoversX5ec88a2d2076
diff --git a/wezterm-mux-server/src/daemonize.rs b/wezterm-mux-server/src/daemonize.rs
index 739f9a21..c55e977c 100644
--- a/wezterm-mux-server/src/daemonize.rs
+++ b/wezterm-mux-server/src/daemonize.rs
@@ -204,3 +204,89 @@ pub fn set_cloexec(fd: RawFd, enable: bool) {
libc::fcntl(fd, libc::F_SETFD, flags);
}
}
+
+/// Replace this process with `program`, making the new image responsible
+/// for itself in macOS privacy (TCC) terms.
+///
+/// A daemon spawned by the GUI is otherwise attributed to that GUI: the
+/// first GUI's grant covers it, and once that GUI has exited the attribution
+/// dangles, so a Documents/Desktop Project is refused and the shell lands in
+/// `$HOME` "sometimes". Disclaiming gives the server one stable identity of
+/// its own, granted once. Only returns on failure, like `exec`.
+#[cfg(target_os = "macos")]
+pub fn exec_as_own_tcc_identity(
+ program: &std::ffi::OsStr,
+ args: &[std::ffi::OsString],
+) -> std::io::Error {
+ use std::ffi::CString;
+ use std::os::unix::ffi::OsStrExt;
+
+ type SetDisclaim = unsafe extern "C" fn(*mut libc::posix_spawnattr_t, libc::c_int) -> libc::c_int;
+ let symbol = unsafe {
+ libc::dlsym(
+ libc::RTLD_DEFAULT,
+ b"responsibility_spawnattrs_setdisclaim\0".as_ptr() as *const libc::c_char,
+ )
+ };
+ if symbol.is_null() {
+ return std::io::Error::new(
+ std::io::ErrorKind::Unsupported,
+ "responsibility_spawnattrs_setdisclaim is not available",
+ );
+ }
+ let set_disclaim: SetDisclaim = unsafe { std::mem::transmute(symbol) };
+
+ let c_program = match CString::new(program.as_bytes()) {
+ Ok(program) => program,
+ Err(err) => return std::io::Error::new(std::io::ErrorKind::InvalidInput, err),
+ };
+ let c_args: Vec<CString> = std::iter::once(program)
+ .chain(args.iter().map(|arg| arg.as_os_str()))
+ .filter_map(|arg| CString::new(arg.as_bytes()).ok())
+ .collect();
+ let c_env: Vec<CString> = std::env::vars_os()
+ .filter_map(|(key, value)| {
+ let mut entry = key.as_bytes().to_vec();
+ entry.push(b'=');
+ entry.extend_from_slice(value.as_bytes());
+ CString::new(entry).ok()
+ })
+ .collect();
+ let mut argv: Vec<*mut libc::c_char> =
+ c_args.iter().map(|arg| arg.as_ptr() as *mut _).collect();
+ argv.push(std::ptr::null_mut());
+ let mut envp: Vec<*mut libc::c_char> =
+ c_env.iter().map(|entry| entry.as_ptr() as *mut _).collect();
+ envp.push(std::ptr::null_mut());
+
+ unsafe {
+ let mut attr: libc::posix_spawnattr_t = std::mem::zeroed();
+ let rc = libc::posix_spawnattr_init(&mut attr);
+ if rc != 0 {
+ return std::io::Error::from_raw_os_error(rc);
+ }
+ // SETEXEC turns posix_spawn into exec-in-place; the disclaim applies
+ // to the replaced image.
+ let rc = libc::posix_spawnattr_setflags(&mut attr, libc::POSIX_SPAWN_SETEXEC as libc::c_short);
+ if rc != 0 {
+ libc::posix_spawnattr_destroy(&mut attr);
+ return std::io::Error::from_raw_os_error(rc);
+ }
+ let rc = set_disclaim(&mut attr, 1);
+ if rc != 0 {
+ libc::posix_spawnattr_destroy(&mut attr);
+ return std::io::Error::from_raw_os_error(rc);
+ }
+ let mut pid: libc::pid_t = 0;
+ let rc = libc::posix_spawn(
+ &mut pid,
+ c_program.as_ptr(),
+ std::ptr::null(),
+ &attr,
+ argv.as_ptr(),
+ envp.as_ptr(),
+ );
+ libc::posix_spawnattr_destroy(&mut attr);
+ std::io::Error::from_raw_os_error(rc)
+ }
+}
diff --git a/wezterm-mux-server/src/main.rs b/wezterm-mux-server/src/main.rs
index cba59418..b70ccc73 100644
--- a/wezterm-mux-server/src/main.rs
+++ b/wezterm-mux-server/src/main.rs
@@ -222,6 +222,18 @@ fn run() -> anyhow::Result<()> {
}
}
+ #[cfg(target_os = "macos")]
+ {
+ // The umask pre_exec above only runs through `exec`; apply
+ // it in place, since a disclaiming exec keeps this process.
+ if let Some(mask) = umask::UmaskSaver::saved_umask() {
+ unsafe { libc::umask(mask) };
+ }
+ let args: Vec<OsString> = cmd.get_args().map(|arg| arg.to_os_string()).collect();
+ let err = daemonize::exec_as_own_tcc_identity(cmd.get_program(), &args);
+ log::warn!("re-exec with own privacy identity failed, using exec: {err:#}");
+ }
+
return Err(anyhow::anyhow!("failed to re-exec: {:?}", cmd.exec()));
}
}