use crate::error::IronError;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ParamCategory {
Network,
Memory,
Security,
FileSystem,
Kernel,
Vm,
Debug,
Unknown,
}
impl std::fmt::Display for ParamCategory {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Network => write!(f, "Network"),
Self::Memory => write!(f, "Memory"),
Self::Security => write!(f, "Security"),
Self::FileSystem => write!(f, "Filesystem"),
Self::Kernel => write!(f, "Kernel"),
Self::Vm => write!(f, "VM/Memory"),
Self::Debug => write!(f, "Debug"),
Self::Unknown => write!(f, "Unknown"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KernelParam {
pub name: String,
pub value: Option<String>,
pub category: ParamCategory,
pub is_recommended: Option<bool>,
pub recommended: Option<String>,
pub description: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KernelParamsReport {
pub params: Vec<KernelParam>,
pub security_params: Vec<KernelParam>,
pub network_params: Vec<KernelParam>,
pub vm_params: Vec<KernelParam>,
pub non_recommended_count: u32,
pub security_score: Option<u32>,
pub network_score: Option<u32>,
pub recommendations: Vec<String>,
}
pub struct KernelParamsMonitor {
report: KernelParamsReport,
}
#[cfg(target_os = "windows")]
fn agreed_tcp_level(cmdlet_output: &str) -> Option<String> {
let levels: Vec<&str> = cmdlet_output
.lines()
.map(str::trim)
.filter(|l| !l.is_empty())
.collect();
let first = levels.first()?;
levels
.iter()
.all(|l| l.eq_ignore_ascii_case(first))
.then(|| (*first).to_string())
}
impl KernelParamsMonitor {
pub fn new() -> Result<Self, IronError> {
let report = Self::analyze()?;
Ok(Self { report })
}
pub fn refresh(&mut self) -> Result<(), IronError> {
self.report = Self::analyze()?;
Ok(())
}
pub fn report(&self) -> &KernelParamsReport {
&self.report
}
pub fn param(&self, name: &str) -> Option<&KernelParam> {
self.report.params.iter().find(|p| p.name == name)
}
fn analyze() -> Result<KernelParamsReport, IronError> {
let params = Self::read_params();
let security_params: Vec<KernelParam> = params
.iter()
.filter(|p| p.category == ParamCategory::Security)
.cloned()
.collect();
let network_params: Vec<KernelParam> = params
.iter()
.filter(|p| p.category == ParamCategory::Network)
.cloned()
.collect();
let vm_params: Vec<KernelParam> = params
.iter()
.filter(|p| p.category == ParamCategory::Vm || p.category == ParamCategory::Memory)
.cloned()
.collect();
let non_recommended = params
.iter()
.filter(|p| p.is_recommended == Some(false) && p.recommended.is_some())
.count() as u32;
let security_score = Self::compute_security_score(&security_params);
let network_score = Self::compute_network_score(&network_params);
let mut recommendations = Vec::new();
for p in ¶ms {
if p.is_recommended == Some(false) {
if let (Some(ref value), Some(ref rec)) = (&p.value, &p.recommended) {
recommendations.push(format!(
"{}: current={}, recommended={}",
p.name, value, rec
));
}
}
}
Ok(KernelParamsReport {
params,
security_params,
network_params,
vm_params,
non_recommended_count: non_recommended,
security_score,
network_score,
recommendations,
})
}
fn compute_security_score(params: &[KernelParam]) -> Option<u32> {
Self::score_against_recommended(params)
}
fn compute_network_score(params: &[KernelParam]) -> Option<u32> {
Self::score_against_recommended(params)
}
fn score_against_recommended(params: &[KernelParam]) -> Option<u32> {
let judged: Vec<bool> = params
.iter()
.filter(|p| p.recommended.is_some())
.filter_map(|p| p.is_recommended)
.collect();
if judged.is_empty() {
return None;
}
let good = judged.iter().filter(|&&matches| matches).count();
Some(((good as f64 / judged.len() as f64) * 100.0) as u32)
}
#[cfg(target_os = "linux")]
fn read_params() -> Vec<KernelParam> {
let checks: Vec<(&str, &str, ParamCategory, &str, &str)> = vec![
(
"kernel.randomize_va_space",
"2",
ParamCategory::Security,
"ASLR level (0=off, 1=stack, 2=full)",
"2",
),
(
"kernel.kptr_restrict",
"1",
ParamCategory::Security,
"Restrict kernel pointer exposure",
"1",
),
(
"kernel.dmesg_restrict",
"1",
ParamCategory::Security,
"Restrict dmesg to privileged users",
"1",
),
(
"kernel.yama.ptrace_scope",
"1",
ParamCategory::Security,
"Ptrace scope restriction",
"1",
),
(
"kernel.unprivileged_bpf_disabled",
"1",
ParamCategory::Security,
"Disable unprivileged BPF",
"1",
),
(
"kernel.kexec_load_disabled",
"1",
ParamCategory::Security,
"Disable kexec_load",
"1",
),
(
"kernel.sysrq",
"0",
ParamCategory::Security,
"Magic SysRq key (0=disable)",
"0",
),
(
"kernel.core_uses_pid",
"1",
ParamCategory::Security,
"Core dumps include PID",
"1",
),
(
"kernel.modules_disabled",
"0",
ParamCategory::Security,
"Disable module loading (0=allow)",
"0",
),
(
"net.ipv4.tcp_syncookies",
"1",
ParamCategory::Network,
"SYN flood protection",
"1",
),
(
"net.ipv4.conf.all.rp_filter",
"1",
ParamCategory::Network,
"Reverse path filtering",
"1",
),
(
"net.ipv4.conf.all.accept_redirects",
"0",
ParamCategory::Network,
"ICMP redirect acceptance",
"0",
),
(
"net.ipv4.conf.all.send_redirects",
"0",
ParamCategory::Network,
"ICMP redirect sending",
"0",
),
(
"net.ipv4.conf.all.accept_source_route",
"0",
ParamCategory::Network,
"Source routing",
"0",
),
(
"net.ipv4.conf.all.log_martians",
"1",
ParamCategory::Network,
"Log martian packets",
"1",
),
(
"net.ipv4.tcp_max_syn_backlog",
"4096",
ParamCategory::Network,
"Max SYN backlog",
"4096",
),
(
"net.core.somaxconn",
"4096",
ParamCategory::Network,
"Max socket backlog",
"4096",
),
(
"net.ipv4.tcp_tw_reuse",
"1",
ParamCategory::Network,
"TIME_WAIT socket reuse",
"1",
),
(
"net.ipv4.tcp_fin_timeout",
"15",
ParamCategory::Network,
"FIN timeout seconds",
"15",
),
(
"net.core.rmem_max",
"16777216",
ParamCategory::Network,
"Max receive buffer",
"16777216",
),
(
"net.core.wmem_max",
"16777216",
ParamCategory::Network,
"Max send buffer",
"16777216",
),
(
"vm.swappiness",
"60",
ParamCategory::Vm,
"Swappiness (0-200)",
"60",
),
(
"vm.dirty_ratio",
"20",
ParamCategory::Vm,
"Dirty page ratio (% of RAM)",
"20",
),
(
"vm.dirty_background_ratio",
"10",
ParamCategory::Vm,
"Background dirty ratio",
"10",
),
(
"vm.overcommit_memory",
"0",
ParamCategory::Vm,
"Memory overcommit (0=heuristic)",
"0",
),
(
"vm.max_map_count",
"65530",
ParamCategory::Vm,
"Max memory mappings",
"65530",
),
(
"vm.vfs_cache_pressure",
"100",
ParamCategory::Vm,
"VFS cache pressure",
"100",
),
(
"vm.min_free_kbytes",
"65536",
ParamCategory::Vm,
"Minimum free memory (kB)",
"65536",
),
(
"fs.file-max",
"1048576",
ParamCategory::FileSystem,
"Max open files system-wide",
"1048576",
),
(
"fs.inotify.max_user_watches",
"524288",
ParamCategory::FileSystem,
"Max inotify watches per user",
"524288",
),
];
let mut params = Vec::new();
for (name, _default, category, description, recommended) in &checks {
let sysctl_path = format!("/proc/sys/{}", name.replace('.', "/"));
let value = std::fs::read_to_string(&sysctl_path)
.ok()
.map(|s| s.trim().to_string());
let is_recommended = value
.as_deref()
.map(|v| Self::check_recommended(v, recommended));
params.push(KernelParam {
name: name.to_string(),
value,
category: *category,
is_recommended,
recommended: Some(recommended.to_string()),
description: description.to_string(),
});
}
params
}
#[cfg(target_os = "linux")]
fn check_recommended(value: &str, recommended: &str) -> bool {
if let (Ok(v), Ok(r)) = (value.parse::<i64>(), recommended.parse::<i64>()) {
return v >= r; }
value == recommended
}
#[cfg(target_os = "windows")]
fn read_params() -> Vec<KernelParam> {
let mut params = Vec::new();
let output = std::process::Command::new("powershell")
.args([
"-NoProfile",
"-Command",
"Get-NetTCPSetting | ForEach-Object { $_.AutoTuningLevelLocal }",
])
.output();
if let Ok(out) = output {
if let Some(val) = agreed_tcp_level(&String::from_utf8_lossy(&out.stdout)) {
params.push(KernelParam {
name: "net.tcp.autotuninglevel".into(),
value: Some(val.clone()),
category: ParamCategory::Network,
is_recommended: Some(val.to_lowercase() == "normal"),
recommended: Some("Normal".into()),
description: "TCP auto-tuning level, agreed by every template that states one"
.into(),
});
}
}
params
}
#[cfg(target_os = "macos")]
fn read_params() -> Vec<KernelParam> {
let checks = vec![
(
"kern.maxfiles",
ParamCategory::FileSystem,
"Max open files",
"49152",
),
(
"kern.maxfilesperproc",
ParamCategory::FileSystem,
"Max files per process",
"24576",
),
(
"net.inet.tcp.msl",
ParamCategory::Network,
"TCP MSL (milliseconds)",
"15000",
),
(
"net.inet.tcp.delayed_ack",
ParamCategory::Network,
"Delayed ACK",
"0",
),
("vm.swapusage", ParamCategory::Vm, "Swap usage", ""),
];
let mut params = Vec::new();
for (name, category, description, recommended) in &checks {
let output = std::process::Command::new("sysctl")
.arg("-n")
.arg(name)
.output();
let value = output
.ok()
.filter(|o| o.status.success())
.map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
.filter(|s| !s.is_empty());
let is_recommended = value
.as_deref()
.filter(|_| !recommended.is_empty())
.map(|v| {
if let (Ok(v_num), Ok(r)) = (v.parse::<i64>(), recommended.parse::<i64>()) {
v_num >= r
} else {
v == *recommended
}
});
params.push(KernelParam {
name: name.to_string(),
value,
category: *category,
is_recommended,
recommended: if recommended.is_empty() {
None
} else {
Some(recommended.to_string())
},
description: description.to_string(),
});
}
params
}
#[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))]
fn read_params() -> Vec<KernelParam> {
Vec::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_category_display() {
assert_eq!(ParamCategory::Network.to_string(), "Network");
assert_eq!(ParamCategory::Security.to_string(), "Security");
assert_eq!(ParamCategory::Vm.to_string(), "VM/Memory");
}
#[test]
fn test_security_scoring() {
let params = vec![
KernelParam {
name: "a".into(),
value: Some("2".into()),
category: ParamCategory::Security,
is_recommended: Some(true),
recommended: Some("2".into()),
description: "".into(),
},
KernelParam {
name: "b".into(),
value: Some("0".into()),
category: ParamCategory::Security,
is_recommended: Some(false),
recommended: Some("1".into()),
description: "".into(),
},
];
let score = KernelParamsMonitor::compute_security_score(¶ms);
assert_eq!(score, Some(50));
}
fn judged(name: &str, value: Option<&str>, is_recommended: Option<bool>) -> KernelParam {
KernelParam {
name: name.into(),
value: value.map(String::from),
category: ParamCategory::Security,
is_recommended,
recommended: Some("1".into()),
description: "".into(),
}
}
#[test]
fn nothing_to_score_is_no_score_not_fifty() {
assert_eq!(KernelParamsMonitor::compute_security_score(&[]), None);
let unread = [judged("a", None, None)];
assert_eq!(KernelParamsMonitor::compute_security_score(&unread), None);
}
#[test]
fn unread_parameters_do_not_move_the_score() {
let params = [judged("a", Some("1"), Some(true)), judged("b", None, None)];
assert_eq!(
KernelParamsMonitor::compute_security_score(¶ms),
Some(100)
);
}
#[test]
fn a_parameter_with_no_recommendation_is_not_judged() {
let mut no_rec = judged("a", Some("0"), Some(false));
no_rec.recommended = None;
assert_eq!(KernelParamsMonitor::compute_network_score(&[no_rec]), None);
}
#[test]
fn test_perfect_score() {
let params = vec![
KernelParam {
name: "a".into(),
value: Some("1".into()),
category: ParamCategory::Security,
is_recommended: Some(true),
recommended: Some("1".into()),
description: "".into(),
},
KernelParam {
name: "b".into(),
value: Some("1".into()),
category: ParamCategory::Security,
is_recommended: Some(true),
recommended: Some("1".into()),
description: "".into(),
},
];
let score = KernelParamsMonitor::compute_security_score(¶ms);
assert_eq!(score, Some(100));
}
#[test]
fn test_serialization() {
let param = KernelParam {
name: "net.ipv4.tcp_syncookies".into(),
value: Some("1".into()),
category: ParamCategory::Network,
is_recommended: Some(true),
recommended: Some("1".into()),
description: "SYN cookies".into(),
};
let json = serde_json::to_string(¶m).unwrap();
assert!(json.contains("tcp_syncookies"));
let _: KernelParam = serde_json::from_str(&json).unwrap();
}
}
#[cfg(all(test, target_os = "windows"))]
mod windows_tcp_tests {
use super::agreed_tcp_level;
#[test]
fn the_empty_first_template_does_not_hide_the_six_that_answer() {
let out = "\nNormal\nNormal\nNormal\nNormal\nNormal\nNormal\n";
assert_eq!(agreed_tcp_level(out).as_deref(), Some("Normal"));
}
#[test]
fn templates_that_disagree_state_nothing_machine_wide() {
assert_eq!(agreed_tcp_level("Normal\nDisabled\n"), None);
}
#[test]
fn no_output_is_no_reading() {
assert_eq!(agreed_tcp_level(""), None);
assert_eq!(agreed_tcp_level("\n \n"), None);
}
}