fix: hardening

This commit is contained in:
FUFSoB committed 2026-10-05 22:44:09 +05:00
1 parent 017a947a18
commit a435942b4f
168 files changed
+9166 -1644

No files matched your search

+108 -17
View File
@@ -15,6 +15,7 @@ let
tproxyLanSysctl
perAppRoutingTun
proxyCfg
cfg
;
inherit (proxyCfg) ipv6;
inherit (constants)
@@ -47,6 +48,23 @@ let
"-6"
];
# A table, as NixOS's firewall includes it (constants.persistedNftDir): written aside and
# renamed, never half there. `lines`: its rules, as shell words printed one per line.
persistedNftDir = ctx.constants.persistedNftDir;
persistTable =
table: lines:
lib.optionalString ctx.firewallIncludesTables ''
${pkgs.coreutils}/bin/install -d -m 0700 ${persistedNftDir}
(
umask 077
printf '%s\n' "add table inet ${table}" "delete table inet ${table}" ${lines} \
> ${persistedNftDir}/${table}.nft.tmp
) && mv -f ${persistedNftDir}/${table}.nft.tmp ${persistedNftDir}/${table}.nft
'';
# Before the table goes: a firewall reload in between would bring it back.
unpersistTable =
table: lib.optionalString ctx.firewallIncludesTables "rm -f ${persistedNftDir}/${table}.nft";
# Replies to connections from outside (sshd, a game server, anything listening) take
# proxyMark, so the fwmark rule sends them back out the way they came instead of into
# the TUN, where nothing expects them. Locally started connections stay "original".
@@ -72,18 +90,30 @@ let
in
{
# One nft transaction swaps the old table for the new: traffic never sees neither. Also the
# unit's reload, which a switch runs instead of a restart (units.nix).
# unit's reload, which a switch runs instead of a restart (units.nix). The time daemons'
# users are looked up here rather than named in the rules: a missing one would fail them all.
killSwitchUpScript = pkgs.writeShellScript "proxy-suite-routing" ''
set -euo pipefail
${nft} -f - <<'EOF'
uids=()
for user in ${lib.escapeShellArgs cfg.killSwitch.timeSyncUsers}; do
uid=$(${pkgs.coreutils}/bin/id -u -- "$user" 2>/dev/null) && uids+=("$uid")
done
time_sync=
if (( ''${#uids[@]} )); then
time_sync="add element inet proxy_suite_killswitch time_sync_uids { $(IFS=,; echo "''${uids[*]}") }"
fi
${nft} -f - <<EOF
add table inet proxy_suite_killswitch
delete table inet proxy_suite_killswitch
include "${killSwitchRulesFile}"
$time_sync
EOF
${persistTable "proxy_suite_killswitch" '''include "${killSwitchRulesFile}"' ''${time_sync:+"$time_sync"}''}
'';
killSwitchDownScript = pkgs.writeShellScript "proxy-suite-routing" ''
set +e
${unpersistTable "proxy_suite_killswitch"}
${deleteKillSwitchTable}
'';
@@ -95,6 +125,10 @@ in
tun6_route_prefix=${lib.escapeShellArg globalTunIPv6RoutePrefix}
tun_addr=""
tun_route_prefix=""
# A kernel booted with ipv6.disable=1 refuses every -6 command: the TUN still comes up.
families=(-4)
if [ -e /proc/net/if_inet6 ]; then families+=(-6); fi
has_ipv6() { [ "''${#families[@]}" = 2 ]; }
${builders.cidrNetworkFunction}
@@ -113,6 +147,7 @@ in
${deleteXrayTunRules}
${deleteXrayTunReplyTable}
${nft} -f ${xrayTunReplyRules}
${persistTable xrayTunReplyTable "'include \"${xrayTunReplyRules}\"'"}
# XRay may not have brought the link up yet, and routes via a down link fail with
# "Device for nexthop is not up".
@@ -121,30 +156,59 @@ in
tun_route_prefix="$(cidr_network "$tun_cidr")"
${ip} -4 addr replace "$tun_cidr" dev ${lib.escapeShellArg globalTun.interface}
${lib.optionalString ipv6 ''${ip} -6 addr replace "$tun6_cidr" dev ${lib.escapeShellArg globalTun.interface}''}
${lib.optionalString ipv6 ''has_ipv6 && ${ip} -6 addr replace "$tun6_cidr" dev ${lib.escapeShellArg globalTun.interface}''}
${ip} -4 route replace "$tun_route_prefix" dev ${lib.escapeShellArg globalTun.interface} src "$tun_addr" table ${toString tunAutoRouteTableIndex}
# No uplink src: it goes stale when the uplink address changes, and XRay's own sockets
# (routed by uid and mark, not bound to an interface) pick theirs from main.
${ip} -4 route replace default dev ${lib.escapeShellArg globalTun.interface} table ${toString tunAutoRouteTableIndex}
${lib.optionalString ipv6 ''
${ip} -6 route replace "$tun6_route_prefix" dev ${lib.escapeShellArg globalTun.interface} table ${toString tunAutoRouteTableIndex}
${ip} -6 route replace default dev ${lib.escapeShellArg globalTun.interface} table ${toString tunAutoRouteTableIndex}
''}
${
if ipv6 then
''
if has_ipv6; then
${ip} -6 route replace "$tun6_route_prefix" dev ${lib.escapeShellArg globalTun.interface} table ${toString tunAutoRouteTableIndex}
${ip} -6 route replace default dev ${lib.escapeShellArg globalTun.interface} table ${toString tunAutoRouteTableIndex}
fi
''
else
''
# No IPv6 in the TUN: unreachable, as sing-box's strict_route and the per-app TUN
# have it, so apps fall back to IPv4. Without, the rules below would find nothing
# in this table and go on to main: every IPv6 connection straight out the uplink.
${ip} -6 route replace unreachable default table ${toString tunAutoRouteTableIndex} 2>/dev/null || true
''
}
${lib.optionalString perAppRoutingTun.enable ''
${ip} -4 rule add pref ${toString xrayTunPerAppTunRulePriority} fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable}
${ip} -6 rule add pref ${toString xrayTunPerAppTunRulePriority} fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable}
for family in "''${families[@]}"; do
${ip} "$family" rule add pref ${toString xrayTunPerAppTunRulePriority} fwmark ${toString perAppRoutingTun.fwmark} table ${toString perAppRoutingTun.routeTable}
done
''}
service_uid=$(${pkgs.coreutils}/bin/id -u ${constants.serviceUser})
${ip} -4 rule add pref ${toString xrayTunServiceUserRulePriority} uidrange "$service_uid-$service_uid" lookup main
${ip} -6 rule add pref ${toString xrayTunServiceUserRulePriority} uidrange "$service_uid-$service_uid" lookup main
for family in -4 -6; do
for service_user in ${lib.escapeShellArgs constants.ownTrafficUsers}; do
service_uid=$(${pkgs.coreutils}/bin/id -u "$service_user")
for family in "''${families[@]}"; do
${ip} "$family" rule add pref ${toString xrayTunServiceUserRulePriority} uidrange "$service_uid-$service_uid" lookup main
done
done
for family in "''${families[@]}"; do
${ip} "$family" rule add pref ${toString xrayTunMarkBypassRulePriority} fwmark ${toString globalTproxy.proxyMark} lookup main
${ip} "$family" rule add pref ${toString xrayTunDnsRulePriority} ipproto udp dport 53 table ${toString tunAutoRouteTableIndex}
${ip} "$family" rule add pref ${toString xrayTunDnsRulePriority} ipproto tcp dport 53 table ${toString tunAutoRouteTableIndex}
${ip} "$family" rule add pref ${toString xrayTunMainRulePriority} lookup main suppress_prefixlength 0
done
${ip} -4 rule add pref ${toString tunAutoRouteRulePriority} not fwmark ${toString globalTproxy.proxyMark} table ${toString tunAutoRouteTableIndex}
${ip} -6 rule add pref ${toString tunAutoRouteRulePriority} not fwmark ${toString globalTproxy.proxyMark} table ${toString tunAutoRouteTableIndex}
for family in "''${families[@]}"; do
${ip} "$family" rule add pref ${toString tunAutoRouteRulePriority} not fwmark ${toString globalTproxy.proxyMark} table ${toString tunAutoRouteTableIndex}
done
'';
# The reply marking, after a firewall reload that flushed the ruleset: without it, replies
# to connections from outside (sshd among them) are routed into the TUN until it restarts.
xrayTunReloadScript = pkgs.writeShellScript "proxy-suite-routing" ''
set -euo pipefail
${builders.mkNftReplaceTable {
inherit nft;
family = "inet";
table = xrayTunReplyTable;
file = xrayTunReplyRules;
}}
'';
tproxyUpScript = pkgs.writeShellScript "proxy-suite-routing" ''
@@ -164,24 +228,50 @@ in
table = globalTproxy.routeTable;
}}
${nft} -f ${nftablesRulesFile}
# Set on NixOS already; system-manager hosts only get them here.
# Set on NixOS already; system-manager hosts only get them here. Left on at the stop:
# by then something else (Docker, libvirt) may rely on forwarding too.
${lib.concatStrings (
lib.mapAttrsToList (name: value: ''
${pkgs.procps}/bin/sysctl -q -w ${name}=${toString value}
'') tproxyLanSysctl
)}
# The route first, then the marking: a packet marked before its rule exists would leave
# by the main table, and the kill switch lets the mark through.
${builders.mkTproxyRoutingUp {
inherit ip;
inherit ipv6;
fwmark = globalTproxy.fwmark;
table = globalTproxy.routeTable;
}}
${nft} -f ${nftablesRulesFile}
${persistTable "singbox" "'include \"${nftablesRulesFile}\"'"}
'';
# After a firewall reload that flushed the ruleset (NixOS's flushRuleset, a distribution's
# `flush ruleset` config): the marking alone, swapped in whole in one transaction. The
# policy routing is no nftables state, and stays.
tproxyReloadScript = pkgs.writeShellScript "proxy-suite-routing" ''
set -euo pipefail
${builders.mkNftReplaceTable {
inherit nft;
family = "inet";
table = "singbox";
file = nftablesRulesFile;
}}
'';
# The daemon guard (constants.daemonMetadataGuard) as loaded: a static table, so its
# listing loads again as it is.
persistDaemonGuardScript = pkgs.writeShellScript "proxy-suite-routing" ''
set -euo pipefail
rules=$(${nft} -s list table inet proxy_suite_daemon_guard) || exit 0
${persistTable "proxy_suite_daemon_guard" ''"$rules"''}
'';
tproxyDownScript = pkgs.writeShellScript "proxy-suite-routing" ''
set +e
${unpersistTable "singbox"}
${builders.mkNftDeleteTable {
inherit nft;
family = "inet";
@@ -216,6 +306,7 @@ in
table = tunAutoRouteTableIndex;
}}
${deleteXrayTunRules}
${unpersistTable xrayTunReplyTable}
${deleteXrayTunReplyTable}
${builders.mkIpRouteFlushTable {
inherit ip;