Skip to content

Commit

Permalink
feat(seccomp): update seccompiler to use libseccomp
Browse files Browse the repository at this point in the history
libseccomp provides better quality compiler for
bpf seccomp programs than our current implementation.

Signed-off-by: Egor Lazarchuk <[email protected]>
  • Loading branch information
ShadowCurse committed Nov 25, 2024
1 parent 81cba86 commit 224fdb4
Show file tree
Hide file tree
Showing 14 changed files with 356 additions and 3,825 deletions.
30 changes: 26 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src/firecracker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@ serde = { version = "1.0.215", features = ["derive"] }
userfaultfd = "0.8.1"

[build-dependencies]
bincode = "1.2.1"
seccompiler = { path = "../seccompiler" }
serde = { version = "1.0.215" }
serde_json = "1.0.133"

[features]
tracing = ["log-instrument", "seccompiler/tracing", "utils/tracing", "vmm/tracing"]
tracing = ["log-instrument", "utils/tracing", "vmm/tracing"]
gdb = ["vmm/gdb"]

[lints]
Expand Down
21 changes: 2 additions & 19 deletions src/firecracker/build.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use std::collections::BTreeMap;
use std::fs::File;
use std::path::Path;

use seccompiler::common::BpfProgram;
use seccompiler::compiler::{Compiler, JsonFile};

const ADVANCED_BINARY_FILTER_FILE_NAME: &str = "seccomp_filter.bpf";

const JSON_DIR: &str = "../../resources/seccomp";
Expand Down Expand Up @@ -44,19 +39,7 @@ fn main() {
// Also retrigger the build script on any seccompiler source code change.
println!("cargo:rerun-if-changed={}", SECCOMPILER_SRC_DIR);

let input = std::fs::read_to_string(seccomp_json_path).expect("Correct input file");
let filters: JsonFile = serde_json::from_str(&input).expect("Input read");

let arch = target_arch.as_str().try_into().expect("Target");
let compiler = Compiler::new(arch);

// transform the IR into a Map of BPFPrograms
let bpf_data: BTreeMap<String, BpfProgram> = compiler
.compile_blob(filters.0, false)
.expect("Successfull compilation");

// serialize the BPF programs & output them to a file
let out_path = format!("{}/{}", out_dir, ADVANCED_BINARY_FILTER_FILE_NAME);
let output_file = File::create(out_path).expect("Create seccompiler output path");
bincode::serialize_into(output_file, &bpf_data).expect("Seccompiler serialization");
seccompiler::compile_bpf(&seccomp_json_path, &target_arch, &out_path, false)
.expect("Cannot compile seccomp filters");
}
15 changes: 4 additions & 11 deletions src/seccompiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,18 @@ bench = false

[[bin]]
name = "seccompiler-bin"
path = "src/seccompiler_bin.rs"
path = "src/bin.rs"
bench = false

[dependencies]
clap = { version = "4.5.21", features = ["derive", "string"] }
bincode = "1.2.1"
displaydoc = "0.2.5"
libc = "0.2.164"
log-instrument = { path = "../log-instrument", optional = true }
libseccomp = "0.3.0"
serde = { version = "1.0.215", features = ["derive"] }
serde_json = "1.0.133"
displaydoc = "0.2.5"
thiserror = "2.0.3"

utils = { path = "../utils" }

[dev-dependencies]
vmm-sys-util = "0.12.1"

[features]
tracing = ["log-instrument", "utils/tracing"]

[lints]
workspace = true
Loading

0 comments on commit 224fdb4

Please sign in to comment.