Merge "[refactor] Split the message macro in a standalone library for reuse" into main am: f1aba2721c am: e82faef4b4

Original change: https://android-review.googlesource.com/c/platform/system/security/+/2817476

Change-Id: Ide0cb603eb415f86c91af12dccf09abd7dc5e0e8
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Alice Wang 2023-11-08 10:07:19 +00:00 committed by Automerger Merge Worker
commit b5c6c2475a
5 changed files with 64 additions and 26 deletions

View file

@ -55,6 +55,7 @@ rust_defaults {
"liblibc",
"liblog_event_list",
"liblog_rust",
"libmessage_macro",
"librand",
"librustutils",
"libserde",

View file

@ -0,0 +1,37 @@
// 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: "libmessage_macro_defaults",
crate_name: "message_macro",
srcs: ["src/lib.rs"],
}
rust_library {
name: "libmessage_macro",
defaults: ["libmessage_macro_defaults"],
apex_available: [
"//apex_available:platform",
"com.android.virt",
],
}

View file

@ -12,20 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//! A ks_err macro that expands error messages to include the file and line number
//! A macro that generates a message containing the current source file name
//! and line number.
/// Generates a message containing the current source file name and line number.
///
/// # Examples
///
/// ```
/// use crate::ks_err;
///
/// ks_err!("Key is expired.");
/// source_location_msg!("Key is expired.");
/// Result:
/// "src/lib.rs:7 Key is expired."
/// ```
#[macro_export]
macro_rules! ks_err {
macro_rules! source_location_msg {
{ $($arg:tt)+ } => {
format!("{}:{}: {}", file!(), line!(), format_args!($($arg)+))
};

View file

@ -28,7 +28,6 @@ pub mod globals;
pub mod id_rotation;
/// Internal Representation of Key Parameter and convenience functions.
pub mod key_parameter;
pub mod ks_err;
pub mod legacy_blob;
pub mod legacy_importer;
pub mod maintenance;
@ -51,3 +50,5 @@ mod km_compat;
mod super_key;
mod sw_keyblob;
mod watchdog_helper;
use message_macro::source_location_msg as ks_err;

View file

@ -15,7 +15,6 @@
//! Helper wrapper around RKPD interface.
use crate::error::{map_binder_status_code, Error, ResponseCode};
use crate::ks_err;
use crate::watchdog_helper::watchdog as wd;
use android_security_rkp_aidl::aidl::android::security::rkp::{
IGetKeyCallback::BnGetKeyCallback, IGetKeyCallback::ErrorCode::ErrorCode as GetKeyErrorCode,
@ -28,6 +27,7 @@ use android_security_rkp_aidl::aidl::android::security::rkp::{
};
use android_security_rkp_aidl::binder::{BinderFeatures, Interface, Strong};
use anyhow::{Context, Result};
use message_macro::source_location_msg;
use std::sync::Mutex;
use std::time::Duration;
use tokio::sync::oneshot;
@ -91,17 +91,17 @@ impl IGetRegistrationCallback for GetRegistrationCallback {
log::warn!("IGetRegistrationCallback cancelled");
self.registration_tx.send(
Err(Error::Rc(ResponseCode::OUT_OF_KEYS_TRANSIENT_ERROR))
.context(ks_err!("GetRegistrationCallback cancelled.")),
.context(source_location_msg!("GetRegistrationCallback cancelled.")),
);
Ok(())
}
fn onError(&self, description: &str) -> binder::Result<()> {
let _wp = wd::watch_millis("IGetRegistrationCallback::onError", 500);
log::error!("IGetRegistrationCallback failed: '{description}'");
self.registration_tx.send(
Err(Error::Rc(ResponseCode::OUT_OF_KEYS_TRANSIENT_ERROR))
.context(ks_err!("GetRegistrationCallback failed: {:?}", description)),
);
self.registration_tx
.send(Err(Error::Rc(ResponseCode::OUT_OF_KEYS_TRANSIENT_ERROR)).context(
source_location_msg!("GetRegistrationCallback failed: {:?}", description),
));
Ok(())
}
}
@ -110,19 +110,18 @@ impl IGetRegistrationCallback for GetRegistrationCallback {
async fn get_rkpd_registration(rpc_name: &str) -> Result<binder::Strong<dyn IRegistration>> {
let remote_provisioning: Strong<dyn IRemoteProvisioning> =
map_binder_status_code(binder::get_interface("remote_provisioning"))
.context(ks_err!("Trying to connect to IRemoteProvisioning service."))?;
.context(source_location_msg!("Trying to connect to IRemoteProvisioning service."))?;
let (tx, rx) = oneshot::channel();
let cb = GetRegistrationCallback::new_native_binder(tx);
remote_provisioning
.getRegistration(rpc_name, &cb)
.context(ks_err!("Trying to get registration."))?;
.context(source_location_msg!("Trying to get registration."))?;
match timeout(RKPD_TIMEOUT, rx).await {
Err(e) => {
Err(Error::Rc(ResponseCode::SYSTEM_ERROR)).context(ks_err!("Waiting for RKPD: {:?}", e))
}
Err(e) => Err(Error::Rc(ResponseCode::SYSTEM_ERROR))
.context(source_location_msg!("Waiting for RKPD: {:?}", e)),
Ok(v) => v.unwrap(),
}
}
@ -156,7 +155,7 @@ impl IGetKeyCallback for GetKeyCallback {
log::warn!("IGetKeyCallback cancelled");
self.key_tx.send(
Err(Error::Rc(ResponseCode::OUT_OF_KEYS_TRANSIENT_ERROR))
.context(ks_err!("GetKeyCallback cancelled.")),
.context(source_location_msg!("GetKeyCallback cancelled.")),
);
Ok(())
}
@ -177,7 +176,7 @@ impl IGetKeyCallback for GetKeyCallback {
ResponseCode::OUT_OF_KEYS_TRANSIENT_ERROR
}
};
self.key_tx.send(Err(Error::Rc(rc)).context(ks_err!(
self.key_tx.send(Err(Error::Rc(rc)).context(source_location_msg!(
"GetKeyCallback failed: {:?} {:?}",
error,
description
@ -195,7 +194,7 @@ async fn get_rkpd_attestation_key_from_registration_async(
registration
.getKey(caller_uid.try_into().unwrap(), &cb)
.context(ks_err!("Trying to get key."))?;
.context(source_location_msg!("Trying to get key."))?;
match timeout(RKPD_TIMEOUT, rx).await {
Err(e) => {
@ -204,7 +203,7 @@ async fn get_rkpd_attestation_key_from_registration_async(
log::error!("IRegistration::cancelGetKey failed: {:?}", e);
}
Err(Error::Rc(ResponseCode::OUT_OF_KEYS_TRANSIENT_ERROR))
.context(ks_err!("Waiting for RKPD key timed out: {:?}", e))
.context(source_location_msg!("Waiting for RKPD key timed out: {:?}", e))
}
Ok(v) => v.unwrap(),
}
@ -216,7 +215,7 @@ async fn get_rkpd_attestation_key_async(
) -> Result<RemotelyProvisionedKey> {
let registration = get_rkpd_registration(rpc_name)
.await
.context(ks_err!("Trying to get to IRegistration service."))?;
.context(source_location_msg!("Trying to get to IRegistration service."))?;
get_rkpd_attestation_key_from_registration_async(&registration, caller_uid).await
}
@ -247,7 +246,7 @@ impl IStoreUpgradedKeyCallback for StoreUpgradedKeyCallback {
log::error!("IGetRegistrationCallback failed: {error}");
self.completer.send(
Err(Error::Rc(ResponseCode::SYSTEM_ERROR))
.context(ks_err!("Failed to store upgraded key: {:?}", error)),
.context(source_location_msg!("Failed to store upgraded key: {:?}", error)),
);
Ok(())
}
@ -263,11 +262,11 @@ async fn store_rkpd_attestation_key_with_registration_async(
registration
.storeUpgradedKeyAsync(key_blob, upgraded_blob, &cb)
.context(ks_err!("Failed to store upgraded blob with RKPD."))?;
.context(source_location_msg!("Failed to store upgraded blob with RKPD."))?;
match timeout(RKPD_TIMEOUT, rx).await {
Err(e) => Err(Error::Rc(ResponseCode::SYSTEM_ERROR))
.context(ks_err!("Waiting for RKPD to complete storing key: {:?}", e)),
.context(source_location_msg!("Waiting for RKPD to complete storing key: {:?}", e)),
Ok(v) => v.unwrap(),
}
}
@ -279,7 +278,7 @@ async fn store_rkpd_attestation_key_async(
) -> Result<()> {
let registration = get_rkpd_registration(rpc_name)
.await
.context(ks_err!("Trying to get to IRegistration service."))?;
.context(source_location_msg!("Trying to get to IRegistration service."))?;
store_rkpd_attestation_key_with_registration_async(&registration, key_blob, upgraded_blob).await
}