Skip to content

Commit

Permalink
Bump libbpf-rs dependency to 0.24
Browse files Browse the repository at this point in the history
Signed-off-by: Daiki Ueno <[email protected]>
  • Loading branch information
ueno committed Aug 28, 2024
1 parent 71233ff commit e2bf359
Show file tree
Hide file tree
Showing 10 changed files with 351 additions and 471 deletions.
720 changes: 298 additions & 422 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ crypto-auditing = { version = "=0.2.2", path = "crypto-auditing" }
futures = "0.3"
hex = "0.4"
inotify = "0.10.2"
libbpf-rs = { version = "0.21", features = ["novendor"] }
libbpf-cargo = { version = "0.21", features = ["novendor"] }
libbpf-rs = { version = "0.24.4", default-features = false }
libbpf-cargo = { version = "0.24.4", default-features = false }
libc = "0.2"
nix = "0.26"
openssl = "0.10"
Expand Down
3 changes: 2 additions & 1 deletion agent/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use libbpf_cargo::SkeletonBuilder;
use std::{
env,
ffi::OsStr,
fs::{self, File},
path::PathBuf,
process::Command,
Expand Down Expand Up @@ -35,7 +36,7 @@ fn main() {
let src = srcdir.join("src").join("bpf").join("audit.bpf.c");
SkeletonBuilder::new()
.source(&src)
.clang_args(&format!("-I{}", builddir.display()))
.clang_args([OsStr::new("-I"), builddir.as_os_str()])
.build_and_generate(&builddir.join("audit.skel.rs"))
.unwrap();
println!("cargo:rerun-if-changed={}", src.display());
Expand Down
26 changes: 10 additions & 16 deletions agent/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use openssl::{
symm::{Cipher, Crypter, Mode},
};
use std::io::prelude::*;
use std::mem::MaybeUninit;
use std::path::Path;
use tokio::io::AsyncReadExt;
use tokio::time::{timeout, Duration};
Expand Down Expand Up @@ -103,60 +104,53 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
bump_memlock_rlimit()?;

let skel_builder = AuditSkelBuilder::default();
let open_skel = skel_builder.open()?;
let mut storage = MaybeUninit::uninit();
let open_skel = skel_builder.open(&mut storage)?;
let mut skel = open_skel.load()?;

let mut progs = skel.progs_mut();

let mut links = Vec::new();
for library in &config.library {
let prog = progs.new_context();
if let Ok(link) = prog.attach_usdt(
if let Ok(link) = skel.progs.new_context.attach_usdt(
-1, // any process
library,
"crypto_auditing",
"new_context",
) {
links.push(link);
}
let prog = progs.word_data();
if let Ok(link) = prog.attach_usdt(
if let Ok(link) = skel.progs.word_data.attach_usdt(
-1, // any process
library,
"crypto_auditing",
"word_data",
) {
links.push(link);
}
let prog = progs.string_data();
if let Ok(link) = prog.attach_usdt(
if let Ok(link) = skel.progs.string_data.attach_usdt(
-1, // any process
library,
"crypto_auditing",
"string_data",
) {
links.push(link);
}
let prog = progs.blob_data();
if let Ok(link) = prog.attach_usdt(
if let Ok(link) = skel.progs.blob_data.attach_usdt(
-1, // any process
library,
"crypto_auditing",
"blob_data",
) {
links.push(link);
}
let prog = progs.data();
if let Ok(link) = prog.attach_usdt(
if let Ok(link) = skel.progs.data.attach_usdt(
-1, // any process
library,
"crypto_auditing",
"data",
) {
links.push(link);
}
let prog = progs.new_context_with_data();
if let Ok(link) = prog.attach_usdt(
if let Ok(link) = skel.progs.new_context_with_data.attach_usdt(
-1, // any process
library,
"crypto_auditing",
Expand All @@ -171,7 +165,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
rand_bytes(&mut encryption_key)?;

start(async {
let mut rb = ringbuf::RingBuffer::new(skel.obj.map_mut("ringbuf").unwrap());
let mut rb = ringbuf::RingBuffer::new(&skel.maps.ringbuf);

if let Some((ref user, ref group)) = config.user {
permissions::run_as(user, group)?;
Expand Down
4 changes: 2 additions & 2 deletions agent/src/ringbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Licensed under LGPL-2.1 or BSD-2-Clause.

use core::task::{Context, Poll};
use libbpf_rs::{query::MapInfoIter, Map};
use libbpf_rs::{query::MapInfoIter, Map, MapCore};
use std::io::Result;
use std::num::NonZeroUsize;
use std::os::fd::{AsFd, AsRawFd, RawFd};
Expand All @@ -26,7 +26,7 @@ impl RingBuffer {
pub fn new(map: &Map) -> Self {
let mut max_entries = 0;
for m in MapInfoIter::default() {
if m.name == map.name() {
if m.name.as_bytes() == map.info().unwrap().name().unwrap().as_bytes() {
max_entries = m.max_entries;
}
}
Expand Down
3 changes: 2 additions & 1 deletion agent/tests/agenttest/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use libbpf_cargo::SkeletonBuilder;
use std::{
env,
ffi::OsStr,
fs::{self, File},
path::PathBuf,
process::Command,
Expand Down Expand Up @@ -35,7 +36,7 @@ fn main() {
let src = srcdir.join("src").join("bpf").join("agent.bpf.c");
SkeletonBuilder::new()
.source(&src)
.clang_args(&format!("-I{}", builddir.display()))
.clang_args([OsStr::new("-I"), builddir.as_os_str()])
.build_and_generate(&builddir.join("agent.skel.rs"))
.unwrap();
println!("cargo:rerun-if-changed={}", src.display());
Expand Down
21 changes: 13 additions & 8 deletions agent/tests/agenttest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
use anyhow::{bail, Result};
use libbpf_rs::{
skel::{OpenSkel, SkelBuilder},
Link, Map, Object, RingBufferBuilder,
Link, Map, OpenObject, RingBufferBuilder,
};
use std::mem::MaybeUninit;
use std::path::Path;
use std::process::Child;
use std::time::Duration;
Expand All @@ -28,15 +29,19 @@ pub fn bump_memlock_rlimit() -> Result<()> {
Ok(())
}

pub fn attach_bpf(process: &Child, path: impl AsRef<Path>) -> Result<(Link, Object)> {
pub fn attach_bpf<'obj>(
process: &'obj Child,
path: impl AsRef<Path>,
storage: &'obj mut MaybeUninit<OpenObject>,
) -> Result<(Link, AgentSkel<'obj>)> {
let skel_builder = AgentSkelBuilder::default();
let open_skel = skel_builder.open()?;
let mut skel = open_skel.load()?;

let mut progs = skel.progs_mut();
let prog = progs.event_group();
let open_skel = skel_builder.open(storage)?;
let mut skel = open_skel.load()?;

let link = prog
let link = skel
.progs
.event_group
.attach_usdt(
process.id() as i32,
path.as_ref(),
Expand All @@ -45,7 +50,7 @@ pub fn attach_bpf(process: &Child, path: impl AsRef<Path>) -> Result<(Link, Obje
)
.expect("unable to attach prog");

Ok((link, skel.obj))
Ok((link, skel))
}

// Copied from libbpf-rs/libbpf-rs/tests/test.rs
Expand Down
11 changes: 6 additions & 5 deletions agent/tests/coalesce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crypto_auditing::types::EventGroup;
use probe::probe;
use serde_cbor::de::Deserializer;
use std::env;
use std::mem::MaybeUninit;
use std::path::PathBuf;
use std::process::{Child, Command};
use std::thread;
Expand Down Expand Up @@ -77,14 +78,14 @@ fn test_probe_coalesce() {
let bar = String::from("bar\0");
let baz = String::from("baz\0");

let (_link, object) =
attach_bpf(&process.0, &agent_path).expect("unable to attach agent.bpf.o");
let map = object.map("ringbuf").expect("unable to get ringbuf map");
let mut storage = MaybeUninit::uninit();
let (_link, skel) =
attach_bpf(&process.0, &agent_path, &mut storage).expect("unable to attach agent.bpf.o");

let timeout = Duration::from_secs(10);

let result = with_ringbuffer(
map,
&skel.maps.ringbuf,
|| {
probe!(crypto_auditing, new_context, 1, 2);
probe!(crypto_auditing, word_data, 1, foo.as_ptr(), 3);
Expand All @@ -103,7 +104,7 @@ fn test_probe_coalesce() {
.expect("unable to exercise probe points");
assert_eq!(result, 4);
let result = with_ringbuffer(
map,
&skel.maps.ringbuf,
|| {
probe!(crypto_auditing, new_context, 4, 5);
},
Expand Down
13 changes: 7 additions & 6 deletions agent/tests/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use libc::{c_long, c_uchar, c_void};
use probe::probe;
use serde_cbor::de::Deserializer;
use std::env;
use std::mem::MaybeUninit;
use std::path::PathBuf;
use std::process::{Child, Command};
use std::thread;
Expand Down Expand Up @@ -100,14 +101,14 @@ fn test_probe_composite() {
},
];

let (_link, object) =
attach_bpf(&process.0, &agent_path).expect("unable to attach agent.bpf.o");
let map = object.map("ringbuf").expect("unable to get ringbuf map");
let mut storage = MaybeUninit::uninit();
let (_link, skel) =
attach_bpf(&process.0, &agent_path, &mut storage).expect("unable to attach agent.bpf.o");

let timeout = Duration::from_secs(10);

let result = with_ringbuffer(
map,
&skel.maps.ringbuf,
|| {
probe!(crypto_auditing, new_context, 1, 2);
},
Expand All @@ -117,7 +118,7 @@ fn test_probe_composite() {
assert_eq!(result, 1);

let result = with_ringbuffer(
map,
&skel.maps.ringbuf,
|| {
probe!(crypto_auditing, data, 1, events.as_ptr(), events.len());
},
Expand All @@ -127,7 +128,7 @@ fn test_probe_composite() {
assert_eq!(result, 1);

let result = with_ringbuffer(
map,
&skel.maps.ringbuf,
|| {
probe!(crypto_auditing, new_context, 4, 5);
},
Expand Down
17 changes: 9 additions & 8 deletions agent/tests/no_coalesce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crypto_auditing::types::EventGroup;
use probe::probe;
use serde_cbor::de::Deserializer;
use std::env;
use std::mem::MaybeUninit;
use std::path::PathBuf;
use std::process::{Child, Command};
use std::thread;
Expand Down Expand Up @@ -75,14 +76,14 @@ fn test_probe_no_coalesce() {
let bar = String::from("bar\0");
let baz = String::from("baz\0");

let (_link, object) =
attach_bpf(&process.0, &agent_path).expect("unable to attach agent.bpf.o");
let map = object.map("ringbuf").expect("unable to get ringbuf map");
let mut storage = MaybeUninit::uninit();
let (_link, skel) =
attach_bpf(&process.0, &agent_path, &mut storage).expect("unable to attach agent.bpf.o");

let timeout = Duration::from_secs(10);

let result = with_ringbuffer(
map,
&skel.maps.ringbuf,
|| {
probe!(crypto_auditing, new_context, 1, 2);
},
Expand All @@ -91,7 +92,7 @@ fn test_probe_no_coalesce() {
.expect("unable to exercise probe points");
assert_eq!(result, 1);
let result = with_ringbuffer(
map,
&skel.maps.ringbuf,
|| {
probe!(crypto_auditing, word_data, 1, foo.as_ptr(), 3);
},
Expand All @@ -100,7 +101,7 @@ fn test_probe_no_coalesce() {
.expect("unable to exercise probe points");
assert_eq!(result, 1);
let result = with_ringbuffer(
map,
&skel.maps.ringbuf,
|| {
probe!(crypto_auditing, string_data, 1, bar.as_ptr(), bar.as_ptr());
},
Expand All @@ -109,7 +110,7 @@ fn test_probe_no_coalesce() {
.expect("unable to exercise probe points");
assert_eq!(result, 1);
let result = with_ringbuffer(
map,
&skel.maps.ringbuf,
|| {
probe!(
crypto_auditing,
Expand All @@ -125,7 +126,7 @@ fn test_probe_no_coalesce() {
.expect("unable to exercise probe points");
assert_eq!(result, 1);
let result = with_ringbuffer(
map,
&skel.maps.ringbuf,
|| {
probe!(crypto_auditing, new_context, 4, 5);
},
Expand Down

0 comments on commit e2bf359

Please sign in to comment.