| 1 | // SPDX-License-Identifier: Apache-2.0 or MIT |
| 2 | // |
| 3 | // Copyright 2021 Sony Group Corporation |
| 4 | // |
| 5 | |
| 6 | use crate::api::ensure_supported_api; |
| 7 | use crate::error::{Result, SeccompError}; |
| 8 | use libseccomp_sys::*; |
| 9 | use std::os::unix::io::{AsFd, AsRawFd}; |
| 10 | use std::ptr::NonNull; |
| 11 | |
| 12 | use crate::*; |
| 13 | |
| 14 | const MINUS_EEXIST: i32 = -libc::EEXIST; |
| 15 | |
| 16 | /// **Represents a filter context in the libseccomp.** |
| 17 | #[derive(Debug)] |
| 18 | pub struct ScmpFilterContext { |
| 19 | ctx: NonNull<libc::c_void>, |
| 20 | } |
| 21 | |
| 22 | impl ScmpFilterContext { |
| 23 | /// Creates and returns a new filter context. |
| 24 | /// |
| 25 | /// This initializes the internal seccomp filter state and should |
| 26 | /// be called before any other functions in this crate to ensure the filter |
| 27 | /// state is initialized. |
| 28 | /// |
| 29 | /// This function returns a valid filter context. |
| 30 | /// |
| 31 | /// This function corresponds to |
| 32 | /// [`seccomp_init`](https://man7.org/linux/man-pages/man3/seccomp_init.3.html). |
| 33 | /// |
| 34 | /// # Arguments |
| 35 | /// |
| 36 | /// * `default_action` - A default action to be taken for syscalls which match no rules in the filter |
| 37 | /// |
| 38 | /// # Errors |
| 39 | /// |
| 40 | /// If the filter context can not be created, an error will be returned. |
| 41 | /// |
| 42 | /// # Examples |
| 43 | /// |
| 44 | /// ``` |
| 45 | /// # use libseccomp::*; |
| 46 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 47 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 48 | /// ``` |
| 49 | pub fn new(default_action: ScmpAction) -> Result<Self> { |
| 50 | let ctx_ptr = unsafe { seccomp_init(default_action.to_sys()) }; |
| 51 | let ctx = NonNull::new(ctx_ptr) |
| 52 | .ok_or_else(|| SeccompError::with_msg("Could not create new filter"))?; |
| 53 | |
| 54 | Ok(Self { ctx }) |
| 55 | } |
| 56 | |
| 57 | /// Deprecated alias for [`ScmpFilterContext::new`]. |
| 58 | #[deprecated(since = "0.4.0", note = "Use ScmpFilterContext::new instead.")] |
| 59 | pub fn new_filter(default_action: ScmpAction) -> Result<Self> { |
| 60 | Self::new(default_action) |
| 61 | } |
| 62 | |
| 63 | /// Merges two filters. |
| 64 | /// |
| 65 | /// In order to merge two seccomp filters, both filters must have the same |
| 66 | /// attribute values and no overlapping architectures. |
| 67 | /// If successful, the `src` seccomp filter is released and all internal memory |
| 68 | /// associated with the filter is freed. |
| 69 | /// |
| 70 | /// This function corresponds to |
| 71 | /// [`seccomp_merge`](https://man7.org/linux/man-pages/man3/seccomp_merge.3.html). |
| 72 | /// |
| 73 | /// # Arguments |
| 74 | /// |
| 75 | /// * `src` - A seccomp filter that will be merged into the filter this is called on. |
| 76 | /// |
| 77 | /// # Errors |
| 78 | /// |
| 79 | /// If merging the filters fails, an error will be returned. |
| 80 | /// |
| 81 | /// # Examples |
| 82 | /// |
| 83 | /// ``` |
| 84 | /// # use libseccomp::*; |
| 85 | /// let mut ctx1 = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 86 | /// let mut ctx2 = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 87 | /// if !ctx1.is_arch_present(ScmpArch::X8664)? { |
| 88 | /// ctx1.add_arch(ScmpArch::X8664)?; |
| 89 | /// ctx1.remove_arch(ScmpArch::Native)?; |
| 90 | /// } |
| 91 | /// if !ctx2.is_arch_present(ScmpArch::Aarch64)? { |
| 92 | /// ctx2.add_arch(ScmpArch::Aarch64)?; |
| 93 | /// ctx2.remove_arch(ScmpArch::Native)?; |
| 94 | /// } |
| 95 | /// ctx1.merge(ctx2)?; |
| 96 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 97 | /// ``` |
| 98 | pub fn merge(&mut self, src: Self) -> Result<&mut Self> { |
| 99 | cvt(unsafe { seccomp_merge(self.ctx.as_ptr(), src.ctx.as_ptr()) })?; |
| 100 | |
| 101 | // The src filter is already released. |
| 102 | std::mem::forget(src); |
| 103 | |
| 104 | Ok(self) |
| 105 | } |
| 106 | |
| 107 | /// Checks if an architecture is present in a filter. |
| 108 | /// |
| 109 | /// If a filter contains an architecture, it uses its default action for |
| 110 | /// syscalls which do not match rules in it, and its rules can match syscalls |
| 111 | /// for that ABI. |
| 112 | /// If a filter does not contain an architecture, all syscalls made to that |
| 113 | /// kernel ABI will fail with the filter's default Bad Architecture Action |
| 114 | /// (by default, killing the proc). |
| 115 | /// |
| 116 | /// This function returns `Ok(true)` if the architecture is present in the filter, |
| 117 | /// `Ok(false)` otherwise. |
| 118 | /// |
| 119 | /// This function corresponds to |
| 120 | /// [`seccomp_arch_exist`](https://man7.org/linux/man-pages/man3/seccomp_arch_exist.3.html). |
| 121 | /// |
| 122 | /// # Arguments |
| 123 | /// |
| 124 | /// * `arch` - An architecture token |
| 125 | /// |
| 126 | /// # Errors |
| 127 | /// |
| 128 | /// If this function is called with an invalid filter or an issue is |
| 129 | /// encountered calling to the libseccomp API, an error will be returned. |
| 130 | /// |
| 131 | /// # Examples |
| 132 | /// |
| 133 | /// ``` |
| 134 | /// # use libseccomp::*; |
| 135 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 136 | /// ctx.add_arch(ScmpArch::Aarch64)?; |
| 137 | /// assert!(ctx.is_arch_present(ScmpArch::Aarch64)?); |
| 138 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 139 | /// ``` |
| 140 | pub fn is_arch_present(&self, arch: ScmpArch) -> Result<bool> { |
| 141 | match unsafe { seccomp_arch_exist(self.ctx.as_ptr(), arch.to_sys()) } { |
| 142 | 0 => Ok(true), |
| 143 | MINUS_EEXIST => Ok(false), |
| 144 | errno => Err(SeccompError::from_errno(errno)), |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /// Adds an architecture to the filter. |
| 149 | /// |
| 150 | /// When this functions exits successfully the architecture is (now) present |
| 151 | /// in the filter. |
| 152 | /// |
| 153 | /// This function corresponds to |
| 154 | /// [`seccomp_arch_add`](https://man7.org/linux/man-pages/man3/seccomp_arch_add.3.html). |
| 155 | /// |
| 156 | /// # Arguments |
| 157 | /// |
| 158 | /// * `arch` - An architecture token |
| 159 | /// |
| 160 | /// # Errors |
| 161 | /// |
| 162 | /// If this function is called with an invalid filter or an issue is |
| 163 | /// encountered adding the architecture, an error will be returned. |
| 164 | /// |
| 165 | /// # Examples |
| 166 | /// |
| 167 | /// ``` |
| 168 | /// # use libseccomp::*; |
| 169 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 170 | /// ctx.add_arch(ScmpArch::X86)?; |
| 171 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 172 | /// ``` |
| 173 | pub fn add_arch(&mut self, arch: ScmpArch) -> Result<&mut Self> { |
| 174 | match unsafe { seccomp_arch_add(self.ctx.as_ptr(), arch.to_sys()) } { |
| 175 | // The libseccomp returns -EEXIST if the specified architecture is already |
| 176 | // present. Succeed silently in this case, as it's not fatal, and the |
| 177 | // architecture is present already. |
| 178 | 0 | MINUS_EEXIST => Ok(self), |
| 179 | errno => Err(SeccompError::from_errno(errno)), |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /// Removes an architecture from the filter. |
| 184 | /// |
| 185 | /// When this functions exits successfully the architecture is not present |
| 186 | /// in the filter (anymore). |
| 187 | /// |
| 188 | /// This function corresponds to |
| 189 | /// [`seccomp_arch_remove`](https://man7.org/linux/man-pages/man3/seccomp_arch_remove.3.html). |
| 190 | /// |
| 191 | /// # Arguments |
| 192 | /// |
| 193 | /// * `arch` - An architecture token |
| 194 | /// |
| 195 | /// # Errors |
| 196 | /// |
| 197 | /// If this function is called with an invalid filter or an issue is |
| 198 | /// encountered removing the architecture, an error will be returned. |
| 199 | /// |
| 200 | /// # Examples |
| 201 | /// |
| 202 | /// ``` |
| 203 | /// # use libseccomp::*; |
| 204 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 205 | /// ctx.add_arch(ScmpArch::X86)?; |
| 206 | /// ctx.remove_arch(ScmpArch::X86)?; |
| 207 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 208 | /// ``` |
| 209 | pub fn remove_arch(&mut self, arch: ScmpArch) -> Result<&mut Self> { |
| 210 | match unsafe { seccomp_arch_remove(self.ctx.as_ptr(), arch.to_sys()) } { |
| 211 | // Similar to add_arch, -EEXIST is returned if the arch is not present. |
| 212 | // Succeed silently in that case, this is not fatal and the architecture |
| 213 | // is not present in the filter after remove_arch |
| 214 | 0 | MINUS_EEXIST => Ok(self), |
| 215 | errno => Err(SeccompError::from_errno(errno)), |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /// Adds a single rule for an unconditional action on a syscall. |
| 220 | /// |
| 221 | /// If the specified rule needs to be rewritten due to architecture specifics, |
| 222 | /// it will be rewritten without notification. |
| 223 | /// |
| 224 | /// This function corresponds to |
| 225 | /// [`seccomp_rule_add`](https://man7.org/linux/man-pages/man3/seccomp_rule_add.3.html). |
| 226 | /// |
| 227 | /// # Arguments |
| 228 | /// |
| 229 | /// * `action` - An action to be taken on the call being made |
| 230 | /// * `syscall` - The number of syscall |
| 231 | /// |
| 232 | /// # Errors |
| 233 | /// |
| 234 | /// If this function is called with an invalid filter or an issue is |
| 235 | /// encountered adding the rule, an error will be returned. |
| 236 | /// |
| 237 | /// # Examples |
| 238 | /// |
| 239 | /// ``` |
| 240 | /// # use libseccomp::*; |
| 241 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 242 | /// let syscall = ScmpSyscall::from_name("ptrace")?; |
| 243 | /// ctx.add_rule(ScmpAction::Errno(libc::EPERM), syscall)?; |
| 244 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 245 | /// ``` |
| 246 | pub fn add_rule<S: Into<ScmpSyscall>>( |
| 247 | &mut self, |
| 248 | action: ScmpAction, |
| 249 | syscall: S, |
| 250 | ) -> Result<&mut Self> { |
| 251 | self.add_rule_conditional(action, syscall, &[]) |
| 252 | } |
| 253 | |
| 254 | /// Adds a single rule for a conditional action on a syscall. |
| 255 | /// |
| 256 | /// If the specified rule needs to be rewritten due to architecture specifics, |
| 257 | /// it will be rewritten without notification. |
| 258 | /// Comparators are AND'd together (i.e. all must match for the rule to match). |
| 259 | /// You can only compare each argument once in a single rule. |
| 260 | /// |
| 261 | /// This function corresponds to |
| 262 | /// [`seccomp_rule_add_array`](https://man7.org/linux/man-pages/man3/seccomp_rule_add_array.3.html). |
| 263 | /// |
| 264 | /// # Arguments |
| 265 | /// |
| 266 | /// * `action` - An action to be taken on the call being made |
| 267 | /// * `syscall` - The number of syscall |
| 268 | /// * `comparators` - An array of the rule in a seccomp filter |
| 269 | /// |
| 270 | /// # Errors |
| 271 | /// |
| 272 | /// If this function is called with an invalid filter or an issue is |
| 273 | /// encountered adding the rule, an error will be returned. |
| 274 | /// |
| 275 | /// # Examples |
| 276 | /// |
| 277 | /// ``` |
| 278 | /// # use libseccomp::*; |
| 279 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 280 | /// let syscall = ScmpSyscall::from_name("open")?; |
| 281 | /// ctx.add_rule_conditional( |
| 282 | /// ScmpAction::Errno(libc::EPERM), |
| 283 | /// syscall, |
| 284 | /// &[scmp_cmp!($arg1 & (libc::O_TRUNC as u64) == libc::O_TRUNC as u64)], |
| 285 | /// )?; |
| 286 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 287 | /// ``` |
| 288 | pub fn add_rule_conditional<S: Into<ScmpSyscall>>( |
| 289 | &mut self, |
| 290 | action: ScmpAction, |
| 291 | syscall: S, |
| 292 | comparators: &[ScmpArgCompare], |
| 293 | ) -> Result<&mut Self> { |
| 294 | cvt(unsafe { |
| 295 | seccomp_rule_add_array( |
| 296 | self.ctx.as_ptr(), |
| 297 | action.to_sys(), |
| 298 | syscall.into().to_sys(), |
| 299 | comparators.len() as u32, |
| 300 | comparators.as_ptr().cast::<scmp_arg_cmp>(), |
| 301 | ) |
| 302 | })?; |
| 303 | Ok(self) |
| 304 | } |
| 305 | |
| 306 | /// Adds a single rule for an unconditional action on a syscall. |
| 307 | /// |
| 308 | /// The functions will attempt to add the rule exactly as specified so it may |
| 309 | /// behave differently on different architectures. |
| 310 | /// If the specified rule can not be represented on the architecture, |
| 311 | /// the function will fail. |
| 312 | /// |
| 313 | /// This function corresponds to |
| 314 | /// [`seccomp_rule_add_exact`](https://man7.org/linux/man-pages/man3/seccomp_rule_add_exact.3.html). |
| 315 | /// |
| 316 | /// # Arguments |
| 317 | /// |
| 318 | /// * `action` - An action to be taken on the call being made |
| 319 | /// * `syscall` - The number of syscall |
| 320 | /// |
| 321 | /// # Errors |
| 322 | /// |
| 323 | /// If this function is called with an invalid filter or an issue is |
| 324 | /// encountered adding the rule, an error will be returned. |
| 325 | /// |
| 326 | /// # Examples |
| 327 | /// |
| 328 | /// ``` |
| 329 | /// # use libseccomp::*; |
| 330 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 331 | /// let syscall = ScmpSyscall::from_name("dup3")?; |
| 332 | /// ctx.add_rule_exact(ScmpAction::KillThread, syscall)?; |
| 333 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 334 | /// ``` |
| 335 | pub fn add_rule_exact<S: Into<ScmpSyscall>>( |
| 336 | &mut self, |
| 337 | action: ScmpAction, |
| 338 | syscall: S, |
| 339 | ) -> Result<&mut Self> { |
| 340 | self.add_rule_conditional_exact(action, syscall, &[]) |
| 341 | } |
| 342 | |
| 343 | /// Adds a single rule for a conditional action on a syscall. |
| 344 | /// |
| 345 | /// The functions will attempt to add the rule exactly as specified so it may |
| 346 | /// behave differently on different architectures. |
| 347 | /// If the specified rule can not be represented on the architecture, |
| 348 | /// the function will fail. |
| 349 | /// |
| 350 | /// This function corresponds to |
| 351 | /// [`seccomp_rule_add_exact_array`](https://man7.org/linux/man-pages/man3/seccomp_rule_add_exact_array.3.html). |
| 352 | /// |
| 353 | /// # Arguments |
| 354 | /// |
| 355 | /// * `action` - An action to be taken on the call being made |
| 356 | /// * `syscall` - The number of syscall |
| 357 | /// * `comparators` - An array of the rule in a seccomp filter |
| 358 | /// |
| 359 | /// # Errors |
| 360 | /// |
| 361 | /// If this function is called with an invalid filter or an issue is |
| 362 | /// encountered adding the rule, an error will be returned. |
| 363 | /// |
| 364 | /// # Examples |
| 365 | /// |
| 366 | /// ``` |
| 367 | /// # use libseccomp::*; |
| 368 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 369 | /// let syscall = ScmpSyscall::from_name("socket")?; |
| 370 | /// ctx.add_rule_conditional_exact( |
| 371 | /// ScmpAction::Errno(libc::EPERM), |
| 372 | /// syscall, |
| 373 | /// &[scmp_cmp!($arg0 != libc::AF_UNIX as u64)], |
| 374 | /// )?; |
| 375 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 376 | /// ``` |
| 377 | pub fn add_rule_conditional_exact<S: Into<ScmpSyscall>>( |
| 378 | &mut self, |
| 379 | action: ScmpAction, |
| 380 | syscall: S, |
| 381 | comparators: &[ScmpArgCompare], |
| 382 | ) -> Result<&mut Self> { |
| 383 | cvt(unsafe { |
| 384 | seccomp_rule_add_exact_array( |
| 385 | self.ctx.as_ptr(), |
| 386 | action.to_sys(), |
| 387 | syscall.into().to_sys(), |
| 388 | comparators.len() as u32, |
| 389 | comparators.as_ptr().cast::<scmp_arg_cmp>(), |
| 390 | ) |
| 391 | })?; |
| 392 | Ok(self) |
| 393 | } |
| 394 | |
| 395 | /// Loads a filter context into the kernel. |
| 396 | /// |
| 397 | /// If the function succeeds, the new filter will be active when the function returns. |
| 398 | /// |
| 399 | /// This function corresponds to |
| 400 | /// [`seccomp_load`](https://man7.org/linux/man-pages/man3/seccomp_load.3.html). |
| 401 | /// |
| 402 | /// # Errors |
| 403 | /// |
| 404 | /// If this function is called with an invalid filter or an issue is |
| 405 | /// encountered loading the rule, an error will be returned. |
| 406 | /// |
| 407 | /// # Examples |
| 408 | /// |
| 409 | /// ``` |
| 410 | /// # use libseccomp::*; |
| 411 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 412 | /// let syscall = ScmpSyscall::from_name("dup3")?; |
| 413 | /// ctx.add_rule(ScmpAction::KillThread, syscall)?; |
| 414 | /// ctx.load()?; |
| 415 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 416 | /// ``` |
| 417 | pub fn load(&self) -> Result<()> { |
| 418 | cvt(unsafe { seccomp_load(self.ctx.as_ptr()) }) |
| 419 | } |
| 420 | |
| 421 | /// Sets a syscall's priority. |
| 422 | /// |
| 423 | /// This provides a priority hint to the seccomp filter generator in the libseccomp |
| 424 | /// such that higher priority syscalls are placed earlier in the seccomp filter code |
| 425 | /// so that they incur less overhead at the expense of lower priority syscalls. |
| 426 | /// |
| 427 | /// This function corresponds to |
| 428 | /// [`seccomp_syscall_priority`](https://man7.org/linux/man-pages/man3/seccomp_syscall_priority.3.html). |
| 429 | /// |
| 430 | /// # Arguments |
| 431 | /// |
| 432 | /// * `syscall` - The number of syscall |
| 433 | /// * `priority` - The priority parameter that is an 8-bit value ranging from 0 to 255; |
| 434 | /// a higher value represents a higher priority. |
| 435 | /// |
| 436 | /// # Errors |
| 437 | /// |
| 438 | /// If this function is called with an invalid filter or the number of syscall |
| 439 | /// is invalid, an error will be returned. |
| 440 | /// |
| 441 | /// # Examples |
| 442 | /// |
| 443 | /// ``` |
| 444 | /// # use libseccomp::*; |
| 445 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 446 | /// let syscall = ScmpSyscall::from_name("open")?; |
| 447 | /// ctx.set_syscall_priority(syscall, 100)?; |
| 448 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 449 | /// ``` |
| 450 | pub fn set_syscall_priority<S: Into<ScmpSyscall>>( |
| 451 | &mut self, |
| 452 | syscall: S, |
| 453 | priority: u8, |
| 454 | ) -> Result<&mut Self> { |
| 455 | cvt(unsafe { |
| 456 | seccomp_syscall_priority(self.ctx.as_ptr(), syscall.into().to_sys(), priority) |
| 457 | })?; |
| 458 | Ok(self) |
| 459 | } |
| 460 | |
| 461 | /// Gets a raw filter attribute value. |
| 462 | /// |
| 463 | /// The seccomp filter attributes are tunable values that affect how the library behaves |
| 464 | /// when generating and loading the seccomp filter into the kernel. |
| 465 | /// |
| 466 | /// > **NOTE**: Usage of this function is discouraged. |
| 467 | /// > Use type safe `get_*` functions instead. |
| 468 | /// |
| 469 | /// This function corresponds to |
| 470 | /// [`seccomp_attr_get`](https://man7.org/linux/man-pages/man3/seccomp_attr_get.3.html). |
| 471 | /// |
| 472 | /// # Arguments |
| 473 | /// |
| 474 | /// * `attr` - A seccomp filter attribute |
| 475 | /// |
| 476 | /// # Errors |
| 477 | /// |
| 478 | /// If this function is called with an invalid filter or an issue is |
| 479 | /// encountered retrieving the attribute, an error will be returned. |
| 480 | /// |
| 481 | /// # Examples |
| 482 | /// |
| 483 | /// ``` |
| 484 | /// # use libseccomp::*; |
| 485 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 486 | /// assert_ne!(ctx.get_filter_attr(ScmpFilterAttr::CtlNnp)?, 0); |
| 487 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 488 | /// ``` |
| 489 | pub fn get_filter_attr(&self, attr: ScmpFilterAttr) -> Result<u32> { |
| 490 | let mut attribute: u32 = 0; |
| 491 | |
| 492 | cvt(unsafe { seccomp_attr_get(self.ctx.as_ptr(), attr.to_sys(), &mut attribute) })?; |
| 493 | |
| 494 | Ok(attribute) |
| 495 | } |
| 496 | |
| 497 | /// Gets the default action as specified in the call to |
| 498 | /// [`new()`](ScmpFilterContext::new) or [`reset()`](ScmpFilterContext::reset). |
| 499 | /// |
| 500 | /// This function corresponds to |
| 501 | /// [`seccomp_attr_get`](https://man7.org/linux/man-pages/man3/seccomp_attr_get.3.html). |
| 502 | /// |
| 503 | /// # Errors |
| 504 | /// |
| 505 | /// If this function is called with an invalid filter or an issue is |
| 506 | /// encountered getting the action, an error will be returned. |
| 507 | /// |
| 508 | /// # Examples |
| 509 | /// |
| 510 | /// ``` |
| 511 | /// # use libseccomp::*; |
| 512 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 513 | /// let action = ctx.get_act_default()?; |
| 514 | /// assert_eq!(action, ScmpAction::Allow); |
| 515 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 516 | /// ``` |
| 517 | pub fn get_act_default(&self) -> Result<ScmpAction> { |
| 518 | let ret = self.get_filter_attr(ScmpFilterAttr::ActDefault)?; |
| 519 | |
| 520 | ScmpAction::from_sys(ret) |
| 521 | } |
| 522 | |
| 523 | /// Gets the default action taken when the loaded filter does not match the architecture |
| 524 | /// of the executing application. |
| 525 | /// |
| 526 | /// This function corresponds to |
| 527 | /// [`seccomp_attr_get`](https://man7.org/linux/man-pages/man3/seccomp_attr_get.3.html). |
| 528 | /// |
| 529 | /// # Errors |
| 530 | /// |
| 531 | /// If this function is called with an invalid filter or an issue is |
| 532 | /// encountered getting the action, an error will be returned. |
| 533 | /// |
| 534 | /// # Examples |
| 535 | /// |
| 536 | /// ``` |
| 537 | /// # use libseccomp::*; |
| 538 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 539 | /// let action = ctx.get_act_badarch()?; |
| 540 | /// assert_eq!(action, ScmpAction::KillThread); |
| 541 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 542 | /// ``` |
| 543 | pub fn get_act_badarch(&self) -> Result<ScmpAction> { |
| 544 | let ret = self.get_filter_attr(ScmpFilterAttr::ActBadArch)?; |
| 545 | |
| 546 | ScmpAction::from_sys(ret) |
| 547 | } |
| 548 | |
| 549 | /// Gets the current state of the [`ScmpFilterAttr::CtlNnp`] attribute. |
| 550 | /// |
| 551 | /// This function returns `Ok(true)` if the [`ScmpFilterAttr::CtlNnp`] attribute is set to on the filter being |
| 552 | /// loaded, `Ok(false)` otherwise. |
| 553 | /// |
| 554 | /// This function corresponds to |
| 555 | /// [`seccomp_attr_get`](https://man7.org/linux/man-pages/man3/seccomp_attr_get.3.html). |
| 556 | /// |
| 557 | /// # Errors |
| 558 | /// |
| 559 | /// If this function is called with an invalid filter or an issue is |
| 560 | /// encountered getting the current state, an error will be returned. |
| 561 | /// |
| 562 | /// # Examples |
| 563 | /// |
| 564 | /// ``` |
| 565 | /// # use libseccomp::*; |
| 566 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 567 | /// ctx.set_ctl_nnp(false)?; |
| 568 | /// assert!(!ctx.get_ctl_nnp()?); |
| 569 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 570 | /// ``` |
| 571 | pub fn get_ctl_nnp(&self) -> Result<bool> { |
| 572 | let ret = self.get_filter_attr(ScmpFilterAttr::CtlNnp)?; |
| 573 | |
| 574 | Ok(ret != 0) |
| 575 | } |
| 576 | |
| 577 | /// Deprecated alias for [`ScmpFilterContext::get_ctl_nnp()`]. |
| 578 | #[deprecated(since = "0.2.3", note = "Use ScmpFilterContext::get_ctl_nnp().")] |
| 579 | pub fn get_no_new_privs_bit(&self) -> Result<bool> { |
| 580 | self.get_ctl_nnp() |
| 581 | } |
| 582 | |
| 583 | /// Gets the current state of the [`ScmpFilterAttr::CtlTsync`] attribute. |
| 584 | /// |
| 585 | /// This function returns `Ok(true)` if the [`ScmpFilterAttr::CtlTsync`] attribute set to on the filter being |
| 586 | /// loaded, `Ok(false)` otherwise. |
| 587 | /// |
| 588 | /// This function corresponds to |
| 589 | /// [`seccomp_attr_get`](https://man7.org/linux/man-pages/man3/seccomp_attr_get.3.html). |
| 590 | /// |
| 591 | /// # Errors |
| 592 | /// |
| 593 | /// If this function is called with an invalid filter, an issue is encountered |
| 594 | /// getting the current state, or the libseccomp API level is less than 2, an error will be returned. |
| 595 | /// |
| 596 | /// # Examples |
| 597 | /// |
| 598 | /// ``` |
| 599 | /// # use libseccomp::*; |
| 600 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 601 | /// ctx.set_ctl_tsync(true)?; |
| 602 | /// assert!(ctx.get_ctl_tsync()?); |
| 603 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 604 | /// ``` |
| 605 | pub fn get_ctl_tsync(&self) -> Result<bool> { |
| 606 | ensure_supported_api("get_ctl_tsync", 2, ScmpVersion::from((2, 2, 0)))?; |
| 607 | let ret = self.get_filter_attr(ScmpFilterAttr::CtlTsync)?; |
| 608 | |
| 609 | Ok(ret != 0) |
| 610 | } |
| 611 | |
| 612 | /// Gets the current state of the [`ScmpFilterAttr::CtlLog`] attribute. |
| 613 | /// |
| 614 | /// This function returns `Ok(true)` if the [`ScmpFilterAttr::CtlLog`] attribute set to on the filter being |
| 615 | /// loaded, `Ok(false)` otherwise. |
| 616 | /// |
| 617 | /// This function corresponds to |
| 618 | /// [`seccomp_attr_get`](https://man7.org/linux/man-pages/man3/seccomp_attr_get.3.html). |
| 619 | /// |
| 620 | /// # Errors |
| 621 | /// |
| 622 | /// If this function is called with an invalid filter, an issue is encountered |
| 623 | /// getting the current state, or the libseccomp API level is less than 3, an error will be returned. |
| 624 | /// |
| 625 | /// # Examples |
| 626 | /// |
| 627 | /// ``` |
| 628 | /// # use libseccomp::*; |
| 629 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 630 | /// ctx.set_ctl_log(true)?; |
| 631 | /// assert!(ctx.get_ctl_log()?); |
| 632 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 633 | /// ``` |
| 634 | pub fn get_ctl_log(&self) -> Result<bool> { |
| 635 | ensure_supported_api("get_ctl_log", 3, ScmpVersion::from((2, 4, 0)))?; |
| 636 | let ret = self.get_filter_attr(ScmpFilterAttr::CtlLog)?; |
| 637 | |
| 638 | Ok(ret != 0) |
| 639 | } |
| 640 | |
| 641 | /// Gets the current state of the [`ScmpFilterAttr::CtlSsb`] attribute. |
| 642 | /// |
| 643 | /// This function returns `Ok(true)` if the [`ScmpFilterAttr::CtlSsb`] attribute set to on the filter being |
| 644 | /// loaded, `Ok(false)` otherwise. |
| 645 | /// The [`ScmpFilterAttr::CtlSsb`] attribute is only usable when the libseccomp API level 4 or higher |
| 646 | /// is supported. |
| 647 | /// |
| 648 | /// This function corresponds to |
| 649 | /// [`seccomp_attr_get`](https://man7.org/linux/man-pages/man3/seccomp_attr_get.3.html). |
| 650 | /// |
| 651 | /// # Errors |
| 652 | /// |
| 653 | /// If this function is called with an invalid filter, an issue is encountered |
| 654 | /// getting the current state, or the libseccomp API level is less than 4, an error will be returned. |
| 655 | /// |
| 656 | /// # Examples |
| 657 | /// |
| 658 | /// ``` |
| 659 | /// # use libseccomp::*; |
| 660 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 661 | /// # if check_api(4, ScmpVersion::from((2, 5, 0))).unwrap() { |
| 662 | /// ctx.set_ctl_ssb(false)?; |
| 663 | /// assert!(!ctx.get_ctl_ssb()?); |
| 664 | /// # } |
| 665 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 666 | /// ``` |
| 667 | pub fn get_ctl_ssb(&self) -> Result<bool> { |
| 668 | ensure_supported_api("get_ctl_ssb", 4, ScmpVersion::from((2, 5, 0)))?; |
| 669 | let ret = self.get_filter_attr(ScmpFilterAttr::CtlSsb)?; |
| 670 | |
| 671 | Ok(ret != 0) |
| 672 | } |
| 673 | |
| 674 | /// Gets the current optimization level of the [`ScmpFilterAttr::CtlOptimize`] attribute. |
| 675 | /// |
| 676 | /// See [`set_ctl_optimize()`](ScmpFilterContext::set_ctl_optimize) for more details about |
| 677 | /// the optimization level. |
| 678 | /// The [`ScmpFilterAttr::CtlOptimize`] attribute is only usable when the libseccomp version 2.5.0 or higher |
| 679 | /// is supported. |
| 680 | /// |
| 681 | /// This function corresponds to |
| 682 | /// [`seccomp_attr_get`](https://man7.org/linux/man-pages/man3/seccomp_attr_get.3.html). |
| 683 | /// |
| 684 | /// # Errors |
| 685 | /// |
| 686 | /// If this function is called with an invalid filter, an issue is encountered |
| 687 | /// getting the current state, or the libseccomp version is less than 2.5.0, an error will be returned. |
| 688 | /// |
| 689 | /// # Examples |
| 690 | /// |
| 691 | /// ``` |
| 692 | /// # use libseccomp::*; |
| 693 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 694 | /// ctx.set_ctl_optimize(2)?; |
| 695 | /// assert_eq!(ctx.get_ctl_optimize()?, 2); |
| 696 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 697 | /// ``` |
| 698 | pub fn get_ctl_optimize(&self) -> Result<u32> { |
| 699 | let ret = self.get_filter_attr(ScmpFilterAttr::CtlOptimize)?; |
| 700 | |
| 701 | Ok(ret) |
| 702 | } |
| 703 | |
| 704 | /// Gets the current state of the [`ScmpFilterAttr::ApiSysRawRc`] attribute. |
| 705 | /// |
| 706 | /// This function returns `Ok(true)` if the [`ScmpFilterAttr::ApiSysRawRc`] attribute set to on the filter |
| 707 | /// being loaded, `Ok(false)` otherwise. |
| 708 | /// The [`ScmpFilterAttr::ApiSysRawRc`] attribute is only usable when the libseccomp version 2.5.0 or higher |
| 709 | /// is supported. |
| 710 | /// |
| 711 | /// This function corresponds to |
| 712 | /// [`seccomp_attr_get`](https://man7.org/linux/man-pages/man3/seccomp_attr_get.3.html). |
| 713 | /// |
| 714 | /// # Errors |
| 715 | /// |
| 716 | /// If this function is called with an invalid filter, an issue is encountered |
| 717 | /// getting the current state, or the libseccomp version is less than 2.5.0, an error will be returned. |
| 718 | /// |
| 719 | /// # Examples |
| 720 | /// |
| 721 | /// ``` |
| 722 | /// # use libseccomp::*; |
| 723 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 724 | /// ctx.set_api_sysrawrc(true)?; |
| 725 | /// assert!(ctx.get_api_sysrawrc()?); |
| 726 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 727 | /// ``` |
| 728 | pub fn get_api_sysrawrc(&self) -> Result<bool> { |
| 729 | let ret = self.get_filter_attr(ScmpFilterAttr::ApiSysRawRc)?; |
| 730 | |
| 731 | Ok(ret != 0) |
| 732 | } |
| 733 | |
| 734 | /// Gets the current state of the [`ScmpFilterAttr::CtlWaitkill`] attribute. |
| 735 | /// |
| 736 | /// This function returns `Ok(true)` if the [`ScmpFilterAttr::CtlWaitkill`] attribute set to on the filter being |
| 737 | /// loaded, `Ok(false)` otherwise. |
| 738 | /// The [`ScmpFilterAttr::CtlWaitkill`] attribute is only usable when the libseccomp API level 7 or higher |
| 739 | /// is supported. |
| 740 | /// |
| 741 | /// This function corresponds to |
| 742 | /// [`seccomp_attr_get`](https://man7.org/linux/man-pages/man3/seccomp_attr_get.3.html). |
| 743 | /// |
| 744 | /// # Errors |
| 745 | /// |
| 746 | /// If this function is called with an invalid filter, an issue is encountered |
| 747 | /// getting the current state, or the libseccomp API level is less than 7, an error will be returned. |
| 748 | /// |
| 749 | /// # Examples |
| 750 | /// |
| 751 | /// ``` |
| 752 | /// # use libseccomp::*; |
| 753 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 754 | /// # if check_api(7, ScmpVersion::from((2, 6, 0))).unwrap() { |
| 755 | /// ctx.set_ctl_waitkill(true)?; |
| 756 | /// assert!(ctx.get_ctl_waitkill()?); |
| 757 | /// # } |
| 758 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 759 | /// ``` |
| 760 | pub fn get_ctl_waitkill(&self) -> Result<bool> { |
| 761 | ensure_supported_api("get_ctl_waitkill", 7, ScmpVersion::from((2, 6, 0)))?; |
| 762 | let ret = self.get_filter_attr(ScmpFilterAttr::CtlWaitkill)?; |
| 763 | |
| 764 | Ok(ret != 0) |
| 765 | } |
| 766 | |
| 767 | /// Gets the current state of the [`ScmpFilterAttr::ApiTskip`] attribute. |
| 768 | /// |
| 769 | /// This function returns `Ok(true)` if the [`ScmpFilterAttr::ApiTskip`] attribute set to on the filter being |
| 770 | /// loaded, `Ok(false)` otherwise. |
| 771 | /// |
| 772 | /// This function corresponds to |
| 773 | /// [`seccomp_attr_get`](https://man7.org/linux/man-pages/man3/seccomp_attr_get.3.html). |
| 774 | /// |
| 775 | /// # Errors |
| 776 | /// |
| 777 | /// If this function is called with an invalid filter or an issue is encountered |
| 778 | /// getting the current state, an error will be returned. |
| 779 | /// |
| 780 | /// # Examples |
| 781 | /// |
| 782 | /// ``` |
| 783 | /// # use libseccomp::*; |
| 784 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 785 | /// ctx.set_api_tskip(true)?; |
| 786 | /// assert!(ctx.get_api_tskip()?); |
| 787 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 788 | /// ``` |
| 789 | pub fn get_api_tskip(&self) -> Result<bool> { |
| 790 | let ret = self.get_filter_attr(ScmpFilterAttr::ApiTskip)?; |
| 791 | |
| 792 | Ok(ret != 0) |
| 793 | } |
| 794 | |
| 795 | /// Sets a raw filter attribute value. |
| 796 | /// |
| 797 | /// The seccomp filter attributes are tunable values that affect how the library behaves |
| 798 | /// when generating and loading the seccomp filter into the kernel. |
| 799 | /// |
| 800 | /// > **NOTE**: Usage of this function is discouraged. |
| 801 | /// > Use type safe `set_*` functions instead. |
| 802 | /// |
| 803 | /// This function corresponds to |
| 804 | /// [`seccomp_attr_set`](https://man7.org/linux/man-pages/man3/seccomp_attr_set.3.html). |
| 805 | /// |
| 806 | /// # Arguments |
| 807 | /// |
| 808 | /// * `attr` - A seccomp filter attribute |
| 809 | /// * `value` - A value of or the parameter of the attribute |
| 810 | /// |
| 811 | /// See the [`seccomp_attr_set(3)`] man page for details on available attribute values. |
| 812 | /// |
| 813 | /// [`seccomp_attr_set(3)`]: https://www.man7.org/linux/man-pages/man3/seccomp_attr_set.3.html |
| 814 | /// |
| 815 | /// # Errors |
| 816 | /// |
| 817 | /// If this function is called with an invalid filter or an issue is |
| 818 | /// encountered setting the attribute, an error will be returned. |
| 819 | pub fn set_filter_attr(&mut self, attr: ScmpFilterAttr, value: u32) -> Result<&mut Self> { |
| 820 | cvt(unsafe { seccomp_attr_set(self.ctx.as_ptr(), attr.to_sys(), value) })?; |
| 821 | Ok(self) |
| 822 | } |
| 823 | |
| 824 | /// Sets the default action taken when the loaded filter does not match the architecture |
| 825 | /// of the executing application. |
| 826 | /// |
| 827 | /// Defaults to on (`action` == [`ScmpAction::KillThread`]). |
| 828 | /// |
| 829 | /// This function corresponds to |
| 830 | /// [`seccomp_attr_set`](https://man7.org/linux/man-pages/man3/seccomp_attr_set.3.html). |
| 831 | /// |
| 832 | /// # Arguments |
| 833 | /// |
| 834 | /// * `action` - An action to be taken on a syscall for an architecture not in the filter. |
| 835 | /// |
| 836 | /// # Errors |
| 837 | /// |
| 838 | /// If this function is called with an invalid filter or an issue is |
| 839 | /// encountered setting the attribute, an error will be returned. |
| 840 | /// |
| 841 | /// # Examples |
| 842 | /// |
| 843 | /// ``` |
| 844 | /// # use libseccomp::*; |
| 845 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 846 | /// ctx.set_act_badarch(ScmpAction::KillProcess)?; |
| 847 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 848 | /// ``` |
| 849 | pub fn set_act_badarch(&mut self, action: ScmpAction) -> Result<&mut Self> { |
| 850 | self.set_filter_attr(ScmpFilterAttr::ActBadArch, action.to_sys()) |
| 851 | } |
| 852 | |
| 853 | /// Sets the state of the [`ScmpFilterAttr::CtlNnp`] attribute which will be applied |
| 854 | /// on filter load. |
| 855 | /// |
| 856 | /// Settings this to off (`state` == `false`) means that loading the seccomp filter |
| 857 | /// into the kernel fill fail if the `CAP_SYS_ADMIN` is missing. |
| 858 | /// |
| 859 | /// Defaults to on (`state` == `true`). |
| 860 | /// |
| 861 | /// This function corresponds to |
| 862 | /// [`seccomp_attr_set`](https://man7.org/linux/man-pages/man3/seccomp_attr_set.3.html). |
| 863 | /// |
| 864 | /// # Arguments |
| 865 | /// |
| 866 | /// * `state` - A state flag to specify whether the [`ScmpFilterAttr::CtlNnp`] attribute should be enabled |
| 867 | /// |
| 868 | /// # Errors |
| 869 | /// |
| 870 | /// If this function is called with an invalid filter or an issue is |
| 871 | /// encountered setting the attribute, an error will be returned. |
| 872 | /// |
| 873 | /// # Examples |
| 874 | /// |
| 875 | /// ``` |
| 876 | /// # use libseccomp::*; |
| 877 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 878 | /// ctx.set_ctl_nnp(false)?; |
| 879 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 880 | /// ``` |
| 881 | pub fn set_ctl_nnp(&mut self, state: bool) -> Result<&mut Self> { |
| 882 | self.set_filter_attr(ScmpFilterAttr::CtlNnp, state.into()) |
| 883 | } |
| 884 | |
| 885 | /// Deprecated alias for [`ScmpFilterContext::set_ctl_nnp()`]. |
| 886 | #[deprecated(since = "0.2.3", note = "Use ScmpFilterContext::set_ctl_nnp().")] |
| 887 | pub fn set_no_new_privs_bit(&mut self, state: bool) -> Result<&mut Self> { |
| 888 | self.set_ctl_nnp(state) |
| 889 | } |
| 890 | |
| 891 | /// Sets the state of the [`ScmpFilterAttr::CtlTsync`] attribute which will be applied |
| 892 | /// on filter load. |
| 893 | /// |
| 894 | /// Settings this to on (`state` == `true`) means that the kernel should attempt to synchronize the filters |
| 895 | /// across all threads on [`ScmpFilterContext::load()`]. |
| 896 | /// If the kernel is unable to synchronize all of the thread then the load operation will fail. |
| 897 | /// The [`ScmpFilterAttr::CtlTsync`] attribute is only usable when the libseccomp API level 2 or higher |
| 898 | /// is supported. |
| 899 | /// If the libseccomp API level is less than 6, the [`ScmpFilterAttr::CtlTsync`] attribute is unusable |
| 900 | /// with the userspace notification API simultaneously. |
| 901 | /// |
| 902 | /// Defaults to off (`state` == `false`). |
| 903 | /// |
| 904 | /// This function corresponds to |
| 905 | /// [`seccomp_attr_set`](https://man7.org/linux/man-pages/man3/seccomp_attr_set.3.html). |
| 906 | /// |
| 907 | /// # Arguments |
| 908 | /// |
| 909 | /// * `state` - A state flag to specify whether the [`ScmpFilterAttr::CtlTsync`] attribute should be enabled |
| 910 | /// |
| 911 | /// # Errors |
| 912 | /// |
| 913 | /// If this function is called with an invalid filter, an issue is encountered |
| 914 | /// setting the attribute, or the libseccomp API level is less than 2, an error will be returned. |
| 915 | /// |
| 916 | /// # Examples |
| 917 | /// |
| 918 | /// ``` |
| 919 | /// # use libseccomp::*; |
| 920 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 921 | /// ctx.set_ctl_tsync(true)?; |
| 922 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 923 | /// ``` |
| 924 | pub fn set_ctl_tsync(&mut self, state: bool) -> Result<&mut Self> { |
| 925 | ensure_supported_api("set_ctl_tsync", 2, ScmpVersion::from((2, 2, 0)))?; |
| 926 | self.set_filter_attr(ScmpFilterAttr::CtlTsync, state.into()) |
| 927 | } |
| 928 | |
| 929 | /// Sets the state of the [`ScmpFilterAttr::CtlLog`] attribute which will be applied on filter load. |
| 930 | /// |
| 931 | /// Settings this to on (`state` == `true`) means that the kernel should log all filter |
| 932 | /// actions taken except for the [`ScmpAction::Allow`] action. |
| 933 | /// The [`ScmpFilterAttr::CtlLog`] attribute is only usable when the libseccomp API level 3 or higher |
| 934 | /// is supported. |
| 935 | /// |
| 936 | /// Defaults to off (`state` == `false`). |
| 937 | /// |
| 938 | /// This function corresponds to |
| 939 | /// [`seccomp_attr_set`](https://man7.org/linux/man-pages/man3/seccomp_attr_set.3.html). |
| 940 | /// |
| 941 | /// # Arguments |
| 942 | /// |
| 943 | /// * `state` - A state flag to specify whether the [`ScmpFilterAttr::CtlLog`] attribute should |
| 944 | /// be enabled |
| 945 | /// |
| 946 | /// # Errors |
| 947 | /// |
| 948 | /// If this function is called with an invalid filter, an issue is encountered |
| 949 | /// setting the attribute, or the libseccomp API level is less than 3, an error will be returned. |
| 950 | /// |
| 951 | /// # Examples |
| 952 | /// |
| 953 | /// ``` |
| 954 | /// # use libseccomp::*; |
| 955 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 956 | /// ctx.set_ctl_log(true)?; |
| 957 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 958 | /// ``` |
| 959 | pub fn set_ctl_log(&mut self, state: bool) -> Result<&mut Self> { |
| 960 | ensure_supported_api("set_ctl_log", 3, ScmpVersion::from((2, 4, 0)))?; |
| 961 | self.set_filter_attr(ScmpFilterAttr::CtlLog, state.into()) |
| 962 | } |
| 963 | |
| 964 | /// Sets the state of the [`ScmpFilterAttr::CtlSsb`] attribute which will be applied on filter load. |
| 965 | /// |
| 966 | /// Settings this to on (`state` == `true`) disables Speculative Store Bypass mitigations for the filter. |
| 967 | /// The [`ScmpFilterAttr::CtlSsb`] attribute is only usable when the libseccomp API level 4 or higher |
| 968 | /// is supported. |
| 969 | /// |
| 970 | /// Defaults to off (`state` == `false`). |
| 971 | /// |
| 972 | /// This function corresponds to |
| 973 | /// [`seccomp_attr_set`](https://man7.org/linux/man-pages/man3/seccomp_attr_set.3.html). |
| 974 | /// |
| 975 | /// # Arguments |
| 976 | /// |
| 977 | /// * `state` - A state flag to specify whether the [`ScmpFilterAttr::CtlSsb`] attribute should |
| 978 | /// be enabled |
| 979 | /// |
| 980 | /// # Errors |
| 981 | /// |
| 982 | /// If this function is called with an invalid filter, an issue is encountered |
| 983 | /// setting the attribute, or the libseccomp API level is less than 4, an error will be returned. |
| 984 | /// |
| 985 | /// # Examples |
| 986 | /// |
| 987 | /// ``` |
| 988 | /// # use libseccomp::*; |
| 989 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 990 | /// # if check_api(4, ScmpVersion::from((2, 5, 0))).unwrap() { |
| 991 | /// ctx.set_ctl_ssb(false)?; |
| 992 | /// # } |
| 993 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 994 | /// ``` |
| 995 | pub fn set_ctl_ssb(&mut self, state: bool) -> Result<&mut Self> { |
| 996 | ensure_supported_api("set_ctl_ssb", 4, ScmpVersion::from((2, 5, 0)))?; |
| 997 | self.set_filter_attr(ScmpFilterAttr::CtlSsb, state.into()) |
| 998 | } |
| 999 | |
| 1000 | /// Sets the [`ScmpFilterAttr::CtlOptimize`] level which will be applied on filter load. |
| 1001 | /// |
| 1002 | /// By default the libseccomp generates a set of sequential "if" statements for each rule in the filter. |
| 1003 | /// [`set_syscall_priority()`](ScmpFilterContext::set_syscall_priority) can be used to prioritize the |
| 1004 | /// order for the default cause. The binary tree optimization sorts by syscall numbers and generates |
| 1005 | /// consistent O(log n) filter traversal for every rule in the filter. The binary tree may be advantageous |
| 1006 | /// for large filters. Note that [`set_syscall_priority()`](ScmpFilterContext::set_syscall_priority) is |
| 1007 | /// ignored when `level` == `2`. |
| 1008 | /// The [`ScmpFilterAttr::CtlOptimize`] attribute is only usable when the libseccomp version 2.5.0 or higher |
| 1009 | /// is supported. |
| 1010 | /// |
| 1011 | /// The different optimization levels are described below: |
| 1012 | /// * `0` - Reserved value, not currently used. |
| 1013 | /// * `1` - Rules sorted by priority and complexity (DEFAULT). |
| 1014 | /// * `2` - Binary tree sorted by syscall number. |
| 1015 | /// |
| 1016 | /// This function corresponds to |
| 1017 | /// [`seccomp_attr_set`](https://man7.org/linux/man-pages/man3/seccomp_attr_set.3.html). |
| 1018 | /// |
| 1019 | /// # Arguments |
| 1020 | /// |
| 1021 | /// * `level` - The optimization level of the filter |
| 1022 | /// |
| 1023 | /// # Errors |
| 1024 | /// |
| 1025 | /// If this function is called with an invalid filter, an issue is encountered |
| 1026 | /// setting the attribute, or the libseccomp version is less than 2.5.0, an error will be returned. |
| 1027 | /// |
| 1028 | /// # Examples |
| 1029 | /// |
| 1030 | /// ``` |
| 1031 | /// # use libseccomp::*; |
| 1032 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 1033 | /// ctx.set_ctl_optimize(2)?; |
| 1034 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 1035 | /// ``` |
| 1036 | pub fn set_ctl_optimize(&mut self, level: u32) -> Result<&mut Self> { |
| 1037 | self.set_filter_attr(ScmpFilterAttr::CtlOptimize, level) |
| 1038 | } |
| 1039 | |
| 1040 | /// Sets the state of the [`ScmpFilterAttr::ApiSysRawRc`] attribute which will be applied on filter load. |
| 1041 | /// |
| 1042 | /// Settings this to on (`state` == `true`) means that the libseccomp should pass system error codes |
| 1043 | /// back to the caller instead of the default -ECANCELED. |
| 1044 | /// The [`ScmpFilterAttr::ApiSysRawRc`] attribute is only usable when the libseccomp version 2.5.0 or higher |
| 1045 | /// is supported. |
| 1046 | /// |
| 1047 | /// Defaults to off (`state` == `false`). |
| 1048 | /// |
| 1049 | /// This function corresponds to |
| 1050 | /// [`seccomp_attr_set`](https://man7.org/linux/man-pages/man3/seccomp_attr_set.3.html). |
| 1051 | /// |
| 1052 | /// # Arguments |
| 1053 | /// |
| 1054 | /// * `state` - A state flag to specify whether the [`ScmpFilterAttr::ApiSysRawRc`] attribute should |
| 1055 | /// be enabled |
| 1056 | /// |
| 1057 | /// # Errors |
| 1058 | /// |
| 1059 | /// If this function is called with an invalid filter, an issue is encountered |
| 1060 | /// setting the attribute, or the libseccomp version is less than 2.5.0, an error will be returned. |
| 1061 | /// |
| 1062 | /// # Examples |
| 1063 | /// |
| 1064 | /// ``` |
| 1065 | /// # use libseccomp::*; |
| 1066 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 1067 | /// ctx.set_api_sysrawrc(true)?; |
| 1068 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 1069 | /// ``` |
| 1070 | pub fn set_api_sysrawrc(&mut self, state: bool) -> Result<&mut Self> { |
| 1071 | self.set_filter_attr(ScmpFilterAttr::ApiSysRawRc, state.into()) |
| 1072 | } |
| 1073 | |
| 1074 | /// Sets the state of the [`ScmpFilterAttr::CtlWaitkill`] attribute which will be applied on filter load. |
| 1075 | /// |
| 1076 | /// Settings this to on (`state` == `true`) specify libseccomp should request wait killable semantics when possible. |
| 1077 | /// The [`ScmpFilterAttr::CtlWaitkill`] attribute is only usable when the libseccomp API level 7 or higher |
| 1078 | /// is supported. |
| 1079 | /// |
| 1080 | /// Defaults to off (`state` == `false`). |
| 1081 | /// |
| 1082 | /// This function corresponds to |
| 1083 | /// [`seccomp_attr_set`](https://man7.org/linux/man-pages/man3/seccomp_attr_set.3.html). |
| 1084 | /// |
| 1085 | /// # Arguments |
| 1086 | /// |
| 1087 | /// * `state` - A state flag to specify whether the [`ScmpFilterAttr::CtlWaitkill`] attribute should |
| 1088 | /// be enabled |
| 1089 | /// |
| 1090 | /// # Errors |
| 1091 | /// |
| 1092 | /// If this function is called with an invalid filter, an issue is encountered |
| 1093 | /// setting the attribute, or the libseccomp API level is less than 7, an error will be returned. |
| 1094 | /// |
| 1095 | /// # Examples |
| 1096 | /// |
| 1097 | /// ``` |
| 1098 | /// # use libseccomp::*; |
| 1099 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 1100 | /// # if check_api(7, ScmpVersion::from((2, 6, 0))).unwrap() { |
| 1101 | /// ctx.set_ctl_waitkill(true)?; |
| 1102 | /// # } |
| 1103 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 1104 | /// ``` |
| 1105 | pub fn set_ctl_waitkill(&mut self, state: bool) -> Result<&mut Self> { |
| 1106 | ensure_supported_api("set_ctl_waitkill", 7, ScmpVersion::from((2, 6, 0)))?; |
| 1107 | self.set_filter_attr(ScmpFilterAttr::CtlWaitkill, state.into()) |
| 1108 | } |
| 1109 | |
| 1110 | /// Sets the state of the [`ScmpFilterAttr::ApiTskip`] attribute which will be applied |
| 1111 | /// on filter load. |
| 1112 | /// |
| 1113 | /// Settings this to on (`state` == `true`) means that the kernel should log all filter |
| 1114 | /// actions taken except for the [`ScmpAction::Allow`](crate::ScmpAction::Allow) action. |
| 1115 | /// |
| 1116 | /// Defaults to off (`state` == `false`). |
| 1117 | /// |
| 1118 | /// This function corresponds to |
| 1119 | /// [`seccomp_attr_set`](https://man7.org/linux/man-pages/man3/seccomp_attr_set.3.html). |
| 1120 | /// |
| 1121 | /// # Arguments |
| 1122 | /// |
| 1123 | /// * `state` - A state flag to specify whether the [`ScmpFilterAttr::ApiTskip`] attribute should be enabled |
| 1124 | /// |
| 1125 | /// # Errors |
| 1126 | /// |
| 1127 | /// If this function is called with an invalid filter or an issue is encountered |
| 1128 | /// setting the attribute, an error will be returned. |
| 1129 | /// |
| 1130 | /// # Examples |
| 1131 | /// |
| 1132 | /// ``` |
| 1133 | /// # use libseccomp::*; |
| 1134 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 1135 | /// ctx.set_api_tskip(true)?; |
| 1136 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 1137 | /// ``` |
| 1138 | pub fn set_api_tskip(&mut self, state: bool) -> Result<&mut Self> { |
| 1139 | self.set_filter_attr(ScmpFilterAttr::ApiTskip, state.into()) |
| 1140 | } |
| 1141 | |
| 1142 | /// Outputs PFC(Pseudo Filter Code)-formatted, human-readable dump of a filter context's rules to a file. |
| 1143 | /// |
| 1144 | /// This function corresponds to |
| 1145 | /// [`seccomp_export_pfc`](https://man7.org/linux/man-pages/man3/seccomp_export_pfc.3.html). |
| 1146 | /// |
| 1147 | /// # Arguments |
| 1148 | /// |
| 1149 | /// * `fd` - A file descriptor to write to (must be open for writing) |
| 1150 | /// |
| 1151 | /// # Errors |
| 1152 | /// |
| 1153 | /// If this function is called with an invalid filter or writing to the file fails, |
| 1154 | /// an error will be returned. |
| 1155 | /// |
| 1156 | /// # Examples |
| 1157 | /// |
| 1158 | /// ``` |
| 1159 | /// # use libseccomp::*; |
| 1160 | /// # use std::io; |
| 1161 | /// let ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 1162 | /// ctx.export_pfc(io::stdout())?; |
| 1163 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 1164 | /// ``` |
| 1165 | pub fn export_pfc<T: AsFd>(&self, fd: T) -> Result<()> { |
| 1166 | cvt(unsafe { seccomp_export_pfc(self.ctx.as_ptr(), fd.as_fd().as_raw_fd()) }) |
| 1167 | } |
| 1168 | |
| 1169 | /// Outputs BPF(Berkeley Packet Filter)-formatted, kernel-readable dump of a |
| 1170 | /// filter context's rules to a file. |
| 1171 | /// |
| 1172 | /// This function corresponds to |
| 1173 | /// [`seccomp_export_bpf`](https://man7.org/linux/man-pages/man3/seccomp_export_bpf.3.html). |
| 1174 | /// |
| 1175 | /// # Arguments |
| 1176 | /// |
| 1177 | /// * `fd` - A file descriptor to write to (must be open for writing) |
| 1178 | /// |
| 1179 | /// # Errors |
| 1180 | /// |
| 1181 | /// If this function is called with an invalid filter or writing to the file fails, |
| 1182 | /// an error will be returned. |
| 1183 | /// |
| 1184 | /// # Examples |
| 1185 | /// |
| 1186 | /// ``` |
| 1187 | /// # use libseccomp::*; |
| 1188 | /// # use std::io; |
| 1189 | /// let ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 1190 | /// ctx.export_bpf(io::stdout())?; |
| 1191 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 1192 | /// ``` |
| 1193 | pub fn export_bpf<T: AsFd>(&self, fd: T) -> Result<()> { |
| 1194 | cvt(unsafe { seccomp_export_bpf(self.ctx.as_ptr(), fd.as_fd().as_raw_fd()) }) |
| 1195 | } |
| 1196 | |
| 1197 | /// Resets a filter context, removing all its existing state. |
| 1198 | /// |
| 1199 | /// This function corresponds to |
| 1200 | /// [`seccomp_reset`](https://man7.org/linux/man-pages/man3/seccomp_reset.3.html). |
| 1201 | /// |
| 1202 | /// # Arguments |
| 1203 | /// |
| 1204 | /// * `action` - A new default action to be taken for syscalls which do not match |
| 1205 | /// |
| 1206 | /// # Errors |
| 1207 | /// |
| 1208 | /// If this function is called with an invalid filter or an issue is encountered |
| 1209 | /// resetting the filter, an error will be returned. |
| 1210 | /// |
| 1211 | /// # Examples |
| 1212 | /// |
| 1213 | /// ``` |
| 1214 | /// # use libseccomp::*; |
| 1215 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 1216 | /// ctx.reset(ScmpAction::KillThread)?; |
| 1217 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 1218 | /// ``` |
| 1219 | pub fn reset(&mut self, action: ScmpAction) -> Result<()> { |
| 1220 | cvt(unsafe { seccomp_reset(self.ctx.as_ptr(), action.to_sys()) }) |
| 1221 | } |
| 1222 | |
| 1223 | /// Gets a raw pointer of a seccomp filter. |
| 1224 | /// |
| 1225 | /// This function returns a raw pointer to the [`scmp_filter_ctx`]. |
| 1226 | /// The caller must ensure that the filter outlives the pointer this function returns, |
| 1227 | /// or else it will end up pointing to garbage. |
| 1228 | /// You may only modify the filter referenced by the pointer with functions intended |
| 1229 | /// for this (the once provided by [`libseccomp_sys`] crate). |
| 1230 | #[must_use] |
| 1231 | pub fn as_ptr(&self) -> scmp_filter_ctx { |
| 1232 | self.ctx.as_ptr() |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | /// This `impl`-block requires libseccomp 2.6.0 or newer. |
| 1237 | #[cfg(any(libseccomp_v2_6, all(doc, not(doctest))))] |
| 1238 | impl ScmpFilterContext { |
| 1239 | /// Outputs BPF(Berkeley Packet Filter)-formatted, kernel-readable dump of a |
| 1240 | /// filter context's rules to a in-memory buffer. |
| 1241 | /// |
| 1242 | /// This function corresponds to |
| 1243 | /// [`seccomp_export_bpf_mem`](https://man7.org/linux/man-pages/man3/seccomp_export_bpf_mem.3.html). |
| 1244 | /// |
| 1245 | /// # Errors |
| 1246 | /// |
| 1247 | /// If this function encounters an issue while exporting the filter, an error will be returned. |
| 1248 | /// |
| 1249 | /// # Examples |
| 1250 | /// |
| 1251 | /// ``` |
| 1252 | /// # use libseccomp::*; |
| 1253 | /// # use std::io::{stdout}; |
| 1254 | /// let ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 1255 | /// let buf = ctx.export_bpf_mem()?; |
| 1256 | /// println!("{buf:?}"); |
| 1257 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 1258 | /// ``` |
| 1259 | pub fn export_bpf_mem(&self) -> Result<Vec<u8>> { |
| 1260 | // We call seccomp_export_bpf_mem with buf specified as a NULL-ptr first |
| 1261 | // to query the required buffer size. |
| 1262 | let mut len: usize = 0; |
| 1263 | cvt(unsafe { seccomp_export_bpf_mem(self.ctx.as_ptr(), std::ptr::null_mut(), &mut len) })?; |
| 1264 | |
| 1265 | let mut buf: Vec<u8> = vec![0; len]; |
| 1266 | let mut buf_len = buf.len(); |
| 1267 | cvt(unsafe { |
| 1268 | seccomp_export_bpf_mem( |
| 1269 | self.ctx.as_ptr(), |
| 1270 | buf.as_mut_ptr().cast::<libc::c_void>(), |
| 1271 | &mut buf_len, |
| 1272 | ) |
| 1273 | })?; |
| 1274 | |
| 1275 | Ok(buf) |
| 1276 | } |
| 1277 | |
| 1278 | /// Start a new seccomp filter transaction |
| 1279 | /// |
| 1280 | /// This function starts a new seccomp filter transaction that the caller can use |
| 1281 | /// to perform any number of filter modifications which can then be committed |
| 1282 | /// to the filter using [`commit_transaction()`](ScmpFilterContext::commit_transaction) |
| 1283 | /// or rejected using [`reject_transaction()`](ScmpFilterContext::reject_transaction). |
| 1284 | /// It is important to note that transactions only affect the seccomp filter state |
| 1285 | /// while it is being managed by libseccomp; seccomp filters which have been loaded into |
| 1286 | /// the kernel can not be modified, only new seccomp filters can be added on top |
| 1287 | /// of the existing loaded filter stack. |
| 1288 | /// |
| 1289 | /// This function corresponds to |
| 1290 | /// [`seccomp_transaction_start`](https://man7.org/linux/man-pages/man3/seccomp_transaction_start.3.html). |
| 1291 | /// |
| 1292 | /// # Errors |
| 1293 | /// |
| 1294 | /// If starting the transaction fails, an error will be returned. |
| 1295 | /// |
| 1296 | /// # Examples |
| 1297 | /// |
| 1298 | /// ``` |
| 1299 | /// # use libseccomp::*; |
| 1300 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 1301 | /// let syscall = ScmpSyscall::from_name("dup3")?; |
| 1302 | /// ctx.start_transaction()?; |
| 1303 | /// ctx.add_rule(ScmpAction::KillThread, syscall)?; |
| 1304 | /// ctx.commit_transaction()?; |
| 1305 | /// ctx.load()?; |
| 1306 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 1307 | /// ``` |
| 1308 | pub fn start_transaction(&self) -> Result<()> { |
| 1309 | cvt(unsafe { seccomp_transaction_start(self.ctx.as_ptr()) }) |
| 1310 | } |
| 1311 | |
| 1312 | /// Reject a transaction started by [`start_transaction`](ScmpFilterContext::start_transaction) |
| 1313 | /// |
| 1314 | /// This function rejects the current seccomp filter transaction, discarding all |
| 1315 | /// the filter modifications made during the transaction. Once rejected, the filter |
| 1316 | /// context remains unchanged as it was before the transaction started. |
| 1317 | /// |
| 1318 | /// This function corresponds to |
| 1319 | /// [`seccomp_transaction_reject`](https://man7.org/linux/man-pages/man3/seccomp_transaction_reject.3.html). |
| 1320 | /// |
| 1321 | /// # Examples |
| 1322 | /// |
| 1323 | /// ``` |
| 1324 | /// # use libseccomp::*; |
| 1325 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 1326 | /// let syscall = ScmpSyscall::from_name("dup3")?; |
| 1327 | /// ctx.start_transaction()?; |
| 1328 | /// ctx.add_rule(ScmpAction::KillThread, syscall)?; |
| 1329 | /// ctx.reject_transaction(); |
| 1330 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 1331 | /// ``` |
| 1332 | pub fn reject_transaction(&mut self) { |
| 1333 | unsafe { seccomp_transaction_reject(self.ctx.as_ptr()) } |
| 1334 | } |
| 1335 | |
| 1336 | /// Commit a transaction started by [`start_transaction`](ScmpFilterContext::start_transaction) |
| 1337 | /// |
| 1338 | /// This function commits the current seccomp filter transaction, applying all |
| 1339 | /// the filter modifications made during the transaction to the filter context. |
| 1340 | /// Once committed, the changes are finalized and cannot be undone. |
| 1341 | /// |
| 1342 | /// This function corresponds to |
| 1343 | /// [`seccomp_transaction_commit`](https://man7.org/linux/man-pages/man3/seccomp_transaction_commit.3.html). |
| 1344 | /// |
| 1345 | /// # Errors |
| 1346 | /// |
| 1347 | /// If committing the transaction fails, an error will be returned. |
| 1348 | /// |
| 1349 | /// # Examples |
| 1350 | /// |
| 1351 | /// ``` |
| 1352 | /// # use libseccomp::*; |
| 1353 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 1354 | /// let syscall = ScmpSyscall::from_name("dup3")?; |
| 1355 | /// ctx.start_transaction()?; |
| 1356 | /// ctx.add_rule(ScmpAction::KillThread, syscall)?; |
| 1357 | /// ctx.commit_transaction()?; |
| 1358 | /// ctx.load()?; |
| 1359 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 1360 | /// ``` |
| 1361 | pub fn commit_transaction(&mut self) -> Result<()> { |
| 1362 | cvt(unsafe { seccomp_transaction_commit(self.ctx.as_ptr()) }) |
| 1363 | } |
| 1364 | |
| 1365 | /// Precompute the seccomp filter for future use |
| 1366 | /// |
| 1367 | /// This function precomputes the seccomp filter and stores it internally for |
| 1368 | /// future use, speeding up [`ScmpFilterContext::load()`] and other functions which require |
| 1369 | /// the generated filter. |
| 1370 | /// |
| 1371 | /// This function corresponds to |
| 1372 | /// [`seccomp_precompute`](https://man7.org/linux/man-pages/man3/seccomp_precompute.3.html). |
| 1373 | /// |
| 1374 | /// # Errors |
| 1375 | /// |
| 1376 | /// If precomputing the filter fails, an error will be returned. |
| 1377 | /// |
| 1378 | /// # Examples |
| 1379 | /// |
| 1380 | /// ``` |
| 1381 | /// # use libseccomp::*; |
| 1382 | /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 1383 | /// let syscall = ScmpSyscall::from_name("dup3")?; |
| 1384 | /// ctx.add_rule(ScmpAction::KillThread, syscall)?; |
| 1385 | /// ctx.precompute()?; |
| 1386 | /// ctx.load()?; |
| 1387 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 1388 | /// ``` |
| 1389 | pub fn precompute(&self) -> Result<()> { |
| 1390 | cvt(unsafe { seccomp_precompute(self.ctx.as_ptr()) }) |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | impl Drop for ScmpFilterContext { |
| 1395 | /// Releases a filter context, freeing its memory. |
| 1396 | /// |
| 1397 | /// After calling this function, the given filter is no longer valid and cannot be used. |
| 1398 | /// |
| 1399 | /// This function corresponds to |
| 1400 | /// [`seccomp_release`](https://man7.org/linux/man-pages/man3/seccomp_release.3.html). |
| 1401 | fn drop(&mut self) { |
| 1402 | unsafe { seccomp_release(self.ctx.as_ptr()) } |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | #[cfg(test)] |
| 1407 | mod tests { |
| 1408 | use super::*; |
| 1409 | |
| 1410 | #[test] |
| 1411 | fn test_as_ptr() { |
| 1412 | let ctx = ScmpFilterContext::new(ScmpAction::Allow).unwrap(); |
| 1413 | assert_eq!(ctx.as_ptr(), ctx.ctx.as_ptr()); |
| 1414 | } |
| 1415 | } |