Skip to content

Commit

Permalink
fix(key): search for key in all relevant keyrings
Browse files Browse the repository at this point in the history
Previously, using `bcachefs unlock -k session` would still cause mount
to ask for a passphrase.

Signed-off-by: Thomas Mühlbacher <[email protected]>
  • Loading branch information
tmuehlbacher committed Jun 18, 2024
1 parent e4271d7 commit f72ded6
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,32 @@ impl KeyHandle {
}
}

pub fn new_from_search(uuid: &Uuid) -> Result<Self> {
let key_name = Self::format_key_name(uuid);
let key_name = CStr::as_ptr(&key_name);
fn search_keyring(keyring: i32, key_name: &CStr) -> Result<i64> {
let key_name = CStr::as_ptr(key_name);
let key_type = c_str!("user");

let key_id =
unsafe { keyctl_search(keyutils::KEY_SPEC_USER_KEYRING, key_type, key_name, 0) };
let key_id = unsafe { keyctl_search(keyring, key_type, key_name, 0) };

if key_id > 0 {
info!("Found key in keyring");
Ok(Self {
_uuid: *uuid,
_id: key_id,
})
Ok(key_id)
} else {
Err(ErrnoError(errno::errno()).into())
}
}

pub fn new_from_search(uuid: &Uuid) -> Result<Self> {
let key_name = Self::format_key_name(uuid);

Self::search_keyring(keyutils::KEY_SPEC_SESSION_KEYRING, &key_name)
.or_else(|_| Self::search_keyring(keyutils::KEY_SPEC_USER_KEYRING, &key_name))
.or_else(|_| Self::search_keyring(keyutils::KEY_SPEC_USER_SESSION_KEYRING, &key_name))
.map(|id| Self {
_uuid: *uuid,
_id: id,
})
}

fn wait_for_unlock(uuid: &Uuid) -> Result<Self> {
loop {
match Self::new_from_search(uuid) {
Expand Down

0 comments on commit f72ded6

Please sign in to comment.