WIP: feat: Hermes confined to act only as an Endo guest #1

Draft
dckc wants to merge 7 commits from endo-guest into main
pull from: endo-guest
Showing only changes of commit a4b704a38c - Show all commits
+45 -30
View File
@@ -20,6 +20,43 @@ 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 = {
@@ -79,60 +116,38 @@ in
systemd.services.hermes-agent = {
after = [ "hermes-llm-bridge.service" ];
requires = [ "hermes-llm-bridge.service" ];
serviceConfig = {
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" ];
serviceConfig = sandbox // {
BindReadOnlyPaths = [
cfg.hostSocket
cfg.llmSocket
];
# the state directory, back inside the empty /var/lib
BindPaths = [ hermes.stateDir ];
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
# 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 = {
description = "Hermes's loopback port to the inference proxy";
unitConfig.JoinsNamespaceOf = "hermes-agent.service";
partOf = [ "hermes-agent.service" ];
serviceConfig = {
serviceConfig = sandbox // {
User = hermes.user;
Group = hermes.group;
PrivateNetwork = true;
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;
CapabilityBoundingSet = "";
};
};