Files
hermes-guest/nix/hermes-guest.nix
T
dckc a4b704a38c 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
2026-11-29 16:36:13 -06:00

157 lines
5.1 KiB
Nix

# Hermes, confined to act only as one Endo guest (of toybox, or of an Endo daemon).
#
# This module is what ports: the same unit runs on a Linux host and inside
# the macOS VM (./vfkit-vm.nix). It reaches the world through exactly two
# Unix sockets, and whatever imports it decides what serves them:
#
# hostSocket the guest's methods (listMessages, request, resolve,
# lookup, ...) as an MCP server, bound to one guest agent
# llmSocket model inference, through a proxy that holds the API key
#
# The service has no network of its own (PrivateNetwork), and sees no
# other Unix sockets on the machine.
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.hermes-guest;
hermes = config.services.hermes-agent;
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
{
options.services.hermes-guest = {
enable = lib.mkEnableOption "Hermes as a confined Endo guest";
hostSocket = lib.mkOption {
type = lib.types.str;
default = "${socketDir}/host.sock";
description = "The Unix socket where the guest's MCP methods are served.";
};
llmSocket = lib.mkOption {
type = lib.types.str;
default = "${socketDir}/llm.sock";
description = "The Unix socket of the inference proxy, an OpenAI-compatible endpoint.";
};
llmPort = lib.mkOption {
type = lib.types.port;
default = 8080;
description = "The loopback port, inside Hermes's own network namespace, that forwards to llmSocket.";
};
model = lib.mkOption {
type = lib.types.str;
description = "The model name, as the proxy's upstream knows it.";
};
};
config = lib.mkIf cfg.enable {
services.hermes-agent = {
enable = true;
addToSystemPackages = false;
settings = {
model = {
provider = "custom";
default = cfg.model;
base_url = "http://127.0.0.1:${toString cfg.llmPort}/v1";
# the proxy adds the real key; Hermes never holds it
api_key = "held-by-proxy";
};
terminal.backend = "local";
# the guest's methods are its whole world: offer them to the model
# directly, not behind tool_search/tool_call
tools.tool_search.enabled = "off";
};
mcpServers.host = {
command = "${pkgs.socat}/bin/socat";
args = [
"STDIO"
"UNIX-CONNECT:${cfg.hostSocket}"
];
};
};
# Upstream lingers the hermes uid so cron jobs can `systemd-run --user`.
# A user manager starts units outside this sandbox, so: no linger, and
# no /run/user (below). Cron jobs then fail closed.
users.users.${hermes.user}.linger = lib.mkForce false;
systemd.services.hermes-agent = {
after = [ "hermes-llm-bridge.service" ];
requires = [ "hermes-llm-bridge.service" ];
serviceConfig = sandbox // {
BindReadOnlyPaths = [
cfg.hostSocket
cfg.llmSocket
];
# the state directory, back inside the empty /var/lib
BindPaths = [ hermes.stateDir ];
ProtectHome = lib.mkForce true;
};
};
# Hermes's client wants a TCP base_url. This joins Hermes's network
# 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 = {
description = "Hermes's loopback port to the inference proxy";
unitConfig.JoinsNamespaceOf = "hermes-agent.service";
partOf = [ "hermes-agent.service" ];
serviceConfig = sandbox // {
User = hermes.user;
Group = hermes.group;
ExecStart = lib.escapeShellArgs [
"${pkgs.socat}/bin/socat"
"TCP-LISTEN:${toString cfg.llmPort},bind=127.0.0.1,fork,reuseaddr"
"UNIX-CONNECT:${cfg.llmSocket}"
];
BindReadOnlyPaths = [ cfg.llmSocket ];
ProtectSystem = "strict";
ProtectHome = true;
PrivateTmp = true;
NoNewPrivileges = true;
};
};
systemd.tmpfiles.rules = [ "d ${socketDir} 0755 root root -" ];
};
}