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::{ |
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::{ |
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 | }; |
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 | |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 72 | fn add_or_remove_user(&self, user_id: i32) -> Result<()> { |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [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::ChangeUser).context(ks_err!())?; |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 76 | |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 77 | DB.with(|db| { |
Nathan Huckleberry | 204a044 | 2023-03-30 17:27:47 +0000 | [diff] [blame] | 78 | SUPER_KEY.write().unwrap().remove_user( |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 79 | &mut db.borrow_mut(), |
Janis Danisevskis | 0ffb8a8 | 2022-02-06 22:37:21 -0800 | [diff] [blame] | 80 | &LEGACY_IMPORTER, |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 81 | user_id as u32, |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 82 | ) |
| 83 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 84 | .context(ks_err!("Trying to delete keys from db."))?; |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 85 | self.delete_listener |
| 86 | .delete_user(user_id as u32) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 87 | .context(ks_err!("While invoking the delete listener.")) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 88 | } |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 89 | |
Eric Biggers | b0478cf | 2023-10-27 03:55:29 +0000 | [diff] [blame] | 90 | fn init_user_super_keys( |
| 91 | &self, |
| 92 | user_id: i32, |
| 93 | password: Password, |
| 94 | allow_existing: bool, |
| 95 | ) -> Result<()> { |
| 96 | // Permission check. Must return on error. Do not touch the '?'. |
| 97 | check_keystore_permission(KeystorePerm::ChangeUser).context(ks_err!())?; |
| 98 | |
| 99 | let mut skm = SUPER_KEY.write().unwrap(); |
| 100 | DB.with(|db| { |
| 101 | skm.initialize_user( |
| 102 | &mut db.borrow_mut(), |
| 103 | &LEGACY_IMPORTER, |
| 104 | user_id as u32, |
| 105 | &password, |
| 106 | allow_existing, |
| 107 | ) |
| 108 | }) |
| 109 | .context(ks_err!("Failed to initialize user super keys")) |
| 110 | } |
| 111 | |
| 112 | // Deletes all auth-bound keys when the user's LSKF is removed. |
| 113 | fn on_user_lskf_removed(user_id: i32) -> Result<()> { |
| 114 | // Permission check. Must return on error. Do not touch the '?'. |
| 115 | check_keystore_permission(KeystorePerm::ChangePassword).context(ks_err!())?; |
| 116 | |
| 117 | LEGACY_IMPORTER |
| 118 | .bulk_delete_user(user_id as u32, true) |
| 119 | .context(ks_err!("Failed to delete legacy keys."))?; |
| 120 | |
| 121 | DB.with(|db| db.borrow_mut().unbind_auth_bound_keys_for_user(user_id as u32)) |
| 122 | .context(ks_err!("Failed to delete auth-bound keys.")) |
| 123 | } |
| 124 | |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 125 | fn clear_namespace(&self, domain: Domain, nspace: i64) -> Result<()> { |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 126 | // Permission check. Must return on error. Do not touch the '?'. |
Janis Danisevskis | a916d99 | 2021-10-19 15:46:09 -0700 | [diff] [blame] | 127 | check_keystore_permission(KeystorePerm::ClearUID).context("In clear_namespace.")?; |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 128 | |
Janis Danisevskis | 0ffb8a8 | 2022-02-06 22:37:21 -0800 | [diff] [blame] | 129 | LEGACY_IMPORTER |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 130 | .bulk_delete_uid(domain, nspace) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 131 | .context(ks_err!("Trying to delete legacy keys."))?; |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 132 | DB.with(|db| db.borrow_mut().unbind_keys_for_namespace(domain, nspace)) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 133 | .context(ks_err!("Trying to delete keys from db."))?; |
Janis Danisevskis | 5898d15 | 2021-06-15 08:23:46 -0700 | [diff] [blame] | 134 | self.delete_listener |
| 135 | .delete_namespace(domain, nspace) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 136 | .context(ks_err!("While invoking the delete listener.")) |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 137 | } |
Hasini Gunasinghe | 9ee1841 | 2021-03-11 20:12:44 +0000 | [diff] [blame] | 138 | |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 139 | fn call_with_watchdog<F>(sec_level: SecurityLevel, name: &'static str, op: &F) -> Result<()> |
| 140 | where |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 141 | F: Fn(Strong<dyn IKeyMintDevice>) -> binder::Result<()>, |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 142 | { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 143 | let (km_dev, _, _) = |
| 144 | get_keymint_device(&sec_level).context(ks_err!("getting keymint device"))?; |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 145 | |
David Drysdale | 387c85b | 2024-06-10 14:40:45 +0100 | [diff] [blame] | 146 | let _wp = wd::watch_millis_with("In call_with_watchdog", 500, (sec_level, name)); |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 147 | map_km_error(op(km_dev)).with_context(|| ks_err!("calling {}", name))?; |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 148 | Ok(()) |
| 149 | } |
| 150 | |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 151 | fn call_on_all_security_levels<F>(name: &'static str, op: F) -> Result<()> |
| 152 | where |
Stephen Crane | 23cf724 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 153 | F: Fn(Strong<dyn IKeyMintDevice>) -> binder::Result<()>, |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 154 | { |
| 155 | let sec_levels = [ |
| 156 | (SecurityLevel::TRUSTED_ENVIRONMENT, "TRUSTED_ENVIRONMENT"), |
| 157 | (SecurityLevel::STRONGBOX, "STRONGBOX"), |
| 158 | ]; |
James Farrell | d77b97f | 2023-08-15 20:03:38 +0000 | [diff] [blame] | 159 | sec_levels.iter().try_fold((), |_result, (sec_level, sec_level_string)| { |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 160 | let curr_result = Maintenance::call_with_watchdog(*sec_level, name, &op); |
| 161 | match curr_result { |
| 162 | Ok(()) => log::info!( |
| 163 | "Call to {} succeeded for security level {}.", |
| 164 | name, |
| 165 | &sec_level_string |
| 166 | ), |
David Drysdale | ce2b90b | 2024-07-17 15:56:29 +0100 | [diff] [blame^] | 167 | Err(ref e) => { |
| 168 | if *sec_level == SecurityLevel::STRONGBOX |
| 169 | && e.downcast_ref::<Error>() |
| 170 | == Some(&Error::Km(ErrorCode::HARDWARE_TYPE_UNAVAILABLE)) |
| 171 | { |
| 172 | log::info!("Call to {} failed for StrongBox as it is not available", name,) |
| 173 | } else { |
| 174 | log::error!( |
| 175 | "Call to {} failed for security level {}: {}.", |
| 176 | name, |
| 177 | &sec_level_string, |
| 178 | e |
| 179 | ) |
| 180 | } |
| 181 | } |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 182 | } |
James Farrell | d77b97f | 2023-08-15 20:03:38 +0000 | [diff] [blame] | 183 | curr_result |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 184 | }) |
| 185 | } |
| 186 | |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 187 | fn early_boot_ended() -> Result<()> { |
Janis Danisevskis | a916d99 | 2021-10-19 15:46:09 -0700 | [diff] [blame] | 188 | check_keystore_permission(KeystorePerm::EarlyBootEnded) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 189 | .context(ks_err!("Checking permission"))?; |
Paul Crowley | 44c02da | 2021-04-08 17:04:43 +0000 | [diff] [blame] | 190 | log::info!("In early_boot_ended."); |
| 191 | |
Janis Danisevskis | 0fd25a6 | 2022-01-04 19:53:37 -0800 | [diff] [blame] | 192 | if let Err(e) = |
| 193 | DB.with(|db| SuperKeyManager::set_up_boot_level_cache(&SUPER_KEY, &mut db.borrow_mut())) |
| 194 | { |
Paul Crowley | 44c02da | 2021-04-08 17:04:43 +0000 | [diff] [blame] | 195 | log::error!("SUPER_KEY.set_up_boot_level_cache failed:\n{:?}\n:(", e); |
| 196 | } |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 197 | Maintenance::call_on_all_security_levels("earlyBootEnded", |dev| dev.earlyBootEnded()) |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 200 | fn migrate_key_namespace(source: &KeyDescriptor, destination: &KeyDescriptor) -> Result<()> { |
John Wu | 889c1cc | 2022-03-14 16:02:56 -0700 | [diff] [blame] | 201 | let calling_uid = ThreadState::get_calling_uid(); |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 202 | |
John Wu | 889c1cc | 2022-03-14 16:02:56 -0700 | [diff] [blame] | 203 | match source.domain { |
| 204 | Domain::SELINUX | Domain::KEY_ID | Domain::APP => (), |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 205 | _ => { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 206 | return Err(Error::Rc(ResponseCode::INVALID_ARGUMENT)) |
| 207 | .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] | 208 | } |
| 209 | }; |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 210 | |
John Wu | 889c1cc | 2022-03-14 16:02:56 -0700 | [diff] [blame] | 211 | match destination.domain { |
| 212 | Domain::SELINUX | Domain::APP => (), |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 213 | _ => { |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 214 | return Err(Error::Rc(ResponseCode::INVALID_ARGUMENT)) |
| 215 | .context(ks_err!("Destination domain must be one of APP or SELINUX.")); |
John Wu | 16db29e | 2022-01-13 15:21:43 -0800 | [diff] [blame] | 216 | } |
| 217 | }; |
| 218 | |
John Wu | 889c1cc | 2022-03-14 16:02:56 -0700 | [diff] [blame] | 219 | let user_id = uid_to_android_user(calling_uid); |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 220 | |
Eric Biggers | 673d34a | 2023-10-18 01:54:18 +0000 | [diff] [blame] | 221 | 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] | 222 | |
| 223 | DB.with(|db| { |
John Wu | 889c1cc | 2022-03-14 16:02:56 -0700 | [diff] [blame] | 224 | let (key_id_guard, _) = LEGACY_IMPORTER |
| 225 | .with_try_import(source, calling_uid, super_key, || { |
| 226 | db.borrow_mut().load_key_entry( |
| 227 | source, |
| 228 | KeyType::Client, |
| 229 | KeyEntryLoadBits::NONE, |
| 230 | calling_uid, |
| 231 | |k, av| { |
| 232 | check_key_permission(KeyPerm::Use, k, &av)?; |
| 233 | check_key_permission(KeyPerm::Delete, k, &av)?; |
| 234 | check_key_permission(KeyPerm::Grant, k, &av) |
| 235 | }, |
| 236 | ) |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 237 | }) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 238 | .context(ks_err!("Failed to load key blob."))?; |
John Wu | 889c1cc | 2022-03-14 16:02:56 -0700 | [diff] [blame] | 239 | { |
| 240 | db.borrow_mut().migrate_key_namespace(key_id_guard, destination, calling_uid, |k| { |
| 241 | check_key_permission(KeyPerm::Rebind, k, &None) |
| 242 | }) |
Janis Danisevskis | f84d0b0 | 2022-01-26 14:11:14 -0800 | [diff] [blame] | 243 | } |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 244 | }) |
| 245 | } |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 246 | |
| 247 | fn delete_all_keys() -> Result<()> { |
| 248 | // Security critical permission check. This statement must return on fail. |
Janis Danisevskis | a916d99 | 2021-10-19 15:46:09 -0700 | [diff] [blame] | 249 | check_keystore_permission(KeystorePerm::DeleteAllKeys) |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 250 | .context(ks_err!("Checking permission"))?; |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 251 | log::info!("In delete_all_keys."); |
| 252 | |
| 253 | Maintenance::call_on_all_security_levels("deleteAllKeys", |dev| dev.deleteAllKeys()) |
| 254 | } |
Eran Messeri | 4dc27b5 | 2024-01-09 12:43:31 +0000 | [diff] [blame] | 255 | |
| 256 | fn get_app_uids_affected_by_sid( |
| 257 | user_id: i32, |
| 258 | secure_user_id: i64, |
| 259 | ) -> Result<std::vec::Vec<i64>> { |
| 260 | // 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] | 261 | // associated with a user, so it requires the "android.permission.MANAGE_USERS" |
| 262 | // permission (to avoid leaking list of apps to unauthorized callers). |
| 263 | check_get_app_uids_affected_by_sid_permissions().context(ks_err!())?; |
Eran Messeri | 4dc27b5 | 2024-01-09 12:43:31 +0000 | [diff] [blame] | 264 | DB.with(|db| db.borrow_mut().get_app_uids_affected_by_sid(user_id, secure_user_id)) |
| 265 | .context(ks_err!("Failed to get app UIDs affected by SID")) |
| 266 | } |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 269 | impl Interface for Maintenance {} |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 270 | |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 271 | impl IKeystoreMaintenance for Maintenance { |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 272 | fn onUserAdded(&self, user_id: i32) -> BinderResult<()> { |
David Drysdale | e85523f | 2023-06-19 12:28:53 +0100 | [diff] [blame] | 273 | log::info!("onUserAdded(user={user_id})"); |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 274 | let _wp = wd::watch("IKeystoreMaintenance::onUserAdded"); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 275 | self.add_or_remove_user(user_id).map_err(into_logged_binder) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Eric Biggers | b0478cf | 2023-10-27 03:55:29 +0000 | [diff] [blame] | 278 | fn initUserSuperKeys( |
| 279 | &self, |
| 280 | user_id: i32, |
| 281 | password: &[u8], |
| 282 | allow_existing: bool, |
| 283 | ) -> BinderResult<()> { |
| 284 | log::info!("initUserSuperKeys(user={user_id}, allow_existing={allow_existing})"); |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 285 | let _wp = wd::watch("IKeystoreMaintenance::initUserSuperKeys"); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 286 | self.init_user_super_keys(user_id, password.into(), allow_existing) |
| 287 | .map_err(into_logged_binder) |
Eric Biggers | b0478cf | 2023-10-27 03:55:29 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 290 | fn onUserRemoved(&self, user_id: i32) -> BinderResult<()> { |
David Drysdale | e85523f | 2023-06-19 12:28:53 +0100 | [diff] [blame] | 291 | log::info!("onUserRemoved(user={user_id})"); |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 292 | let _wp = wd::watch("IKeystoreMaintenance::onUserRemoved"); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 293 | self.add_or_remove_user(user_id).map_err(into_logged_binder) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 294 | } |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 295 | |
Eric Biggers | b0478cf | 2023-10-27 03:55:29 +0000 | [diff] [blame] | 296 | fn onUserLskfRemoved(&self, user_id: i32) -> BinderResult<()> { |
| 297 | log::info!("onUserLskfRemoved(user={user_id})"); |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 298 | let _wp = wd::watch("IKeystoreMaintenance::onUserLskfRemoved"); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 299 | Self::on_user_lskf_removed(user_id).map_err(into_logged_binder) |
Eric Biggers | b0478cf | 2023-10-27 03:55:29 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 302 | fn clearNamespace(&self, domain: Domain, nspace: i64) -> BinderResult<()> { |
David Drysdale | e85523f | 2023-06-19 12:28:53 +0100 | [diff] [blame] | 303 | log::info!("clearNamespace({domain:?}, nspace={nspace})"); |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 304 | let _wp = wd::watch("IKeystoreMaintenance::clearNamespace"); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 305 | self.clear_namespace(domain, nspace).map_err(into_logged_binder) |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 306 | } |
Hasini Gunasinghe | 9ee1841 | 2021-03-11 20:12:44 +0000 | [diff] [blame] | 307 | |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 308 | fn earlyBootEnded(&self) -> BinderResult<()> { |
David Drysdale | e85523f | 2023-06-19 12:28:53 +0100 | [diff] [blame] | 309 | log::info!("earlyBootEnded()"); |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 310 | let _wp = wd::watch("IKeystoreMaintenance::earlyBootEnded"); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 311 | Self::early_boot_ended().map_err(into_logged_binder) |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 314 | fn migrateKeyNamespace( |
| 315 | &self, |
| 316 | source: &KeyDescriptor, |
| 317 | destination: &KeyDescriptor, |
| 318 | ) -> BinderResult<()> { |
David Drysdale | e85523f | 2023-06-19 12:28:53 +0100 | [diff] [blame] | 319 | log::info!("migrateKeyNamespace(src={source:?}, dest={destination:?})"); |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 320 | let _wp = wd::watch("IKeystoreMaintenance::migrateKeyNamespace"); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 321 | Self::migrate_key_namespace(source, destination).map_err(into_logged_binder) |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 322 | } |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 323 | |
| 324 | fn deleteAllKeys(&self) -> BinderResult<()> { |
David Drysdale | ce2b90b | 2024-07-17 15:56:29 +0100 | [diff] [blame^] | 325 | log::warn!("deleteAllKeys() invoked, indicating initial setup or post-factory reset"); |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 326 | let _wp = wd::watch("IKeystoreMaintenance::deleteAllKeys"); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 327 | Self::delete_all_keys().map_err(into_logged_binder) |
Paul Crowley | 46c703e | 2021-08-06 15:13:53 -0700 | [diff] [blame] | 328 | } |
Eran Messeri | 4dc27b5 | 2024-01-09 12:43:31 +0000 | [diff] [blame] | 329 | |
| 330 | fn getAppUidsAffectedBySid( |
| 331 | &self, |
| 332 | user_id: i32, |
| 333 | secure_user_id: i64, |
| 334 | ) -> BinderResult<std::vec::Vec<i64>> { |
| 335 | log::info!("getAppUidsAffectedBySid(secure_user_id={secure_user_id:?})"); |
David Drysdale | 541846b | 2024-05-23 13:16:07 +0100 | [diff] [blame] | 336 | let _wp = wd::watch("IKeystoreMaintenance::getAppUidsAffectedBySid"); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 337 | 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] | 338 | } |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 339 | } |