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}; |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 23 | use crate::permission::{KeyPerm, KeystorePerm}; |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 24 | use crate::super_key::{SuperKeyManager, UserState}; |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 25 | use crate::utils::{ |
| 26 | check_key_permission, check_keystore_permission, list_key_entries, watchdog as wd, |
| 27 | }; |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 28 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
| 29 | IKeyMintDevice::IKeyMintDevice, SecurityLevel::SecurityLevel, |
| 30 | }; |
Hasini Gunasinghe | 9ee1841 | 2021-03-11 20:12:44 +0000 | [diff] [blame] | 31 | use android_security_maintenance::aidl::android::security::maintenance::{ |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 32 | IKeystoreMaintenance::{BnKeystoreMaintenance, IKeystoreMaintenance, UID_SELF}, |
Hasini Gunasinghe | 9ee1841 | 2021-03-11 20:12:44 +0000 | [diff] [blame] | 33 | UserState::UserState as AidlUserState, |
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; |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 42 | use keystore2_selinux as selinux; |
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. |
Janis Danisevskis | a916d99 | 2021-10-19 15:46:09 -0700 | [diff] [blame] | 75 | check_keystore_permission(KeystorePerm::ChangePassword) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 76 | .context("In on_user_password_changed.")?; |
| 77 | |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 78 | let mut skm = SUPER_KEY.write().unwrap(); |
| 79 | |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 80 | if let Some(pw) = password.as_ref() { |
| 81 | DB.with(|db| { |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 82 | skm.unlock_screen_lock_bound_key(&mut db.borrow_mut(), user_id as u32, pw) |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 83 | }) |
Paul Crowley | 8d5b253 | 2021-03-19 10:53:07 -0700 | [diff] [blame] | 84 | .context("In on_user_password_changed: unlock_screen_lock_bound_key failed")?; |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 87 | match DB |
| 88 | .with(|db| { |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 89 | skm.reset_or_init_user_and_get_user_state( |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 90 | &mut db.borrow_mut(), |
Janis Danisevskis | 0ffb8a8 | 2022-02-06 22:37:21 -0800 | [diff] [blame] | 91 | &LEGACY_IMPORTER, |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 92 | user_id as u32, |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame] | 93 | password.as_ref(), |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 94 | ) |
| 95 | }) |
| 96 | .context("In on_user_password_changed.")? |
| 97 | { |
| 98 | UserState::LskfLocked => { |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 99 | // Error - password can not be changed when the device is locked |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 100 | Err(Error::Rc(ResponseCode::LOCKED)) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 101 | .context("In on_user_password_changed. Device is locked.") |
| 102 | } |
| 103 | _ => { |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 104 | // LskfLocked is the only error case for password change |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 105 | Ok(()) |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 110 | fn add_or_remove_user(&self, user_id: i32) -> Result<()> { |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 111 | // Check permission. Function should return if this failed. Therefore having '?' at the end |
| 112 | // is very important. |
Janis Danisevskis | a916d99 | 2021-10-19 15:46:09 -0700 | [diff] [blame] | 113 | check_keystore_permission(KeystorePerm::ChangeUser).context("In add_or_remove_user.")?; |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 114 | |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 115 | DB.with(|db| { |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 116 | SUPER_KEY.write().unwrap().reset_user( |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 117 | &mut db.borrow_mut(), |
Janis Danisevskis | 0ffb8a8 | 2022-02-06 22:37:21 -0800 | [diff] [blame] | 118 | &LEGACY_IMPORTER, |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 119 | user_id as u32, |
| 120 | false, |
| 121 | ) |
| 122 | }) |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 123 | .context("In add_or_remove_user: Trying to delete keys from db.")?; |
| 124 | self.delete_listener |
| 125 | .delete_user(user_id as u32) |
| 126 | .context("In add_or_remove_user: While invoking the delete listener.") |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 127 | } |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 128 | |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 129 | fn clear_namespace(&self, domain: Domain, nspace: i64) -> Result<()> { |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 130 | // Permission check. Must return on error. Do not touch the '?'. |
Janis Danisevskis | a916d99 | 2021-10-19 15:46:09 -0700 | [diff] [blame] | 131 | check_keystore_permission(KeystorePerm::ClearUID).context("In clear_namespace.")?; |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 132 | |
Janis Danisevskis | 0ffb8a8 | 2022-02-06 22:37:21 -0800 | [diff] [blame] | 133 | LEGACY_IMPORTER |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 134 | .bulk_delete_uid(domain, nspace) |
| 135 | .context("In clear_namespace: Trying to delete legacy keys.")?; |
| 136 | DB.with(|db| db.borrow_mut().unbind_keys_for_namespace(domain, nspace)) |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 137 | .context("In clear_namespace: Trying to delete keys from db.")?; |
| 138 | self.delete_listener |
| 139 | .delete_namespace(domain, nspace) |
| 140 | .context("In clear_namespace: While invoking the delete listener.") |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 141 | } |
Hasini Gunasinghe | 9ee1841 | 2021-03-11 20:12:44 +0000 | [diff] [blame] | 142 | |
| 143 | fn get_state(user_id: i32) -> Result<AidlUserState> { |
| 144 | // Check permission. Function should return if this failed. Therefore having '?' at the end |
| 145 | // is very important. |
Janis Danisevskis | a916d99 | 2021-10-19 15:46:09 -0700 | [diff] [blame] | 146 | check_keystore_permission(KeystorePerm::GetState).context("In get_state.")?; |
Hasini Gunasinghe | 9ee1841 | 2021-03-11 20:12:44 +0000 | [diff] [blame] | 147 | let state = DB |
| 148 | .with(|db| { |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 149 | SUPER_KEY.read().unwrap().get_user_state( |
| 150 | &mut db.borrow_mut(), |
Janis Danisevskis | 0ffb8a8 | 2022-02-06 22:37:21 -0800 | [diff] [blame] | 151 | &LEGACY_IMPORTER, |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 152 | user_id as u32, |
| 153 | ) |
Hasini Gunasinghe | 9ee1841 | 2021-03-11 20:12:44 +0000 | [diff] [blame] | 154 | }) |
| 155 | .context("In get_state. Trying to get UserState.")?; |
| 156 | |
| 157 | match state { |
| 158 | UserState::Uninitialized => Ok(AidlUserState::UNINITIALIZED), |
| 159 | UserState::LskfUnlocked(_) => Ok(AidlUserState::LSKF_UNLOCKED), |
| 160 | UserState::LskfLocked => Ok(AidlUserState::LSKF_LOCKED), |
| 161 | } |
| 162 | } |
Janis Danisevskis | 333b7c0 | 2021-03-23 18:57:41 -0700 | [diff] [blame] | 163 | |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 164 | fn call_with_watchdog<F>(sec_level: SecurityLevel, name: &'static str, op: &F) -> Result<()> |
| 165 | where |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 166 | F: Fn(Strong<dyn IKeyMintDevice>) -> binder::Result<()>, |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 167 | { |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame] | 168 | let (km_dev, _, _) = get_keymint_device(&sec_level) |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 169 | .context("In call_with_watchdog: getting keymint device")?; |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 170 | |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 171 | let _wp = wd::watch_millis_with("In call_with_watchdog", 500, move || { |
| 172 | format!("Seclevel: {:?} Op: {}", sec_level, name) |
| 173 | }); |
| 174 | map_km_error(op(km_dev)).with_context(|| format!("In keymint device: calling {}", name))?; |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 175 | Ok(()) |
| 176 | } |
| 177 | |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 178 | fn call_on_all_security_levels<F>(name: &'static str, op: F) -> Result<()> |
| 179 | where |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 180 | F: Fn(Strong<dyn IKeyMintDevice>) -> binder::Result<()>, |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 181 | { |
| 182 | let sec_levels = [ |
| 183 | (SecurityLevel::TRUSTED_ENVIRONMENT, "TRUSTED_ENVIRONMENT"), |
| 184 | (SecurityLevel::STRONGBOX, "STRONGBOX"), |
| 185 | ]; |
| 186 | sec_levels.iter().fold(Ok(()), move |result, (sec_level, sec_level_string)| { |
| 187 | let curr_result = Maintenance::call_with_watchdog(*sec_level, name, &op); |
| 188 | match curr_result { |
| 189 | Ok(()) => log::info!( |
| 190 | "Call to {} succeeded for security level {}.", |
| 191 | name, |
| 192 | &sec_level_string |
| 193 | ), |
| 194 | Err(ref e) => log::error!( |
| 195 | "Call to {} failed for security level {}: {}.", |
| 196 | name, |
| 197 | &sec_level_string, |
| 198 | e |
| 199 | ), |
| 200 | } |
| 201 | result.and(curr_result) |
| 202 | }) |
| 203 | } |
| 204 | |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 205 | fn early_boot_ended() -> Result<()> { |
Janis Danisevskis | a916d99 | 2021-10-19 15:46:09 -0700 | [diff] [blame] | 206 | check_keystore_permission(KeystorePerm::EarlyBootEnded) |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 207 | .context("In early_boot_ended. Checking permission")?; |
Paul Crowley | 44c02da | 2021-04-08 17:04:43 +0000 | [diff] [blame] | 208 | log::info!("In early_boot_ended."); |
| 209 | |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 210 | if let Err(e) = |
| 211 | DB.with(|db| SuperKeyManager::set_up_boot_level_cache(&SUPER_KEY, &mut db.borrow_mut())) |
| 212 | { |
Paul Crowley | 44c02da | 2021-04-08 17:04:43 +0000 | [diff] [blame] | 213 | log::error!("SUPER_KEY.set_up_boot_level_cache failed:\n{:?}\n:(", e); |
| 214 | } |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 215 | Maintenance::call_on_all_security_levels("earlyBootEnded", |dev| dev.earlyBootEnded()) |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Janis Danisevskis | 333b7c0 | 2021-03-23 18:57:41 -0700 | [diff] [blame] | 218 | fn on_device_off_body() -> Result<()> { |
| 219 | // Security critical permission check. This statement must return on fail. |
Janis Danisevskis | a916d99 | 2021-10-19 15:46:09 -0700 | [diff] [blame] | 220 | check_keystore_permission(KeystorePerm::ReportOffBody).context("In on_device_off_body.")?; |
Janis Danisevskis | 333b7c0 | 2021-03-23 18:57:41 -0700 | [diff] [blame] | 221 | |
Matthew Maurer | d7815ca | 2021-05-06 21:58:45 -0700 | [diff] [blame] | 222 | DB.with(|db| db.borrow_mut().update_last_off_body(MonotonicRawTime::now())); |
| 223 | Ok(()) |
Janis Danisevskis | 333b7c0 | 2021-03-23 18:57:41 -0700 | [diff] [blame] | 224 | } |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 225 | |
| 226 | fn migrate_key_namespace(source: &KeyDescriptor, destination: &KeyDescriptor) -> Result<()> { |
Ashwini Oruganti | daf73bc | 2021-11-15 10:46:31 -0800 | [diff] [blame] | 227 | let migrate_any_key_permission = |
| 228 | check_keystore_permission(KeystorePerm::MigrateAnyKey).is_ok(); |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 229 | |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 230 | let src_uid = match source.domain { |
| 231 | Domain::SELINUX | Domain::KEY_ID => ThreadState::get_calling_uid(), |
| 232 | Domain::APP if source.nspace == UID_SELF.into() => ThreadState::get_calling_uid(), |
| 233 | Domain::APP if source.nspace != UID_SELF.into() && migrate_any_key_permission => { |
| 234 | source.nspace as u32 |
| 235 | } |
| 236 | _ => { |
| 237 | return Err(Error::Rc(ResponseCode::INVALID_ARGUMENT)).context( |
Janis Danisevskis | 478a9a2 | 2022-01-28 08:22:59 -0800 | [diff] [blame] | 238 | "In migrate_key_namespace: \ |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 239 | Source domain must be one of APP, SELINUX, or KEY_ID.", |
| 240 | ) |
| 241 | } |
| 242 | }; |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 243 | |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 244 | let dest_uid = match destination.domain { |
| 245 | Domain::SELINUX => ThreadState::get_calling_uid(), |
| 246 | Domain::APP if destination.nspace == UID_SELF.into() => ThreadState::get_calling_uid(), |
| 247 | Domain::APP if destination.nspace != UID_SELF.into() && migrate_any_key_permission => { |
| 248 | destination.nspace as u32 |
| 249 | } |
| 250 | _ => { |
| 251 | return Err(Error::Rc(ResponseCode::INVALID_ARGUMENT)).context( |
Janis Danisevskis | 478a9a2 | 2022-01-28 08:22:59 -0800 | [diff] [blame] | 252 | "In migrate_key_namespace: \ |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 253 | Destination domain must be one of APP or SELINUX.", |
| 254 | ) |
| 255 | } |
| 256 | }; |
| 257 | |
| 258 | DB.with(|db| { |
Janis Danisevskis | 0ffb8a8 | 2022-02-06 22:37:21 -0800 | [diff] [blame] | 259 | let (key_id_guard, _) = LEGACY_IMPORTER |
| 260 | .with_try_import(source, src_uid, || { |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 261 | db.borrow_mut().load_key_entry( |
| 262 | source, |
| 263 | KeyType::Client, |
| 264 | KeyEntryLoadBits::NONE, |
| 265 | src_uid, |
| 266 | |k, av| { |
| 267 | if migrate_any_key_permission { |
| 268 | Ok(()) |
| 269 | } else { |
| 270 | check_key_permission(KeyPerm::Use, k, &av)?; |
| 271 | check_key_permission(KeyPerm::Delete, k, &av)?; |
| 272 | check_key_permission(KeyPerm::Grant, k, &av) |
| 273 | } |
| 274 | }, |
| 275 | ) |
| 276 | }) |
| 277 | .context("In migrate_key_namespace: Failed to load key blob.")?; |
| 278 | |
| 279 | db.borrow_mut().migrate_key_namespace(key_id_guard, destination, dest_uid, |k| { |
| 280 | if migrate_any_key_permission { |
| 281 | Ok(()) |
| 282 | } else { |
| 283 | check_key_permission(KeyPerm::Rebind, k, &None) |
Ashwini Oruganti | daf73bc | 2021-11-15 10:46:31 -0800 | [diff] [blame] | 284 | } |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 285 | }) |
| 286 | }) |
| 287 | } |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 288 | |
| 289 | fn delete_all_keys() -> Result<()> { |
| 290 | // Security critical permission check. This statement must return on fail. |
Janis Danisevskis | a916d99 | 2021-10-19 15:46:09 -0700 | [diff] [blame] | 291 | check_keystore_permission(KeystorePerm::DeleteAllKeys) |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 292 | .context("In delete_all_keys. Checking permission")?; |
| 293 | log::info!("In delete_all_keys."); |
| 294 | |
| 295 | Maintenance::call_on_all_security_levels("deleteAllKeys", |dev| dev.deleteAllKeys()) |
| 296 | } |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 297 | |
| 298 | fn list_entries(domain: Domain, nspace: i64) -> Result<Vec<KeyDescriptor>> { |
| 299 | let k = match domain { |
| 300 | Domain::APP | Domain::SELINUX => KeyDescriptor{domain, nspace, ..Default::default()}, |
| 301 | _ => return Err(Error::perm()).context( |
| 302 | "In list_entries: List entries is only supported for Domain::APP and Domain::SELINUX." |
| 303 | ), |
| 304 | }; |
| 305 | |
| 306 | // The caller has to have either GetInfo for the namespace or List permission |
| 307 | check_key_permission(KeyPerm::GetInfo, &k, &None) |
| 308 | .or_else(|e| { |
| 309 | if Some(&selinux::Error::PermissionDenied) |
| 310 | == e.root_cause().downcast_ref::<selinux::Error>() |
| 311 | { |
| 312 | check_keystore_permission(KeystorePerm::List) |
| 313 | } else { |
| 314 | Err(e) |
| 315 | } |
| 316 | }) |
| 317 | .context("In list_entries: While checking key and keystore permission.")?; |
| 318 | |
| 319 | DB.with(|db| list_key_entries(&mut db.borrow_mut(), domain, nspace)) |
| 320 | } |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 323 | impl Interface for Maintenance {} |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 324 | |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 325 | impl IKeystoreMaintenance for Maintenance { |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 326 | fn onUserPasswordChanged(&self, user_id: i32, password: Option<&[u8]>) -> BinderResult<()> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 327 | let _wp = wd::watch_millis("IKeystoreMaintenance::onUserPasswordChanged", 500); |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame] | 328 | 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] | 329 | } |
| 330 | |
| 331 | fn onUserAdded(&self, user_id: i32) -> BinderResult<()> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 332 | let _wp = wd::watch_millis("IKeystoreMaintenance::onUserAdded", 500); |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 333 | map_or_log_err(self.add_or_remove_user(user_id), Ok) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | fn onUserRemoved(&self, user_id: i32) -> BinderResult<()> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 337 | let _wp = wd::watch_millis("IKeystoreMaintenance::onUserRemoved", 500); |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 338 | map_or_log_err(self.add_or_remove_user(user_id), Ok) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 339 | } |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 340 | |
| 341 | fn clearNamespace(&self, domain: Domain, nspace: i64) -> BinderResult<()> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 342 | let _wp = wd::watch_millis("IKeystoreMaintenance::clearNamespace", 500); |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 343 | map_or_log_err(self.clear_namespace(domain, nspace), Ok) |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 344 | } |
Hasini Gunasinghe | 9ee1841 | 2021-03-11 20:12:44 +0000 | [diff] [blame] | 345 | |
Janis Danisevskis | 333b7c0 | 2021-03-23 18:57:41 -0700 | [diff] [blame] | 346 | fn getState(&self, user_id: i32) -> BinderResult<AidlUserState> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 347 | let _wp = wd::watch_millis("IKeystoreMaintenance::getState", 500); |
Hasini Gunasinghe | 9ee1841 | 2021-03-11 20:12:44 +0000 | [diff] [blame] | 348 | map_or_log_err(Self::get_state(user_id), Ok) |
| 349 | } |
Janis Danisevskis | 333b7c0 | 2021-03-23 18:57:41 -0700 | [diff] [blame] | 350 | |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 351 | fn earlyBootEnded(&self) -> BinderResult<()> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 352 | let _wp = wd::watch_millis("IKeystoreMaintenance::earlyBootEnded", 500); |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 353 | map_or_log_err(Self::early_boot_ended(), Ok) |
| 354 | } |
| 355 | |
Janis Danisevskis | 333b7c0 | 2021-03-23 18:57:41 -0700 | [diff] [blame] | 356 | fn onDeviceOffBody(&self) -> BinderResult<()> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 357 | let _wp = wd::watch_millis("IKeystoreMaintenance::onDeviceOffBody", 500); |
Janis Danisevskis | 333b7c0 | 2021-03-23 18:57:41 -0700 | [diff] [blame] | 358 | map_or_log_err(Self::on_device_off_body(), Ok) |
| 359 | } |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 360 | |
| 361 | fn migrateKeyNamespace( |
| 362 | &self, |
| 363 | source: &KeyDescriptor, |
| 364 | destination: &KeyDescriptor, |
| 365 | ) -> BinderResult<()> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 366 | let _wp = wd::watch_millis("IKeystoreMaintenance::migrateKeyNamespace", 500); |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 367 | map_or_log_err(Self::migrate_key_namespace(source, destination), Ok) |
| 368 | } |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 369 | |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 370 | fn listEntries(&self, domain: Domain, namespace: i64) -> BinderResult<Vec<KeyDescriptor>> { |
| 371 | let _wp = wd::watch_millis("IKeystoreMaintenance::listEntries", 500); |
| 372 | map_or_log_err(Self::list_entries(domain, namespace), Ok) |
| 373 | } |
| 374 | |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 375 | fn deleteAllKeys(&self) -> BinderResult<()> { |
| 376 | let _wp = wd::watch_millis("IKeystoreMaintenance::deleteAllKeys", 500); |
| 377 | map_or_log_err(Self::delete_all_keys(), Ok) |
| 378 | } |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 379 | } |