Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions fact-ebpf/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(dead_code, non_camel_case_types)]

use std::{error::Error, ffi::c_char, fmt::Display, hash::Hash, path::PathBuf};
use std::{error::Error, ffi::c_char, fmt::Display, hash::Hash, path::Path};

use aya::{maps::lpm_trie, Pod};
use libc::memcpy;
Expand All @@ -25,10 +25,10 @@ impl Display for PathPrefixError {
}
}

impl TryFrom<&PathBuf> for path_prefix_t {
impl TryFrom<&Path> for path_prefix_t {
type Error = PathPrefixError;

fn try_from(value: &PathBuf) -> Result<Self, PathPrefixError> {
fn try_from(value: &Path) -> Result<Self, PathPrefixError> {
let Some(filename) = value.to_str() else {
return Err(PathPrefixError {
prefix: value.display().to_string(),
Expand Down
41 changes: 16 additions & 25 deletions fact/src/bpf/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{io, path::PathBuf};
use std::io;

use anyhow::{Context, bail};
use aya::{
Expand All @@ -7,7 +7,6 @@ use aya::{
programs::{Program, lsm::LsmLink},
};
use checks::Checks;
use globset::{Glob, GlobSet, GlobSetBuilder};
use libc::c_char;
use log::{error, info, warn};
use tokio::{
Expand All @@ -16,7 +15,12 @@ use tokio::{
task::JoinSet,
};

use crate::{config::BpfConfig, event::Event, host_info, metrics::EventCounter};
use crate::{
config::{BpfConfig, PathsConfig},
event::Event,
host_info,
metrics::EventCounter,
};

use fact_ebpf::{LPM_SIZE_MAX, event_t, inode_key_t, inode_value_t, metrics_t, path_prefix_t};

Expand All @@ -30,11 +34,9 @@ pub struct Bpf {

tx: mpsc::Sender<Event>,

paths_config: watch::Receiver<Vec<PathBuf>>,
paths_config: watch::Receiver<PathsConfig>,
paths_lpm_map: LpmTrie<MapData, [c_char; LPM_SIZE_MAX as usize], c_char>,

paths_globset: GlobSet,

links: Vec<LsmLink>,

running: watch::Receiver<bool>,
Expand All @@ -43,7 +45,7 @@ pub struct Bpf {

impl Bpf {
pub fn new(
paths_config: watch::Receiver<Vec<PathBuf>>,
paths_config: watch::Receiver<PathsConfig>,
bpf_config: &BpfConfig,
running: watch::Receiver<bool>,
metrics: EventCounter,
Expand All @@ -66,7 +68,6 @@ impl Bpf {
checks,
tx,
paths_config,
paths_globset: GlobSet::empty(),
paths_lpm_map,
links: Vec::new(),
running,
Expand Down Expand Up @@ -176,10 +177,9 @@ impl Bpf {
}

fn load_paths(&mut self) -> anyhow::Result<()> {
if self.paths_config.borrow().is_empty() {
if self.paths_config.borrow().patterns().is_empty() {
self.detach_progs();
self.cleanup_lpm_map(&[])?;
self.paths_globset = GlobSet::empty();
return Ok(());
}

Expand All @@ -190,22 +190,13 @@ impl Bpf {
// Add the new prefixes
let new_paths = {
let paths_config = self.paths_config.borrow();
let mut new_paths = Vec::with_capacity(paths_config.len());
let mut builder = GlobSetBuilder::new();
for p in paths_config.iter() {
let Some(glob_str) = p.to_str() else {
bail!("failed to convert path {} to string", p.display());
};

builder.add(
Glob::new(glob_str).with_context(|| format!("invalid glob {}", glob_str))?,
);

let patterns = paths_config.patterns();
let mut new_paths = Vec::with_capacity(patterns.len());
for p in patterns.iter().map(|p| host_info::remove_host_mount(p)) {
let prefix = path_prefix_t::try_from(p)?;
self.paths_lpm_map.insert(&prefix.into(), 0, 0)?;
new_paths.push(prefix);
}
self.paths_globset = builder.build()?;
new_paths
};

Expand Down Expand Up @@ -318,7 +309,7 @@ impl Bpf {
// so we let the event go into HostScanner and make the
// decision there.
if !event.is_monitored_by_parent() &&
event.is_ignored(&self.paths_globset) {
event.is_ignored(&self.paths_config.borrow().globset) {
Comment thread
Molter73 marked this conversation as resolved.
self.metrics.ignored();
continue;
}
Expand Down Expand Up @@ -391,9 +382,9 @@ mod bpf_tests {

let monitored_path = env!("CARGO_MANIFEST_DIR");
let monitored_path = PathBuf::from(monitored_path);
let paths = vec![PathBuf::from(format!("{}/**/*", monitored_path.display()))];
let paths = [PathBuf::from(format!("{}/**/*", monitored_path.display()))];
let mut config = FactConfig::default();
config.set_paths(paths);
config.set_paths(&paths);
let bpf_config = config.bpf.clone();
let reloader = Reloader::from(config);
let metrics = Metrics::new();
Expand Down
Loading
Loading