1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/// A class that may appear in SELinux policy or an access vector cache query.
#[derive(Clone, Debug, PartialEq)]
pub enum AbstractObjectClass {
Unspecified,
/// A well-known class used in the SELinux system, such as `process` or `file`.
System(ObjectClass),
/// A custom class that only has meaning in policies that define class with the given string
/// name.
Custom(String),
}
impl Default for AbstractObjectClass {
fn default() -> Self {
Self::Unspecified
}
}
macro_rules! enumerable_enum {
($(#[$meta:meta])* $name:ident {
$($(#[$variant_meta:meta])* $variant:ident),*,
}) => {
$(#[$meta])*
pub enum $name {
$($(#[$variant_meta])* $variant),*
}
impl $name {
pub fn all_variants() -> Vec<Self> {
vec![
$($name::$variant),*
]
}
}
}
}
enumerable_enum! {
/// A well-known class in SELinux policy that has a particular meaning in policy enforcement
/// hooks.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
ObjectClass {
/// The SELinux "process" object class.
Process,
/// The SELinux "file" object class.
File,
/// The SELinux "blk_file" object class.
Block,
/// The SELinux "chr_file" object class.
Character,
/// The SELinux "lnk_file" object class.
Link,
/// The SELinux "fifo_file" object class.
Fifo,
/// The SELinux "sock_file" object class.
Socket,
}
}
impl ObjectClass {
pub fn name(&self) -> &'static str {
match self {
Self::Process => "process",
Self::File => "file",
Self::Block => "blk_file",
Self::Character => "chr_file",
Self::Link => "lnk_file",
Self::Fifo => "fifo_file",
Self::Socket => "sock_file",
}
}
}
impl From<ObjectClass> for AbstractObjectClass {
fn from(object_class: ObjectClass) -> Self {
Self::System(object_class)
}
}
impl From<String> for AbstractObjectClass {
fn from(name: String) -> Self {
Self::Custom(name)
}
}
enumerable_enum! {
/// A well-known file-like class in SELinux policy that has a particular meaning in policy
/// enforcement hooks.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
FileClass {
/// The SELinux "file" object class.
File,
/// The SELinux "blk_file" object class.
Block,
/// The SELinux "chr_file" object class.
Character,
/// The SELinux "lnk_file" object class.
Link,
/// The SELinux "fifo_file" object class.
Fifo,
/// The SELinux "sock_file" object class.
Socket,
}
}
impl From<FileClass> for ObjectClass {
fn from(file_class: FileClass) -> Self {
match file_class {
FileClass::File => Self::File,
FileClass::Block => Self::Block,
FileClass::Character => Self::Character,
FileClass::Link => Self::Link,
FileClass::Fifo => Self::Fifo,
FileClass::Socket => Self::Socket,
}
}
}
/// A permission that may appear in SELinux policy or an access vector cache query.
#[derive(Clone, Debug, PartialEq)]
pub enum AbstractPermission {
/// A permission that is interpreted directly by the system. These are kernel objects such as
/// a "process", "file", etc.
System(Permission),
/// A permission with an arbitrary string identifier.
Custom { class: AbstractObjectClass, permission: String },
}
impl AbstractPermission {
pub fn new_custom(class: AbstractObjectClass, permission: String) -> Self {
Self::Custom { class, permission }
}
}
impl From<Permission> for AbstractPermission {
fn from(permission: Permission) -> Self {
Self::System(permission)
}
}
pub trait ClassPermission {
fn class(&self) -> ObjectClass;
}
macro_rules! permission_enum {
($(#[$meta:meta])* $name:ident {
$($(#[$variant_meta:meta])* $variant:ident($inner:ident)),*,
}) => {
$(#[$meta])*
pub enum $name {
$($(#[$variant_meta])* $variant($inner)),*
}
$(impl From<$inner> for $name {
fn from(v: $inner) -> Self {
Self::$variant(v)
}
})*
impl ClassPermission for $name {
fn class(&self) -> ObjectClass {
match self {
$($name::$variant(_) => ObjectClass::$variant),*
}
}
}
impl $name {
pub fn name(&self) -> &'static str {
match self {
$($name::$variant(v) => v.name()),*
}
}
pub fn all_variants() -> Vec<Self> {
let mut all_variants = vec![];
$(all_variants.extend($inner::all_variants().into_iter().map($name::from));)*
all_variants
}
}
}
}
permission_enum! {
/// A well-known `(class, permission)` pair in SELinux policy that has a particular meaning in
/// policy enforcement hooks.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
Permission {
/// Permissions for the well-known SELinux "process" object class.
Process(ProcessPermission),
/// Permissions for the well-known SELinux "file" object class.
File(FilePermission),
}
}
macro_rules! class_permission_enum {
($(#[$meta:meta])* $name:ident {
$($(#[$variant_meta:meta])* $variant:ident ($variant_name:literal)),*,
}) => {
enumerable_enum! {
$(#[$meta])* $name {
$($(#[$variant_meta])* $variant),*,
}
}
impl ClassPermission for $name {
fn class(&self) -> ObjectClass {
Permission::from(self.clone()).class()
}
}
impl $name {
fn name(&self) -> &'static str {
match self {
$($name::$variant => $variant_name),*
}
}
}
}
}
class_permission_enum! {
/// A well-known "process" class permission in SELinux policy that has a particular meaning in
/// policy enforcement hooks.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
ProcessPermission {
/// Permission to fork the current running process.
Fork("fork"),
/// Permission to transition to a different security domain.
Transition("transition"),
/// Permission to get scheduling policy currently applied to a process.
GetSched("getsched"),
/// Permission to set scheduling policy for a process.
SetSched("setsched"),
/// Permission to get the process group ID.
GetPgid("getpgid"),
/// Permission to set the process group ID.
SetPgid("setpgid"),
/// Permission to send a signal other than SIGKILL, SIGSTOP, or SIGCHLD to a process.
Signal("signal"),
/// Permission to send SIGKILL to a process.
SigKill("sigkill"),
/// Permission to send SIGSTOP to a process.
SigStop("sigstop"),
/// Permission to send SIGCHLD to a process.
SigChld("sigchld"),
/// Permission to trace a process.
Ptrace("ptrace"),
/// Permission to get the session ID.
GetSession("getsession"),
}
}
class_permission_enum! {
/// A well-known "file" class permission in SELinux policy that has a particular meaning in
/// policy enforcement hooks.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
FilePermission {
/// Permission to create a file.
Create("create"),
/// Permission to open a file.
Open("open"),
/// Permission to use a file as an entry point to the calling domain without performing a
/// transition.
ExecuteNoTrans("execute_no_trans"),
/// Permission to use a file as an entry point into the new domain on transition.
Entrypoint("entrypoint"),
}
}
/// Initial Security Identifier (SID) values defined by the SELinux Reference Policy.
/// The presence and ordering of all values, including ones unused by the implementation,
/// ensures that the numeric values match those output by userspace policy tooling.
#[repr(u64)]
enum ReferenceInitialSid {
Kernel = 1,
_Security,
Unlabeled,
_Fs,
_File,
_FileLabels,
_Init,
_AnySocket,
_Port,
_Netif,
_Netmsg,
_Node,
_IgmpPacket,
_IcmpSocket,
_TcpSocket,
_SysctlModprobe,
_Sysctl,
_SysctlFs,
_SysctlKernel,
_SysctlNet,
_SysctlNetUnix,
_SysctlVm,
_SysctlDev,
_Kmod,
_Policy,
_ScmpPacket,
_Devnull,
FirstUnused,
}
/// Lowest Security Identifier value guaranteed not to be used by this
/// implementation to refer to an initial Security Context.
pub const FIRST_UNUSED_SID: u32 = ReferenceInitialSid::FirstUnused as u32;
macro_rules! initial_sid_enum {
($(#[$meta:meta])* $name:ident {
$($(#[$variant_meta:meta])* $variant:ident ($variant_name: literal)),*,
}) => {
$(#[$meta])*
pub enum $name {
$($(#[$variant_meta])* $variant = ReferenceInitialSid::$variant as isize),*
}
impl $name {
pub fn all_variants() -> Vec<Self> {
vec![
$($name::$variant),*
]
}
pub fn name(&self) -> &'static str {
match self {
$($name::$variant => $variant_name),*
}
}
}
}
}
initial_sid_enum! {
/// Initial Security Identifier (SID) values actually used by this implementation.
/// These must be present in the policy, for it to be valid.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
InitialSid {
Kernel("kernel"),
Unlabeled("unlabeled"),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn object_class_permissions() {
assert_eq!(AbstractObjectClass::Unspecified, AbstractObjectClass::default());
assert_eq!(
AbstractObjectClass::Custom(String::from("my_class")),
String::from("my_class").into()
);
for variant in ProcessPermission::all_variants().into_iter() {
assert_eq!(ObjectClass::Process, variant.class());
assert_eq!("process", variant.class().name());
let permission: Permission = variant.clone().into();
assert_eq!(Permission::Process(variant.clone()), permission);
assert_eq!(
AbstractPermission::System(Permission::Process(variant.clone())),
permission.into()
);
assert_eq!(AbstractObjectClass::System(ObjectClass::Process), variant.class().into());
}
}
}