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 structured_log::{structured_log, LOG_ID_SECURITY};
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 let owner = key_owner(key.domain, key.nspace, key.nspace as i32);
62 let alias = String::from(key.alias.as_ref().map_or("none", String::as_str));
63 LOGS_HANDLER.queue_lo(move |_| {
64 let _result =
65 structured_log!(log_id: LOG_ID_SECURITY, TAG_KEY_INTEGRITY_VIOLATION, alias, owner);
66 });
67 }
68
log_key_event(tag: u32, key: &KeyDescriptor, calling_app: uid_t, success: bool)69 fn log_key_event(tag: u32, key: &KeyDescriptor, calling_app: uid_t, success: bool) {
70 let owner = key_owner(key.domain, key.nspace, calling_app as i32);
71 let alias = String::from(key.alias.as_ref().map_or("none", String::as_str));
72 LOGS_HANDLER.queue_lo(move |_| {
73 let _result =
74 structured_log!(log_id: LOG_ID_SECURITY, tag, i32::from(success), alias, owner);
75 });
76 }
77