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