Skip to content

Commit

Permalink
Fork loopdev
Browse files Browse the repository at this point in the history
Sadly the loopdev crates is unmaintained and this patch needs some
upstream functionality beyond latest crates.io release.
  • Loading branch information
Felix Obenhuber committed Sep 12, 2023
1 parent 514334a commit 6dad08d
Show file tree
Hide file tree
Showing 7 changed files with 630 additions and 122 deletions.
46 changes: 2 additions & 44 deletions Cargo.lock

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

7 changes: 3 additions & 4 deletions northstar-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ itertools = { version = "0.11.0", optional = true }
lazy_static = { version = "1.4.0", optional = true }
libc = { version = "0.2.140", optional = true }
log = { version = "0.4.19", features = [ "serde", "max_level_trace", "release_max_level_debug"] }
loopdev = { version = "0.4.0", optional = true }
memchr = "2.6.2"
memfd = { version = "0.6.2", optional = true }
memoffset = { version = "0.9.0", optional = true }
Expand Down Expand Up @@ -68,11 +67,11 @@ zip = { version = "0.6.6", default-features = false, optional = true }
api = ["bytes", "futures", "npk", "pkg-version", "serde_json", "tokio", "tokio-util"]
npk = ["base64", "byteorder", "ed25519-dalek", "hex", "humanize-rs", "itertools", "pkg-version", "rand_core", "seccomp", "serde_json", "serde_plain", "serde_with", "serde_yaml", "sha2", "strum", "strum_macros", "tempfile", "toml", "uuid", "zeroize", "zip"]
rexec = ["nix", "memfd"]
runtime = ["api", "async-stream", "async-trait", "bincode", "bytesize", "caps", "cgroups-rs", "ed25519-dalek", "futures", "heck", "hex", "hmac", "humantime", "humantime-serde", "inotify", "itertools", "lazy_static", "libc", "loopdev", "memfd", "memoffset", "nanoid", "nix", "npk", "rlimit", "serde_plain", "tempfile", "tokio", "tokio-eventfd", "tokio-util", "url", "umask"]
runtime = ["api", "async-stream", "async-trait", "bincode", "bindgen", "bytesize", "caps", "cgroups-rs", "ed25519-dalek", "futures", "heck", "hex", "hmac", "humantime-serde", "inotify", "itertools", "lazy_static", "libc", "memfd", "memoffset", "nanoid", "nix", "npk", "rlimit", "serde_plain", "tempfile", "tokio", "tokio-eventfd", "tokio-util", "url", "umask"]
seccomp = ["bindgen", "caps", "lazy_static", "memoffset", "nix", "npk"]

[dev-dependencies]
anyhow = { version = "1.0.71", features = ["backtrace"] }
anyhow = "1.0.71"
memfd = "0.6.2"
proptest = "1.2.0"
rstest = { version = "0.18.1", default-features = false }
Expand All @@ -82,7 +81,7 @@ tokio-test = "0.4.2"
toml = "0.7.6"

[build-dependencies]
anyhow = { version = "1.0.71", features = ["backtrace"] }
anyhow = "1.0.71"
bindgen = { version = "0.65.1", default-features = false, features = ["runtime"], optional = true }

[package.metadata.docs.rs]
Expand Down
160 changes: 89 additions & 71 deletions northstar-runtime/build.rs
Original file line number Diff line number Diff line change
@@ -1,84 +1,102 @@
fn main() {
use anyhow::{Context, Result};
use std::{env, path::PathBuf};

use bindgen::Builder;

fn main() -> Result<()> {
loopdev()?;

#[cfg(feature = "seccomp")]
generate_seccomp();
generate_seccomp()?;

Ok(())
}

#[cfg(feature = "seccomp")]
fn generate_seccomp() {
use std::{env, fs, io::Write, path};
fn generate_seccomp() -> Result<()> {
use std::{fs, io::Write};

fn generate() -> anyhow::Result<()> {
let lines = bindgen::Builder::default()
.header_contents("syscall.h", "#include <sys/syscall.h>")
.allowlist_var("SYS_[0-9a-zA-Z_]+")
.generate()
.expect("failed to generate syscall bindings")
.to_string();
let lines: Vec<&str> = lines
.lines()
.filter(|s| s.starts_with("pub const SYS_"))
.collect();
let lines = bindgen::Builder::default()
.header_contents("syscall.h", "#include <sys/syscall.h>")
.allowlist_var("SYS_[0-9a-zA-Z_]+")
.generate()
.context("failed to generate syscall bindings")?
.to_string();
let lines: Vec<&str> = lines
.lines()
.filter(|s| s.starts_with("pub const SYS_"))
.collect();

let out_path = path::PathBuf::from(env::var("OUT_DIR")?);
let mut f = fs::File::create(out_path.join("syscall_bindings.rs"))?;
let out_path = PathBuf::from(env::var("OUT_DIR")?);
let mut f = fs::File::create(out_path.join("syscall_bindings.rs"))?;

f.write_all(lines.join("\n").as_bytes())?;
writeln!(f)?;
writeln!(f)?;
f.write_all(lines.join("\n").as_bytes())?;
writeln!(f)?;
writeln!(f)?;

// Write static map that associates syscall strings with syscall numbers
writeln!(f, "lazy_static::lazy_static! {{")?;
writeln!(
f,
" pub(super) static ref SYSCALL_MAP: std::collections::HashMap<&'static str, u32> = {{"
)?;
writeln!(f, " let mut map = std::collections::HashMap::new();")?;
lines.iter().try_for_each(|l| {
let mut split = l.split_ascii_whitespace();
let var = split.nth(2).unwrap().trim_end_matches(':');
let name = var.replace("SYS_", "");
writeln!(f, " map.insert(\"{name}\", {var});")?;
std::io::Result::Ok(())
})?;
writeln!(f, " map")?;
writeln!(f, " }};")?;
writeln!(f, "}}")?;
// Write static map that associates syscall strings with syscall numbers
writeln!(f, "lazy_static::lazy_static! {{")?;
writeln!(
f,
" pub(super) static ref SYSCALL_MAP: std::collections::HashMap<&'static str, u32> = {{"
)?;
writeln!(f, " let mut map = std::collections::HashMap::new();")?;
lines.iter().try_for_each(|l| {
let mut split = l.split_ascii_whitespace();
let var = split.nth(2).unwrap().trim_end_matches(':');
let name = var.replace("SYS_", "");
writeln!(f, " map.insert(\"{name}\", {var});")?;
std::io::Result::Ok(())
})?;
writeln!(f, " map")?;
writeln!(f, " }};")?;
writeln!(f, "}}")?;

let out_path = std::path::PathBuf::from(env::var("OUT_DIR")?);
bindgen::Builder::default()
.header_contents(
"seccomp.h",
r#"#include <linux/seccomp.h>
let out_path = PathBuf::from(env::var("OUT_DIR")?);
bindgen::Builder::default()
.header_contents(
"seccomp.h",
r#"#include <linux/seccomp.h>
#include <linux/filter.h>
#include <linux/audit.h>"#,
)
.layout_tests(false)
.allowlist_type("seccomp_data")
.allowlist_type("sock_fprog")
.allowlist_var("BPF_ABS")
.allowlist_var("BPF_AND")
.allowlist_var("BPF_ALU")
.allowlist_var("BPF_IMM")
.allowlist_var("BPF_IND")
.allowlist_var("BPF_JEQ")
.allowlist_var("BPF_JMP")
.allowlist_var("BPF_NEG")
.allowlist_var("BPF_K")
.allowlist_var("BPF_LD")
.allowlist_var("BPF_LDX")
.allowlist_var("BPF_MEM")
.allowlist_var("BPF_OR")
.allowlist_var("BPF_RET")
.allowlist_var("BPF_ST")
.allowlist_var("BPF_W")
.allowlist_var("BPF_MAXINSNS")
.allowlist_var("AUDIT_ARCH_X86_64")
.allowlist_var("AUDIT_ARCH_AARCH64")
.generate()
.expect("failed to generate seccomp bindings")
.write_to_file(out_path.join("seccomp_bindings.rs"))?;
Ok(())
}
)
.layout_tests(false)
.allowlist_type("seccomp_data")
.allowlist_type("sock_fprog")
.allowlist_var("BPF_ABS")
.allowlist_var("BPF_AND")
.allowlist_var("BPF_ALU")
.allowlist_var("BPF_IMM")
.allowlist_var("BPF_IND")
.allowlist_var("BPF_JEQ")
.allowlist_var("BPF_JMP")
.allowlist_var("BPF_NEG")
.allowlist_var("BPF_K")
.allowlist_var("BPF_LD")
.allowlist_var("BPF_LDX")
.allowlist_var("BPF_MEM")
.allowlist_var("BPF_OR")
.allowlist_var("BPF_RET")
.allowlist_var("BPF_ST")
.allowlist_var("BPF_W")
.allowlist_var("BPF_MAXINSNS")
.allowlist_var("AUDIT_ARCH_X86_64")
.allowlist_var("AUDIT_ARCH_AARCH64")
.generate()
.context("failed to generate seccomp bindings")?
.write_to_file(out_path.join("seccomp_bindings.rs"))?;
Ok(())
}

fn loopdev() -> Result<()> {
let out_path = PathBuf::from(env::var("OUT_DIR")?);
let bindings = Builder::default()
.header_contents("loopdev.h", "#include <linux/loop.h>")
.derive_default(true)
.generate()
.context("failed to generated loopdev bindings")?;

generate().expect("failed to generate seccomp bindings");
bindings
.write_to_file(out_path.join("loopdev.rs"))
.context("failed to generated loopdev bindings")
}
20 changes: 20 additions & 0 deletions northstar-runtime/src/runtime/loopdev/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2016 Michael Daffin

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Loading

0 comments on commit 6dad08d

Please sign in to comment.