Skip to main content

artifactize_core/platform/
unix.rs

1//! Unix private file modes, process control, signals, and termios.
2//! Pinned file access lives in artifactize-tools.
3
4mod descriptors;
5pub(crate) mod ipc;
6mod process;
7
8use std::{
9    fs::{self, DirBuilder, File, OpenOptions, Permissions},
10    future::Future,
11    io,
12    os::unix::{
13        fs::{DirBuilderExt, MetadataExt, OpenOptionsExt, PermissionsExt},
14        process::ExitStatusExt,
15    },
16    path::Path,
17    process::ExitStatus,
18};
19
20pub(crate) use tempfile::TempDir as PrivateTempDir;
21use tokio::signal::unix::{SignalKind, signal};
22
23pub(crate) use process::{
24    Child, hide_console_window, kill_saved_group, kill_saved_session, process_start_time,
25    running_process_start_time, spawn_detached, spawn_gated, spawn_now, spawn_run, spawn_watched,
26};
27
28/// Give the heap memory freed so far back to the system. glibc keeps freed memory in its
29/// arenas for reuse, so a long-lived process that once read much stays that large; macOS's
30/// allocator returns it by itself.
31pub(crate) fn release_free_memory() {
32    #[cfg(all(target_os = "linux", target_env = "gnu"))]
33    // SAFETY: malloc_trim only releases memory that is already free, under glibc's own locks.
34    unsafe {
35        libc::malloc_trim(0);
36    }
37}
38
39/// No Windows-style pending sharing disposition survives group cleanup.
40pub(crate) fn remove_abandoned_directory(path: &Path) -> io::Result<()> {
41    std::fs::remove_dir_all(path)
42}
43
44/// POSIX file locks are advisory, and unlinking a name never invalidates another handle's
45/// access to it, so a plain metadata or open race like Windows's never happens here.
46pub(crate) fn transient_file_access(_error: &io::Error) -> bool {
47    false
48}
49
50/// Owner-only directory mode (rwx------): these directories hold credentials, Agent sessions,
51/// sockets and review state, so no other local user may list or enter them.
52const PRIVATE_DIR_MODE: u32 = 0o700;
53
54/// Owner-only file mode (rw-------): these files hold credentials, Agent transcripts and review
55/// evidence, so no other local user may read them.
56const PRIVATE_FILE_MODE: u32 = 0o600;
57
58/// How a private directory and file are described in messages.
59pub(crate) const PRIVATE_DIRECTORY: &str = "owner-only (mode 0700)";
60pub(crate) const PRIVATE_FILE: &str = "readable by their owner only (mode 0600)";
61
62/// The group and other permission bits; a private path has none of them set.
63pub(crate) const GROUP_OTHER_BITS: u32 = 0o077;
64
65/// Group/other write bits let another user replace system-alias or IPC parent
66/// directories, so neither the alias nor the endpoint under them can be trusted.
67const GROUP_OTHER_WRITE_BITS: u32 = 0o022;
68
69/// The permission bits of a mode, without the file type and set-id bits, so a file can be
70/// compared with [`PRIVATE_FILE_MODE`] exactly.
71const PERMISSION_BITS: u32 = 0o777;
72
73/// Create a directory and its missing parents as 0700; existing ones keep their mode.
74pub(crate) fn create_private_dir_all(path: &Path) -> io::Result<()> {
75    DirBuilder::new()
76        .recursive(true)
77        .mode(PRIVATE_DIR_MODE)
78        .create(path)
79}
80
81/// Create one directory as exactly 0700, whatever the umask.
82pub(crate) fn create_private_dir(path: &Path) -> io::Result<()> {
83    DirBuilder::new().mode(PRIVATE_DIR_MODE).create(path)?;
84    fs::set_permissions(path, Permissions::from_mode(PRIVATE_DIR_MODE))
85}
86
87/// A new 0700 temporary directory below `parent`.
88pub(crate) fn private_tempdir_in(prefix: &str, parent: &Path) -> io::Result<PrivateTempDir> {
89    let directory = tempfile::Builder::new()
90        .prefix(prefix)
91        .permissions(Permissions::from_mode(PRIVATE_DIR_MODE))
92        .tempdir_in(parent)?;
93    fs::set_permissions(directory.path(), Permissions::from_mode(PRIVATE_DIR_MODE))?;
94    Ok(directory)
95}
96
97/// Whether a directory grants nothing to its group or others.
98pub(crate) fn is_private_dir(path: &Path) -> io::Result<bool> {
99    Ok(fs::metadata(path)?.mode() & GROUP_OTHER_BITS == 0)
100}
101
102/// Whether an open file is a regular, single-link 0600 file.
103pub(crate) fn is_private_file(file: &File) -> io::Result<bool> {
104    let metadata = file.metadata()?;
105    Ok(metadata.is_file()
106        && metadata.mode() & PERMISSION_BITS == PRIVATE_FILE_MODE
107        && metadata.nlink() == 1)
108}
109
110/// Options that create files as 0600.
111pub(crate) fn private_options() -> OpenOptions {
112    let mut options = OpenOptions::new();
113    options.mode(PRIVATE_FILE_MODE);
114    options
115}
116
117/// Make an open file 0600.
118pub(crate) fn restrict_file(file: &File) -> io::Result<()> {
119    file.set_permissions(Permissions::from_mode(PRIVATE_FILE_MODE))
120}
121
122/// Persist a directory's entries, such as a file just renamed into it.
123pub(crate) fn sync_dir(path: &Path) -> io::Result<()> {
124    File::open(path).and_then(|directory| directory.sync_all())
125}
126
127/// A file's identity on its volume: its device and inode.
128pub(crate) fn file_identity(file: &File) -> io::Result<super::FileIdentity> {
129    let metadata = file.metadata()?;
130    Ok(super::FileIdentity {
131        volume: metadata.dev(),
132        index: metadata.ino(),
133    })
134}
135
136/// The effective user, the owner that private paths must have.
137pub(crate) fn current_user() -> u32 {
138    // SAFETY: geteuid has no preconditions and does not mutate process identity.
139    unsafe { libc::geteuid() }
140}
141
142/// Whether an open file or directory belongs to the current user and grants nothing to its
143/// group or others.
144pub(crate) fn is_owner_only(file: &File) -> io::Result<bool> {
145    let metadata = file.metadata()?;
146    Ok(metadata.mode() & GROUP_OTHER_BITS == 0 && metadata.uid() == current_user())
147}
148
149/// Environment variable names compare exactly.
150pub(crate) const ENV_NAMES_IGNORE_CASE: bool = false;
151
152/// The variable naming the user's home directory.
153pub(crate) const HOME_VARIABLE: &str = "HOME";
154
155/// Variables programs read their temporary directory from. `TMP` and `TEMP` are set too, for
156/// programs written for Windows.
157pub(crate) const TEMP_VARIABLES: &[&str] = &["TMPDIR", "TMP", "TEMP"];
158
159/// Variables naming a per-user state directory, in order; artifactize keeps its state in an
160/// `artifactize` folder below the first one set, and otherwise below `HOME`.
161pub(crate) const STATE_VARIABLES: &[&str] = &["XDG_STATE_HOME"];
162
163/// The variables naming the signed-in user, in order.
164pub(crate) const USER_VARIABLES: &[&str] = &["USER", "LOGNAME"];
165
166/// The user's home directory.
167pub(crate) fn home_directory() -> Option<std::path::PathBuf> {
168    std::env::var_os(HOME_VARIABLE)
169        .filter(|path| !path.is_empty())
170        .map(Into::into)
171}
172
173/// A path from the raw bytes a tool such as git prints: any bytes name a Unix path.
174pub(crate) fn path_from_bytes(bytes: &[u8]) -> std::path::PathBuf {
175    use std::os::unix::ffi::OsStringExt;
176    std::ffi::OsString::from_vec(bytes.to_vec()).into()
177}
178
179/// Unix programs end a line with LF only.
180pub(crate) const CRLF_LINE_ENDINGS: bool = false;
181
182/// Whether a terminal that cannot hide input is still read, visibly, after a warning. A Unix
183/// terminal always can; failing to hide input is an error.
184pub const VISIBLE_INPUT_FALLBACK: bool = false;
185
186/// The signal that ended a process, if one did.
187pub(crate) fn exit_signal(status: &ExitStatus) -> Option<i32> {
188    status.signal()
189}
190
191/// The operating system's host name, without depending on a proc filesystem.
192pub(crate) fn host_name() -> Option<String> {
193    hostname::get().ok()?.into_string().ok()
194}
195
196/// The absolute form of `path` with a leading macOS system alias (`/tmp`, `/var`, `/etc`)
197/// replaced by its fixed `/private` target. The alias and its parents must be root-owned
198/// and not replaceable; descendants are left for the caller to open without following links.
199#[cfg(target_os = "macos")]
200pub(crate) fn resolve_system_aliases(path: &Path) -> io::Result<std::path::PathBuf> {
201    let absolute = std::path::absolute(path)?;
202    for name in ["tmp", "var", "etc"] {
203        let alias = Path::new("/").join(name);
204        if let Ok(suffix) = absolute.strip_prefix(&alias) {
205            let metadata = alias.symlink_metadata()?;
206            let target = Path::new("/private").join(name);
207            let link = fs::read_link(&alias)?;
208            let link = if link.is_absolute() {
209                link
210            } else {
211                Path::new("/").join(link)
212            };
213            if !metadata.is_symlink() || metadata.uid() != 0 || link != target {
214                return Err(io::Error::other(format!(
215                    "Refusing an untrusted system alias: {}",
216                    alias.display()
217                )));
218            }
219            for parent in [Path::new("/"), Path::new("/private")] {
220                let metadata = parent.symlink_metadata()?;
221                if !metadata.is_dir()
222                    || metadata.uid() != 0
223                    || metadata.mode() & GROUP_OTHER_WRITE_BITS != 0
224                {
225                    return Err(io::Error::other(
226                        "System alias parents must not be replaceable.",
227                    ));
228                }
229            }
230            return Ok(target.join(suffix));
231        }
232    }
233    Ok(absolute)
234}
235
236/// The absolute form of `path`; this system has no aliased system roots.
237#[cfg(not(target_os = "macos"))]
238pub(crate) fn resolve_system_aliases(path: &Path) -> io::Result<std::path::PathBuf> {
239    std::path::absolute(path)
240}
241
242/// Register for SIGINT and SIGTERM now; the future completes at the first of them.
243pub fn stop_requested() -> io::Result<impl Future<Output = ()> + Send + 'static> {
244    let mut interrupt = signal(SignalKind::interrupt())?;
245    let mut terminate = signal(SignalKind::terminate())?;
246    Ok(async move {
247        tokio::select! { _ = interrupt.recv() => {}, _ = terminate.recv() => {} }
248    })
249}
250
251/// Turns terminal echo off until dropped, so a pasted secret is not shown.
252pub struct HiddenInput(libc::termios);
253
254impl HiddenInput {
255    pub fn new() -> io::Result<Self> {
256        let mut termios = std::mem::MaybeUninit::uninit();
257        // SAFETY: tcgetattr fills the struct on success; it is read only then.
258        let saved = unsafe {
259            if libc::tcgetattr(libc::STDIN_FILENO, termios.as_mut_ptr()) != 0 {
260                return Err(io::Error::last_os_error());
261            }
262            termios.assume_init()
263        };
264        let mut hidden = saved;
265        hidden.c_lflag &= !libc::ECHO;
266        // SAFETY: a valid termios for the same descriptor.
267        if unsafe { libc::tcsetattr(libc::STDIN_FILENO, libc::TCSANOW, &hidden) } != 0 {
268            return Err(io::Error::last_os_error());
269        }
270        Ok(Self(saved))
271    }
272}
273
274impl Drop for HiddenInput {
275    fn drop(&mut self) {
276        // SAFETY: restores the attributes read in `new`.
277        unsafe { libc::tcsetattr(libc::STDIN_FILENO, libc::TCSANOW, &self.0) };
278    }
279}
280
281#[cfg(test)]
282mod tests {
283    use super::*;
284
285    /// Unlike Windows, no error here is ever transient in this sense; the predicate is a
286    /// permanent no-op, not a gap in detecting a race that would need retrying.
287    #[test]
288    fn transient_file_access_is_always_false() {
289        assert!(!transient_file_access(&io::Error::from_raw_os_error(5)));
290        assert!(!transient_file_access(&io::Error::new(
291            io::ErrorKind::PermissionDenied,
292            "permission denied"
293        )));
294    }
295}