Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(keyring): reject writable secret mounts

Steven Enamakel232b62fdf123
diff --git a/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs b/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs
index 16b17cfd04..a487f91e96 100644
--- a/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs
+++ b/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs
@@ -256,11 +256,13 @@ fn master_key_from_env(
/// Reads the file named by [`MASTER_KEY_FILE_ENV`].
///
-/// On Unix a file any user on the host can access is accepted but logged at
+/// On Unix a file readable by any user on the host is accepted but logged at
/// `warn`, with how to tighten it. It is not refused: the deployments this
/// path exists for mount secrets read-only with modes the process cannot
/// change — Docker secrets `0444`, Kubernetes secret volumes `0644` unless
/// `defaultMode` is set — so refusing would break them out of the box.
+/// Files writable by other users are refused because another local user could
+/// replace the key and orphan the encrypted keyring on the next restart.
/// Group access alone is not warned about: a non-root pod reads a root-owned
/// secret through its `fsGroup`, and kubelet then grants group read, so
/// `defaultMode: 0400` lands as `0440` — the tightest mode that deployment
@@ -280,6 +282,11 @@ fn read_master_key_file(path: &Path) -> Result<String, String> {
Docker Swarm: `mode: 0400` on the secret)"
);
}
+ if key_file_mode_is_other_writable(mode) {
+ return Err(format!(
+ "master key file ({MASTER_KEY_FILE_ENV}) is writable by other users; + restrict it to a read-only secret mount"
+ ));
+ }
}
}
std::fs::read_to_string(path).map_err(|e| format!("cannot read master key file: {e}"))
@@ -292,6 +299,11 @@ fn key_file_mode_is_world_accessible(mode: u32) -> bool {
mode & 0o007 != 0
}
+#[cfg(unix)]
+fn key_file_mode_is_other_writable(mode: u32) -> bool {
+ mode & 0o002 != 0
+}
+
/// Decodes a master key supplied as exactly `2 * KEY_LEN` hex characters.
/// The value never appears in the error.
fn parse_master_key_hex(hex: &str) -> Result<[u8; KEY_LEN], String> {
diff --git a/crates/openhuman-core/src/security/keyring/encrypted_file_backend_tests.rs b/crates/openhuman-core/src/security/keyring/encrypted_file_backend_tests.rs
index 2906433632..f03b27810b 100644
--- a/crates/openhuman-core/src/security/keyring/encrypted_file_backend_tests.rs
+++ b/crates/openhuman-core/src/security/keyring/encrypted_file_backend_tests.rs
@@ -268,6 +268,19 @@ fn read_master_key_file_accepts_a_world_readable_secret_mount() {
assert_eq!(contents, hex_key(0x44));
}
+#[cfg(unix)]
+#[test]
+fn read_master_key_file_rejects_other_writable_files() {
+ use std::os::unix::fs::PermissionsExt;
+ let tmp = tempfile::TempDir::new().unwrap();
+ let path = tmp.path().join("master.key");
+ std::fs::write(&path, hex_key(0x45)).unwrap();
+ std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o602)).unwrap();
+
+ let err = read_master_key_file(&path).expect_err("other-writable key files are unsafe");
+ assert!(err.contains("writable by other users"), "{err}");
+}
+
#[cfg(unix)]
#[test]
fn key_file_warning_covers_default_mounts_but_not_owner_or_fsgroup_modes() {
@@ -280,6 +293,8 @@ fn key_file_warning_covers_default_mounts_but_not_owner_or_fsgroup_modes() {
for mode in [0o400, 0o600, 0o440, 0o640] {
assert!(!key_file_mode_is_world_accessible(mode), "{mode:04o}");
}
+ assert!(key_file_mode_is_other_writable(0o602));
+ assert!(!key_file_mode_is_other_writable(0o644));
}
#[test]
diff --git a/gitbooks/features/cloud-deploy.md b/gitbooks/features/cloud-deploy.md
index 60af504513..675a5e8724 100644
--- a/gitbooks/features/cloud-deploy.md
+++ b/gitbooks/features/cloud-deploy.md
@@ -495,8 +495,10 @@ read-only and it cannot fix the mode, but it logs a warning at every start.
Restrict it where the mount is declared:
- Kubernetes: set `defaultMode: 0400` on the secret volume (or `mode: 0400` on
- the item). A pod with `fsGroup` set gets `0440` instead, since kubelet adds
- group read so the non-root user can open it. That mode does not warn.
+ the item) and set `fsGroup` for the non-root core. Kubelet adds group read,
+ so the file is `0440` and the core can open it. Without `fsGroup` or
+ equivalent ownership/group access, the root-owned `0400` file is unreadable
+ by the core and key loading can fail at startup. This mode does not warn.
- Docker Swarm: set `mode: 0400` (with `uid`/`gid` for a non-root user) on the
service's secret.
- A plain file: `chmod 600` it, owned by the user the core runs as.