blob: 0e5dfeb63fa97e9021dbb833d74d89bf944e4eb2 [file] [log] [blame]
Pavel Grafov94243c22021-04-21 18:03:11 +01001// 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
18use crate::globals::LOGS_HANDLER;
19use android_system_keystore2::aidl::android::system::keystore2::{
20 Domain::Domain, KeyDescriptor::KeyDescriptor,
21};
22use libc::uid_t;
Marcin Radomskib948e922023-06-14 10:35:36 +000023use log_event_list::{LogContext, LogContextError, LogIdSecurity};
Pavel Grafov94243c22021-04-21 18:03:11 +010024
25const TAG_KEY_GENERATED: u32 = 210024;
26const TAG_KEY_IMPORTED: u32 = 210025;
27const TAG_KEY_DESTROYED: u32 = 210026;
Pavel Grafovf45034a2021-05-12 22:35:45 +010028const TAG_KEY_INTEGRITY_VIOLATION: u32 = 210032;
Pavel Grafov94243c22021-04-21 18:03:11 +010029
Pavel Grafovf45034a2021-05-12 22:35:45 +010030const FLAG_NAMESPACE: i64 = 0x80000000;
Pavel Grafov94243c22021-04-21 18:03:11 +010031
Pavel Grafovf45034a2021-05-12 22:35:45 +010032/// Encode key owner as either uid or namespace with a flag.
33fn 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 Grafov94243c22021-04-21 18:03:11 +010037 _ => {
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.
45pub 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.
50pub 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.
55pub 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 Grafovf45034a2021-05-12 22:35:45 +010059/// Logs key integrity violation to NIAP audit log.
60pub 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);
Marcin Radomskib948e922023-06-14 10:35:36 +000063 ctx.append_str(key.alias.as_ref().map_or("none", String::as_str))?.append_i32(owner)
Pavel Grafovf45034a2021-05-12 22:35:45 +010064 })
65}
66
Pavel Grafov94243c22021-04-21 18:03:11 +010067fn log_key_event(tag: u32, key: &KeyDescriptor, calling_app: uid_t, success: bool) {
Pavel Grafovf45034a2021-05-12 22:35:45 +010068 with_log_context(tag, |ctx| {
69 let owner = key_owner(key.domain, key.nspace, calling_app as i32);
Marcin Radomskib948e922023-06-14 10:35:36 +000070 ctx.append_i32(i32::from(success))?
71 .append_str(key.alias.as_ref().map_or("none", String::as_str))?
Pavel Grafovf45034a2021-05-12 22:35:45 +010072 .append_i32(owner)
73 })
74}
75
76fn with_log_context<F>(tag: u32, f: F)
77where
Marcin Radomskib948e922023-06-14 10:35:36 +000078 F: Fn(LogContext) -> Result<LogContext, LogContextError>,
Pavel Grafovf45034a2021-05-12 22:35:45 +010079{
80 if let Some(ctx) = LogContext::new(LogIdSecurity, tag) {
Marcin Radomskib948e922023-06-14 10:35:36 +000081 if let Ok(event) = f(ctx) {
82 LOGS_HANDLER.queue_lo(move |_| {
83 let _result = event.write();
84 });
85 }
Pavel Grafov94243c22021-04-21 18:03:11 +010086 }
87}