feat(sandbox): an empty root, by NixOS confinement
The filesystem was the one part of the sandbox that started from the host's view and hid places from it (/run, /var/lib, /tmp, /home), so the argument had to assume no sockets elsewhere. Both units now start from an empty root: their closure (fullUnit, so PATH's packages too), /bin/sh, the state directory and the two sockets, and nothing else. socat is added to Hermes's closure by hand, since only config.yaml names it. CONTRIBUTING's table now has one row for files and sockets by path, and the "sockets the sandbox doesn't hide" caveat is gone. Generated-by: Claude Opus 5.5
This commit is contained in:
1 parent
6d48662c94
commit
d58b77aa89
2 files changed
+28
-25
No files matched your search
+12
-11
@@ -36,19 +36,20 @@ Python here follows [DisciplinedPython](https://codeberg.org/dckc/awesome-ocap/s
|
||||
2. connecting to `llm.sock`: inference at one upstream, with a key it never holds;
|
||||
3. writing to its stdout and stderr, which go to the journal.
|
||||
|
||||
**Approach: assume nothing about Hermes.** Hermes can rewrite its own `config.yaml`, add MCP servers, and run anything its shell reaches. All of it runs inside the unit, so it inherits the same namespaces, mounts, cgroup and seccomp filter. So the argument is about the `sandbox` in [nix/hermes-guest.nix](nix/hermes-guest.nix), which every unit running as Hermes's user gets, not about Hermes or its model.
|
||||
**Approach: assume nothing about Hermes.** Hermes can rewrite its own `config.yaml`, add MCP servers, and run anything its shell reaches. All of it runs inside the unit, so it inherits the same namespaces, mounts, cgroup and seccomp filter. So the argument is about the `sandbox` in [nix/hermes-guest.nix](nix/hermes-guest.nix), which every unit running as Hermes's user gets, and their `confinement`, not about Hermes or its model.
|
||||
|
||||
Each part of the sandbox starts from nothing and adds what's needed, the way a CSS reset does. There's no denylist to keep complete.
|
||||
|
||||
| Channel | What closes it |
|
||||
|---|---|
|
||||
| IP networking | `PrivateNetwork`: only loopback, shared with the bridge alone, whose one listener forwards to `llm.sock`. `IPAddressDeny=any` with `IPAddressAllow=localhost` is a second check. |
|
||||
| Unix sockets, by path | These cross network namespaces, and accept `connect()` even on a read-only mount. `TemporaryFileSystem` empties `/run` (so `/var/run`) and `/var/lib`, `PrivateTmp` covers `/tmp`, and `ProtectHome` covers `/home`, `/root` and `/run/user`. `BindReadOnlyPaths` binds back the two sockets. |
|
||||
| Files, and Unix sockets by path | NixOS `confinement`: the root is an empty directory. Bound into it are the unit's closure (read-only, `fullUnit`, so `PATH`'s packages too), `/bin/sh`, the state directory (`BindPaths`) and the two sockets (`BindReadOnlyPaths`), plus private `/dev`, `/proc`, `/sys` and `/tmp`. A path outside those doesn't exist in the unit's view. That matters for sockets, which cross network namespaces and accept `connect()` even on a read-only mount. |
|
||||
| Unix sockets, abstract | Private to the network namespace. |
|
||||
| Other socket families | `RestrictAddressFamilies`: `AF_UNIX`, and `AF_INET`/`AF_INET6` for loopback. No `AF_NETLINK`, `AF_PACKET` or `AF_VSOCK`. |
|
||||
| systemd, D-Bus | The system bus is in the hidden `/run`. No user manager: the user doesn't linger (so Hermes's cron jobs fail closed), and `/run/user` is hidden. |
|
||||
| systemd, D-Bus | The bus sockets aren't in the root. No user manager: the user doesn't linger, so Hermes's cron jobs fail closed. |
|
||||
| Escaping the unit | Descendants stay in the unit's cgroup (read-only under `ProtectControlGroups`) and stop with it, detached or not. |
|
||||
| Files | `ProtectSystem=strict`: read-only, except the state directory, a private `/tmp`, and a minimal private `/dev`. |
|
||||
| Other processes | `ProtectProc=invisible`, `ProcSubset=pid`, `PrivateIPC`. Hermes's user can still ptrace its own processes, so the bridge, which runs as that user, gets the same sandbox. |
|
||||
| Privilege | `NoNewPrivileges`, an empty `CapabilityBoundingSet`, `RestrictSUIDSGID`, and `RestrictNamespaces` (no user namespaces). |
|
||||
| Privilege | `PrivateUsers` (a user namespace mapping only the unit's own user), `NoNewPrivileges`, an empty `CapabilityBoundingSet`, `RestrictSUIDSGID`, and `RestrictNamespaces`, so it can't make namespaces of its own. |
|
||||
| Kernel interfaces | `ProtectKernelTunables`, `ProtectKernelModules`, `ProtectKernelLogs`, `ProtectClock`, `ProtectHostname`, `LockPersonality`, `SystemCallArchitectures=native`. |
|
||||
|
||||
**`host.sock`** grants what its host serves. The host must serve only the guest's methods, bound to one agent. Arguments must be data (pet names, strings, message numbers), and references must stay in the host's pet store. The socket is mode 0600, owned by Hermes's user, so the connection designates the guest. Any process as that user counts as Hermes, and all of them are confined alike.
|
||||
@@ -58,18 +59,18 @@ Python here follows [DisciplinedPython](https://codeberg.org/dckc/awesome-ocap/s
|
||||
**On macOS** the VM runs the same unit, and adds a boundary that holds even if the VM's kernel is compromised. Root in the VM reaches no network interface. It reaches the two vsock ports vfkit relays, the host's `/nix/store` read-only, and the state image. The trusted base gains Apple's hypervisor and loses the VM's kernel.
|
||||
|
||||
**Not claimed:**
|
||||
- **Secrecy.** Hermes can read what its user can read, and pass it to the model provider or to its host.
|
||||
- **Secrecy.** Hermes can read its closure and its state, and pass them to the model provider or to its host.
|
||||
- **Denial of service.** No memory, task or CPU limits are set.
|
||||
- **Kernel bugs** on a Linux host, and covert channels.
|
||||
- **Sockets the sandbox doesn't hide**, such as under `/srv`. If the host listens on one that Hermes's user may connect to, add its directory to `TemporaryFileSystem`.
|
||||
|
||||
To probe a running Linux host, enter the unit's namespaces. `nsenter` doesn't apply the seccomp filter or the capability limits, so this covers the network and file rows:
|
||||
To probe a running Linux host, look at the unit's root and network from outside:
|
||||
|
||||
```sh
|
||||
pid=$(systemctl show -p MainPID --value hermes-agent)
|
||||
sudo nsenter -t "$pid" -a -S "$(id -u hermes)" -G "$(id -g hermes)" sh -c \
|
||||
'ls -A /run /var/lib /home; ip -brief link; socat -T2 - TCP:1.1.1.1:443 </dev/null'
|
||||
sudo ls -A /proc/$pid/root /proc/$pid/root/run/hermes-guest /proc/$pid/root/var/lib
|
||||
sudo ls /proc/$pid/root/nix/store | wc -l # the closure, not the whole store
|
||||
sudo nsenter -t "$pid" -n ip -brief link
|
||||
systemd-analyze security hermes-agent hermes-llm-bridge
|
||||
```
|
||||
|
||||
Expect only `hermes-guest` in `/run`, only `hermes` in `/var/lib`, an empty `/home`, only `lo`, and a failed connection.
|
||||
Expect no `etc` or `home` in the root, the two sockets, only `hermes` in `var/lib`, and only `lo`.
|
||||
+16
-14
@@ -8,8 +8,8 @@
|
||||
# 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.
|
||||
# The service has no network of its own (PrivateNetwork), and its root
|
||||
# is empty except for its closure and those two sockets (confinement).
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
@@ -21,8 +21,17 @@ let
|
||||
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.
|
||||
# The sandbox every process running as Hermes's user gets, beside
|
||||
# `confinement` (below): an empty root, plus the unit's closure and the
|
||||
# binds each unit lists. See CONTRIBUTING.md#confinement for why it
|
||||
# confines.
|
||||
confine = extra: {
|
||||
enable = true;
|
||||
# the closure of everything the unit mentions, so PATH's packages too:
|
||||
# Hermes's shell tool runs bash, coreutils, git
|
||||
fullUnit = true;
|
||||
packages = extra;
|
||||
};
|
||||
sandbox = {
|
||||
PrivateNetwork = true;
|
||||
IPAddressDeny = "any";
|
||||
@@ -32,15 +41,6 @@ let
|
||||
"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;
|
||||
@@ -116,12 +116,13 @@ in
|
||||
systemd.services.hermes-agent = {
|
||||
after = [ "hermes-llm-bridge.service" ];
|
||||
requires = [ "hermes-llm-bridge.service" ];
|
||||
# socat: config.yaml's MCP command, which the unit itself doesn't name
|
||||
confinement = confine [ pkgs.socat ];
|
||||
serviceConfig = sandbox // {
|
||||
BindReadOnlyPaths = [
|
||||
cfg.hostSocket
|
||||
cfg.llmSocket
|
||||
];
|
||||
# the state directory, back inside the empty /var/lib
|
||||
BindPaths = [ hermes.stateDir ];
|
||||
ProtectHome = lib.mkForce true;
|
||||
};
|
||||
@@ -135,6 +136,7 @@ in
|
||||
description = "Hermes's loopback port to the inference proxy";
|
||||
unitConfig.JoinsNamespaceOf = "hermes-agent.service";
|
||||
partOf = [ "hermes-agent.service" ];
|
||||
confinement = confine [ ];
|
||||
serviceConfig = sandbox // {
|
||||
User = hermes.user;
|
||||
Group = hermes.group;
|
||||
|
||||
Reference in new issue
Block a user