diff --git a/drivers/soc/apple/refkey.rs b/drivers/soc/apple/refkey.rs index f0fde4827754a..f4c9b5f736ea8 100644 --- a/drivers/soc/apple/refkey.rs +++ b/drivers/soc/apple/refkey.rs @@ -596,10 +596,10 @@ impl SepData { } /// Volume-key sealing: a ref-key without an access-control list under - /// the designated user bag `user` (outer handle `-uid`), protection - /// class 9, so the enclave uses it only while that bag is unlocked. The - /// caller has unlocked the bag with the user's password. Variant-5 key - /// store only. + /// the designated user bag `user` (outer handle `-(USER_BAG_BASE + uid)`, + /// from `userbag::bag_designation`), protection class 9, so the enclave + /// uses it only while that bag is unlocked. The caller has unlocked the + /// bag with the user's password. Variant-5 key store only. pub(crate) fn fde_key_create(&self, user: crate::sks::DesignateUser) -> Result> { if self.profile.key_store != profile::KeyStore::Variant5 { return Err(EOPNOTSUPP); diff --git a/drivers/soc/apple/userbag.rs b/drivers/soc/apple/userbag.rs index 5b6bdeff833fb..77ce2e7fa6103 100644 --- a/drivers/soc/apple/userbag.rs +++ b/drivers/soc/apple/userbag.rs @@ -923,7 +923,7 @@ pub(crate) fn parse_fde(req: &FdeSeal, seal: bool) -> Result i32::MAX as u32 || record_len == 0 || record_len > FDE_RECORD_MAX { + if req.uid < MIN_UID || record_len == 0 || record_len > FDE_RECORD_MAX { return Err(EINVAL); } if seal && (secret_len == 0 || secret_len > FDE_SECRET_MAX || sealed_len != 0) { @@ -932,7 +932,8 @@ pub(crate) fn parse_fde(req: &FdeSeal, seal: bool) -> Result FDE_SEALED_MAX || secret_len != 0) { return Err(EINVAL); } - crate::sks::DesignateUser::new(req.uid as i32).ok_or(EINVAL) + // The bag SEP_BIO_KEYBAG_UNLOCK opens, not the identity bag at `uid`. + bag_designation(req.uid) } pub(crate) fn is_ioctl(cmd: u32) -> bool { @@ -1359,10 +1360,10 @@ impl crate::SepData { let mut buf = KVec::::with_capacity(core::mem::size_of::(), GFP_KERNEL)?; reader.read_all(&mut buf, GFP_KERNEL)?; let key = FdeKey::from_bytes_mut(&mut buf).ok_or(EINVAL)?; - if key.uid < MIN_UID || key.uid > i32::MAX as u32 { + if key.uid < MIN_UID { return Err(EINVAL); } - let user = crate::sks::DesignateUser::new(key.uid as i32).ok_or(EINVAL)?; + let user = bag_designation(key.uid)?; let record = self.fde_key_create(user)?; if record.len() > FDE_RECORD_MAX { return Err(E2BIG); @@ -1985,9 +1986,20 @@ mod tests { assert!(parse_fde(&fde_request(ok, 0, 32, 0), true).is_err()); assert!(parse_fde(&fde_request(ok, FDE_RECORD_MAX as u32 + 1, 32, 0), true).is_err()); assert!(parse_fde(&fde_request(MIN_UID - 1, 600, 32, 0), true).is_err()); + assert!(parse_fde(&fde_request(MAX_UID + 1, 600, 32, 0), true).is_err()); assert!(parse_fde(&fde_request(u32::MAX, 600, 32, 0), true).is_err()); } + #[test] + fn fde_uses_the_user_bag() { + // The sealing key lives in the bag SEP_BIO_KEYBAG_UNLOCK opens + // (USER_BAG_BASE + uid), not in the identity bag designated at 1000. + let user = parse_fde(&fde_request(1000, 600, 32, 0), true).expect("valid request"); + assert_eq!(user.special_handle().value(), -((USER_BAG_BASE + 1000) as i32)); + let user = parse_fde(&fde_request(MAX_UID, 600, 0, 113), false).expect("valid request"); + assert_eq!(user.special_handle().value(), -(i32::MAX)); + } + #[test] fn fde_ioctls_are_userbag_ioctls() { assert!(is_ioctl(IOC_FDE_CREATE));