starnix_core/security/mod.rs
1// Copyright 2024 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! This module provides types and hook APIs supporting Linux Security Modules
6//! functionality in Starnix. LSM provides a generic set of hooks, and opaque
7//! types, used to decouple the rest of the kernel from the details of any
8//! specific security enforcement subsystem (e.g. SELinux, POSIX.1e, etc).
9//!
10//! Although this module is hard-wired to the SELinux implementation, callers
11//! should treat the types as opaque; hook implementations necessarily have access
12//! to kernel structures, but not the other way around.
13
14use selinux::{SecurityId, SecurityServer};
15use starnix_sync::Mutex;
16use std::sync::Arc;
17
18/// SELinux implementations called by the LSM hooks.
19mod selinux_hooks;
20pub use selinux_hooks::audit::Auditable;
21
22/// Common capabilities hook implementations called by the LSM hooks.
23mod common_cap;
24
25/// YAMA hook implementations used to restirct ptrace access.
26pub mod yama;
27
28/// Linux Security Modules hooks for use within the Starnix kernel.
29mod hooks;
30pub use hooks::*;
31
32/// Audit logging to be used from different kernel components
33mod audit;
34pub use audit::*;
35
36/// Opaque structure encapsulating security subsystem state for the whole system.
37pub struct KernelState {
38 state: Option<selinux_hooks::KernelState>,
39}
40
41impl KernelState {
42 pub fn access_denial_count(&self) -> u64 {
43 self.state.as_ref().map_or(0u64, |state| state.access_denial_count())
44 }
45}
46
47/// Opaque structure encapsulating active security state for a `Task`.
48#[derive(Debug)]
49pub struct TaskState(Arc<Mutex<selinux_hooks::TaskAttrs>>);
50
51impl TaskState {
52 pub(in crate::security) fn lock(
53 &self,
54 ) -> starnix_sync::MutexGuard<'_, selinux_hooks::TaskAttrs> {
55 self.0.lock()
56 }
57}
58
59impl Clone for TaskState {
60 fn clone(&self) -> Self {
61 TaskState(Arc::new(self.0.lock().clone().into()))
62 }
63}
64
65/// Structure holding security state associated with a `ResolvedElf` instance.
66/// TODO(https://fxbug.dev/378835222): Consider restructuring hook calls so that
67/// the kernel does not need to depend on the contents of this struct.
68#[derive(Clone, Debug, PartialEq)]
69pub struct ResolvedElfState {
70 sid: Option<SecurityId>,
71 /// Whether SELinux requires that this executable runs in secure mode.
72 require_secure_exec: bool,
73}
74
75impl ResolvedElfState {
76 pub fn require_secure_exec(&self) -> bool {
77 self.require_secure_exec
78 }
79}
80
81/// The opaque type used by [`crate::vfs::FsNodeInfo`] to store security state.
82#[derive(Debug, Default)]
83pub struct FsNodeState(Mutex<selinux_hooks::FsNodeState>);
84
85impl FsNodeState {
86 pub fn lock(&self) -> starnix_sync::MutexGuard<'_, selinux_hooks::FsNodeState> {
87 self.0.lock()
88 }
89}
90
91/// Opaque structure holding security state for a [`binderfs::BinderConnection`].
92#[derive(Debug)]
93pub struct BinderConnectionState {
94 state: selinux_hooks::BinderConnectionState,
95}
96
97/// Opaque structure holding security state for a [`crate::vfs::socket::Socket`].
98#[derive(Debug, Default)]
99pub struct SocketState {
100 state: selinux_hooks::SocketState,
101}
102
103/// Opaque structure holding security state for a [`crate::vfs::FileObject`].
104#[derive(Debug)]
105pub struct FileObjectState {
106 state: selinux_hooks::FileObjectState,
107}
108
109/// Opaque structure holding security state for a [`crate::vfs::FileSystem`].
110#[derive(Debug)]
111pub struct FileSystemState {
112 state: selinux_hooks::FileSystemState,
113}
114
115/// Opaque structure holding security state for a bpf [`ebpf_api::maps::Map`].
116#[derive(Debug)]
117pub struct BpfMapState {
118 state: selinux_hooks::BpfMapState,
119}
120
121/// Opaque structure holding security state for a bpf [`crate::bpf::program::Program`].
122#[derive(Debug)]
123pub struct BpfProgState {
124 state: selinux_hooks::BpfProgState,
125}
126
127/// Opaque structure holding security state for a PerfEventFileState.
128#[derive(Debug)]
129pub struct PerfEventState {
130 state: selinux_hooks::PerfEventState,
131}