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