From 83c6aefb5dd7866d64c44af8cd6d771b8d4e9517 Mon Sep 17 00:00:00 2001 From: Alice Wang Date: Fri, 3 Nov 2023 17:17:34 +0000 Subject: [PATCH] [refactor] Split watchdog in a standalone library for reuse This simplifies the task of creating an independent library of rkpd_client later. Test: atest keystore2_test Bug: 241428146 Change-Id: I2834c9be9f5100d52829e6392f0dd48e7c76beb1 --- keystore2/Android.bp | 1 + keystore2/TEST_MAPPING | 3 + keystore2/src/lib.rs | 4 +- keystore2/src/rkpd_client.rs | 2 +- keystore2/src/utils.rs | 50 +-------------- keystore2/src/watchdog_helper.rs | 64 +++++++++++++++++++ keystore2/watchdog/Android.bp | 49 ++++++++++++++ .../{src/watchdog.rs => watchdog/src/lib.rs} | 0 8 files changed, 120 insertions(+), 53 deletions(-) create mode 100644 keystore2/src/watchdog_helper.rs create mode 100644 keystore2/watchdog/Android.bp rename keystore2/{src/watchdog.rs => watchdog/src/lib.rs} (100%) diff --git a/keystore2/Android.bp b/keystore2/Android.bp index 271f94df..dd036c25 100644 --- a/keystore2/Android.bp +++ b/keystore2/Android.bp @@ -61,6 +61,7 @@ rust_defaults { "libserde_cbor", "libthiserror", "libtokio", + "libwatchdog_rs", ], shared_libs: [ "libcutils", diff --git a/keystore2/TEST_MAPPING b/keystore2/TEST_MAPPING index f8a13020..1038bead 100644 --- a/keystore2/TEST_MAPPING +++ b/keystore2/TEST_MAPPING @@ -33,6 +33,9 @@ }, { "name": "keystore2_client_tests" + }, + { + "name": "libwatchdog_rs.test" } ] } diff --git a/keystore2/src/lib.rs b/keystore2/src/lib.rs index 3233017c..8c08d3ec 100644 --- a/keystore2/src/lib.rs +++ b/keystore2/src/lib.rs @@ -50,6 +50,4 @@ mod gc; mod km_compat; mod super_key; mod sw_keyblob; - -#[cfg(feature = "watchdog")] -mod watchdog; +mod watchdog_helper; diff --git a/keystore2/src/rkpd_client.rs b/keystore2/src/rkpd_client.rs index 7b4131d0..50092787 100644 --- a/keystore2/src/rkpd_client.rs +++ b/keystore2/src/rkpd_client.rs @@ -17,7 +17,7 @@ use crate::error::{map_binder_status_code, Error, ResponseCode}; use crate::globals::get_remotely_provisioned_component_name; use crate::ks_err; -use crate::utils::watchdog as wd; +use crate::watchdog_helper::watchdog as wd; use android_hardware_security_keymint::aidl::android::hardware::security::keymint::SecurityLevel::SecurityLevel; use android_security_rkp_aidl::aidl::android::security::rkp::{ IGetKeyCallback::BnGetKeyCallback, IGetKeyCallback::ErrorCode::ErrorCode as GetKeyErrorCode, diff --git a/keystore2/src/utils.rs b/keystore2/src/utils.rs index 80aa7c38..f028491a 100644 --- a/keystore2/src/utils.rs +++ b/keystore2/src/utils.rs @@ -20,6 +20,7 @@ use crate::key_parameter::KeyParameter; use crate::ks_err; use crate::permission; use crate::permission::{KeyPerm, KeyPermSet, KeystorePerm}; +pub use crate::watchdog_helper::watchdog; use crate::{ database::{KeyType, KeystoreDB}, globals::LEGACY_IMPORTER, @@ -421,36 +422,6 @@ pub fn count_key_entries(db: &mut KeystoreDB, domain: Domain, namespace: i64) -> Ok((legacy_keys.len() + num_keys_in_db) as i32) } -/// This module provides helpers for simplified use of the watchdog module. -#[cfg(feature = "watchdog")] -pub mod watchdog { - pub use crate::watchdog::WatchPoint; - use crate::watchdog::Watchdog; - use lazy_static::lazy_static; - use std::sync::Arc; - use std::time::Duration; - - lazy_static! { - /// A Watchdog thread, that can be used to create watch points. - static ref WD: Arc = Watchdog::new(Duration::from_secs(10)); - } - - /// Sets a watch point with `id` and a timeout of `millis` milliseconds. - pub fn watch_millis(id: &'static str, millis: u64) -> Option { - Watchdog::watch(&WD, id, Duration::from_millis(millis)) - } - - /// Like `watch_millis` but with a callback that is called every time a report - /// is printed about this watch point. - pub fn watch_millis_with( - id: &'static str, - millis: u64, - callback: impl Fn() -> String + Send + 'static, - ) -> Option { - Watchdog::watch_with(&WD, id, Duration::from_millis(millis), callback) - } -} - /// Trait implemented by objects that can be used to decrypt cipher text using AES-GCM. pub trait AesGcm { /// Deciphers `data` using the initialization vector `iv` and AEAD tag `tag` @@ -480,25 +451,6 @@ impl AesGcm for T { } } -/// This module provides empty/noop implementations of the watch dog utility functions. -#[cfg(not(feature = "watchdog"))] -pub mod watchdog { - /// Noop watch point. - pub struct WatchPoint(); - /// Sets a Noop watch point. - fn watch_millis(_: &'static str, _: u64) -> Option { - None - } - - pub fn watch_millis_with( - _: &'static str, - _: u64, - _: impl Fn() -> String + Send + 'static, - ) -> Option { - None - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/keystore2/src/watchdog_helper.rs b/keystore2/src/watchdog_helper.rs new file mode 100644 index 00000000..92a0abc1 --- /dev/null +++ b/keystore2/src/watchdog_helper.rs @@ -0,0 +1,64 @@ +// Copyright 2023, The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Helpers for the watchdog module. + +/// This module provides helpers for simplified use of the watchdog module. +#[cfg(feature = "watchdog")] +pub mod watchdog { + use lazy_static::lazy_static; + use std::sync::Arc; + use std::time::Duration; + pub use watchdog_rs::WatchPoint; + use watchdog_rs::Watchdog; + + lazy_static! { + /// A Watchdog thread, that can be used to create watch points. + static ref WD: Arc = Watchdog::new(Duration::from_secs(10)); + } + + /// Sets a watch point with `id` and a timeout of `millis` milliseconds. + pub fn watch_millis(id: &'static str, millis: u64) -> Option { + Watchdog::watch(&WD, id, Duration::from_millis(millis)) + } + + /// Like `watch_millis` but with a callback that is called every time a report + /// is printed about this watch point. + pub fn watch_millis_with( + id: &'static str, + millis: u64, + callback: impl Fn() -> String + Send + 'static, + ) -> Option { + Watchdog::watch_with(&WD, id, Duration::from_millis(millis), callback) + } +} + +/// This module provides empty/noop implementations of the watch dog utility functions. +#[cfg(not(feature = "watchdog"))] +pub mod watchdog { + /// Noop watch point. + pub struct WatchPoint(); + /// Sets a Noop watch point. + fn watch_millis(_: &'static str, _: u64) -> Option { + None + } + + pub fn watch_millis_with( + _: &'static str, + _: u64, + _: impl Fn() -> String + Send + 'static, + ) -> Option { + None + } +} diff --git a/keystore2/watchdog/Android.bp b/keystore2/watchdog/Android.bp new file mode 100644 index 00000000..62ede89e --- /dev/null +++ b/keystore2/watchdog/Android.bp @@ -0,0 +1,49 @@ +// Copyright 2023, The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package { + // See: http://go/android-license-faq + // A large-scale-change added 'default_applicable_licenses' to import + // all of the 'license_kinds' from "system_security_license" + // to get the below license kinds: + // SPDX-license-identifier-Apache-2.0 + default_applicable_licenses: ["system_security_license"], +} + +rust_defaults { + name: "libwatchdog_defaults", + crate_name: "watchdog_rs", + srcs: ["src/lib.rs"], + rustlibs: [ + "liblog_rust", + ] +} + +rust_library { + name: "libwatchdog_rs", + defaults: ["libwatchdog_defaults"], + apex_available: [ + "//apex_available:platform", + "com.android.virt", + ], +} + +rust_test { + name: "libwatchdog_rs.test", + defaults: ["libwatchdog_defaults"], + test_suites: ["general-tests"], + rustlibs: [ + "libandroid_logger", + ] +} diff --git a/keystore2/src/watchdog.rs b/keystore2/watchdog/src/lib.rs similarity index 100% rename from keystore2/src/watchdog.rs rename to keystore2/watchdog/src/lib.rs