diff --git a/client/src/file/api/encrypted_item.rs b/client/src/file/api/encrypted_item.rs index 955fd337..0f1918f2 100644 --- a/client/src/file/api/encrypted_item.rs +++ b/client/src/file/api/encrypted_item.rs @@ -83,6 +83,9 @@ impl EncryptedItem { let n_iv = crypto::iv_len(); // The encrypted data, the iv, and the mac are concatenated into blob. + if n < n_mac + n_iv { + return Err(Error::NoData); + } let (encrypted_data_with_iv, mac_tag) = &self.blob.split_at(n - n_mac); // verify item @@ -132,3 +135,21 @@ impl EncryptedItem { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn decrypt_short_blob_is_an_error() { + // The placeholder items of a pending migration carry no blob + let item = EncryptedItem { + hashed_attributes: HashMap::new(), + blob: Vec::new(), + }; + let key = Key::new(vec![ + 132, 3, 113, 222, 81, 209, 49, 43, 81, 232, 243, 46, 1, 103, 184, 42, + ]); + assert!(matches!(item.decrypt(Some(&key)), Err(Error::NoData))); + } +} diff --git a/server/src/collection/mod.rs b/server/src/collection/mod.rs index 80a07931..c358c16f 100644 --- a/server/src/collection/mod.rs +++ b/server/src/collection/mod.rs @@ -65,11 +65,14 @@ impl Collection { let prompt_path = OwnedObjectPath::from(prompt.path().clone()); let collection = self.clone(); + let service = self.service.clone(); let caller_owned = caller; let action = crate::prompt::PromptAction::new(move |unlock_secret: Option| async move { - // Unlock the collection - collection.set_locked(false, unlock_secret).await?; + // Unlock the collection, completing a pending migration + service + .unlock_collection(&collection, unlock_secret) + .await?; collection.delete_unlocked(&caller_owned).await?; @@ -201,9 +204,13 @@ impl Collection { let prompt_path = OwnedObjectPath::from(prompt.path().clone()); let collection = self.clone(); + let service = self.service.clone(); let action = crate::prompt::PromptAction::new(move |unlock_secret: Option| async move { - collection.set_locked(false, unlock_secret).await?; + // Unlock the collection, completing a pending migration + service + .unlock_collection(&collection, unlock_secret) + .await?; let item_path = collection .create_item_unlocked(properties, secret, replace) diff --git a/server/src/collection/tests/deletion.rs b/server/src/collection/tests/deletion.rs index fa79e7de..c4c2d508 100644 --- a/server/src/collection/tests/deletion.rs +++ b/server/src/collection/tests/deletion.rs @@ -124,3 +124,28 @@ async fn delete_locked_collection_with_prompt_gnome() -> Result<(), Box Result<(), Box> { delete_locked_collection_with_prompt_impl(PrompterType::Plasma).await } + +// https://github.com/linux-credentials/oo7/issues/696 +#[tokio::test] +async fn delete_locked_pending_migration() -> Result<(), Box> { + let (setup, _temp_dir, collection_path) = crate::tests::pending_migration_setup(None).await?; + + let collection = + oo7::dbus::api::Collection::new(&setup.client_conn, collection_path.clone()).await?; + assert!(collection.is_locked().await?); + + setup + .set_password_queue(vec![oo7::Secret::from("test")]) + .await; + collection.delete(None).await?; + + assert_eq!(setup.server.pending_migrations.lock().await.len(), 0); + let collections = setup.service_api.collections().await?; + assert!( + collections + .iter() + .all(|c| c.inner().path() != collection.inner().path()) + ); + + Ok(()) +} diff --git a/server/src/collection/tests/items.rs b/server/src/collection/tests/items.rs index 919d1d0f..ff642383 100644 --- a/server/src/collection/tests/items.rs +++ b/server/src/collection/tests/items.rs @@ -522,3 +522,70 @@ async fn concurrent_replace_and_lookup() -> Result<(), Box Result<(), Box> { + let (setup, _temp_dir, collection_path) = crate::tests::pending_migration_setup(None).await?; + + let collection = + oo7::dbus::api::Collection::new(&setup.client_conn, collection_path.clone()).await?; + assert!(collection.is_locked().await?); + + setup + .set_password_queue(vec![oo7::Secret::from("test")]) + .await; + let item = collection + .create_item( + "New Item", + &[("application", "test-app")], + &setup.create_dbus_secret("item-secret")?, + false, + None, + ) + .await?; + + assert_eq!(setup.server.pending_migrations.lock().await.len(), 0); + assert!(!collection.is_locked().await?); + let items = collection.items().await?; + assert_eq!(items.len(), 2); + assert_eq!(item.label().await?, "New Item"); + assert_eq!(item.secret(&setup.session).await?.value(), b"item-secret"); + let mut has_legacy_item = false; + for i in &items { + if i.label().await? == "foo" { + has_legacy_item = true; + } + } + assert!(has_legacy_item, "The legacy item should have been migrated"); + Ok(()) +} + +// https://github.com/linux-credentials/oo7/issues/696 +#[tokio::test] +async fn delete_item_pending_migration() -> Result<(), Box> { + let (setup, _temp_dir, collection_path) = crate::tests::pending_migration_setup(None).await?; + + let collection = + oo7::dbus::api::Collection::new(&setup.client_conn, collection_path.clone()).await?; + + // Delete the placeholder item reported by SearchItems + let (_, locked) = setup + .service_api + .search_items(&[("xdg:schema", "org.gnome.keyring.Note")]) + .await?; + assert_eq!(locked.len(), 1); + + setup + .set_password_queue(vec![oo7::Secret::from("test")]) + .await; + locked[0].delete(None).await?; + + // The migration completed and the item was deleted from the migrated + // keyring + assert_eq!(setup.server.pending_migrations.lock().await.len(), 0); + assert!(!collection.is_locked().await?); + assert_eq!(collection.items().await?.len(), 0); + + Ok(()) +} diff --git a/server/src/gnome/internal.rs b/server/src/gnome/internal.rs index 53eb9e1d..f313d548 100644 --- a/server/src/gnome/internal.rs +++ b/server/src/gnome/internal.rs @@ -225,7 +225,7 @@ mod tests { use oo7::{Secret, dbus}; use zbus::wire::{ObjectPath, OwnedObjectPath}; - use crate::tests::TestServiceSetup; + use crate::tests::{TestServiceSetup, pending_migration_setup}; /// Proxy for the InternalUnsupportedGuiltRiddenInterface #[zbus::proxy( @@ -455,6 +455,66 @@ mod tests { Ok(()) } + // https://github.com/linux-credentials/oo7/issues/696 + #[tokio::test] + async fn test_change_with_prompt_pending_migration() -> Result<(), Box> { + let (setup, _temp_dir, collection_path) = pending_migration_setup(None).await?; + let internal_proxy = InternalInterfaceProxyProxy::builder(&setup.client_conn) + .build() + .await?; + + let collection = + oo7::dbus::api::Collection::new(&setup.client_conn, collection_path.clone()).await?; + assert!(collection.is_locked().await?); + + // Queue: old password first (to unlock and migrate), then the new + // password (to change to) + let new_password = Secret::text("changed-via-prompt"); + setup + .set_password_queue(vec![Secret::from("test"), new_password.clone()]) + .await; + + let prompt_path = internal_proxy + .change_with_prompt(&collection_path.as_ref()) + .await?; + let prompt_proxy = dbus::api::Prompt::new(&setup.client_conn, prompt_path) + .await? + .unwrap(); + prompt_proxy.receive_completed(None).await?; + + // The migration completed and the keyring holds its items + assert_eq!(setup.server.pending_migrations.lock().await.len(), 0); + assert!(!collection.is_locked().await?); + assert_eq!(collection.items().await?.len(), 1); + assert_eq!(collection.items().await?[0].label().await?, "foo"); + + // The keyring now opens with the new password, not the old one + setup + .service_api + .lock(std::slice::from_ref(&collection_path), None) + .await?; + internal_proxy + .unlock_with_master_password( + &collection_path.as_ref(), + setup.create_dbus_secret(new_password)?.into(), + ) + .await?; + assert!(!collection.is_locked().await?); + setup + .service_api + .lock(std::slice::from_ref(&collection_path), None) + .await?; + let result = internal_proxy + .unlock_with_master_password( + &collection_path.as_ref(), + setup.create_dbus_secret("test")?.into(), + ) + .await; + assert!(result.is_err(), "The old password should no longer unlock"); + + Ok(()) + } + #[tokio::test] async fn test_change_with_prompt_locked_collection() -> Result<(), Box> { let setup = TestServiceSetup::plain_session(true).await?; @@ -649,41 +709,11 @@ mod tests { Ok(()) } - /// Set up a service with a legacy v0 keyring awaiting migration - async fn pending_migration_setup() - -> Result<(TestServiceSetup, tempfile::TempDir, OwnedObjectPath), Box> - { - let temp_dir = tempfile::tempdir()?; - let keyrings_dir = temp_dir.path().join("keyrings"); - tokio::fs::create_dir_all(keyrings_dir.join("v1")).await?; - - let fixture_path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .parent() - .unwrap() - .join("client/fixtures/legacy.keyring"); - tokio::fs::copy(&fixture_path, keyrings_dir.join("legacy.keyring")).await?; - - let setup = - TestServiceSetup::with_disk_keyrings(temp_dir.path().to_path_buf(), None, None).await?; - - let collections = setup.service_api.collections().await?; - let mut legacy_path = None; - for collection in &collections { - if collection.label().await? == "Legacy" { - legacy_path = Some(collection.inner().path().to_owned().into()); - break; - } - } - let legacy_path = legacy_path.expect("Should have Legacy placeholder collection"); - - Ok((setup, temp_dir, legacy_path)) - } - // https://github.com/linux-credentials/oo7/issues/689 #[tokio::test] async fn test_unlock_with_master_password_pending_migration() -> Result<(), Box> { - let (setup, _temp_dir, collection_path) = pending_migration_setup().await?; + let (setup, _temp_dir, collection_path) = pending_migration_setup(None).await?; let internal_proxy = InternalInterfaceProxyProxy::builder(&setup.client_conn) .build() .await?; @@ -713,7 +743,7 @@ mod tests { #[tokio::test] async fn test_unlock_with_master_password_pending_migration_wrong_password() -> Result<(), Box> { - let (setup, _temp_dir, collection_path) = pending_migration_setup().await?; + let (setup, _temp_dir, collection_path) = pending_migration_setup(None).await?; let internal_proxy = InternalInterfaceProxyProxy::builder(&setup.client_conn) .build() .await?; @@ -741,7 +771,7 @@ mod tests { #[tokio::test] async fn test_change_with_master_password_pending_migration() -> Result<(), Box> { - let (setup, _temp_dir, collection_path) = pending_migration_setup().await?; + let (setup, _temp_dir, collection_path) = pending_migration_setup(None).await?; let internal_proxy = InternalInterfaceProxyProxy::builder(&setup.client_conn) .build() .await?; diff --git a/server/src/gnome/prompter.rs b/server/src/gnome/prompter.rs index 074a9099..33ba2cba 100644 --- a/server/src/gnome/prompter.rs +++ b/server/src/gnome/prompter.rs @@ -525,7 +525,8 @@ impl GNOMEPrompterCallback { let collection = prompt .collection() .expect("ChangePassword requires a collection"); - let properties = match collection.set_locked(false, secret).await { + let properties = match self.service.unlock_collection(collection, secret).await + { Ok(()) => { self.change_password_unlocked.store(true, Ordering::Relaxed); Properties::for_change_password_new( diff --git a/server/src/item/mod.rs b/server/src/item/mod.rs index b8279eef..4881a603 100644 --- a/server/src/item/mod.rs +++ b/server/src/item/mod.rs @@ -49,14 +49,20 @@ impl Item { let item_self = self.clone(); let coll = collection.clone(); + let service = self.service.clone(); let caller = caller.to_owned(); let action = crate::prompt::PromptAction::new( move |unlock_secret: Option| async move { - // Unlock the collection - coll.set_locked(false, unlock_secret).await?; + service.unlock_collection(&coll, unlock_secret).await?; + + // The migration replaced the placeholder items + let item = coll + .item_from_path(item_self.path()) + .await + .ok_or_else(|| ServiceError::NoSuchObject(item_self.path().to_string()))?; // Now delete the item - item_self.delete_unlocked(&coll, &caller).await?; + item.delete_unlocked(&coll, &caller).await?; Ok(zbus::wire::Variant::new(OwnedObjectPath::default()) .try_into_owned() diff --git a/server/src/pam_listener/mod.rs b/server/src/pam_listener/mod.rs index a4c3c856..79fea633 100644 --- a/server/src/pam_listener/mod.rs +++ b/server/src/pam_listener/mod.rs @@ -306,10 +306,17 @@ impl PamListener { tracing::debug!("Attempting to unlock collection: {}", collection.path()); // Try to unlock with the provided secret - if let Err(e) = collection.set_locked(false, Some(secret.clone())).await { - tracing::debug!("Failed to unlock collection {}: {}", collection.path(), e); - } else { - tracing::info!("Unlocked collection: {}", collection.path()); + match self + .service + .unlock_collection(collection, Some(secret.clone())) + .await + { + Ok(()) => { + tracing::info!("Unlocked collection: {}", collection.path()); + } + Err(e) => { + tracing::debug!("Failed to unlock collection {}: {}", collection.path(), e); + } } } } @@ -352,7 +359,11 @@ impl PamListener { ); drop(keyring_guard); - if let Err(e) = collection.set_locked(false, Some(old_secret.clone())).await { + if let Err(e) = self + .service + .unlock_collection(collection, Some(old_secret.clone())) + .await + { tracing::warn!( "Failed to unlock collection {} with old password: {}", path, diff --git a/server/src/pam_listener/tests.rs b/server/src/pam_listener/tests.rs index ffb00628..10c684c9 100644 --- a/server/src/pam_listener/tests.rs +++ b/server/src/pam_listener/tests.rs @@ -404,3 +404,78 @@ async fn message_serialization() -> Result<(), Box> { Ok(()) } + +// https://github.com/linux-credentials/oo7/issues/696 +#[tokio::test] +async fn pam_wrong_secret_keeps_pending_migration_locked() -> Result<(), Box> +{ + let temp_dir = tempfile::tempdir()?; + let (setup, _temp_dir, _) = + crate::tests::pending_migration_setup(Some(temp_dir.path().join("pam.sock"))).await?; + + let legacy = setup + .server + .collection_by_label("Legacy") + .await + .expect("Legacy placeholder collection should exist"); + assert!(legacy.is_locked().await); + + let socket_path = spawn_pam_listener(&setup.server).await; + + let message = create_pam_message( + PamOperation::Unlock, + "testuser", + &[], + Secret::from("wrong-password").as_bytes(), + ); + send_pam_message(&socket_path, &message).await?; + + let legacy = setup + .server + .collection_by_label("Legacy") + .await + .expect("Legacy placeholder collection should exist"); + assert!( + legacy.is_locked().await, + "The placeholder should stay locked, so the migration can be retried" + ); + assert_eq!( + setup.server.pending_migrations.lock().await.len(), + 1, + "The migration should still be pending" + ); + + Ok(()) +} + +// https://github.com/linux-credentials/oo7/issues/696 +#[tokio::test] +async fn pam_change_password_pending_migration() -> Result<(), Box> { + let socket_dir = tempfile::tempdir()?; + let (setup, data_dir, _) = + crate::tests::pending_migration_setup(Some(socket_dir.path().join("pam.sock"))).await?; + + let socket_path = spawn_pam_listener(&setup.server).await; + + let new_secret = Secret::from("new-password"); + let message = create_pam_message( + PamOperation::ChangePassword, + "testuser", + Secret::from("test").as_bytes(), + new_secret.as_bytes(), + ); + send_pam_message(&socket_path, &message).await?; + + assert_eq!( + setup.server.pending_migrations.lock().await.len(), + 0, + "The migration should have completed" + ); + + let keyring = UnlockedKeyring::open_at(data_dir.path(), "legacy", Some(new_secret)).await?; + let items = keyring.items().await?; + assert_eq!(items.len(), 1); + assert_eq!(items[0].label(), "foo"); + + Ok(()) +} diff --git a/server/src/prompt/mod.rs b/server/src/prompt/mod.rs index 17d30b34..11c85d8d 100644 --- a/server/src/prompt/mod.rs +++ b/server/src/prompt/mod.rs @@ -287,7 +287,13 @@ impl Prompt { }; // Execute the unlock action after successful validation - let result_value = action.execute(secret).await?; + let result_value = match action.execute(secret).await { + Ok(value) => value, + Err(err) => { + self.emit_dismissed().await; + return Err(err); + } + }; let prompt_path = self.path().to_owned(); let signal_emitter = self.service.signal_emitter(&prompt_path)?; diff --git a/server/src/tests.rs b/server/src/tests.rs index e0f83fd1..30f95f85 100644 --- a/server/src/tests.rs +++ b/server/src/tests.rs @@ -449,3 +449,44 @@ impl TestServiceSetup { Ok(()) } } + +/// Set up a service with a legacy v0 keyring awaiting migration, and +/// return the path of its locked placeholder collection. The fixture +/// keyring is encrypted with "test" and holds a single item labeled +/// "foo". +pub(crate) async fn pending_migration_setup( + pam_socket: Option, +) -> Result< + ( + TestServiceSetup, + tempfile::TempDir, + zbus::wire::OwnedObjectPath, + ), + Box, +> { + let temp_dir = tempfile::tempdir()?; + let keyrings_dir = temp_dir.path().join("keyrings"); + tokio::fs::create_dir_all(keyrings_dir.join("v1")).await?; + + let fixture_path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .parent() + .unwrap() + .join("client/fixtures/legacy.keyring"); + tokio::fs::copy(&fixture_path, keyrings_dir.join("legacy.keyring")).await?; + + let setup = + TestServiceSetup::with_disk_keyrings(temp_dir.path().to_path_buf(), pam_socket, None) + .await?; + + let collections = setup.service_api.collections().await?; + let mut legacy_path = None; + for collection in &collections { + if collection.label().await? == "Legacy" { + legacy_path = Some(collection.inner().path().to_owned().into()); + break; + } + } + let legacy_path = legacy_path.expect("Should have Legacy placeholder collection"); + + Ok((setup, temp_dir, legacy_path)) +}