diff --git a/client/src/file/api/legacy_keyring/encrypted.rs b/client/src/file/api/legacy_keyring/encrypted.rs index 38219f2d..63153500 100644 --- a/client/src/file/api/legacy_keyring/encrypted.rs +++ b/client/src/file/api/legacy_keyring/encrypted.rs @@ -66,6 +66,9 @@ impl Keyring { self.iteration_count.try_into().unwrap(), )?; let decrypted = crypto::decrypt_no_padding(&self.encrypted_content, &key, iv)?; + if decrypted.len() < 16 { + return Err(Error::NoData); + } let (digest, content) = decrypted.split_at(16); if !crypto::verify_checksum_md5(digest, content) { return Err(Error::ChecksumMismatch); @@ -321,6 +324,21 @@ mod tests { use super::*; + #[test] + fn decrypt_empty_content_is_an_error() { + // A crafted keyring file can declare an empty encrypted content + let keyring = Keyring { + salt: vec![0; 8], + iteration_count: 100000, + encrypted_content: Vec::new(), + hashed_items: Vec::new(), + }; + assert!(matches!( + keyring.decrypt_items(&Secret::from("test")), + Err(Error::NoData) + )); + } + #[test] fn legacy_decrypt() -> Result<(), Error> { let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) diff --git a/client/src/file/api/mod.rs b/client/src/file/api/mod.rs index 67577797..26c83564 100644 --- a/client/src/file/api/mod.rs +++ b/client/src/file/api/mod.rs @@ -92,7 +92,7 @@ impl Keyring { // TODO: UTC? modified_time: std::time::SystemTime::UNIX_EPOCH .elapsed() - .unwrap() + .unwrap_or_default() .as_secs(), usage_count: 0, items: Vec::new(), @@ -157,7 +157,7 @@ impl Keyring { self.modified_time = std::time::SystemTime::UNIX_EPOCH .elapsed() - .unwrap() + .unwrap_or_default() .as_secs(); self.usage_count += 1; diff --git a/server/src/collection/mod.rs b/server/src/collection/mod.rs index c358c16f..48aa8e20 100644 --- a/server/src/collection/mod.rs +++ b/server/src/collection/mod.rs @@ -458,9 +458,13 @@ pub(crate) fn alias_path(alias: &str) -> Option { } pub(crate) fn collection_path(label: &str) -> Result { - let sanitized_label = label + let mut sanitized_label = label .to_lowercase() .replace(|c: char| !c.is_ascii_alphanumeric() && c != '_', "_"); + if sanitized_label.is_empty() { + // An empty label would produce a path with a trailing slash + sanitized_label = "_".to_owned(); + } OwnedObjectPath::try_from(format!( "/org/freedesktop/secrets/collection/{sanitized_label}" diff --git a/server/src/collection/tests/properties.rs b/server/src/collection/tests/properties.rs index dfbe4f0a..83218031 100644 --- a/server/src/collection/tests/properties.rs +++ b/server/src/collection/tests/properties.rs @@ -77,7 +77,10 @@ async fn timestamps() -> Result<(), Box> { fn test_collection_path() { use crate::collection::collection_path; - assert!(collection_path("").is_err()); + assert_eq!( + collection_path("").unwrap().as_str(), + "/org/freedesktop/secrets/collection/_" + ); assert_eq!( collection_path("bär").unwrap().as_str(), "/org/freedesktop/secrets/collection/b_r" diff --git a/server/src/pam_listener/mod.rs b/server/src/pam_listener/mod.rs index 79fea633..e3090a0b 100644 --- a/server/src/pam_listener/mod.rs +++ b/server/src/pam_listener/mod.rs @@ -299,9 +299,17 @@ impl PamListener { tracing::info!("Migrated {} v0 keyring(s): {:?}", migrated.len(), migrated); } - let collections = self.service.collections.lock().await; - - for collection in collections.values() { + // Unlocking derives keys and reads files, don't hold the lock for it + let collections: Vec<_> = self + .service + .collections + .lock() + .await + .values() + .cloned() + .collect(); + + for collection in &collections { if collection.is_locked().await { tracing::debug!("Attempting to unlock collection: {}", collection.path()); @@ -330,20 +338,29 @@ impl PamListener { old_secret: &Secret, new_secret: &Secret, ) -> Result { - let collections = self.service.collections.lock().await; + // Changing a secret derives keys and writes files, don't hold the + // lock for it + let collections: Vec<_> = self + .service + .collections + .lock() + .await + .values() + .cloned() + .collect(); let mut changed_count = 0; - for (path, collection) in collections.iter() { + for collection in &collections { // Skip session collection (it's temporary and doesn't persist) if collection.alias().await == oo7::dbus::Service::SESSION_COLLECTION { - tracing::debug!("Skipping session collection: {}", path); + tracing::debug!("Skipping session collection: {}", collection.path()); continue; } // Get the keyring from the collection let keyring_guard = collection.keyring.read().await; let Some(keyring) = keyring_guard.as_ref() else { - tracing::debug!("Collection {} has no keyring", path); + tracing::debug!("Collection {} has no keyring", collection.path()); continue; }; @@ -355,7 +372,7 @@ impl PamListener { // Try to unlock with old password first tracing::debug!( "Collection {} is locked, attempting to unlock with old password", - path + collection.path() ); drop(keyring_guard); @@ -366,7 +383,7 @@ impl PamListener { { tracing::warn!( "Failed to unlock collection {} with old password: {}", - path, + collection.path(), e ); continue; @@ -378,7 +395,7 @@ impl PamListener { // Re-acquire the lock to get the unlocked keyring let keyring_guard = collection.keyring.read().await; let Some(oo7::file::Keyring::Unlocked(uk)) = keyring_guard.as_ref() else { - tracing::warn!("Collection {} is not unlocked", path); + tracing::warn!("Collection {} is not unlocked", collection.path()); continue; }; @@ -388,7 +405,7 @@ impl PamListener { Err(e) => { tracing::warn!( "Failed to validate old password for collection {}: {}", - path, + collection.path(), e ); continue; @@ -398,7 +415,7 @@ impl PamListener { if !can_decrypt { tracing::debug!( "Old password does not match keyring {} password, skipping", - path + collection.path() ); continue; } @@ -406,7 +423,10 @@ impl PamListener { // Change the keyring password match uk.change_secret(new_secret.clone()).await { Ok(_) => { - tracing::info!("Successfully changed password for collection: {}", path); + tracing::info!( + "Successfully changed password for collection: {}", + collection.path() + ); changed_count += 1; // Re-lock the collection if it was locked before we @@ -416,16 +436,20 @@ impl PamListener { if let Err(e) = collection.set_locked(true, None).await { tracing::warn!( "Failed to re-lock collection {} after password change: {}", - path, + collection.path(), e ); } else { - tracing::debug!("Re-locked collection: {}", path); + tracing::debug!("Re-locked collection: {}", collection.path()); } } } Err(e) => { - tracing::error!("Failed to change password for collection {}: {}", path, e); + tracing::error!( + "Failed to change password for collection {}: {}", + collection.path(), + e + ); } } } diff --git a/server/src/service/locking.rs b/server/src/service/locking.rs index 571bf080..e4f1133b 100644 --- a/server/src/service/locking.rs +++ b/server/src/service/locking.rs @@ -7,7 +7,7 @@ use zbus::{ }; use super::Service; -use crate::{collection::Collection, error::custom_service_error}; +use crate::{collection::Collection, error::custom_service_error, migration::PendingMigration}; impl Service { pub async fn set_locked( @@ -225,10 +225,18 @@ impl Service { pub async fn migrate_pending_keyrings(&self, secret: &Secret) -> Vec { let mut migrated = Vec::new(); - let mut pending = self.pending_migrations.lock().await; + // Migrating reads files and derives keys, don't hold the lock for it + let migrations: Vec = self + .pending_migrations + .lock() + .await + .values() + .cloned() + .collect(); let mut to_remove = Vec::new(); - for (name, migration) in pending.iter() { + for migration in &migrations { + let name = migration.name(); tracing::debug!("Attempting to migrate pending keyring: {name}"); match migration.migrate(&self.data_dir, Some(secret)).await { @@ -284,8 +292,8 @@ impl Service { } } - migrated.push(name.clone()); - to_remove.push(name.clone()); + migrated.push(name.to_owned()); + to_remove.push(name.to_owned()); } Err(e) => { tracing::debug!( @@ -296,6 +304,7 @@ impl Service { } } + let mut pending = self.pending_migrations.lock().await; for name in &to_remove { pending.remove(name); } diff --git a/server/src/service/tests/collection.rs b/server/src/service/tests/collection.rs index 37273fa3..12ec3d06 100644 --- a/server/src/service/tests/collection.rs +++ b/server/src/service/tests/collection.rs @@ -230,3 +230,23 @@ async fn complete_collection_creation_no_pending() -> Result<(), Box Result<(), Box> { + let setup = TestServiceSetup::plain_session(false).await?; + + let collection = setup.service_api.create_collection("", None, None).await?; + + assert_eq!(collection.label().await?, ""); + assert!( + setup + .service_api + .collections() + .await? + .iter() + .any(|c| c.inner().path() == collection.inner().path()) + ); + + Ok(()) +}