Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions client/src/file/api/legacy_keyring/encrypted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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"))
Expand Down
4 changes: 2 additions & 2 deletions client/src/file/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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;

Expand Down
6 changes: 5 additions & 1 deletion server/src/collection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,13 @@ pub(crate) fn alias_path(alias: &str) -> Option<OwnedObjectPath> {
}

pub(crate) fn collection_path(label: &str) -> Result<OwnedObjectPath, wire::Error> {
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}"
Expand Down
5 changes: 4 additions & 1 deletion server/src/collection/tests/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ async fn timestamps() -> Result<(), Box<dyn std::error::Error>> {
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"
Expand Down
56 changes: 40 additions & 16 deletions server/src/pam_listener/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -330,20 +338,29 @@ impl PamListener {
old_secret: &Secret,
new_secret: &Secret,
) -> Result<usize, Error> {
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;
};

Expand All @@ -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);

Expand All @@ -366,7 +383,7 @@ impl PamListener {
{
tracing::warn!(
"Failed to unlock collection {} with old password: {}",
path,
collection.path(),
e
);
continue;
Expand All @@ -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;
};

Expand All @@ -388,7 +405,7 @@ impl PamListener {
Err(e) => {
tracing::warn!(
"Failed to validate old password for collection {}: {}",
path,
collection.path(),
e
);
continue;
Expand All @@ -398,15 +415,18 @@ impl PamListener {
if !can_decrypt {
tracing::debug!(
"Old password does not match keyring {} password, skipping",
path
collection.path()
);
continue;
}

// 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
Expand All @@ -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
);
}
}
}
Expand Down
19 changes: 14 additions & 5 deletions server/src/service/locking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -225,10 +225,18 @@ impl Service {

pub async fn migrate_pending_keyrings(&self, secret: &Secret) -> Vec<String> {
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<PendingMigration> = 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 {
Expand Down Expand Up @@ -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!(
Expand All @@ -296,6 +304,7 @@ impl Service {
}
}

let mut pending = self.pending_migrations.lock().await;
for name in &to_remove {
pending.remove(name);
}
Expand Down
20 changes: 20 additions & 0 deletions server/src/service/tests/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,23 @@ async fn complete_collection_creation_no_pending() -> Result<(), Box<dyn std::er
);
Ok(())
}

// An empty label must not panic the daemon
#[tokio::test]
async fn create_collection_empty_label() -> Result<(), Box<dyn std::error::Error>> {
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(())
}
Loading