1 // Copyright 2020, 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 15 //! This crate implements the `IKeystoreOperation` AIDL interface, which represents 16 //! an ongoing key operation, as well as the operation database, which is mainly 17 //! required for tracking operations for the purpose of pruning. 18 //! This crate also implements an operation pruning strategy. 19 //! 20 //! Operations implement the API calls update, finish, and abort. 21 //! Additionally, an operation can be dropped and pruned. The former 22 //! happens if the client deletes a binder to the operation object. 23 //! An existing operation may get pruned when running out of operation 24 //! slots and a new operation takes precedence. 25 //! 26 //! ## Operation Lifecycle 27 //! An operation gets created when the client calls `IKeystoreSecurityLevel::create`. 28 //! It may receive zero or more update request. The lifecycle ends when: 29 //! * `update` yields an error. 30 //! * `finish` is called. 31 //! * `abort` is called. 32 //! * The operation gets dropped. 33 //! * The operation gets pruned. 34 //! `Operation` has an `Outcome` member. While the outcome is `Outcome::Unknown`, 35 //! the operation is active and in a good state. Any of the above conditions may 36 //! change the outcome to one of the defined outcomes Success, Abort, Dropped, 37 //! Pruned, or ErrorCode. The latter is chosen in the case of an unexpected error, during 38 //! `update` or `finish`. `Success` is chosen iff `finish` completes without error. 39 //! Note that all operations get dropped eventually in the sense that they lose 40 //! their last reference and get destroyed. At that point, the fate of the operation 41 //! gets logged. However, an operation will transition to `Outcome::Dropped` iff 42 //! the operation was still active (`Outcome::Unknown`) at that time. 43 //! 44 //! ## Operation Dropping 45 //! To observe the dropping of an operation, we have to make sure that there 46 //! are no strong references to the IBinder representing this operation. 47 //! This would be simple enough if the operation object would need to be accessed 48 //! only by transactions. But to perform pruning, we have to retain a reference to the 49 //! original operation object. 50 //! 51 //! ## Operation Pruning 52 //! Pruning an operation happens during the creation of a new operation. 53 //! We have to iterate through the operation database to find a suitable 54 //! candidate. Then we abort and finalize this operation setting its outcome to 55 //! `Outcome::Pruned`. The corresponding KeyMint operation slot will have been freed 56 //! up at this point, but the `Operation` object lingers. When the client 57 //! attempts to use the operation again they will receive 58 //! ErrorCode::INVALID_OPERATION_HANDLE indicating that the operation no longer 59 //! exits. This should be the cue for the client to destroy its binder. 60 //! At that point the operation gets dropped. 61 //! 62 //! ## Architecture 63 //! The `IKeystoreOperation` trait is implemented by `KeystoreOperation`. 64 //! This acts as a proxy object holding a strong reference to actual operation 65 //! implementation `Operation`. 66 //! 67 //! ``` 68 //! struct KeystoreOperation { 69 //! operation: Mutex<Option<Arc<Operation>>>, 70 //! } 71 //! ``` 72 //! 73 //! The `Mutex` serves two purposes. It provides interior mutability allowing 74 //! us to set the Option to None. We do this when the life cycle ends during 75 //! a call to `update`, `finish`, or `abort`. As a result most of the Operation 76 //! related resources are freed. The `KeystoreOperation` proxy object still 77 //! lingers until dropped by the client. 78 //! The second purpose is to protect operations against concurrent usage. 79 //! Failing to lock this mutex yields `ResponseCode::OPERATION_BUSY` and indicates 80 //! a programming error in the client. 81 //! 82 //! Note that the Mutex only protects the operation against concurrent client calls. 83 //! We still retain weak references to the operation in the operation database: 84 //! 85 //! ``` 86 //! struct OperationDb { 87 //! operations: Mutex<Vec<Weak<Operation>>> 88 //! } 89 //! ``` 90 //! 91 //! This allows us to access the operations for the purpose of pruning. 92 //! We do this in three phases. 93 //! 1. We gather the pruning information. Besides non mutable information, 94 //! we access `last_usage` which is protected by a mutex. 95 //! We only lock this mutex for single statements at a time. During 96 //! this phase we hold the operation db lock. 97 //! 2. We choose a pruning candidate by computing the pruning resistance 98 //! of each operation. We do this entirely with information we now 99 //! have on the stack without holding any locks. 100 //! (See `OperationDb::prune` for more details on the pruning strategy.) 101 //! 3. During pruning we briefly lock the operation database again to get the 102 //! the pruning candidate by index. We then attempt to abort the candidate. 103 //! If the candidate was touched in the meantime or is currently fulfilling 104 //! a request (i.e., the client calls update, finish, or abort), 105 //! we go back to 1 and try again. 106 //! 107 //! So the outer Mutex in `KeystoreOperation::operation` only protects 108 //! operations against concurrent client calls but not against concurrent 109 //! pruning attempts. This is what the `Operation::outcome` mutex is used for. 110 //! 111 //! ``` 112 //! struct Operation { 113 //! ... 114 //! outcome: Mutex<Outcome>, 115 //! ... 116 //! } 117 //! ``` 118 //! 119 //! Any request that can change the outcome, i.e., `update`, `finish`, `abort`, 120 //! `drop`, and `prune` has to take the outcome lock and check if the outcome 121 //! is still `Outcome::Unknown` before entering. `prune` is special in that 122 //! it will `try_lock`, because we don't want to be blocked on a potentially 123 //! long running request at another operation. If it fails to get the lock 124 //! the operation is either being touched, which changes its pruning resistance, 125 //! or it transitions to its end-of-life, which means we may get a free slot. 126 //! Either way, we have to revaluate the pruning scores. 127 128 use crate::enforcements::AuthInfo; 129 use crate::error::{map_err_with, map_km_error, map_or_log_err, Error, ErrorCode, ResponseCode}; 130 use crate::metrics_store::log_key_operation_event_stats; 131 use crate::utils::watchdog as wd; 132 use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ 133 IKeyMintOperation::IKeyMintOperation, KeyParameter::KeyParameter, KeyPurpose::KeyPurpose, 134 SecurityLevel::SecurityLevel, 135 }; 136 use android_hardware_security_keymint::binder::{BinderFeatures, Strong}; 137 use android_system_keystore2::aidl::android::system::keystore2::{ 138 IKeystoreOperation::BnKeystoreOperation, IKeystoreOperation::IKeystoreOperation, 139 }; 140 use anyhow::{anyhow, Context, Result}; 141 use std::{ 142 collections::HashMap, 143 sync::{Arc, Mutex, MutexGuard, Weak}, 144 time::Duration, 145 time::Instant, 146 }; 147 148 /// Operations have `Outcome::Unknown` as long as they are active. They transition 149 /// to one of the other variants exactly once. The distinction in outcome is mainly 150 /// for the statistic. 151 #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] 152 pub enum Outcome { 153 /// Operations have `Outcome::Unknown` as long as they are active. 154 Unknown, 155 /// Operation is successful. 156 Success, 157 /// Operation is aborted. 158 Abort, 159 /// Operation is dropped. 160 Dropped, 161 /// Operation is pruned. 162 Pruned, 163 /// Operation is failed with the error code. 164 ErrorCode(ErrorCode), 165 } 166 167 /// Operation bundles all of the operation related resources and tracks the operation's 168 /// outcome. 169 #[derive(Debug)] 170 pub struct Operation { 171 // The index of this operation in the OperationDb. 172 index: usize, 173 km_op: Strong<dyn IKeyMintOperation>, 174 last_usage: Mutex<Instant>, 175 outcome: Mutex<Outcome>, 176 owner: u32, // Uid of the operation's owner. 177 auth_info: Mutex<AuthInfo>, 178 forced: bool, 179 logging_info: LoggingInfo, 180 } 181 182 /// Keeps track of the information required for logging operations. 183 #[derive(Debug)] 184 pub struct LoggingInfo { 185 sec_level: SecurityLevel, 186 purpose: KeyPurpose, 187 op_params: Vec<KeyParameter>, 188 key_upgraded: bool, 189 } 190 191 impl LoggingInfo { 192 /// Constructor new( sec_level: SecurityLevel, purpose: KeyPurpose, op_params: Vec<KeyParameter>, key_upgraded: bool, ) -> LoggingInfo193 pub fn new( 194 sec_level: SecurityLevel, 195 purpose: KeyPurpose, 196 op_params: Vec<KeyParameter>, 197 key_upgraded: bool, 198 ) -> LoggingInfo { 199 Self { sec_level, purpose, op_params, key_upgraded } 200 } 201 } 202 203 struct PruningInfo { 204 last_usage: Instant, 205 owner: u32, 206 index: usize, 207 forced: bool, 208 } 209 210 // We don't except more than 32KiB of data in `update`, `updateAad`, and `finish`. 211 const MAX_RECEIVE_DATA: usize = 0x8000; 212 213 impl Operation { 214 /// Constructor new( index: usize, km_op: binder::Strong<dyn IKeyMintOperation>, owner: u32, auth_info: AuthInfo, forced: bool, logging_info: LoggingInfo, ) -> Self215 pub fn new( 216 index: usize, 217 km_op: binder::Strong<dyn IKeyMintOperation>, 218 owner: u32, 219 auth_info: AuthInfo, 220 forced: bool, 221 logging_info: LoggingInfo, 222 ) -> Self { 223 Self { 224 index, 225 km_op, 226 last_usage: Mutex::new(Instant::now()), 227 outcome: Mutex::new(Outcome::Unknown), 228 owner, 229 auth_info: Mutex::new(auth_info), 230 forced, 231 logging_info, 232 } 233 } 234 get_pruning_info(&self) -> Option<PruningInfo>235 fn get_pruning_info(&self) -> Option<PruningInfo> { 236 // An operation may be finalized. 237 if let Ok(guard) = self.outcome.try_lock() { 238 match *guard { 239 Outcome::Unknown => {} 240 // If the outcome is any other than unknown, it has been finalized, 241 // and we can no longer consider it for pruning. 242 _ => return None, 243 } 244 } 245 // Else: If we could not grab the lock, this means that the operation is currently 246 // being used and it may be transitioning to finalized or it was simply updated. 247 // In any case it is fair game to consider it for pruning. If the operation 248 // transitioned to a final state, we will notice when we attempt to prune, and 249 // a subsequent attempt to create a new operation will succeed. 250 Some(PruningInfo { 251 // Expect safety: 252 // `last_usage` is locked only for primitive single line statements. 253 // There is no chance to panic and poison the mutex. 254 last_usage: *self.last_usage.lock().expect("In get_pruning_info."), 255 owner: self.owner, 256 index: self.index, 257 forced: self.forced, 258 }) 259 } 260 prune(&self, last_usage: Instant) -> Result<(), Error>261 fn prune(&self, last_usage: Instant) -> Result<(), Error> { 262 let mut locked_outcome = match self.outcome.try_lock() { 263 Ok(guard) => match *guard { 264 Outcome::Unknown => guard, 265 _ => return Err(Error::Km(ErrorCode::INVALID_OPERATION_HANDLE)), 266 }, 267 Err(_) => return Err(Error::Rc(ResponseCode::OPERATION_BUSY)), 268 }; 269 270 // In `OperationDb::prune`, which is our caller, we first gather the pruning 271 // information including the last usage. When we select a candidate 272 // we call `prune` on that candidate passing the last_usage 273 // that we gathered earlier. If the actual last usage 274 // has changed since than, it means the operation was busy in the 275 // meantime, which means that we have to reevaluate the pruning score. 276 // 277 // Expect safety: 278 // `last_usage` is locked only for primitive single line statements. 279 // There is no chance to panic and poison the mutex. 280 if *self.last_usage.lock().expect("In Operation::prune()") != last_usage { 281 return Err(Error::Rc(ResponseCode::OPERATION_BUSY)); 282 } 283 *locked_outcome = Outcome::Pruned; 284 285 let _wp = wd::watch_millis("In Operation::prune: calling abort()", 500); 286 287 // We abort the operation. If there was an error we log it but ignore it. 288 if let Err(e) = map_km_error(self.km_op.abort()) { 289 log::error!("In prune: KeyMint::abort failed with {:?}.", e); 290 } 291 292 Ok(()) 293 } 294 295 // This function takes a Result from a KeyMint call and inspects it for errors. 296 // If an error was found it updates the given `locked_outcome` accordingly. 297 // It forwards the Result unmodified. 298 // The precondition to this call must be *locked_outcome == Outcome::Unknown. 299 // Ideally the `locked_outcome` came from a successful call to `check_active` 300 // see below. update_outcome<T>( &self, locked_outcome: &mut Outcome, err: Result<T, Error>, ) -> Result<T, Error>301 fn update_outcome<T>( 302 &self, 303 locked_outcome: &mut Outcome, 304 err: Result<T, Error>, 305 ) -> Result<T, Error> { 306 match &err { 307 Err(Error::Km(e)) => *locked_outcome = Outcome::ErrorCode(*e), 308 Err(_) => *locked_outcome = Outcome::ErrorCode(ErrorCode::UNKNOWN_ERROR), 309 Ok(_) => (), 310 } 311 err 312 } 313 314 // This function grabs the outcome lock and checks the current outcome state. 315 // If the outcome is still `Outcome::Unknown`, this function returns 316 // the locked outcome for further updates. In any other case it returns 317 // ErrorCode::INVALID_OPERATION_HANDLE indicating that this operation has 318 // been finalized and is no longer active. check_active(&self) -> Result<MutexGuard<Outcome>>319 fn check_active(&self) -> Result<MutexGuard<Outcome>> { 320 let guard = self.outcome.lock().expect("In check_active."); 321 match *guard { 322 Outcome::Unknown => Ok(guard), 323 _ => Err(Error::Km(ErrorCode::INVALID_OPERATION_HANDLE)).context(format!( 324 "In check_active: Call on finalized operation with outcome: {:?}.", 325 *guard 326 )), 327 } 328 } 329 330 // This function checks the amount of input data sent to us. We reject any buffer 331 // exceeding MAX_RECEIVE_DATA bytes as input to `update`, `update_aad`, and `finish` 332 // in order to force clients into using reasonable limits. check_input_length(data: &[u8]) -> Result<()>333 fn check_input_length(data: &[u8]) -> Result<()> { 334 if data.len() > MAX_RECEIVE_DATA { 335 // This error code is unique, no context required here. 336 return Err(anyhow!(Error::Rc(ResponseCode::TOO_MUCH_DATA))); 337 } 338 Ok(()) 339 } 340 341 // Update the last usage to now. touch(&self)342 fn touch(&self) { 343 // Expect safety: 344 // `last_usage` is locked only for primitive single line statements. 345 // There is no chance to panic and poison the mutex. 346 *self.last_usage.lock().expect("In touch.") = Instant::now(); 347 } 348 349 /// Implementation of `IKeystoreOperation::updateAad`. 350 /// Refer to the AIDL spec at system/hardware/interfaces/keystore2 for details. update_aad(&self, aad_input: &[u8]) -> Result<()>351 fn update_aad(&self, aad_input: &[u8]) -> Result<()> { 352 let mut outcome = self.check_active().context("In update_aad")?; 353 Self::check_input_length(aad_input).context("In update_aad")?; 354 self.touch(); 355 356 let (hat, tst) = self 357 .auth_info 358 .lock() 359 .unwrap() 360 .before_update() 361 .context("In update_aad: Trying to get auth tokens.")?; 362 363 self.update_outcome(&mut *outcome, { 364 let _wp = wd::watch_millis("Operation::update_aad: calling updateAad", 500); 365 map_km_error(self.km_op.updateAad(aad_input, hat.as_ref(), tst.as_ref())) 366 }) 367 .context("In update_aad: KeyMint::update failed.")?; 368 369 Ok(()) 370 } 371 372 /// Implementation of `IKeystoreOperation::update`. 373 /// Refer to the AIDL spec at system/hardware/interfaces/keystore2 for details. update(&self, input: &[u8]) -> Result<Option<Vec<u8>>>374 fn update(&self, input: &[u8]) -> Result<Option<Vec<u8>>> { 375 let mut outcome = self.check_active().context("In update")?; 376 Self::check_input_length(input).context("In update")?; 377 self.touch(); 378 379 let (hat, tst) = self 380 .auth_info 381 .lock() 382 .unwrap() 383 .before_update() 384 .context("In update: Trying to get auth tokens.")?; 385 386 let output = self 387 .update_outcome(&mut *outcome, { 388 let _wp = wd::watch_millis("Operation::update: calling update", 500); 389 map_km_error(self.km_op.update(input, hat.as_ref(), tst.as_ref())) 390 }) 391 .context("In update: KeyMint::update failed.")?; 392 393 if output.is_empty() { 394 Ok(None) 395 } else { 396 Ok(Some(output)) 397 } 398 } 399 400 /// Implementation of `IKeystoreOperation::finish`. 401 /// Refer to the AIDL spec at system/hardware/interfaces/keystore2 for details. finish(&self, input: Option<&[u8]>, signature: Option<&[u8]>) -> Result<Option<Vec<u8>>>402 fn finish(&self, input: Option<&[u8]>, signature: Option<&[u8]>) -> Result<Option<Vec<u8>>> { 403 let mut outcome = self.check_active().context("In finish")?; 404 if let Some(input) = input { 405 Self::check_input_length(input).context("In finish")?; 406 } 407 self.touch(); 408 409 let (hat, tst, confirmation_token) = self 410 .auth_info 411 .lock() 412 .unwrap() 413 .before_finish() 414 .context("In finish: Trying to get auth tokens.")?; 415 416 let output = self 417 .update_outcome(&mut *outcome, { 418 let _wp = wd::watch_millis("Operation::finish: calling finish", 500); 419 map_km_error(self.km_op.finish( 420 input, 421 signature, 422 hat.as_ref(), 423 tst.as_ref(), 424 confirmation_token.as_deref(), 425 )) 426 }) 427 .context("In finish: KeyMint::finish failed.")?; 428 429 self.auth_info.lock().unwrap().after_finish().context("In finish.")?; 430 431 // At this point the operation concluded successfully. 432 *outcome = Outcome::Success; 433 434 if output.is_empty() { 435 Ok(None) 436 } else { 437 Ok(Some(output)) 438 } 439 } 440 441 /// Aborts the operation if it is active. IFF the operation is aborted the outcome is 442 /// set to `outcome`. `outcome` must reflect the reason for the abort. Since the operation 443 /// gets aborted `outcome` must not be `Operation::Success` or `Operation::Unknown`. abort(&self, outcome: Outcome) -> Result<()>444 fn abort(&self, outcome: Outcome) -> Result<()> { 445 let mut locked_outcome = self.check_active().context("In abort")?; 446 *locked_outcome = outcome; 447 448 { 449 let _wp = wd::watch_millis("Operation::abort: calling abort", 500); 450 map_km_error(self.km_op.abort()).context("In abort: KeyMint::abort failed.") 451 } 452 } 453 } 454 455 impl Drop for Operation { drop(&mut self)456 fn drop(&mut self) { 457 let guard = self.outcome.lock().expect("In drop."); 458 log_key_operation_event_stats( 459 self.logging_info.sec_level, 460 self.logging_info.purpose, 461 &(self.logging_info.op_params), 462 &guard, 463 self.logging_info.key_upgraded, 464 ); 465 if let Outcome::Unknown = *guard { 466 drop(guard); 467 // If the operation was still active we call abort, setting 468 // the outcome to `Outcome::Dropped` 469 if let Err(e) = self.abort(Outcome::Dropped) { 470 log::error!("While dropping Operation: abort failed:\n {:?}", e); 471 } 472 } 473 } 474 } 475 476 /// The OperationDb holds weak references to all ongoing operations. 477 /// Its main purpose is to facilitate operation pruning. 478 #[derive(Debug, Default)] 479 pub struct OperationDb { 480 // TODO replace Vec with WeakTable when the weak_table crate becomes 481 // available. 482 operations: Mutex<Vec<Weak<Operation>>>, 483 } 484 485 impl OperationDb { 486 /// Creates a new OperationDb. new() -> Self487 pub fn new() -> Self { 488 Self { operations: Mutex::new(Vec::new()) } 489 } 490 491 /// Creates a new operation. 492 /// This function takes a KeyMint operation and an associated 493 /// owner uid and returns a new Operation wrapped in a `std::sync::Arc`. create_operation( &self, km_op: binder::Strong<dyn IKeyMintOperation>, owner: u32, auth_info: AuthInfo, forced: bool, logging_info: LoggingInfo, ) -> Arc<Operation>494 pub fn create_operation( 495 &self, 496 km_op: binder::Strong<dyn IKeyMintOperation>, 497 owner: u32, 498 auth_info: AuthInfo, 499 forced: bool, 500 logging_info: LoggingInfo, 501 ) -> Arc<Operation> { 502 // We use unwrap because we don't allow code that can panic while locked. 503 let mut operations = self.operations.lock().expect("In create_operation."); 504 505 let mut index: usize = 0; 506 // First we iterate through the operation slots to try and find an unused 507 // slot. If we don't find one, we append the new entry instead. 508 match (*operations).iter_mut().find(|s| { 509 index += 1; 510 s.upgrade().is_none() 511 }) { 512 Some(free_slot) => { 513 let new_op = Arc::new(Operation::new( 514 index - 1, 515 km_op, 516 owner, 517 auth_info, 518 forced, 519 logging_info, 520 )); 521 *free_slot = Arc::downgrade(&new_op); 522 new_op 523 } 524 None => { 525 let new_op = Arc::new(Operation::new( 526 operations.len(), 527 km_op, 528 owner, 529 auth_info, 530 forced, 531 logging_info, 532 )); 533 operations.push(Arc::downgrade(&new_op)); 534 new_op 535 } 536 } 537 } 538 get(&self, index: usize) -> Option<Arc<Operation>>539 fn get(&self, index: usize) -> Option<Arc<Operation>> { 540 self.operations.lock().expect("In OperationDb::get.").get(index).and_then(|op| op.upgrade()) 541 } 542 543 /// Attempts to prune an operation. 544 /// 545 /// This function is used during operation creation, i.e., by 546 /// `KeystoreSecurityLevel::create_operation`, to try and free up an operation slot 547 /// if it got `ErrorCode::TOO_MANY_OPERATIONS` from the KeyMint backend. It is not 548 /// guaranteed that an operation slot is available after this call successfully 549 /// returned for various reasons. E.g., another thread may have snatched up the newly 550 /// available slot. Callers may have to call prune multiple times before they get a 551 /// free operation slot. Prune may also return `Err(Error::Rc(ResponseCode::BACKEND_BUSY))` 552 /// which indicates that no prunable operation was found. 553 /// 554 /// To find a suitable candidate we compute the malus for the caller and each existing 555 /// operation. The malus is the inverse of the pruning power (caller) or pruning 556 /// resistance (existing operation). 557 /// 558 /// The malus is based on the number of sibling operations and age. Sibling 559 /// operations are operations that have the same owner (UID). 560 /// 561 /// Every operation, existing or new, starts with a malus of 1. Every sibling 562 /// increases the malus by one. The age is the time since an operation was last touched. 563 /// It increases the malus by log6(<age in seconds> + 1) rounded down to the next 564 /// integer. So the malus increases stepwise after 5s, 35s, 215s, ... 565 /// Of two operations with the same malus the least recently used one is considered 566 /// weaker. 567 /// 568 /// For the caller to be able to prune an operation it must find an operation 569 /// with a malus higher than its own. 570 /// 571 /// The malus can be expressed as 572 /// ``` 573 /// malus = 1 + no_of_siblings + floor(log6(age_in_seconds + 1)) 574 /// ``` 575 /// where the constant `1` accounts for the operation under consideration. 576 /// In reality we compute it as 577 /// ``` 578 /// caller_malus = 1 + running_siblings 579 /// ``` 580 /// because the new operation has no age and is not included in the `running_siblings`, 581 /// and 582 /// ``` 583 /// running_malus = running_siblings + floor(log6(age_in_seconds + 1)) 584 /// ``` 585 /// because a running operation is included in the `running_siblings` and it has 586 /// an age. 587 /// 588 /// ## Example 589 /// A caller with no running operations has a malus of 1. Young (age < 5s) operations 590 /// also with no siblings have a malus of one and cannot be pruned by the caller. 591 /// We have to find an operation that has at least one sibling or is older than 5s. 592 /// 593 /// A caller with one running operation has a malus of 2. Now even young siblings 594 /// or single child aging (5s <= age < 35s) operations are off limit. An aging 595 /// sibling of two, however, would have a malus of 3 and would be fair game. 596 /// 597 /// ## Rationale 598 /// Due to the limitation of KeyMint operation slots, we cannot get around pruning or 599 /// a single app could easily DoS KeyMint. 600 /// Keystore 1.0 used to always prune the least recently used operation. This at least 601 /// guaranteed that new operations can always be started. With the increased usage 602 /// of Keystore we saw increased pruning activity which can lead to a livelock 603 /// situation in the worst case. 604 /// 605 /// With the new pruning strategy we want to provide well behaved clients with 606 /// progress assurances while punishing DoS attempts. As a result of this 607 /// strategy we can be in the situation where no operation can be pruned and the 608 /// creation of a new operation fails. This allows single child operations which 609 /// are frequently updated to complete, thereby breaking up livelock situations 610 /// and facilitating system wide progress. 611 /// 612 /// ## Update 613 /// We also allow callers to cannibalize their own sibling operations if no other 614 /// slot can be found. In this case the least recently used sibling is pruned. prune(&self, caller: u32, forced: bool) -> Result<(), Error>615 pub fn prune(&self, caller: u32, forced: bool) -> Result<(), Error> { 616 loop { 617 // Maps the uid of the owner to the number of operations that owner has 618 // (running_siblings). More operations per owner lowers the pruning 619 // resistance of the operations of that owner. Whereas the number of 620 // ongoing operations of the caller lowers the pruning power of the caller. 621 let mut owners: HashMap<u32, u64> = HashMap::new(); 622 let mut pruning_info: Vec<PruningInfo> = Vec::new(); 623 624 let now = Instant::now(); 625 self.operations 626 .lock() 627 .expect("In OperationDb::prune: Trying to lock self.operations.") 628 .iter() 629 .for_each(|op| { 630 if let Some(op) = op.upgrade() { 631 if let Some(p_info) = op.get_pruning_info() { 632 let owner = p_info.owner; 633 pruning_info.push(p_info); 634 // Count operations per owner. 635 *owners.entry(owner).or_insert(0) += 1; 636 } 637 } 638 }); 639 640 // If the operation is forced, the caller has a malus of 0. 641 let caller_malus = if forced { 0 } else { 1u64 + *owners.entry(caller).or_default() }; 642 643 // We iterate through all operations computing the malus and finding 644 // the candidate with the highest malus which must also be higher 645 // than the caller_malus. 646 struct CandidateInfo { 647 index: usize, 648 malus: u64, 649 last_usage: Instant, 650 age: Duration, 651 } 652 let mut oldest_caller_op: Option<CandidateInfo> = None; 653 let candidate = pruning_info.iter().fold( 654 None, 655 |acc: Option<CandidateInfo>, &PruningInfo { last_usage, owner, index, forced }| { 656 // Compute the age of the current operation. 657 let age = now 658 .checked_duration_since(last_usage) 659 .unwrap_or_else(|| Duration::new(0, 0)); 660 661 // Find the least recently used sibling as an alternative pruning candidate. 662 if owner == caller { 663 if let Some(CandidateInfo { age: a, .. }) = oldest_caller_op { 664 if age > a { 665 oldest_caller_op = 666 Some(CandidateInfo { index, malus: 0, last_usage, age }); 667 } 668 } else { 669 oldest_caller_op = 670 Some(CandidateInfo { index, malus: 0, last_usage, age }); 671 } 672 } 673 674 // Compute the malus of the current operation. 675 let malus = if forced { 676 // Forced operations have a malus of 0. And cannot even be pruned 677 // by other forced operations. 678 0 679 } else { 680 // Expect safety: Every owner in pruning_info was counted in 681 // the owners map. So this unwrap cannot panic. 682 *owners.get(&owner).expect( 683 "This is odd. We should have counted every owner in pruning_info.", 684 ) + ((age.as_secs() + 1) as f64).log(6.0).floor() as u64 685 }; 686 687 // Now check if the current operation is a viable/better candidate 688 // the one currently stored in the accumulator. 689 match acc { 690 // First we have to find any operation that is prunable by the caller. 691 None => { 692 if caller_malus < malus { 693 Some(CandidateInfo { index, malus, last_usage, age }) 694 } else { 695 None 696 } 697 } 698 // If we have found one we look for the operation with the worst score. 699 // If there is a tie, the older operation is considered weaker. 700 Some(CandidateInfo { index: i, malus: m, last_usage: l, age: a }) => { 701 if malus > m || (malus == m && age > a) { 702 Some(CandidateInfo { index, malus, last_usage, age }) 703 } else { 704 Some(CandidateInfo { index: i, malus: m, last_usage: l, age: a }) 705 } 706 } 707 } 708 }, 709 ); 710 711 // If we did not find a suitable candidate we may cannibalize our oldest sibling. 712 let candidate = candidate.or(oldest_caller_op); 713 714 match candidate { 715 Some(CandidateInfo { index, malus: _, last_usage, age: _ }) => { 716 match self.get(index) { 717 Some(op) => { 718 match op.prune(last_usage) { 719 // We successfully freed up a slot. 720 Ok(()) => break Ok(()), 721 // This means the operation we tried to prune was on its way 722 // out. It also means that the slot it had occupied was freed up. 723 Err(Error::Km(ErrorCode::INVALID_OPERATION_HANDLE)) => break Ok(()), 724 // This means the operation we tried to prune was currently 725 // servicing a request. There are two options. 726 // * Assume that it was touched, which means that its 727 // pruning resistance increased. In that case we have 728 // to start over and find another candidate. 729 // * Assume that the operation is transitioning to end-of-life. 730 // which means that we got a free slot for free. 731 // If we assume the first but the second is true, we prune 732 // a good operation without need (aggressive approach). 733 // If we assume the second but the first is true, our 734 // caller will attempt to create a new KeyMint operation, 735 // fail with `ErrorCode::TOO_MANY_OPERATIONS`, and call 736 // us again (conservative approach). 737 Err(Error::Rc(ResponseCode::OPERATION_BUSY)) => { 738 // We choose the conservative approach, because 739 // every needlessly pruned operation can impact 740 // the user experience. 741 // To switch to the aggressive approach replace 742 // the following line with `continue`. 743 break Ok(()); 744 } 745 746 // The candidate may have been touched so the score 747 // has changed since our evaluation. 748 _ => continue, 749 } 750 } 751 // This index does not exist any more. The operation 752 // in this slot was dropped. Good news, a slot 753 // has freed up. 754 None => break Ok(()), 755 } 756 } 757 // We did not get a pruning candidate. 758 None => break Err(Error::Rc(ResponseCode::BACKEND_BUSY)), 759 } 760 } 761 } 762 } 763 764 /// Implementation of IKeystoreOperation. 765 pub struct KeystoreOperation { 766 operation: Mutex<Option<Arc<Operation>>>, 767 } 768 769 impl KeystoreOperation { 770 /// Creates a new operation instance wrapped in a 771 /// BnKeystoreOperation proxy object. It also enables 772 /// `BinderFeatures::set_requesting_sid` on the new interface, because 773 /// we need it for checking Keystore permissions. new_native_binder(operation: Arc<Operation>) -> binder::Strong<dyn IKeystoreOperation>774 pub fn new_native_binder(operation: Arc<Operation>) -> binder::Strong<dyn IKeystoreOperation> { 775 BnKeystoreOperation::new_binder( 776 Self { operation: Mutex::new(Some(operation)) }, 777 BinderFeatures { set_requesting_sid: true, ..BinderFeatures::default() }, 778 ) 779 } 780 781 /// Grabs the outer operation mutex and calls `f` on the locked operation. 782 /// The function also deletes the operation if it returns with an error or if 783 /// `delete_op` is true. with_locked_operation<T, F>(&self, f: F, delete_op: bool) -> Result<T> where for<'a> F: FnOnce(&'a Operation) -> Result<T>,784 fn with_locked_operation<T, F>(&self, f: F, delete_op: bool) -> Result<T> 785 where 786 for<'a> F: FnOnce(&'a Operation) -> Result<T>, 787 { 788 let mut delete_op: bool = delete_op; 789 match self.operation.try_lock() { 790 Ok(mut mutex_guard) => { 791 let result = match &*mutex_guard { 792 Some(op) => { 793 let result = f(&*op); 794 // Any error here means we can discard the operation. 795 if result.is_err() { 796 delete_op = true; 797 } 798 result 799 } 800 None => Err(Error::Km(ErrorCode::INVALID_OPERATION_HANDLE)) 801 .context("In KeystoreOperation::with_locked_operation"), 802 }; 803 804 if delete_op { 805 // We give up our reference to the Operation, thereby freeing up our 806 // internal resources and ending the wrapped KeyMint operation. 807 // This KeystoreOperation object will still be owned by an SpIBinder 808 // until the client drops its remote reference. 809 *mutex_guard = None; 810 } 811 result 812 } 813 Err(_) => Err(Error::Rc(ResponseCode::OPERATION_BUSY)) 814 .context("In KeystoreOperation::with_locked_operation"), 815 } 816 } 817 } 818 819 impl binder::Interface for KeystoreOperation {} 820 821 impl IKeystoreOperation for KeystoreOperation { updateAad(&self, aad_input: &[u8]) -> binder::Result<()>822 fn updateAad(&self, aad_input: &[u8]) -> binder::Result<()> { 823 let _wp = wd::watch_millis("IKeystoreOperation::updateAad", 500); 824 map_or_log_err( 825 self.with_locked_operation( 826 |op| op.update_aad(aad_input).context("In KeystoreOperation::updateAad"), 827 false, 828 ), 829 Ok, 830 ) 831 } 832 update(&self, input: &[u8]) -> binder::Result<Option<Vec<u8>>>833 fn update(&self, input: &[u8]) -> binder::Result<Option<Vec<u8>>> { 834 let _wp = wd::watch_millis("IKeystoreOperation::update", 500); 835 map_or_log_err( 836 self.with_locked_operation( 837 |op| op.update(input).context("In KeystoreOperation::update"), 838 false, 839 ), 840 Ok, 841 ) 842 } finish( &self, input: Option<&[u8]>, signature: Option<&[u8]>, ) -> binder::Result<Option<Vec<u8>>>843 fn finish( 844 &self, 845 input: Option<&[u8]>, 846 signature: Option<&[u8]>, 847 ) -> binder::Result<Option<Vec<u8>>> { 848 let _wp = wd::watch_millis("IKeystoreOperation::finish", 500); 849 map_or_log_err( 850 self.with_locked_operation( 851 |op| op.finish(input, signature).context("In KeystoreOperation::finish"), 852 true, 853 ), 854 Ok, 855 ) 856 } 857 abort(&self) -> binder::Result<()>858 fn abort(&self) -> binder::Result<()> { 859 let _wp = wd::watch_millis("IKeystoreOperation::abort", 500); 860 map_err_with( 861 self.with_locked_operation( 862 |op| op.abort(Outcome::Abort).context("In KeystoreOperation::abort"), 863 true, 864 ), 865 |e| { 866 match e.root_cause().downcast_ref::<Error>() { 867 // Calling abort on expired operations is something very common. 868 // There is no reason to clutter the log with it. It is never the cause 869 // for a true problem. 870 Some(Error::Km(ErrorCode::INVALID_OPERATION_HANDLE)) => {} 871 _ => log::error!("{:?}", e), 872 }; 873 e 874 }, 875 Ok, 876 ) 877 } 878 } 879