#[cfg(target_os = "linux")]
use super::{normalize, output};
#[cfg(target_os = "linux")]
use assay_common::MonitorEvent;
use assay_common::{EVENT_EXEC, EVENT_OPENAT};
use assay_core::mcp::runtime_features::{
MonitorAction, MonitorRule, MonitorRuleType, RuntimeMonitorConfig,
};
#[derive(Debug)]
pub(crate) struct ActiveRule {
pub(crate) id: String,
pub(crate) action: MonitorAction,
pub(crate) event_type: u32,
pub(crate) allow: globset::GlobSet,
pub(crate) deny: Option<globset::GlobSet>,
}
pub(crate) fn compile_globset(globs: &[String]) -> anyhow::Result<globset::GlobSet> {
let mut b = globset::GlobSetBuilder::new();
for g in globs {
b.add(globset::Glob::new(g)?);
}
Ok(b.build()?)
}
fn plan_rule(rule: &MonitorRule, kill_switch_enabled: bool) -> Result<Option<ActiveRule>, String> {
let matcher = &rule.match_config;
let event_type = match rule.rule_type {
MonitorRuleType::FileOpen => EVENT_OPENAT,
MonitorRuleType::ProcExec => {
if rule.action == MonitorAction::Deny {
return Err(
"proc_exec cannot be denied: the monitor sees an exec once the new \
program has started. Use trigger_kill, alert or log"
.to_string(),
);
}
if rule.action == MonitorAction::TriggerKill && !kill_switch_enabled {
return Err(
"trigger_kill on proc_exec needs an enabled kill_switch: without one the \
monitor would only log the exec. Enable kill_switch, or use alert or log"
.to_string(),
);
}
EVENT_EXEC
}
MonitorRuleType::NetConnect => {
if !matches!(
rule.action,
MonitorAction::Deny | MonitorAction::TriggerKill
) {
return Err(
"net_connect rules are installed in the kernel as deny rules; \
action log or alert has no effect"
.to_string(),
);
}
if matcher.dest_globs.is_empty() {
return Err("has no dest_globs, so it can never match".to_string());
}
return Ok(None);
}
};
if matcher.path_globs.is_empty() {
return Err("has no path_globs, so it can never match".to_string());
}
let allow = compile_globset(&matcher.path_globs)
.map_err(|error| format!("path_globs do not compile: {error}"))?;
let deny = matcher
.not
.as_ref()
.map(|not| compile_globset(¬.path_globs))
.transpose()
.map_err(|error| format!("not.path_globs do not compile: {error}"))?;
Ok(Some(ActiveRule {
id: rule.id.clone(),
action: rule.action.clone(),
event_type,
allow,
deny,
}))
}
pub(crate) fn compile_active_rules(
runtime_config: Option<&RuntimeMonitorConfig>,
kill_switch_enabled: bool,
) -> Vec<ActiveRule> {
runtime_config
.map(|cfg| {
cfg.rules
.iter()
.filter_map(|rule| plan_rule(rule, kill_switch_enabled).ok().flatten())
.collect()
})
.unwrap_or_default()
}
pub(crate) fn unenforceable_rules(
cfg: &RuntimeMonitorConfig,
kill_switch_enabled: bool,
) -> Vec<String> {
cfg.rules
.iter()
.filter_map(|rule| {
plan_rule(rule, kill_switch_enabled)
.err()
.map(|reason| format!("rule '{}': {reason}", rule.id))
})
.collect()
}
pub(crate) fn kill_switch_enabled(
kill_switch: Option<&assay_core::mcp::runtime_features::KillSwitchConfig>,
) -> bool {
kill_switch.is_some_and(|config| config.enabled)
}
pub(crate) fn matching_rule<'a>(
event_type: u32,
path: &str,
rules: &'a [ActiveRule],
) -> Option<&'a ActiveRule> {
rules.iter().find(|rule| {
rule.event_type == event_type
&& rule.allow.is_match(path)
&& !rule.deny.as_ref().is_some_and(|deny| deny.is_match(path))
})
}
#[cfg(target_os = "linux")]
pub(crate) fn find_violation_rule<'a>(
event: &MonitorEvent,
rules: &'a [ActiveRule],
) -> Option<&'a ActiveRule> {
if rules.is_empty() || !matches!(event.event_type, EVENT_OPENAT | EVENT_EXEC) {
return None;
}
let raw = output::decode_utf8_cstr(&event.data);
let path = normalize::normalize_path_syntactic(&raw);
matching_rule(event.event_type, &path, rules)
}
#[cfg(test)]
mod tests {
use super::*;
use assay_common::{EVENT_CONNECT, EVENT_EXEC, EVENT_OPENAT};
use assay_core::mcp::runtime_features::RuntimeMonitorConfig;
fn config(rules_yaml: &str) -> RuntimeMonitorConfig {
serde_yaml::from_str(&format!("enabled: true\nrules:\n{rules_yaml}"))
.expect("monitor config")
}
const EXEC_KILL: &str = " - id: no-curl\n type: proc_exec\n match:\n path_globs: ['**/curl']\n not:\n path_globs: ['/opt/safe/**']\n action: trigger_kill\n";
const KILL_SWITCH_ON: bool = true;
const KILL_SWITCH_OFF: bool = false;
const FILE_DENY: &str = " - id: no-secrets\n type: file_open\n match:\n path_globs: ['/etc/secret/**']\n action: deny\n";
#[test]
fn a_proc_exec_rule_matches_the_exec_it_names_and_only_exec_events() {
let cfg = config(&format!("{EXEC_KILL}{FILE_DENY}"));
assert_eq!(
unenforceable_rules(&cfg, KILL_SWITCH_ON),
Vec::<String>::new()
);
let rules = compile_active_rules(Some(&cfg), KILL_SWITCH_ON);
let hit = matching_rule(EVENT_EXEC, "/usr/bin/curl", &rules).expect("exec of curl matches");
assert_eq!(hit.id, "no-curl");
assert!(matching_rule(EVENT_EXEC, "/usr/bin/true", &rules).is_none());
assert!(matching_rule(EVENT_EXEC, "curl", &rules).is_some());
assert!(matching_rule(EVENT_EXEC, "/opt/safe/curl", &rules).is_none());
assert!(matching_rule(EVENT_OPENAT, "/usr/bin/curl", &rules).is_none());
assert!(matching_rule(EVENT_EXEC, "/etc/secret/key", &rules).is_none());
assert_eq!(
matching_rule(EVENT_OPENAT, "/etc/secret/key", &rules).map(|r| r.id.as_str()),
Some("no-secrets")
);
assert!(matching_rule(EVENT_CONNECT, "/usr/bin/curl", &rules).is_none());
}
#[test]
fn a_rule_the_monitor_cannot_act_on_is_named_at_load() {
let cases = [
(
" - id: exec-deny\n type: proc_exec\n match:\n path_globs: ['**/curl']\n action: deny\n",
"rule 'exec-deny': proc_exec cannot be denied",
),
(
" - id: net-log\n type: net_connect\n match:\n dest_globs: ['10.0.0.0/8']\n action: log\n",
"rule 'net-log': net_connect",
),
(
" - id: exec-empty\n type: proc_exec\n match: {}\n action: alert\n",
"rule 'exec-empty': has no path_globs",
),
(
" - id: file-empty\n type: file_open\n match:\n dest_globs: ['10.0.0.0/8']\n action: deny\n",
"rule 'file-empty': has no path_globs",
),
(
" - id: net-empty\n type: net_connect\n match:\n path_globs: ['/x']\n action: deny\n",
"rule 'net-empty': has no dest_globs",
),
(
" - id: bad-glob\n type: file_open\n match:\n path_globs: ['/etc/[']\n action: log\n",
"rule 'bad-glob': path_globs do not compile",
),
(
" - id: bad-not\n type: file_open\n match:\n path_globs: ['/etc/**']\n not:\n path_globs: ['/etc/[']\n action: trigger_kill\n",
"rule 'bad-not': not.path_globs do not compile",
),
];
for (yaml, want) in cases {
let refused = unenforceable_rules(&config(yaml), KILL_SWITCH_ON);
assert_eq!(refused.len(), 1, "{yaml} -> {refused:?}");
assert!(
refused[0].starts_with(want),
"{:?} does not start with {want:?}",
refused[0]
);
assert!(compile_active_rules(Some(&config(yaml)), KILL_SWITCH_ON).is_empty());
}
for yaml in [
EXEC_KILL,
FILE_DENY,
" - id: exec-log\n type: proc_exec\n match:\n path_globs: ['**/sh']\n action: log\n",
" - id: net-deny\n type: net_connect\n match:\n dest_globs: ['10.0.0.0/8']\n action: deny\n",
] {
assert_eq!(unenforceable_rules(&config(yaml), KILL_SWITCH_ON), Vec::<String>::new(), "{yaml}");
}
}
#[test]
fn a_proc_exec_kill_rule_needs_the_kill_switch() {
let refused = unenforceable_rules(&config(EXEC_KILL), KILL_SWITCH_OFF);
assert_eq!(refused.len(), 1, "{refused:?}");
assert!(
refused[0].starts_with(
"rule 'no-curl': trigger_kill on proc_exec needs an enabled kill_switch"
),
"{:?}",
refused[0]
);
assert!(compile_active_rules(Some(&config(EXEC_KILL)), KILL_SWITCH_OFF).is_empty());
let file_kill = " - id: f\n type: file_open\n match:\n path_globs: ['/etc/secret/**']\n action: trigger_kill\n";
let exec_alert = " - id: e\n type: proc_exec\n match:\n path_globs: ['**/sh']\n action: alert\n";
for yaml in [file_kill, exec_alert] {
assert_eq!(
unenforceable_rules(&config(yaml), KILL_SWITCH_OFF),
Vec::<String>::new(),
"{yaml}"
);
}
}
#[test]
fn the_first_matching_rule_in_policy_order_wins() {
let two = " - id: first\n type: proc_exec\n match:\n path_globs: ['**/curl']\n action: log\n - id: second\n type: proc_exec\n match:\n path_globs: ['/usr/**']\n action: trigger_kill\n";
let rules = compile_active_rules(Some(&config(two)), KILL_SWITCH_ON);
assert_eq!(
matching_rule(EVENT_EXEC, "/usr/bin/curl", &rules).map(|r| r.id.as_str()),
Some("first")
);
}
#[test]
fn the_kill_switch_is_enabled_only_when_present_and_not_switched_off() {
let parse = |yaml: &str| {
serde_yaml::from_str::<assay_core::mcp::runtime_features::KillSwitchConfig>(yaml)
.expect("kill switch config")
};
assert!(!kill_switch_enabled(None), "no kill_switch section");
assert!(
!kill_switch_enabled(Some(&parse("enabled: false"))),
"switched off"
);
assert!(
kill_switch_enabled(Some(&parse("enabled: true"))),
"switched on"
);
assert!(
kill_switch_enabled(Some(&parse("{}"))),
"present, default on"
);
let off = kill_switch_enabled(Some(&parse("enabled: false")));
assert_eq!(unenforceable_rules(&config(EXEC_KILL), off).len(), 1);
}
}