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
78 changes: 64 additions & 14 deletions drivers/soc/apple/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -962,10 +962,25 @@ pub(crate) trait ContextDelete {

impl ContextDelete for SepData {
fn delete_context(&self, handle: &[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]) -> Result<()> {
self.delete_unlock_evaluation(handle)
let result = latch_failed_delete(&self.key_context_failed, self.delete_unlock_evaluation(handle));
if result.is_err() {
dev_warn_ratelimited!(self.dev, "sep-key: a key context was not deleted; private key operations disabled until reboot\n");
}
result
}
}

/// A key operation's context that could not be deleted may still authorize
/// the key it was opened for, so /dev/sep-key stops until reboot. A Touch ID
/// unlock evaluation that /dev/sep-bio fails to delete authorizes no key and
/// is only logged (`close_unlock_evaluation`).
fn latch_failed_delete(failed: &kernel::sync::atomic::Atomic<bool>, result: Result<()>) -> Result<()> {
if result.is_err() {
failed.store(true, Relaxed);
}
result
}

/// A credential context the kernel opened for one operation: deleted when
/// this is dropped, on every path out of the code that owns it (success,
/// error, cancel, timeout, signal). Its handle never leaves the kernel.
Expand Down Expand Up @@ -1694,7 +1709,7 @@ impl SepData {
(out.reply.status == 0).then_some(())
})();
if ok.is_none() {
self.close_unlock_evaluation(&handle);
let _ = self.delete_context(&handle);
return Err(EIO);
}
Ok(handle)
Expand All @@ -1714,9 +1729,10 @@ impl SepData {
}
let (status, out) = self.scrd_command(&crate::scrd::scrd_context_create_tracked(uid)).ok_or(EIO)?;
let ctx = crate::scrd::tracked_create_reference(&out).filter(|_| status == 0).ok_or(EIO)?;
let (status, _) = self.scrd_command(&crate::scrd::scrd_context_externalize(&ctx)).ok_or(EIO)?;
if status != 0 {
self.close_unlock_evaluation(&ctx);
// The context exists once created: an externalize that is refused or
// gets no reply still deletes it.
if !matches!(self.scrd_command(&crate::scrd::scrd_context_externalize(&ctx)), Some((0, _))) {
let _ = self.delete_context(&ctx);
return Err(EIO);
}
let mut verify = 0;
Expand Down Expand Up @@ -1766,7 +1782,7 @@ impl SepData {
req.result2 = v;
self.last_key_status.store(0, Relaxed);
let r = self.user_key_sign_in_bag(user, &record, &[0x5a; DIGEST_LEN], Some(&ctx));
self.close_unlock_evaluation(&ctx);
let _ = self.delete_context(&ctx);
req.result = r.err().map(|e| e.to_errno()).unwrap_or(0);
req.enclave_status = self.last_key_status.load(Relaxed) as i32;
dev_info_ratelimited!(
Expand Down Expand Up @@ -1842,12 +1858,12 @@ impl SepData {
r[2] = sign(None);
let (fresh, v) = self.key_context_for(designated_id(user), Some((user.special_handle(), &pw[..len])))?;
r[3] = if v == 0 { sign(Some(&fresh)) } else { v };
self.close_unlock_evaluation(&fresh);
let _ = self.delete_context(&fresh);
self.userbag_unlock(user, &pw[..len])?;
r[7] = self.userbag_lock_phase(user).map(|p| p as i32).unwrap_or(-1);
let (fresh2, v2) = self.key_context_for(designated_id(user), Some((user.special_handle(), &pw[..len])))?;
r[4] = if v2 == 0 { sign(Some(&fresh2)) } else { v2 };
self.close_unlock_evaluation(&fresh2);
let _ = self.delete_context(&fresh2);
Ok(r)
})();
pw.fill(0);
Expand Down Expand Up @@ -1909,7 +1925,7 @@ impl SepData {
}
};
(req.result, req.enclave_status) = sign(&ctx);
self.close_unlock_evaluation(&ctx);
let _ = self.delete_context(&ctx);
let second = if req.mode == SELFTEST_USERBAG_KEY_SAME_BAG {
self.key_context_for(designated_id(user), None)
} else {
Expand All @@ -1918,7 +1934,7 @@ impl SepData {
pw.fill(0);
let (ctx2, v2) = second?;
(req.result2, req.enclave_status2) = sign(&ctx2);
self.close_unlock_evaluation(&ctx2);
let _ = self.delete_context(&ctx2);
dev_info_ratelimited!(
self.dev,
"sep-key: self-test {}: key under uid {}'s bag: first ({}) verify {} -> {} ({}); second ({}) verify {} -> {} ({})\n",
Expand Down Expand Up @@ -1966,7 +1982,7 @@ impl SepData {
let r = self.user_key_sign(&blob, &digest, Some(&ctx));
req.result = r.err().map(|e| e.to_errno()).unwrap_or(0);
req.enclave_status = self.last_key_status.load(Relaxed) as i32;
self.close_unlock_evaluation(&ctx);
let _ = self.delete_context(&ctx);
dev_info_ratelimited!(
self.dev,
"sep-key: self-test presence key with a credential from uid {}'s bag: verify status {}, signature {} (enclave status {})\n",
Expand All @@ -1989,11 +2005,11 @@ impl SepData {
self.last_key_status.store(0, Relaxed);
let ctx = self.passcode_context()?;
(req.result, req.enclave_status) = record(self.user_key_sign(&blob, &digest, Some(&ctx)), self);
self.close_unlock_evaluation(&ctx);
let _ = self.delete_context(&ctx);
self.last_key_status.store(0, Relaxed);
let ctx = self.open_unlock_evaluation(user).ok_or(EIO)?;
(req.result2, req.enclave_status2) = record(self.user_key_sign(&blob, &digest, Some(&ctx)), self);
self.close_unlock_evaluation(&ctx);
let _ = self.delete_context(&ctx);
dev_info_ratelimited!(
self.dev,
"sep-key: self-test presence probe: bag-secret context {} (enclave status {}), unsatisfied context {} (enclave status {})\n",
Expand Down Expand Up @@ -2189,7 +2205,7 @@ impl SepData {
}
SELFTEST_DELETED => {
let ctx = self.open_unlock_evaluation(user).ok_or(EIO)?;
self.close_unlock_evaluation(&ctx);
let _ = self.delete_context(&ctx);
record(self.user_key_sign(&owned, &digest, Some(&ctx)), self)
}
SELFTEST_TOUCH_THEN_DELETED => {
Expand Down Expand Up @@ -2625,6 +2641,40 @@ mod one_op_tests {
assert!(selftest_mode_built(mode));
}
}

// Only a key context's failed delete stops /dev/sep-key: an owned
// context whose delete is confirmed leaves it running, one whose delete
// fails (finished or dropped) stops it, and a later confirmed delete
// does not restart it. Kept below the host harness's slice, which has
// no kernel atomics.
#[test]
fn only_a_failed_key_context_delete_latches() {
struct Latching {
failed: kernel::sync::atomic::Atomic<bool>,
result: Cell<Result<()>>,
}

impl ContextDelete for Latching {
fn delete_context(&self, _: &[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]) -> Result<()> {
latch_failed_delete(&self.failed, self.result.get())
}
}

let d = Latching { failed: kernel::sync::atomic::Atomic::new(false), result: Cell::new(Ok(())) };
let h = [0x54; crate::scrd::SCRD_ACM_HANDLE_LEN];
assert_eq!(OwnedContext::new(&d, h).finish(Ok(7)), Ok(7));
drop(OwnedContext::new(&d, h));
assert!(!d.failed.load(Relaxed));
d.result.set(Err(EIO));
assert_eq!(OwnedContext::new(&d, h).finish(Ok(7)), Err(EIO));
assert!(d.failed.load(Relaxed));
let d = Latching { failed: kernel::sync::atomic::Atomic::new(false), result: Cell::new(Err(EIO)) };
drop(OwnedContext::new(&d, h));
assert!(d.failed.load(Relaxed));
d.result.set(Ok(()));
assert_eq!(OwnedContext::new(&d, h).finish(Ok(7)), Ok(7));
assert!(d.failed.load(Relaxed));
}
}

#[cfg(CONFIG_APPLE_SEP_KUNIT_TEST)]
Expand Down
3 changes: 1 addition & 2 deletions drivers/soc/apple/sep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2779,10 +2779,9 @@ impl SepData {
match self.scrd_command(&crate::scrd::scrd_context_delete(handle)) {
Some((0, _)) => Ok(()),
other => {
self.key_context_failed.store(true, Relaxed);
dev_warn_ratelimited!(
self.dev,
"scrd: deleting the unlock evaluation answered {:?}; private key operations disabled until reboot\n",
"scrd: deleting the unlock evaluation answered {:?}\n",
other.map(|(status, _)| status)
);
Err(EIO)
Expand Down
Loading