fix(sandbox): one sandbox for every process running as Hermes's user

- The LLM bridge runs as Hermes's user but saw the host's /run. Where
  same-uid ptrace is allowed, Hermes could take it over and reach any
  socket there. It now gets Hermes's sandbox, with only llm.sock.
- A socket on a read-only mount still accepts connect(), so
  ProtectSystem=strict doesn't hide sockets under /var/lib. /var/lib is
  now empty, with the state directory bound back in.
- ProtectProc=invisible and ProcSubset=pid: other processes, and their
  command lines, are out of sight.

Generated-by: Claude Opus 5.5
This commit is contained in:
dckc committed 2026-11-29 16:36:13 -06:00
1 parent e38d681a9e
commit a4b704a38c
1 file changed
+45 -30
+45 -30
View File
@@ -20,6 +20,43 @@ let
cfg = config.services.hermes-guest; cfg = config.services.hermes-guest;
hermes = config.services.hermes-agent; hermes = config.services.hermes-agent;
socketDir = "/run/hermes-guest"; socketDir = "/run/hermes-guest";
# The sandbox every process running as Hermes's user gets. See
# CONTRIBUTING.md#confinement for why it confines.
sandbox = {
PrivateNetwork = true;
IPAddressDeny = "any";
IPAddressAllow = "localhost";
RestrictAddressFamilies = [
"AF_UNIX"
"AF_INET"
"AF_INET6"
];
# Path-named Unix sockets ignore network namespaces, and a socket on a
# read-only mount still accepts connect(): hide the places sockets
# live, then bind back what each unit may use. (/var/run is a symlink
# to /run, so it's covered too; PrivateTmp and ProtectHome cover /tmp
# and /home.)
TemporaryFileSystem = [
"/run"
"/var/lib"
];
ProtectProc = "invisible";
ProcSubset = "pid";
PrivateDevices = true;
PrivateIPC = true;
ProtectKernelTunables = true;
ProtectKernelModules = true;
ProtectKernelLogs = true;
ProtectControlGroups = true;
ProtectClock = true;
ProtectHostname = true;
RestrictNamespaces = true;
RestrictSUIDSGID = true;
LockPersonality = true;
CapabilityBoundingSet = "";
SystemCallArchitectures = "native";
};
in in
{ {
options.services.hermes-guest = { options.services.hermes-guest = {
@@ -79,60 +116,38 @@ in
systemd.services.hermes-agent = { systemd.services.hermes-agent = {
after = [ "hermes-llm-bridge.service" ]; after = [ "hermes-llm-bridge.service" ];
requires = [ "hermes-llm-bridge.service" ]; requires = [ "hermes-llm-bridge.service" ];
serviceConfig = { serviceConfig = sandbox // {
PrivateNetwork = true;
IPAddressDeny = "any";
IPAddressAllow = "localhost";
RestrictAddressFamilies = [
"AF_UNIX"
"AF_INET"
"AF_INET6"
];
# Path-named Unix sockets ignore network namespaces: hide them all,
# then put back the two this service may use. (/var/run is a
# symlink to /run, so it's covered too.)
TemporaryFileSystem = [ "/run" ];
BindReadOnlyPaths = [ BindReadOnlyPaths = [
cfg.hostSocket cfg.hostSocket
cfg.llmSocket cfg.llmSocket
]; ];
# the state directory, back inside the empty /var/lib
BindPaths = [ hermes.stateDir ];
ProtectHome = lib.mkForce true; ProtectHome = lib.mkForce true;
PrivateDevices = true;
PrivateIPC = true;
ProtectKernelTunables = true;
ProtectKernelModules = true;
ProtectKernelLogs = true;
ProtectControlGroups = true;
ProtectClock = true;
ProtectHostname = true;
RestrictNamespaces = true;
RestrictSUIDSGID = true;
LockPersonality = true;
CapabilityBoundingSet = "";
SystemCallArchitectures = "native";
}; };
}; };
# Hermes's client wants a TCP base_url. This joins Hermes's network # Hermes's client wants a TCP base_url. This joins Hermes's network
# namespace and forwards its loopback port to llmSocket. # namespace and forwards its loopback port to llmSocket. It runs as
# Hermes's user, so it gets the same sandbox: taking it over (ptrace,
# same uid) must gain nothing.
systemd.services.hermes-llm-bridge = { systemd.services.hermes-llm-bridge = {
description = "Hermes's loopback port to the inference proxy"; description = "Hermes's loopback port to the inference proxy";
unitConfig.JoinsNamespaceOf = "hermes-agent.service"; unitConfig.JoinsNamespaceOf = "hermes-agent.service";
partOf = [ "hermes-agent.service" ]; partOf = [ "hermes-agent.service" ];
serviceConfig = { serviceConfig = sandbox // {
User = hermes.user; User = hermes.user;
Group = hermes.group; Group = hermes.group;
PrivateNetwork = true;
ExecStart = lib.escapeShellArgs [ ExecStart = lib.escapeShellArgs [
"${pkgs.socat}/bin/socat" "${pkgs.socat}/bin/socat"
"TCP-LISTEN:${toString cfg.llmPort},bind=127.0.0.1,fork,reuseaddr" "TCP-LISTEN:${toString cfg.llmPort},bind=127.0.0.1,fork,reuseaddr"
"UNIX-CONNECT:${cfg.llmSocket}" "UNIX-CONNECT:${cfg.llmSocket}"
]; ];
BindReadOnlyPaths = [ cfg.llmSocket ];
ProtectSystem = "strict"; ProtectSystem = "strict";
ProtectHome = true; ProtectHome = true;
PrivateTmp = true; PrivateTmp = true;
NoNewPrivileges = true; NoNewPrivileges = true;
CapabilityBoundingSet = "";
}; };
}; };