windows: the kill switch holds to what is wanted on, and says what is in place
From the security review of 32ff264:
- Turning a system-wide AmneziaWG profile off lifted the whole kill switch,
the TUN's protection too, through a request of the AmneziaWG scope. Now
the profile only leaves the plan; lifting it all stays killswitch off
(services scope).
- Which profiles count came from their tunnels' state this moment (one that
crashed fell out: fail open), and one whose addresses did not read made
the plan none. Now from intent (started at boot, or turned on), and one
unread still holds, letting nothing through it.
- holding said what was meant, not what was in place; failed now says
filters that did not go in.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
32ff264c81
commit
aa44527082
5 files changed
+70
-22
No files matched your search
@@ -63,11 +63,15 @@ const LOCAL: [&str; 13] = [
|
||||
"0.0.0.0/32",
|
||||
];
|
||||
|
||||
/// What the tunnels are, for the plan.
|
||||
/// What the tunnels are, for the plan: what is wanted on, whatever runs this moment (a tunnel
|
||||
/// that failed or restarts is still held to).
|
||||
pub struct Tunnels {
|
||||
/// The TUN is configured, and wanted on now.
|
||||
pub tun: bool,
|
||||
/// The system-wide AmneziaWG profiles' interface addresses (their `Address` lines).
|
||||
/// A system-wide AmneziaWG profile is wanted on (turned on, or started at boot).
|
||||
pub awg_on: bool,
|
||||
/// Those profiles' interface addresses (their `Address` lines); none read still holds,
|
||||
/// with nothing let out through them.
|
||||
pub awg: Vec<Net>,
|
||||
}
|
||||
|
||||
@@ -75,7 +79,7 @@ pub struct Tunnels {
|
||||
/// or no global tunnel to protect).
|
||||
pub fn plan(cfg: &Config, tunnels: &Tunnels, lifted: bool, programs: Vec<PathBuf>) -> Option<Plan> {
|
||||
let k = &cfg.kill_switch;
|
||||
if !cfg.enable || !k.enable || lifted || (!tunnels.tun && tunnels.awg.is_empty()) {
|
||||
if !cfg.enable || !k.enable || lifted || (!tunnels.tun && !tunnels.awg_on) {
|
||||
return None;
|
||||
}
|
||||
let mut open: Vec<Net> = LOCAL.iter().filter_map(|n| Net::parse(n)).collect();
|
||||
@@ -105,17 +109,27 @@ pub fn awg_addresses(conf: &str) -> Vec<Net> {
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
pub use wfp::apply;
|
||||
pub use wfp::{apply, installed};
|
||||
|
||||
/// Off Windows there is nothing to filter with: the plan is kept, for the status, and said.
|
||||
#[cfg(not(windows))]
|
||||
static APPLIED: std::sync::Mutex<Option<Plan>> = std::sync::Mutex::new(None);
|
||||
|
||||
/// Off Windows there is nothing to filter with: said, and left.
|
||||
#[cfg(not(windows))]
|
||||
pub fn apply(plan: Option<&Plan>) -> anyhow::Result<()> {
|
||||
if let Some(p) = plan {
|
||||
tracing::debug!("kill switch (not on this system): {} open ranges, {} tunnel ranges, {} programs", p.open.len(), p.tunnels.len(), p.programs.len());
|
||||
}
|
||||
*APPLIED.lock().unwrap_or_else(|e| e.into_inner()) = plan.cloned();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The plan whose filters are in place.
|
||||
#[cfg(not(windows))]
|
||||
pub fn installed() -> Option<Plan> {
|
||||
APPLIED.lock().unwrap_or_else(|e| e.into_inner()).clone()
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
mod wfp {
|
||||
use std::sync::Mutex;
|
||||
@@ -337,6 +351,11 @@ mod wfp {
|
||||
result.map(|()| engine)
|
||||
}
|
||||
|
||||
/// The plan whose filters are in place.
|
||||
pub fn installed() -> Option<Plan> {
|
||||
ACTIVE.lock().unwrap_or_else(|e| e.into_inner()).as_ref().map(|(p, _, _)| p.clone())
|
||||
}
|
||||
|
||||
/// The filters for `plan` in place of the ones there (none: lifted). A plan already in
|
||||
/// place is left as it is; a new one is put in before the old goes, so nothing leaks
|
||||
/// between them. One that cannot be put in leaves the old one holding.
|
||||
@@ -382,22 +401,25 @@ mod tests {
|
||||
#[test]
|
||||
fn it_holds_only_with_a_tunnel_to_protect_and_not_lifted() {
|
||||
let c = cfg();
|
||||
let tun = Tunnels { tun: true, awg: Vec::new() };
|
||||
let none = Tunnels { tun: false, awg: Vec::new() };
|
||||
let tun = Tunnels { tun: true, awg_on: false, awg: Vec::new() };
|
||||
let none = Tunnels { tun: false, awg_on: false, awg: Vec::new() };
|
||||
assert!(plan(&c, &tun, false, Vec::new()).is_some());
|
||||
assert_eq!(plan(&c, &tun, true, Vec::new()), None, "lifted");
|
||||
assert_eq!(plan(&c, &none, false, Vec::new()), None, "no tunnel");
|
||||
let mut off = c.clone();
|
||||
off.kill_switch.enable = false;
|
||||
assert_eq!(plan(&off, &tun, false, Vec::new()), None);
|
||||
let awg = Tunnels { tun: false, awg: awg_addresses("[Interface]\nAddress = 10.8.0.2/24, fd00:8::2/64\n") };
|
||||
let awg = Tunnels { tun: false, awg_on: true, awg: awg_addresses("[Interface]\nAddress = 10.8.0.2/24, fd00:8::2/64\n") };
|
||||
let p = plan(&c, &awg, false, Vec::new()).unwrap();
|
||||
assert_eq!(p.tunnels, [Net::parse("10.8.0.2/32").unwrap(), Net::parse("fd00:8::2/128").unwrap()]);
|
||||
// A profile wanted on whose addresses do not read still holds, letting nothing through it.
|
||||
let unread = Tunnels { tun: false, awg_on: true, awg: Vec::new() };
|
||||
assert!(plan(&c, &unread, false, Vec::new()).is_some_and(|p| p.tunnels.is_empty()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_plan_opens_the_local_networks_and_the_tuns_addresses() {
|
||||
let p = plan(&cfg(), &Tunnels { tun: true, awg: Vec::new() }, false, vec![PathBuf::from(r"C:\sing-box.exe")]).unwrap();
|
||||
let p = plan(&cfg(), &Tunnels { tun: true, awg_on: false, awg: Vec::new() }, false, vec![PathBuf::from(r"C:\sing-box.exe")]).unwrap();
|
||||
for n in ["192.168.0.0/16", "fe80::/10", "198.51.100.0/24", "255.255.255.255/32"] {
|
||||
assert!(p.open.contains(&Net::parse(n).unwrap()), "{n}");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user