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
20 changes: 16 additions & 4 deletions drivers/soc/apple/bio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,28 @@ pub(crate) struct Lockout {
pub(crate) const LOCKOUT_FLAG_EVERY_PASSWORD: u32 = 1;
/// Touch ID waits for the first password since this boot.
pub(crate) const LOCKOUT_FLAG_AFTER_BOOT: u32 = 2;
/// The lockout follows too many failed matches.
pub(crate) const LOCKOUT_FLAG_FAILED_ATTEMPTS: u32 = 4;

impl Lockout {
/// `locked` is a lockout after failed matches (or one recorded before the
/// restart); `after_boot` is the wait for the first password since boot.
/// Either one refuses every verify.
pub(crate) fn new(locked: bool, after_boot: bool, failures: u32) -> Lockout {
/// `locked` is a lockout (or one recorded before the restart);
/// `after_boot` is the wait for the first password since boot. Either
/// one refuses every verify. `failed_attempts` says that the lockout
/// follows too many failed matches; without it, a lockout needs the
/// password for another reason, such as a lapsed match credential.
pub(crate) fn new(
locked: bool,
after_boot: bool,
failed_attempts: bool,
failures: u32,
) -> Lockout {
let mut flags = LOCKOUT_FLAG_EVERY_PASSWORD;
if after_boot {
flags |= LOCKOUT_FLAG_AFTER_BOOT;
}
if locked && failed_attempts {
flags |= LOCKOUT_FLAG_FAILED_ATTEMPTS;
}
Lockout {
locked: u32::from(locked || after_boot),
failures,
Expand Down
94 changes: 72 additions & 22 deletions drivers/soc/apple/sbio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ fn lockout_record_failures(raw: &[u8; LOCKOUT_RECORD_LEN]) -> u32 {
u32::from_le_bytes([raw[12], raw[13], raw[14], raw[15]]).min(VERIFY_RETRY_LIMIT)
}

/// Whether a lockout follows too many failed matches: `VERIFY_RETRY_LIMIT`
/// of them since the last match or password, or the enclave's own lockout
/// after failed matches (which macOS can have caused). Otherwise the lockout
/// needs the password for another reason, such as a lapsed match credential.
fn lockout_after_failures(failures: u32, enclave_lockout: bool) -> bool {
failures >= VERIFY_RETRY_LIMIT || enclave_lockout
}

fn lockout_record_locked(raw: &[u8; LOCKOUT_RECORD_LEN]) -> bool {
raw[..LOCKOUT_MAGIC.len()] == LOCKOUT_MAGIC
&& u32::from_le_bytes([raw[8], raw[9], raw[10], raw[11]]) == LOCKOUT_LOCKED
Expand Down Expand Up @@ -192,22 +200,45 @@ mod lockout_tests {
#[test]
fn after_boot_wait_reports_locked() {
use crate::bio::{Lockout, LOCKOUT_FLAG_AFTER_BOOT, LOCKOUT_FLAG_EVERY_PASSWORD};
let plain = Lockout::new(false, false, 2);
let plain = Lockout::new(false, false, false, 2);
assert_eq!(plain.locked, 0);
assert_eq!(plain.failures, 2);
assert_eq!(plain.flags, LOCKOUT_FLAG_EVERY_PASSWORD);
let waiting = Lockout::new(false, true, 0);
let waiting = Lockout::new(false, true, false, 0);
assert_eq!(waiting.locked, 1);
assert_eq!(waiting.flags, LOCKOUT_FLAG_EVERY_PASSWORD | LOCKOUT_FLAG_AFTER_BOOT);
// A lockout and the wait together: one password ends both.
let both = Lockout::new(true, true, 5);
let both = Lockout::new(true, true, true, 5);
assert_eq!(both.locked, 1);
assert_ne!(both.flags & LOCKOUT_FLAG_AFTER_BOOT, 0);
let locked = Lockout::new(true, false, 5);
let locked = Lockout::new(true, false, true, 5);
assert_eq!(locked.locked, 1);
assert_eq!(locked.flags & LOCKOUT_FLAG_AFTER_BOOT, 0);
}

#[test]
fn lockout_names_failed_attempts() {
use crate::bio::{Lockout, LOCKOUT_FLAG_FAILED_ATTEMPTS};
// Five misses, on the host or in the enclave, are the real lockout.
assert!(lockout_after_failures(VERIFY_RETRY_LIMIT, false));
assert!(lockout_after_failures(0, true));
// Fewer misses and no lockout in the enclave: the password is needed
// for another reason (a lapsed credential, a refused match context).
assert!(!lockout_after_failures(0, false));
assert!(!lockout_after_failures(VERIFY_RETRY_LIMIT - 1, false));
let locked = Lockout::new(true, false, true, VERIFY_RETRY_LIMIT);
assert_ne!(locked.flags & LOCKOUT_FLAG_FAILED_ATTEMPTS, 0);
let lapsed = Lockout::new(true, false, false, 0);
assert_eq!(lapsed.locked, 1);
assert_eq!(lapsed.flags & LOCKOUT_FLAG_FAILED_ATTEMPTS, 0);
// Only a lockout carries the flag; failed matches alone do not.
let open = Lockout::new(false, false, true, VERIFY_RETRY_LIMIT - 1);
assert_eq!(open.flags & LOCKOUT_FLAG_FAILED_ATTEMPTS, 0);
// The wait after boot is not a lockout after failed matches.
let waiting = Lockout::new(false, true, false, 0);
assert_eq!(waiting.flags & LOCKOUT_FLAG_FAILED_ATTEMPTS, 0);
}

#[test]
fn record_round_trips() {
assert!(lockout_record_locked(&lockout_record(true, 5)));
Expand Down Expand Up @@ -5052,29 +5083,42 @@ impl SepData {
Ok(())
}

/// Reports the lockout, and why Touch ID needs the password, so that
/// login screens can say which. Read-only, on both nodes.
fn write_lockout_state(&self, arg: usize) -> Result<()> {
let (locked, failures) = if self.touchid_started.load(Relaxed) {
(
self.bio_lockout.load(Relaxed),
self.bio_nomatch_streak.load(Relaxed),
)
} else {
(self.read_lockout_record(), self.read_recorded_failures())
};
let failed_attempts = locked
&& lockout_after_failures(
failures,
self.enclave_reports_state(LOCK_STATE_LOCKOUT | LOCK_STATE_BIO_LOCKOUT),
);
let state = bio::Lockout::new(
locked,
self.bio_after_boot.load(Relaxed),
failed_attempts,
failures,
);
kernel::uaccess::UserSlice::new(
kernel::uaccess::UserPtr::from_addr(arg),
core::mem::size_of::<bio::Lockout>(),
)
.writer()
.write(&state)
}

pub(crate) fn rearm_ioctl(&self, cmd: u32, arg: usize) -> Result<()> {
if self.shutting_down.load(Relaxed) {
return Err(ENODEV);
}
match cmd {
bio::IOC_LOCKOUT_STATE => {
let locked = if self.touchid_started.load(Relaxed) {
self.bio_lockout.load(Relaxed)
} else {
self.read_lockout_record()
};
let state = bio::Lockout::new(
locked,
self.bio_after_boot.load(Relaxed),
self.bio_nomatch_streak.load(Relaxed),
);
kernel::uaccess::UserSlice::new(
kernel::uaccess::UserPtr::from_addr(arg),
core::mem::size_of::<bio::Lockout>(),
)
.writer()
.write(&state)
}
bio::IOC_LOCKOUT_STATE => self.write_lockout_state(arg),
bio::IOC_REARM => {
bio::require_admin()?;
let mut request: bio::Rearm = kernel::uaccess::UserSlice::new(
Expand Down Expand Up @@ -5530,6 +5574,12 @@ impl SepData {
if cmd == bio::IOC_PREPARE_ENROL {
return self.bio_prepare_enrol(arg);
}
// fprintd holds this node, not the re-arm one: it reads here why a
// verify was refused, to tell the user.
if cmd == bio::IOC_LOCKOUT_STATE {
self.write_lockout_state(arg)?;
return bio::ok();
}
// With xART writes off the enclave cannot persist what an enrolment or
// a match changes. It saves anti-replay state after every match, and a
// refused save has left it unresponsive until reboot. Refuse the
Expand Down
14 changes: 13 additions & 1 deletion include/uapi/linux/apple-sep.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,12 @@ struct sep_bio_attest {
*/
#define SEP_BIO_STATUS_LOCKOUT 8

/* /dev/sep-bio-rearm, which any number of root callers may open. */
/*
* SEP_BIO_LOCKOUT_STATE works on /dev/sep-bio-rearm, which any number of root
* callers may open, and on /dev/sep-bio, so that the verifying daemon can say
* why a verify ended with SEP_BIO_STATUS_LOCKOUT. Older kernels answer it on
* /dev/sep-bio with -ENOTTY.
*/
struct sep_bio_lockout {
__u32 locked; /* 1 while every verify is refused */
__u32 failures; /* failed matches since the last match or re-arm */
Expand All @@ -191,6 +196,13 @@ struct sep_bio_lockout {
* (SEP_BIO_REQUIRE_PASSWORD); 'locked' is 1 then too.
*/
#define SEP_BIO_LOCKOUT_FLAG_AFTER_BOOT 2
/*
* Set while 'locked' follows too many failed matches: five since the last
* match or password, or the enclave's own lockout after failed matches. A
* lockout without it (and without SEP_BIO_LOCKOUT_FLAG_AFTER_BOOT) needs the
* password for another reason, such as a lapsed match credential.
*/
#define SEP_BIO_LOCKOUT_FLAG_FAILED_ATTEMPTS 4

#define SEP_BIO_LOCKOUT_STATE _IOR(SEP_BIO_IOC_MAGIC, 0x0e, struct sep_bio_lockout)
/*
Expand Down
Loading