key: Pass --keyname and --accept-cached to systemd-ask-password

Before asking for a passphrase, mount.bcachefs searches for an existing
key in the keyring containing a passphrase for the filesystem. This
means that the user only has to enter the passphrase once when mounting
the filesystem multiple times.

However, if the key appears in between the check and the time when
systemd-ask-password queries the user for a password, the existing key
will not be reused. Also, when multiple instances of mount.bcachefs
are started in parallel for the same filesystem (such as during boot),
each of them will see that the key is not in the keyring and start an
instance of systemd-ask-password, meaning that the user will be queried
multiple times.

Fix the race condition by passing the --keyname and --accept-cached
options to systemd-ask-password which also makes it try itself to
retreive a cached password from the keyring before querying the user
for a password.
This commit is contained in:
beviu 2025-09-10 18:59:48 +02:00
parent e5f4be87a8
commit 69cb6428ba
No known key found for this signature in database
GPG Key ID: C6AAE70FD32E0112

View File

@ -159,6 +159,8 @@ impl Passphrase {
let output = Command::new("systemd-ask-password")
.arg("--icon=drive-harddisk")
.arg(format!("--id=bcachefs:{}", uuid.as_hyphenated()))
.arg(format!("--keyname={}", uuid.as_hyphenated()))
.arg("--accept-cached")
.arg("-n")
.arg("Enter passphrase: ")
.stdin(Stdio::inherit())