fix: hardening and fixes

This commit is contained in:
fufsob committed 2026-09-15 08:56:33 +00:00
1 parent aa19042ee5
commit a82417a2bd
32 files changed
+619 -256

No files matched your search

+32 -16
View File
@@ -3,12 +3,22 @@
lib,
pkgs,
cfg,
derived,
}:
let
s = cfg.sshProxy;
inherit (derived.constants) serviceUser unprivilegedServiceConfig;
destination = if s.server.user == null then s.server.host else "${s.server.user}@${s.server.host}";
extraArgs = lib.concatMapStrings (arg: " args+=(${lib.escapeShellArg arg})\n") s.extraArgs;
# The service user's home is /var/empty: accepted host keys are kept in the state dir.
knownHosts =
if s.knownHostsFile != null then
''"$CREDENTIALS_DIRECTORY/known_hosts"''
else if s.serviceUser == serviceUser then
''"$STATE_DIRECTORY/known_hosts"''
else
null;
startScript = pkgs.writeShellScript "proxy-suite-ssh-proxy" ''
set -euo pipefail
@@ -23,10 +33,8 @@ let
-D ${lib.escapeShellArg "${s.listener.address}:${toString s.listener.port}"}
)
${lib.optionalString (s.server.port != 22) "args+=(-p ${toString s.server.port})"}
${lib.optionalString (s.identityFile != null) "args+=(-i ${lib.escapeShellArg s.identityFile})"}
${lib.optionalString (
s.knownHostsFile != null
) "args+=(-o ${lib.escapeShellArg "UserKnownHostsFile=${s.knownHostsFile}"})"}
${lib.optionalString (s.identityFile != null) ''args+=(-i "$CREDENTIALS_DIRECTORY/identity")''}
${lib.optionalString (knownHosts != null) ''args+=(-o UserKnownHostsFile=${knownHosts})''}
${extraArgs}
exec ${pkgs.openssh}/bin/ssh "''${args[@]}" ${lib.escapeShellArg destination}
'';
@@ -40,17 +48,25 @@ in
# Restart=always plus a server that refuses connections would otherwise hit
# the default 5-starts-in-10s limit and stay failed.
startLimitIntervalSec = 0;
serviceConfig = {
ExecStart = startScript;
# `ssh -N` exits 0 when the server closes the connection cleanly, which
# under on-failure left the tunnel down for good.
Restart = "always";
RestartSec = 5;
# One fd per forwarded channel; the 1024 default is low for a relay.
LimitNOFILE = 65536;
}
// lib.optionalAttrs (s.serviceUser != null) {
User = s.serviceUser;
};
serviceConfig =
lib.optionalAttrs (s.serviceUser == serviceUser) (unprivilegedServiceConfig [ "net_bind_service" ])
// {
ExecStart = startScript;
# Copies the service user can read, whoever owns the originals.
LoadCredential =
lib.optional (s.identityFile != null) "identity:${s.identityFile}"
++ lib.optional (s.knownHostsFile != null) "known_hosts:${s.knownHostsFile}";
StateDirectory = "proxy-suite/ssh";
StateDirectoryMode = "0700";
# `ssh -N` exits 0 when the server closes the connection cleanly, which
# under on-failure left the tunnel down for good.
Restart = "always";
RestartSec = 5;
# One fd per forwarded channel; the 1024 default is low for a relay.
LimitNOFILE = 65536;
}
// lib.optionalAttrs (s.serviceUser != null && s.serviceUser != serviceUser) {
User = s.serviceUser;
};
};
}