Skip to content

Conversation

@mikewiacek
Copy link

Overview

This PR fixes a crash in netd caused by unsafe handling of errors returned from iptables operations.

Previously, code paths in IPTablesRuleConfig.Ensure assumed all errors from AppendUnique and Delete would be of type *iptables.Error.

If the error was not (e.g. exec.ExitError or another Go error type), the type assertion failed, leaving eerr == nil.
The code still attempted to call eerr.Error() or eerr.ExitStatus(), resulting in a panic:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation]

Root Cause

  • The condition mixed !ok with calls to methods on eerr.
  • When !ok, eerr is nil, so dereferencing it caused a panic.

Fix

  • Refactor error handling to only access ExitStatus() or Error() when the type assertion succeeds.
  • If the error is not an *iptables.Error, bubble it up cleanly instead of attempting to interpret it.

Impact

  • Prevents netd from crashing on unexpected iptables error types.
  • Makes error handling more robust against changes in underlying iptables behavior or Go’s exec error types.

Before

!ok branch still tried to dereference eerr, leading to panic.

After

  • Safe separation of the two cases:
    • Handle *iptables.Error with exit status logic.
    • Return all other error types without dereferencing.

Testing

  • Verified with synthetic error injections: non-iptables errors no longer crash the daemon.
  • Normal iptables error handling remains unchanged.

@google-oss-prow
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: mikewiacek
Once this PR has been reviewed and has the lgtm label, please assign skmatti for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@jingyuanliang
Copy link
Member

			if err := r.IPT.Delete(r.Spec.TableName, r.Spec.ChainName, rs...); err != nil {
				if eerr, eok := err.(*iptables.Error); !eok || eerr.ExitStatus() != 2 && !strings.Contains(eerr.Error(), "No chain/target/match") {
					return err
				}
			}

should be enough?

but I'd like to confirm the exact behavior desired... e.g. what's the definition of the exit status values?

@jingyuanliang
Copy link
Member

@MrHohn

@mikewiacek
Copy link
Author

ping?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants