Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion drivers/soc/apple/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions drivers/soc/apple/bio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
100 changes: 100 additions & 0 deletions drivers/soc/apple/capture.rs
Original file line number Diff line number Diff line change
@@ -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<Request>),
}

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<u64> {
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<Request>) -> Result {
if !matches!(self.owner, Owner::Idle) { return Err(EBUSY); }
self.owner = Owner::Key(request);
Ok(())
}

pub(crate) fn key(&self) -> Option<Arc<Request>> {
match &self.owner {
Owner::Key(request) => Some(request.clone()),
_ => None,
}
}

pub(crate) fn finish_key(&mut self, request: &Arc<Request>) {
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<Self> {
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;
}
}
}
10 changes: 7 additions & 3 deletions drivers/soc/apple/der.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ fn integer_bytes(v: u32) -> Result<KVec<u8>> {
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<KVec<u8>> {
let mut members: KVec<(&[u8], KVec<u8>)> = KVec::new();
for (key, value) in items {
Expand All @@ -65,6 +65,10 @@ pub(crate) fn encode_refkey_set(items: &[(&[u8], RefKeyValue<'_>)]) -> Result<KV
put_header(&mut inner, TAG_UTF8, key.len())?;
inner.extend_from_slice(key, GFP_KERNEL)?;
match value {
RefKeyValue::Boolean(v) => {
// 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);
Expand All @@ -91,7 +95,7 @@ pub(crate) fn encode_refkey_set(items: &[(&[u8], RefKeyValue<'_>)]) -> Result<KV
seq.extend_from_slice(&inner, GFP_KERNEL)?;
members.push((key, seq), GFP_KERNEL)?;
}
members.sort_unstable_by(|a, b| a.0.cmp(b.0));
members.sort_unstable_by(|a, b| a.1.as_slice().cmp(b.1.as_slice()));
let mut body = KVec::new();
for (_, seq) in members.iter() {
body.extend_from_slice(seq, GFP_KERNEL)?;
Expand Down
Loading