Skip to content

networking/nftables: add .tables property and disable ruleset flushing by default#207758

Merged
mkg20001 merged 10 commits intoNixOS:masterfrom
mkg20001:nftex
Aug 27, 2023
Merged

networking/nftables: add .tables property and disable ruleset flushing by default#207758
mkg20001 merged 10 commits intoNixOS:masterfrom
mkg20001:nftex

Conversation

@mkg20001
Copy link
Copy Markdown
Member

Description of changes

This allows for other unmanaged tables to co-exist peacefully on the os,
by having the nixos-managed tables be re-created atomically and the other
tables will simply be left untouched.

Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 23.05 Release Notes (or backporting 22.11 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
    • (Release notes changes) Ran nixos/doc/manual/md-to-db.sh to update generated release notes
  • Fits CONTRIBUTING.md.

@github-actions github-actions Bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` labels Dec 26, 2022
@github-actions github-actions Bot added 8.has: changelog This PR adds or changes release notes 8.has: documentation This PR adds or changes documentation labels Dec 26, 2022
@ofborg ofborg Bot added 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. labels Dec 26, 2022
@mkg20001 mkg20001 requested a review from ncfavier December 26, 2022 00:20
@mkg20001 mkg20001 force-pushed the nftex branch 3 times, most recently from 256fc47 to 49ccf93 Compare December 26, 2022 00:49
Copy link
Copy Markdown
Member

@ncfavier ncfavier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't really comment on the nftables changes, sorry.

Comment thread nixos/modules/services/networking/nftables.nix Outdated
@duament
Copy link
Copy Markdown
Contributor

duament commented Dec 27, 2022

We may also disable flushing in execStop to fix #207058.

@ofborg ofborg Bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. and removed 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. labels Dec 28, 2022
@mkg20001
Copy link
Copy Markdown
Member Author

mkg20001 commented Dec 28, 2022

We may also disable flushing in execStop to fix #207058.

There appears to have been a systemd bug systemd/systemd#11238 and it's no longer occuring

nftables.service loaded active exited nftables firewall (after failed reload)

What I've added is that if flushRuleset is set to false, then the service will only remove tables it created, but this may be undesired. Opinions?

@mkg20001
Copy link
Copy Markdown
Member Author

So I couldn't reproduce it or I did it wrong, but seems the issue is fresh. Not sure how to reproduce then. Added a table with invalid contents and had a failure recorded, nft list ruleset still had the same tables as before.

@mkg20001
Copy link
Copy Markdown
Member Author

Thinking about flushRuleset I don't know if it makes sense to have it always off by default. Perhaps the default could be dynamic based on whether or not .tables is being used? Or does that make things even more complicated?

@duament
Copy link
Copy Markdown
Contributor

duament commented Dec 28, 2022

I can't reproduce that issue either. It seems he restarted the nftables.service and the rulesets got flushed. This PR may not help that.

@duament
Copy link
Copy Markdown
Contributor

duament commented Dec 28, 2022

Thinking about flushRuleset I don't know if it makes sense to have it always off by default. Perhaps the default could be dynamic based on whether or not .tables is being used? Or does that make things even more complicated?

I prefer to keep it off by default. This prevents deleting tables maintained by other programs like systemd, wg-quick, etc.

There's another problem that when the user switches to a profile with firewall disabled, the nixos-fw table remains.
Similar to this issue https://web.archive.org/web/20180605181330/https://github.com/NixOS/nixpkgs/issues/27510.

@mkg20001
Copy link
Copy Markdown
Member Author

mkg20001 commented Dec 28, 2022

Thinking about flushRuleset I don't know if it makes sense to have it always off by default. Perhaps the default could be dynamic based on whether or not .tables is being used? Or does that make things even more complicated?

I prefer to keep it off by default. This prevents deleting tables maintained by other programs like systemd, wg-quick, etc.

Makes sense. I'll add a note to ruleset+rulesetFile that the user has to delete tables manually

There's another problem that when the user switches to a profile with firewall disabled, the nixos-fw table remains. Similar to this issue web.archive.org/web/20180605181330/https://github.com/NixOS/nixpkgs/issues/27510.

  • A script that runs ExecStartPost, that has the nftables file as an argument and generates a list of deletions, stores it as /var/lib/nftables/deletions.nft by running over the nftables file with grep or something (cat nftables | grep -r "^ *table" | sed -e "s|^ *table|delete table|g" -e 's|{||g'

    • This would be hacky, maybe cause unwanted behaviour
  • Make a script that greps for all delete statements, puts them into /var/lib/nftables/deletions.nft (this would be more explicit and this has the advantage of including delete statements from custom ruleset/rulesetFile)

  • Or instead just write the deletions generated from .tables into /var/lib/nftables/deletions.nft

  • ExecStop changed to "nft -f /var/lib/nftables/deletions.nft"

Edit: We need to store it in deletions.nft anyways, since the tables that get removed from config won't be deleted otherwise

@duament
Copy link
Copy Markdown
Contributor

duament commented Dec 29, 2022

A simple solution is to set an empty table when the firewall is disabled. Your solution seems more generic.

@mkg20001
Copy link
Copy Markdown
Member Author

@duament .tables allows adding custom tables. We need to handle those aswell. So it must be generic.

Comment thread nixos/doc/manual/from_md/release-notes/rl-2305.section.xml Outdated
@mkg20001 mkg20001 force-pushed the nftex branch 2 times, most recently from 61a95bc to 50d7ec2 Compare March 27, 2023 17:52
@mkg20001
Copy link
Copy Markdown
Member Author

How about keeping the flushRulesets option and disable .tables options when it's true?

Hrm this means any module we have that uses .tables is automatically turning off flushRuleset, which may not be what users want. Another idea is to disable it if no user extraRules are set. (This is only affecting the option default value, so users can change it anyways)

@mkg20001
Copy link
Copy Markdown
Member Author

I made it so flushRuleset is enabled on rulset{,File} being used (plus 23.05 being used so we can backport)

Also made rulesetFile and ruleset non-exclusive (there's no assertion for it and there's no good reason I see for it to be exclusive)

Not sure if I'm over feature-freeze with this pr? Otherwise feel free to merge

Copy link
Copy Markdown
Contributor

@sg-qwt sg-qwt Apr 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

after applying this patch with a nixos-rebuild switch, nfttables.service failed with following error:

Apr 22 00:36:33 xx systemd[1]: Reloading nftables firewall...
Apr 22 00:36:33 xx gfzcn50sarqv9s711n1zfy6yky1wa2rf-nftables-rules[1274297]: /nix/store/gfzcn50sarqv9s711n1zfy6yky1wa2rf-nftables-rules:3:1-42: Error: File not found: /var/lib/nftables/deletions.nft
Apr 22 00:36:33 xx gfzcn50sarqv9s711n1zfy6yky1wa2rf-nftables-rules[1274297]: include "/var/lib/nftables/deletions.nft"
Apr 22 00:36:33 xx gfzcn50sarqv9s711n1zfy6yky1wa2rf-nftables-rules[1274297]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I have to manually restart nftables.service again to work. Maybe we missed a check for deletionsScriptVar here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, we missed an ensureDeletions in ExecReload

Copy link
Copy Markdown
Contributor

@sg-qwt sg-qwt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hit some error on a first nixos switch with this patch, check comments.
Second switch with some firewall changes works as expected, custom tables are not flushed, which is cool!

@KiruyaMomochi
Copy link
Copy Markdown
Contributor

This PR is great. For me the conflict between networking.nftables.rulesetFile and the firewall is disturbing.

Considering the reload issue, can I assume that ensureDeletions always run before rulesScript?
If yes, we can make a single script that first ensure deletions, then run the rule script.

@mkg20001
Copy link
Copy Markdown
Member Author

Considering the reload issue, can I assume that ensureDeletions always run before rulesScript?
If yes, we can make a single script that first ensure deletions, then run the rule script.

We need to run the previous deletions to clean up the previous rules.

@nixos-discourse
Copy link
Copy Markdown

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/is-it-possible-to-write-custom-rules-to-the-nixos-firewall/27900/7

@justinas justinas mentioned this pull request Aug 20, 2023
12 tasks
@justinas
Copy link
Copy Markdown
Member

Any movement on this?

mkg20001 and others added 3 commits August 28, 2023 00:30
…g by default

This allows for other unmanaged tables to co-exist peacefully on the os,
by having the nixos-managed tables be re-created atomically and the other
tables will simply be left untouched.
@mkg20001
Copy link
Copy Markdown
Member Author

Rebased.

It turns out the reload error occurs when the service was started before this patchset was applied, as ensureDeletions wasn't run on startup then.

So added ensureDeletions to execreload which should fix the error

@mkg20001 mkg20001 merged commit 18748c1 into NixOS:master Aug 27, 2023
@mkg20001 mkg20001 deleted the nftex branch August 27, 2023 23:29
${table.content}
}
'') enabledTables)}
${cfg.ruleset}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some defines in my config like this:

  networking.nftables.ruleset = lib.mkBefore ''
    define NET_BLA = 128.0.0.0/19
  '';

which break with this change, because the config from nftables.tables is inserted here before ruleset.

Not sure if and how we can fix this, just commenting here so you're aware.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can either move cfg.ruleset up and/or introduce properly typed nftables.define.<key>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the proper type would be for define tbh. Something like attrsOf str can work, but that doesn't allow for any merging, e.g. of lists/sets. Moving rulset up modifies the behavior from what's on master after this, but maybe it's closer to before? I'll see if I can find the time to run some tests.

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

Labels

6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: changelog This PR adds or changes release notes 8.has: documentation This PR adds or changes documentation 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants