From 592dbffa22a1a4e8d029f5657543916d794efec5 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 19 Dec 2020 14:01:39 -0800 Subject: [PATCH] pty: avoid leaking random fds to children on macOS Big Sur I'm assuming this is Big Sur specified, as I hadn't noticed this until upgrading; there are a bunch of gui related files inherited by my shell: ``` $ lsof -p $$ COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME zsh 66016 wez cwd DIR 1,16 1760 396092 /Users/wez/wez-personal/wezterm zsh 66016 wez txt REG 1,16 1331248 1152921500312774717 /bin/zsh zsh 66016 wez txt REG 1,16 138640 1152921500312777271 /usr/lib/zsh/5.8/zsh/terminfo.so zsh 66016 wez txt REG 1,16 174352 1152921500312777285 /usr/lib/zsh/5.8/zsh/zutil.so zsh 66016 wez txt REG 1,16 138128 1152921500312777261 /usr/lib/zsh/5.8/zsh/rlimits.so zsh 66016 wez txt REG 1,16 579888 1152921500312777275 /usr/lib/zsh/5.8/zsh/zle.so zsh 66016 wez txt REG 1,16 341904 1152921500312777223 /usr/lib/zsh/5.8/zsh/complete.so zsh 66016 wez txt REG 1,16 174752 1152921500312777257 /usr/lib/zsh/5.8/zsh/parameter.so zsh 66016 wez txt REG 1,16 211584 1152921500312777225 /usr/lib/zsh/5.8/zsh/complist.so zsh 66016 wez txt REG 1,16 208768 1152921500312777227 /usr/lib/zsh/5.8/zsh/computil.so zsh 66016 wez txt REG 1,16 2528384 1152921500312776994 /usr/lib/dyld zsh 66016 wez 0u CHR 16,1 0t158834 1019 /dev/ttys001 zsh 66016 wez 1u CHR 16,1 0t158834 1019 /dev/ttys001 zsh 66016 wez 2u CHR 16,1 0t158834 1019 /dev/ttys001 zsh 66016 wez 8r REG 1,16 273022 1152921500312217452 /System/Library/Extensions/AppleMetalGLRenderer.bundle/Contents/Resources/default.metallib zsh 66016 wez 10u CHR 16,1 0t20923 1019 /dev/ttys001 zsh 66016 wez 11u REG 1,16 65536 576611 /private/var/folders/mg/hk5n1_9x7014sm0syzqzzn640000gn/C/com.github.wez.wezterm/com.apple.metal/16777235_322/functions.data zsh 66016 wez 12u REG 1,16 1276 2360034 /private/var/folders/mg/hk5n1_9x7014sm0syzqzzn640000gn/C/com.github.wez.wezterm/com.apple.metal/16777235_322/functions.list zsh 66016 wez 13r REG 1,16 215992 2354718 /Library/Application Support/CrashReporter/SubmitDiagInfo.domains zsh 66016 wez 14u REG 1,16 5232 2360053 /Users/wez/Library/Saved Application State/com.github.wez.wezterm.savedState/window_2.data zsh 66016 wez 15u REG 1,16 59040 2360054 /Users/wez/Library/Saved Application State/com.github.wez.wezterm.savedState/data.data zsh 66016 wez 16w REG 1,16 4716 2360055 /Users/wez/Library/Saved Application State/com.github.wez.wezterm.savedState/windows.plist ``` This commit iterates the list of open descriptors just prior to calling exec and closes any that aren't the stdio (0-2) descriptors. --- docs/changelog.markdown | 1 + pty/src/unix.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/docs/changelog.markdown b/docs/changelog.markdown index cd91f3106..21ea0dad7 100644 --- a/docs/changelog.markdown +++ b/docs/changelog.markdown @@ -26,6 +26,7 @@ brief notes about them may accumulate here. * macOS: Adjusted default dpi to 72 to bring point sizes into alignment with other macOS apps. [#332](https://github.com/wez/wezterm/issues/332) * Improved font fallback; we now try harder to find a system-provided font for glyphs that are not found in your explicitly configured fonts. * Revised pty output processing and removed the related `ratelimit_output_bytes_per_second` option +* Workaround Cocoa leaking window position saved state file descriptors to child processes on macOS Big Sur ### 20201101-103216-403d002d diff --git a/pty/src/unix.rs b/pty/src/unix.rs index e7fc4fc21..560ce4e97 100644 --- a/pty/src/unix.rs +++ b/pty/src/unix.rs @@ -98,6 +98,36 @@ impl Read for PtyFd { } } +// On Big Sur, Cocoa leaks various file descriptors to child processes, +// so we need to make a pass through the open descriptors beyond just the +// stdio descriptors and close them all out. +// This is approximately equivalent to the darwin `posix_spawnattr_setflags` +// option POSIX_SPAWN_CLOEXEC_DEFAULT which is used as a bit of a cheat +// on macOS. +#[cfg(target_os = "macos")] +fn close_random_fds() { + if let Ok(dir) = std::fs::read_dir("/dev/fd") { + let mut fds = vec![]; + for entry in dir { + if let Some(num) = entry + .ok() + .map(|e| e.file_name()) + .and_then(|s| s.into_string().ok()) + .and_then(|n| n.parse::().ok()) + { + if num > 2 { + fds.push(num); + } + } + } + for fd in fds { + unsafe { + libc::close(fd); + } + } + } +} + impl PtyFd { fn resize(&self, size: PtySize) -> Result<(), Error> { let ws_size = winsize { @@ -188,6 +218,10 @@ impl PtyFd { return Err(io::Error::last_os_error()); } } + + #[cfg(target_os = "macos")] + close_random_fds(); + Ok(()) }) };