Skip to content

Commit c680203

Browse files
committed
feat: remove libseccomp rust dependency
Replace `libseccomp` with in house binding. This way we don't need to add another dependency and allows us to have access the `seccomp_export_bpf_mem` method, not exposed in the `libseccomp` crate. This creates another issue though: the `seccomp_export_bpf_mem` function needs to be exposed by the libseccomp library. This is not an issue when `seccompiler` is build in the docker environment because we build it from source. But version provided by linux distribution might have this function not exposed. Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent 412c3d2 commit c680203

File tree

6 files changed

+330
-129
lines changed

6 files changed

+330
-129
lines changed

Cargo.lock

Lines changed: 0 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/seccompiler/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ bincode = "1.2.1"
2020
clap = { version = "4.5.21", features = ["derive", "string"] }
2121
displaydoc = "0.2.5"
2222
libc = "0.2.167"
23-
libseccomp = "0.3.0"
2423
serde = { version = "1.0.215", features = ["derive"] }
2524
serde_json = "1.0.133"
2625
thiserror = "2.0.3"

src/seccompiler/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
fn main() {
5+
println!("cargo::rustc-link-search=/usr/local/lib");
56
println!("cargo::rustc-link-lib=seccomp");
67
}

src/seccompiler/src/bindings.rs

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#![allow(non_camel_case_types)]
5+
#![allow(non_snake_case)]
6+
7+
//! Raw FFI bindings for libseccomp library
8+
9+
use std::os::raw::*;
10+
11+
/// Filter context/handle (`*mut`)
12+
pub type scmp_filter_ctx = *mut c_void;
13+
/// Filter context/handle (`*const`)
14+
pub type const_scmp_filter_ctx = *const c_void;
15+
16+
/// Comparison operators
17+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
18+
#[repr(C)]
19+
pub enum scmp_compare {
20+
_SCMP_CMP_MIN = 0,
21+
/// not equal
22+
SCMP_CMP_NE = 1,
23+
/// less than
24+
SCMP_CMP_LT = 2,
25+
/// less than or equal
26+
SCMP_CMP_LE = 3,
27+
/// equal
28+
SCMP_CMP_EQ = 4,
29+
/// greater than or equal
30+
SCMP_CMP_GE = 5,
31+
/// greater than
32+
SCMP_CMP_GT = 6,
33+
/// masked equality
34+
SCMP_CMP_MASKED_EQ = 7,
35+
_SCMP_CMP_MAX,
36+
}
37+
38+
/// Argument datum
39+
pub type scmp_datum_t = u64;
40+
41+
/// Argument / Value comparison definition
42+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
43+
#[repr(C)]
44+
pub struct scmp_arg_cmp {
45+
/// argument number, starting at 0
46+
pub arg: c_uint,
47+
/// the comparison op, e.g. `SCMP_CMP_*`
48+
pub op: scmp_compare,
49+
pub datum_a: scmp_datum_t,
50+
pub datum_b: scmp_datum_t,
51+
}
52+
53+
pub const SCMP_ARCH_X86_64: u32 = 0xc000003e;
54+
pub const SCMP_ARCH_AARCH64: u32 = 0xc00000b7;
55+
/// Kill the process
56+
pub const SCMP_ACT_KILL_PROCESS: u32 = 0x80000000;
57+
/// Kill the thread
58+
pub const SCMP_ACT_KILL_THREAD: u32 = 0x00000000;
59+
/// Throw a `SIGSYS` signal
60+
pub const SCMP_ACT_TRAP: u32 = 0x00030000;
61+
/// Notifies userspace
62+
pub const SCMP_ACT_ERRNO_MASK: u32 = 0x00050000;
63+
/// Return the specified error code
64+
#[must_use]
65+
pub const fn SCMP_ACT_ERRNO(x: u16) -> u32 {
66+
SCMP_ACT_ERRNO_MASK | x as u32
67+
}
68+
pub const SCMP_ACT_TRACE_MASK: u32 = 0x7ff00000;
69+
/// Notify a tracing process with the specified value
70+
#[must_use]
71+
pub const fn SCMP_ACT_TRACE(x: u16) -> u32 {
72+
SCMP_ACT_TRACE_MASK | x as u32
73+
}
74+
/// Allow the syscall to be executed after the action has been logged
75+
pub const SCMP_ACT_LOG: u32 = 0x7ffc0000;
76+
/// Allow the syscall to be executed
77+
pub const SCMP_ACT_ALLOW: u32 = 0x7fff0000;
78+
79+
#[link(name = "seccomp")]
80+
extern "C" {
81+
/// Initialize the filter state
82+
///
83+
/// - `def_action`: the default filter action
84+
///
85+
/// This function initializes the internal seccomp filter state and should
86+
/// be called before any other functions in this library to ensure the filter
87+
/// state is initialized. Returns a filter context on success, `ptr::null()` on failure.
88+
pub fn seccomp_init(def_action: u32) -> scmp_filter_ctx;
89+
90+
/// Adds an architecture to the filter
91+
///
92+
/// - `ctx`: the filter context
93+
/// - `arch_token`: the architecture token, e.g. `SCMP_ARCH_*`
94+
///
95+
/// This function adds a new architecture to the given seccomp filter context.
96+
/// Any new rules added after this function successfully returns will be added
97+
/// to this architecture but existing rules will not be added to this
98+
/// architecture. If the architecture token is [`SCMP_ARCH_NATIVE`] then the native
99+
/// architecture will be assumed. Returns zero on success, `-libc::EEXIST` if
100+
/// specified architecture is already present, other negative values on failure.
101+
pub fn seccomp_arch_add(ctx: scmp_filter_ctx, arch_token: u32) -> c_int;
102+
103+
/// Resolve a syscall name to a number
104+
///
105+
/// - `name`: the syscall name
106+
///
107+
/// Resolve the given syscall name to the syscall number. Returns the syscall
108+
/// number on success, including negative pseudo syscall numbers (e.g. `__PNR_*`);
109+
/// returns [`__NR_SCMP_ERROR`] on failure.
110+
pub fn seccomp_syscall_resolve_name(name: *const c_char) -> c_int;
111+
112+
/// Add a new rule to the filter
113+
///
114+
/// - `ctx`: the filter context
115+
/// - `action`: the filter action
116+
/// - `syscall`: the syscall number
117+
/// - `arg_cnt`: the number of argument filters in the argument filter chain
118+
/// - `...`: [`scmp_arg_cmp`] structs
119+
///
120+
/// This function adds a series of new argument/value checks to the seccomp
121+
/// filter for the given syscall; multiple argument/value checks can be
122+
/// specified and they will be chained together (AND'd together) in the filter.
123+
/// If the specified rule needs to be adjusted due to architecture specifics it
124+
/// will be adjusted without notification. Returns zero on success, negative
125+
/// values on failure.
126+
pub fn seccomp_rule_add(
127+
ctx: scmp_filter_ctx,
128+
action: u32,
129+
syscall: c_int,
130+
arg_cnt: c_uint,
131+
...
132+
) -> c_int;
133+
134+
/// Add a new rule to the filter
135+
///
136+
/// - `ctx`: the filter context
137+
/// - `action`: the filter action
138+
/// - `syscall`: the syscall number
139+
/// - `arg_cnt`: the number of elements in the arg_array parameter
140+
/// - `arg_array`: array of [`scmp_arg_cmp`] structs
141+
///
142+
/// This function adds a series of new argument/value checks to the seccomp
143+
/// filter for the given syscall; multiple argument/value checks can be
144+
/// specified and they will be chained together (AND'd together) in the filter.
145+
/// If the specified rule needs to be adjusted due to architecture specifics it
146+
/// will be adjusted without notification. Returns zero on success, negative
147+
/// values on failure.
148+
pub fn seccomp_rule_add_array(
149+
ctx: scmp_filter_ctx,
150+
action: u32,
151+
syscall: c_int,
152+
arg_cnt: c_uint,
153+
arg_array: *const scmp_arg_cmp,
154+
) -> c_int;
155+
156+
/// Generate seccomp Berkeley Packet Filter (BPF) code and export it to a buffer
157+
///
158+
/// - `ctx`: the filter context
159+
/// - `buf`: the destination buffer
160+
/// - `len`: on input the length of the buffer, on output the number of bytes in the program
161+
///
162+
/// This function generates seccomp Berkeley Packer Filter (BPF) code and writes
163+
/// it to the given buffer. Returns zero on success, negative values on failure.
164+
pub fn seccomp_export_bpf_mem(
165+
ctx: const_scmp_filter_ctx,
166+
buf: *mut c_void,
167+
len: *mut usize,
168+
) -> c_int;
169+
}
170+
171+
/// Negative pseudo syscall number returned by some functions in case of an error
172+
pub const __NR_SCMP_ERROR: c_int = -1;

0 commit comments

Comments
 (0)