Skip to main content

selinux/new_policy/
access_vector.rs

1// Copyright 2026 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
5use std::fmt::{Debug, Formatter, LowerHex};
6use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Not, Sub, SubAssign};
7use std::str::FromStr;
8
9use selinux_policy_derive::{Parse, Serialize, Validate};
10
11use super::PermissionId;
12use super::traits::PolicyId;
13
14/// Set of permissions that may be granted to sources accessing targets of a particular class.
15#[derive(
16    Copy, Clone, Default, Eq, Hash, PartialEq, PartialOrd, Ord, Parse, Serialize, Validate,
17)]
18pub struct AccessVector(u32);
19
20impl AccessVector {
21    pub const NONE: Self = Self(0);
22    pub const ALL: Self = Self(std::u32::MAX);
23
24    pub fn from_class_permission_id(id: PermissionId) -> Self {
25        Self::from(id)
26    }
27
28    /// Returns the raw access vector value.
29    pub fn value(&self) -> u32 {
30        self.0
31    }
32}
33
34impl From<u32> for AccessVector {
35    fn from(value: u32) -> Self {
36        Self(value)
37    }
38}
39
40impl From<AccessVector> for u32 {
41    fn from(value: AccessVector) -> Self {
42        value.0
43    }
44}
45
46impl Debug for AccessVector {
47    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
48        write!(f, "AccessVector({:0>8x})", self.0)
49    }
50}
51
52impl LowerHex for AccessVector {
53    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
54        LowerHex::fmt(&self.0, f)
55    }
56}
57
58impl FromStr for AccessVector {
59    type Err = <u32 as FromStr>::Err;
60
61    fn from_str(value: &str) -> Result<Self, Self::Err> {
62        // Access Vector values are always serialized to/from hexadecimal.
63        Ok(Self(u32::from_str_radix(value, 16)?))
64    }
65}
66
67impl BitAnd for AccessVector {
68    type Output = Self;
69
70    fn bitand(self, rhs: Self) -> Self::Output {
71        Self(self.0 & rhs.0)
72    }
73}
74
75impl BitOr for AccessVector {
76    type Output = Self;
77
78    fn bitor(self, rhs: Self) -> Self::Output {
79        Self(self.0 | rhs.0)
80    }
81}
82
83impl BitAndAssign for AccessVector {
84    fn bitand_assign(&mut self, rhs: Self) {
85        self.0 &= rhs.0
86    }
87}
88
89impl BitOrAssign for AccessVector {
90    fn bitor_assign(&mut self, rhs: Self) {
91        self.0 |= rhs.0
92    }
93}
94
95impl SubAssign for AccessVector {
96    fn sub_assign(&mut self, rhs: Self) {
97        self.0 = self.0 ^ (self.0 & rhs.0);
98    }
99}
100
101impl Sub for AccessVector {
102    type Output = Self;
103
104    fn sub(self, rhs: Self) -> Self::Output {
105        Self(self.0 ^ (self.0 & rhs.0))
106    }
107}
108
109impl Not for AccessVector {
110    type Output = Self;
111
112    fn not(self) -> Self {
113        Self(!self.0)
114    }
115}
116
117impl From<PermissionId> for AccessVector {
118    fn from(id: PermissionId) -> Self {
119        Self((1 as u32) << (id.as_u32() - 1))
120    }
121}