2023-02-13 18:49:10 -06:00
|
|
|
use std::ffi::OsString;
|
2023-01-12 20:10:58 -06:00
|
|
|
use std::fs;
|
|
|
|
use std::io::Write;
|
2023-02-13 18:49:10 -06:00
|
|
|
use std::iter::repeat_with;
|
2023-01-12 20:10:58 -06:00
|
|
|
use std::os::unix::fs::OpenOptionsExt;
|
2023-02-13 18:49:10 -06:00
|
|
|
use std::path::{Path, PathBuf};
|
2023-01-12 20:10:58 -06:00
|
|
|
|
|
|
|
use anyhow::{Context, Result};
|
2023-02-11 16:38:01 -06:00
|
|
|
use sha2::{Digest, Sha256};
|
2023-01-12 20:10:58 -06:00
|
|
|
use tempfile::TempDir;
|
|
|
|
|
2023-02-13 18:49:10 -06:00
|
|
|
/// The number of random alphanumeric characters in the tempfiles.
|
|
|
|
const TEMPFILE_RANDOM_LENGTH: usize = 32;
|
|
|
|
|
2023-01-12 20:10:58 -06:00
|
|
|
/// Extension for a temporary directory that enables creating secure temporary files in it.
|
|
|
|
pub trait SecureTempDirExt {
|
2023-02-13 18:49:10 -06:00
|
|
|
fn create_secure_file(&self, path: &Path) -> Result<fs::File>;
|
|
|
|
fn write_secure_file(&self, contents: impl AsRef<[u8]>) -> Result<PathBuf>;
|
2023-01-12 20:10:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This implementation has three useful properties:
|
|
|
|
///
|
|
|
|
/// - Files are created with mode 0o600, so that they are only accessible by the current user.
|
|
|
|
/// - Files are named and not ephemeral (unlike a real temporary file).
|
|
|
|
/// - The directory and its children are cleaned up (i.e. deleted) when the variable that holds the
|
|
|
|
/// directory goes out of scope.
|
|
|
|
///
|
|
|
|
/// This protects against an attacker _without_ root access from modifying files undetected. It
|
|
|
|
/// provides no prection against an attacker _with_ root access. Additionally, because the files
|
|
|
|
/// have named paths, they can be passed to external programs while still being securely deleted
|
|
|
|
/// after they are not needed anymore.
|
|
|
|
impl SecureTempDirExt for TempDir {
|
|
|
|
/// Create a temporary file that can only be accessed by the current Linux user.
|
2023-02-13 18:49:10 -06:00
|
|
|
fn create_secure_file(&self, path: &Path) -> Result<fs::File> {
|
2023-01-12 20:10:58 -06:00
|
|
|
fs::OpenOptions::new()
|
|
|
|
.create(true)
|
|
|
|
.write(true)
|
|
|
|
.mode(0o600)
|
2023-02-13 18:49:10 -06:00
|
|
|
.open(path)
|
2023-01-12 20:10:58 -06:00
|
|
|
.with_context(|| format!("Failed to create tempfile: {path:?}"))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a temporary file and write a `u8` slice to it.
|
2023-02-13 18:49:10 -06:00
|
|
|
fn write_secure_file(&self, contents: impl AsRef<[u8]>) -> Result<PathBuf> {
|
|
|
|
let path = self.path().join(tmpname());
|
|
|
|
let mut tmpfile = self.create_secure_file(&path)?;
|
2023-01-12 20:10:58 -06:00
|
|
|
|
|
|
|
tmpfile
|
|
|
|
.write_all(contents.as_ref())
|
|
|
|
.with_context(|| format!("Failed to write to tempfile {path:?}"))?;
|
|
|
|
|
|
|
|
Ok(path)
|
|
|
|
}
|
|
|
|
}
|
2023-02-13 18:49:10 -06:00
|
|
|
|
|
|
|
/// Generate a random (but not cryptographically secure) name for a temporary file.
|
|
|
|
///
|
|
|
|
/// This is heavily inspired by the way temporary names are generated in the `tempfile` crate.
|
|
|
|
/// Since the `tempfile` crate does not expose this functionality, we have to recreate it here.
|
|
|
|
pub fn tmpname() -> OsString {
|
|
|
|
let mut buf = OsString::with_capacity(TEMPFILE_RANDOM_LENGTH);
|
|
|
|
let mut char_buf = [0u8; 4];
|
|
|
|
for c in repeat_with(fastrand::alphanumeric).take(TEMPFILE_RANDOM_LENGTH) {
|
|
|
|
buf.push(c.encode_utf8(&mut char_buf));
|
|
|
|
}
|
|
|
|
buf
|
|
|
|
}
|
2023-02-11 16:38:01 -06:00
|
|
|
|
|
|
|
type Hash = sha2::digest::Output<Sha256>;
|
|
|
|
|
|
|
|
/// Compute the SHA 256 hash of a file.
|
|
|
|
pub fn file_hash(file: &Path) -> Result<Hash> {
|
|
|
|
Ok(Sha256::digest(fs::read(file).with_context(|| {
|
|
|
|
format!("Failed to read file to hash: {file:?}")
|
|
|
|
})?))
|
|
|
|
}
|