From b948e92b70a7681f5ecb6ec0425471c4bd814162 Mon Sep 17 00:00:00 2001 From: Marcin Radomski Date: Wed, 14 Jun 2023 10:35:36 +0000 Subject: [PATCH] audit_log.rs: handle Results in LogContext handling The Rust liblog_event_list API used to silently ignore any errors reported by liblog. aosp/2617613 attempts to make the operations propagate the failure instead. Note that this introduces a subtle behavior change: when *creating the log record* fails, the API with Results does not allow submitting a partially constructed log. Otherwise, the result of the write operation is ignored as it was before. Bug: 282691103 Test: m Test: atest keystore2_test Change-Id: I7c43100149b4ca831050af0a9229b95d2f7f8392 --- keystore2/src/audit_log.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/keystore2/src/audit_log.rs b/keystore2/src/audit_log.rs index 07509d36..0e5dfeb6 100644 --- a/keystore2/src/audit_log.rs +++ b/keystore2/src/audit_log.rs @@ -20,7 +20,7 @@ use android_system_keystore2::aidl::android::system::keystore2::{ Domain::Domain, KeyDescriptor::KeyDescriptor, }; use libc::uid_t; -use log_event_list::{LogContext, LogIdSecurity}; +use log_event_list::{LogContext, LogContextError, LogIdSecurity}; const TAG_KEY_GENERATED: u32 = 210024; const TAG_KEY_IMPORTED: u32 = 210025; @@ -60,27 +60,28 @@ pub fn log_key_deleted(key: &KeyDescriptor, calling_app: uid_t, success: bool) { pub fn log_key_integrity_violation(key: &KeyDescriptor) { with_log_context(TAG_KEY_INTEGRITY_VIOLATION, |ctx| { let owner = key_owner(key.domain, key.nspace, key.nspace as i32); - ctx.append_str(key.alias.as_ref().map_or("none", String::as_str)).append_i32(owner) + ctx.append_str(key.alias.as_ref().map_or("none", String::as_str))?.append_i32(owner) }) } fn log_key_event(tag: u32, key: &KeyDescriptor, calling_app: uid_t, success: bool) { with_log_context(tag, |ctx| { let owner = key_owner(key.domain, key.nspace, calling_app as i32); - ctx.append_i32(i32::from(success)) - .append_str(key.alias.as_ref().map_or("none", String::as_str)) + ctx.append_i32(i32::from(success))? + .append_str(key.alias.as_ref().map_or("none", String::as_str))? .append_i32(owner) }) } fn with_log_context(tag: u32, f: F) where - F: Fn(LogContext) -> LogContext, + F: Fn(LogContext) -> Result, { if let Some(ctx) = LogContext::new(LogIdSecurity, tag) { - let event = f(ctx); - LOGS_HANDLER.queue_lo(move |_| { - event.write(); - }); + if let Ok(event) = f(ctx) { + LOGS_HANDLER.queue_lo(move |_| { + let _result = event.write(); + }); + } } }