starnix_core/security/selinux_hooks/
audit.rs1use crate::task::{CurrentTask, Task};
6use crate::vfs::{
7 DirEntry, DirEntryHandle, FileObject, FileSystem, FsNode, FsStr, NamespaceNode,
8 PathWithReachability,
9};
10use bstr::BStr;
11use fuchsia_rcu::RcuReadScope;
12use fuchsia_sync::Mutex;
13use hex;
14use linux_uapi::AUDIT_AVC;
15use selinux::permission_check::{PermissionCheck, PermissionCheckResult};
16use selinux::{ClassPermission, KernelClass, KernelPermission, SecurityId};
17use starnix_logging::{CATEGORY_STARNIX_SECURITY, trace_instant};
18use std::collections::HashMap;
19use std::fmt::{Display, Error};
20use std::num::NonZeroU32;
21use std::sync::LazyLock;
22
23#[derive(Clone, Eq, Hash, PartialEq)]
25struct AuditableInstance {
26 source_sid: SecurityId,
27 target_sid: SecurityId,
28 class: KernelClass,
29 bug: NonZeroU32,
30}
31
32static TODO_DENY_COUNTS: LazyLock<Mutex<HashMap<AuditableInstance, u32>>> =
34 LazyLock::new(|| Mutex::new(HashMap::new()));
35
36fn should_audit(
38 source_sid: SecurityId,
39 target_sid: SecurityId,
40 class: KernelClass,
41 bug: NonZeroU32,
42) -> bool {
43 const MAX_TODO_AUDIT_DENIALS: u32 = 5;
45
46 let mut counts = TODO_DENY_COUNTS.lock();
47 let count = counts.entry(AuditableInstance { source_sid, target_sid, class, bug }).or_default();
48 *count += 1;
49 *count <= MAX_TODO_AUDIT_DENIALS
50}
51
52#[derive(Debug, Clone, Copy)]
74pub enum Auditable<'a> {
75 AuditContext(&'a [Auditable<'a>]),
77 Bug(u32),
78 CurrentTask,
79 DirEntry(&'a DirEntry),
80 FileObject(&'a FileObject),
81 FileSystem(&'a FileSystem),
82 FsNode(&'a FsNode),
83 IoctlCommand(u16),
84 Location(&'a std::panic::Location<'a>),
85 Name(&'a FsStr),
86 NamespaceNode(&'a NamespaceNode),
87 NlMsgtype(u16),
88 None,
89 SockOptArguments(u32, u32),
90 Task(&'a Task),
91 }
93
94impl Auditable<'_> {
95 fn from_bug(bug_id: u32) -> Self {
96 Auditable::Bug(bug_id)
97 }
98}
99
100impl<'a> From<&'a CurrentTask> for Auditable<'a> {
101 fn from(_value: &'a CurrentTask) -> Self {
102 Auditable::CurrentTask
104 }
105}
106
107impl<'a> From<&'a Task> for Auditable<'a> {
108 fn from(value: &'a Task) -> Self {
109 Auditable::Task(value)
110 }
111}
112
113impl<'a> From<&'a DirEntry> for Auditable<'a> {
114 fn from(value: &'a DirEntry) -> Self {
115 Auditable::DirEntry(value)
116 }
117}
118
119impl<'a> From<&'a DirEntryHandle> for Auditable<'a> {
120 fn from(value: &'a DirEntryHandle) -> Self {
121 Auditable::DirEntry(&*value)
122 }
123}
124
125impl<'a> From<&'a FileObject> for Auditable<'a> {
126 fn from(value: &'a FileObject) -> Self {
127 Auditable::FileObject(value)
128 }
129}
130
131impl<'a> From<&'a FsNode> for Auditable<'a> {
132 fn from(value: &'a FsNode) -> Self {
133 Auditable::FsNode(value)
134 }
135}
136
137impl<'a> From<&'a FileSystem> for Auditable<'a> {
138 fn from(value: &'a FileSystem) -> Self {
139 Auditable::FileSystem(value)
140 }
141}
142
143impl<'a> From<&'a std::panic::Location<'a>> for Auditable<'a> {
144 fn from(value: &'a std::panic::Location<'a>) -> Self {
145 Auditable::Location(value)
146 }
147}
148
149impl<'a> From<&'a NamespaceNode> for Auditable<'a> {
150 fn from(value: &'a NamespaceNode) -> Self {
151 Auditable::NamespaceNode(value)
152 }
153}
154
155impl<'a, const N: usize> From<&'a [Auditable<'a>; N]> for Auditable<'a> {
156 fn from(value: &'a [Auditable<'a>; N]) -> Self {
157 Auditable::AuditContext(value)
158 }
159}
160
161pub(super) fn audit_decision(
175 current_task: &CurrentTask,
176 permission_check: &PermissionCheck<'_>,
177 result: PermissionCheckResult,
178 source_sid: SecurityId,
179 target_sid: SecurityId,
180 permission: KernelPermission,
181 audit_data: Auditable<'_>,
182) {
183 trace_instant!(
184 CATEGORY_STARNIX_SECURITY,
185 match (result.granted, result.todo_bug) {
186 (true, None) => c"audit.granted",
187 (true, Some(_)) => c"audit.todo_deny",
188 _ => c"audit.denied",
189 },
190 fuchsia_trace::Scope::Thread
191 );
192
193 let decision = if let Some(todo_bug) = result.todo_bug {
194 if !should_audit(source_sid, target_sid, permission.class(), todo_bug) {
200 return;
201 }
202
203 "todo_deny"
205 } else {
206 if result.granted { "granted" } else { "denied" }
207 };
208
209 let audit_data_with_bug =
211 [Auditable::from_bug(result.todo_bug.map(NonZeroU32::get).unwrap_or(0)), audit_data];
212 let audit_data =
213 if result.todo_bug.is_some() { (&audit_data_with_bug).into() } else { audit_data };
214
215 let audit_logger = current_task.kernel().audit_logger();
216 audit_logger.audit_log(
217 AUDIT_AVC as u16,
218 || {
219 let tclass = permission.class().name();
220 let permission_name = permission.name();
221
222 let security_server = permission_check.security_server();
225 let scontext = security_server.sid_to_security_context(source_sid).unwrap();
226 let scontext = BStr::new(&scontext);
227 let tcontext = security_server.sid_to_security_context(target_sid).unwrap();
228 let tcontext = BStr::new(&tcontext);
229
230 let pid = current_task.get_pid();
232 let command = current_task.command();
233
234 let is_permissive = result.permissive as u8;
235
236 format!("avc: {decision} {{ {permission_name} }} for pid={pid} comm=\"{command}\"{audit_data} scontext={scontext} tcontext={tcontext} tclass={tclass} permissive={is_permissive}")
237 }
238 );
239}
240
241impl Display for Auditable<'_> {
242 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), Error> {
243 match self {
244 Auditable::AuditContext(audit_context) => {
245 for item in *audit_context {
246 item.fmt(f)?;
247 }
248 Ok(())
249 }
250 Auditable::Bug(bug_id) => {
251 write!(f, " bug={}", bug_id)
252 }
253 Auditable::CurrentTask => Ok(()),
254 Auditable::DirEntry(entry) => {
255 let scope = RcuReadScope::new();
256 write!(f, " name={}", hex_escape(entry.local_name(&scope)))
257 }
258 Auditable::FileObject(file) => {
259 write!(f, " path={}", hex_escape(&file.name.path_escaping_chroot()))
260 }
261 Auditable::FileSystem(fs) => {
262 write!(f, " dev={}", hex_escape(&fs.options.source))
263 }
264 Auditable::FsNode(node) => {
265 write!(f, " ino={}", node.ino)
266 }
267 Auditable::IoctlCommand(ioctl) => {
268 write!(f, " ioctlcmd={:#x}", ioctl)
269 }
270 Auditable::NlMsgtype(message_type) => {
271 write!(f, " nl-msgtype={}", message_type)
272 }
273 Auditable::Location(location) => {
274 write!(f, " caller={:?}", location)
275 }
276 Auditable::Name(name) => {
277 write!(f, " name={}", hex_escape(name))
278 }
279 Auditable::NamespaceNode(node) => {
280 let PathWithReachability::Reachable(path) = node.path_from_root(None) else {
281 return Ok(());
282 };
283 write!(f, " path={}", hex_escape(&path))
284 }
285 Auditable::SockOptArguments(level, optname) => {
286 write!(f, " level={}, optname={}", level, optname)
287 }
288 Auditable::None => Ok(()),
289 Auditable::Task(task) => {
290 write!(f, " pid={}, comm={}", task.get_pid(), task.command())
291 }
292 }
293 }
294}
295
296struct EscapedString<'a> {
297 value: &'a [u8],
298}
299
300impl<'a> Display for EscapedString<'a> {
301 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), Error> {
302 let maybe_utf8 = str::from_utf8(self.value).ok();
307 if let Some(utf8) = maybe_utf8 {
308 if utf8.find(|c| c <= ' ').is_none() {
309 return write!(f, "\"{}\"", BStr::new(self.value));
310 }
311 }
312 hex::encode_upper(self.value).fmt(f)
313 }
314}
315
316fn hex_escape<'a>(value: &'a [u8]) -> EscapedString<'a> {
317 EscapedString { value }
318}