apt: keep the keyring cache private to the invoking user

The shared world-writable /tmp/pkh-keyrings directory, combined with the
skip-if-exists logic, let any local user pre-plant keyrings that pkh
then trusts into the chroot's trusted.gpg.d. Use a per-uid 0700
directory instead, refuse to reuse a pre-existing directory that is not
owned by the current user or is group/other-writable, and drop the now
unnecessary world-accessibility chmods (mmdebstrap in unshare mode runs
with the same real uid).
This commit is contained in:
2026-09-15 23:47:21 +02:00
parent 48f6e6ce4e
commit ea70ddc10d
2 changed files with 158 additions and 24 deletions
+101 -15
View File
@@ -7,6 +7,7 @@ use crate::context;
use crate::distro_info; use crate::distro_info;
use serde::Deserialize; use serde::Deserialize;
use std::error::Error; use std::error::Error;
use std::os::unix::fs::MetadataExt;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::Arc; use std::sync::Arc;
@@ -47,19 +48,48 @@ pub async fn download_cache_keyrings(
// Use system temp directory for keyrings since it's accessible from unshare mode // Use system temp directory for keyrings since it's accessible from unshare mode
// The home directory may not be accessible from mmdebstrap's unshare namespace // The home directory may not be accessible from mmdebstrap's unshare namespace
let temp_dir = std::env::temp_dir(); let temp_dir = std::env::temp_dir();
let keyring_dir = temp_dir.join("pkh-keyrings"); // Name the cache directory per-uid: a single shared /tmp directory would
// be writable by any local user, and the skip-if-exists logic below
// trusts pre-existing keyrings, so it must never be shared.
let euid = current_euid();
let keyring_dir = temp_dir.join(format!("pkh-keyrings-{euid}"));
// Create keyring directory if it doesn't exist if ctx.exists(&keyring_dir)? {
if !ctx.exists(&keyring_dir)? { if let context::ContextConfig::Local = ctx.config {
ctx.command("mkdir").arg("-p").arg(&keyring_dir).status()?; // Cached keyrings are trusted as-is whenever they already exist,
// so refuse to reuse a directory that is not owned by the current
// user or is writable by group/others (it could have been planted
// by another local user).
let metadata = std::fs::symlink_metadata(&keyring_dir)?;
validate_keyring_dir(metadata.uid(), metadata.mode(), euid).map_err(|reason| {
format!(
"Refusing to use keyring cache directory {}: {reason}; \
remove the directory and re-run pkh",
keyring_dir.display()
)
})?;
} else {
// Remote contexts (e.g. ssh) have no stat/metadata access through
// the context API, so the ownership guard cannot be performed;
// keep the previous best-effort behavior of tightening the
// directory permissions instead (0700 instead of the former
// world-writable a+rwx).
ctx.command("chmod").arg("700").arg(&keyring_dir).status()?;
}
} else {
// Create the directory private to the invoking user (0700). This is
// sufficient for mmdebstrap in unshare mode: it runs with the same
// real uid (the user namespace only maps that uid to root, file
// access still happens as the real uid), so no world-accessible
// permissions are needed.
ctx.command("mkdir")
.arg("-p")
.arg("-m")
.arg("700")
.arg(&keyring_dir)
.status()?;
} }
// Make keyring directory world-accessible so mmdebstrap in unshare mode can access it
ctx.command("chmod")
.arg("a+rwx")
.arg(&keyring_dir)
.status()?;
for keyring_url in keyring_urls { for keyring_url in keyring_urls {
// Extract the original filename from the keyring URL // Extract the original filename from the keyring URL
let filename = keyring_url let filename = keyring_url
@@ -116,9 +146,6 @@ pub async fn download_cache_keyrings(
let _ = ctx.command("rm").arg("-f").arg(&download_path).status(); let _ = ctx.command("rm").arg("-f").arg(&download_path).status();
} }
// Make the keyring file world-readable so mmdebstrap in unshare mode can access it
ctx.command("chmod").arg("a+r").arg(&binary_path).status()?;
log::info!( log::info!(
"Successfully downloaded keyring for {} to {}", "Successfully downloaded keyring for {} to {}",
series, series,
@@ -129,8 +156,6 @@ pub async fn download_cache_keyrings(
"Keyring already exists at {}, skipping download", "Keyring already exists at {}, skipping download",
binary_path.display() binary_path.display()
); );
// Ensure existing keyring is world-readable
ctx.command("chmod").arg("a+r").arg(&binary_path).status()?;
} }
} }
@@ -143,6 +168,67 @@ pub async fn download_cache_keyrings(
Ok(keyring_dir) Ok(keyring_dir)
} }
/// Effective uid of the current process
fn current_euid() -> u32 {
unsafe { libc::geteuid() }
}
/// Check that an existing keyring cache directory is safe to reuse
///
/// Cached keyrings are trusted whenever the files already exist (see the
/// skip-if-exists logic in [`download_cache_keyrings`]), so the directory
/// must be owned by the current user and must not be writable by group or
/// others, otherwise another local user could plant a malicious keyring.
///
/// Takes the directory's owner uid and permission mode (e.g. from
/// `std::fs::symlink_metadata`) so it can be unit tested without touching
/// the filesystem.
fn validate_keyring_dir(dir_uid: u32, mode: u32, euid: u32) -> Result<(), String> {
if dir_uid != euid {
return Err(format!(
"owned by uid {dir_uid}, not by the current user (uid {euid})"
));
}
if mode & 0o022 != 0 {
return Err(format!(
"writable by group or others (permissions {:04o})",
mode & 0o7777
));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_validate_keyring_dir_accepts_private_dir_owned_by_current_user() {
assert!(validate_keyring_dir(1000, 0o700, 1000).is_ok());
assert!(validate_keyring_dir(1000, 0o750, 1000).is_ok());
assert!(validate_keyring_dir(1000, 0o1744, 1000).is_ok());
assert!(validate_keyring_dir(0, 0o700, 0).is_ok());
}
#[test]
fn test_validate_keyring_dir_rejects_foreign_owner() {
let err = validate_keyring_dir(1000, 0o700, 1001).unwrap_err();
assert!(err.contains("owned by uid 1000"));
let err = validate_keyring_dir(1001, 0o700, 1000).unwrap_err();
assert!(err.contains("owned by uid 1001"));
}
#[test]
fn test_validate_keyring_dir_rejects_group_or_other_writable() {
assert!(validate_keyring_dir(1000, 0o770, 1000).is_err());
assert!(validate_keyring_dir(1000, 0o706, 1000).is_err());
assert!(validate_keyring_dir(1000, 0o707, 1000).is_err());
assert!(validate_keyring_dir(1000, 0o777, 1000).is_err());
// Sticky bit does not neutralize the group/other write bits.
assert!(validate_keyring_dir(1000, 0o1777, 1000).is_err());
}
}
/// Download and import a PPA key using Launchpad API /// Download and import a PPA key using Launchpad API
/// ///
/// # Arguments /// # Arguments
+57 -9
View File
@@ -11,7 +11,8 @@
//! unmounting before they can be removed. //! unmounting before they can be removed.
//! - **Cached chroot tarballs** (`~/.cache/pkh/*-buildd.tar.xz`) and their //! - **Cached chroot tarballs** (`~/.cache/pkh/*-buildd.tar.xz`) and their
//! **stale download lockfiles** (`~/.cache/pkh/*.lock`). //! **stale download lockfiles** (`~/.cache/pkh/*.lock`).
//! - **The shared apt keyring directory** (`/tmp/pkh-keyrings`), used by //! - **The apt keyring cache directories** (`pkh-keyrings` and the per-uid
//! `pkh-keyrings-<uid>` under the system temp directory), used by
//! mmdebstrap runs. //! mmdebstrap runs.
//! - **Build logs** (`~/.cache/pkh/logs/deb-*.log`) written by `pkh deb`. By //! - **Build logs** (`~/.cache/pkh/logs/deb-*.log`) written by `pkh deb`. By
//! default only logs beyond a small retention window (the newest //! default only logs beyond a small retention window (the newest
@@ -67,7 +68,8 @@ pub(crate) const KEEP_LOGS: usize = 10;
enum Artifact { enum Artifact {
/// A residual build/chroot directory under the system temp dir. /// A residual build/chroot directory under the system temp dir.
TempDir(PathBuf), TempDir(PathBuf),
/// The shared apt keyring directory (`/tmp/pkh-keyrings`). /// The apt keyring cache directory (`pkh-keyrings*` under the system
/// temp dir).
KeyringDir(PathBuf), KeyringDir(PathBuf),
/// A stale chroot tarball download lockfile. /// A stale chroot tarball download lockfile.
LockFile(PathBuf), LockFile(PathBuf),
@@ -105,11 +107,24 @@ pub fn cache_dir() -> Option<PathBuf> {
ProjectDirs::from("com", "pkh", "pkh").map(|d| d.cache_dir().to_path_buf()) ProjectDirs::from("com", "pkh", "pkh").map(|d| d.cache_dir().to_path_buf())
} }
/// Check whether a directory entry name is the apt keyring cache directory,
/// i.e. the legacy shared `pkh-keyrings` name or the per-uid
/// `pkh-keyrings-<uid>` used by current pkh versions.
fn is_keyring_dir_name(name: &str) -> bool {
if name == "pkh-keyrings" {
return true;
}
match name.strip_prefix("pkh-keyrings-") {
Some(uid) => !uid.is_empty() && uid.bytes().all(|b| b.is_ascii_digit()),
None => false,
}
}
/// Check whether a directory entry name is a residual pkh build directory, /// Check whether a directory entry name is a residual pkh build directory,
/// i.e. it matches `pkh-<digits>` or `pkh-<digits>-<digits>`. /// i.e. it matches `pkh-<digits>` or `pkh-<digits>-<digits>`.
/// ///
/// This deliberately rejects the `pkh-keyrings` and `pkh-build-*` names so they /// This deliberately rejects the `pkh-keyrings*` and `pkh-build-*` names so
/// are not mistaken for residual build chroots. /// they are not mistaken for residual build chroots.
fn is_residual_temp_dir(name: &str) -> bool { fn is_residual_temp_dir(name: &str) -> bool {
let Some(rest) = name.strip_prefix("pkh-") else { let Some(rest) = name.strip_prefix("pkh-") else {
return false; return false;
@@ -189,7 +204,7 @@ fn discover_artifacts(temp_dir: &Path, cache_dir: Option<&Path>) -> Vec<Artifact
} }
if is_residual_temp_dir(&name) { if is_residual_temp_dir(&name) {
artifacts.push(Artifact::TempDir(path)); artifacts.push(Artifact::TempDir(path));
} else if name == "pkh-keyrings" { } else if is_keyring_dir_name(&name) {
artifacts.push(Artifact::KeyringDir(path)); artifacts.push(Artifact::KeyringDir(path));
} }
} }
@@ -404,6 +419,7 @@ mod tests {
// Keyrings and unshare work dirs must NOT match. // Keyrings and unshare work dirs must NOT match.
assert!(!is_residual_temp_dir("pkh-keyrings")); assert!(!is_residual_temp_dir("pkh-keyrings"));
assert!(!is_residual_temp_dir("pkh-keyrings-1000"));
assert!(!is_residual_temp_dir("pkh-build-1700000000")); assert!(!is_residual_temp_dir("pkh-build-1700000000"));
// Non-numeric / malformed names. // Non-numeric / malformed names.
@@ -415,6 +431,21 @@ mod tests {
assert!(!is_residual_temp_dir("other-123")); assert!(!is_residual_temp_dir("other-123"));
} }
#[test]
fn test_is_keyring_dir_name() {
// Legacy shared name and current per-uid names are matched.
assert!(is_keyring_dir_name("pkh-keyrings"));
assert!(is_keyring_dir_name("pkh-keyrings-0"));
assert!(is_keyring_dir_name("pkh-keyrings-1000"));
// Everything else is not.
assert!(!is_keyring_dir_name("pkh-keyrings-"));
assert!(!is_keyring_dir_name("pkh-keyrings-abc"));
assert!(!is_keyring_dir_name("pkh-keyrings-1000-2"));
assert!(!is_keyring_dir_name("keyrings"));
assert!(!is_keyring_dir_name("pkh-1000"));
}
#[test] #[test]
fn test_unescape_mountpath() { fn test_unescape_mountpath() {
assert_eq!(unescape_mountpath("/tmp/simple"), "/tmp/simple"); assert_eq!(unescape_mountpath("/tmp/simple"), "/tmp/simple");
@@ -455,8 +486,9 @@ none /tmp/other proc rw 0 0
// Residual build dirs. // Residual build dirs.
fs::create_dir_all(temp_path.join("pkh-1700000000")).unwrap(); fs::create_dir_all(temp_path.join("pkh-1700000000")).unwrap();
fs::create_dir_all(temp_path.join("pkh-1700000001-2")).unwrap(); fs::create_dir_all(temp_path.join("pkh-1700000001-2")).unwrap();
// Keyring dir. // Keyring dirs (legacy shared name and per-uid name).
fs::create_dir_all(temp_path.join("pkh-keyrings")).unwrap(); fs::create_dir_all(temp_path.join("pkh-keyrings")).unwrap();
fs::create_dir_all(temp_path.join("pkh-keyrings-1000")).unwrap();
// Non-matching entries that should be ignored. // Non-matching entries that should be ignored.
fs::create_dir_all(temp_path.join("pkh-build-1700000000")).unwrap(); fs::create_dir_all(temp_path.join("pkh-build-1700000000")).unwrap();
fs::create_dir_all(temp_path.join("other-dir")).unwrap(); fs::create_dir_all(temp_path.join("other-dir")).unwrap();
@@ -477,21 +509,21 @@ none /tmp/other proc rw 0 0
let artifacts = discover_artifacts(temp_path, Some(cache_path)); let artifacts = discover_artifacts(temp_path, Some(cache_path));
let mut temp_dirs = 0; let mut temp_dirs = 0;
let mut keyring = false; let mut keyring_dirs = 0;
let mut locks = 0; let mut locks = 0;
let mut tarballs = 0; let mut tarballs = 0;
let mut logs = 0; let mut logs = 0;
for a in &artifacts { for a in &artifacts {
match a { match a {
Artifact::TempDir(_) => temp_dirs += 1, Artifact::TempDir(_) => temp_dirs += 1,
Artifact::KeyringDir(_) => keyring = true, Artifact::KeyringDir(_) => keyring_dirs += 1,
Artifact::LockFile(_) => locks += 1, Artifact::LockFile(_) => locks += 1,
Artifact::Tarball(_) => tarballs += 1, Artifact::Tarball(_) => tarballs += 1,
Artifact::LogFile(_) => logs += 1, Artifact::LogFile(_) => logs += 1,
} }
} }
assert_eq!(temp_dirs, 2); assert_eq!(temp_dirs, 2);
assert!(keyring); assert_eq!(keyring_dirs, 2);
assert_eq!(locks, 1); assert_eq!(locks, 1);
assert_eq!(tarballs, 2); assert_eq!(tarballs, 2);
assert_eq!(logs, 1); assert_eq!(logs, 1);
@@ -564,6 +596,7 @@ none /tmp/other proc rw 0 0
let dir = temp_path.join("pkh-1700000000"); let dir = temp_path.join("pkh-1700000000");
fs::create_dir_all(&dir).unwrap(); fs::create_dir_all(&dir).unwrap();
fs::create_dir_all(temp_path.join("pkh-keyrings")).unwrap(); fs::create_dir_all(temp_path.join("pkh-keyrings")).unwrap();
fs::create_dir_all(temp_path.join("pkh-keyrings-1000")).unwrap();
fs::write(cache_path.join("noble-buildd.tar.xz"), "tarball").unwrap(); fs::write(cache_path.join("noble-buildd.tar.xz"), "tarball").unwrap();
fs::write(cache_path.join("noble-buildd.tar.lock"), "lock").unwrap(); fs::write(cache_path.join("noble-buildd.tar.lock"), "lock").unwrap();
@@ -587,6 +620,12 @@ none /tmp/other proc rw 0 0
.iter() .iter()
.any(|p| p == &temp_path.join("pkh-keyrings")) .any(|p| p == &temp_path.join("pkh-keyrings"))
); );
assert!(
report
.removed
.iter()
.any(|p| p == &temp_path.join("pkh-keyrings-1000"))
);
assert!( assert!(
report report
.removed .removed
@@ -604,6 +643,7 @@ none /tmp/other proc rw 0 0
// Nothing was actually removed. // Nothing was actually removed.
assert!(dir.exists()); assert!(dir.exists());
assert!(temp_path.join("pkh-keyrings").exists()); assert!(temp_path.join("pkh-keyrings").exists());
assert!(temp_path.join("pkh-keyrings-1000").exists());
assert!(cache_path.join("noble-buildd.tar.xz").exists()); assert!(cache_path.join("noble-buildd.tar.xz").exists());
assert!(cache_path.join("noble-buildd.tar.lock").exists()); assert!(cache_path.join("noble-buildd.tar.lock").exists());
} }
@@ -619,6 +659,7 @@ none /tmp/other proc rw 0 0
let cache_path = cache.path(); let cache_path = cache.path();
fs::create_dir_all(temp_path.join("pkh-keyrings")).unwrap(); fs::create_dir_all(temp_path.join("pkh-keyrings")).unwrap();
fs::create_dir_all(temp_path.join("pkh-keyrings-1000")).unwrap();
fs::write(cache_path.join("noble-buildd.tar.lock"), "lock").unwrap(); fs::write(cache_path.join("noble-buildd.tar.lock"), "lock").unwrap();
fs::write(cache_path.join("noble-buildd.tar.xz"), "tarball").unwrap(); fs::write(cache_path.join("noble-buildd.tar.xz"), "tarball").unwrap();
@@ -639,6 +680,12 @@ none /tmp/other proc rw 0 0
.iter() .iter()
.any(|p| p == &temp_path.join("pkh-keyrings")) .any(|p| p == &temp_path.join("pkh-keyrings"))
); );
assert!(
report
.removed
.iter()
.any(|p| p == &temp_path.join("pkh-keyrings-1000"))
);
assert!( assert!(
report report
.removed .removed
@@ -647,6 +694,7 @@ none /tmp/other proc rw 0 0
); );
assert!(!temp_path.join("pkh-keyrings").exists()); assert!(!temp_path.join("pkh-keyrings").exists());
assert!(!temp_path.join("pkh-keyrings-1000").exists());
assert!(!cache_path.join("noble-buildd.tar.lock").exists()); assert!(!cache_path.join("noble-buildd.tar.lock").exists());
// Tarball is preserved when --all is not set. // Tarball is preserved when --all is not set.
assert!(cache_path.join("noble-buildd.tar.xz").exists()); assert!(cache_path.join("noble-buildd.tar.xz").exists());