Skip to content

Commit 95cdade

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 95cdade

File tree

6 files changed

+333
-129
lines changed

6 files changed

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

0 commit comments

Comments
 (0)