From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 660671FF0BC for ; Thu, 15 Oct 2026 14:44:50 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 20EA021533; Thu, 15 Oct 2026 14:44:40 +0200 (CEST) MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Subject: Re: [PATCH proxmox-firewall v2 10/12] firewall: dump config to local directory after apply From: Stefan Hanreich To: Arthur Bied-Charreton In-Reply-To: <20261001122625.348730-11-a.bied-charreton@proxmox.com> References: <20261001122625.348730-1-a.bied-charreton@proxmox.com> <20261001122625.348730-11-a.bied-charreton@proxmox.com> Date: Thu, 15 Oct 2026 14:44:27 +0200 Message-Id: <179206826792.200327.730453550222014690.b4-review@b4> X-Mailer: b4 0.16-dev-fbf84 X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1792068275619 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.545 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: 3RP33AYSCD76H3OVTOW2QZJXUPDF7VOR X-Message-ID-Hash: 3RP33AYSCD76H3OVTOW2QZJXUPDF7VOR X-MailFrom: s.hanreich@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: pve-devel@lists.proxmox.com X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: It feels as though it might make sense to move the cache handling into its own struct instead of using a HashMap here that gets passed around? > The firewall configuration files are stored on pmxcfs, which is only > available when pve-cluster is up. To be able to restore firewall rules > before the network comes up and close the boot-time window in which the > PVE host is unprotected, dump the required configuration files to a > local directory after every apply. > > The last dumped version is cached for each file, the daemon only > actually writes to disk if there has been a change since the last write > or the dump file does not exist. > > Ownership of the dumps is keyed on the nftables option in the host > config. When the firewall is disabled, the dumps are only removed if > nftables is enabled - otherwise that responsibility is on pve-firewall. > > The dumps are also removed when the daemon is stopped, to prevent a > ruleset restored at boot from living forever if no daemon starts > afterwards. > > Signed-off-by: Arthur Bied-Charreton > Reviewed-by: Thomas Ellmenreich > Tested-by: Thomas Ellmenreich > > diff --git a/debian/dirs b/debian/dirs > new file mode 100644 > index 0000000..34355af > --- /dev/null > +++ b/debian/dirs > @@ -0,0 +1 @@ > +/var/lib/pve/firewall > diff --git a/proxmox-firewall/Cargo.toml b/proxmox-firewall/Cargo.toml > index fa7fd34..77ca285 100644 > --- a/proxmox-firewall/Cargo.toml > +++ b/proxmox-firewall/Cargo.toml > @@ -22,9 +22,9 @@ proxmox-log.workspace = true > proxmox-network-types.workspace = true > proxmox-network-api = { workspace = true, features = [ "impl" ] } > proxmox-nftables = { workspace = true, features = [ "config-ext" ] } > +proxmox-sys.workspace = true > proxmox-systemd.workspace = true > proxmox-ve-config.workspace = true > > [dev-dependencies] > insta = { workspace = true, features = [ "json" ] } > -proxmox-sys.workspace = true > diff --git a/proxmox-firewall/src/bin/proxmox-firewall.rs b/proxmox-firewall/src/bin/proxmox-firewall.rs > index 27fd67d..1ac7c6c 100644 > --- a/proxmox-firewall/src/bin/proxmox-firewall.rs > +++ b/proxmox-firewall/src/bin/proxmox-firewall.rs > @@ -1,3 +1,6 @@ > +use std::collections::HashMap; > +use std::os::unix::fs::PermissionsExt; > +use std::path::Path; > use std::sync::Arc; > use std::sync::atomic::{AtomicBool, Ordering}; > use std::time::{Duration, Instant}; > @@ -5,11 +8,14 @@ use std::time::{Duration, Instant}; > use anyhow::{Context, Error, bail, format_err}; > use pico_args::Arguments; > > -use proxmox_firewall::config::{FirewallConfig, PveFirewallConfigLoader, PveNftConfigLoader}; > +use proxmox_firewall::config::{ > + DUMP_DIR, FirewallConfig, PveFirewallConfigLoader, PveNftConfigLoader, > +}; > use proxmox_firewall::firewall::Firewall; > use proxmox_log as log; > use proxmox_log::{LevelFilter, Logger}; > use proxmox_nftables::{NftClient, client::NftError}; > +use proxmox_sys::fs; > use proxmox_systemd::systemctl; > use proxmox_ve_config::firewall::host::Config as HostConfig; > > @@ -42,14 +48,58 @@ fn remove_firewall() -> Result<(), std::io::Error> { > Ok(()) > } > > +fn dump_config(cfg: &FirewallConfig, cache: &mut HashMap<&str, Vec>) { > + let dump_dir = Path::new(DUMP_DIR); > + if !dump_dir.exists() > + && let Err(e) = fs::create_path(dump_dir, None, None) > + { > + log::error!("could not create {DUMP_DIR}: {e} - config will not be dumped"); > + return; > + } > + > + if let Err(e) = std::fs::set_permissions(dump_dir, std::fs::Permissions::from_mode(0o700)) { > + log::warn!("could not set permissions on {DUMP_DIR}: {e}"); > + } Would make sense to set the permissions on creation here as well instead of doing it every update cycle. > + > + for (bytes, name) in [ > + (cfg.cluster_raw(), "cluster.fw"), > + (cfg.host_raw(), "host.fw"), > + (cfg.sdn_raw(), "sdn.json"), > + ] { > + let out = dump_dir.join(name); > + match bytes { > + Some(b) if !out.exists() || b != cache.get(name).map(Vec::as_slice).unwrap_or(&[]) => { > + match fs::replace_file(&out, b, fs::CreateOptions::new(), true) { > + Err(e) => log::error!("could not dump {name} to {out:?}: {e}"), > + Ok(()) => { > + log::info!("successfully dumped {out:?}"); > + cache.insert(name, b.to_vec()); > + } > + } > + } > + Some(_) => log::debug!("no changes to {name} since last dump to {out:?}"), > + None => { > + log::debug!("nothing to dump for {name}"); > + cache.remove(name); > + _ = std::fs::remove_file(&out); at least log the error here if unsuccessful? > + } > + } > + } > +} > + > fn create_firewall_instance() -> Result { > let config = FirewallConfig::new(&PveFirewallConfigLoader::new(), &PveNftConfigLoader::new())?; > Ok(Firewall::new(config)) > } > > -fn handle_firewall() -> Result<(), Error> { > - let firewall = create_firewall_instance()?; > +fn delete_dumps(cache: &mut HashMap<&str, Vec>) { > + cache.clear(); > + for f in ["cluster.fw", "host.fw", "sdn.json"] { > + _ = std::fs::remove_file(Path::new(DUMP_DIR).join(f)); same w.r.t. logging errors here as well... -- Stefan Hanreich