Implementation:1ay1 Agentty claybin backend
| Knowledge Sources | |
|---|---|
| Domains | Security, Operating_Systems |
| Last Updated | 2026-10-28 12:00 GMT |
Overview
Concrete wrapper around the claybin external submodule that probes host capabilities, compiles sandbox postures into kernel mechanisms, and spawns processes under OS-level containment.
Description
The claybin_backend namespace (in `tool/util/sandbox_claybin.hpp`) is agentty's interface to the claybin external submodule — the sandbox engine that compiles landlock rulesets, seccomp BPF filters, and cgroup2 limits from a declarative Posture and applies them at spawn time.
This is a Wrapper Doc: the underlying compile/spawn logic lives in claybin (external), and this header documents the repo-specific interface — how agentty probes the host, describes a posture's walls, and spawns a shell command under containment.
Host Probe
- available() — forks and attempts a uid_map write, so it predicts spawn() rather than guessing from sysctls. Probed once per process.
- landlock_abi() — returns the Landlock ABI version (0 = absent). Same cached probe.
Posture → Report
- describe(Posture) — compiles the posture and reports which walls it yields, without running anything. Returns `Report{ok, detail, walls}`.
Spawn
- spawn_shell(Posture, cmd, stdout_fd, stderr_fd) — forks, applies the sandbox, execs `/bin/sh -c cmd`. Returns `SpawnResult` with pid, pidfd, supervisor_fd (seccomp-notify broker), kill_tree callback.
- spawn_argv(Posture, argv, stdout_fd, stderr_fd) — same but with raw argv (no shell re-parsing).
Usage
- Before spawning: call `available()` to check the host can build the sandbox; `describe(posture)` to preview the walls.
- Spawning a command: call `spawn_shell(posture, cmd, out_fd, err_fd)`, then poll `supervisor_fd` for brokered syscalls.
- Killing: call the `kill_tree` callback to SIGKILL the guest and every descendant atomically (via cgroup2).
Code Reference
Source Location
- Repository: agentty
- File: include/agentty/tool/util/sandbox_claybin.hpp
- External Dependency: claybin submodule (landlock, seccomp, cgroup2)
Signature
namespace agentty::tools::util::sandbox::claybin_backend {
struct Wall {
std::string name;
std::string strength;
std::string mechanism;
};
struct Report {
bool ok{false};
std::string detail;
std::vector<Wall> walls;
};
struct SpawnResult {
bool started{false};
int pid{-1};
int pidfd{-1};
std::string start_error;
int supervisor_fd{-1}; // seccomp-notify broker
std::function<bool()> service_broker; // answer one notification
std::function<bool()> kill_tree; // SIGKILL guest + descendants
};
// Host capability probe (cached, forks once per process).
[[nodiscard]] bool available();
[[nodiscard]] std::uint32_t landlock_abi();
// Compile posture → report walls, without spawning.
[[nodiscard]] Report describe(const Posture& p);
// Fork, apply sandbox, exec /bin/sh -c shell_cmd.
[[nodiscard]] SpawnResult spawn_shell(const Posture& p,
const std::string& shell_cmd,
int stdout_fd, int stderr_fd);
// Same, with raw argv (no shell re-parsing).
[[nodiscard]] SpawnResult spawn_argv(const Posture& p,
const std::vector<std::string>& argv,
int stdout_fd, int stderr_fd,
const std::vector<std::pair<std::string,std::string>>& env = {});
} // namespace agentty::tools::util::sandbox::claybin_backend
Import
#include "agentty/tool/util/sandbox_claybin.hpp"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| p | const Posture& | Yes | Compiled sandbox policy (fs scope, net mode, syscalls, resources, masked paths) |
| shell_cmd | const std::string& | Yes (spawn_shell) | Command to run via /bin/sh -c |
| argv | const vector<string>& | Yes (spawn_argv) | Raw argv for exec |
| stdout_fd, stderr_fd | int | Yes | Caller's pipe descriptors |
Outputs
| Name | Type | Description |
|---|---|---|
| return (available) | bool | Whether host can build the sandbox |
| return (describe) | Report | Walls (name, strength, mechanism) + ok flag |
| return (spawn_*) | SpawnResult | pid, pidfd, supervisor_fd, kill_tree callback |
Usage Examples
Probing and Spawning
#include "agentty/tool/util/sandbox_claybin.hpp"
using namespace agentty::tools::util::sandbox;
// 1. Check the host can build the sandbox
if (!claybin_backend::available()) {
// Fall back to running unconfined, or refuse
return error("host does not support sandboxing");
}
// 2. Preview the walls
auto report = claybin_backend::describe(posture);
if (!report.ok) {
return error(report.detail);
}
for (const auto& w : report.walls)
log("{}: {} ({})", w.name, w.strength, w.mechanism);
// 3. Spawn a command under containment
auto result = claybin_backend::spawn_shell(
posture, "npm test", stdout_pipe, stderr_pipe);
if (!result.started) {
return error(result.start_error);
}
// 4. Poll supervisor_fd for brokered syscalls while the child runs
while (result.supervisor_fd >= 0) {
// ... poll, then:
if (!result.service_broker()) break; // listener finished
}
// 5. Kill the tree if needed
if (need_to_kill) result.kill_tree();