Hasini Gunasinghe | f04d07a | 2020-11-25 22:41:35 +0000 | [diff] [blame] | 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 module implements the handling of background tasks such as obtaining timestamp tokens from |
| 16 | //! the timestamp service (or TEE KeyMint in legacy devices), via a separate thread. |
| 17 | |
| 18 | use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 19 | HardwareAuthToken::HardwareAuthToken, |
| 20 | }; |
| 21 | use android_hardware_security_secureclock::aidl::android::hardware::security::secureclock::{ |
| 22 | ISecureClock::ISecureClock, TimeStampToken::TimeStampToken, |
Hasini Gunasinghe | f04d07a | 2020-11-25 22:41:35 +0000 | [diff] [blame] | 23 | }; |
| 24 | use android_system_keystore2::aidl::android::system::keystore2::OperationChallenge::OperationChallenge; |
| 25 | use anyhow::Result; |
| 26 | use log::error; |
| 27 | use std::sync::mpsc::{Receiver, Sender}; |
| 28 | use std::sync::Mutex; |
| 29 | use std::thread::{spawn, JoinHandle}; |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 30 | |
| 31 | use crate::globals::get_timestamp_service; |
| 32 | |
Hasini Gunasinghe | f04d07a | 2020-11-25 22:41:35 +0000 | [diff] [blame] | 33 | /// This is the struct encapsulating the thread which handles background tasks such as |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 34 | /// obtaining timestamp tokens. |
Hasini Gunasinghe | f04d07a | 2020-11-25 22:41:35 +0000 | [diff] [blame] | 35 | pub struct BackgroundTaskHandler { |
| 36 | task_handler: Mutex<Option<JoinHandle<()>>>, |
| 37 | } |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 38 | |
Hasini Gunasinghe | f04d07a | 2020-11-25 22:41:35 +0000 | [diff] [blame] | 39 | /// This enum defines the two variants of a message that can be passed down to the |
| 40 | /// BackgroundTaskHandler via the channel. |
| 41 | pub enum Message { |
| 42 | ///This variant represents a message sent down the channel when requesting a timestamp token. |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 43 | Inputs((HardwareAuthToken, OperationChallenge, Sender<(HardwareAuthToken, TimeStampToken)>)), |
Hasini Gunasinghe | f04d07a | 2020-11-25 22:41:35 +0000 | [diff] [blame] | 44 | ///This variant represents a message sent down the channel when signalling the thread to stop. |
| 45 | Shutdown, |
| 46 | } |
| 47 | |
| 48 | impl BackgroundTaskHandler { |
| 49 | /// Initialize the BackgroundTaskHandler with the task_handler field set to None. |
| 50 | /// The thread is not started during initialization, as it needs the receiver end of a channel |
| 51 | /// to function. |
| 52 | pub fn new() -> Self { |
| 53 | BackgroundTaskHandler { task_handler: Mutex::new(None) } |
| 54 | } |
| 55 | |
| 56 | /// Start the background task handler (bth) by passing in the receiver end of a channel, through |
| 57 | /// which the enforcement module can send messages to the bth thread. |
| 58 | pub fn start_bth(&self, receiver: Receiver<Message>) -> Result<()> { |
| 59 | let task_handler = Self::start_thread(receiver)?; |
| 60 | // it is ok to unwrap here because there is no way that this lock can get poisoned. |
| 61 | let mut thread_guard = self.task_handler.lock().unwrap(); |
| 62 | *thread_guard = Some(task_handler); |
| 63 | Ok(()) |
| 64 | } |
| 65 | |
| 66 | fn start_thread(receiver: Receiver<Message>) -> Result<JoinHandle<()>> { |
| 67 | // TODO: initialize timestamp service/keymint instances. |
| 68 | // First lookup timestamp token service, if this is not a legacy device. |
| 69 | // Otherwise, lookup keymaster 4.1 or 4.0 (previous ones are not relevant, because strongbox |
| 70 | // was introduced with keymaster 4.0). |
| 71 | // If either a timestamp service or a keymint instance is expected to be found and neither |
| 72 | // is found, an error is returned. |
| 73 | // If neither is expected to be found, make timestamp_service field None, and in the thread, |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 74 | // send a default timestamp token down the channel to the operation. |
Hasini Gunasinghe | f04d07a | 2020-11-25 22:41:35 +0000 | [diff] [blame] | 75 | // Until timestamp service is available and proper probing of legacy keymaster devices are |
| 76 | // done, the keymint service is initialized here as it is done in security_level module. |
| 77 | Ok(spawn(move || { |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 78 | while let Message::Inputs((auth_token, op_challenge, op_sender)) = |
| 79 | receiver.recv().expect( |
| 80 | "In background task handler thread. Failed to |
| 81 | receive message over the channel.", |
| 82 | ) |
| 83 | { |
| 84 | let dev: Box<dyn ISecureClock> = get_timestamp_service() |
| 85 | .expect(concat!( |
| 86 | "Secure Clock service must be present ", |
| 87 | "if TimeStampTokens are required." |
| 88 | )) |
| 89 | .get_interface() |
| 90 | .expect("Fatal: Timestamp service does not implement ISecureClock."); |
| 91 | let result = dev.generateTimeStamp(op_challenge.challenge); |
Hasini Gunasinghe | f04d07a | 2020-11-25 22:41:35 +0000 | [diff] [blame] | 92 | match result { |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 93 | Ok(timestamp_token) => { |
Hasini Gunasinghe | f04d07a | 2020-11-25 22:41:35 +0000 | [diff] [blame] | 94 | // this can fail if the operation is dropped and hence the channel |
| 95 | // is hung up. |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 96 | op_sender.send((auth_token, timestamp_token)).unwrap_or_else(|e| { |
Hasini Gunasinghe | f04d07a | 2020-11-25 22:41:35 +0000 | [diff] [blame] | 97 | error!( |
| 98 | "In background task handler thread. Failed to send |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 99 | timestamp token to operation {} due to error {:?}.", |
Hasini Gunasinghe | f04d07a | 2020-11-25 22:41:35 +0000 | [diff] [blame] | 100 | op_challenge.challenge, e |
| 101 | ) |
| 102 | }); |
| 103 | } |
| 104 | Err(e) => { |
| 105 | // log error |
| 106 | error!( |
| 107 | "In background task handler thread. Failed to receive |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 108 | timestamp token for operation {} due to error {:?}.", |
Hasini Gunasinghe | f04d07a | 2020-11-25 22:41:35 +0000 | [diff] [blame] | 109 | op_challenge.challenge, e |
| 110 | ); |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 111 | // send default timestamp token |
Hasini Gunasinghe | f04d07a | 2020-11-25 22:41:35 +0000 | [diff] [blame] | 112 | // this can fail if the operation is dropped and the channel is |
| 113 | // hung up. |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 114 | op_sender.send((auth_token, TimeStampToken::default())).unwrap_or_else( |
Hasini Gunasinghe | f04d07a | 2020-11-25 22:41:35 +0000 | [diff] [blame] | 115 | |e| { |
| 116 | error!( |
| 117 | "In background task handler thread. Failed to send default |
Janis Danisevskis | c3a496b | 2021-01-05 10:37:22 -0800 | [diff] [blame] | 118 | timestamp token to operation {} due to error {:?}.", |
Hasini Gunasinghe | f04d07a | 2020-11-25 22:41:35 +0000 | [diff] [blame] | 119 | op_challenge.challenge, e |
| 120 | ) |
| 121 | }, |
| 122 | ); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | })) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | impl Default for BackgroundTaskHandler { |
| 131 | fn default() -> Self { |
| 132 | Self::new() |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // TODO: Verify if we want the thread to finish the requests they are working on, during drop. |
| 137 | impl Drop for BackgroundTaskHandler { |
| 138 | fn drop(&mut self) { |
| 139 | // it is ok to unwrap here as there is no way this lock can get poisoned. |
| 140 | let mut thread_guard = self.task_handler.lock().unwrap(); |
| 141 | if let Some(thread) = (*thread_guard).take() { |
| 142 | // TODO: Verify how best to handle the error in this case. |
| 143 | thread.join().unwrap_or_else(|e| { |
| 144 | panic!("Failed to join the background task handling thread because of {:?}.", e); |
| 145 | }); |
| 146 | } |
| 147 | } |
| 148 | } |