| Pavel Grafov | 94243c2 | 2021-04-21 18:03:11 +0100 | [diff] [blame] | 1 | // Copyright 2021, 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 functions to log audit events to binary security log buffer for NIAP | 
 | 16 | //! compliance. | 
 | 17 |  | 
 | 18 | use crate::globals::LOGS_HANDLER; | 
 | 19 | use android_system_keystore2::aidl::android::system::keystore2::{ | 
 | 20 |     Domain::Domain, KeyDescriptor::KeyDescriptor, | 
 | 21 | }; | 
 | 22 | use libc::uid_t; | 
 | 23 | use log_event_list::{LogContext, LogIdSecurity}; | 
 | 24 |  | 
 | 25 | const TAG_KEY_GENERATED: u32 = 210024; | 
 | 26 | const TAG_KEY_IMPORTED: u32 = 210025; | 
 | 27 | const TAG_KEY_DESTROYED: u32 = 210026; | 
| Pavel Grafov | f45034a | 2021-05-12 22:35:45 +0100 | [diff] [blame] | 28 | const TAG_KEY_INTEGRITY_VIOLATION: u32 = 210032; | 
| Pavel Grafov | 94243c2 | 2021-04-21 18:03:11 +0100 | [diff] [blame] | 29 |  | 
| Pavel Grafov | f45034a | 2021-05-12 22:35:45 +0100 | [diff] [blame] | 30 | const FLAG_NAMESPACE: i64 = 0x80000000; | 
| Pavel Grafov | 94243c2 | 2021-04-21 18:03:11 +0100 | [diff] [blame] | 31 |  | 
| Pavel Grafov | f45034a | 2021-05-12 22:35:45 +0100 | [diff] [blame] | 32 | /// Encode key owner as either uid or namespace with a flag. | 
 | 33 | fn key_owner(domain: Domain, nspace: i64, uid: i32) -> i32 { | 
 | 34 |     match domain { | 
 | 35 |         Domain::APP => uid, | 
 | 36 |         Domain::SELINUX => (nspace | FLAG_NAMESPACE) as i32, | 
| Pavel Grafov | 94243c2 | 2021-04-21 18:03:11 +0100 | [diff] [blame] | 37 |         _ => { | 
 | 38 |             log::info!("Not logging audit event for key with unexpected domain"); | 
 | 39 |             0 | 
 | 40 |         } | 
 | 41 |     } | 
 | 42 | } | 
 | 43 |  | 
 | 44 | /// Logs key generation event to NIAP audit log. | 
 | 45 | pub fn log_key_generated(key: &KeyDescriptor, calling_app: uid_t, success: bool) { | 
 | 46 |     log_key_event(TAG_KEY_GENERATED, key, calling_app, success); | 
 | 47 | } | 
 | 48 |  | 
 | 49 | /// Logs key import event to NIAP audit log. | 
 | 50 | pub fn log_key_imported(key: &KeyDescriptor, calling_app: uid_t, success: bool) { | 
 | 51 |     log_key_event(TAG_KEY_IMPORTED, key, calling_app, success); | 
 | 52 | } | 
 | 53 |  | 
 | 54 | /// Logs key deletion event to NIAP audit log. | 
 | 55 | pub fn log_key_deleted(key: &KeyDescriptor, calling_app: uid_t, success: bool) { | 
 | 56 |     log_key_event(TAG_KEY_DESTROYED, key, calling_app, success); | 
 | 57 | } | 
 | 58 |  | 
| Pavel Grafov | f45034a | 2021-05-12 22:35:45 +0100 | [diff] [blame] | 59 | /// Logs key integrity violation to NIAP audit log. | 
 | 60 | pub fn log_key_integrity_violation(key: &KeyDescriptor) { | 
 | 61 |     with_log_context(TAG_KEY_INTEGRITY_VIOLATION, |ctx| { | 
 | 62 |         let owner = key_owner(key.domain, key.nspace, key.nspace as i32); | 
 | 63 |         ctx.append_str(key.alias.as_ref().map_or("none", String::as_str)).append_i32(owner) | 
 | 64 |     }) | 
 | 65 | } | 
 | 66 |  | 
| Pavel Grafov | 94243c2 | 2021-04-21 18:03:11 +0100 | [diff] [blame] | 67 | fn log_key_event(tag: u32, key: &KeyDescriptor, calling_app: uid_t, success: bool) { | 
| Pavel Grafov | f45034a | 2021-05-12 22:35:45 +0100 | [diff] [blame] | 68 |     with_log_context(tag, |ctx| { | 
 | 69 |         let owner = key_owner(key.domain, key.nspace, calling_app as i32); | 
 | 70 |         ctx.append_i32(if success { 1 } else { 0 }) | 
| Pavel Grafov | 94243c2 | 2021-04-21 18:03:11 +0100 | [diff] [blame] | 71 |             .append_str(key.alias.as_ref().map_or("none", String::as_str)) | 
| Pavel Grafov | f45034a | 2021-05-12 22:35:45 +0100 | [diff] [blame] | 72 |             .append_i32(owner) | 
 | 73 |     }) | 
 | 74 | } | 
 | 75 |  | 
 | 76 | fn with_log_context<F>(tag: u32, f: F) | 
 | 77 | where | 
 | 78 |     F: Fn(LogContext) -> LogContext, | 
 | 79 | { | 
 | 80 |     if let Some(ctx) = LogContext::new(LogIdSecurity, tag) { | 
 | 81 |         let event = f(ctx); | 
| Pavel Grafov | 94243c2 | 2021-04-21 18:03:11 +0100 | [diff] [blame] | 82 |         LOGS_HANDLER.queue_lo(move |_| { | 
 | 83 |             event.write(); | 
 | 84 |         }); | 
 | 85 |     } | 
 | 86 | } |