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
// Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
use super::{Error, Result, SeccompCmpArgLen, SeccompCmpOp};
use crate::backend::bpf::*;
/// Condition that a syscall must match in order to satisfy a rule.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SeccompCondition {
/// Index of the argument that is to be compared.
arg_index: u8,
/// Length of the argument value that is to be compared.
arg_len: SeccompCmpArgLen,
/// Comparison operator to perform.
operator: SeccompCmpOp,
/// The value that will be compared with the argument value of the syscall.
value: u64,
}
impl SeccompCondition {
/// Creates a new [`SeccompCondition`].
///
/// # Arguments
///
/// * `arg_index` - Index of the argument that is to be compared.
/// * `arg_len` - Length of the argument value that is to be compared.
/// * `operator` - Comparison operator to perform.
/// * `value` - The value that will be compared with the argument value of the syscall.
///
/// # Example
///
/// ```
/// use seccompiler::{SeccompCmpArgLen, SeccompCmpOp, SeccompCondition};
///
/// let condition = SeccompCondition::new(0, SeccompCmpArgLen::Dword, SeccompCmpOp::Eq, 1).unwrap();
/// ```
///
/// [`SeccompCondition`]: struct.SeccompCondition.html
pub fn new(
arg_index: u8,
arg_len: SeccompCmpArgLen,
operator: SeccompCmpOp,
value: u64,
) -> Result<Self> {
let instance = Self {
arg_index,
arg_len,
operator,
value,
};
instance.validate().map(|_| Ok(instance))?
}
/// Validates the `SeccompCondition` data.
///
/// [`SeccompCondition`]: struct.SeccompCondition.html
fn validate(&self) -> Result<()> {
// Checks that the given argument number is valid.
if self.arg_index > ARG_NUMBER_MAX {
return Err(Error::InvalidArgumentNumber);
}
Ok(())
}
/// Computes the offsets of the syscall argument data passed to the BPF program.
///
/// Returns the offsets of the most significant and least significant halves of the argument
/// specified by `arg_index` relative to `struct seccomp_data` passed to the BPF program by
/// the kernel.
fn get_data_offsets(&self) -> (u8, u8) {
// Offset to the argument specified by `arg_index`.
// Cannot overflow because the value will be at most 16 + 5 * 8 = 56.
let arg_offset = SECCOMP_DATA_ARGS_OFFSET + self.arg_index * SECCOMP_DATA_ARG_SIZE;
// Extracts offsets of most significant and least significant halves of argument.
// Addition cannot overflow because it's at most `arg_offset` + 4 = 68.
(arg_offset + SECCOMP_DATA_ARG_SIZE / 2, arg_offset)
}
/// Splits the `value` field into 32 bit chunks
///
/// Returns the most significant and least significant halves of the `value`.
fn split_value(&self) -> (u32, u32) {
((self.value >> 32) as u32, self.value as u32)
}
/// Translates the `eq` (equal) condition into BPF statements.
///
/// # Arguments
///
/// * `offset` - The given jump offset to the start of the next rule.
fn into_eq_bpf(self, offset: u8) -> Vec<sock_filter> {
let (msb, lsb) = self.split_value();
let (msb_offset, lsb_offset) = self.get_data_offsets();
let mut bpf = match self.arg_len {
SeccompCmpArgLen::Dword => vec![],
SeccompCmpArgLen::Qword => vec![
bpf_stmt(BPF_LD | BPF_W | BPF_ABS, u32::from(msb_offset)),
bpf_jump(BPF_JMP | BPF_JEQ | BPF_K, msb, 0, offset + 2),
],
};
bpf.append(&mut vec![
bpf_stmt(BPF_LD | BPF_W | BPF_ABS, u32::from(lsb_offset)),
bpf_jump(BPF_JMP | BPF_JEQ | BPF_K, lsb, 0, offset),
]);
bpf
}
/// Translates the `ne` (not equal) condition into BPF statements.
///
/// # Arguments
///
/// * `offset` - The given jump offset to the start of the next rule.
fn into_ne_bpf(self, offset: u8) -> Vec<sock_filter> {
let (msb, lsb) = self.split_value();
let (msb_offset, lsb_offset) = self.get_data_offsets();
let mut bpf = match self.arg_len {
SeccompCmpArgLen::Dword => vec![],
SeccompCmpArgLen::Qword => vec![
bpf_stmt(BPF_LD | BPF_W | BPF_ABS, u32::from(msb_offset)),
bpf_jump(BPF_JMP | BPF_JEQ | BPF_K, msb, 0, 2),
],
};
bpf.append(&mut vec![
bpf_stmt(BPF_LD | BPF_W | BPF_ABS, u32::from(lsb_offset)),
bpf_jump(BPF_JMP | BPF_JEQ | BPF_K, lsb, offset, 0),
]);
bpf
}
/// Translates the `ge` (greater than or equal) condition into BPF statements.
///
/// # Arguments
///
/// * `offset` - The given jump offset to the start of the next rule.
fn into_ge_bpf(self, offset: u8) -> Vec<sock_filter> {
let (msb, lsb) = self.split_value();
let (msb_offset, lsb_offset) = self.get_data_offsets();
let mut bpf = match self.arg_len {
SeccompCmpArgLen::Dword => vec![],
SeccompCmpArgLen::Qword => vec![
bpf_stmt(BPF_LD | BPF_W | BPF_ABS, u32::from(msb_offset)),
bpf_jump(BPF_JMP | BPF_JGT | BPF_K, msb, 3, 0),
bpf_jump(BPF_JMP | BPF_JEQ | BPF_K, msb, 0, offset + 2),
],
};
bpf.append(&mut vec![
bpf_stmt(BPF_LD | BPF_W | BPF_ABS, u32::from(lsb_offset)),
bpf_jump(BPF_JMP | BPF_JGE | BPF_K, lsb, 0, offset),
]);
bpf
}
/// Translates the `gt` (greater than) condition into BPF statements.
///
/// # Arguments
///
/// * `offset` - The given jump offset to the start of the next rule.
fn into_gt_bpf(self, offset: u8) -> Vec<sock_filter> {
let (msb, lsb) = self.split_value();
let (msb_offset, lsb_offset) = self.get_data_offsets();
let mut bpf = match self.arg_len {
SeccompCmpArgLen::Dword => vec![],
SeccompCmpArgLen::Qword => vec![
bpf_stmt(BPF_LD | BPF_W | BPF_ABS, u32::from(msb_offset)),
bpf_jump(BPF_JMP | BPF_JGT | BPF_K, msb, 3, 0),
bpf_jump(BPF_JMP | BPF_JEQ | BPF_K, msb, 0, offset + 2),
],
};
bpf.append(&mut vec![
bpf_stmt(BPF_LD | BPF_W | BPF_ABS, u32::from(lsb_offset)),
bpf_jump(BPF_JMP | BPF_JGT | BPF_K, lsb, 0, offset),
]);
bpf
}
/// Translates the `le` (less than or equal) condition into BPF statements.
///
/// # Arguments
///
/// * `offset` - The given jump offset to the start of the next rule.
fn into_le_bpf(self, offset: u8) -> Vec<sock_filter> {
let (msb, lsb) = self.split_value();
let (msb_offset, lsb_offset) = self.get_data_offsets();
let mut bpf = match self.arg_len {
SeccompCmpArgLen::Dword => vec![],
SeccompCmpArgLen::Qword => vec![
bpf_stmt(BPF_LD | BPF_W | BPF_ABS, u32::from(msb_offset)),
bpf_jump(BPF_JMP | BPF_JGT | BPF_K, msb, offset + 3, 0),
bpf_jump(BPF_JMP | BPF_JEQ | BPF_K, msb, 0, 2),
],
};
bpf.append(&mut vec![
bpf_stmt(BPF_LD | BPF_W | BPF_ABS, u32::from(lsb_offset)),
bpf_jump(BPF_JMP | BPF_JGT | BPF_K, lsb, offset, 0),
]);
bpf
}
/// Translates the `lt` (less than) condition into BPF statements.
///
/// # Arguments
///
/// * `offset` - The given jump offset to the start of the next rule.
fn into_lt_bpf(self, offset: u8) -> Vec<sock_filter> {
let (msb, lsb) = self.split_value();
let (msb_offset, lsb_offset) = self.get_data_offsets();
let mut bpf = match self.arg_len {
SeccompCmpArgLen::Dword => vec![],
SeccompCmpArgLen::Qword => vec![
bpf_stmt(BPF_LD | BPF_W | BPF_ABS, u32::from(msb_offset)),
bpf_jump(BPF_JMP | BPF_JGT | BPF_K, msb, offset + 3, 0),
bpf_jump(BPF_JMP | BPF_JEQ | BPF_K, msb, 0, 2),
],
};
bpf.append(&mut vec![
bpf_stmt(BPF_LD | BPF_W | BPF_ABS, u32::from(lsb_offset)),
bpf_jump(BPF_JMP | BPF_JGE | BPF_K, lsb, offset, 0),
]);
bpf
}
/// Translates the `masked_eq` (masked equal) condition into BPF statements.
///
/// The `masked_eq` condition is `true` if the result of logical `AND` between the given value
/// and the mask is the value being compared against.
///
/// # Arguments
///
/// * `offset` - The given jump offset to the start of the next rule.
fn into_masked_eq_bpf(mut self, offset: u8, mask: u64) -> Vec<sock_filter> {
// Mask the current value.
self.value &= mask;
let (msb_offset, lsb_offset) = self.get_data_offsets();
let (msb, lsb) = self.split_value();
let (mask_msb, mask_lsb) = ((mask >> 32) as u32, mask as u32);
let mut bpf = match self.arg_len {
SeccompCmpArgLen::Dword => vec![],
SeccompCmpArgLen::Qword => vec![
bpf_stmt(BPF_LD | BPF_W | BPF_ABS, u32::from(msb_offset)),
bpf_stmt(BPF_ALU | BPF_AND | BPF_K, mask_msb),
bpf_jump(BPF_JMP | BPF_JEQ | BPF_K, msb, 0, offset + 3),
],
};
bpf.append(&mut vec![
bpf_stmt(BPF_LD | BPF_W | BPF_ABS, u32::from(lsb_offset)),
bpf_stmt(BPF_ALU | BPF_AND | BPF_K, mask_lsb),
bpf_jump(BPF_JMP | BPF_JEQ | BPF_K, lsb, 0, offset),
]);
bpf
}
/// Translates the [`SeccompCondition`] into BPF statements.
///
/// # Arguments
///
/// * `offset` - The given jump offset to the start of the next rule.
///
/// The jump is performed if the condition fails and thus the current rule does not match so
/// `seccomp` tries to match the next rule by jumping out of the current rule.
///
/// In case the condition is part of the last rule, the jump offset is to the default action of
/// respective filter.
///
/// The most significant and least significant halves of the argument value are compared
/// separately since the BPF operand and accumulator are 4 bytes whereas an argument value is 8.
pub(crate) fn into_bpf(self, offset: u8) -> Vec<sock_filter> {
let result = match self.operator {
SeccompCmpOp::Eq => self.into_eq_bpf(offset),
SeccompCmpOp::Ge => self.into_ge_bpf(offset),
SeccompCmpOp::Gt => self.into_gt_bpf(offset),
SeccompCmpOp::Le => self.into_le_bpf(offset),
SeccompCmpOp::Lt => self.into_lt_bpf(offset),
SeccompCmpOp::MaskedEq(mask) => self.into_masked_eq_bpf(offset, mask),
SeccompCmpOp::Ne => self.into_ne_bpf(offset),
};
// Verifies that the `CONDITION_MAX_LEN` constant was properly updated.
assert!(result.len() <= CONDITION_MAX_LEN as usize);
result
}
}
#[cfg(test)]
#[allow(clippy::undocumented_unsafe_blocks)]
mod tests {
use super::*;
#[test]
fn test_new_condition() {
assert!(SeccompCondition::new(0, SeccompCmpArgLen::Dword, SeccompCmpOp::Eq, 60).is_ok());
assert_eq!(
SeccompCondition::new(7, SeccompCmpArgLen::Dword, SeccompCmpOp::Eq, 60).unwrap_err(),
Error::InvalidArgumentNumber
);
}
#[test]
fn test_get_data_offsets() {
let cond = SeccompCondition::new(1, SeccompCmpArgLen::Qword, SeccompCmpOp::Eq, 60).unwrap();
let (msb_offset, lsb_offset) = cond.get_data_offsets();
assert_eq!(
(msb_offset, lsb_offset),
(
SECCOMP_DATA_ARGS_OFFSET + SECCOMP_DATA_ARG_SIZE + 4,
SECCOMP_DATA_ARGS_OFFSET + SECCOMP_DATA_ARG_SIZE
)
);
let data = libc::seccomp_data {
nr: 0,
arch: 0,
instruction_pointer: AUDIT_ARCH_X86_64 as u64,
args: [
u64::MAX,
u32::MAX as u64 + 1,
u64::MAX,
u64::MAX,
u64::MAX,
u64::MAX,
],
};
let data_ptr = (&data as *const libc::seccomp_data) as *const u32;
assert_eq!(
unsafe { *(data_ptr.offset((lsb_offset / 4) as isize) as *const u32) },
0
);
assert_eq!(
unsafe { *(data_ptr.offset((msb_offset / 4) as isize) as *const u32) },
1
);
}
#[test]
fn test_split_value() {
let cond = SeccompCondition::new(
1,
SeccompCmpArgLen::Qword,
SeccompCmpOp::Eq,
u32::MAX as u64 + 1,
)
.unwrap();
assert_eq!(cond.split_value(), (1, 0));
}
}