Struct ProgramBuffer

Source
pub struct ProgramBuffer { /* private fields */ }
Expand description

Fixed size buffer for building seccomp BPF programs

Conceptually this is like a Vec, but to keep compatibility with no_std and take advantage of the small maximum length of a BPF program, this type features a fixed size array that can hold the maximum (4096) instructions.

use sc::nr;
use seccomp_tiny::{ProgramBuffer, abi, bpf::ret};

let mut p = ProgramBuffer::new();

p.if_any_eq(&[
    nr::ARCH_PRCTL,
    nr::PRCTL,
    nr::WAITID,
    nr::PTRACE,
    nr::KILL,
], &[
    ret(abi::SECCOMP_RET_ALLOW)
]);

p.inst(ret(abi::SECCOMP_RET_TRACE));

println!("{:?}", p);

Implementations§

Source§

impl ProgramBuffer

Source

pub fn new() -> Self

Construct a new empty ProgramBuffer

Source

pub fn instructions(&self) -> &[SockFilter]

Returns a slice referring to all SockFilter instructions added to the buffer

Source

pub fn activate(&self)

Activate the seccomp program, panic on error.

This is equivalent to:


let prog = abi::SockFilterProg::new(buffer.instructions());
let result = seccomp_tiny::activate(&prog);
if let Err(code) = result {
    panic!("... {}", code);
}
Source

pub fn block(&mut self, block: &[SockFilter])

Copy a slice of SockFilter instructions to the end of the buffer

Panics on buffer full.

Source

pub fn inst(&mut self, instruction: SockFilter)

Copy a SockFilter instruction to the end of the buffer

Panics on buffer full.

Source

pub fn if_eq(&mut self, k: usize, block: &[SockFilter])

Build a conditional instruction block

This copies a group of SockFilter instructions to the end of the buffer, gated by a conditional jump such that the block runs if the accumulator matches the value k.

Panics if the buffer is full, or the block we are adding is larger than the reach of a single jump (256 instructions).

Source

pub fn if_any_eq(&mut self, k_list: &[usize], block: &[SockFilter])

Build a conditional block that checks multiple values

This is similar to making repeated calls to if_eq(), however the block of code is only included once. This generates a series of conditional jump instructions which test each value in k_list, and another jump which skips the block if none of the values have matched.

Panics if the buffer is full, or if either the list of values or the instruction block are too large to jump past at once (256 instructions each).

Trait Implementations§

Source§

impl Clone for ProgramBuffer

Source§

fn clone(&self) -> ProgramBuffer

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ProgramBuffer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Format a ProgramBuffer as a list of instructions, one per line.

Source§

impl PartialEq for ProgramBuffer

Source§

fn eq(&self, other: &ProgramBuffer) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for ProgramBuffer

Source§

impl StructuralPartialEq for ProgramBuffer

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.