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
use byteorder::{ByteOrder, NativeEndian};
use crate::{constants::*, DecodeError};
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct RuleSyscalls(pub(crate) Vec<u32>);
const BITMASK_BYTE_LEN: usize = AUDIT_BITMASK_SIZE * 4;
const BITMASK_BIT_LEN: u32 = AUDIT_BITMASK_SIZE as u32 * 32;
impl RuleSyscalls {
pub fn from_slice(slice: &[u8]) -> Result<Self, DecodeError> {
if slice.len() != BITMASK_BYTE_LEN {
return Err(DecodeError::from(format!(
"invalid bitmask size: expected {} bytes got {}",
BITMASK_BYTE_LEN,
slice.len()
)));
}
let mut mask = RuleSyscalls::new_zeroed();
let mut word = 0;
while word < AUDIT_BITMASK_SIZE {
mask.0[word] = NativeEndian::read_u32(&slice[word * 4..word * 4 + 4]);
word += 1;
}
Ok(mask)
}
pub fn new_zeroed() -> Self {
RuleSyscalls(vec![0; AUDIT_BITMASK_SIZE])
}
pub fn new_maxed() -> Self {
RuleSyscalls(vec![0xffff_ffff; AUDIT_BITMASK_SIZE])
}
pub fn unset_all(&mut self) -> &mut Self {
self.0 = vec![0; AUDIT_BITMASK_SIZE];
self
}
pub fn is_all(&self) -> bool {
for i in 0..AUDIT_BITMASK_SIZE {
if self.0[i] != 0xffff_ffff {
return false;
}
}
true
}
pub fn set_all(&mut self) -> &mut Self {
self.0 = vec![0xffff_ffff; AUDIT_BITMASK_SIZE];
self
}
pub fn unset(&mut self, syscall: u32) -> &mut Self {
let (word, mask) = Self::syscall_coordinates(syscall);
self.0[word] &= !mask;
self
}
pub fn set(&mut self, syscall: u32) -> &mut Self {
let (word, mask) = Self::syscall_coordinates(syscall);
self.0[word] |= mask;
self
}
pub fn has(&self, syscall: u32) -> bool {
let (word, mask) = Self::syscall_coordinates(syscall);
self.0[word] & mask == mask
}
fn syscall_coordinates(syscall: u32) -> (usize, u32) {
let word_index = syscall / 32;
let mask = 0x0000_0001 << (syscall - word_index * 32);
(word_index as usize, mask)
}
}
pub struct RuleSyscallsIter<T> {
index: u32,
syscalls: T,
}
impl IntoIterator for RuleSyscalls {
type Item = u32;
type IntoIter = RuleSyscallsIter<RuleSyscalls>;
fn into_iter(self) -> Self::IntoIter {
RuleSyscallsIter {
index: 0,
syscalls: self,
}
}
}
impl Iterator for RuleSyscallsIter<RuleSyscalls> {
type Item = u32;
fn next(&mut self) -> Option<Self::Item> {
while self.index < BITMASK_BIT_LEN {
let index = self.index;
self.index += 1;
if self.syscalls.has(index) {
return Some(index as u32);
}
}
None
}
}
impl<'a> IntoIterator for &'a RuleSyscalls {
type Item = u32;
type IntoIter = RuleSyscallsIter<&'a RuleSyscalls>;
fn into_iter(self) -> Self::IntoIter {
RuleSyscallsIter {
index: 0,
syscalls: self,
}
}
}
impl<'a> Iterator for RuleSyscallsIter<&'a RuleSyscalls> {
type Item = u32;
fn next(&mut self) -> Option<Self::Item> {
while self.index < BITMASK_BIT_LEN {
let index = self.index;
self.index += 1;
if self.syscalls.has(index) {
return Some(index as u32);
}
}
None
}
}
impl<'a> IntoIterator for &'a mut RuleSyscalls {
type Item = u32;
type IntoIter = RuleSyscallsIter<&'a mut RuleSyscalls>;
fn into_iter(self) -> Self::IntoIter {
RuleSyscallsIter {
index: 0,
syscalls: self,
}
}
}
impl<'a> Iterator for RuleSyscallsIter<&'a mut RuleSyscalls> {
type Item = u32;
fn next(&mut self) -> Option<Self::Item> {
while self.index < BITMASK_BIT_LEN {
let index = self.index;
self.index += 1;
if self.syscalls.has(index) {
return Some(index as u32);
}
}
None
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_from_slice() {
let s: Vec<u8> = vec![0xff; BITMASK_BYTE_LEN];
let syscalls = RuleSyscalls::from_slice(&s[..]).unwrap();
assert_eq!(syscalls.0, vec![0xffff_ffff; AUDIT_BITMASK_SIZE]);
let s: Vec<u8> = vec![0; BITMASK_BYTE_LEN];
let syscalls = RuleSyscalls::from_slice(&s[..]).unwrap();
assert_eq!(syscalls.0, vec![0; AUDIT_BITMASK_SIZE]);
}
#[test]
fn test_iter() {
let s: Vec<u8> = vec![0xff; BITMASK_BYTE_LEN];
let syscalls = RuleSyscalls::from_slice(&s[..]).unwrap();
let mut iter = syscalls.into_iter();
for i in 0..BITMASK_BIT_LEN {
assert_eq!(i as u32, iter.next().unwrap());
}
assert!(iter.next().is_none());
let s: Vec<u8> = vec![0; BITMASK_BYTE_LEN];
let syscalls = RuleSyscalls::from_slice(&s[..]).unwrap();
let mut iter = syscalls.into_iter();
assert!(iter.next().is_none());
}
#[test]
fn test_set_unset() {
let mut syscalls = RuleSyscalls::new_zeroed();
for i in 0..BITMASK_BIT_LEN {
syscalls.set(i);
}
assert_eq!(syscalls.0, vec![0xffff_ffff; AUDIT_BITMASK_SIZE]);
for i in 0..BITMASK_BIT_LEN {
syscalls.unset(BITMASK_BIT_LEN - 1 - i);
}
assert_eq!(syscalls.0, vec![0; AUDIT_BITMASK_SIZE]);
}
}