Skip to main content

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 std::sync::Arc;
16
17/// SELinux implementations called by the LSM hooks.
18mod selinux_hooks;
19pub use selinux_hooks::audit::Auditable;
20
21/// Common capabilities hook implementations called by the LSM hooks.
22mod common_cap;
23
24/// YAMA hook implementations used to restirct ptrace access.
25pub mod yama;
26
27/// Linux Security Modules hooks for use within the Starnix kernel.
28mod hooks;
29pub use hooks::*;
30
31/// Audit logging to be used from different kernel components
32mod audit;
33pub use audit::*;
34
35/// Opaque structure encapsulating security subsystem state for the whole system.
36pub struct KernelState {
37    state: Option<selinux_hooks::KernelState>,
38}
39
40impl KernelState {
41    pub fn access_denial_count(&self) -> u64 {
42        self.state.as_ref().map_or(0u64, |state| state.access_denial_count())
43    }
44}
45
46/// Wrapper types enforcing at compile time that kernel-private `FsNode`s do not access security
47/// state.
48mod non_private;
49pub use non_private::FsNodeState;
50use non_private::{NonPrivateDirEntry, NonPrivateFsNode, NonPrivateSocket, NonPrivateSocketFile};
51
52/// Opaque structure holding security state for a [`crate::vfs::socket::Socket`].
53#[derive(Debug, Default)]
54pub struct SocketState {
55    state: selinux_hooks::SocketState,
56}
57
58/// Opaque structure holding security state for a [`crate::vfs::FileObject`].
59#[derive(Debug)]
60pub struct FileObjectState {
61    state: selinux_hooks::FileObjectState,
62}
63
64/// Opaque structure holding security state for a [`crate::vfs::FileSystem`].
65#[derive(Debug)]
66pub struct FileSystemState {
67    state: selinux_hooks::FileSystemState,
68}
69
70/// Opaque structure holding security state for a bpf [`ebpf_api::maps::Map`].
71#[derive(Debug)]
72pub struct BpfMapState {
73    state: selinux_hooks::BpfMapState,
74}
75
76/// Opaque structure holding security state for a bpf [`crate::bpf::program::Program`].
77#[derive(Debug)]
78pub struct BpfProgState {
79    state: selinux_hooks::BpfProgState,
80}
81
82/// Opaque structure holding security state for a PerfEventFileState.
83#[derive(Debug)]
84pub struct PerfEventState {
85    state: selinux_hooks::PerfEventState,
86}
87
88/// Opaque structure holding security state for a current task.
89#[derive(Default, Debug)]
90pub struct CurrentTaskState {
91    pub state: selinux_hooks::CurrentTaskState,
92}