← Back to Pull requests

Don't leak sysctl.d drop-ins for internal networks #1498

Closed
hcebke closed this pull request without merging on Dec 02, 2026 15:03 UTC
View on GitHub
hcebke hcebke commented on Aug 09, 2026 20:38 UTC
Author

Fixes: #1497

These changed were generated by Claude Opus and then reviewed, edited, and tested by me.

Diff: 109 lines (+89 / -20) across 3 files Reviewer: Luap99 Review duration: 165264m
All Vouch review confidence checks passed
Residual risk 0.07 = Change Risk (0.39) × (1 − Review Confidence (0.83)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.39 · 109 lines diff across 3 files
0.39
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.70 · 5 substantive comment(s)
0.70
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
83%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.281
Ai Authored ai_authored
0.050
Real PR Conversation Timeline
21 items
Luap99 Luap99 commented on code on Aug 11, 2026 11:34 UTC
Code Review
test/250-bridge-nftables.bats:1
@@ -32,6 +32,43 @@ export NETAVARK_FW=nftables
    run_netavark --file ${TESTSDIR}/testfiles/internal.json teardown $(get_container_netns_path)
 }
 
+@test "$fw_driver - internal network sysctl.d file is removed on teardown" {
+   run_netavark --file ${TESTSDIR}/testfiles/internal.json setup $(get_container_netns_path)
+
+   local sysctl_content="# autogenerated by netavark
+net/ipv4/conf/podman0/forwarding = 0
+net/ipv4/conf/podman0/rp_filter = 2"
+   run_in_host_netns cat /run/sysctl.d/10-netavark-podman0.conf
+   assert "$output" == "$sysctl_content" "sysctl.d file content"
+
+   run_netavark --file ${TESTSDIR}/testfiles/internal.json teardown $(get_container_netns_path)
+
+   # The file must not be leaked. Bridge names are reused so systemd-sysctl would
+   # apply forwarding = 0 to the next, possibly not internal, network with that name.
+   expected_rc=1 run_in_host_netns cat /run/sysctl.d/10-netavark-podman0.conf

this should just be added to an existing internal test

Luap99 Luap99 requested changes on Aug 11, 2026 11:36 UTC
Changes Requested Reviewer

This is mixing way to many things at once, this needs to be split in proper commits with proper reasons why they are needed. Staring with the bug at hand of the wrong internal condition.

I am also not convinced we need the "atomic" write behavior, our writes are small enough. And enabling forwarding on that one bridge should not be needed as we already enable the default forwarding.

hcebke hcebke commented on Aug 12, 2026 14:01 UTC
Author

Fair point! I've split it up into multiple commits. I still think the atomic write is necessary (see my reasoning in the commit message). I removed emitting the forwarding = 1 setting for non-internal networks altogether, since it shouldn't be necessary as long as everything else operates as it should. (It would be kind of nice from a symmetry perspective, though, so maybe consider it. ;-))

Unit tests are green, Hoping the github tests will be too once they have finished running. 🤞

hcebke hcebke commented on Aug 12, 2026 14:04 UTC
Author

I did fat-finger the issue # in one commit message, though. :-(
Will force-push a revised commit message.

hcebke hcebke commented on code on Aug 12, 2026 14:05 UTC
Code Review
test/250-bridge-nftables.bats:1
@@ -32,6 +32,43 @@ export NETAVARK_FW=nftables
    run_netavark --file ${TESTSDIR}/testfiles/internal.json teardown $(get_container_netns_path)
 }
 
+@test "$fw_driver - internal network sysctl.d file is removed on teardown" {
+   run_netavark --file ${TESTSDIR}/testfiles/internal.json setup $(get_container_netns_path)
+
+   local sysctl_content="# autogenerated by netavark
+net/ipv4/conf/podman0/forwarding = 0
+net/ipv4/conf/podman0/rp_filter = 2"
+   run_in_host_netns cat /run/sysctl.d/10-netavark-podman0.conf
+   assert "$output" == "$sysctl_content" "sysctl.d file content"
+
+   run_netavark --file ${TESTSDIR}/testfiles/internal.json teardown $(get_container_netns_path)
+
+   # The file must not be leaked. Bridge names are reused so systemd-sysctl would
+   # apply forwarding = 0 to the next, possibly not internal, network with that name.
+   expected_rc=1 run_in_host_netns cat /run/sysctl.d/10-netavark-podman0.conf

Done

mheon mheon commented on code on Aug 17, 2026 20:31 UTC
Code Review
src/network/bridge.rs:1
@@ -484,16 +484,19 @@ impl<'a> Bridge<'a> {
         };
 
         // 2. Tear down firewall & sysctl FIRST (while routes still exist)
-        if !self.info.network.internal && mode == BridgeMode::Managed {
-            match self.teardown_firewall(complete_teardown, bridge_name.clone()) {
-                Ok(_) => {}
-                Err(err) => {
-                    error_list.push(err);
+        if mode == BridgeMode::Managed {

Is this functionally different from what's already there? It does swap the order of the comparisons, but it could easily still do the two checks on a single line?

Luap99 Luap99 commented on code on Aug 18, 2026 16:51 UTC
Code Review
src/network/bridge.rs:1
@@ -484,16 +484,19 @@ impl<'a> Bridge<'a> {
         };
 
         // 2. Tear down firewall & sysctl FIRST (while routes still exist)
-        if !self.info.network.internal && mode == BridgeMode::Managed {
-            match self.teardown_firewall(complete_teardown, bridge_name.clone()) {
-                Ok(_) => {}
-                Err(err) => {
-                    error_list.push(err);
+        if mode == BridgeMode::Managed {

yes see the second complete_teardown branch inside which is needed to remove the sysctl file

Luap99 Luap99 commented on Aug 18, 2026 16:57 UTC
Reviewer

This needs a rebase, logic wise seem fine now for me

hcebke hcebke commented on code on Aug 18, 2026 18:29 UTC
Code Review
src/network/bridge.rs:1
@@ -484,16 +484,19 @@ impl<'a> Bridge<'a> {
         };
 
         // 2. Tear down firewall & sysctl FIRST (while routes still exist)
-        if !self.info.network.internal && mode == BridgeMode::Managed {
-            match self.teardown_firewall(complete_teardown, bridge_name.clone()) {
-                Ok(_) => {}
-                Err(err) => {
-                    error_list.push(err);
+        if mode == BridgeMode::Managed {

To elaborate on Paul's point: the !self.info.network.internal condition used to guard the entire block, now it's only guarding the teardown_firewall part. The sysctl.d drop-in deletion now happens regardless of the network being internal or not.

hcebke hcebke commented on Aug 18, 2026 18:43 UTC
Author

Rebased. Checks currently in progress.

mheon mheon commented on Aug 18, 2026 18:46 UTC
Contributor

LGTM

hcebke hcebke commented on Aug 23, 2026 09:58 UTC
Author

I rebased once more. If you don't have any more change requests, would you mind merging this?

mheon mheon commented on Aug 26, 2026 16:13 UTC
Contributor

@Luap99 PTAL

hcebke hcebke commented on Sep 19, 2026 23:30 UTC
Author

Rebased once again. I know you guys are busy, so if there is anything I can do to help you with this PR, let me know!

ajoshua2004 ajoshua2004 commented on code on Oct 07, 2026 18:24 UTC
Code Review
src/network/sysctl.rs:1
@@ -104,6 +99,26 @@ impl<'a, P: AsRef<Path>, V: AsRef<str> + 'a> SysctlDWriter<'a, P, V> {
         }
     }
 
+    /// write the sysctl.d(5) file, replacing an already existing file
+    ///
+    /// The content is written to a temporary file in the same directory which is
+    /// then renamed over the target. That way systemd-sysctl can never read a
+    /// partially written file, it either sees the old or the new content.
+    fn write_file_atomic(path: &Path, sysctls: &[(V, &'a str)]) -> Result<()> {

I agree with @Luap99 that atomic isnt needed, the file is written before the bridge exists so a plain fs::write should be enough.

ajoshua2004 ajoshua2004 commented on code on Oct 07, 2026 18:26 UTC
Code Review
test/250-bridge-nftables.bats:1
@@ -29,7 +29,37 @@ export NETAVARK_FW=nftables
 
    run_in_container_netns ping -c 1 10.88.0.1
 
+   local sysctl_content="# autogenerated by netavark
+net/ipv4/conf/podman0/forwarding = 0
+net/ipv4/conf/podman0/rp_filter = 2"
+   run_in_host_netns cat /run/sysctl.d/10-netavark-podman0.conf
+   assert "$output" == "$sysctl_content" "sysctl.d file content"
+
    run_netavark --file ${TESTSDIR}/testfiles/internal.json teardown $(get_container_netns_path)
+
+   # The file must not be leaked. Bridge names are reused so systemd-sysctl would
+   # apply forwarding = 0 to the next, possibly not internal, network with that name.
+   expected_rc=1 run_in_host_netns cat /run/sysctl.d/10-netavark-podman0.conf
+}
+
+@test "$fw_driver - stale sysctl.d file is replaced on setup" {
+   # Simulate a leaked file from an internal network with the same bridge name,
+   # i.e. one written by an older netavark or left behind by a killed process.
+   run_in_host_netns sh -c 'printf "# autogenerated by netavark\nnet/ipv4/conf/podman0/forwarding = 0\nnet/ipv4/conf/podman0/rp_filter = 2\n" > /run/sysctl.d/10-netavark-podman0.conf'
+
+   run_netavark --file ${TESTSDIR}/testfiles/simplebridge.json setup $(get_container_netns_path)
+
+   local sysctl_content="# autogenerated by netavark
+net/ipv4/ip_forward = 1
+net/ipv4/conf/podman0/route_localnet = 1
+net/ipv4/conf/podman0/rp_filter = 2"
+   run_in_host_netns cat /run/sysctl.d/10-netavark-podman0.conf
+   assert "$output" == "$sysctl_content" "stale sysctl.d file content was replaced"
+
+   run_in_host_netns cat /proc/sys/net/ipv4/conf/podman0/forwarding

This passes on main too. Netavark sets ip_forward = 1, which forces podman0/forwarding to 1 regardless of the stale file. Only the file-content assert tests the fix, so I'd drop these lines.

ajoshua2004 ajoshua2004 commented on code on Oct 07, 2026 18:27 UTC
Code Review
src/network/sysctl.rs:1
@@ -85,17 +85,12 @@ impl<'a, P: AsRef<Path>, V: AsRef<str> + 'a> SysctlDWriter<'a, P, V> {
     /// Important one must call commit() otherwise we remove the file again on drop.
     pub fn new(path: Option<P>, sysctls: Vec<(V, &'a str)>) -> Self {
         if let Some(path) = &path {
-            // Note using create_new here as we don't want to overwrite an existing file
-            // If such file already exists it should be safe to assume it has the right
-            // content so we can skip writing it again.
-            let _ = File::create_new(path).and_then(|mut f| {
-                if Self::write_file(&mut f, &sysctls).is_err() {
-                    // remove file again on write errors
-                    fs::remove_file(path)
-                } else {
-                    Ok(())
-                }
-            });
+            // Note we must replace any already existing file.
+            // The file is named after the interface and such names are reused, so a
+            // leftover file from a network that no longer exists (i.e. netavark got
+            // killed before it could tear the network down) may contain values that
+            // are wrong for the interface we are about to create.
+            let _ = Self::write_file_atomic(path.as_ref(), &sysctls);

If this write fails, the stale file stays, I feel like we should keep the error and use a log::warn!?

ajoshua2004 ajoshua2004 commented on Oct 07, 2026 18:30 UTC
Reviewer

LGTM logic wise just some thoughts

fpob fpob commented on code on Dec 02, 2026 13:29 UTC
Code Review
src/network/sysctl.rs:1
@@ -104,6 +99,26 @@ impl<'a, P: AsRef<Path>, V: AsRef<str> + 'a> SysctlDWriter<'a, P, V> {
         }
     }
 
+    /// write the sysctl.d(5) file, replacing an already existing file
+    ///
+    /// The content is written to a temporary file in the same directory which is
+    /// then renamed over the target. That way systemd-sysctl can never read a
+    /// partially written file, it either sees the old or the new content.
+    fn write_file_atomic(path: &Path, sysctls: &[(V, &'a str)]) -> Result<()> {
+        let mut tmp_path = path.as_os_str().to_os_string();
+        tmp_path.push(format!(".tmp.{}", std::process::id()));
+        let tmp_path = PathBuf::from(tmp_path);

I think you could use with_added_extension instead of these 3 lines.

hcebke hcebke commented on Dec 02, 2026 15:03 UTC
Author

Looks like the bug has been fixed in 5d969595efeaf20b30ddaa135f43bbc7c95d3a19.
What remains of this changeset (after rebase) is

1. overwriting an existing sysctl.d file,
2. the atomic write,
3. a bats test.

1\. was only needed for hosts to recover without a reboot (nice to have), 2. is apparently not necessary (judging from the comments on this PR), 3. is only nice to have (I didn't check if an equivalent test has already been added in the meantime).

Thus, I conclude this PR is no longer needed, and #1497 is already fixed.

I'll close this PR.

Reviewers
Assignees
hcebke hcebke
Labels
risk: low confidence: 83% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 39% (MEDIUM) · Higher = larger blast radius
39%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 83% · Lower = rushed or shallow review
83%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 6.6% · Safe to merge
6.6%