fix: detect a native nftables ruleset in the firewall policy checks - #366
Open
nicodarge wants to merge 1 commit into
Open
fix: detect a native nftables ruleset in the firewall policy checks#366nicodarge wants to merge 1 commit into
nicodarge wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
net_fw_default_policy_drop(3.5.4.1.1) only read the iptables chains. A host that filters with nftables and does not have theiptablespackage installed at all was reported as having no defaultDROPpolicy, even withpolicy dropset on both theinputandforwardhooks.While fixing it, three parsing defects showed up in the other nftables checks, which matched hook names with
grep 'hook input'andawk '/hook input/,/}/':input,outputandforwardhook names in those families for a packet path that carries neither routed nor locally delivered IP traffic. A host whose IP traffic was not filtered at all could pass on the strength of a bridge policy.type filter hook ...line was scanned, so the valid form below was read as the implicitaccept, failing a compliant host:tcp dport { 22, 80 } accept, truncated the body, hiding the rules that followed fromnftables_loopback_is_configuredandnftables_established_connections.Change
Two helpers in
lib/utils.sh,nft_ip_base_chainsandnft_ip_hook_policies, now do the parsing for all six checks. They keep only theip,ip6andinetfamilies, count braces to delimit a chain, treat a base chain declared without a policy asaccept, and also understand the flatadd chain ...syntax a hand-written persistent rules file may use.For
net_fw_default_policy_drop, a chain is compliant as soon as one of the two backends denies: anacceptverdict does not end the traversal of a hook, so a single base chain set todropis enough. Each ruleset is now read once per audit instead of once per assertion.The addition to
lib/utils.shis purely additive, so no other script is affected.Tests
tests/hardening/nftables_rules_permanent.shwas asserting on a file containing the literal lineshook input/hook output/hook forward, which only ever passed because of theawkrange. It now uses a real ruleset, plus a bridge-only case that must fail.net_fw_default_policy_drop,nftables_default_deny_policyandnftables_has_base_chainsgained cases covering a bridge-only ruleset and a compliant IP one. They are guarded onnft list rulesetsucceeding, since reading and writing a ruleset needsCAP_NET_ADMIN, which the test container does not have — they skip in CI and were run locally with the capability granted, on debian11, debian12 and debian13.