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}; |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 18 | use crate::error::into_logged_binder; |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 19 | use crate::error::map_km_error; |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 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}; |
Eric Biggers | 9f7ebeb | 2024-06-20 14:59:32 +0000 | [diff] [blame] | 25 | use crate::super_key::SuperKeyManager; |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 26 | use crate::utils::{ |
David Drysdale | 0fefae3 | 2024-09-16 13:32:27 +0100 | [diff] [blame] | 27 | check_dump_permission, check_get_app_uids_affected_by_sid_permissions, check_key_permission, |
Eran Messeri | cfe79f1 | 2024-02-05 17:50:41 +0000 | [diff] [blame] | 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::{ |
David Drysdale | ce2b90b | 2024-07-17 15:56:29 +0100 | [diff] [blame] | 31 | ErrorCode::ErrorCode, IKeyMintDevice::IKeyMintDevice, SecurityLevel::SecurityLevel, |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 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 | }; |
David Drysdale | 0fefae3 | 2024-09-16 13:32:27 +0100 | [diff] [blame] | 39 | use android_security_metrics::aidl::android::security::metrics::{ |
| 40 | KeystoreAtomPayload::KeystoreAtomPayload::StorageStats |
| 41 | }; |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 42 | use android_system_keystore2::aidl::android::system::keystore2::KeyDescriptor::KeyDescriptor; |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 43 | use android_system_keystore2::aidl::android::system::keystore2::ResponseCode::ResponseCode; |
| 44 | use anyhow::{Context, Result}; |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame] | 45 | use keystore2_crypto::Password; |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 46 | |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 47 | /// Reexport Domain for the benefit of DeleteListener |
| 48 | pub use android_system_keystore2::aidl::android::system::keystore2::Domain::Domain; |
| 49 | |
| 50 | /// The Maintenance module takes a delete listener argument which observes user and namespace |
| 51 | /// deletion events. |
| 52 | pub trait DeleteListener { |
| 53 | /// Called by the maintenance module when an app/namespace is deleted. |
| 54 | fn delete_namespace(&self, domain: Domain, namespace: i64) -> Result<()>; |
| 55 | /// Called by the maintenance module when a user is deleted. |
| 56 | fn delete_user(&self, user_id: u32) -> Result<()>; |
| 57 | } |
| 58 | |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 59 | /// This struct is defined to implement the aforementioned AIDL interface. |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 60 | pub struct Maintenance { |
| 61 | delete_listener: Box<dyn DeleteListener + Send + Sync + 'static>, |
| 62 | } |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 63 | |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 64 | impl Maintenance { |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 65 | /// Create a new instance of Keystore Maintenance service. |
| 66 | pub fn new_native_binder( |
| 67 | delete_listener: Box<dyn DeleteListener + Send + Sync + 'static>, |
| 68 | ) -> Result<Strong<dyn IKeystoreMaintenance>> { |
Andrew Walbran | de45c8b | 2021-04-13 14:42:38 +0000 | [diff] [blame] | 69 | Ok(BnKeystoreMaintenance::new_binder( |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 70 | Self { delete_listener }, |
Andrew Walbran | de45c8b | 2021-04-13 14:42:38 +0000 | [diff] [blame] | 71 | BinderFeatures { set_requesting_sid: true, ..BinderFeatures::default() }, |
| 72 | )) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 75 | fn add_or_remove_user(&self, user_id: i32) -> Result<()> { |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 76 | // Check permission. Function should return if this failed. Therefore having '?' at the end |
| 77 | // is very important. |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 78 | check_keystore_permission(KeystorePerm::ChangeUser).context(ks_err!())?; |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 79 | |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 80 | DB.with(|db| { |
Nathan Huckleberry | 204a044 | 2023-03-30 17:27:47 +0000 | [diff] [blame] | 81 | SUPER_KEY.write().unwrap().remove_user( |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 82 | &mut db.borrow_mut(), |
Janis Danisevskis | 0ffb8a8 | 2022-02-06 22:37:21 -0800 | [diff] [blame] | 83 | &LEGACY_IMPORTER, |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 84 | user_id as u32, |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 85 | ) |
| 86 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 87 | .context(ks_err!("Trying to delete keys from db."))?; |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 88 | self.delete_listener |
| 89 | .delete_user(user_id as u32) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 90 | .context(ks_err!("While invoking the delete listener.")) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 91 | } |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 92 | |
Eric Biggers | b0478cf | 2023-10-27 03:55:29 +0000 | [diff] [blame] | 93 | fn init_user_super_keys( |
| 94 | &self, |
| 95 | user_id: i32, |
| 96 | password: Password, |
| 97 | allow_existing: bool, |
| 98 | ) -> Result<()> { |
| 99 | // Permission check. Must return on error. Do not touch the '?'. |
| 100 | check_keystore_permission(KeystorePerm::ChangeUser).context(ks_err!())?; |
| 101 | |
| 102 | let mut skm = SUPER_KEY.write().unwrap(); |
| 103 | DB.with(|db| { |
| 104 | skm.initialize_user( |
| 105 | &mut db.borrow_mut(), |
| 106 | &LEGACY_IMPORTER, |
| 107 | user_id as u32, |
| 108 | &password, |
| 109 | allow_existing, |
| 110 | ) |
| 111 | }) |
| 112 | .context(ks_err!("Failed to initialize user super keys")) |
| 113 | } |
| 114 | |
| 115 | // Deletes all auth-bound keys when the user's LSKF is removed. |
| 116 | fn on_user_lskf_removed(user_id: i32) -> Result<()> { |
| 117 | // Permission check. Must return on error. Do not touch the '?'. |
| 118 | check_keystore_permission(KeystorePerm::ChangePassword).context(ks_err!())?; |
| 119 | |
| 120 | LEGACY_IMPORTER |
| 121 | .bulk_delete_user(user_id as u32, true) |
| 122 | .context(ks_err!("Failed to delete legacy keys."))?; |
| 123 | |
| 124 | DB.with(|db| db.borrow_mut().unbind_auth_bound_keys_for_user(user_id as u32)) |
| 125 | .context(ks_err!("Failed to delete auth-bound keys.")) |
| 126 | } |
| 127 | |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 128 | fn clear_namespace(&self, domain: Domain, nspace: i64) -> Result<()> { |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 129 | // Permission check. Must return on error. Do not touch the '?'. |
Janis Danisevskis | a916d99 | 2021-10-19 15:46:09 -0700 | [diff] [blame] | 130 | check_keystore_permission(KeystorePerm::ClearUID).context("In clear_namespace.")?; |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 131 | |
Janis Danisevskis | 0ffb8a8 | 2022-02-06 22:37:21 -0800 | [diff] [blame] | 132 | LEGACY_IMPORTER |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 133 | .bulk_delete_uid(domain, nspace) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 134 | .context(ks_err!("Trying to delete legacy keys."))?; |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 135 | DB.with(|db| db.borrow_mut().unbind_keys_for_namespace(domain, nspace)) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 136 | .context(ks_err!("Trying to delete keys from db."))?; |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 137 | self.delete_listener |
| 138 | .delete_namespace(domain, nspace) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 139 | .context(ks_err!("While invoking the delete listener.")) |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 140 | } |
Hasini Gunasinghe | 9ee1841 | 2021-03-11 20:12:44 +0000 | [diff] [blame] | 141 | |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 142 | fn call_with_watchdog<F>(sec_level: SecurityLevel, name: &'static str, op: &F) -> Result<()> |
| 143 | where |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 144 | F: Fn(Strong<dyn IKeyMintDevice>) -> binder::Result<()>, |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 145 | { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 146 | let (km_dev, _, _) = |
| 147 | get_keymint_device(&sec_level).context(ks_err!("getting keymint device"))?; |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 148 | |
David Drysdale | c652f6c | 2024-07-18 13:01:23 +0100 | [diff] [blame] | 149 | let _wp = wd::watch_millis_with("Maintenance::call_with_watchdog", 500, (sec_level, name)); |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 150 | map_km_error(op(km_dev)).with_context(|| ks_err!("calling {}", name))?; |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 151 | Ok(()) |
| 152 | } |
| 153 | |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 154 | fn call_on_all_security_levels<F>(name: &'static str, op: F) -> Result<()> |
| 155 | where |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 156 | F: Fn(Strong<dyn IKeyMintDevice>) -> binder::Result<()>, |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 157 | { |
| 158 | let sec_levels = [ |
| 159 | (SecurityLevel::TRUSTED_ENVIRONMENT, "TRUSTED_ENVIRONMENT"), |
| 160 | (SecurityLevel::STRONGBOX, "STRONGBOX"), |
| 161 | ]; |
James Farrell | d77b97f | 2023-08-15 20:03:38 +0000 | [diff] [blame] | 162 | sec_levels.iter().try_fold((), |_result, (sec_level, sec_level_string)| { |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 163 | let curr_result = Maintenance::call_with_watchdog(*sec_level, name, &op); |
| 164 | match curr_result { |
| 165 | Ok(()) => log::info!( |
| 166 | "Call to {} succeeded for security level {}.", |
| 167 | name, |
| 168 | &sec_level_string |
| 169 | ), |
David Drysdale | ce2b90b | 2024-07-17 15:56:29 +0100 | [diff] [blame] | 170 | Err(ref e) => { |
| 171 | if *sec_level == SecurityLevel::STRONGBOX |
| 172 | && e.downcast_ref::<Error>() |
| 173 | == Some(&Error::Km(ErrorCode::HARDWARE_TYPE_UNAVAILABLE)) |
| 174 | { |
| 175 | log::info!("Call to {} failed for StrongBox as it is not available", name,) |
| 176 | } else { |
| 177 | log::error!( |
| 178 | "Call to {} failed for security level {}: {}.", |
| 179 | name, |
| 180 | &sec_level_string, |
| 181 | e |
| 182 | ) |
| 183 | } |
| 184 | } |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 185 | } |
James Farrell | d77b97f | 2023-08-15 20:03:38 +0000 | [diff] [blame] | 186 | curr_result |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 187 | }) |
| 188 | } |
| 189 | |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 190 | fn early_boot_ended() -> Result<()> { |
Janis Danisevskis | a916d99 | 2021-10-19 15:46:09 -0700 | [diff] [blame] | 191 | check_keystore_permission(KeystorePerm::EarlyBootEnded) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 192 | .context(ks_err!("Checking permission"))?; |
Paul Crowley | 44c02da | 2021-04-08 17:04:43 +0000 | [diff] [blame] | 193 | log::info!("In early_boot_ended."); |
| 194 | |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 195 | if let Err(e) = |
| 196 | DB.with(|db| SuperKeyManager::set_up_boot_level_cache(&SUPER_KEY, &mut db.borrow_mut())) |
| 197 | { |
Paul Crowley | 44c02da | 2021-04-08 17:04:43 +0000 | [diff] [blame] | 198 | log::error!("SUPER_KEY.set_up_boot_level_cache failed:\n{:?}\n:(", e); |
| 199 | } |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 200 | Maintenance::call_on_all_security_levels("earlyBootEnded", |dev| dev.earlyBootEnded()) |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 203 | fn migrate_key_namespace(source: &KeyDescriptor, destination: &KeyDescriptor) -> Result<()> { |
John Wu | 889c1cc | 2022-03-14 16:02:56 -0700 | [diff] [blame] | 204 | let calling_uid = ThreadState::get_calling_uid(); |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 205 | |
John Wu | 889c1cc | 2022-03-14 16:02:56 -0700 | [diff] [blame] | 206 | match source.domain { |
| 207 | Domain::SELINUX | Domain::KEY_ID | Domain::APP => (), |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 208 | _ => { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 209 | return Err(Error::Rc(ResponseCode::INVALID_ARGUMENT)) |
| 210 | .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] | 211 | } |
| 212 | }; |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 213 | |
John Wu | 889c1cc | 2022-03-14 16:02:56 -0700 | [diff] [blame] | 214 | match destination.domain { |
| 215 | Domain::SELINUX | Domain::APP => (), |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 216 | _ => { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 217 | return Err(Error::Rc(ResponseCode::INVALID_ARGUMENT)) |
| 218 | .context(ks_err!("Destination domain must be one of APP or SELINUX.")); |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 219 | } |
| 220 | }; |
| 221 | |
John Wu | 889c1cc | 2022-03-14 16:02:56 -0700 | [diff] [blame] | 222 | let user_id = uid_to_android_user(calling_uid); |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 223 | |
Eric Biggers | 673d34a | 2023-10-18 01:54:18 +0000 | [diff] [blame] | 224 | 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] | 225 | |
| 226 | DB.with(|db| { |
John Wu | 889c1cc | 2022-03-14 16:02:56 -0700 | [diff] [blame] | 227 | let (key_id_guard, _) = LEGACY_IMPORTER |
| 228 | .with_try_import(source, calling_uid, super_key, || { |
| 229 | db.borrow_mut().load_key_entry( |
| 230 | source, |
| 231 | KeyType::Client, |
| 232 | KeyEntryLoadBits::NONE, |
| 233 | calling_uid, |
| 234 | |k, av| { |
| 235 | check_key_permission(KeyPerm::Use, k, &av)?; |
| 236 | check_key_permission(KeyPerm::Delete, k, &av)?; |
| 237 | check_key_permission(KeyPerm::Grant, k, &av) |
| 238 | }, |
| 239 | ) |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 240 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 241 | .context(ks_err!("Failed to load key blob."))?; |
John Wu | 889c1cc | 2022-03-14 16:02:56 -0700 | [diff] [blame] | 242 | { |
| 243 | db.borrow_mut().migrate_key_namespace(key_id_guard, destination, calling_uid, |k| { |
| 244 | check_key_permission(KeyPerm::Rebind, k, &None) |
| 245 | }) |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 246 | } |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 247 | }) |
| 248 | } |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 249 | |
| 250 | fn delete_all_keys() -> Result<()> { |
| 251 | // Security critical permission check. This statement must return on fail. |
Janis Danisevskis | a916d99 | 2021-10-19 15:46:09 -0700 | [diff] [blame] | 252 | check_keystore_permission(KeystorePerm::DeleteAllKeys) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 253 | .context(ks_err!("Checking permission"))?; |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 254 | log::info!("In delete_all_keys."); |
| 255 | |
| 256 | Maintenance::call_on_all_security_levels("deleteAllKeys", |dev| dev.deleteAllKeys()) |
| 257 | } |
Eran Messeri | 4dc27b5 | 2024-01-09 12:43:31 +0000 | [diff] [blame] | 258 | |
| 259 | fn get_app_uids_affected_by_sid( |
| 260 | user_id: i32, |
| 261 | secure_user_id: i64, |
| 262 | ) -> Result<std::vec::Vec<i64>> { |
| 263 | // 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] | 264 | // associated with a user, so it requires the "android.permission.MANAGE_USERS" |
| 265 | // permission (to avoid leaking list of apps to unauthorized callers). |
| 266 | check_get_app_uids_affected_by_sid_permissions().context(ks_err!())?; |
Eran Messeri | 4dc27b5 | 2024-01-09 12:43:31 +0000 | [diff] [blame] | 267 | DB.with(|db| db.borrow_mut().get_app_uids_affected_by_sid(user_id, secure_user_id)) |
| 268 | .context(ks_err!("Failed to get app UIDs affected by SID")) |
| 269 | } |
David Drysdale | 0fefae3 | 2024-09-16 13:32:27 +0100 | [diff] [blame] | 270 | |
| 271 | fn dump_state(&self, f: &mut dyn std::io::Write) -> std::io::Result<()> { |
| 272 | writeln!(f, "keystore2 running")?; |
| 273 | writeln!(f)?; |
| 274 | |
| 275 | // Display underlying device information |
| 276 | for sec_level in &[SecurityLevel::TRUSTED_ENVIRONMENT, SecurityLevel::STRONGBOX] { |
| 277 | let Ok((_dev, hw_info, uuid)) = get_keymint_device(sec_level) else { continue }; |
| 278 | |
| 279 | writeln!(f, "Device info for {sec_level:?} with {uuid:?}")?; |
| 280 | writeln!(f, " HAL version: {}", hw_info.versionNumber)?; |
| 281 | writeln!(f, " Implementation name: {}", hw_info.keyMintName)?; |
| 282 | writeln!(f, " Implementation author: {}", hw_info.keyMintAuthorName)?; |
| 283 | writeln!(f, " Timestamp token required: {}", hw_info.timestampTokenRequired)?; |
| 284 | } |
| 285 | writeln!(f)?; |
| 286 | |
| 287 | // Display database size information. |
| 288 | match crate::metrics_store::pull_storage_stats() { |
| 289 | Ok(atoms) => { |
| 290 | writeln!(f, "Database size information (in bytes):")?; |
| 291 | for atom in atoms { |
| 292 | if let StorageStats(stats) = &atom.payload { |
| 293 | let stype = format!("{:?}", stats.storage_type); |
| 294 | if stats.unused_size == 0 { |
| 295 | writeln!(f, " {:<40}: {:>12}", stype, stats.size)?; |
| 296 | } else { |
| 297 | writeln!( |
| 298 | f, |
| 299 | " {:<40}: {:>12} (unused {})", |
| 300 | stype, stats.size, stats.unused_size |
| 301 | )?; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | Err(e) => { |
| 307 | writeln!(f, "Failed to retrieve storage stats: {e:?}")?; |
| 308 | } |
| 309 | } |
| 310 | writeln!(f)?; |
| 311 | |
David Drysdale | 49811e2 | 2023-05-22 18:51:30 +0100 | [diff] [blame] | 312 | // Display accumulated metrics. |
| 313 | writeln!(f, "Metrics information:")?; |
| 314 | writeln!(f)?; |
| 315 | write!(f, "{:?}", *crate::metrics_store::METRICS_STORE)?; |
| 316 | writeln!(f)?; |
| 317 | |
David Drysdale | 0fefae3 | 2024-09-16 13:32:27 +0100 | [diff] [blame] | 318 | // Reminder: any additional information added to the `dump_state()` output needs to be |
| 319 | // careful not to include confidential information (e.g. key material). |
| 320 | |
| 321 | Ok(()) |
| 322 | } |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 323 | } |
| 324 | |
David Drysdale | 0fefae3 | 2024-09-16 13:32:27 +0100 | [diff] [blame] | 325 | impl Interface for Maintenance { |
| 326 | fn dump( |
| 327 | &self, |
| 328 | f: &mut dyn std::io::Write, |
| 329 | _args: &[&std::ffi::CStr], |
| 330 | ) -> Result<(), binder::StatusCode> { |
| 331 | if !keystore2_flags::enable_dump() { |
| 332 | log::info!("skipping dump() as flag not enabled"); |
| 333 | return Ok(()); |
| 334 | } |
| 335 | log::info!("dump()"); |
| 336 | let _wp = wd::watch("IKeystoreMaintenance::dump"); |
| 337 | check_dump_permission().map_err(|_e| { |
| 338 | log::error!("dump permission denied"); |
| 339 | binder::StatusCode::PERMISSION_DENIED |
| 340 | })?; |
| 341 | |
| 342 | self.dump_state(f).map_err(|e| { |
| 343 | log::error!("dump_state failed: {e:?}"); |
| 344 | binder::StatusCode::UNKNOWN_ERROR |
| 345 | }) |
| 346 | } |
| 347 | } |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 348 | |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 349 | impl IKeystoreMaintenance for Maintenance { |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 350 | fn onUserAdded(&self, user_id: i32) -> BinderResult<()> { |
David Drysdale | e85523f | 2023-06-19 12:28:53 +0100 | [diff] [blame] | 351 | log::info!("onUserAdded(user={user_id})"); |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 352 | let _wp = wd::watch("IKeystoreMaintenance::onUserAdded"); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 353 | self.add_or_remove_user(user_id).map_err(into_logged_binder) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Eric Biggers | b0478cf | 2023-10-27 03:55:29 +0000 | [diff] [blame] | 356 | fn initUserSuperKeys( |
| 357 | &self, |
| 358 | user_id: i32, |
| 359 | password: &[u8], |
| 360 | allow_existing: bool, |
| 361 | ) -> BinderResult<()> { |
| 362 | log::info!("initUserSuperKeys(user={user_id}, allow_existing={allow_existing})"); |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 363 | let _wp = wd::watch("IKeystoreMaintenance::initUserSuperKeys"); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 364 | self.init_user_super_keys(user_id, password.into(), allow_existing) |
| 365 | .map_err(into_logged_binder) |
Eric Biggers | b0478cf | 2023-10-27 03:55:29 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 368 | fn onUserRemoved(&self, user_id: i32) -> BinderResult<()> { |
David Drysdale | e85523f | 2023-06-19 12:28:53 +0100 | [diff] [blame] | 369 | log::info!("onUserRemoved(user={user_id})"); |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 370 | let _wp = wd::watch("IKeystoreMaintenance::onUserRemoved"); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 371 | self.add_or_remove_user(user_id).map_err(into_logged_binder) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 372 | } |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 373 | |
Eric Biggers | b0478cf | 2023-10-27 03:55:29 +0000 | [diff] [blame] | 374 | fn onUserLskfRemoved(&self, user_id: i32) -> BinderResult<()> { |
| 375 | log::info!("onUserLskfRemoved(user={user_id})"); |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 376 | let _wp = wd::watch("IKeystoreMaintenance::onUserLskfRemoved"); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 377 | Self::on_user_lskf_removed(user_id).map_err(into_logged_binder) |
Eric Biggers | b0478cf | 2023-10-27 03:55:29 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 380 | fn clearNamespace(&self, domain: Domain, nspace: i64) -> BinderResult<()> { |
David Drysdale | e85523f | 2023-06-19 12:28:53 +0100 | [diff] [blame] | 381 | log::info!("clearNamespace({domain:?}, nspace={nspace})"); |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 382 | let _wp = wd::watch("IKeystoreMaintenance::clearNamespace"); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 383 | self.clear_namespace(domain, nspace).map_err(into_logged_binder) |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 384 | } |
Hasini Gunasinghe | 9ee1841 | 2021-03-11 20:12:44 +0000 | [diff] [blame] | 385 | |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 386 | fn earlyBootEnded(&self) -> BinderResult<()> { |
David Drysdale | e85523f | 2023-06-19 12:28:53 +0100 | [diff] [blame] | 387 | log::info!("earlyBootEnded()"); |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 388 | let _wp = wd::watch("IKeystoreMaintenance::earlyBootEnded"); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 389 | Self::early_boot_ended().map_err(into_logged_binder) |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 390 | } |
| 391 | |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 392 | fn migrateKeyNamespace( |
| 393 | &self, |
| 394 | source: &KeyDescriptor, |
| 395 | destination: &KeyDescriptor, |
| 396 | ) -> BinderResult<()> { |
David Drysdale | e85523f | 2023-06-19 12:28:53 +0100 | [diff] [blame] | 397 | log::info!("migrateKeyNamespace(src={source:?}, dest={destination:?})"); |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 398 | let _wp = wd::watch("IKeystoreMaintenance::migrateKeyNamespace"); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 399 | Self::migrate_key_namespace(source, destination).map_err(into_logged_binder) |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 400 | } |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 401 | |
| 402 | fn deleteAllKeys(&self) -> BinderResult<()> { |
David Drysdale | ce2b90b | 2024-07-17 15:56:29 +0100 | [diff] [blame] | 403 | log::warn!("deleteAllKeys() invoked, indicating initial setup or post-factory reset"); |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 404 | let _wp = wd::watch("IKeystoreMaintenance::deleteAllKeys"); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 405 | Self::delete_all_keys().map_err(into_logged_binder) |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 406 | } |
Eran Messeri | 4dc27b5 | 2024-01-09 12:43:31 +0000 | [diff] [blame] | 407 | |
| 408 | fn getAppUidsAffectedBySid( |
| 409 | &self, |
| 410 | user_id: i32, |
| 411 | secure_user_id: i64, |
| 412 | ) -> BinderResult<std::vec::Vec<i64>> { |
| 413 | log::info!("getAppUidsAffectedBySid(secure_user_id={secure_user_id:?})"); |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 414 | let _wp = wd::watch("IKeystoreMaintenance::getAppUidsAffectedBySid"); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 415 | Self::get_app_uids_affected_by_sid(user_id, secure_user_id).map_err(into_logged_binder) |
Eran Messeri | 4dc27b5 | 2024-01-09 12:43:31 +0000 | [diff] [blame] | 416 | } |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 417 | } |