Skip to content

Commit a6597cc

Browse files
authored
Merge pull request #28 from josephlr/read
Fix race conditions in Keyring::read
2 parents 4a4cfb5 + f7cfd18 commit a6597cc

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

src/api.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -250,17 +250,26 @@ impl Keyring {
250250
)
251251
})?;
252252
unsafe { buffer.set_len((actual_sz as usize) / mem::size_of::<KeyringSerial>()) };
253-
let keys = buffer
254-
.iter()
255-
.map(|&id| Key::new_impl(id))
256-
.partition(|key| key.description().unwrap().type_ == keytypes::Keyring::name());
257-
Ok((
258-
keys.1,
259-
keys.0
260-
.iter()
261-
.map(|key| Keyring::new_impl(key.id))
262-
.collect::<Vec<_>>(),
263-
))
253+
254+
let mut keys = Vec::new();
255+
let mut keyrings = Vec::new();
256+
for key in buffer.into_iter().map(|id| Key::new_impl(id)) {
257+
match key.description() {
258+
Ok(description) => {
259+
if description.type_ == keytypes::Keyring::name() {
260+
keyrings.push(Keyring::new_impl(key.id))
261+
} else {
262+
keys.push(key)
263+
}
264+
},
265+
// Keys can be invalidated between reading the keyring and
266+
// reading the child key's description. If this happens, we get
267+
// ENOKEY and just skip that key.
268+
Err(errno::Errno(libc::ENOKEY)) => {},
269+
Err(e) => return Err(e),
270+
}
271+
}
272+
Ok((keys, keyrings))
264273
}
265274

266275
/// Attach the persistent keyring for the current user to the current keyring.

0 commit comments

Comments
 (0)