Emre Şahin

Inženierijas žurnāls

A Fault Agent That Fails Safe

Designing a host agent that injects faults on real machines without ever leaving them broken: least privilege, dead-man's switch, hard caps, write-ahead cleanup.

Emre Şahin ·

Kubernetes makes chaos engineering comparatively polite. Chaos Mesh applies a custom resource, the controller does the work, and deleting the resource undoes it. The cluster is the safety net.

Bare-metal hosts and VMs have no such net. To inject a fault on a plain Linux box, something has to run on that box, with enough privilege to hurt it. That is the FaultMesh host agent, and designing it was mostly an exercise in answering one question: what happens when this goes wrong?

The threat model is mostly me

When people hear "agent with network privileges", they think about attackers. That matters, and I will come to it. But the more likely failure is mundane: the agent crashes mid-fault, the network partitions, the control plane is redeployed, an operator sends a typo. A tool designed to break things has to be very good at not breaking things it wasn't asked to.

So the design principle was: every fault must have a bounded lifetime that does not depend on the control plane, the network or the agent process staying healthy.

Outbound only

The agent never opens a port. It registers with the control plane, sends heartbeats, pulls the commands queued for it, runs them and reports back. All traffic goes from agent to control plane over HTTPS.

The alternative, a listening agent that the control plane pushes commands to, would give lower latency. It would also mean every host running the agent has a new inbound service with root-adjacent privileges. Firewalls get exceptions, security reviews get longer, and the blast radius of a bug in the agent's HTTP handling grows. Pull adds a few seconds of latency to fault start. For a chaos experiment, that is irrelevant.

Identity: enroll once, revoke individually

There is no shared API key copied onto hosts. An operator creates a short-lived enrollment token in the control plane. The agent presents it once and receives its own per-agent credential, stored locally with file mode 0600. From then on every call carries that credential.

export FAULTMESH_ENROLL_TOKEN="fmet_..."   # short-lived, first run only
faultmesh-agent --api-base https://control.internal --name web-01 \
  --label env=staging --label role=web

Each agent can be revoked on its own. A revoked agent's next heartbeat gets a 403 agent_revoked; it aborts whatever it is running and exits. Compromising one host's credential does not compromise the fleet, and turning one agent off does not require rotating anything else.

Least privilege, and advertising only what you can do

CPU and memory stress run in-process, bounded, and need no special privileges. They are on by default.

Network and IO faults are off by default and require an explicit --allow-system-faults flag. Network faults use tc netem, which needs CAP_NET_ADMIN. The agent does not need full root for that; running under systemd with AmbientCapabilities=CAP_NET_ADMIN is enough, and that is what I recommend.

The agent advertises only the faults it can actually execute right now. A network fault is advertised only on Linux, with the capability present and tc installed. The control plane's dispatch API refuses any fault the target agent did not advertise. That is fail-closed by construction: a misconfigured host cannot receive a command it would half-execute.

Hard caps on both sides

The control plane rejects commands that exceed hard limits: duration up to 3600 seconds, at most 64 CPU workers, at most 4096 MB of memory stress. The agent then clamps the same parameters again locally.

Checking twice looks redundant. It isn't. The server-side check protects against a bad experiment definition. The agent-side check protects against everything between the definition and the host: a bug in the control plane, a future API version, a compromised control plane, a replayed message. The host should not have to trust that the server got it right. Each side enforces the limits it owns.

The dead-man's switch

This is the piece I care about most. If the agent cannot reach the control plane for a grace period, it aborts every in-flight fault on its own.

The default grace period is three heartbeat intervals, with a floor of 30 seconds. Three missed heartbeats is long enough to ride out a blip, short enough that a real partition does not leave a host under stress for an hour.

Think about the failure without it. A fault is injected: 200 ms of latency on the primary interface for 30 minutes. Ten minutes in, the experiment shows something alarming, and the operator presses abort. But the reason things look alarming is that the injected latency has also degraded the agent's own connection to the control plane. The abort never arrives. The fault runs its full duration, on the host you were trying to protect.

With the dead-man's switch, losing contact is itself the abort signal. The agent does not need to receive "stop". It needs to stop hearing "continue".

Choosing the default was a trade-off. Shorter means faster safety and more spurious aborts during network blips. Longer means fewer spurious aborts and a longer window of uncontrolled fault. For network faults specifically, which are the ones most likely to cut their own control channel, I would rather lose an experiment than a host.

Cleanup that survives the agent dying

Normal cleanup runs in a finally block. When the fault ends, is aborted or is halted by the dead-man's switch, the netem qdisc is removed. That covers almost every case.

Almost. If the agent is killed hard, by SIGKILL or the OOM killer, finally never runs. The netem rule stays in the kernel. The host now has 200 ms of added latency, and nothing on it knows why.

So network faults use a small write-ahead journal. Before a qdisc is applied, the interface name is written to a journal file next to the agent's state. After the qdisc is removed, the entry is deleted. On startup, the agent reads the journal and removes netem from every interface listed there, then clears it.

apply fault:   journal.add(eth0)  →  tc qdisc add dev eth0 root netem delay 200ms
finish fault:  tc qdisc del dev eth0 root  →  journal.remove(eth0)
agent start:   for iface in journal: tc qdisc del dev iface root; journal.clear()

The ordering is what matters. Write intent before acting, clear intent after the action is undone. It is the same idea as a database write-ahead log, applied to a single kernel setting.

Two details matter in the reconciler. First, it only touches interfaces in the journal. An operator might have their own qdiscs for real traffic shaping, and the agent must never remove something it did not create. Second, it is best-effort and idempotent. If the host rebooted and the qdisc is already gone, tc returns an error, and that is fine.

What this does not cover

A dead-man's switch protects against losing contact. It does not protect against a fault that is too aggressive while contact is fine. That is the job of the control plane's safety envelope and auto-abort on SLI breach, which was still being finished when I wrote the first version of the agent. Neither layer replaces the other.

The journal covers netem. CPU and memory stress are in-process, so they die with the agent, which is the correct failure mode for them. The IO fault writes to a temp file that is removed afterwards; a hard kill can leave the file behind, which is untidy but not harmful.

The general lesson

Every fault needs at least two independent ways to end. The normal path, where someone says stop or the duration runs out. And a path that requires nothing to go right: no network, no control plane, no clean shutdown.

If you are building anything that changes live systems on purpose, a deploy tool, a feature-flag system, a traffic shifter, the same question applies. What happens if everything that is supposed to undo this disappears halfway through? If the answer is "it stays like that", that is the first thing to fix.

FaultMesh is a personal product under active development. More at faultmesh.com.