2011-03-11 13:38:52 +00:00
|
|
|
# Test the firewall module.
|
|
|
|
|
2022-12-22 16:23:23 +00:00
|
|
|
import ./make-test-python.nix (
|
|
|
|
{ pkgs, nftables, ... }:
|
|
|
|
{
|
|
|
|
name = "firewall" + pkgs.lib.optionalString nftables "-nftables";
|
2021-01-10 19:08:30 +00:00
|
|
|
meta = with pkgs.lib.maintainers; {
|
2024-07-04 16:30:39 +00:00
|
|
|
maintainers = [
|
|
|
|
rvfg
|
|
|
|
garyguo
|
|
|
|
];
|
2015-07-12 10:09:40 +00:00
|
|
|
};
|
2011-03-11 13:38:52 +00:00
|
|
|
|
|
|
|
nodes = {
|
2011-09-14 18:20:50 +00:00
|
|
|
walled =
|
2018-07-20 20:56:59 +00:00
|
|
|
{ ... }:
|
2024-07-04 16:30:39 +00:00
|
|
|
{
|
|
|
|
networking.firewall = {
|
|
|
|
enable = true;
|
|
|
|
logRefusedPackets = true;
|
|
|
|
# Syntax smoke test, not actually verified otherwise
|
|
|
|
allowedTCPPorts = [
|
|
|
|
25
|
|
|
|
993
|
|
|
|
8005
|
|
|
|
];
|
|
|
|
allowedTCPPortRanges = [
|
|
|
|
{
|
|
|
|
from = 980;
|
|
|
|
to = 1000;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
from = 990;
|
|
|
|
to = 1010;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
from = 8000;
|
|
|
|
to = 8010;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
interfaces.eth0 = {
|
|
|
|
allowedTCPPorts = [ 10003 ];
|
|
|
|
allowedTCPPortRanges = [
|
|
|
|
{
|
|
|
|
from = 10000;
|
|
|
|
to = 10005;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
interfaces.eth3 = {
|
|
|
|
allowedUDPPorts = [ 10003 ];
|
|
|
|
allowedUDPPortRanges = [
|
|
|
|
{
|
|
|
|
from = 10000;
|
|
|
|
to = 10005;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
2022-12-22 16:23:23 +00:00
|
|
|
networking.nftables.enable = nftables;
|
2011-03-11 13:38:52 +00:00
|
|
|
services.httpd.enable = true;
|
|
|
|
services.httpd.adminAddr = "foo@example.org";
|
|
|
|
|
2024-09-08 12:39:35 +00:00
|
|
|
specialisation.different-config.configuration = {
|
|
|
|
networking.firewall.rejectPackets = true;
|
|
|
|
};
|
2016-09-07 12:18:32 +00:00
|
|
|
};
|
|
|
|
|
2011-09-14 18:20:50 +00:00
|
|
|
attacker =
|
2018-07-20 20:56:59 +00:00
|
|
|
{ ... }:
|
2011-03-11 13:38:52 +00:00
|
|
|
{
|
|
|
|
services.httpd.enable = true;
|
|
|
|
services.httpd.adminAddr = "foo@example.org";
|
2014-04-11 15:15:56 +00:00
|
|
|
networking.firewall.enable = false;
|
2011-03-11 13:38:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-09-07 12:18:32 +00:00
|
|
|
testScript =
|
|
|
|
{ nodes, ... }:
|
|
|
|
let
|
2022-12-22 16:23:23 +00:00
|
|
|
unit = if nftables then "nftables" else "firewall";
|
2016-09-07 12:18:32 +00:00
|
|
|
in
|
|
|
|
''
|
2019-11-09 18:35:48 +00:00
|
|
|
start_all()
|
2016-09-07 12:18:32 +00:00
|
|
|
|
2022-12-22 16:23:23 +00:00
|
|
|
walled.wait_for_unit("${unit}")
|
2019-11-09 18:35:48 +00:00
|
|
|
walled.wait_for_unit("httpd")
|
|
|
|
attacker.wait_for_unit("network.target")
|
2011-03-11 13:38:52 +00:00
|
|
|
|
2016-09-07 12:18:32 +00:00
|
|
|
# Local connections should still work.
|
2019-11-09 18:35:48 +00:00
|
|
|
walled.succeed("curl -v http://localhost/ >&2")
|
2011-03-11 13:38:52 +00:00
|
|
|
|
2016-09-07 12:18:32 +00:00
|
|
|
# Connections to the firewalled machine should fail, but ping should succeed.
|
2019-11-09 18:35:48 +00:00
|
|
|
attacker.fail("curl --fail --connect-timeout 2 http://walled/ >&2")
|
|
|
|
attacker.succeed("ping -c 1 walled >&2")
|
2011-03-11 13:38:52 +00:00
|
|
|
|
2016-09-07 12:18:32 +00:00
|
|
|
# Outgoing connections/pings should still work.
|
2019-11-09 18:35:48 +00:00
|
|
|
walled.succeed("curl -v http://attacker/ >&2")
|
|
|
|
walled.succeed("ping -c 1 attacker >&2")
|
2011-03-11 13:38:52 +00:00
|
|
|
|
2024-07-04 16:30:39 +00:00
|
|
|
# Open tcp port 80 at runtime
|
|
|
|
walled.succeed("nixos-firewall-tool open tcp 80")
|
|
|
|
attacker.succeed("curl -v http://walled/ >&2")
|
|
|
|
|
|
|
|
# Reset the firewall
|
|
|
|
walled.succeed("nixos-firewall-tool reset")
|
|
|
|
attacker.fail("curl --fail --connect-timeout 2 http://walled/ >&2")
|
|
|
|
|
2016-09-07 12:18:32 +00:00
|
|
|
# If we stop the firewall, then connections should succeed.
|
2022-12-22 16:23:23 +00:00
|
|
|
walled.stop_job("${unit}")
|
2019-11-09 18:35:48 +00:00
|
|
|
attacker.succeed("curl -v http://walled/ >&2")
|
2011-03-11 13:38:52 +00:00
|
|
|
|
2016-09-07 12:18:32 +00:00
|
|
|
# Check whether activation of a new configuration reloads the firewall.
|
2019-11-09 18:35:48 +00:00
|
|
|
walled.succeed(
|
2024-09-08 12:39:35 +00:00
|
|
|
"/run/booted-system/specialisation/different-config/bin/switch-to-configuration test 2>&1 | grep -qF ${unit}.service"
|
2019-11-09 18:35:48 +00:00
|
|
|
)
|
2016-09-07 12:18:32 +00:00
|
|
|
'';
|
2015-07-12 10:09:40 +00:00
|
|
|
}
|
|
|
|
)
|