# Sovereign Evolution 3 — Landlock validation

## Environment (observed)

| Item | Value |
|---|---|
| Kernel | Linux 7.0.0-38-generic |
| Active LSMs | `lockdown,capability,landlock,yama,apparmor,ima,evm` |
| Landlock ABI | 8 (`landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION)`) |
| Test privileges | uid 0, but Landlock is unprivileged: `PR_SET_NO_NEW_PRIVS` + `landlock_restrict_self` need no capability |
| Container | tests run on the host, in disposable child processes over pytest `tmp_path` dirs |

## Implementation

`cain/landlock_sandbox.py` (commit bfe0637e). It is a ctypes binding to the kernel's stable syscalls 444/445/446.
The only PyPI binding, `landlock 1.0.0.dev5`, is a single-maintainer pre-release and was rejected as a dependency.
- Profile: `read_only` / `read_exec` / `read_write` path lists (resolved with `realpath`), plus optional
  `tcp_connect_ports` / `tcp_bind_ports`. Every filesystem access type the kernel knows is *handled*, so anything not
  explicitly allowed is denied. Unknown profile keys and missing paths are refused.
- Applied in the child between fork and exec. `plan()` runs in the parent first, so an unsupported kernel fails
  before any spawn. A child that cannot apply its rules writes `CAIN Landlock Fatal` and exits 131 before the
  workload starts.
- `StdioMCPProxy(landlock_profile=…)` or `CAIN_STDIO_LANDLOCK_PROFILE=<json>` applies it after the seccomp filter.
  If it's requested but not enforceable, the proxy **refuses to start the tool server**. The only exception is the
  explicit operator override `CAIN_LANDLOCK_ALLOW_UNSUPPORTED=1`, which is logged as an error.
- **Staged:** off unless a profile is configured. No production policy was changed.

## Tests (`tests/test_sovereign_e3_landlock.py`, 11 passed; deprecation warnings treated as errors: 11 passed)

Command: `python3 -m pytest -q -s -p no:cacheprovider -o addopts="" tests/test_sovereign_e3_landlock.py`, exit 0.
Measured in the restricted child (the kernel's answer, not the application's):

`{"read_allowed":"OK","read_secret":"DENIED","write_rw":"OK","remove_rw":"OK","mkdir_rw":"OK","write_ro":"DENIED","create_ro":"DENIED","remove_ro":"DENIED","mkdir_outside":"DENIED","symlink_escape":"DENIED","dotdot_escape":"DENIED","exec_system":"OK","exec_from_ro_dir":"DENIED","grandchild_read_secret":"DENIED"}`

Also tested:
- TCP connect is allowed only to the listed port (another local port DENIED).
- The parent stays unrestricted, and denied writes or removals had no effect.
- An unsupported kernel (ABI 0) is refused before spawning, and TCP rules on ABI 3 are refused rather than ignored.
- An old ABI reports `refer`/`truncate`/`ioctl_dev` as unavailable.
- A typo'd key or missing path is refused.
- A rule target that disappears between planning and applying gives child exit 131 and the workload never runs.
- **Stdio proxy end to end:** MCPGate allowed both calls. The tool server's write inside its profile succeeded, and
  its write to the secret file was refused by the kernel (`denied_by_kernel`). The proxy refuses to start when
  Landlock can't be enforced, and refuses a malformed profile.

The file is in the CI authorization gate.

## Limits (stated, not hidden)

- Landlock restricts **filesystem** access and **TCP** bind/connect only. It is **not** a syscall firewall: UDP,
  raw sockets, and most other syscalls are unaffected. The seccomp list covers only a small privilege set.
- It does not stop kernel exploits, memory-corruption attacks, side channels, or container escapes. It does not
  restrict file descriptors the process held before restricting itself.
- `exec` is controlled only through the `execute` right on paths. Any binary inside an allowed `read_exec` tree
  (for example `/usr`) can run.
- **Only the stdio tool-server path is covered.** Hosted `/mcp` tools run inside the gateway process, which is not
  restricted. Restricting the gateway itself would also restrict the gateway, and needs a separate worker-process
  design (Evolution 4).
- Profiles must be written per tool server, so a too-broad profile weakens the protection.
