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; |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 22 | use crate::globals::{DB, LEGACY_MIGRATOR, SUPER_KEY}; |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 23 | use crate::permission::{KeyPerm, KeystorePerm}; |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 24 | use crate::super_key::UserState; |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 25 | use crate::utils::{check_key_permission, check_keystore_permission, watchdog as wd}; |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 26 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::SecurityLevel::SecurityLevel; |
Hasini Gunasinghe | 9ee1841 | 2021-03-11 20:12:44 +0000 | [diff] [blame] | 27 | use android_security_maintenance::aidl::android::security::maintenance::{ |
| 28 | IKeystoreMaintenance::{BnKeystoreMaintenance, IKeystoreMaintenance}, |
| 29 | UserState::UserState as AidlUserState, |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 30 | }; |
Andrew Walbran | de45c8b | 2021-04-13 14:42:38 +0000 | [diff] [blame] | 31 | use android_security_maintenance::binder::{ |
| 32 | BinderFeatures, Interface, Result as BinderResult, Strong, ThreadState, |
| 33 | }; |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 34 | use android_system_keystore2::aidl::android::system::keystore2::ResponseCode::ResponseCode; |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 35 | use android_system_keystore2::aidl::android::system::keystore2::{ |
| 36 | Domain::Domain, KeyDescriptor::KeyDescriptor, |
| 37 | }; |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 38 | use anyhow::{Context, Result}; |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame] | 39 | use keystore2_crypto::Password; |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 40 | |
| 41 | /// This struct is defined to implement the aforementioned AIDL interface. |
| 42 | /// As of now, it is an empty struct. |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 43 | pub struct Maintenance; |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 44 | |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 45 | impl Maintenance { |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 46 | /// Create a new instance of Keystore User Manager service. |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 47 | pub fn new_native_binder() -> Result<Strong<dyn IKeystoreMaintenance>> { |
Andrew Walbran | de45c8b | 2021-04-13 14:42:38 +0000 | [diff] [blame] | 48 | Ok(BnKeystoreMaintenance::new_binder( |
| 49 | Self, |
| 50 | BinderFeatures { set_requesting_sid: true, ..BinderFeatures::default() }, |
| 51 | )) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame] | 54 | fn on_user_password_changed(user_id: i32, password: Option<Password>) -> Result<()> { |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 55 | //Check permission. Function should return if this failed. Therefore having '?' at the end |
| 56 | //is very important. |
| 57 | check_keystore_permission(KeystorePerm::change_password()) |
| 58 | .context("In on_user_password_changed.")?; |
| 59 | |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 60 | if let Some(pw) = password.as_ref() { |
| 61 | DB.with(|db| { |
Paul Crowley | 8d5b253 | 2021-03-19 10:53:07 -0700 | [diff] [blame] | 62 | SUPER_KEY.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] | 63 | }) |
Paul Crowley | 8d5b253 | 2021-03-19 10:53:07 -0700 | [diff] [blame] | 64 | .context("In on_user_password_changed: unlock_screen_lock_bound_key failed")?; |
Paul Crowley | 7a65839 | 2021-03-18 17:08:20 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 67 | match DB |
| 68 | .with(|db| { |
| 69 | UserState::get_with_password_changed( |
| 70 | &mut db.borrow_mut(), |
Hasini Gunasinghe | 3ed5da7 | 2021-02-04 15:18:54 +0000 | [diff] [blame] | 71 | &LEGACY_MIGRATOR, |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 72 | &SUPER_KEY, |
| 73 | user_id as u32, |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame] | 74 | password.as_ref(), |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 75 | ) |
| 76 | }) |
| 77 | .context("In on_user_password_changed.")? |
| 78 | { |
| 79 | UserState::LskfLocked => { |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 80 | // Error - password can not be changed when the device is locked |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 81 | Err(Error::Rc(ResponseCode::LOCKED)) |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 82 | .context("In on_user_password_changed. Device is locked.") |
| 83 | } |
| 84 | _ => { |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 85 | // LskfLocked is the only error case for password change |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 86 | Ok(()) |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | fn add_or_remove_user(user_id: i32) -> Result<()> { |
| 92 | // Check permission. Function should return if this failed. Therefore having '?' at the end |
| 93 | // is very important. |
| 94 | check_keystore_permission(KeystorePerm::change_user()).context("In add_or_remove_user.")?; |
Janis Danisevskis | eed6984 | 2021-02-18 20:04:10 -0800 | [diff] [blame] | 95 | DB.with(|db| { |
| 96 | UserState::reset_user( |
| 97 | &mut db.borrow_mut(), |
| 98 | &SUPER_KEY, |
| 99 | &LEGACY_MIGRATOR, |
| 100 | user_id as u32, |
| 101 | false, |
| 102 | ) |
| 103 | }) |
| 104 | .context("In add_or_remove_user: Trying to delete keys from db.") |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 105 | } |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 106 | |
| 107 | fn clear_namespace(domain: Domain, nspace: i64) -> Result<()> { |
| 108 | // Permission check. Must return on error. Do not touch the '?'. |
| 109 | check_keystore_permission(KeystorePerm::clear_uid()).context("In clear_namespace.")?; |
| 110 | |
| 111 | LEGACY_MIGRATOR |
| 112 | .bulk_delete_uid(domain, nspace) |
| 113 | .context("In clear_namespace: Trying to delete legacy keys.")?; |
| 114 | DB.with(|db| db.borrow_mut().unbind_keys_for_namespace(domain, nspace)) |
| 115 | .context("In clear_namespace: Trying to delete keys from db.") |
| 116 | } |
Hasini Gunasinghe | 9ee1841 | 2021-03-11 20:12:44 +0000 | [diff] [blame] | 117 | |
| 118 | fn get_state(user_id: i32) -> Result<AidlUserState> { |
| 119 | // Check permission. Function should return if this failed. Therefore having '?' at the end |
| 120 | // is very important. |
| 121 | check_keystore_permission(KeystorePerm::get_state()).context("In get_state.")?; |
| 122 | let state = DB |
| 123 | .with(|db| { |
| 124 | UserState::get(&mut db.borrow_mut(), &LEGACY_MIGRATOR, &SUPER_KEY, user_id as u32) |
| 125 | }) |
| 126 | .context("In get_state. Trying to get UserState.")?; |
| 127 | |
| 128 | match state { |
| 129 | UserState::Uninitialized => Ok(AidlUserState::UNINITIALIZED), |
| 130 | UserState::LskfUnlocked(_) => Ok(AidlUserState::LSKF_UNLOCKED), |
| 131 | UserState::LskfLocked => Ok(AidlUserState::LSKF_LOCKED), |
| 132 | } |
| 133 | } |
Janis Danisevskis | 333b7c0 | 2021-03-23 18:57:41 -0700 | [diff] [blame] | 134 | |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 135 | fn early_boot_ended_help(sec_level: SecurityLevel) -> Result<()> { |
Janis Danisevskis | 5f3a057 | 2021-06-18 11:26:42 -0700 | [diff] [blame^] | 136 | let (km_dev, _, _) = get_keymint_device(&sec_level) |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 137 | .context("In early_boot_ended: getting keymint device")?; |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 138 | |
| 139 | let _wp = wd::watch_millis_with( |
| 140 | "In early_boot_ended_help: calling earlyBootEnded()", |
| 141 | 500, |
| 142 | move || format!("Seclevel: {:?}", sec_level), |
| 143 | ); |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 144 | map_km_error(km_dev.earlyBootEnded()) |
| 145 | .context("In keymint device: calling earlyBootEnded")?; |
| 146 | Ok(()) |
| 147 | } |
| 148 | |
| 149 | fn early_boot_ended() -> Result<()> { |
| 150 | check_keystore_permission(KeystorePerm::early_boot_ended()) |
| 151 | .context("In early_boot_ended. Checking permission")?; |
Paul Crowley | 44c02da | 2021-04-08 17:04:43 +0000 | [diff] [blame] | 152 | log::info!("In early_boot_ended."); |
| 153 | |
| 154 | if let Err(e) = DB.with(|db| SUPER_KEY.set_up_boot_level_cache(&mut db.borrow_mut())) { |
| 155 | log::error!("SUPER_KEY.set_up_boot_level_cache failed:\n{:?}\n:(", e); |
| 156 | } |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 157 | |
| 158 | let sec_levels = [ |
| 159 | (SecurityLevel::TRUSTED_ENVIRONMENT, "TRUSTED_ENVIRONMENT"), |
| 160 | (SecurityLevel::STRONGBOX, "STRONGBOX"), |
| 161 | ]; |
| 162 | sec_levels.iter().fold(Ok(()), |result, (sec_level, sec_level_string)| { |
Janis Danisevskis | 2ee014b | 2021-05-05 14:29:08 -0700 | [diff] [blame] | 163 | let curr_result = Maintenance::early_boot_ended_help(*sec_level); |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 164 | if curr_result.is_err() { |
| 165 | log::error!( |
| 166 | "Call to earlyBootEnded failed for security level {}.", |
| 167 | &sec_level_string |
| 168 | ); |
| 169 | } |
| 170 | result.and(curr_result) |
| 171 | }) |
| 172 | } |
| 173 | |
Janis Danisevskis | 333b7c0 | 2021-03-23 18:57:41 -0700 | [diff] [blame] | 174 | fn on_device_off_body() -> Result<()> { |
| 175 | // Security critical permission check. This statement must return on fail. |
| 176 | check_keystore_permission(KeystorePerm::report_off_body()) |
| 177 | .context("In on_device_off_body.")?; |
| 178 | |
Matthew Maurer | d7815ca | 2021-05-06 21:58:45 -0700 | [diff] [blame] | 179 | DB.with(|db| db.borrow_mut().update_last_off_body(MonotonicRawTime::now())); |
| 180 | Ok(()) |
Janis Danisevskis | 333b7c0 | 2021-03-23 18:57:41 -0700 | [diff] [blame] | 181 | } |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 182 | |
| 183 | fn migrate_key_namespace(source: &KeyDescriptor, destination: &KeyDescriptor) -> Result<()> { |
| 184 | let caller_uid = ThreadState::get_calling_uid(); |
| 185 | |
| 186 | DB.with(|db| { |
| 187 | let key_id_guard = match source.domain { |
| 188 | Domain::APP | Domain::SELINUX | Domain::KEY_ID => { |
| 189 | let (key_id_guard, _) = LEGACY_MIGRATOR |
| 190 | .with_try_migrate(&source, caller_uid, || { |
| 191 | db.borrow_mut().load_key_entry( |
| 192 | &source, |
| 193 | KeyType::Client, |
| 194 | KeyEntryLoadBits::NONE, |
| 195 | caller_uid, |
| 196 | |k, av| { |
| 197 | check_key_permission(KeyPerm::use_(), k, &av)?; |
| 198 | check_key_permission(KeyPerm::delete(), k, &av)?; |
| 199 | check_key_permission(KeyPerm::grant(), k, &av) |
| 200 | }, |
| 201 | ) |
| 202 | }) |
| 203 | .context("In migrate_key_namespace: Failed to load key blob.")?; |
| 204 | key_id_guard |
| 205 | } |
| 206 | _ => { |
| 207 | return Err(Error::Rc(ResponseCode::INVALID_ARGUMENT)).context(concat!( |
| 208 | "In migrate_key_namespace: ", |
| 209 | "Source domain must be one of APP, SELINUX, or KEY_ID." |
| 210 | )) |
| 211 | } |
| 212 | }; |
| 213 | |
| 214 | db.borrow_mut().migrate_key_namespace(key_id_guard, destination, caller_uid, |k| { |
| 215 | check_key_permission(KeyPerm::rebind(), k, &None) |
| 216 | }) |
| 217 | }) |
| 218 | } |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 221 | impl Interface for Maintenance {} |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 222 | |
Janis Danisevskis | 34a0cf2 | 2021-03-08 09:19:03 -0800 | [diff] [blame] | 223 | impl IKeystoreMaintenance for Maintenance { |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 224 | fn onUserPasswordChanged(&self, user_id: i32, password: Option<&[u8]>) -> BinderResult<()> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 225 | let _wp = wd::watch_millis("IKeystoreMaintenance::onUserPasswordChanged", 500); |
Paul Crowley | f61fee7 | 2021-03-17 14:38:44 -0700 | [diff] [blame] | 226 | 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] | 227 | } |
| 228 | |
| 229 | fn onUserAdded(&self, user_id: i32) -> BinderResult<()> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 230 | let _wp = wd::watch_millis("IKeystoreMaintenance::onUserAdded", 500); |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 231 | map_or_log_err(Self::add_or_remove_user(user_id), Ok) |
| 232 | } |
| 233 | |
| 234 | fn onUserRemoved(&self, user_id: i32) -> BinderResult<()> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 235 | let _wp = wd::watch_millis("IKeystoreMaintenance::onUserRemoved", 500); |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 236 | map_or_log_err(Self::add_or_remove_user(user_id), Ok) |
| 237 | } |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 238 | |
| 239 | fn clearNamespace(&self, domain: Domain, nspace: i64) -> BinderResult<()> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 240 | let _wp = wd::watch_millis("IKeystoreMaintenance::clearNamespace", 500); |
Janis Danisevskis | ddd6e75 | 2021-02-22 18:46:55 -0800 | [diff] [blame] | 241 | map_or_log_err(Self::clear_namespace(domain, nspace), Ok) |
| 242 | } |
Hasini Gunasinghe | 9ee1841 | 2021-03-11 20:12:44 +0000 | [diff] [blame] | 243 | |
Janis Danisevskis | 333b7c0 | 2021-03-23 18:57:41 -0700 | [diff] [blame] | 244 | fn getState(&self, user_id: i32) -> BinderResult<AidlUserState> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 245 | let _wp = wd::watch_millis("IKeystoreMaintenance::getState", 500); |
Hasini Gunasinghe | 9ee1841 | 2021-03-11 20:12:44 +0000 | [diff] [blame] | 246 | map_or_log_err(Self::get_state(user_id), Ok) |
| 247 | } |
Janis Danisevskis | 333b7c0 | 2021-03-23 18:57:41 -0700 | [diff] [blame] | 248 | |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 249 | fn earlyBootEnded(&self) -> BinderResult<()> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 250 | let _wp = wd::watch_millis("IKeystoreMaintenance::earlyBootEnded", 500); |
Satya Tangirala | 5b9e5b1 | 2021-03-09 12:54:21 -0800 | [diff] [blame] | 251 | map_or_log_err(Self::early_boot_ended(), Ok) |
| 252 | } |
| 253 | |
Janis Danisevskis | 333b7c0 | 2021-03-23 18:57:41 -0700 | [diff] [blame] | 254 | fn onDeviceOffBody(&self) -> BinderResult<()> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 255 | let _wp = wd::watch_millis("IKeystoreMaintenance::onDeviceOffBody", 500); |
Janis Danisevskis | 333b7c0 | 2021-03-23 18:57:41 -0700 | [diff] [blame] | 256 | map_or_log_err(Self::on_device_off_body(), Ok) |
| 257 | } |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 258 | |
| 259 | fn migrateKeyNamespace( |
| 260 | &self, |
| 261 | source: &KeyDescriptor, |
| 262 | destination: &KeyDescriptor, |
| 263 | ) -> BinderResult<()> { |
Hasini Gunasinghe | 5a893e8 | 2021-05-05 14:32:32 +0000 | [diff] [blame] | 264 | let _wp = wd::watch_millis("IKeystoreMaintenance::migrateKeyNamespace", 500); |
Janis Danisevskis | cdcf4e5 | 2021-04-14 15:44:36 -0700 | [diff] [blame] | 265 | map_or_log_err(Self::migrate_key_namespace(source, destination), Ok) |
| 266 | } |
Hasini Gunasinghe | da89555 | 2021-01-27 19:34:37 +0000 | [diff] [blame] | 267 | } |