Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 1 | // Copyright 2021, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 15 | //! This module implements IKeystoreMaintenance AIDL interface. |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 16 | |
Eric Biggers | b5613da | 2024-03-13 19:31:42 +0000 | [diff] [blame^] | 17 | use crate::database::{KeyEntryLoadBits, KeyType}; |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 18 | use crate::error::map_km_error; |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 19 | use crate::error::map_or_log_err; |
| 20 | use crate::error::Error; |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 21 | use crate::globals::get_keymint_device; |
Janis Danisevskis | 0ffb8a8 | 2022-02-06 22:37:21 -0800 | [diff] [blame] | 22 | use crate::globals::{DB, LEGACY_IMPORTER, SUPER_KEY}; |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 23 | use crate::ks_err; |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 24 | use crate::permission::{KeyPerm, KeystorePerm}; |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 25 | use crate::super_key::{SuperKeyManager, UserState}; |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 26 | use crate::utils::{ |
Eran Messeri | cfe79f1 | 2024-02-05 17:50:41 +0000 | [diff] [blame] | 27 | check_get_app_uids_affected_by_sid_permissions, check_key_permission, |
| 28 | check_keystore_permission, uid_to_android_user, watchdog as wd, |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 29 | }; |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 30 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
| 31 | IKeyMintDevice::IKeyMintDevice, SecurityLevel::SecurityLevel, |
| 32 | }; |
Eric Biggers | 2f9498a | 2023-10-09 23:16:05 +0000 | [diff] [blame] | 33 | use android_security_maintenance::aidl::android::security::maintenance::IKeystoreMaintenance::{ |
| 34 | BnKeystoreMaintenance, IKeystoreMaintenance, |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 35 | }; |
Andrew Walbran | de45c8b | 2021-04-13 14:42:38 +0000 | [diff] [blame] | 36 | use android_security_maintenance::binder::{ |
| 37 | BinderFeatures, Interface, Result as BinderResult, Strong, ThreadState, |
| 38 | }; |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 39 | use android_system_keystore2::aidl::android::system::keystore2::KeyDescriptor::KeyDescriptor; |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 40 | use android_system_keystore2::aidl::android::system::keystore2::ResponseCode::ResponseCode; |
| 41 | use anyhow::{Context, Result}; |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame] | 42 | use keystore2_crypto::Password; |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 43 | |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 44 | /// Reexport Domain for the benefit of DeleteListener |
| 45 | pub use android_system_keystore2::aidl::android::system::keystore2::Domain::Domain; |
| 46 | |
| 47 | /// The Maintenance module takes a delete listener argument which observes user and namespace |
| 48 | /// deletion events. |
| 49 | pub trait DeleteListener { |
| 50 | /// Called by the maintenance module when an app/namespace is deleted. |
| 51 | fn delete_namespace(&self, domain: Domain, namespace: i64) -> Result<()>; |
| 52 | /// Called by the maintenance module when a user is deleted. |
| 53 | fn delete_user(&self, user_id: u32) -> Result<()>; |
| 54 | } |
| 55 | |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 56 | /// This struct is defined to implement the aforementioned AIDL interface. |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 57 | pub struct Maintenance { |
| 58 | delete_listener: Box<dyn DeleteListener + Send + Sync + 'static>, |
| 59 | } |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 60 | |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 61 | impl Maintenance { |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 62 | /// Create a new instance of Keystore Maintenance service. |
| 63 | pub fn new_native_binder( |
| 64 | delete_listener: Box<dyn DeleteListener + Send + Sync + 'static>, |
| 65 | ) -> Result<Strong<dyn IKeystoreMaintenance>> { |
Andrew Walbran | de45c8b | 2021-04-13 14:42:38 +0000 | [diff] [blame] | 66 | Ok(BnKeystoreMaintenance::new_binder( |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 67 | Self { delete_listener }, |
Andrew Walbran | de45c8b | 2021-04-13 14:42:38 +0000 | [diff] [blame] | 68 | BinderFeatures { set_requesting_sid: true, ..BinderFeatures::default() }, |
| 69 | )) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame] | 72 | fn on_user_password_changed(user_id: i32, password: Option<Password>) -> Result<()> { |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 73 | // Check permission. Function should return if this failed. Therefore having '?' at the end |
| 74 | // is very important. |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 75 | check_keystore_permission(KeystorePerm::ChangePassword).context(ks_err!())?; |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 76 | |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 77 | let mut skm = SUPER_KEY.write().unwrap(); |
| 78 | |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 79 | if let Some(pw) = password.as_ref() { |
| 80 | DB.with(|db| { |
Eric Biggers | b1f641d | 2023-10-18 01:54:18 +0000 | [diff] [blame] | 81 | skm.unlock_unlocked_device_required_keys(&mut db.borrow_mut(), user_id as u32, pw) |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 82 | }) |
Eric Biggers | b1f641d | 2023-10-18 01:54:18 +0000 | [diff] [blame] | 83 | .context(ks_err!("unlock_unlocked_device_required_keys failed"))?; |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Eric Biggers | 1386937 | 2023-10-18 01:54:18 +0000 | [diff] [blame] | 86 | if let UserState::BeforeFirstUnlock = DB |
Nathan Huckleberry | 204a044 | 2023-03-30 17:27:47 +0000 | [diff] [blame] | 87 | .with(|db| skm.get_user_state(&mut db.borrow_mut(), &LEGACY_IMPORTER, user_id as u32)) |
| 88 | .context(ks_err!("Could not get user state while changing password!"))? |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 89 | { |
Nathan Huckleberry | 204a044 | 2023-03-30 17:27:47 +0000 | [diff] [blame] | 90 | // Error - password can not be changed when the device is locked |
| 91 | return Err(Error::Rc(ResponseCode::LOCKED)).context(ks_err!("Device is locked.")); |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 92 | } |
Nathan Huckleberry | 204a044 | 2023-03-30 17:27:47 +0000 | [diff] [blame] | 93 | |
| 94 | DB.with(|db| match password { |
| 95 | Some(pass) => { |
| 96 | skm.init_user(&mut db.borrow_mut(), &LEGACY_IMPORTER, user_id as u32, &pass) |
| 97 | } |
| 98 | None => { |
| 99 | // User transitioned to swipe. |
| 100 | skm.reset_user(&mut db.borrow_mut(), &LEGACY_IMPORTER, user_id as u32) |
| 101 | } |
| 102 | }) |
| 103 | .context(ks_err!("Failed to change user password!")) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 106 | fn add_or_remove_user(&self, user_id: i32) -> Result<()> { |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 107 | // Check permission. Function should return if this failed. Therefore having '?' at the end |
| 108 | // is very important. |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 109 | check_keystore_permission(KeystorePerm::ChangeUser).context(ks_err!())?; |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 110 | |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 111 | DB.with(|db| { |
Nathan Huckleberry | 204a044 | 2023-03-30 17:27:47 +0000 | [diff] [blame] | 112 | SUPER_KEY.write().unwrap().remove_user( |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 113 | &mut db.borrow_mut(), |
Janis Danisevskis | 0ffb8a8 | 2022-02-06 22:37:21 -0800 | [diff] [blame] | 114 | &LEGACY_IMPORTER, |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 115 | user_id as u32, |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 116 | ) |
| 117 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 118 | .context(ks_err!("Trying to delete keys from db."))?; |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 119 | self.delete_listener |
| 120 | .delete_user(user_id as u32) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 121 | .context(ks_err!("While invoking the delete listener.")) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 122 | } |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 123 | |
Eric Biggers | b0478cf | 2023-10-27 03:55:29 +0000 | [diff] [blame] | 124 | fn init_user_super_keys( |
| 125 | &self, |
| 126 | user_id: i32, |
| 127 | password: Password, |
| 128 | allow_existing: bool, |
| 129 | ) -> Result<()> { |
| 130 | // Permission check. Must return on error. Do not touch the '?'. |
| 131 | check_keystore_permission(KeystorePerm::ChangeUser).context(ks_err!())?; |
| 132 | |
| 133 | let mut skm = SUPER_KEY.write().unwrap(); |
| 134 | DB.with(|db| { |
| 135 | skm.initialize_user( |
| 136 | &mut db.borrow_mut(), |
| 137 | &LEGACY_IMPORTER, |
| 138 | user_id as u32, |
| 139 | &password, |
| 140 | allow_existing, |
| 141 | ) |
| 142 | }) |
| 143 | .context(ks_err!("Failed to initialize user super keys")) |
| 144 | } |
| 145 | |
| 146 | // Deletes all auth-bound keys when the user's LSKF is removed. |
| 147 | fn on_user_lskf_removed(user_id: i32) -> Result<()> { |
| 148 | // Permission check. Must return on error. Do not touch the '?'. |
| 149 | check_keystore_permission(KeystorePerm::ChangePassword).context(ks_err!())?; |
| 150 | |
| 151 | LEGACY_IMPORTER |
| 152 | .bulk_delete_user(user_id as u32, true) |
| 153 | .context(ks_err!("Failed to delete legacy keys."))?; |
| 154 | |
| 155 | DB.with(|db| db.borrow_mut().unbind_auth_bound_keys_for_user(user_id as u32)) |
| 156 | .context(ks_err!("Failed to delete auth-bound keys.")) |
| 157 | } |
| 158 | |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 159 | fn clear_namespace(&self, domain: Domain, nspace: i64) -> Result<()> { |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 160 | // Permission check. Must return on error. Do not touch the '?'. |
Janis Danisevskis | a916d99 | 2021-10-19 15:46:09 -0700 | [diff] [blame] | 161 | check_keystore_permission(KeystorePerm::ClearUID).context("In clear_namespace.")?; |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 162 | |
Janis Danisevskis | 0ffb8a8 | 2022-02-06 22:37:21 -0800 | [diff] [blame] | 163 | LEGACY_IMPORTER |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 164 | .bulk_delete_uid(domain, nspace) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 165 | .context(ks_err!("Trying to delete legacy keys."))?; |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 166 | DB.with(|db| db.borrow_mut().unbind_keys_for_namespace(domain, nspace)) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 167 | .context(ks_err!("Trying to delete keys from db."))?; |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 168 | self.delete_listener |
| 169 | .delete_namespace(domain, nspace) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 170 | .context(ks_err!("While invoking the delete listener.")) |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 171 | } |
Hasini Gunasinghe | 9ee1841 | 2021-03-11 20:12:44 +0000 | [diff] [blame] | 172 | |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 173 | fn call_with_watchdog<F>(sec_level: SecurityLevel, name: &'static str, op: &F) -> Result<()> |
| 174 | where |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 175 | F: Fn(Strong<dyn IKeyMintDevice>) -> binder::Result<()>, |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 176 | { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 177 | let (km_dev, _, _) = |
| 178 | get_keymint_device(&sec_level).context(ks_err!("getting keymint device"))?; |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 179 | |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 180 | let _wp = wd::watch_millis_with("In call_with_watchdog", 500, move || { |
| 181 | format!("Seclevel: {:?} Op: {}", sec_level, name) |
| 182 | }); |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 183 | map_km_error(op(km_dev)).with_context(|| ks_err!("calling {}", name))?; |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 184 | Ok(()) |
| 185 | } |
| 186 | |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 187 | fn call_on_all_security_levels<F>(name: &'static str, op: F) -> Result<()> |
| 188 | where |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 189 | F: Fn(Strong<dyn IKeyMintDevice>) -> binder::Result<()>, |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 190 | { |
| 191 | let sec_levels = [ |
| 192 | (SecurityLevel::TRUSTED_ENVIRONMENT, "TRUSTED_ENVIRONMENT"), |
| 193 | (SecurityLevel::STRONGBOX, "STRONGBOX"), |
| 194 | ]; |
James Farrell | d77b97f | 2023-08-15 20:03:38 +0000 | [diff] [blame] | 195 | sec_levels.iter().try_fold((), |_result, (sec_level, sec_level_string)| { |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 196 | let curr_result = Maintenance::call_with_watchdog(*sec_level, name, &op); |
| 197 | match curr_result { |
| 198 | Ok(()) => log::info!( |
| 199 | "Call to {} succeeded for security level {}.", |
| 200 | name, |
| 201 | &sec_level_string |
| 202 | ), |
| 203 | Err(ref e) => log::error!( |
| 204 | "Call to {} failed for security level {}: {}.", |
| 205 | name, |
| 206 | &sec_level_string, |
| 207 | e |
| 208 | ), |
| 209 | } |
James Farrell | d77b97f | 2023-08-15 20:03:38 +0000 | [diff] [blame] | 210 | curr_result |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 211 | }) |
| 212 | } |
| 213 | |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 214 | fn early_boot_ended() -> Result<()> { |
Janis Danisevskis | a916d99 | 2021-10-19 15:46:09 -0700 | [diff] [blame] | 215 | check_keystore_permission(KeystorePerm::EarlyBootEnded) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 216 | .context(ks_err!("Checking permission"))?; |
Paul Crowley | 44c02da | 2021-04-08 17:04:43 +0000 | [diff] [blame] | 217 | log::info!("In early_boot_ended."); |
| 218 | |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 219 | if let Err(e) = |
| 220 | DB.with(|db| SuperKeyManager::set_up_boot_level_cache(&SUPER_KEY, &mut db.borrow_mut())) |
| 221 | { |
Paul Crowley | 44c02da | 2021-04-08 17:04:43 +0000 | [diff] [blame] | 222 | log::error!("SUPER_KEY.set_up_boot_level_cache failed:\n{:?}\n:(", e); |
| 223 | } |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 224 | Maintenance::call_on_all_security_levels("earlyBootEnded", |dev| dev.earlyBootEnded()) |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 227 | fn migrate_key_namespace(source: &KeyDescriptor, destination: &KeyDescriptor) -> Result<()> { |
John Wu | 889c1cc | 2022-03-14 16:02:56 -0700 | [diff] [blame] | 228 | let calling_uid = ThreadState::get_calling_uid(); |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 229 | |
John Wu | 889c1cc | 2022-03-14 16:02:56 -0700 | [diff] [blame] | 230 | match source.domain { |
| 231 | Domain::SELINUX | Domain::KEY_ID | Domain::APP => (), |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 232 | _ => { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 233 | return Err(Error::Rc(ResponseCode::INVALID_ARGUMENT)) |
| 234 | .context(ks_err!("Source domain must be one of APP, SELINUX, or KEY_ID.")); |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 235 | } |
| 236 | }; |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 237 | |
John Wu | 889c1cc | 2022-03-14 16:02:56 -0700 | [diff] [blame] | 238 | match destination.domain { |
| 239 | Domain::SELINUX | Domain::APP => (), |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 240 | _ => { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 241 | return Err(Error::Rc(ResponseCode::INVALID_ARGUMENT)) |
| 242 | .context(ks_err!("Destination domain must be one of APP or SELINUX.")); |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 243 | } |
| 244 | }; |
| 245 | |
John Wu | 889c1cc | 2022-03-14 16:02:56 -0700 | [diff] [blame] | 246 | let user_id = uid_to_android_user(calling_uid); |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 247 | |
Eric Biggers | 673d34a | 2023-10-18 01:54:18 +0000 | [diff] [blame] | 248 | let super_key = SUPER_KEY.read().unwrap().get_after_first_unlock_key_by_user_id(user_id); |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 249 | |
| 250 | DB.with(|db| { |
John Wu | 889c1cc | 2022-03-14 16:02:56 -0700 | [diff] [blame] | 251 | let (key_id_guard, _) = LEGACY_IMPORTER |
| 252 | .with_try_import(source, calling_uid, super_key, || { |
| 253 | db.borrow_mut().load_key_entry( |
| 254 | source, |
| 255 | KeyType::Client, |
| 256 | KeyEntryLoadBits::NONE, |
| 257 | calling_uid, |
| 258 | |k, av| { |
| 259 | check_key_permission(KeyPerm::Use, k, &av)?; |
| 260 | check_key_permission(KeyPerm::Delete, k, &av)?; |
| 261 | check_key_permission(KeyPerm::Grant, k, &av) |
| 262 | }, |
| 263 | ) |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 264 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 265 | .context(ks_err!("Failed to load key blob."))?; |
John Wu | 889c1cc | 2022-03-14 16:02:56 -0700 | [diff] [blame] | 266 | { |
| 267 | db.borrow_mut().migrate_key_namespace(key_id_guard, destination, calling_uid, |k| { |
| 268 | check_key_permission(KeyPerm::Rebind, k, &None) |
| 269 | }) |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 270 | } |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 271 | }) |
| 272 | } |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 273 | |
| 274 | fn delete_all_keys() -> Result<()> { |
| 275 | // Security critical permission check. This statement must return on fail. |
Janis Danisevskis | a916d99 | 2021-10-19 15:46:09 -0700 | [diff] [blame] | 276 | check_keystore_permission(KeystorePerm::DeleteAllKeys) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 277 | .context(ks_err!("Checking permission"))?; |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 278 | log::info!("In delete_all_keys."); |
| 279 | |
| 280 | Maintenance::call_on_all_security_levels("deleteAllKeys", |dev| dev.deleteAllKeys()) |
| 281 | } |
Eran Messeri | 4dc27b5 | 2024-01-09 12:43:31 +0000 | [diff] [blame] | 282 | |
| 283 | fn get_app_uids_affected_by_sid( |
| 284 | user_id: i32, |
| 285 | secure_user_id: i64, |
| 286 | ) -> Result<std::vec::Vec<i64>> { |
| 287 | // This method is intended to be called by Settings and discloses a list of apps |
Eran Messeri | cfe79f1 | 2024-02-05 17:50:41 +0000 | [diff] [blame] | 288 | // associated with a user, so it requires the "android.permission.MANAGE_USERS" |
| 289 | // permission (to avoid leaking list of apps to unauthorized callers). |
| 290 | check_get_app_uids_affected_by_sid_permissions().context(ks_err!())?; |
Eran Messeri | 4dc27b5 | 2024-01-09 12:43:31 +0000 | [diff] [blame] | 291 | DB.with(|db| db.borrow_mut().get_app_uids_affected_by_sid(user_id, secure_user_id)) |
| 292 | .context(ks_err!("Failed to get app UIDs affected by SID")) |
| 293 | } |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 296 | impl Interface for Maintenance {} |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 297 | |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 298 | impl IKeystoreMaintenance for Maintenance { |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 299 | fn onUserPasswordChanged(&self, user_id: i32, password: Option<&[u8]>) -> BinderResult<()> { |
David Drysdale | e85523f | 2023-06-19 12:28:53 +0100 | [diff] [blame] | 300 | log::info!( |
| 301 | "onUserPasswordChanged(user={}, password.is_some()={})", |
| 302 | user_id, |
| 303 | password.is_some() |
| 304 | ); |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 305 | let _wp = wd::watch_millis("IKeystoreMaintenance::onUserPasswordChanged", 500); |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame] | 306 | map_or_log_err(Self::on_user_password_changed(user_id, password.map(|pw| pw.into())), Ok) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | fn onUserAdded(&self, user_id: i32) -> BinderResult<()> { |
David Drysdale | e85523f | 2023-06-19 12:28:53 +0100 | [diff] [blame] | 310 | log::info!("onUserAdded(user={user_id})"); |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 311 | let _wp = wd::watch_millis("IKeystoreMaintenance::onUserAdded", 500); |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 312 | map_or_log_err(self.add_or_remove_user(user_id), Ok) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Eric Biggers | b0478cf | 2023-10-27 03:55:29 +0000 | [diff] [blame] | 315 | fn initUserSuperKeys( |
| 316 | &self, |
| 317 | user_id: i32, |
| 318 | password: &[u8], |
| 319 | allow_existing: bool, |
| 320 | ) -> BinderResult<()> { |
| 321 | log::info!("initUserSuperKeys(user={user_id}, allow_existing={allow_existing})"); |
| 322 | let _wp = wd::watch_millis("IKeystoreMaintenance::initUserSuperKeys", 500); |
| 323 | map_or_log_err(self.init_user_super_keys(user_id, password.into(), allow_existing), Ok) |
| 324 | } |
| 325 | |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 326 | fn onUserRemoved(&self, user_id: i32) -> BinderResult<()> { |
David Drysdale | e85523f | 2023-06-19 12:28:53 +0100 | [diff] [blame] | 327 | log::info!("onUserRemoved(user={user_id})"); |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 328 | let _wp = wd::watch_millis("IKeystoreMaintenance::onUserRemoved", 500); |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 329 | map_or_log_err(self.add_or_remove_user(user_id), Ok) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 330 | } |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 331 | |
Eric Biggers | b0478cf | 2023-10-27 03:55:29 +0000 | [diff] [blame] | 332 | fn onUserLskfRemoved(&self, user_id: i32) -> BinderResult<()> { |
| 333 | log::info!("onUserLskfRemoved(user={user_id})"); |
| 334 | let _wp = wd::watch_millis("IKeystoreMaintenance::onUserLskfRemoved", 500); |
| 335 | map_or_log_err(Self::on_user_lskf_removed(user_id), Ok) |
| 336 | } |
| 337 | |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 338 | fn clearNamespace(&self, domain: Domain, nspace: i64) -> BinderResult<()> { |
David Drysdale | e85523f | 2023-06-19 12:28:53 +0100 | [diff] [blame] | 339 | log::info!("clearNamespace({domain:?}, nspace={nspace})"); |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 340 | let _wp = wd::watch_millis("IKeystoreMaintenance::clearNamespace", 500); |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 341 | map_or_log_err(self.clear_namespace(domain, nspace), Ok) |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 342 | } |
Hasini Gunasinghe | 9ee1841 | 2021-03-11 20:12:44 +0000 | [diff] [blame] | 343 | |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 344 | fn earlyBootEnded(&self) -> BinderResult<()> { |
David Drysdale | e85523f | 2023-06-19 12:28:53 +0100 | [diff] [blame] | 345 | log::info!("earlyBootEnded()"); |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 346 | let _wp = wd::watch_millis("IKeystoreMaintenance::earlyBootEnded", 500); |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 347 | map_or_log_err(Self::early_boot_ended(), Ok) |
| 348 | } |
| 349 | |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 350 | fn migrateKeyNamespace( |
| 351 | &self, |
| 352 | source: &KeyDescriptor, |
| 353 | destination: &KeyDescriptor, |
| 354 | ) -> BinderResult<()> { |
David Drysdale | e85523f | 2023-06-19 12:28:53 +0100 | [diff] [blame] | 355 | log::info!("migrateKeyNamespace(src={source:?}, dest={destination:?})"); |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 356 | let _wp = wd::watch_millis("IKeystoreMaintenance::migrateKeyNamespace", 500); |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 357 | map_or_log_err(Self::migrate_key_namespace(source, destination), Ok) |
| 358 | } |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 359 | |
| 360 | fn deleteAllKeys(&self) -> BinderResult<()> { |
David Drysdale | e85523f | 2023-06-19 12:28:53 +0100 | [diff] [blame] | 361 | log::warn!("deleteAllKeys()"); |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 362 | let _wp = wd::watch_millis("IKeystoreMaintenance::deleteAllKeys", 500); |
| 363 | map_or_log_err(Self::delete_all_keys(), Ok) |
| 364 | } |
Eran Messeri | 4dc27b5 | 2024-01-09 12:43:31 +0000 | [diff] [blame] | 365 | |
| 366 | fn getAppUidsAffectedBySid( |
| 367 | &self, |
| 368 | user_id: i32, |
| 369 | secure_user_id: i64, |
| 370 | ) -> BinderResult<std::vec::Vec<i64>> { |
| 371 | log::info!("getAppUidsAffectedBySid(secure_user_id={secure_user_id:?})"); |
| 372 | let _wp = wd::watch_millis("IKeystoreMaintenance::getAppUidsAffectedBySid", 500); |
| 373 | map_or_log_err(Self::get_app_uids_affected_by_sid(user_id, secure_user_id), Ok) |
| 374 | } |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 375 | } |