diff --git a/drivers/soc/apple/Makefile b/drivers/soc/apple/Makefile index 10d52840245eeb..d106c305637158 100644 --- a/drivers/soc/apple/Makefile +++ b/drivers/soc/apple/Makefile @@ -41,7 +41,7 @@ obj-$(CONFIG_APPLE_SEP_FV_INTERFACE) += fv_shim.o obj-$(CONFIG_APPLE_SEP) += apple-sep.o apple-sep-y := sep.o hwrng_shim.o store_shim.o bio_shim.o sha_shim.o \ - crypto_shim.o sensor_shim.o p256_shim.o trusted_shim.o work_shim.o \ + crypto_shim.o sensor_shim.o p256_shim.o trusted_shim.o work_shim.o key_shim.o \ pm_shim.o diag_shim.o obj-$(CONFIG_APPLE_PMP) += apple-pmp.o diff --git a/drivers/soc/apple/bio.rs b/drivers/soc/apple/bio.rs index 6b4e1edbe29664..0489a785eaadaf 100644 --- a/drivers/soc/apple/bio.rs +++ b/drivers/soc/apple/bio.rs @@ -460,6 +460,10 @@ impl Session { Ok(()) } + pub(crate) fn available(&self) -> bool { + self.worker_epoch.is_none() && self.op.may_start() + } + pub(crate) fn ready(&self) -> bool { self.unseen } @@ -955,6 +959,11 @@ pub(crate) fn abort_capture(session: &mut Session) -> bool { true } +/// Firmware calls that must not interleave with another file's capture. +pub(crate) fn uses_enclave(cmd: u32) -> bool { + starts_capture(cmd) || matches!(cmd, IOC_GET_INFO | IOC_DELETE | IOC_DELETE_ALL | IOC_ATTEST) +} + /// The ioctls that start a sensor capture. pub(crate) fn starts_capture(cmd: u32) -> bool { cmd == IOC_ENROL_START || cmd == IOC_VERIFY_START diff --git a/drivers/soc/apple/capture.rs b/drivers/soc/apple/capture.rs new file mode 100644 index 00000000000000..8c5a24cc53f983 --- /dev/null +++ b/drivers/soc/apple/capture.rs @@ -0,0 +1,100 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +//! Shared capture ownership; an idle biometric file does not own the sensor. + +use crate::{key_request::Request, SepData}; +use kernel::prelude::*; +use kernel::sync::{atomic::Relaxed, Arc}; + +enum Owner { + Idle, + Sync(u64), + Bio(u64), + Key(Arc), +} + +pub(crate) struct State { + generation: u64, + owner: Owner, +} + +impl State { + pub(crate) fn new() -> Self { + Self { generation: 0, owner: Owner::Idle } + } + + fn acquire(&mut self) -> Result { + if !matches!(self.owner, Owner::Idle) { return Err(EBUSY); } + self.generation = self.generation.checked_add(1).ok_or(EOVERFLOW)?; + self.owner = Owner::Sync(self.generation); + Ok(self.generation) + } + + pub(crate) fn start_key(&mut self, request: Arc) -> Result { + if !matches!(self.owner, Owner::Idle) { return Err(EBUSY); } + self.owner = Owner::Key(request); + Ok(()) + } + + pub(crate) fn key(&self) -> Option> { + match &self.owner { + Owner::Key(request) => Some(request.clone()), + _ => None, + } + } + + pub(crate) fn finish_key(&mut self, request: &Arc) { + if matches!(&self.owner, Owner::Key(active) if Arc::ptr_eq(active, request)) { + self.owner = Owner::Idle; + } + } + + pub(crate) fn finish_bio(&mut self, epoch: u64) { + if matches!(self.owner, Owner::Bio(active) if active == epoch) { + self.owner = Owner::Idle; + } + } +} + +/// A synchronous firmware operation joins the suspend drain. Ownership can be +/// transferred to a queued bio worker, which releases it only after cleanup. +pub(crate) struct Lease<'a> { + sep: &'a SepData, + generation: u64, + running: bool, +} + +impl<'a> Lease<'a> { + pub(crate) fn acquire(sep: &'a SepData) -> Result { + let generation = { + let mut state = sep.capture.lock(); + if sep.shutting_down.load(Relaxed) { return Err(ENODEV); } + if sep.suspending.load(Relaxed) { return Err(EBUSY); } + state.acquire()? + }; + let mut lease = Self { sep, generation, running: false }; + if !sep.capture_begin() { return Err(EBUSY); } + lease.running = true; + Ok(lease) + } + + pub(crate) fn into_bio(mut self, epoch: u64) { + let mut state = self.sep.capture.lock(); + if matches!(state.owner, Owner::Sync(active) if active == self.generation) { + state.owner = Owner::Bio(epoch); + } + // The queued worker will join the drain when it starts; a suspend + // before then aborts its session, and capture_begin refuses hardware. + self.sep.capture_end(); + self.running = false; + } +} + +impl Drop for Lease<'_> { + fn drop(&mut self) { + if self.running { self.sep.capture_end(); } + let mut state = self.sep.capture.lock(); + if matches!(state.owner, Owner::Sync(active) if active == self.generation) { + state.owner = Owner::Idle; + } + } +} diff --git a/drivers/soc/apple/der.rs b/drivers/soc/apple/der.rs index fcd198561872f4..808a2e2cc46c41 100644 --- a/drivers/soc/apple/der.rs +++ b/drivers/soc/apple/der.rs @@ -48,13 +48,13 @@ fn integer_bytes(v: u32) -> Result> { pub(crate) enum RefKeyValue<'a> { /// op mnemonic: `oc`/`ow`/`ouw`. Utf8(&'a [u8]), + Boolean(bool), Integer(u32), Octets(&'a [u8]), Der(&'a [u8]), } -/// Canonical DER `SET OF SEQUENCE`, members sorted by key — the shape the -/// enclave parser requires. +/// Apple key-store `SET OF SEQUENCE`, sorted by complete encoded member. pub(crate) fn encode_refkey_set(items: &[(&[u8], RefKeyValue<'_>)]) -> Result> { let mut members: KVec<(&[u8], KVec)> = KVec::new(); for (key, value) in items { @@ -65,6 +65,10 @@ pub(crate) fn encode_refkey_set(items: &[(&[u8], RefKeyValue<'_>)]) -> Result { + // Apple's access-control serializer uses 0x01 for true. + inner.extend_from_slice(&[0x01, 0x01, u8::from(*v)], GFP_KERNEL)?; + } RefKeyValue::Utf8(s) => { if s.len() > MAX_ITEM { return Err(EINVAL); @@ -91,7 +95,7 @@ pub(crate) fn encode_refkey_set(items: &[(&[u8], RefKeyValue<'_>)]) -> Result() == 16); +static_assert!(core::mem::size_of::() == 88); +static_assert!(core::mem::size_of::() == 136); +static_assert!(core::mem::size_of::() == 56); +static_assert!(core::mem::size_of::() == 8); +// SAFETY: all repr(C) layouts contain initialized integers/bytes without padding. +unsafe impl kernel::transmute::AsBytes for Info {} +// SAFETY: initialized integers/bytes, no padding. +unsafe impl kernel::transmute::AsBytes for Create {} +// SAFETY: every bit pattern is valid. +unsafe impl kernel::transmute::FromBytes for Create {} +// SAFETY: initialized integers/bytes, no padding. +unsafe impl kernel::transmute::AsBytes for Sign {} +// SAFETY: every bit pattern is valid. +unsafe impl kernel::transmute::FromBytes for Sign {} +// SAFETY: initialized integers, no padding. +unsafe impl kernel::transmute::AsBytes for Decrypt {} +// SAFETY: every bit pattern is valid. +unsafe impl kernel::transmute::FromBytes for Decrypt {} +// SAFETY: every bit pattern is valid. +unsafe impl kernel::transmute::FromBytes for Cancel {} + +const IOC_INFO: u32 = kernel::ioctl::_IOR::(0xb2, 0); +const IOC_CREATE: u32 = kernel::ioctl::_IOWR::(0xb2, 1); +const IOC_SIGN: u32 = kernel::ioctl::_IOWR::(0xb2, 2); +const IOC_DECRYPT: u32 = kernel::ioctl::_IOWR::(0xb2, 3); +const IOC_CANCEL: u32 = kernel::ioctl::_IOW::(0xb2, 4); + +enum Operation { + Idle, + Create, + Private(Arc), +} + +struct State { + last_id: u64, + cancelled_through: u64, + operation: Operation, +} + +#[pin_data] +struct File { + sep: Arc, + #[pin] + state: Mutex, +} + +/// The ioctl owns the file's active slot until it has finished copying out. +struct Active<'a>(&'a File); + +impl Drop for Active<'_> { + fn drop(&mut self) { self.0.state.lock().operation = Operation::Idle; } +} + +enum Destination { + Sign(Sign), + Decrypt(Decrypt), +} + +impl File { + fn begin(&self, request: Option>) -> Result> { + let mut state = self.state.lock(); + if !matches!(state.operation, Operation::Idle) { return Err(EBUSY); } + state.operation = if let Some(request) = request { + if request.id == 0 { return Err(EINVAL); } + if request.id <= state.cancelled_through { return Err(ECANCELED); } + if request.id <= state.last_id { return Err(EINVAL); } + state.last_id = request.id; + Operation::Private(request) + } else { Operation::Create }; + Ok(Active(self)) + } + + fn cancel(&self, id: u64) -> Result { + if id == 0 { return Err(EINVAL); } + let request = { + let mut state = self.state.lock(); + match &state.operation { + Operation::Private(request) if request.id == id => request.clone(), + _ if id > state.last_id => { + // Cover cancellation while EXECUTE is still copying input, + // before an active request can be observed by this thread. + state.cancelled_through = state.cancelled_through.max(id); + return Ok(()); + } + _ => return Err(ENOENT), + } + }; + request.cancel(ECANCELED) + } + + fn check_api(&self) -> Result { + if !self.sep.biometric_keys_supported() { return Err(EOPNOTSUPP); } + if !self.sep.xart_writable() { return Err(EROFS); } + Ok(()) + } + + fn ioctl(&self, cmd: u32, arg: usize) -> Result { + if self.sep.shutting_down.load(Relaxed) { return Err(ENODEV); } + let user = UserPtr::from_addr(arg); + if cmd == IOC_INFO { + let out = Info { version: 1, + capabilities: if self.sep.biometric_keys_supported() { 3 } else { 0 }, + reserved: [0; 2] }; + return UserSlice::new(user, core::mem::size_of_val(&out)).writer().write(&out); + } + if cmd == IOC_CANCEL { + let request: Cancel = UserSlice::new(user, core::mem::size_of::()).reader().read()?; + return self.cancel(request.request_id); + } + if !matches!(cmd, IOC_CREATE | IOC_SIGN | IOC_DECRYPT) { return Err(ENOTTY); } + self.check_api()?; + if cmd == IOC_CREATE { + let request: Create = UserSlice::new(user, core::mem::size_of::()).reader().read()?; + if request.flags > 1 || request.reserved != [0; 7] || request.blob_len as usize != keys::MAX_BLOB { + return Err(EINVAL); + } + let _active = self.begin(None)?; + let operation = Request::new(0, keys::KeyRequest { blob: KVec::new(), + input: keys::Input::Create(request.flags), + deadline_ns: crate::shim::monotonic_ns().saturating_add(120_000_000_000) })?; + SepData::queue_key(self.sep.clone(), operation.clone())?; + let keys::Output::Created(blob, public_key) = operation.wait()? else { return Err(EIO); }; + UserSlice::new(UserPtr::from_addr(request.blob_ptr as usize), keys::MAX_BLOB).writer().write_slice(&blob)?; + let out = Create { flags: 0, blob_len: blob.len() as u32, blob_ptr: request.blob_ptr, + public_key, reserved: [0; 7] }; + return UserSlice::new(user, core::mem::size_of_val(&out)).writer().write(&out); + } + + let (id, blob_len, blob_ptr, timeout_ms, input, destination) = match cmd { + IOC_SIGN => { + let request: Sign = UserSlice::new(user, core::mem::size_of::()).reader().read()?; + if request.flags != 0 || request.sig_len != 0 || request.signature != [0; 72] { return Err(EINVAL); } + (request.request_id, request.blob_len, request.blob_ptr, request.timeout_ms, + keys::Input::Sign(request.digest), Destination::Sign(request)) + } + IOC_DECRYPT => { + let request: Decrypt = UserSlice::new(user, core::mem::size_of::()).reader().read()?; + if request.flags != 0 || request.reserved != 0 || request.plain_len as usize > keys::MAX_BLOB + || !(81..=keys::MAX_BLOB).contains(&(request.sealed_len as usize)) + { + return Err(EINVAL); + } + let mut sealed = KVec::new(); + sealed.resize(request.sealed_len as usize, 0, GFP_KERNEL)?; + UserSlice::new(UserPtr::from_addr(request.sealed_ptr as usize), sealed.len()).reader().read_slice(&mut sealed)?; + if sealed[0] != 4 { return Err(EINVAL); } + (request.request_id, request.blob_len, request.blob_ptr, request.timeout_ms, + keys::Input::Decrypt(sealed), Destination::Decrypt(request)) + } + _ => return Err(ENOTTY), + }; + if blob_len == 0 || blob_len as usize > keys::MAX_BLOB || !(1..=120_000).contains(&timeout_ms) { + return Err(EINVAL); + } + let mut blob = KVec::new(); + blob.resize(blob_len as usize, 0, GFP_KERNEL)?; + UserSlice::new(UserPtr::from_addr(blob_ptr as usize), blob.len()).reader().read_slice(&mut blob)?; + if SepData::refkey_pub(&blob).is_none() { return Err(EINVAL); } + let deadline_ns = crate::shim::monotonic_ns().saturating_add(u64::from(timeout_ms) * 1_000_000); + let request = Request::new(id, keys::KeyRequest { blob, input, deadline_ns })?; + let _active = self.begin(Some(request.clone()))?; + SepData::queue_key(self.sep.clone(), request.clone())?; + let result = request.wait()?; + match (result, destination) { + (keys::Output::Signature(signature), Destination::Sign(mut out)) => { + out.sig_len = signature.len; + out.signature = signature.bytes; + UserSlice::new(user, core::mem::size_of_val(&out)).writer().write(&out) + } + (keys::Output::Plaintext(plain), Destination::Decrypt(mut out)) => { + let capacity = out.plain_len as usize; + out.plain_len = plain.len() as u32; + if capacity < plain.len() { + UserSlice::new(user, core::mem::size_of_val(&out)).writer().write(&out)?; + return Err(ENOSPC); + } + UserSlice::new(UserPtr::from_addr(out.plain_ptr as usize), capacity).writer().write_slice(&plain)?; + UserSlice::new(user, core::mem::size_of_val(&out)).writer().write(&out) + } + _ => Err(EIO), + } + } +} + +extern "C" { + fn sep_key_register(ctx: *mut c_void, + open: unsafe extern "C" fn(*mut c_void, *mut *mut c_void) -> c_int, + release: unsafe extern "C" fn(*mut c_void), + ioctl: unsafe extern "C" fn(*mut c_void, c_uint, c_ulong) -> c_long) -> *mut c_void; + fn sep_key_unregister(device: *mut c_void); +} + +pub(crate) struct Registration(*mut c_void); +// SAFETY: registration has no thread affinity; the C shim serializes open/detach. +unsafe impl Send for Registration {} + +impl Registration { + /// # Safety + /// The driver's original Arc must keep sep alive until this is dropped. + pub(crate) unsafe fn new(sep: &SepData) -> Result { + // SAFETY: the caller supplies the registered-window guarantee above. + let device = unsafe { sep_key_register(core::ptr::from_ref(sep).cast_mut().cast(), open, release, ioctl) }; + if device.is_null() { return Err(ENODEV); } + Ok(Self(device)) + } +} + +impl Drop for Registration { + fn drop(&mut self) { + // SAFETY: one live registration, never used after this call. + unsafe { sep_key_unregister(self.0) }; + } +} + +// SAFETY: the C shim holds its registration lock throughout open. ctx is the +// original SepData Arc's pointer, and out points to its per-file storage. +unsafe extern "C" fn open(ctx: *mut c_void, out: *mut *mut c_void) -> c_int { + let sep = unsafe { ArcBorrow::::from_raw(ctx.cast()) }; + if sep.shutting_down.load(Relaxed) { return ENODEV.to_errno(); } + let file = Arc::pin_init(try_pin_init!(File { + sep: sep.into(), + state <- kernel::new_mutex!(State { last_id: 0, cancelled_through: 0, operation: Operation::Idle }), + }), GFP_KERNEL); + match file { + Ok(file) => { + // SAFETY: out is writable; ownership moves to the matching release. + unsafe { out.write(file.into_foreign()) }; + 0 + } + Err(error) => error.to_errno(), + } +} + +// SAFETY: C passes the owned file pointer once, after all file callbacks stop. +unsafe extern "C" fn release(ctx: *mut c_void) { + drop(unsafe { as ForeignOwnable>::from_foreign(ctx) }); +} + +// SAFETY: every successful open stores an Arc, retained until release; +// the file reference held by ioctl keeps it live even during concurrent close. +unsafe extern "C" fn ioctl(ctx: *mut c_void, cmd: c_uint, arg: c_ulong) -> c_long { + let file = unsafe { as ForeignOwnable>::borrow(ctx) }; + match file.ioctl(cmd, arg) { + Ok(()) => 0, + Err(error) => error.to_errno() as c_long, + } +} + +impl SepData { + pub(crate) fn register_keys(&self) -> Result { + let mut device = self.key_dev.lock(); + if device.is_none() { + // SAFETY: the driver's Arc outlives this registration; remove drops + // it before tearing down the driver. Opens acquire their own Arc. + *device = Some(unsafe { Registration::new(self)? }); + } + Ok(()) + } + + pub(crate) fn queue_key(this: Arc, request: Arc) -> Result { + // remove sets shutting_down, then takes this same lock to revoke the + // active request before cancel_work_sync. Keep enqueue in that window. + let mut capture = this.capture.lock(); + if this.shutting_down.load(Relaxed) { return Err(ENODEV); } + if this.suspending.load(Relaxed) { return Err(EBUSY); } + capture.start_key(request.clone())?; + if kernel::workqueue::system_dfl() + .enqueue::, { crate::KEY_WORK_ID }>(this.clone()).is_err() + { + request.complete(Err(EIO)); + capture.finish_key(&request); + return Err(EIO); + } + Ok(()) + } + + pub(crate) fn cancel_key(&self, reason: Error) { + let request = self.capture.lock().key(); + if let Some(request) = request { let _ = request.cancel(reason); } + } + + pub(crate) fn capture_wanted(&self) -> bool { + if self.shutting_down.load(Relaxed) || self.suspending.load(Relaxed) { return false; } + let request = self.capture.lock().key(); + if let Some(request) = request { return request.is_live(); } + crate::bio::capture_is_live(&self.bio_session.lock(), self.capture_epoch.load(Relaxed)) + } +} diff --git a/drivers/soc/apple/key_request.rs b/drivers/soc/apple/key_request.rs new file mode 100644 index 00000000000000..8919499eda154b --- /dev/null +++ b/drivers/soc/apple/key_request.rs @@ -0,0 +1,110 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +//! One owned application-key request, shared by its file, waiter and worker. + +use crate::keys::{KeyRequest, Output}; +use kernel::prelude::*; +use kernel::sync::{new_condvar, Arc, CondVar, CondVarTimeoutResult, Mutex}; + +enum Phase { + Pending, + Complete(Result), + Abandoned(Error), + Claimed, +} + +#[pin_data] +pub(crate) struct Request { + pub(crate) id: u64, + pub(crate) data: KeyRequest, + #[pin] + phase: Mutex, + #[pin] + changed: CondVar, +} + +impl Request { + pub(crate) fn new(id: u64, data: KeyRequest) -> Result> { + Arc::pin_init(try_pin_init!(Self { + id, + data, + phase <- kernel::new_mutex!(Phase::Pending), + changed <- new_condvar!("sep_key_request"), + }), GFP_KERNEL) + } + + pub(crate) fn is_live(&self) -> bool { + matches!(*self.phase.lock(), Phase::Pending) + && crate::shim::monotonic_ns() < self.data.deadline_ns + } + + pub(crate) fn complete(&self, result: Result) { + let mut phase = self.phase.lock(); + if matches!(*phase, Phase::Pending) { + // Worker errors must never restart an accepted private-key syscall. + // EBUSY is reserved for contention before hardware submission. + let result = result.map_err(|error| match error { + ERESTARTSYS | ERESTARTNOINTR | ERESTARTNOHAND | ERESTART_RESTARTBLOCK => EINTR, + EBUSY => EIO, + _ => error, + }); + *phase = Phase::Complete(if crate::shim::monotonic_ns() >= self.data.deadline_ns { + Err(ETIMEDOUT) + } else { result }); + } + drop(phase); + self.changed.notify_all(); + } + + /// Cancellation revokes delivery, not firmware work already submitted. + pub(crate) fn cancel(&self, reason: Error) -> Result { + let mut phase = self.phase.lock(); + match &*phase { + Phase::Claimed => return Err(EALREADY), + Phase::Complete(Err(_)) | Phase::Abandoned(_) => return Err(ENOENT), + _ => *phase = Phase::Abandoned(reason), + } + drop(phase); + self.changed.notify_all(); + Ok(()) + } + + pub(crate) fn wait(&self) -> Result { + let mut phase = self.phase.lock(); + loop { + let now = crate::shim::monotonic_ns(); + if now >= self.data.deadline_ns + && matches!(*phase, Phase::Pending | Phase::Complete(Ok(_))) + { + *phase = Phase::Abandoned(ETIMEDOUT); + } + match &*phase { + Phase::Abandoned(error) => return Err(*error), + Phase::Claimed => return Err(ENODATA), + Phase::Complete(Err(error)) => return Err(*error), + Phase::Complete(_) => { + let Phase::Complete(result) = core::mem::replace(&mut *phase, Phase::Claimed) else { + return Err(EIO); + }; + return result; + } + Phase::Pending => {}, + } + let timeout = kernel::time::msecs_to_jiffies( + self.data.deadline_ns.saturating_sub(now).div_ceil(1_000_000) as u32, + ); + if matches!(self.changed.wait_interruptible_timeout(&mut phase, timeout), + CondVarTimeoutResult::Signal { .. }) + { + // Never ERESTARTSYS after submission: a retry could perform a + // second private-key operation. Also discard a raced success. + if matches!(*phase, Phase::Pending | Phase::Complete(Ok(_))) { + *phase = Phase::Abandoned(EINTR); + } + return match &*phase { + Phase::Complete(Err(error)) | Phase::Abandoned(error) => Err(*error), + _ => Err(EINTR), + }; + } + } + } +} diff --git a/drivers/soc/apple/key_shim.c b/drivers/soc/apple/key_shim.c new file mode 100644 index 00000000000000..23b51dae1fbd03 --- /dev/null +++ b/drivers/soc/apple/key_shim.c @@ -0,0 +1,114 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +/* Application-key files own their Rust context, independently of registration. */ +#include +#include +#include +#include + +#include "shim.h" + +struct sep_key_device { + struct miscdevice misc; + struct mutex lock; + void *ctx; + int (*open)(void *ctx, void **file); + void (*release)(void *file); + long (*ioctl)(void *file, unsigned int cmd, unsigned long arg); +}; + +struct sep_key_file { + void *ctx; + void (*release)(void *file); + long (*ioctl)(void *file, unsigned int cmd, unsigned long arg); +}; + +static int sep_key_open(struct inode *inode, struct file *file) +{ + struct sep_key_device *d = container_of(file->private_data, + struct sep_key_device, misc); + struct sep_key_file *f; + int ret; + + f = kzalloc(sizeof(*f), GFP_KERNEL); + if (!f) + return -ENOMEM; + /* + * misc_open serializes registration lifetime. Rust clones the device Arc + * here; subsequent ioctls and release never borrow the registration. + */ + mutex_lock(&d->lock); + ret = d->ctx ? d->open(d->ctx, &f->ctx) : -ENODEV; + f->release = d->release; + f->ioctl = d->ioctl; + mutex_unlock(&d->lock); + if (ret) { + kfree(f); + return ret; + } + file->private_data = f; + return 0; +} + +static int sep_key_release(struct inode *inode, struct file *file) +{ + struct sep_key_file *f = file->private_data; + + f->release(f->ctx); + kfree(f); + return 0; +} + +static long sep_key_ioctl(struct file *file, unsigned int cmd, unsigned long arg) +{ + struct sep_key_file *f = file->private_data; + + /* Rust serializes request transitions, never a complete blocking ioctl. */ + return f->ioctl(f->ctx, cmd, arg); +} + +static const struct file_operations sep_key_fops = { + .owner = THIS_MODULE, + .open = sep_key_open, + .release = sep_key_release, + .unlocked_ioctl = sep_key_ioctl, + .compat_ioctl = compat_ptr_ioctl, +}; + +void *sep_key_register(void *ctx, int (*open)(void *, void **), + void (*release)(void *), + long (*ioctl)(void *, unsigned int, unsigned long)) +{ + struct sep_key_device *d; + + d = kzalloc(sizeof(*d), GFP_KERNEL); + if (!d) + return NULL; + mutex_init(&d->lock); + d->ctx = ctx; + d->open = open; + d->release = release; + d->ioctl = ioctl; + d->misc.minor = MISC_DYNAMIC_MINOR; + d->misc.name = "sep-keys"; + d->misc.mode = 0600; + d->misc.fops = &sep_key_fops; + if (misc_register(&d->misc)) { + mutex_destroy(&d->lock); + kfree(d); + return NULL; + } + return d; +} + +void sep_key_unregister(void *device) +{ + struct sep_key_device *d = device; + + misc_deregister(&d->misc); + mutex_lock(&d->lock); + d->ctx = NULL; + mutex_unlock(&d->lock); + /* Each successful open owns its context and copies of the callbacks. */ + mutex_destroy(&d->lock); + kfree(d); +} diff --git a/drivers/soc/apple/keys.rs b/drivers/soc/apple/keys.rs new file mode 100644 index 00000000000000..f96f62a80f52c8 --- /dev/null +++ b/drivers/soc/apple/keys.rs @@ -0,0 +1,227 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +//! Application-owned biometric P-256 keys. Authentication contexts stay in kernel. + +use crate::{bio, der, keybag, profile, proto, sbio, scrd, SepData}; +use crate::der::RefKeyValue as V; +use kernel::prelude::*; +use kernel::sync::atomic::Relaxed; + +pub(crate) const MAX_BLOB: usize = 4096; + +/// A request owns a fresh credential. Discard the grant even on cancellation or +/// transport failure; a failed deletion disables application keys until reboot. +pub(crate) struct Credential<'a> { + sep: &'a SepData, + handle: [u8; scrd::SCRD_ACM_HANDLE_LEN], +} + +impl<'a> Credential<'a> { + pub(crate) fn new(sep: &'a SepData, handle: [u8; scrd::SCRD_ACM_HANDLE_LEN]) -> Self { + Self { sep, handle } + } + + pub(crate) fn handle(&self) -> &[u8; scrd::SCRD_ACM_HANDLE_LEN] { + &self.handle + } + + pub(crate) fn into_handle(self) -> [u8; scrd::SCRD_ACM_HANDLE_LEN] { + let handle = self.handle; + core::mem::forget(self); + handle + } +} + +impl Drop for Credential<'_> { + fn drop(&mut self) { + if !matches!(self.sep.scrd_command(&scrd::scrd_context_delete(&self.handle)), Some((0, _))) { + self.sep.key_api_failed.store(true, Relaxed); + dev_err!(self.sep.dev, "keys: credential disposal failed; application key operations disabled until reboot\n"); + } + self.handle.fill(0); + } +} + +impl SepData { + pub(crate) fn biometric_keys_supported(&self) -> bool { + // The credential-match layout and policy are validated on this protocol + // generation only. Other profiles retain their existing capabilities. + matches!(self.profile.key_store, profile::KeyStore::Sepos13 { .. }) + && !self.key_api_failed.load(Relaxed) + } + + pub(crate) fn key_credential(&self) -> Result> { + if !self.biometric_keys_supported() { + return Err(Error::from_errno(-(kernel::bindings::EOPNOTSUPP as i32))); + } + let keybag::State::Present(stored) = keybag::read(keybag::Slot::Identity)? else { + return Err(ENOENT); + }; + let (handle, _) = self.sks_recover(&stored).ok_or(EIO)?; + let result = (|| { + if !self.sks_prepare_identity_refkey(handle, stored.secret()) { + return Err(EIO); + } + let user = sbio::UserId::new(crate::SBIO_PROBE_USER_ID).ok_or(EINVAL)?; + let context = self.establish_passcode_validated_context(user).ok_or_else(|| { + self.key_api_failed.store(true, Relaxed); + EIO + })?; + Ok(Credential::new(self, context)) + })(); + let unloaded = self.sks_send(self.sks_req_unload_keybag(handle)); + if !matches!(unloaded, Some(ref out) if out.reply.status == 0) { + self.key_api_failed.store(true, Relaxed); + return Err(EIO); + } + result + } + + fn application_refkey_op(&self, items: &[(&[u8], V<'_>)]) -> Result> { + let encoded = der::encode_refkey_set(items)?; + let out = self.sks_send(self.sks_req_refkey(0, &encoded)).ok_or(EIO)?; + if out.reply.status == -8 { + return Err(EACCES); + } + if out.reply.status != 0 { + return Err(EIO); + } + let body = self.sks_report_response(crate::sks::SKS_PERFORM_OP_NAME, &out).ok_or(EIO)?; + let mut fields = proto::FieldCursor::new(body); + match fields.i32().ok_or(EIO)? { + 0 => {}, + -8 => return Err(EACCES), + _ => return Err(EIO), + } + let blob = fields.blob().ok_or(EIO)?; + if blob.len() > MAX_BLOB { + return Err(EIO); + } + let mut owned = KVec::new(); + owned.extend_from_slice(blob, GFP_KERNEL)?; + Ok(owned) + } + + pub(crate) fn create_biometric_key(&self, policy_kind: u32) -> Result<(KVec, [u8; bio::ATTEST_PUB_LEN])> { + let credential = self.key_credential()?; + let result = (|| { + let user = sbio::UserId::new(crate::SBIO_PROBE_USER_ID).ok_or(EINVAL)?; + let crate::SbioOutcome::Ok(binding) = self.sbio_call(&sbio::sbio_catacomb_id(user)) else { + return Err(EIO); + }; + let binding: &[u8; 16] = binding.as_slice().try_into().map_err(|_| EIO)?; + let policy = if policy_kind == 1 { + crate::refkey_policy::biometric_combined_policy(binding)? + } else { + crate::refkey_policy::biometric_signing_policy(binding)? + }; + let items = [ + (b"o".as_slice(), V::Utf8(b"oc")), + (b"bc".as_slice(), V::Integer(9)), + (b"kt".as_slice(), V::Integer(4)), + (b"ed".as_slice(), V::Der(&policy)), + (b"acmh".as_slice(), V::Octets(credential.handle())), + ]; + let blob = self.application_refkey_op(&items)?; + let mut public = [0; bio::ATTEST_PUB_LEN]; + public.copy_from_slice(Self::refkey_pub(&blob).ok_or(EIO)?); + Ok((blob, public)) + })(); + drop(credential); + if self.key_api_failed.load(Relaxed) { return Err(EIO); } + result + } + + pub(crate) fn sign_biometric_key( + &self, + blob: &[u8], + digest: &[u8; 32], + credential: &Credential<'_>, + ) -> Result { + let encoded = self.application_refkey_op(&[ + (b"o", V::Utf8(b"osgn")), + (b"d", V::Octets(digest)), + (b"rk", V::Der(blob)), + (b"acmh", V::Octets(credential.handle())), + ])?; + let bytes = der::octet_string_body(&encoded).unwrap_or(&encoded); + if bytes.len() > bio::ATTEST_SIG_MAX || bytes.first() != Some(&0x30) { + return Err(EIO); + } + let mut signature = Signature { len: bytes.len() as u32, bytes: [0; bio::ATTEST_SIG_MAX] }; + signature.bytes[..bytes.len()].copy_from_slice(bytes); + Ok(signature) + } + + fn decrypt_biometric_key( + &self, + blob: &[u8], + sealed: &[u8], + credential: &Credential<'_>, + ) -> Result { + let mut items = KVec::new(); + items.push((b"o".as_slice(), V::Utf8(b"oecd")), GFP_KERNEL)?; + items.push((b"d".as_slice(), V::Octets(sealed)), GFP_KERNEL)?; + items.push((b"rk".as_slice(), V::Der(blob)), GFP_KERNEL)?; + items.push((b"acmh".as_slice(), V::Octets(credential.handle())), GFP_KERNEL)?; + self.application_refkey_op(&items).map(crate::Secret) + } +} + +pub(crate) struct Signature { + pub(crate) len: u32, + pub(crate) bytes: [u8; bio::ATTEST_SIG_MAX], +} + +pub(crate) struct KeyRequest { + pub(crate) blob: KVec, + pub(crate) input: Input, + pub(crate) deadline_ns: u64, +} + +pub(crate) enum Input { + Create(u32), + Sign([u8; 32]), + Decrypt(KVec), +} + +pub(crate) enum Output { + Created(KVec, [u8; bio::ATTEST_PUB_LEN]), + Signature(Signature), + Plaintext(crate::Secret), +} + +impl SepData { + pub(crate) fn run_key_operation(&self, request: &crate::key_request::Request) { + let result = (|| { + if !request.is_live() { return Err(ECANCELED); } + if let Input::Create(policy) = &request.data.input { + return self.create_biometric_key(*policy).map(|(blob, public)| Output::Created(blob, public)); + } + let prepared = self.prepare_verify_capture().map_err(|_| EIO)?; + if !request.is_live() { return Err(ECANCELED); } + let credential = self.key_credential()?; + if !request.is_live() { return Err(ECANCELED); } + let result = match prepared.verify(Some(credential.handle())) { + bio::VerifyOutcome::Matched(_) => { + if !request.is_live() { Err(ECANCELED) } + else { + match &request.data.input { + Input::Create(_) => Err(EINVAL), + Input::Sign(digest) => self.sign_biometric_key(&request.data.blob, digest, &credential) + .map(Output::Signature), + Input::Decrypt(sealed) => self.decrypt_biometric_key(&request.data.blob, sealed, &credential) + .map(Output::Plaintext), + } + } + } + bio::VerifyOutcome::NoMatch => Err(EACCES), + bio::VerifyOutcome::Failed(crate::ENROL_STATUS_TIMEOUT) => Err(ETIMEDOUT), + bio::VerifyOutcome::Failed(_) => Err(EIO), + }; + drop(credential); + if self.key_api_failed.load(Relaxed) { return Err(EIO); } + result + })(); + request.complete(result); + } +} diff --git a/drivers/soc/apple/refkey.rs b/drivers/soc/apple/refkey.rs index e380dfd9ecfb05..40badaa7f08140 100644 --- a/drivers/soc/apple/refkey.rs +++ b/drivers/soc/apple/refkey.rs @@ -14,7 +14,7 @@ use kernel::sync::atomic::Relaxed; pub(crate) const REFKEY_ENVELOPE_VERSION: u32 = 2; impl SepData { - fn sks_req_refkey(&self, keybag: i32, der_set: &[u8]) -> Result { + pub(crate) fn sks_req_refkey(&self, keybag: i32, der_set: &[u8]) -> Result { let (keybag, envelope) = match self.profile.key_store { // The 13.5 operation envelope is 3. Use the designated identity // session rather than the source handle; class F with -1 expired @@ -43,7 +43,7 @@ impl SepData { }) } - fn sks_prepare_identity_refkey( + pub(crate) fn sks_prepare_identity_refkey( &self, handle: crate::sks::KeyBagHandle, secret: &[u8], @@ -157,7 +157,7 @@ impl SepData { Some(refkey_blob) } - fn refkey_pub(blob: &[u8]) -> Option<&[u8]> { + pub(crate) fn refkey_pub(blob: &[u8]) -> Option<&[u8]> { let rk_tlv = crate::der::refkey_find(blob, b"rk")?; let pub_raw = crate::der::refkey_find(rk_tlv, b"pub").and_then(crate::der::octet_string_body)?; diff --git a/drivers/soc/apple/refkey_policy.rs b/drivers/soc/apple/refkey_policy.rs new file mode 100644 index 00000000000000..c6194115c60e1d --- /dev/null +++ b/drivers/soc/apple/refkey_policy.rs @@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +//! Access control for application-owned biometric signing keys. + +use crate::der::{encode_refkey_set, RefKeyValue as V}; +use kernel::prelude::*; + +/// Bind private-key use to this biometric credential set. Enrollment changes +/// within the set are permitted; this is not a current-enrollment-set policy. +pub(crate) fn biometric_signing_policy(binding: &[u8; 16]) -> Result> { + let biometric = biometric_constraint(binding)?; + let acl = encode_refkey_set(&[ + (b"oa", V::Boolean(true)), + (b"ock", V::Der(&biometric)), + (b"okd", V::Der(&biometric)), + (b"odel", V::Boolean(true)), + (b"osgn", V::Der(&biometric)), + ])?; + encode_refkey_set(&[(b"acl", V::Der(&acl))]) +} + +pub(crate) fn biometric_combined_policy(binding: &[u8; 16]) -> Result> { + let biometric = biometric_constraint(binding)?; + let acl = encode_refkey_set(&[ + (b"oa", V::Boolean(true)), + (b"ock", V::Der(&biometric)), + (b"okd", V::Der(&biometric)), + (b"osgn", V::Der(&biometric)), + (b"od", V::Der(&biometric)), + (b"oecd", V::Der(&biometric)), + (b"oe", V::Boolean(true)), + (b"odel", V::Boolean(true)), + ])?; + encode_refkey_set(&[(b"acl", V::Der(&acl))]) +} + +fn biometric_constraint(binding: &[u8; 16]) -> Result> { + if binding.iter().all(|v| *v == 0) { return Err(EINVAL); } + let credential = encode_refkey_set(&[(b"pbioc", V::Octets(binding))])?; + encode_refkey_set(&[(b"cbio", V::Der(&credential))]) +} + +#[kernel::macros::kunit_tests(apple_sep_refkey_policy)] +mod tests { + use super::*; + + #[test] + fn member_order_uses_encoded_length_before_key() -> Result { + let encoded = encode_refkey_set(&[ + (b"a", V::Octets(&[0; 16])), + (b"z", V::Boolean(true)), + ])?; + assert_eq!(&encoded[..10], &[0x31, 31, 0x30, 6, 0x0c, 1, b'z', 1, 1, 1]); + Ok(()) + } + + #[test] + fn policy_matches_native_encoding() -> Result { + let binding = core::array::from_fn(|i| i as u8); + let expected: &[u8] = &[ + 0x31, 0x81, 0xaa, 0x30, 0x81, 0xa7, 0x0c, 0x03, 0x61, 0x63, 0x6c, 0x31, + 0x81, 0x9f, 0x30, 0x07, 0x0c, 0x02, 0x6f, 0x61, 0x01, 0x01, 0x01, 0x30, + 0x09, 0x0c, 0x04, 0x6f, 0x64, 0x65, 0x6c, 0x01, 0x01, 0x01, 0x30, 0x2c, + 0x0c, 0x03, 0x6f, 0x63, 0x6b, 0x31, 0x25, 0x30, 0x23, 0x0c, 0x04, 0x63, + 0x62, 0x69, 0x6f, 0x31, 0x1b, 0x30, 0x19, 0x0c, 0x05, 0x70, 0x62, 0x69, + 0x6f, 0x63, 0x04, 0x10, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x30, 0x2c, 0x0c, 0x03, + 0x6f, 0x6b, 0x64, 0x31, 0x25, 0x30, 0x23, 0x0c, 0x04, 0x63, 0x62, 0x69, + 0x6f, 0x31, 0x1b, 0x30, 0x19, 0x0c, 0x05, 0x70, 0x62, 0x69, 0x6f, 0x63, + 0x04, 0x10, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, + 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x30, 0x2d, 0x0c, 0x04, 0x6f, 0x73, + 0x67, 0x6e, 0x31, 0x25, 0x30, 0x23, 0x0c, 0x04, 0x63, 0x62, 0x69, 0x6f, + 0x31, 0x1b, 0x30, 0x19, 0x0c, 0x05, 0x70, 0x62, 0x69, 0x6f, 0x63, 0x04, + 0x10, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, + 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + ]; + assert_eq!(biometric_signing_policy(&binding)?.as_slice(), expected); + Ok(()) + } + + #[test] + fn policy_requires_a_credential_set() { + assert!(biometric_signing_policy(&[0; 16]).is_err()); + } +} diff --git a/drivers/soc/apple/sbio.rs b/drivers/soc/apple/sbio.rs index 9c69ca184c6912..5189e9a5d85d4a 100644 --- a/drivers/soc/apple/sbio.rs +++ b/drivers/soc/apple/sbio.rs @@ -10,6 +10,36 @@ use crate::sks::SKS_AUTH_TOKEN_LEN; use kernel::prelude::*; use kernel::soc::apple::mailbox::Message; +/// Owns the online sensor until capture finishes or its caller abandons it. +pub(crate) struct PreparedCapture<'a> { + sep: &'a SepData, +} + +impl PreparedCapture<'_> { + pub(crate) fn verify(self, context: Option<&[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]>) -> bio::VerifyOutcome { + let sep = self.sep; + let outcome = sep.verify_one_image(context); + drop(self); + if matches!(&outcome, bio::VerifyOutcome::Matched(_) | bio::VerifyOutcome::NoMatch) { + let Some(user) = crate::sbio::UserId::new(SBIO_PROBE_USER_ID) else { + return bio::VerifyOutcome::Failed(ENROL_STATUS_ENCLAVE); + }; + if !sep.save_after_match(user) { + return bio::VerifyOutcome::Failed(ENROL_STATUS_ENCLAVE); + } + } + outcome + } +} + +impl Drop for PreparedCapture<'_> { + fn drop(&mut self) { + let _ = sensor::idle(); + sensor::power(false); + self.sep.note_capture_end(); + } +} + impl SepData { fn sbio_enrol_step(&self, op: &crate::sbio::SbioOp) -> Option> { match self.sbio_call(op) { @@ -41,13 +71,17 @@ impl SepData { // answer has to be recorded here or nowhere. Success is decided as in // `sbio_call`. fn sbio_verify_step(&self, op: &crate::sbio::SbioOp) -> Option> { - let done = match self.sbio_transfer(op) { + self.sbio_verify_payload(op.opcode(), op.name(), op.payload()) + } + + fn sbio_verify_payload(&self, opcode: u16, name: &'static CStr, payload: &[u8]) -> Option> { + let done = match self.sbio_transfer_raw(opcode, name, payload) { Ok(done) => done, Err(e) => { dev_err!( self.dev, "verify: {} transfer failed ({:?})\n", - op.name(), + name, e ); return None; @@ -59,7 +93,7 @@ impl SepData { dev_err!( self.dev, "verify: {} answered status {}\n", - op.name(), + name, done.status ); None @@ -288,7 +322,7 @@ impl SepData { self.bio_wake(); } - fn bio_wake(&self) { + pub(crate) fn bio_wake(&self) { if let Some(dev) = self.bio_dev.lock().as_ref() { dev.wake(); } @@ -1211,15 +1245,14 @@ impl SepData { } // Enrol, verify and deletion drive the sensor and the enclave under // the same lock, and `capture_begin` lets a suspend wait for us. - let _writer = this.catacomb_writer.lock(); - if !this.capture_begin() { + let Ok(_lease) = crate::capture::Lease::acquire(this) else { Self::defer_late_attach(this); return; - } + }; + let _writer = this.catacomb_writer.lock(); if this.attach_complete(this.device_view_synced.load(Relaxed)) { // An enrol or a verify got there first. diag::sensor_attach(registration::AttachState::Attached); - this.capture_end(); return; } dev_info!( @@ -1233,7 +1266,6 @@ impl SepData { || !this.shutting_down.load(Relaxed) && !this.suspending.load(Relaxed), ); if let registration::Outcome::Abandoned(_) = outcome { - this.capture_end(); Self::defer_late_attach(this); return; } @@ -1250,7 +1282,6 @@ impl SepData { "sensor: authenticated attach incomplete after the late retry; capture unavailable until an enrol or verify brings the sensor up\n" ); } - this.capture_end(); } /// The late attach could not run yet; try again later, a few times. @@ -1383,6 +1414,9 @@ impl SepData { e ); } + if let Err(e) = self.register_keys() { + dev_err!(self.dev, "bringup: could not publish /dev/sep-keys: {:?}\n", e); + } } fn activate_touchid(&self) -> Result<()> { @@ -1508,7 +1542,7 @@ impl SepData { } } - fn prepare_bio_open(&self) -> Result<()> { + pub(crate) fn prepare_bio_open(&self) -> Result<()> { if let Err(e) = self.activate_touchid() { self.touchid_failed.store(true, Relaxed); diag::touchid(false); @@ -1559,7 +1593,7 @@ impl SepData { /// not ended by a suspend and not closed. fn verify_wanted(&self) -> bool { !self.shutting_down.load(Relaxed) - && bio::verify_is_live(&self.bio_session.lock(), self.capture_epoch.load(Relaxed)) + && self.capture_wanted() } // Every verify registers the sensor again. On a profile with a nonce @@ -1578,19 +1612,8 @@ impl SepData { } pub(crate) fn run_verify(&self) { - self.retry_restore_proof(); - if !self.templates_restored.load(Relaxed) { - dev_err!( - self.dev, - "verify: refusing before touching the sensor; cold-match prep, restore, or device-view identity proof did not complete. This is not a non-matching finger\n" - ); - self.finish_verify( - bio::VerifyOutcome::Failed(ENROL_STATUS_ENCLAVE), - [0u8; bio::TOKEN_LEN], - ); - return; - } - + let epoch = self.capture_epoch.load(Relaxed); + if !bio::verify_is_live(&self.bio_session.lock(), epoch) { return; } let Some(token_bytes) = self.mint_token_bytes() else { dev_err!(self.dev, "verify: could not draw a result token\n"); self.finish_verify( @@ -1599,51 +1622,37 @@ impl SepData { ); return; }; + let outcome = match self.prepare_verify_capture() { + Ok(prepared) => prepared.verify(None), + Err(status) => bio::VerifyOutcome::Failed(status), + }; + self.finish_verify(outcome, token_bytes); + } + pub(crate) fn prepare_verify_capture(&self) -> core::result::Result, u32> { + self.retry_restore_proof(); + if !self.templates_restored.load(Relaxed) { + dev_err!( + self.dev, + "verify: refusing before touching the sensor; cold-match prep, restore, or device-view identity proof did not complete. This is not a non-matching finger\n" + ); + return Err(ENROL_STATUS_ENCLAVE); + } self.settle_before_capture(); // The enclave refuses a capture from an uncalibrated sensor (0x65 answers 1). if !self.bring_sensor_online_for_verify() { dev_err!(self.dev, "verify: the sensor did not come back online\n"); - self.finish_verify(bio::VerifyOutcome::Failed(ENROL_STATUS_SENSOR), token_bytes); let _ = sensor::idle(); - return; + return Err(ENROL_STATUS_SENSOR); } - - // The match uses the plain 0x23->0x06->0x65 pipeline, with no SCRD step. - - let outcome = self.verify_one_image(); - let _ = sensor::idle(); - sensor::power(false); - self.note_capture_end(); - - let definitive = matches!( - &outcome, - bio::VerifyOutcome::Matched(_) | bio::VerifyOutcome::NoMatch - ); - if definitive { - let Some(user) = crate::sbio::UserId::new(SBIO_PROBE_USER_ID) else { - self.finish_verify( - bio::VerifyOutcome::Failed(ENROL_STATUS_ENCLAVE), - token_bytes, - ); - return; - }; - if !self.save_after_match(user) { - self.finish_verify( - bio::VerifyOutcome::Failed(ENROL_STATUS_ENCLAVE), - token_bytes, - ); - return; - } - } - self.finish_verify(outcome, token_bytes); + Ok(PreparedCapture { sep: self }) } // Every exit that fails a live verify logs why: userspace sees only a bare // "unspecified error", which cannot tell a sensor fault from a refused // match step. - fn verify_one_image(&self) -> bio::VerifyOutcome { + fn verify_one_image(&self, context: Option<&[u8; crate::scrd::SCRD_ACM_HANDLE_LEN]>) -> bio::VerifyOutcome { let Some(user) = crate::sbio::UserId::new(SBIO_PROBE_USER_ID) else { dev_err!(self.dev, "verify: invalid probe user id\n"); return bio::VerifyOutcome::Failed(ENROL_STATUS_ENCLAVE); @@ -1730,9 +1739,15 @@ impl SepData { user, crate::shim::monotonic_ns(), ); - if self.sbio_verify_step(&init).is_none() { - return bio::VerifyOutcome::Failed(ENROL_STATUS_ENCLAVE); - } + let accepted = if let Some(context) = context { + match crate::sbio::sbio_credential_image_init(&init, context) { + Ok(payload) => self.sbio_verify_payload(init.opcode(), init.name(), &payload).is_some(), + Err(_) => false, + } + } else { + self.sbio_verify_step(&init).is_some() + }; + if !accepted { return bio::VerifyOutcome::Failed(ENROL_STATUS_ENCLAVE); } let relay = crate::sbio::SbioRelay::new(&capture); if self.sbio_relay(&relay).is_none() { @@ -2870,7 +2885,7 @@ impl SepData { ); let mut attempt: u32 = 0; loop { - if !bio::capture_is_live(&self.bio_session.lock(), self.capture_epoch.load(Relaxed)) { + if !self.capture_wanted() { return CaptureWait::Abandon; } @@ -3217,7 +3232,10 @@ impl SepData { self.wait_for_worker_exit(&mut session, true)?; bio::open(&mut session)?; drop(session); - if let Err(e) = self.prepare_bio_open() { + let prepared = if self.touchid_started.load(Relaxed) { Ok(()) } else { + crate::capture::Lease::acquire(self).and_then(|_lease| self.prepare_bio_open()) + }; + if let Err(e) = prepared { bio::release(&mut self.bio_session.lock()); return Err(e); } @@ -3257,6 +3275,7 @@ impl SepData { /// The enrol or verify work item `epoch` has stopped. pub(crate) fn capture_worker_stopped(&self, epoch: u64) { bio::worker_stopped(&mut self.bio_session.lock(), epoch); + self.capture.lock().finish_bio(epoch); self.worker_gone.notify_all(); } @@ -3302,6 +3321,7 @@ impl SepData { self.suspending.store(true, Relaxed); bio::abort_capture(&mut session) }; + self.cancel_key(ECANCELED); if woke { dev_info!( self.dev, @@ -3407,7 +3427,13 @@ impl SepData { if self.shutting_down.load(Relaxed) { return Err(ENODEV); } + if bio::starts_capture(cmd) { + self.wait_until_awake()?; + self.wait_for_worker_exit(&mut self.bio_session.lock(), false)?; + } + let lease = if bio::uses_enclave(cmd) { Some(crate::capture::Lease::acquire(self)?) } else { None }; if cmd == bio::IOC_ATTEST { + if !self.bio_session.lock().available() { return Err(EBUSY); } return self.bio_attest(arg); } // With xART writes off the enclave cannot persist what an enrolment or @@ -3423,11 +3449,6 @@ impl SepData { } return Err(EROFS); } - // A capture started while the system goes to sleep would run into the - // suspend; `capture_begin` repeats this check under the session lock. - if bio::starts_capture(cmd) { - self.wait_until_awake()?; - } // Query SEP outside the bio session/index locks. A host index can // outlive its SEP identity after a failed cold restore; userspace // must not mistake that index for proof of a live template. @@ -3471,20 +3492,16 @@ impl SepData { // (they wait on mailbox replies). The system must not sleep // partway through the saves, and a verify that finishes after a // cancel saves too. - let deleted = loop { - self.wait_until_awake()?; - let _writer = self.catacomb_writer.lock(); - // A suspend began while this waited for the lock: wait it out. - if !self.capture_begin() { - continue; - } - let deleted = self.delete_and_persist(&handled); - self.capture_end(); - break deleted; - }; + // The synchronous capture lease already joined the suspend drain. + let _writer = self.catacomb_writer.lock(); + let deleted = self.delete_and_persist(&handled); deleted?; } + if handled.start_enrol || handled.start_verify { + let epoch = bio::queued_epoch(&self.bio_session.lock()).ok_or(EIO)?; + lease.ok_or(EIO)?.into_bio(epoch); + } if handled.wake { self.bio_wake(); } @@ -4081,6 +4098,9 @@ pub(crate) struct SbioOp { const SBIO_MAX_PAYLOAD: usize = 0x97; pub(crate) const SBIO_IMAGE_INIT_LEN: usize = 0x97; +pub(crate) const SBIO_CREDENTIAL_IMAGE_INIT_LEN: usize = + SBIO_IMAGE_INIT_LEN + crate::scrd::SCRD_ACM_HANDLE_LEN; + static_assert!(SBIO_IMAGE_INIT_LEN <= SBIO_MAX_PAYLOAD); const fn pad_payload(src: &[u8]) -> [u8; SBIO_MAX_PAYLOAD] { @@ -4696,6 +4716,15 @@ pub(crate) fn sbio_free_identity_count(user: UserId) -> SbioOp { } } +pub(crate) fn sbio_catacomb_id(user: UserId) -> SbioOp { + SbioOp { + opcode: 0x33, + payload: pad_payload(&user.value().to_le_bytes()), + payload_len: 4, + name: c"CATACOMB_ID", + } +} + const OP_SBIO_DELETE_IDENTITY: u16 = 0x57; pub(crate) fn sbio_delete_identity(identity: &IdentityV1) -> SbioOp { SbioOp { @@ -4909,6 +4938,41 @@ pub(crate) fn sbio_prepare_image_processing() -> SbioOp { } const OP_SBIO_IMAGE_INIT: u16 = 0x06; +/// Bind a match to a credential set, rather than returning identification only. +pub(crate) fn sbio_credential_image_init( + base: &SbioOp, + context: &[u8; crate::scrd::SCRD_ACM_HANDLE_LEN], +) -> Result<[u8; SBIO_CREDENTIAL_IMAGE_INIT_LEN]> { + if base.opcode() != OP_SBIO_IMAGE_INIT || base.payload().len() != SBIO_IMAGE_INIT_LEN { + return Err(EINVAL); + } + let mut payload = [0u8; SBIO_CREDENTIAL_IMAGE_INIT_LEN]; + payload[..SBIO_IMAGE_INIT_LEN].copy_from_slice(base.payload()); + payload[..4].copy_from_slice(&8u32.to_le_bytes()); + payload[0x93..SBIO_IMAGE_INIT_LEN] + .copy_from_slice(&(context.len() as u32).to_le_bytes()); + payload[SBIO_IMAGE_INIT_LEN..].copy_from_slice(context); + Ok(payload) +} + +#[kernel::macros::kunit_tests(apple_sep_credential_capture)] +mod credential_capture_tests { + use super::*; + + #[test] + fn credential_match_preserves_capture_fields() -> Result { + let user = UserId::new(501).ok_or(EINVAL)?; + let base = sbio_image_processing_init(ImagePurpose::Matching, true, true, 0, user, 1000); + let context = [0x5a; crate::scrd::SCRD_ACM_HANDLE_LEN]; + let payload = sbio_credential_image_init(&base, &context)?; + assert_eq!(&payload[..4], &8u32.to_le_bytes()); + assert_eq!(&payload[4..0x93], &base.payload()[4..0x93]); + assert_eq!(&payload[0x93..0x97], &16u32.to_le_bytes()); + assert_eq!(&payload[0x97..], &context); + assert!(sbio_credential_image_init(&sbio_match_result(), &context).is_err()); + Ok(()) + } +} pub(crate) fn sbio_image_processing_init( purpose: ImagePurpose, first_image: bool, diff --git a/drivers/soc/apple/scrd.rs b/drivers/soc/apple/scrd.rs index acdcdab12db658..d33b3344dc6002 100644 --- a/drivers/soc/apple/scrd.rs +++ b/drivers/soc/apple/scrd.rs @@ -17,6 +17,7 @@ const SCRD_MAGIC: [u8; 4] = *b"DRCS"; const SCRD_CMD_INITIALIZE: u8 = 0x0a; const SCRD_CMD_CONTEXT_CREATE_TRACKED: u8 = 0x24; const SCRD_CMD_CONTEXT_EXTERNALIZE: u8 = 0x13; +const SCRD_CMD_CONTEXT_DELETE: u8 = 0x02; const SCRD_CMD_VERIFY_POLICY: u8 = 0x03; const SCRD_INIT_LOG_LEVEL: u8 = 0x28; @@ -90,6 +91,18 @@ pub(crate) fn scrd_context_externalize(handle: &[u8; SCRD_ACM_HANDLE_LEN]) -> Sc } } +pub(crate) fn scrd_context_delete(handle: &[u8; SCRD_ACM_HANDLE_LEN]) -> ScrdCommand { + let mut payload = [0u8; SCRD_MAX_LOGICAL]; + scrd_header(&mut payload, SCRD_CMD_CONTEXT_DELETE, 0, 1); + payload[8..8 + SCRD_ACM_HANDLE_LEN].copy_from_slice(handle); + ScrdCommand { + request: SCRD_REQUEST_COMMAND, + payload, + payload_len: 8 + SCRD_ACM_HANDLE_LEN, + name: c"SCRD_CONTEXT_DELETE", + } +} + pub(crate) fn scrd_verify_touchid_enrollment(handle: &[u8; SCRD_ACM_HANDLE_LEN]) -> ScrdCommand { let mut payload = [0u8; SCRD_MAX_LOGICAL]; scrd_header(&mut payload, SCRD_CMD_VERIFY_POLICY, 0, 1); diff --git a/drivers/soc/apple/sep.rs b/drivers/soc/apple/sep.rs index 87b90a7d986ee6..983cfc647b5155 100644 --- a/drivers/soc/apple/sep.rs +++ b/drivers/soc/apple/sep.rs @@ -11,6 +11,7 @@ compile_error!("apple_sep requires CONFIG_OF_DYNAMIC: the SEP and DART nodes shi mod bio; mod catacomb; +mod capture; mod control; mod der; mod diag; @@ -18,12 +19,16 @@ mod dt; mod fv; mod hwrng; mod image; +mod keys; +mod key_device; +mod key_request; mod keybag; mod keybag_identity; mod pm; mod profile; mod proto; mod refkey; +mod refkey_policy; mod refkey_seal; mod registration; mod rxring; @@ -102,6 +107,10 @@ const LATE_ATTACH_WORK_ID: u64 = 4; static_assert!(LATE_ATTACH_WORK_ID != VERIFY_WORK_ID && LATE_ATTACH_WORK_ID != ENROL_WORK_ID); static_assert!(LATE_ATTACH_WORK_ID != SETTLE_WORK_ID && LATE_ATTACH_WORK_ID != 0); +const KEY_WORK_ID: u64 = 5; +static_assert!(KEY_WORK_ID != 0 && KEY_WORK_ID != SETTLE_WORK_ID && KEY_WORK_ID != ENROL_WORK_ID); +static_assert!(KEY_WORK_ID != VERIFY_WORK_ID && KEY_WORK_ID != LATE_ATTACH_WORK_ID); + // When an attach-time sensor bring-up falls short, one more round runs this // long after Touch ID was activated. A round that cannot run yet, because the // boot-time bring-up is still going or the system is going to sleep, is put @@ -204,6 +213,7 @@ const RNG_SLEEP_DRAIN_MS: u32 = proto::CONTROL_TIMEOUT_MS + 1000; extern "C" { fn sep_cancel_work_sync(work: *mut c_void); + fn sep_flush_work(work: *mut c_void); fn sep_cancel_delayed_work_sync(work: *mut c_void); } @@ -709,6 +719,12 @@ struct SepData { #[pin] bio_session: Mutex, + key_api_failed: Atomic, + + #[pin] + capture: Mutex, + #[pin] + key_dev: Mutex>, #[pin] bio_index: Mutex, @@ -791,6 +807,9 @@ struct SepData { #[pin] verify_work: Work, + #[pin] + key_work: Work, + #[pin] late_attach_work: DelayedWork, } @@ -799,6 +818,7 @@ impl_has_work! { impl HasWork for SepData { self.rx_work } impl HasWork for SepData { self.enrol_work } impl HasWork for SepData { self.verify_work } + impl HasWork for SepData { self.key_work } } impl_has_delayed_work! { @@ -953,6 +973,9 @@ impl SepData { sbio_wq <- new_condvar!("SepData::sbio_wq"), sbio_ready: Atomic::new(false), bio_session <- new_mutex!(bio::Session::new()), + key_api_failed: Atomic::new(false), + capture <- new_mutex!(capture::State::new()), + key_dev <- new_mutex!(None), bio_index <- new_mutex!(bio_index), bio_dev <- new_mutex!(None), store <- new_mutex!(store), @@ -981,6 +1004,7 @@ impl SepData { rx_work <- new_work!("SepData::rx_work"), enrol_work <- new_work!("SepData::enrol_work"), verify_work <- new_work!("SepData::verify_work"), + key_work <- new_work!("SepData::key_work"), settle_work <- new_delayed_work!("SepData::settle_work"), late_attach_work <- new_delayed_work!("SepData::late_attach_work"), }), @@ -1954,6 +1978,7 @@ impl SepData { } let mut acm_handle = [0u8; crate::scrd::SCRD_ACM_HANDLE_LEN]; acm_handle.copy_from_slice(&out[..crate::scrd::SCRD_ACM_HANDLE_LEN]); + let credential = keys::Credential::new(self, acm_handle); let Some((status, _)) = self.scrd_command(&crate::scrd::scrd_context_externalize(&acm_handle)) else { dev_err!(self.dev, "enrol: SCRD context externalize did not answer\n"); @@ -1984,7 +2009,7 @@ impl SepData { return None; } - Some(acm_handle) + Some(credential.into_handle()) } fn establish_scrd_match_context(&self, user: crate::sbio::UserId) -> bool { @@ -2353,6 +2378,7 @@ impl SepData { fn remove(&self) { self.shutting_down.store(true, Relaxed); + self.cancel_key(ENODEV); { let _guard = self.endpoints.lock(); self.trusted_ready.notify_all(); @@ -2371,6 +2397,8 @@ impl SepData { trusted::unregister(); self.unregister_fv_kernel(); + let keys = self.key_dev.lock().take(); + drop(keys); let dev = self.bio_dev.lock().take(); drop(dev); @@ -2385,12 +2413,15 @@ impl SepData { *self.mbox.lock() = None; - // SAFETY: all five pointers refer to pinned work fields in `self`. + // SAFETY: all six pointers refer to pinned work fields in `self`. // Shutdown blocks requeueing, and the mailbox can no longer add work. unsafe { sep_cancel_work_sync(Work::raw_get(core::ptr::addr_of!(self.rx_work)).cast()); sep_cancel_work_sync(Work::raw_get(core::ptr::addr_of!(self.enrol_work)).cast()); sep_cancel_work_sync(Work::raw_get(core::ptr::addr_of!(self.verify_work)).cast()); + // A queued Rust work item owns an Arc reclaimed by its callback. + // Let the revoked key worker run its no-hardware exit and drain it. + sep_flush_work(Work::raw_get(core::ptr::addr_of!(self.key_work)).cast()); sep_cancel_delayed_work_sync( DelayedWork::raw_as_work(core::ptr::addr_of!(self.settle_work)).cast(), ); @@ -2603,6 +2634,28 @@ impl WorkItem for SepData { } +impl WorkItem for SepData { + type Pointer = Arc; + + fn run(this: Arc) { + let Some(request) = this.capture.lock().key() else { return; }; + let _writer = this.catacomb_writer.lock(); + if !this.shutting_down.load(Relaxed) && this.capture_begin() { + if request.is_live() { + match this.prepare_bio_open() { + Ok(()) => this.run_key_operation(&request), + Err(error) => request.complete(Err(error)), + } + } + this.capture_end(); + } + // A canceled caller may already have returned. Keep ownership until + // hardware cleanup finishes; an old worker never releases a new lease. + request.complete(Err(if this.shutting_down.load(Relaxed) { ENODEV } else { ECANCELED })); + this.capture.lock().finish_key(&request); + } +} + struct SepDriver(Arc); // Deliberately not exported as a module alias: udev must not load the driver diff --git a/drivers/soc/apple/shim.h b/drivers/soc/apple/shim.h index 0425bf2cdaa6ed..1f9bd92569e808 100644 --- a/drivers/soc/apple/shim.h +++ b/drivers/soc/apple/shim.h @@ -18,9 +18,15 @@ #include +void *sep_key_register(void *ctx, int (*open)(void *, void **), + void (*release)(void *), + long (*ioctl)(void *, unsigned int, unsigned long)); +void sep_key_unregister(void *device); + struct apple_sep_fv_new_file_key; void sep_cancel_work_sync(void *work); +void sep_flush_work(void *work); void sep_cancel_delayed_work_sync(void *work); /* -- pm_shim.c ---------------------------------------------------------- */ diff --git a/drivers/soc/apple/work_shim.c b/drivers/soc/apple/work_shim.c index ad8e147cd6f63f..7167bb917ff27a 100644 --- a/drivers/soc/apple/work_shim.c +++ b/drivers/soc/apple/work_shim.c @@ -8,6 +8,11 @@ void sep_cancel_work_sync(void *work) cancel_work_sync(work); } +void sep_flush_work(void *work) +{ + flush_work(work); +} + void sep_cancel_delayed_work_sync(void *work) { cancel_delayed_work_sync(work); diff --git a/include/uapi/linux/apple-sep.h b/include/uapi/linux/apple-sep.h index 394b497ff7a59c..64f63659ea67c3 100644 --- a/include/uapi/linux/apple-sep.h +++ b/include/uapi/linux/apple-sep.h @@ -3,9 +3,9 @@ /* * Userspace interface for the SEP biometric device. * - * The enclave does the matching. Nothing biometric crosses this interface: only - * an operation, a stage, a status, an opaque identity UUID, and an opaque host - * label userspace chooses. + * The enclave does the matching. Fingerprint images and templates never cross + * this interface. Application keys return signatures or decrypted application + * data; their private keys remain inside the enclave. */ #ifndef _UAPI_LINUX_APPLE_SEP_H @@ -130,4 +130,92 @@ struct sep_bio_attest { #define SEP_BIO_DELETE_ALL _IO(SEP_BIO_IOC_MAGIC, 0x09) #define SEP_BIO_ATTEST _IOWR(SEP_BIO_IOC_MAGIC, 0x0a, struct sep_bio_attest) +/* + * EXPERIMENTAL blocking application-key ABI on /dev/sep-keys. + * Opening does not acquire the sensor; concurrent capture returns EBUSY before + * hardware submission. No fingerprint enrollment/deletion ioctls on this node. + * Each open file accepts one operation at a time. Creation uses the same worker + * and teardown drain, with a 120-second deadline and no private-request ID. + * Current creation policies accept any enrolled finger, not a fixed set. + */ +#define SEP_KEY_IOC_MAGIC 0xb2 +#define SEP_KEY_IFACE_VERSION 1 +#define SEP_KEY_CAP_SIGN 1u +#define SEP_KEY_CAP_DECRYPT 2u +#define SEP_KEY_BLOB_MAX 4096 +#define SEP_KEY_PUBLIC_LEN 65 +#define SEP_KEY_SIGNATURE_MAX 72 +/* Combined signing and biometric ECIES use, not a decryption-only policy. */ +#define SEP_KEY_CREATE_DECRYPT 1u + +struct sep_key_info { + __u32 version; + __u32 capabilities; /* zero when the key API has failed closed */ + __u32 reserved[2]; +}; + +struct sep_key_create { + __u32 flags; /* zero or SEP_KEY_CREATE_DECRYPT */ + __u32 blob_len; /* in: BLOB_MAX capacity; out: actual length */ + __aligned_u64 blob_ptr; + __u8 public_key[SEP_KEY_PUBLIC_LEN]; + __u8 reserved[7]; /* zero */ +}; + +/* + * Request IDs are nonzero and strictly increasing per open file, including + * failed submission attempts. Each accepted request requires a fresh match. + * CANCEL(id) may precede acceptance while EXECUTE is copying its inputs: it + * retires all as-yet-unaccepted IDs up to id, so they cannot start afterward. + * It never cancels a different active request. A stale cancel returns ENOENT; + * cancellation after a result claim returns EALREADY. Neither grants permission + * to retry. + * + * EINTR after submission is final, never ERESTARTSYS. A caller/runtime MUST NOT + * automatically retry any interrupted/uncertain private-key request. CANCEL + * revokes delivery; it cannot undo a submitted firmware operation. Closing an + * fd in another thread does not interrupt its in-flight ioctl. The sensor + * remains busy until the previous worker completes its hardware cleanup. + * EBUSY denotes refusal before submission; worker busy errors return EIO. + * + * Results are consumed before any user-memory copy. EFAULT may follow a partial + * copy. ENOSPC reports required capacity but consumes the result too. Neither + * permits redelivery or automatic resubmission. Copying is outside request + * locks; cancellation cannot recall an already-claimed copy. The broker must + * discard a successful response if its application request has been revoked. + */ +struct sep_key_sign { + __aligned_u64 request_id; + __u32 flags; /* zero */ + __u32 timeout_ms; /* 1..120000, CLOCK_MONOTONIC */ + __u32 blob_len; /* 1..BLOB_MAX */ + __u32 sig_len; /* in: zero; out: DER signature length */ + __aligned_u64 blob_ptr; + __u8 digest[32]; + __u8 signature[SEP_KEY_SIGNATURE_MAX]; /* in: zero */ +}; + +struct sep_key_decrypt { + __aligned_u64 request_id; + __u32 flags; /* zero */ + __u32 timeout_ms; /* 1..120000, CLOCK_MONOTONIC */ + __u32 blob_len; /* 1..BLOB_MAX */ + __u32 sealed_len; /* 81..BLOB_MAX: P-256 point, ciphertext, tag */ + __aligned_u64 blob_ptr; + __aligned_u64 sealed_ptr; + __u32 plain_len; /* in: 0..BLOB_MAX capacity; out: actual length */ + __u32 reserved; /* zero */ + __aligned_u64 plain_ptr; +}; + +struct sep_key_cancel { + __aligned_u64 request_id; +}; + +#define SEP_KEY_INFO _IOR(SEP_KEY_IOC_MAGIC, 0, struct sep_key_info) +#define SEP_KEY_CREATE _IOWR(SEP_KEY_IOC_MAGIC, 1, struct sep_key_create) +#define SEP_KEY_SIGN _IOWR(SEP_KEY_IOC_MAGIC, 2, struct sep_key_sign) +#define SEP_KEY_DECRYPT _IOWR(SEP_KEY_IOC_MAGIC, 3, struct sep_key_decrypt) +#define SEP_KEY_CANCEL _IOW(SEP_KEY_IOC_MAGIC, 4, struct sep_key_cancel) + #endif /* _UAPI_LINUX_APPLE_SEP_H */ diff --git a/tools/aurora-ci/host-tests.json b/tools/aurora-ci/host-tests.json index fb9cdbdd66fc69..403e391e560963 100644 --- a/tools/aurora-ci/host-tests.json +++ b/tools/aurora-ci/host-tests.json @@ -1132,6 +1132,53 @@ "-v" ] }, + { + "name": "tools/aurora-sep/tests/test_application_key_cleanup.py", + "group": "aurora-sep", + "argv": [ + "python3", + "-m", + "unittest", + "discover", + "-s", + "tools/aurora-sep/tests", + "-p", + "test_application_key_cleanup.py", + "-v" + ], + "rust": "1.85.0" + }, + { + "name": "tools/aurora-sep/tests/test_application_keys.py", + "group": "aurora-sep", + "argv": [ + "python3", + "-m", + "unittest", + "discover", + "-s", + "tools/aurora-sep/tests", + "-p", + "test_application_keys.py", + "-v" + ] + }, + { + "name": "tools/aurora-sep/tests/test_blocking_requests.py", + "group": "aurora-sep", + "argv": [ + "python3", + "-m", + "unittest", + "discover", + "-s", + "tools/aurora-sep/tests", + "-p", + "test_blocking_requests.py", + "-v" + ], + "rust": "1.85.0" + }, { "name": "tools/aurora-sep/tests/test_desktop_fixes.py", "group": "aurora-sep", diff --git a/tools/aurora-sep/README.md b/tools/aurora-sep/README.md index aa8d5ce4e7a6b9..2e6822a8621bf6 100644 --- a/tools/aurora-sep/README.md +++ b/tools/aurora-sep/README.md @@ -317,6 +317,11 @@ systemctl enable aurora-sep-greeter-fingerprint.service Disabling it and rebooting returns the login screen to GNOME's behaviour. +## Biometric application keys + +The separate `/dev/sep-keys` API supports biometric P-256 signing and ECIES item +decryption. See [the reference client and broker contract](keys/README.md). + ## Biometric userspace header The canonical ioctl ABI is `include/uapi/linux/apple-sep.h`. Export this diff --git a/tools/aurora-sep/keys/README.md b/tools/aurora-sep/keys/README.md new file mode 100644 index 00000000000000..ebefd9d4dbc1b7 --- /dev/null +++ b/tools/aurora-sep/keys/README.md @@ -0,0 +1,129 @@ +# Biometric application keys + +`/dev/sep-keys` creates device-bound P-256 keys, signs SHA-256 digests and +performs biometric ECIES decryption. Private-key operations require a fresh +fingerprint match into a kernel-owned credential; the credential is disposed +before success is delivered. The canonical ABI is ``, exported +by `make headers_install`. Its comments define layouts, limits and ownership. + +This is a separate device from `/dev/sep-bio`. Query its interface version and +capabilities before use. Unsupported profiles or a failed credential-cleanup +latch report unavailable capabilities; operations return `EOPNOTSUPP`. This does +not add enrollment or deletion commands to the key device. + +There is **no password/passphrase fallback**. The driver's automatically supplied +identity-bag secret is machine-held setup material, not fresh user authentication. +Current keys accept any enrolled finger; adding a finger can give it access to +existing keys. Current-enrollment-set binding and private-key import are not +implemented. Adding a future creation policy will not upgrade existing blobs. + +## Reference client + +`sep_key.py` is an importable Python 3.11+ client requiring `cryptography`. +It needs an existing biometric installation with enrolled fingers and enabled +persistence. It does not provision keybags, enroll fingers or change services. +Run these examples from this directory: + +```sh +sudo python3 sep_key.py create signing-key.json +printf 'disposable message' > message +sudo python3 sep_key.py sign signing-key.json message signature.der +sudo python3 sep_key.py create --decrypt item-key.json +sudo python3 sep_key.py selftest-decrypt item-key.json +``` + +Creation requires no fingerprint scan. `sign` hashes once with SHA-256, then +independently verifies the returned signature before writing it. `sign-digest` +accepts exactly 32 digest bytes. The decryption self-test uses disposable data. + +Persist the opaque blob and public point atomically in authenticated application +storage. The example JSON is a container, not an authorization record. Blobs do +not restore keys on another device or after erasing secure state. Key population +limits have not been measured. + +Python callers use `from sep_key import SepClient`, then `sign_digest(blob, digest)` +or `decrypt_into(blob, sealed, output)`. Decryption fills a caller-owned 4096-byte +bytearray and returns its used length. The client wipes it on failure; the caller +must wipe successful output after use. Python/backend temporary copies cannot all +be erased; production memory handling belongs in the application's bounded, +zeroizing secret-memory implementation. + +## Requests and cancellation + +Opening a key file does not reserve the sensor. Idle BIO and key files can +coexist; conflicting hardware operations return `EBUSY` before submission. Each +key file accepts one operation at a time, with no kernel request queue. A worker +retains the hardware lease until cleanup, even if its waiting caller has returned. +BIO queries that need firmware may also receive `EBUSY` during a capture. + +For an interruptible UI, keep the device context open and: + +1. Allocate `request_id = client.new_request_id()` before starting a worker. +2. Pass `request_id=` to `sign_digest` or `decrypt_into` on that worker. +3. Revoke application delivery immediately on UI cancellation and call + `client.cancel(request_id)` from another thread. +4. Join the caller before closing the device context. Discard any response for + a revoked application request, even if completion raced cancellation. + +IDs are nonzero and strictly increasing per open file. Preacceptance cancellation +retires unaccepted IDs through the canceled ID, covering input-copy races. It +never cancels a different active request. `EALREADY` means a result was claimed; +`ENOENT` means no cancellable accepted request with that ID remains. Neither is +authorization to use a revoked result or repeat a key operation. + +CANCEL revokes delivery; it cannot undo firmware work already submitted. Closing +an fd in another thread does not cancel its active syscall. Signals return final +`EINTR` after submission. The client calls libc directly because some runtimes +retry interrupted ioctls automatically; private-key requests must never be +implicitly resubmitted. + +## Results and errors + +The blocking call returns one signature or plaintext result. It claims ownership +before any user copy, outside file/request state locks. An `EFAULT`, including a +response-descriptor fault after a successful data copy, consumes the result. +`ENOSPC` reports required capacity but also consumes it. Wipe any partial output; +neither error permits redelivery or automatic resubmission. + +Cancellation/deadlines discard undelivered results. An in-flight copy cannot be +recalled. Request deadlines do not bound a stalled user copy or erase application +memory. Completed failures retain their diagnosis. + +`EACCES` reports mismatch/authorization refusal, `ECANCELED` cancellation, +`ETIMEDOUT` expiry and `EROFS` disabled persistence. `EIO` includes uncertain +transport, protocol, decrypt or cleanup failures. Worker busy failures also use +`EIO`, preserving `EBUSY` as pre-submission contention. Credential-disposal failure +disables the application-key API until reboot. No distinct biometric-lockout or +password-required reason is currently exposed; do not infer unlimited attempts. + +Never automatically retry an uncertain private-key operation. Bounded waiting +before a new submission after a known `EBUSY` refusal is different from replaying +an operation that may have reached firmware. Keep explicit user-request and +revocation state in the broker. + +## Application security boundary + +A trusted broker enforces caller/session identity, item ownership, allowed use +and WebAuthn relying-party/origin rules. Device access is privileged and does not +isolate Linux users sharing one SEP identity. A touch does not bind a trusted +display to the user's intended transaction. Comprehensive protection against a +compromised host/root/kernel has not been established. + +Native signing returns a signature while the private scalar stays in SEP. +Decryption returns application plaintext to the host. A synced/software passkey +stored as an encrypted item therefore signs on the host after unsealing. Native +SEP passkeys need their public key registered with the relying party; WebAuthn +signs SHA256(authenticatorData || clientDataHash). Two different signing keys do +not provide alternative unlock routes for the same credential. + +The combined creation policy deliberately enables signing and decryption. Its +signing-only counterpart has not received a complete forbidden-operation matrix. +The broker must enforce its narrower usage rules. Ordinary BIO tokens, machine +attestation and trusted-key sealing do not grant biometric application-key use. + +`sep_envelope.seal_item(public, context, plaintext)` encrypts without a scan. +After decryption, `item_view(output, length, expected_context)` validates context +and returns a borrowed view. Derive context from trusted selected-record state, +binding owner, application, record, purpose and algorithm. Checking it does not +prevent rollback or identify the sender: anyone with the public point can encrypt. +The application remains responsible for authenticated storage and rollback policy. diff --git a/tools/aurora-sep/keys/sep_envelope.py b/tools/aurora-sep/keys/sep_envelope.py new file mode 100644 index 00000000000000..73d1b2a5157fb7 --- /dev/null +++ b/tools/aurora-sep/keys/sep_envelope.py @@ -0,0 +1,63 @@ +# SPDX-License-Identifier: GPL-2.0-only OR MIT +"""Reference item envelope for the local biometric ECIES decrypt extension. + +Requires Python cryptography. This is an integration example, not a production +secret-memory implementation: Python/backend temporaries cannot all be wiped. +""" + +import hashlib +import hmac + +from cryptography.hazmat.primitives import serialization +from cryptography.hazmat.primitives.asymmetric import ec +from cryptography.hazmat.primitives.ciphers.aead import AESGCM + + +MAX_SEALED = 4096 +MAGIC = b'SEPITEM\x01' + + +def seal_item(public, context, plaintext): + """Encrypt to a recipient public point; no biometric scan is needed. + + context is the application's canonical binding of owner, record ID, purpose + and algorithm. It is encrypted/authenticated with the item, not sent as AAD. + """ + if len(public) != 65 or public[0] != 4: + raise ValueError('expected uncompressed P-256 public point') + if not context or len(context) > 1024: + raise ValueError('expected nonempty bounded item context') + envelope = MAGIC + len(context).to_bytes(4, 'big') + context + plaintext + if len(envelope) + 81 > MAX_SEALED: + raise ValueError('item envelope exceeds driver input limit') + peer = ec.EllipticCurvePublicKey.from_encoded_point(ec.SECP256R1(), public) + sender = ec.generate_private_key(ec.SECP256R1()) + ephemeral = sender.public_key().public_bytes( + serialization.Encoding.X962, serialization.PublicFormat.UncompressedPoint) + shared = sender.exchange(ec.ECDH(), peer) + material = b''.join(hashlib.sha256(shared + counter.to_bytes(4, 'big') + ephemeral).digest() + for counter in (1, 2)) + # Firmware profile: AES-256, derived 16-byte IV, no AAD, 16-byte tag. + return ephemeral + AESGCM(material[:32]).encrypt(material[32:48], envelope, None) + + +def item_view(output, length, expected_context): + """Validate trusted expected context, then borrow plaintext without copying. + + The caller owns output and must wipe it on success AND on exceptions. Do not + derive expected_context from an untrusted field beside the ciphertext. + """ + if not isinstance(output, bytearray) or not 0 <= length <= len(output): + raise ValueError('invalid plaintext buffer') + if not expected_context or len(expected_context) > 1024: + raise ValueError('invalid expected context') + view = memoryview(output)[:length] + header = len(MAGIC) + 4 + if length < header or view[:len(MAGIC)] != MAGIC: + raise ValueError('unsupported item envelope') + context_len = int.from_bytes(view[len(MAGIC):header], 'big') + if context_len != len(expected_context) or header + context_len > length: + raise ValueError('item context mismatch') + if not hmac.compare_digest(view[header:header + context_len], expected_context): + raise ValueError('item context mismatch') + return view[header + context_len:] diff --git a/tools/aurora-sep/keys/sep_key.py b/tools/aurora-sep/keys/sep_key.py new file mode 100644 index 00000000000000..35f937a908e260 --- /dev/null +++ b/tools/aurora-sep/keys/sep_key.py @@ -0,0 +1,227 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only OR MIT +"""Reference client for blocking, biometric application keys on /dev/sep-keys. + +Allocate a request ID before dispatching a worker, pass it as request_id=, and +cancel that ID from the UI thread. Never automatically retry an interrupted or +uncertain key operation. Join the caller before closing the device context. +""" + +import argparse +import base64 +import ctypes +import errno +import hashlib +import json +import os +from pathlib import Path +import struct +import threading + + +MAX_BLOB = 4096 + + +def ioctl_number(direction, number, size): + return (direction << 30) | (size << 16) | (0xB2 << 8) | number + + +INFO = ioctl_number(2, 0, 16) +CREATE = ioctl_number(3, 1, 88) +SIGN = ioctl_number(3, 2, 136) +DECRYPT = ioctl_number(3, 3, 56) +CANCEL = ioctl_number(1, 4, 8) +CAP_SIGN, CAP_DECRYPT = 1, 2 +libc = ctypes.CDLL(None, use_errno=True) +libc.ioctl.argtypes = (ctypes.c_int, ctypes.c_ulong, ctypes.c_void_p) +libc.ioctl.restype = ctypes.c_int + + +class SepClient: + def __init__(self): + self._next_id = 0 + self._id_lock = threading.Lock() + + def __enter__(self): + self.fd = os.open('/dev/sep-keys', os.O_RDWR | os.O_CLOEXEC) + try: + info = bytearray(16) + self._call(INFO, info) + version, self.capabilities, r1, r2 = struct.unpack('=4I', info) + if version != 1 or r1 or r2: + raise RuntimeError('unsupported key interface') + if not self.capabilities & CAP_SIGN: + raise OSError(errno.EOPNOTSUPP, 'application-key API unavailable') + except BaseException: + os.close(self.fd) + raise + return self + + def __exit__(self, *unused): + os.close(self.fd) + + def _call(self, command, request): + # fcntl.ioctl may automatically retry EINTR. Call libc exactly once. + storage = (ctypes.c_ubyte * len(request)).from_buffer(request) + if libc.ioctl(self.fd, command, ctypes.addressof(storage)) == -1: + error = ctypes.get_errno() + raise OSError(error, os.strerror(error)) + + def new_request_id(self): + with self._id_lock: + if self._next_id == (1 << 64) - 1: + raise OverflowError('open a new key device file') + self._next_id += 1 + return self._next_id + + def cancel(self, request_id): + request_id = self._request_id(request_id) + self._call(CANCEL, bytearray(struct.pack('=Q', request_id))) + + def _request_id(self, request_id): + if request_id is None: + return self.new_request_id() + if not 0 < request_id < 1 << 64: + raise ValueError('invalid request ID') + with self._id_lock: + self._next_id = max(self._next_id, request_id) + return request_id + + def create(self, decrypt=False): + if decrypt and not self.capabilities & CAP_DECRYPT: + raise OSError(errno.EOPNOTSUPP, 'biometric decryption unavailable') + blob = ctypes.create_string_buffer(MAX_BLOB) + request = bytearray(struct.pack('=IIQ65s7x', int(decrypt), MAX_BLOB, + ctypes.addressof(blob), b'')) + self._call(CREATE, request) + _, size, _, public = struct.unpack('=IIQ65s7x', request) + if not 0 < size <= MAX_BLOB or public[0] != 4: + raise RuntimeError('invalid key response') + return bytes(blob[:size]), public + + def sign_digest(self, blob, digest, timeout_ms=60000, *, request_id=None): + if not 0 < len(blob) <= MAX_BLOB or len(digest) != 32: + raise ValueError('expected wrapped key and SHA-256 digest') + if not 1 <= timeout_ms <= 120000: + raise ValueError('timeout must be 1..120000 ms') + request_id = self._request_id(request_id) + key = ctypes.create_string_buffer(blob) + request = bytearray(struct.pack('=QIIIIQ32s72x', request_id, 0, timeout_ms, len(blob), 0, + ctypes.addressof(key), digest)) + self._call(SIGN, request) + size = struct.unpack_from('=I', request, 20)[0] + if not 0 < size <= 72: + raise RuntimeError('invalid signature length') + return bytes(request[64:64 + size]) + + def decrypt_into(self, blob, sealed, output, timeout_ms=60000, *, request_id=None): + if not isinstance(output, bytearray) or len(output) != MAX_BLOB: + raise ValueError('output must be a 4096-byte bytearray') + destination = (ctypes.c_ubyte * len(output)).from_buffer(output) + ctypes.memset(ctypes.addressof(destination), 0, len(output)) + success = False + try: + if not self.capabilities & CAP_DECRYPT: + raise OSError(errno.EOPNOTSUPP, 'biometric decryption unavailable') + if not 0 < len(blob) <= MAX_BLOB or not 81 <= len(sealed) <= MAX_BLOB: + raise ValueError('invalid wrapped key or sealed length') + if sealed[0] != 4 or not 1 <= timeout_ms <= 120000: + raise ValueError('expected P-256 point and valid timeout') + request_id = self._request_id(request_id) + key, ciphertext = ctypes.create_string_buffer(blob), ctypes.create_string_buffer(sealed) + request = bytearray(struct.pack('=QIIIIQQIIQ', request_id, 0, timeout_ms, len(blob), len(sealed), + ctypes.addressof(key), ctypes.addressof(ciphertext), len(output), 0, + ctypes.addressof(destination))) + self._call(DECRYPT, request) + size = struct.unpack_from('=I', request, 40)[0] + if size > len(output): + raise RuntimeError('invalid plaintext length') + success = True + return size + finally: + if not success: + ctypes.memset(ctypes.addressof(destination), 0, len(output)) + + + +def verify(public, digest, signature): + from cryptography.hazmat.primitives import hashes + from cryptography.hazmat.primitives.asymmetric import ec, utils + + key = ec.EllipticCurvePublicKey.from_encoded_point(ec.SECP256R1(), public) + key.verify(signature, digest, ec.ECDSA(utils.Prehashed(hashes.SHA256()))) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + commands = parser.add_subparsers(dest='command', required=True) + create = commands.add_parser('create') + create.add_argument('key', type=Path) + create.add_argument('--decrypt', action='store_true', + help='create a biometric signing/decryption key') + for name in ('sign', 'sign-digest'): + sign = commands.add_parser(name) + sign.add_argument('key', type=Path) + sign.add_argument('message', type=Path) + sign.add_argument('signature', type=Path) + check = commands.add_parser('selftest-decrypt', help='decrypt disposable data with an existing key') + check.add_argument('key', type=Path) + args = parser.parse_args() + if args.command == 'create': + if args.key.exists(): + raise FileExistsError(args.key) + with SepClient() as client: + blob, public = client.create(decrypt=args.decrypt) + data = {'version': 1, 'policy': 'biometry-any', + 'blob': base64.b64encode(blob).decode(), + 'public': base64.b64encode(public).decode()} + if args.decrypt: + data['operations'] = ['sign', 'decrypt'] + fd = os.open(args.key, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_CLOEXEC, 0o600) + with os.fdopen(fd, 'w') as output: + json.dump(data, output) + output.flush() + os.fsync(output.fileno()) + print('Biometric P-256 key created') + else: + if args.key.stat().st_size > 8192: + raise ValueError('key file too large') + data = json.loads(args.key.read_text()) + if data['version'] != 1 or data['policy'] != 'biometry-any': + raise ValueError('unsupported key file') + blob = base64.b64decode(data['blob'], validate=True) + public = base64.b64decode(data['public'], validate=True) + if args.command == 'selftest-decrypt': + from sep_envelope import seal_item, item_view + context = b'sep-example:selftest-decrypt:v1' + expected = os.urandom(32) + sealed = seal_item(public, context, expected) + output = bytearray(MAX_BLOB) + try: + print('Place an enrolled finger on Touch ID', flush=True) + with SepClient() as client: + length = client.decrypt_into(blob, sealed, output) + if item_view(output, length, context) != expected: + raise RuntimeError('disposable plaintext mismatch') + finally: + destination = (ctypes.c_ubyte * MAX_BLOB).from_buffer(output) + ctypes.memset(ctypes.addressof(destination), 0, MAX_BLOB) + print('Disposable decryption verified') + return + with args.message.open('rb') as message: + if args.command == 'sign': + digest = hashlib.file_digest(message, 'sha256').digest() + else: + digest = message.read(33) + if len(digest) != 32: + raise ValueError('sign-digest requires exactly 32 raw SHA-256 bytes') + print('Place an enrolled finger on Touch ID', flush=True) + with SepClient() as client: + signature = client.sign_digest(blob, digest) + verify(public, digest, signature) + args.signature.write_bytes(signature) + print('Biometric signature verified') + + +if __name__ == '__main__': + main() diff --git a/tools/aurora-sep/tests/test_application_key_cleanup.py b/tools/aurora-sep/tests/test_application_key_cleanup.py new file mode 100644 index 00000000000000..a3fdc035c4e3a1 --- /dev/null +++ b/tools/aurora-sep/tests/test_application_key_cleanup.py @@ -0,0 +1,352 @@ +# SPDX-License-Identifier: GPL-2.0-only OR MIT +"""Run actual credential and application-key paths with scripted firmware replies. + +This complements the lifecycle/UAPI tests: allocation, transport, policy and +capture are adapters, not a second implementation of cleanup/publication. No +SEP device, secret material or live fault injection is involved. +""" +from pathlib import Path +import re +import shutil +import subprocess +import tempfile +import unittest + +APPLE = Path(__file__).resolve().parents[3] / 'drivers/soc/apple' + + +def function(source, name): + match = re.search(r'^ *(?:pub\(crate\) )?fn ' + name + r'\(', source, re.M) + if match is None: + raise AssertionError(f'Missing production function {name}') + opening = source.index('{', match.start()) + depth, end = 1, opening + 1 + while depth: + depth += (source[end] == '{') - (source[end] == '}') + end += 1 + return source[match.start():end] + + +HARNESS = r''' +#![allow(dead_code, unused_imports, unused_variables)] +use std::ffi::CStr; +use std::cell::Cell; +use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::Relaxed}; + +type Result = std::result::Result; +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct Error(i32); +impl Error { fn from_errno(n: i32) -> Self { Self(n) } } +const EIO: Error = Error(-5); +const EINVAL: Error = Error(-22); +const ENOENT: Error = Error(-2); +const EACCES: Error = Error(-13); +const ECANCELED: Error = Error(-125); +const ETIMEDOUT: Error = Error(-110); +const EROFS: Error = Error(-30); +const EBUSY: Error = Error(-16); +const EOPNOTSUPP: Error = Error(-95); +const GFP_KERNEL: () = (); +const SBIO_PROBE_USER_ID: i32 = 1000; +const ENROL_STATUS_TIMEOUT: u32 = 1; +static DELETES: AtomicUsize = AtomicUsize::new(0); +static WIPES: AtomicUsize = AtomicUsize::new(0); +macro_rules! dev_err { ($($t:tt)*) => { () }; } +macro_rules! static_assert { ($e:expr) => { const _: () = assert!($e); }; } +struct KVec(Vec); +impl KVec { + fn new() -> Self { Self(Vec::new()) } + fn as_slice(&self) -> &[T] { &self.0 } + fn push(&mut self, value: T, _: ()) -> Result { self.0.push(value); Ok(()) } +} +impl KVec { + fn extend_from_slice(&mut self, value: &[T], _: ()) -> Result { + self.0.extend_from_slice(value); Ok(()) + } +} +impl std::ops::Deref for KVec { type Target = [T]; fn deref(&self) -> &[T] { &self.0 } } +impl std::ops::DerefMut for KVec { fn deref_mut(&mut self) -> &mut [T] { &mut self.0 } } +fn bytes(value: &[u8]) -> KVec { KVec(value.to_vec()) } +struct Mutex(std::sync::Mutex); +impl Mutex { + fn new(value: T) -> Self { Self(std::sync::Mutex::new(value)) } + fn lock(&self) -> std::sync::MutexGuard<'_, T> { self.0.lock().unwrap() } +} +mod kernel { + pub mod bindings { pub const EOPNOTSUPP: u32 = 95; } + pub mod time { pub fn msecs_to_jiffies(n: u32) -> u32 { n } } + pub mod sync { pub enum CondVarTimeoutResult { Signal {}, Timeout } } +} +struct CondVar; +impl CondVar { + fn wait_interruptible_timeout(&self, _: &mut T, _: u32) -> kernel::sync::CondVarTimeoutResult { + panic!("cleanup tests must not enter a plaintext delivery wait") + } +} +mod shim { pub fn monotonic_ns() -> u64 { 1 } } +mod sks { pub const SKS_AUTH_TOKEN_LEN: usize = 16; pub const SKS_PERFORM_OP_NAME: &str = "perform"; } +mod profile { + pub enum KeyStore { Sepos13 {}, Other } + pub struct PlatformProfile { pub key_store: KeyStore } +} +mod der { + pub enum RefKeyValue<'a> { Utf8(&'a [u8]), Octets(&'a [u8]), Der(&'a [u8]), Integer(u32) } + pub fn encode_refkey_set(items: &[(&[u8], RefKeyValue<'_>)]) -> super::Result> { + for (name, value) in items { + if *name == b"o" { if let RefKeyValue::Utf8(op) = value { return Ok(super::bytes(op)); } } + } + Err(super::EINVAL) + } + pub fn octet_string_body(_: &[u8]) -> Option<&[u8]> { None } +} +mod refkey_policy { + pub fn biometric_signing_policy(_: &[u8; 16]) -> super::Result> { Ok(super::bytes(b"policy")) } + pub use biometric_signing_policy as biometric_combined_policy; +} +mod proto { + pub struct FieldCursor<'a>(&'a [u8]); + impl<'a> FieldCursor<'a> { + pub fn new(b: &'a [u8]) -> Self { Self(b) } + pub fn i32(&mut self) -> Option { Some(0) } + pub fn blob(&mut self) -> Option<&'a [u8]> { Some(self.0) } + } +} +mod keybag { + pub enum Slot { Identity } + pub enum State { Present(Stored), Absent } + pub struct Stored; + impl Stored { pub fn secret(&self) -> &[u8] { b"synthetic bag secret" } } + pub fn read(_: Slot) -> super::Result { Ok(State::Present(Stored)) } +} +mod sbio { + pub struct UserId(i32); + impl UserId { pub fn new(n: i32) -> Option { Some(Self(n)) } pub fn value(&self) -> i32 { self.0 } } + pub fn sbio_catacomb_id(user: UserId) -> UserId { user } +} +enum SbioOutcome { Ok(KVec), Failed } +mod bio { + pub const ATTEST_PUB_LEN: usize = 65; + pub const ATTEST_SIG_MAX: usize = 72; + pub enum VerifyOutcome { Matched(()), NoMatch, Failed(u32) } + pub struct Session { + pub checks: super::Cell, + pub cancel_at_check: u32, + pub result: Option>, + pub deletes_at_publish: usize, + pub wipes_at_publish: usize, + } + impl Session { pub fn available(&self) -> bool { true } } + pub mod key { + use super::super::*; + pub fn key_is_live(s: &super::Session, _: u64) -> bool { + let n = s.checks.get() + 1; s.checks.set(n); + s.cancel_at_check == 0 || n < s.cancel_at_check + } + pub fn finish_key(s: &mut super::Session, _: u64, result: Result) -> bool { + s.deletes_at_publish = DELETES.load(Relaxed); + s.wipes_at_publish = WIPES.load(Relaxed); + s.result = Some(result); true + } + pub fn key_plaintext_wait_ns(_: &mut super::Session, _: u64, _: u64) -> Option { None } + } + pub fn abort_capture(_: &mut Session) {} +} +mod key_request { + use super::*; + pub struct Request<'a> { pub data: keys::KeyRequest, pub sep: &'a SepData } + impl Request<'_> { + pub fn is_live(&self) -> bool { bio::key::key_is_live(&self.sep.bio_session.lock(), 1) } + pub fn complete(&self, result: Result) { + bio::key::finish_key(&mut self.sep.bio_session.lock(), 1, result); + } + } +} +#[derive(Clone, Copy, PartialEq)] +enum Fault { None, Externalize, Unload, Binding, Operation } +struct Outcome { reply: Reply, body: KVec } +struct Reply { status: i32 } +enum Request { Unload, Verify, Refkey(KVec) } +struct Material { special: i32, secret: KVec } +struct SepData { + profile: profile::PlatformProfile, + key_api_failed: AtomicBool, + suspending: AtomicBool, + bio_session: Mutex, + key_result_changed: CondVar, + enrol_material: Mutex>, + delete_mode: u8, + fault: Fault, + calls: Cell, + private_uses: Cell, +} +struct Prepared<'a>(&'a SepData); +impl Prepared<'_> { + fn verify(self, _: Option<&[u8; 16]>) -> bio::VerifyOutcome { bio::VerifyOutcome::Matched(()) } +} +impl SepData { + fn new(delete_mode: u8, fault: Fault, cancel_at_check: u32) -> Self { + DELETES.store(0, Relaxed); WIPES.store(0, Relaxed); + Self { profile: profile::PlatformProfile { key_store: profile::KeyStore::Sepos13 {} }, + key_api_failed: AtomicBool::new(false), suspending: AtomicBool::new(false), + bio_session: Mutex::new(bio::Session { checks: Cell::new(0), cancel_at_check, + result: None, deletes_at_publish: 0, wipes_at_publish: 0 }), key_result_changed: CondVar, + enrol_material: Mutex::new(Some(Material { special: 1, secret: bytes(b"synthetic") })), + delete_mode, fault, calls: Cell::new(0), private_uses: Cell::new(0) } + } + fn called(&self) { self.calls.set(self.calls.get() + 1); } + fn scrd_ready(&self) -> bool { true } + fn scrd_command(&self, c: &scrd::ScrdCommand) -> Option<(i32, KVec)> { + self.called(); + match c.payload()[4] { + 0x02 => { DELETES.fetch_add(1, Relaxed); match self.delete_mode { + 0 => Some((0, bytes(b""))), 1 => Some((1, bytes(b""))), _ => None } }, + 0x24 => Some((0, bytes(&[1; 16]))), + 0x13 if self.fault == Fault::Externalize => None, + 0x03 => Some((0, bytes(&1u32.to_le_bytes()))), + _ => Some((0, bytes(b""))), + } + } + fn sks_recover(&self, _: &keybag::Stored) -> Option<(i32, ())> { self.called(); Some((1, ())) } + fn sks_prepare_identity_refkey(&self, _: i32, _: &[u8]) -> bool { self.called(); true } + fn sks_req_unload_keybag(&self, _: i32) -> Result { Ok(Request::Unload) } + fn sks_req_verify_secret(&self, _: i32, _: &[u8], _: &[u8]) -> Result { Ok(Request::Verify) } + fn sks_req_refkey(&self, _: i32, body: &[u8]) -> Result { Ok(Request::Refkey(bytes(body))) } + fn sks_send(&self, request: Result) -> Option { + self.called(); + let request = request.ok()?; + if matches!(request, Request::Unload) && self.fault == Fault::Unload { return None; } + if let Request::Refkey(op) = request { + if &*op != b"oc" { self.private_uses.set(self.private_uses.get() + 1); } + if self.fault == Fault::Operation { return Some(Outcome { reply: Reply { status: -8 }, body: bytes(b"") }); } + let body = match &*op { b"oc" => bytes(b"wrapped"), b"osgn" => bytes(&[0x30, 0]), b"oecd" => bytes(&[0x7a; 32]), _ => panic!("unexpected op") }; + return Some(Outcome { reply: Reply { status: 0 }, body }); + } + Some(Outcome { reply: Reply { status: 0 }, body: bytes(b"") }) + } + fn sks_report_response<'a>(&self, _: &str, out: &'a Outcome) -> Option<&'a [u8]> { Some(&out.body) } + fn sbio_call(&self, _: &sbio::UserId) -> SbioOutcome { + self.called(); SbioOutcome::Ok(bytes(if self.fault == Fault::Binding { &[1; 15] } else { &[1; 16] })) + } + fn refkey_pub(_: &[u8]) -> Option<&'static [u8]> { Some(&[4; 65]) } + fn prepare_verify_capture(&self) -> std::result::Result, u32> { self.called(); Ok(Prepared(self)) } + fn bio_wake(&self) {} + fn xart_writable(&self) -> bool { true } +} +''' + +TESTS = r''' +#[test] +fn cleanup_failure_suppresses_success_and_latches() { + for operation in 0..3 { + for delete_mode in 0..3 { + let sep = SepData::new(delete_mode, Fault::None, 0); + let input = match operation { + 0 => keys::Input::Create(0), + 1 => keys::Input::Sign([0; 32]), + _ => keys::Input::Decrypt(bytes(b"sealed")), + }; + let success = { + sep.run_key_operation(&key_request::Request { sep: &sep, data: keys::KeyRequest { blob: bytes(b"wrapped"), + input, deadline_ns: u64::MAX } }); + let mut session = sep.bio_session.lock(); + assert_eq!(session.deletes_at_publish, 1, "published before disposal"); + if delete_mode != 0 { + assert!(matches!(session.result, Some(Err(EIO)))); + if operation == 2 { assert_eq!(session.wipes_at_publish, 1, "plaintext not wiped before error publication"); } + } + session.result.take().unwrap().is_ok() + }; + assert_eq!(DELETES.load(Relaxed), 1, "context must be deleted exactly once"); + assert_eq!(success, delete_mode == 0); + if operation == 2 { assert_eq!(WIPES.load(Relaxed), 1); } + if delete_mode != 0 { + assert!(!sep.biometric_keys_supported()); + let calls = sep.calls.get(); + assert!(matches!(sep.start_gate(), Err(Error(-95)))); + assert!(matches!(sep.create_biometric_key(0), Err(Error(-95)))); + assert!(matches!(sep.key_credential(), Err(Error(-95)))); + assert_eq!(sep.calls.get(), calls, "poisoned operations reached firmware"); + } + } + } +} + +#[test] +fn early_exits_keep_fallback_cleanup() { + for fault in [Fault::Externalize, Fault::Unload, Fault::Binding, Fault::Operation] { + let sep = SepData::new(0, fault, 0); + assert!(sep.create_biometric_key(0).is_err()); + assert_eq!(DELETES.load(Relaxed), 1); + } + for decrypt in [false, true] { + let sep = SepData::new(0, Fault::Operation, 0); + sep.run_key_operation(&key_request::Request { sep: &sep, data: keys::KeyRequest { blob: bytes(b"wrapped"), input: if decrypt { + keys::Input::Decrypt(bytes(b"sealed")) + } else { keys::Input::Sign([0; 32]) }, deadline_ns: u64::MAX } }); + assert_eq!(DELETES.load(Relaxed), 1); + assert!(matches!(sep.bio_session.lock().result, Some(Err(EACCES)))); + } + // Cancel after context creation and after matching, before private-key use. + for cancel_at_check in [3, 4] { + for delete_mode in 0..3 { + let sep = SepData::new(delete_mode, Fault::None, cancel_at_check); + sep.run_key_operation(&key_request::Request { sep: &sep, data: keys::KeyRequest { blob: bytes(b"wrapped"), input: keys::Input::Sign([0; 32]), deadline_ns: u64::MAX } }); + assert_eq!(DELETES.load(Relaxed), 1); + // At this worker boundary cleanup failure may take precedence as + // EIO. The real session-generation cancellation tests are separate. + let session = sep.bio_session.lock(); + assert!(matches!(session.result, Some(Err(ECANCELED | EIO)))); + assert_eq!(sep.private_uses.get(), 0, "private-key operation after cancellation"); + assert_eq!(sep.key_api_failed.load(Relaxed), delete_mode != 0); + } + } +} +''' + + +class CredentialCleanup(unittest.TestCase): + @unittest.skipUnless(shutil.which('rustc'), 'requires rustc') + def test_driver_cleanup_paths(self): + keys = (APPLE / 'keys.rs').read_text() + sep = (APPLE / 'sep.rs').read_text() + scrd = (APPLE / 'scrd.rs').read_text() + image = (APPLE / 'image.rs').read_text() + code = HARNESS + code += '\nmod scrd { use super::*; use crate::sks::SKS_AUTH_TOKEN_LEN;\n' + code += scrd[scrd.index('pub(crate) const SCRD_ACM_HANDLE_LEN'):scrd.index('pub(crate) fn decode_scrd')] + code += '\n}\nmod image { use super::*;\n' + code += function(image, 'wipe').replace('fn wipe(', 'fn production_wipe(', 1) + code += ''' + pub fn wipe(bytes: &mut [u8]) { + let plaintext = bytes == [0x7a; 32]; + production_wipe(bytes); + if plaintext { assert!(bytes.iter().all(|b| *b == 0)); WIPES.fetch_add(1, Relaxed); } + } + }\n''' + code += sep[sep.index('pub(crate) struct Secret'):sep.index('#[derive(Clone, Copy)]\nstruct Abandoned')] + code += '\nmod keys { use super::*; use crate::der::RefKeyValue as V;\n' + code += keys[keys.index('pub(crate) struct Credential'):keys.index('impl SepData {')] + code += keys[keys.index('pub(crate) struct Signature'):keys.index('impl SepData {', keys.index('pub(crate) struct Signature'))] + code += 'pub const MAX_BLOB: usize = 4096;\nimpl SepData {\n' + for name in ('biometric_keys_supported', 'key_credential', 'application_refkey_op', + 'create_biometric_key', 'sign_biometric_key', 'decrypt_biometric_key', 'run_key_operation'): + code += function(keys, name) + '\n' + code += function(sep, 'establish_passcode_validated_context') + # Exercise the new node's actual failure-latch gate too. + device = (APPLE / 'key_device.rs').read_text() + code += '\npub(crate) fn start_gate(&self) -> Result { key_device::File { sep: self }.check_api() }\n}\n}\n' + code += "mod key_device {use super::*; pub struct File<'a>{pub sep:&'a SepData} impl File<'_>{\n" + code += function(device, 'check_api').replace('fn check_api', 'pub fn check_api', 1) + code += '} }\n' + code += TESTS + with tempfile.TemporaryDirectory(prefix='sep-cleanup-') as directory: + source = Path(directory) / 'cleanup.rs' + source.write_text(code) + binary = Path(directory) / 'cleanup' + subprocess.run(['rustc', '--edition=2021', '--test', str(source), '-o', str(binary)], check=True) + # Shared counters observe destruction before publication; serialize cases. + subprocess.run([str(binary), '--test-threads=1'], check=True) + + +if __name__ == '__main__': + unittest.main() diff --git a/tools/aurora-sep/tests/test_application_keys.py b/tools/aurora-sep/tests/test_application_keys.py new file mode 100644 index 00000000000000..2ca438a8ea5680 --- /dev/null +++ b/tools/aurora-sep/tests/test_application_keys.py @@ -0,0 +1,84 @@ +# SPDX-License-Identifier: GPL-2.0-only OR MIT +"""Check the exported ABI and reference-client failure cleanup without SEP.""" +from pathlib import Path +import errno +import shutil +import struct +import subprocess +import sys +import tempfile +import unittest +from unittest.mock import patch + +ROOT = Path(__file__).resolve().parents[3] +KEYS = ROOT / 'tools/aurora-sep/keys' +sys.path.insert(0, str(KEYS)) + +import sep_key + + +class ApplicationKeys(unittest.TestCase): + def test_c_and_cpp_uapi_matches_client(self): + constants = ('INFO', 'CANCEL', 'CREATE', 'SIGN', 'DECRYPT') + header = ROOT / 'include/uapi/linux/apple-sep.h' + source = f'#include \"{header}\"\n#include \n' + source += '#ifdef __cplusplus\n#define CHECK static_assert\n#else\n#define CHECK _Static_assert\n#endif\n' + for name in constants: + symbol = 'SEP_KEY_' + name + client = sep_key + source += f'CHECK({symbol} == {getattr(client, name)}, "{name}");\n' + for name, size in [('info', 16), ('create', 88), ('sign', 136), ('decrypt', 56), ('cancel', 8)]: + source += f'CHECK(sizeof(struct sep_key_{name}) == {size}, "{name}");\n' + source += 'CHECK(offsetof(struct sep_bio_attest, public_key) == 36, "attest");\n' + source += 'CHECK(offsetof(struct sep_key_create, public_key) == 16, "create");\n' + source += f'CHECK(SEP_KEY_CAP_SIGN == {sep_key.CAP_SIGN}, "sign capability");\n' + source += f'CHECK(SEP_KEY_CAP_DECRYPT == {sep_key.CAP_DECRYPT}, "decrypt capability");\n' + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / 'abi.c' + path.write_text(source) + for compiler, language in [('cc', 'c'), ('c++', 'c++')]: + if not shutil.which(compiler): + self.skipTest(f'requires {compiler}') + subprocess.run([compiler, '-x', language, '-fsyntax-only', + str(path)], check=True) + + def test_interrupted_and_failed_syscalls_are_not_retried(self): + for decrypt in (False, True): + for failure in (errno.EINTR, errno.EIO, errno.EFAULT): + with self.subTest(decrypt=decrypt, failure=failure): + client = sep_key.SepClient() + client.fd, client.capabilities = 42, 3 + output = bytearray(b'x' * sep_key.MAX_BLOB) + with patch.object(client, '_call', side_effect=OSError(failure, 'injected')) as call: + with self.assertRaises(OSError) as error: + if decrypt: + client.decrypt_into(b'blob', b'\x04' + bytes(80), output) + else: + client.sign_digest(b'blob', bytes(32)) + self.assertEqual(error.exception.errno, failure) + self.assertEqual(call.call_count, 1) + if decrypt: + self.assertFalse(any(output)) + + def test_preallocated_cancellation_id_is_not_replaced_or_retried(self): + client = sep_key.SepClient() + client.fd, client.capabilities = 42, 3 + request_id = client.new_request_id() + calls = [] + + def call(command, request): + calls.append((command, struct.unpack_from('=Q', request)[0])) + if command == sep_key.SIGN: + raise OSError(errno.ECANCELED, 'retired before acceptance') + + with patch.object(client, '_call', call): + client.cancel(request_id) + with self.assertRaises(OSError) as error: + client.sign_digest(b'blob', bytes(32), request_id=request_id) + self.assertEqual(error.exception.errno, errno.ECANCELED) + self.assertEqual(calls, [(sep_key.CANCEL, request_id), (sep_key.SIGN, request_id)]) + self.assertGreater(client.new_request_id(), request_id) + + +if __name__ == '__main__': + unittest.main() diff --git a/tools/aurora-sep/tests/test_blocking_requests.py b/tools/aurora-sep/tests/test_blocking_requests.py new file mode 100644 index 00000000000000..e5c9af76969bf0 --- /dev/null +++ b/tools/aurora-sep/tests/test_blocking_requests.py @@ -0,0 +1,276 @@ +# SPDX-License-Identifier: GPL-2.0-only OR MIT +"""Exercise production blocking request/file/lease transitions without SEP. + +Pin allocation, condvar signals and uaccess are host adapters. Request, file, +capture ownership, result-copy branches and volatile wiping use actual source. +""" +from pathlib import Path +import re +import shutil +import subprocess +import tempfile +import unittest + +APPLE = Path(__file__).resolve().parents[3] / 'drivers/soc/apple' + + +def function(source, name): + match = re.search(r'^ *(?:pub\(crate\) )?fn ' + name + r'\(', source, re.M) + opening = source.index('{', match.start()) + depth, end = 1, opening + 1 + while depth: + depth += (source[end] == '{') - (source[end] == '}') + end += 1 + return source[match.start():end] + + +ADAPTER = r''' +#![allow(dead_code, unused_imports)] +use std::sync::{Arc, atomic::{AtomicBool, AtomicUsize, AtomicU64, Ordering::Relaxed}}; +use std::ops::{Deref, DerefMut}; +use crate::key_request::Request; +type Error = i32; +type Result = std::result::Result; +const EBUSY:i32=-16; const EINVAL:i32=-22; const EIO:i32=-5; +const ECANCELED:i32=-125; const ETIMEDOUT:i32=-110; const EACCES:i32=-13; +const EALREADY:i32=-114; const ENOENT:i32=-2; const ENODATA:i32=-61; +const EOVERFLOW:i32=-75; const ENODEV:i32=-19; const EINTR:i32=-4; +const EFAULT:i32=-14; const ENOSPC:i32=-28; +const ERESTARTSYS:i32=-512; const ERESTARTNOINTR:i32=-513; +const ERESTARTNOHAND:i32=-514; const ERESTART_RESTARTBLOCK:i32=-516; +const GFP_KERNEL:()=(); +static CLOCK:AtomicU64=AtomicU64::new(1); +static WIPES:AtomicUsize=AtomicUsize::new(0); +static LOCKED:AtomicUsize=AtomicUsize::new(0); +static COPIES:AtomicUsize=AtomicUsize::new(0); +static FAULT:AtomicUsize=AtomicUsize::new(0); +static ENQUEUED:AtomicUsize=AtomicUsize::new(0); +static QUEUE_FAIL:AtomicBool=AtomicBool::new(false); +const KEY_WORK_ID:u64=5; +struct KVec(Vec); +impl KVec{fn new()->Self{Self(Vec::new())}} +impl Deref for KVec{type Target=[T];fn deref(&self)->&[T]{&self.0}} +impl DerefMut for KVec{fn deref_mut(&mut self)->&mut[T]{&mut self.0}} +struct Mutex(std::sync::Mutex); +struct Guard<'a,T>(Option>); +impl Mutex{ + fn new(value:T)->Self{Self(std::sync::Mutex::new(value))} + fn lock(&self)->Guard<'_,T>{let guard=self.0.lock().unwrap();LOCKED.fetch_add(1,Relaxed);Guard(Some(guard))} +} +impl Deref for Guard<'_,T>{type Target=T;fn deref(&self)->&T{self.0.as_ref().unwrap()}} +impl DerefMut for Guard<'_,T>{fn deref_mut(&mut self)->&mut T{self.0.as_mut().unwrap()}} +impl Drop for Guard<'_,T>{fn drop(&mut self){LOCKED.fetch_sub(1,Relaxed);}} +enum CondVarTimeoutResult{Signal{},Timeout,Woken{}} +struct CondVar{cv:std::sync::Condvar, signal:AtomicUsize} +impl CondVar{ + fn new()->Self{Self{cv:std::sync::Condvar::new(),signal:AtomicUsize::new(0)}} + fn notify_all(&self){self.cv.notify_all();} + fn wait_interruptible_timeout(&self,guard:&mut Guard<'_,key_request::Phase>,ms:u32)->CondVarTimeoutResult{ + match self.signal.swap(0,Relaxed){ + 1=>return CondVarTimeoutResult::Signal{}, + 2=>{**guard=key_request::Phase::Complete(Ok(keys::Output::Plaintext(Secret(KVec(vec![42])))));return CondVarTimeoutResult::Signal{}}, + _=>{}, + } + LOCKED.fetch_sub(1,Relaxed); + let (inner,timed)=self.cv.wait_timeout(guard.0.take().unwrap(),std::time::Duration::from_millis(u64::from(ms.min(100)))).unwrap(); + guard.0=Some(inner);LOCKED.fetch_add(1,Relaxed); + if timed.timed_out(){CondVarTimeoutResult::Timeout}else{CondVarTimeoutResult::Woken{}} + } +} +mod shim{pub fn monotonic_ns()->u64{super::CLOCK.load(super::Relaxed)}} +mod kernel{ + pub mod time{pub fn msecs_to_jiffies(ms:u32)->u32{ms}} + pub mod workqueue{ + pub struct Queue; + pub fn system_dfl()->Queue{Queue} + impl Queue{pub fn enqueue(&self,_:W)->std::result::Result<(),()>{ + use super::super::*; + assert!(LOCKED.load(Relaxed)>0,"enqueue escaped the removal-serialization lock"); + ENQUEUED.fetch_add(1,Relaxed); + if QUEUE_FAIL.load(Relaxed){Err(())}else{Ok(())} + }} + } + pub mod uaccess{ + #[derive(Clone,Copy)] pub struct UserPtr; + impl UserPtr{pub fn from_addr(_:usize)->Self{Self}} + pub struct UserSlice; pub struct Writer; + impl UserSlice{pub fn new(_:UserPtr,_:usize)->Self{Self}pub fn writer(&self)->Writer{Writer}} + impl Writer{ + fn copy(&self)->super::super::Result{use super::super::*;assert_eq!(LOCKED.load(Relaxed),0,"user copy under a state lock");let n=COPIES.fetch_add(1,Relaxed)+1;if FAULT.load(Relaxed)==n{Err(EFAULT)}else{Ok(())}} + pub fn write(&mut self,_:&T)->super::super::Result{self.copy()} + pub fn write_slice(&mut self,_:&[u8])->super::super::Result{self.copy()} + } + } +} +mod bio{pub const ATTEST_SIG_MAX:usize=72;pub const ATTEST_PUB_LEN:usize=65;} +''' + +TESTS = r''' +fn data(deadline_ns:u64)->keys::KeyRequest{ + keys::KeyRequest{blob:KVec::new(),input:keys::Input::Decrypt(KVec::new()),deadline_ns} +} +fn request(id:u64)->Arc{key_request::Request::new(id,data(u64::MAX)).unwrap()} +fn plain()->Result{Ok(keys::Output::Plaintext(Secret(KVec(vec![42]))))} + +#[test] +fn cancellation_expiry_interruption_and_cleanup_failures_suppress_delivery(){ + for mode in 0..6{ + CLOCK.store(1,Relaxed);WIPES.store(0,Relaxed); + let request=request(1); + match mode{ + 0=>{request.complete(plain());request.cancel(ECANCELED).unwrap();}, + 1=>{request.cancel(ENODEV).unwrap();request.complete(plain());}, + 2=>{request.complete(plain());CLOCK.store(u64::MAX,Relaxed);}, + 3=>{request.inject_signal(2);}, + 4=>{request.complete(Err(EIO));assert!(matches!(request.cancel(ECANCELED),Err(ENOENT)));CLOCK.store(u64::MAX,Relaxed);}, + 5=>{request.inject_signal(1);}, + _=>unreachable!(), + } + let expected=match mode{0=>ECANCELED,1=>ENODEV,2=>ETIMEDOUT,3|5=>EINTR,_=>EIO}; + assert!(matches!(request.wait(),Err(e) if e==expected)); + if mode<4{assert_eq!(WIPES.load(Relaxed),1);} + assert!(!request.is_live()); + request.complete(plain()); + assert!(matches!(request.wait(),Err(e) if e==expected)); + } + CLOCK.store(1,Relaxed); +} + +#[test] +fn concurrent_cancel_is_request_scoped_and_covers_preacceptance(){ + let sep=SepData::new(); + let first=Arc::new(file::new(sep.clone()));let other=file::new(sep); + // Cancel before descriptor/input copying has registered the request. + first.cancel(1).unwrap(); + assert!(matches!(first.begin(Some(request(1))),Err(ECANCELED))); + let req=request(2);let active=first.begin(Some(req.clone())).unwrap(); + assert!(matches!(first.begin(Some(request(3))),Err(EBUSY))); + assert!(other.cancel(2).is_ok()); // retires only the other file's future ID + assert!(req.is_live()); + std::thread::scope(|scope|{ + let waiter=scope.spawn(||req.wait()); + first.cancel(2).unwrap(); + assert!(matches!(waiter.join().unwrap(),Err(ECANCELED))); + }); + drop(active); + let next=request(3);let _active=first.begin(Some(next.clone())).unwrap(); + assert!(matches!(first.cancel(2),Err(ENOENT))); + req.complete(plain());assert!(next.is_live()); + next.complete(plain());let result=next.wait().unwrap(); + assert!(matches!(first.cancel(3),Err(EALREADY)));drop(result); +} + +#[test] +fn capture_lease_survives_cancel_until_worker_drain(){ + let sep=SepData::new(); + let req=request(1);sep.capture.lock().start_key(req.clone()).unwrap(); + req.cancel(ECANCELED).unwrap();assert!(matches!(capture::Lease::acquire(&sep),Err(EBUSY))); + let unrelated=request(2);sep.capture.lock().finish_key(&unrelated); + assert!(matches!(capture::Lease::acquire(&sep),Err(EBUSY))); + sep.capture.lock().finish_key(&req); + let lease=capture::Lease::acquire(&sep).unwrap();assert_eq!(sep.running.load(Relaxed),1); + lease.into_bio(8);assert_eq!(sep.running.load(Relaxed),0); + sep.capture.lock().finish_bio(7);assert!(matches!(capture::Lease::acquire(&sep),Err(EBUSY))); + sep.capture.lock().finish_bio(8); + drop(capture::Lease::acquire(&sep).unwrap());assert_eq!(sep.running.load(Relaxed),0); + sep.suspending.store(true,Relaxed);assert!(matches!(capture::Lease::acquire(&sep),Err(EBUSY))); + sep.shutting_down.store(true,Relaxed);assert!(matches!(capture::Lease::acquire(&sep),Err(ENODEV))); +} + +#[test] +fn enqueue_failure_and_removal_revoke_without_losing_ownership(){ + CLOCK.store(1,Relaxed);ENQUEUED.store(0,Relaxed);QUEUE_FAIL.store(true,Relaxed); + let sep=SepData::new();let failed=request(1); + assert_eq!(SepData::queue_key(sep.clone(),failed.clone()),Err(EIO)); + assert!(matches!(failed.wait(),Err(EIO))); + drop(capture::Lease::acquire(&sep).unwrap()); + QUEUE_FAIL.store(false,Relaxed); + let active=request(2);SepData::queue_key(sep.clone(),active.clone()).unwrap(); + sep.shutting_down.store(true,Relaxed);sep.cancel_key(ENODEV); + assert!(matches!(active.wait(),Err(ENODEV))); + assert!(sep.capture.lock().key().is_some()); // worker still owns its lease + assert_eq!(SepData::queue_key(sep.clone(),request(3)),Err(ENODEV)); + assert_eq!(ENQUEUED.load(Relaxed),2); + sep.capture.lock().finish_key(&active); +} + +#[test] +fn faulted_copy_consumes_claim_and_wipes_without_holding_locks(){ + for fault in 0..3{ + WIPES.store(0,Relaxed);COPIES.store(0,Relaxed);FAULT.store(fault,Relaxed); + let request=request(1);request.complete(plain());let result=request.wait().unwrap(); + let destination=file::decrypt_destination(); + let delivery=file::deliver(result,destination,kernel::uaccess::UserPtr); + assert_eq!(delivery,if fault==0{Ok(())}else{Err(EFAULT)}); + assert_eq!(WIPES.load(Relaxed),1); + assert!(matches!(request.wait(),Err(ENODATA))); + } +} + +#[test] +fn worker_errors_cannot_restart_or_look_like_presubmission_contention(){ + CLOCK.store(1,Relaxed); + for error in [ERESTARTSYS,ERESTARTNOINTR,ERESTARTNOHAND,ERESTART_RESTARTBLOCK,EBUSY]{ + let request=request(1);request.complete(Err(error)); + let expected=if error==EBUSY{EIO}else{EINTR}; + assert!(matches!(request.wait(),Err(e) if e==expected)); + assert!(matches!(request.wait(),Err(e) if e==expected)); + } +} +''' + + +class BlockingRequests(unittest.TestCase): + @unittest.skipUnless(shutil.which('rustc'), 'requires rustc') + def test_request_file_capture_and_delivery(self): + keys = (APPLE / 'keys.rs').read_text() + device = (APPLE / 'key_device.rs').read_text() + request = (APPLE / 'key_request.rs').read_text() + capture = (APPLE / 'capture.rs').read_text() + sep = (APPLE / 'sep.rs').read_text() + code = ADAPTER + '\nmod image {use super::*;\n' + code += function((APPLE / 'image.rs').read_text(), 'wipe').replace('fn wipe(', 'fn production_wipe(', 1) + code += '\npub fn wipe(bytes:&mut[u8]){let plain=bytes==[42];production_wipe(bytes);if plain{assert_eq!(bytes,[0]);WIPES.fetch_add(1,Relaxed);}}}\n' + code += sep[sep.index('pub(crate) struct Secret'):sep.index('#[derive(Clone, Copy)]\nstruct Abandoned')] + code += '\nmod keys{use super::*;\n' + code += keys[keys.index('pub(crate) struct Signature'):keys.index('impl SepData {', keys.index('pub(crate) struct Signature'))] + '}\n' + body = request[request.index('enum Phase'):].replace('enum Phase', 'pub(crate) enum Phase', 1) + body = body.replace(function(body, 'new'), '''pub(crate) fn new(id:u64,data:KeyRequest)->Result>{ + Ok(Arc::new(Self{id,data,phase:Mutex::new(Phase::Pending),changed:CondVar::new()})) + } + pub(crate) fn inject_signal(&self,n:usize){self.changed.signal.store(n,Relaxed);}''') + code += '\nmod key_request{use super::*;use crate::keys::{KeyRequest,Output};\n' + body + '}\n' + code += '\nmod capture{use super::*;use crate::key_request::Request;\n' + capture[capture.index('enum Owner'):] + '}\n' + code += '''struct SepData{capture:Mutex,shutting_down:AtomicBool,suspending:AtomicBool,running:AtomicUsize} + impl SepData{ + fn new()->Arc{Arc::new(Self{capture:Mutex::new(capture::State::new()),shutting_down:AtomicBool::new(false),suspending:AtomicBool::new(false),running:AtomicUsize::new(0)})} + fn capture_begin(&self)->bool{if self.suspending.load(Relaxed){false}else{self.running.fetch_add(1,Relaxed);true}} + fn capture_end(&self){assert!(self.running.fetch_sub(1,Relaxed)>0);} + }\n''' + code += 'impl SepData {\n' + function(device, 'queue_key') + '\n' + function(device, 'cancel_key') + '\n}\n' + code += 'mod file{use super::*;use crate::key_request::Request;use kernel::uaccess::{UserPtr,UserSlice};\n' + code += device[device.index('#[repr(C)]'):device.index('static_assert!')] + code += device[device.index('enum Operation'):device.index('impl File {')] + code += 'impl File {\n' + function(device, 'begin') + '\n' + function(device, 'cancel') + '\n}\n' + code += '''pub(crate) fn new(sep:Arc)->File{File{sep,state:Mutex::new(State{last_id:0,cancelled_through:0,operation:Operation::Idle})}} + pub(crate) fn decrypt_destination()->Destination{Destination::Decrypt(Decrypt{request_id:1,flags:0,timeout_ms:1,blob_len:1,sealed_len:81,blob_ptr:0,sealed_ptr:0,plain_len:4096,reserved:0,plain_ptr:0})} + pub(crate) fn deliver(result:keys::Output,destination:Destination,user:UserPtr)->Result{ + ''' + code += device[device.index(' match (result, destination)'):device.index('\nextern "C"')] + # The source's ioctl/impl closing braces also close this adapter module. + code = code.replace('struct File {', 'pub(crate) struct File {') + code = code.replace("struct Active<'a>", "pub(crate) struct Active<'a>") + code = code.replace('enum Destination {', 'pub(crate) enum Destination {') + code = re.sub(r'(?m)^struct (Sign|Decrypt) \{', r'pub(crate) struct \1 {', code) + code = re.sub(r'(?m)^ fn (begin|cancel)\(', r' pub(crate) fn \1(', code) + code = re.sub(r'#\[pin_data\]|#\[pin\]', '', code) + code += TESTS + with tempfile.TemporaryDirectory(prefix='sep-blocking-') as directory: + source, binary = Path(directory) / 'request.rs', Path(directory) / 'request' + source.write_text(code) + subprocess.run(['rustc', '--edition=2021', '--test', str(source), '-o', str(binary)], check=True) + subprocess.run([str(binary), '--test-threads=1'], check=True, timeout=10) + + +if __name__ == '__main__': + unittest.main()