Files
LLeMbas-TTY/umask/src/lib.rs
T
Wez Furlong db0d54cf44 Take care to restore the original umask
wezterm sets a more restrictive umask (`0o077`) by default so that any files
that it creates (eg: unix domain socket, log files) are more secure
by default.

However, some environments rely on the more general default of (`0o022`)
without checking that it is set.

This matters because programs spawned by wezterm inherit its more
restricted umask.

I hadn't noticed this because I've had `umask 022` in my shell RC files
since sometime in the 1990's.

This commit adds some plumbing to the pty layer to specify an optional
umask for the child process, and some more to our umask saver helper
so that any thread can determine the saved umask without needing a
reference to the saver itself, which may be in a different crate.

The logic in the config crate has been adjusted to connect the saved
value to the default command builder arguments.

The net result of this is that running `wezterm -n start bash -- --norc`
and typing `umask` in the resultant window now prints `0022`.

refs: #416
2021-01-07 09:20:13 -08:00

56 lines
1.3 KiB
Rust

#[cfg(unix)]
use libc::{mode_t, umask};
#[cfg(unix)]
use std::sync::Mutex;
#[cfg(unix)]
lazy_static::lazy_static! {
static ref SAVED_UMASK: Mutex<Option<libc::mode_t>> = Mutex::new(None);
}
/// Unfortunately, novice unix users can sometimes be running
/// with an overly permissive umask so we take care to install
/// a more restrictive mask while we might be creating things
/// in the filesystem.
/// This struct locks down the umask for its lifetime, restoring
/// the prior umask when it is dropped.
pub struct UmaskSaver {
#[cfg(unix)]
mask: mode_t,
}
impl UmaskSaver {
pub fn new() -> Self {
let me = Self {
#[cfg(unix)]
mask: unsafe { umask(0o077) },
};
#[cfg(unix)]
{
SAVED_UMASK.lock().unwrap().replace(me.mask);
}
me
}
/// Retrieves the mask saved by a UmaskSaver, without
/// having a reference to the UmaskSaver.
/// This is only meaningful if a single UmaskSaver is
/// used in a program.
#[cfg(unix)]
pub fn saved_umask() -> Option<mode_t> {
SAVED_UMASK.lock().unwrap().clone()
}
}
impl Drop for UmaskSaver {
fn drop(&mut self) {
#[cfg(unix)]
unsafe {
umask(self.mask);
SAVED_UMASK.lock().unwrap().take();
}
}
}