• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
28 const TAG_KEY_INTEGRITY_VIOLATION: u32 = 210032;
29 
30 const FLAG_NAMESPACE: i64 = 0x80000000;
31 
32 /// Encode key owner as either uid or namespace with a flag.
key_owner(domain: Domain, nspace: i64, uid: i32) -> i3233 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,
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.
log_key_generated(key: &KeyDescriptor, calling_app: uid_t, success: bool)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.
log_key_imported(key: &KeyDescriptor, calling_app: uid_t, success: bool)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.
log_key_deleted(key: &KeyDescriptor, calling_app: uid_t, success: bool)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 
59 /// Logs key integrity violation to NIAP audit log.
log_key_integrity_violation(key: &KeyDescriptor)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 
log_key_event(tag: u32, key: &KeyDescriptor, calling_app: uid_t, success: bool)67 fn log_key_event(tag: u32, key: &KeyDescriptor, calling_app: uid_t, success: bool) {
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 })
71             .append_str(key.alias.as_ref().map_or("none", String::as_str))
72             .append_i32(owner)
73     })
74 }
75 
with_log_context<F>(tag: u32, f: F) where F: Fn(LogContext) -> LogContext,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);
82         LOGS_HANDLER.queue_lo(move |_| {
83             event.write();
84         });
85     }
86 }
87