From 6b568c9b2fb3e29a0efa252fdd9ee1cd55d85212 Mon Sep 17 00:00:00 2001 From: Bilal Elmoussaoui Date: Thu, 19 Nov 2026 10:42:04 +0100 Subject: [PATCH 1/3] server: Handle pending migrations when unlocking an item path The item branch of perform_unlock skipped the migration of a pending legacy keyring and unlocked the empty placeholder instead. --- server/src/service/locking.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/server/src/service/locking.rs b/server/src/service/locking.rs index 03608af6..571bf080 100644 --- a/server/src/service/locking.rs +++ b/server/src/service/locking.rs @@ -186,7 +186,13 @@ impl Service { if let Some((collection, item, is_locked)) = found_collection { if is_locked { - let _ = collection.set_locked(false, secret.clone()).await; + if let Err(e) = self.unlock_collection(&collection, secret.clone()).await { + tracing::warn!( + "Failed to unlock collection {}: {}", + collection.path(), + e + ); + } } else { let keyring = collection.keyring.read().await; match keyring.as_ref() { @@ -195,7 +201,15 @@ impl Service { } _ => { drop(keyring); - let _ = collection.set_locked(false, secret.clone()).await; + if let Err(e) = + self.unlock_collection(&collection, secret.clone()).await + { + tracing::warn!( + "Failed to unlock collection {}: {}", + collection.path(), + e + ); + } } } } From 995f25c86f9216c3ab893d7857582c019b5c83ad Mon Sep 17 00:00:00 2001 From: Bilal Elmoussaoui Date: Thu, 19 Nov 2026 11:24:17 +0100 Subject: [PATCH 2/3] client: Expose the items of legacy keyrings Legacy keyring files keep the items' attributes outside the encrypted block, with values stored as MD5 digests: expose the items carrying those attributes, locked for encrypted keyrings and unlocked with their data for plain keyrings. --- client/src/crypto/native.rs | 12 +++ client/src/crypto/openssl.rs | 10 ++ .../src/file/api/legacy_keyring/encrypted.rs | 98 +++++++++++++++---- client/src/file/api/legacy_keyring/mod.rs | 41 ++++++++ client/src/file/api/legacy_keyring/plain.rs | 13 +++ 5 files changed, 155 insertions(+), 19 deletions(-) diff --git a/client/src/crypto/native.rs b/client/src/crypto/native.rs index 5a1e7ece..c85bc38c 100644 --- a/client/src/crypto/native.rs +++ b/client/src/crypto/native.rs @@ -156,6 +156,18 @@ pub(crate) fn verify_checksum_md5(digest: impl AsRef<[u8]>, content: impl AsRef< .into() } +/// The lowercase hex encoded MD5 digest of `value`, as stored in the +/// cleartext attribute section of legacy keyring files. +pub fn md5_hex(value: &str) -> String { + let mut hasher = Md5::new(); + hasher.update(value.as_bytes()); + hasher + .finalize_fixed() + .iter() + .map(|b| format!("{b:02x}")) + .collect() +} + pub(crate) fn derive_key( secret: impl AsRef<[u8]>, key_strength: Result<(), file::WeakKeyError>, diff --git a/client/src/crypto/openssl.rs b/client/src/crypto/openssl.rs index 97a51d0a..ec7a1ebb 100644 --- a/client/src/crypto/openssl.rs +++ b/client/src/crypto/openssl.rs @@ -172,6 +172,16 @@ pub(crate) fn verify_checksum_md5(digest: impl AsRef<[u8]>, content: impl AsRef< ) } +/// The lowercase hex encoded MD5 digest of `value`, as stored in the +/// cleartext attribute section of legacy keyring files. +pub fn md5_hex(value: &str) -> String { + hash(MessageDigest::md5(), value.as_bytes()) + .unwrap() + .iter() + .map(|b| format!("{b:02x}")) + .collect() +} + pub(crate) fn derive_key( secret: impl AsRef<[u8]>, key_strength: Result<(), file::WeakKeyError>, diff --git a/client/src/file/api/legacy_keyring/encrypted.rs b/client/src/file/api/legacy_keyring/encrypted.rs index 539f597d..38219f2d 100644 --- a/client/src/file/api/legacy_keyring/encrypted.rs +++ b/client/src/file/api/legacy_keyring/encrypted.rs @@ -8,7 +8,14 @@ use std::{ use endi::{Endian, ReadBytes}; use zeroize::{Zeroize, ZeroizeOnDrop}; +#[cfg(any(feature = "unstable", test))] +use super::super::EncryptedItem; use super::{FILE_HEADER, FILE_HEADER_LEN, item_type_schema}; +#[cfg(any(feature = "unstable", test))] +use crate::{ + Mac, + file::{Item, LockedItem}, +}; use crate::{ Secret, XDG_SCHEMA_ATTRIBUTE, crypto, file::{Error, UnlockedItem}, @@ -17,13 +24,22 @@ use crate::{ pub const MAJOR_VERSION: u8 = 0; pub const MINOR_VERSION: u8 = 0; +/// An item of the cleartext attribute section of the keyring file. +#[derive(Debug)] +struct HashedItem { + item_type: u32, + #[cfg_attr(not(feature = "unstable"), allow(dead_code))] + attributes: HashMap, +} + #[derive(Debug, Zeroize, ZeroizeOnDrop)] pub struct Keyring { salt: Vec, iteration_count: u32, encrypted_content: Vec, // In the same order as the encrypted items - item_types: Vec, + #[zeroize(skip)] + hashed_items: Vec, } impl Keyring { @@ -86,10 +102,36 @@ impl Keyring { Ok(result) } + /// The items of the keyring, locked. + /// + /// The items have no label nor secret until the keyring is migrated + /// and unlocked; they only carry the cleartext attributes of the + /// keyring file. + #[cfg(any(feature = "unstable", test))] + pub fn items(&self) -> Vec { + self.hashed_items + .iter() + .map(|item| { + Item::Locked(LockedItem { + inner: EncryptedItem { + hashed_attributes: item + .attributes + .iter() + .map(|(key, value)| (key.clone(), Mac::new(value.as_bytes().to_vec()))) + .collect(), + // The secret is only available once decrypted + blob: Vec::new(), + }, + }) + }) + .collect() + } + fn read_items(self, decrypted: &[u8]) -> Result, Error> { let mut cursor = Cursor::new(decrypted); - let mut items = Vec::with_capacity(self.item_types.len()); - for &item_type in &self.item_types { + let mut items = Vec::with_capacity(self.hashed_items.len()); + for hashed_item in &self.hashed_items { + let item_type = hashed_item.item_type; let display_name = Self::read_string(&mut cursor)?.unwrap_or_else(|| { #[cfg(feature = "tracing")] tracing::warn!("Item has no label, defaulting to empty"); @@ -150,21 +192,27 @@ impl Keyring { Ok((hi << 32) | lo) } - fn read_hashed_item_types(cursor: &mut Cursor<&[u8]>, count: usize) -> Result, Error> { - let mut item_types = Vec::new(); + fn read_hashed_items( + cursor: &mut Cursor<&[u8]>, + count: usize, + ) -> Result, Error> { + let mut items = Vec::with_capacity(count); for _ in 0..count { let _id = cursor.read_u32(Endian::Big)?; - item_types.push(cursor.read_u32(Endian::Big)?); - let num_attributes = cursor.read_u32(Endian::Big)?; + let item_type = cursor.read_u32(Endian::Big)?; + let num_attributes = cursor.read_u32(Endian::Big)? as usize; + let mut attributes = HashMap::with_capacity(num_attributes); for _ in 0..num_attributes { - let _name = Self::read_string(cursor)?; - match cursor.read_u32(Endian::Big)? { - 0 => { - let _value = Self::read_string(cursor); - } - 1 => { - let _value = cursor.read_u32(Endian::Big); - } + let name = Self::read_string(cursor)?.ok_or_else(|| { + io::Error::new(io::ErrorKind::InvalidInput, "empty attribute name") + })?; + let value = match cursor.read_u32(Endian::Big)? { + 0 => Self::read_string(cursor)? + .ok_or_else(|| { + io::Error::new(io::ErrorKind::InvalidInput, "empty attribute value") + })? + .to_string(), + 1 => cursor.read_u32(Endian::Big)?.to_string(), _ => { return Err(io::Error::new( io::ErrorKind::InvalidInput, @@ -172,10 +220,22 @@ impl Keyring { ) .into()); } - } + }; + attributes.insert(name.to_string(), value); + } + // Mirror the schema synthesis applied to the decrypted items, + // so schema based searches match the migrated items + if !attributes.contains_key(XDG_SCHEMA_ATTRIBUTE) + && let Some(schema) = item_type_schema(item_type) + { + attributes.insert(XDG_SCHEMA_ATTRIBUTE.to_owned(), crypto::md5_hex(schema)); } + items.push(HashedItem { + item_type, + attributes, + }); } - Ok(item_types) + Ok(items) } fn skip_acls(cursor: &mut Cursor<&[u8]>, count: usize) -> Result<(), Error> { @@ -211,7 +271,7 @@ impl Keyring { let _ = cursor.read_u32(Endian::Big)?; } let item_count = cursor.read_u32(Endian::Big)? as usize; - let item_types = Self::read_hashed_item_types(&mut cursor, item_count)?; + let hashed_items = Self::read_hashed_items(&mut cursor, item_count)?; let mut size = cursor.read_u32(Endian::Big)? as usize; let pos = cursor.position() as usize; if size > cursor.get_ref()[pos..].len() { @@ -226,7 +286,7 @@ impl Keyring { salt, iteration_count, encrypted_content, - item_types, + hashed_items, }) } } diff --git a/client/src/file/api/legacy_keyring/mod.rs b/client/src/file/api/legacy_keyring/mod.rs index 80a1ee52..fecec8f0 100644 --- a/client/src/file/api/legacy_keyring/mod.rs +++ b/client/src/file/api/legacy_keyring/mod.rs @@ -8,6 +8,8 @@ mod plain; pub use encrypted::MAJOR_VERSION; +#[cfg(any(feature = "unstable", test))] +use crate::file::Item; use crate::{ Secret, file::{Error, UnlockedItem}, @@ -64,6 +66,20 @@ impl Keyring { Self::Plain(_) => Ok(true), } } + + /// The items of the keyring. + /// + /// Items of an encrypted keyring are locked: they carry no label nor + /// secret until the keyring is migrated and unlocked, only the + /// cleartext attributes of the keyring file. Items of a plain + /// keyring are unlocked and carry their data. + #[cfg(any(feature = "unstable", test))] + pub fn items(&self) -> Vec { + match self { + Self::Encrypted(keyring) => keyring.items(), + Self::Plain(keyring) => keyring.items(), + } + } } impl TryFrom<&[u8]> for Keyring { @@ -107,6 +123,20 @@ mod tests { .expect("validation should not fail") ); + // The cleartext attributes are searchable without the secret, + // values are stored as their MD5 digests + let items = keyring.items(); + assert_eq!(items.len(), 1); + assert!(items[0].is_locked()); + assert!(items[0].matches_attributes( + &[( + crate::XDG_SCHEMA_ATTRIBUTE, + crate::crypto::md5_hex("org.gnome.keyring.Note") + )], + None + )); + assert!(!items[0].matches_attributes(&[("unique", "nope")], None)); + let items = keyring .decrypt_items(&Secret::from("test")) .expect("decryption should not fail"); @@ -125,5 +155,16 @@ mod tests { .validate_secret(&Secret::from("anything")) .expect("validation should not fail") ); + + // The items are unlocked and carry their data + let items = keyring.items(); + assert_eq!(items.len(), 3); + assert!(items.iter().all(|item| !item.is_locked())); + assert!(items.iter().any(|item| { + item.matches_attributes( + &[(crate::XDG_SCHEMA_ATTRIBUTE, "org.remmina.Password")], + None, + ) + })); } } diff --git a/client/src/file/api/legacy_keyring/plain.rs b/client/src/file/api/legacy_keyring/plain.rs index 4a98d275..cb157dc8 100644 --- a/client/src/file/api/legacy_keyring/plain.rs +++ b/client/src/file/api/legacy_keyring/plain.rs @@ -26,6 +26,8 @@ use std::{collections::HashMap, io}; use zeroize::Zeroizing; use super::{FILE_HEADER_LEN, item_type_schema}; +#[cfg(any(feature = "unstable", test))] +use crate::file::Item; use crate::{ Secret, XDG_SCHEMA_ATTRIBUTE, file::{Error, UnlockedItem}, @@ -43,6 +45,17 @@ impl Keyring { self.items } + /// The items of this keyring, unlocked. + /// + /// As the keyring is not encrypted, the items carry their data. + #[cfg(any(feature = "unstable", test))] + pub fn items(&self) -> Vec { + self.items + .iter() + .map(|item| Item::Unlocked(item.clone())) + .collect() + } + fn read_item(id: &str, group: &Group, attribute_groups: &[&Group]) -> UnlockedItem { let label = group.string("display-name").unwrap_or_else(|| { #[cfg(feature = "tracing")] From 70e7bed8194aca1323eca80cffcbfecd5e95e161 Mon Sep 17 00:00:00 2001 From: Bilal Elmoussaoui Date: Thu, 19 Nov 2026 11:32:47 +0100 Subject: [PATCH 3/3] server: Report the items of a pending migration as locked SearchItems found nothing in an empty placeholder, so clients created new secrets instead of unlocking the keyring holding the legacy ones. Search the placeholder items by hashing the searched values, and hand their paths to the migrated items on unlock. https://github.com/linux-credentials/oo7/issues/690 --- server/src/collection/mod.rs | 50 ++++++++++++-- server/src/gnome/internal.rs | 6 +- server/src/migration.rs | 15 ++++ server/src/service/lifecycle.rs | 20 +++++- server/src/service/tests/discovery.rs | 99 ++++++++++++++++++++++++++- 5 files changed, 180 insertions(+), 10 deletions(-) diff --git a/server/src/collection/mod.rs b/server/src/collection/mod.rs index bad65b9e..80a07931 100644 --- a/server/src/collection/mod.rs +++ b/server/src/collection/mod.rs @@ -544,7 +544,25 @@ impl Collection { return Ok(Vec::new()); }; if keyring.is_locked() { - return Ok(Vec::new()); + drop(keyring_guard); + + // The placeholder items of a pending migration store their + // attribute values as MD5 digests; hash the searched values + // to match them. The generic schema fallback is compared + // as-is. + let hashed: HashMap = attributes + .iter() + .map(|(key, value)| { + if key.as_str() == oo7::XDG_SCHEMA_ATTRIBUTE + && value.as_str() == oo7::GENERIC_SCHEMA + { + (key.clone(), value.clone()) + } else { + (key.clone(), oo7::crypto::md5_hex(value)) + } + }) + .collect(); + return self.search_items_with_key(&hashed, None, false).await; } let key = keyring .as_unlocked() @@ -667,16 +685,25 @@ impl Collection { } pub async fn dispatch_items(&self) -> Result<(), Error> { - let keyring_guard = self.keyring.read().await; - let keyring = keyring_guard.as_ref().unwrap(); + let keyring_items = { + let keyring_guard = self.keyring.read().await; + let keyring = keyring_guard.as_ref().unwrap(); + keyring.items().await? + }; + self.dispatch_file_items(keyring_items).await + } - let keyring_items = keyring.items().await?; + /// Register the given file items on the object server and notify + /// clients. + pub async fn dispatch_file_items( + &self, + keyring_items: Vec, + ) -> Result<(), Error> { tracing::debug!( "Dispatching {} items for collection `{}`.", keyring_items.len(), self.path ); - drop(keyring_guard); let mut dispatched_items = Vec::new(); { @@ -713,11 +740,24 @@ impl Collection { Ok(()) } + /// Remove all items from the object server and the collection. + async fn clear_items(&self) -> Result<(), Error> { + let mut items = self.items.lock().await; + let object_server = self.service.object_server(); + for item in items.iter() { + object_server.remove::(item.path()).await?; + } + items.clear(); + Ok(()) + } + /// Replace the keyring with a just-migrated one, dispatch its items /// and emit the corresponding change signals. pub async fn apply_migrated_keyring(&self, keyring: Keyring) -> Result<(), Error> { *self.keyring.write().await = Some(keyring); + // Drop the placeholder items, the migrated items take their paths + self.clear_items().await?; self.dispatch_items().await?; // The keyring swap bypasses set_locked, so emit Locked here. diff --git a/server/src/gnome/internal.rs b/server/src/gnome/internal.rs index 2ff5cef1..53eb9e1d 100644 --- a/server/src/gnome/internal.rs +++ b/server/src/gnome/internal.rs @@ -691,7 +691,9 @@ mod tests { let collection = oo7::dbus::api::Collection::new(&setup.client_conn, collection_path.clone()).await?; assert!(collection.is_locked().await?); - assert!(collection.items().await?.is_empty()); + // The placeholder only exposes the locked stand-in item + assert_eq!(collection.items().await?.len(), 1); + assert!(collection.items().await?[0].is_locked().await?); let dbus_secret = setup.create_dbus_secret("test")?; internal_proxy @@ -700,6 +702,8 @@ mod tests { assert!(!collection.is_locked().await?); assert_eq!(collection.items().await?.len(), 1); + assert!(!collection.items().await?[0].is_locked().await?); + assert_eq!(collection.items().await?[0].label().await?, "foo"); assert_eq!(setup.server.pending_migrations.lock().await.len(), 0); Ok(()) diff --git a/server/src/migration.rs b/server/src/migration.rs index a23de266..0638d938 100644 --- a/server/src/migration.rs +++ b/server/src/migration.rs @@ -65,6 +65,21 @@ impl PendingMigration { } } + /// The placeholder items of the legacy keyring, exposing its + /// cleartext attributes so they can be searched while the keyring + /// is locked. + pub async fn items(&self) -> Result, Error> { + match self { + Self::V0 { path, .. } => { + let content = tokio::fs::read(path).await?; + let keyring = oo7::file::api::LegacyKeyring::try_from(content.as_slice())?; + Ok(keyring.items()) + } + #[cfg(feature = "kwallet_migration")] + Self::KWallet { .. } => Ok(Vec::new()), + } + } + /// Attempt to migrate this keyring with the provided secret pub async fn migrate( &self, diff --git a/server/src/service/lifecycle.rs b/server/src/service/lifecycle.rs index 4446f7c6..a194470f 100644 --- a/server/src/service/lifecycle.rs +++ b/server/src/service/lifecycle.rs @@ -259,7 +259,25 @@ impl Service { // Now register on D-Bus and dispatch items without holding the lock for (collection, alias) in &built_collections { - collection.dispatch_items().await?; + // A pending migration dispatches the placeholder items of the + // legacy keyring, so they can be searched while locked + let migration = { + let pending = self.pending_migrations.lock().await; + pending.get(collection.name()).cloned() + }; + match migration { + Some(migration) => { + let items = migration.items().await.unwrap_or_else(|e| { + tracing::warn!( + "Failed to read the items of the pending migration of '{}': {e}", + collection.name() + ); + Vec::new() + }); + collection.dispatch_file_items(items).await?; + } + None => collection.dispatch_items().await?, + } object_server .at(collection.path(), collection.clone()) .await?; diff --git a/server/src/service/tests/discovery.rs b/server/src/service/tests/discovery.rs index 2d6a3f77..7683ce2d 100644 --- a/server/src/service/tests/discovery.rs +++ b/server/src/service/tests/discovery.rs @@ -257,12 +257,12 @@ async fn unlock_pending_v0_migration_emits_change_signals() -> Result<(), Box Result<(), Box> { + let temp_dir = tempfile::tempdir()?; + let keyrings_dir = temp_dir.path().join("keyrings"); + tokio::fs::create_dir_all(keyrings_dir.join("v1")).await?; + + // The fixture keyring holds a single item with the + // org.gnome.keyring.Note schema + 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 (unlocked, locked) = setup + .service_api + .search_items(&[("xdg:schema", "org.gnome.keyring.Note")]) + .await?; + assert!(unlocked.is_empty(), "No item should be unlocked"); + assert_eq!(locked.len(), 1, "The placeholder item should be locked"); + assert_eq!( + locked[0].inner().path().as_str(), + "/org/freedesktop/secrets/collection/legacy/1" + ); + assert!(locked[0].is_locked().await?); + + // Non-matching attributes find nothing + let (unlocked, locked) = setup + .service_api + .search_items(&[("application", "nope")]) + .await?; + assert!(unlocked.is_empty()); + assert!(locked.is_empty()); + + Ok(()) +} + +// https://github.com/linux-credentials/oo7/issues/690 +// +// Unlocking the item path returned by SearchItems migrates the keyring, +// and the path resolves to the migrated item. +#[tokio::test] +async fn unlock_item_path_pending_migration() -> Result<(), 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 (_, locked) = setup + .service_api + .search_items(&[("xdg:schema", "org.gnome.keyring.Note")]) + .await?; + assert_eq!(locked.len(), 1); + let item_path = locked[0].inner().path().to_owned(); + + setup.set_password_queue(vec![Secret::from("test")]).await; + let unlocked = setup + .service_api + .unlock(std::slice::from_ref(&item_path), None) + .await?; + assert_eq!(unlocked.len(), 1, "Should have unlocked the item"); + + assert_eq!( + setup.server.pending_migrations.lock().await.len(), + 0, + "The migration should have completed" + ); + + // The same path now resolves to the migrated item, with its data + let item = oo7::dbus::api::Item::new(&setup.client_conn, item_path).await?; + assert!(!item.is_locked().await?); + assert_eq!(item.label().await?, "foo"); + assert_eq!(item.secret(&setup.session).await?.value(), b"foo"); + + Ok(()) +} + #[tokio::test] async fn discover_v0_keyrings() -> Result<(), Box> { let temp_dir = tempfile::tempdir()?;