Files
gitea-ci-in-gvisor/README.md
T

354 lines
12 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# gitea-ci-in-gvisor
A self-contained Gitea Actions deployment where CI jobs run in **gVisor**
sandboxes instead of sharing the host Docker socket.
It replaces the common but unsafe pattern of:
```text
act_runner container -> /var/run/docker.sock from the host
```
with:
```text
Gitea -> act_runner -> private Docker daemon -> runsc -> CI job
```
Jobs that need Docker for e2e tests get a **separate, sandboxed Docker daemon**.
They never receive the runner's privileged Docker socket.
## Architecture
### Container nesting
```text
physical host
└── dind container [runc] Docker host at tcp://dind
├── gitea container [runc]
└── runner container [runc] privileged
├── job container [runsc] gVisor sandbox
└── gvisor-dind container [runsc-net] separate gVisor sandbox
└── compose / e2e service container [runc] inside gvisor-dind's sandbox
```
Only container nesting is shown above. The runner's `act_runner` and inner
`dockerd` are processes inside the runner container, not nested containers.
### Network and Docker access
```text
job container
│ DOCKER_HOST=tcp://127.0.0.1:2375
▼
runner container loopback
▼
gvisor-dind :2375 [inside its own gVisor sandbox]
├── dockerd
└── nested e2e containers [runc, but still inside gVisor]
job container also reaches:
├── gitea:3000
├── internet
└── 127.0.0.1:2375 (the sandboxed daemon only)
```
The runner's own privileged Docker daemon listens on a Unix socket only. Jobs
have neither that socket nor a TCP API for it.
### Runtimes
```json
{
"default-runtime": "runsc",
"runtimes": {
"runsc": {
"path": "/usr/local/bin/runsc",
"runtimeArgs": ["--platform=systrap", "--network=host"]
},
"runsc-net": {
"path": "/usr/local/bin/runsc",
"runtimeArgs": [
"--platform=systrap",
"--net-raw",
"--allow-packet-socket-write"
]
}
}
}
```
- `runsc` is used for CI jobs. Jobs share the runner network namespace so they
can resolve `gitea` and reach the internet, but have no raw-socket or
packet-write permission.
- `runsc-net` is used only for `gvisor-dind`. It uses gVisor's own network stack
so Docker can create bridge networks inside its sandbox. Docker 28 needs raw
sockets and packet writes there.
- Both entries use the same `runsc` binary and `systrap` platform. They are not
different gVisor installations.
## Quick start
Requirements: Docker with the `runc` runtime, Docker Compose, and outbound
network access for images.
```bash
cp .env.example .env
# edit .env if needed
docker build -t gitea-gvisor-runner:latest runner
docker compose up -d --no-build
```
The Compose file explicitly sets `runtime: runc` for Gitea and the runner.
This matters if the target Docker host defaults to `runsc`; only the nested
sandboxes should use gVisor.
Open Gitea on `http://<docker-host>:3000` and create the first admin account,
or use the CLI:
```bash
docker compose exec -u git gitea gitea admin user create \
--username ciadmin --password 'CiAdminPass12345' \
--email ciadmin@example.com --admin --must-change-password=false
```
### Runner registration
Two options:
1. Put an explicit registration token in `.env`:
```bash
docker compose exec -u git gitea gitea actions generate-runner-token
# GITEA_RUNNER_REGISTRATION_TOKEN=...
docker compose up -d --no-build --force-recreate runner
```
2. Leave the token empty and provide an existing admin account:
```bash
GITEA_ADMIN_USER=ciadmin
GITEA_ADMIN_PASSWORD=CiAdminPass12345
```
The runner fetches a global token through the Gitea API. The token is only
used for the first registration; it persists in the `runner-data` volume.
## Runner labels
Defined in `runner/config.yaml`:
| Label | Image | Purpose |
| --- | --- | --- |
| `ubuntu-latest` | `node:22-bookworm` | bash/Node and general CI |
| `gvisor` | `alpine:3.20` | small jobs; use `shell: sh` |
| `docker` | `docker:28-cli` | Docker CLI, Compose, e2e tests |
The runner capacity is fixed at **1**. Recreate the runner after changing
labels. To force re-registration, remove `.runner` from the `runner-data`
volume.
## Tests
The test script creates a temporary Gitea repository, pushes a workflow through
the API, and waits for the result.
```bash
# Basic isolation: gVisor kernel, hidden privileged socket, sandboxed Docker API
./scripts/e2e-test.sh
# Docker inside the sandboxed daemon
WORKFLOW=scripts/workflow-docker.yml SHOW_LOGS=1 ./scripts/e2e-test.sh
# A real Compose stack inside the sandboxed daemon
WORKFLOW=scripts/workflow-compose.yml SHOW_LOGS=1 ./scripts/e2e-test.sh
# Disk, memory, and CPU stress
WORKFLOW=scripts/workflow-resource.yml SHOW_LOGS=1 ./scripts/e2e-test.sh
```
`GITEA_URL` defaults to `http://localhost:3000`. For the reference DinD test
environment:
```bash
GITEA_URL=http://dind:3000 ./scripts/e2e-test.sh
```
### Known Compose limitation
Docker's embedded DNS at `127.0.0.11` depends on Docker-managed iptables, which
is disabled inside gVisor. Nested Compose service names therefore do **not**
resolve automatically.
The Compose test uses a static IP and `extra_hosts`:
```yaml
services:
web:
networks:
testnet:
ipv4_address: 172.31.0.10
probe:
extra_hosts:
- "web:172.31.0.10"
```
For real e2e Compose files, use one of:
- `extra_hosts` with static IPs
- explicit container IPs
- host networking inside the sandbox
- a custom DNS design
## Security model
### What is protected
- No host Docker socket is mounted into jobs.
- The runner's privileged Docker daemon has no TCP listener.
- Jobs run in gVisor, not directly on the host kernel.
- The job-accessible Docker daemon (`gvisor-dind`) is itself in a separate
gVisor sandbox.
- Nested Docker containers controlled by a job remain inside that sandbox.
- Direct access from jobs to the outer Docker host gateway is rejected, except
DNS. This closes an otherwise trivial escape when the host exposes its Docker
API on the bridge address.
- Job runtime has no `--net-raw` or AF_PACKET write permission.
### Escape paths considered
| Path | Result |
| --- | --- |
| `/var/run/docker.sock` in job | absent |
| runner Docker TCP API | disabled |
| `gvisor-dind` API | reachable, but it is a separate sandbox |
| outer host gateway | blocked by iptables, DNS excepted |
| host filesystem/devices | no host bind mounts; gVisor virtualizes |
| raw packet sniffing/spoofing | disabled for jobs |
| registration token/admin password | not injected into jobs |
| resource exhaustion | **not prevented**; add quotas if needed |
| shared tool cache | **poisonable** by a malicious job; use ephemeral runners |
### Residual risk
The primary boundary is **gVisor itself**. A runsc/sentry escape could reach
the privileged runner container. Keep gVisor current, prefer KVM where
practical, and use a dedicated or disposable Docker host for high-assurance CI.
The gateway firewall only covers the default gateway address. Other host/service
IPs may still be reachable. Use a dedicated runner network, host firewall
rules, and never expose an unauthenticated Docker API to containers.
## Compatibility and limitations
### Docker versions
The runner is based on `gitea/act_runner:0.6.1-dind` (Docker 28.5.2), and the
sandboxed daemon uses `docker:28-dind`. This deliberately avoids Docker 29's
containerd-snapshotter/overlayfs complications.
If upgrading to Docker 29+:
- disable the containerd snapshotter with
`--feature containerd-snapshotter=false` and verify overlay2 on a disk-backed
volume, or
- use `vfs` if overlay2 still cannot mount, or
- use a RAM-backed tmpfs only if bounded image/build storage is acceptable.
The deployment uses a real disk volume for `/var/lib/docker`; arbitrary job
storage should not be capped by runner RAM.
### gVisor cgroups
`runsc` needs to enable cgroup controllers on the root of its cgroup namespace.
On cgroup v2, a root containing processes cannot do that. The runner entrypoint
moves PID 1 into a child cgroup so the namespace root is empty. Docker CPU and
memory limits then apply to the sandbox.
On cgroup v1 this step is a no-op.
### Nested Docker networking
`gvisor-dind` starts Docker with `--iptables=false --ip6tables=false`.
`runner/start-dind.sh` adds a manual SNAT rule so nested containers have
outbound internet.
Consequences:
- Docker-managed NAT and embedded DNS are unavailable.
- Bridge networking works inside gVisor's own network stack.
- Nested containers need IPs/`extra_hosts`/host networking for service names.
### Performance
Rough numbers from the reference DinD host (16 vCPU, 30 GiB RAM). These are
short microbenchmarks, not guarantees:
| Workload | `runc` baseline | gVisor job | Nested Docker under `gvisor-dind` |
| --- | ---: | ---: | ---: |
| 2 GiB write | 2.7 GB/s | 2.5 GB/s | 2.4 GB/s |
| 2 GiB read | 18.6 GB/s | ~14 GB/s | 13.4 GB/s |
| 2 GiB memory touch | ~1.9 s | ~2–3 s | ~2.0 s |
| BusyBox awk 10M loop | 5.18 s | ~9.3 s | 5.25 s |
I/O and memory are close to native. CPU/syscall-heavy work pays gVisor overhead.
Benchmark the actual CI image rather than extrapolating.
## Known workarounds and their reasons
These are intentional but not invisible:
1. **PID 1 moved into a child cgroup** — required for cgroup v2 + runsc.
2. **Custom s6 readiness wait** — stock notify-on-check can time out during slow
nested Docker startup.
3. **Custom dockerd wrapper** — keeps the privileged runner daemon on a Unix
socket only; the stock dind entrypoint adds TCP.
4. **Manual SNAT** — Docker iptables cannot initialize inside gVisor.
5. **Loopback TCP for the sandboxed daemon** — Unix socket files created inside
a gVisor sandbox cannot usefully be shared through a Docker volume.
6. **Separate `runsc` / `runsc-net` runtimes** — Docker-in-gVisor needs raw
socket support and gVisor's own network stack, while jobs do not.
7. **Docker 28 pin** — avoids Docker 29 containerd-snapshotter/overlayfs
incompatibilities.
8. **Gateway reject rule** — blocks the outer Docker host API, which was
reachable from the shared runner network namespace.
9. **Static IP + `extra_hosts` in the Compose test** — works around missing
nested Docker DNS.
10. **`docker build` instead of `docker compose build` in the test script** —
the test environment's Compose plugin is older than its buildx plugin.
## Repository layout
```text
.
├── docker-compose.yml Gitea + privileged runner
├── .env.example
├── runner/
│ ├── Dockerfile act_runner + runsc + sandboxed dind setup
│ ├── daemon.json runsc and runsc-net runtime entries
│ ├── config.yaml labels, capacity, DOCKER_HOST
│ ├── gvisor-entrypoint.sh cgroup setup, token fetch, gateway block
│ ├── docker-run.sh runner dockerd, Unix socket only
│ ├── act-runner-run.sh wait for Docker, start sandboxed daemon
│ ├── start-sandboxed-dind.sh launch gvisor-dind under runsc-net
│ └── start-dind.sh gVisor-aware dockerd startup + SNAT
└── scripts/
├── e2e-test.sh API-driven end-to-end test
├── workflow-gvisor.yml basic isolation
├── workflow-docker.yml Docker-in-sandbox test
├── workflow-compose.yml Compose-in-sandbox test
└── workflow-resource.yml disk/memory/CPU stress
```
## Production hardening checklist
- Do not expose the host Docker API over TCP to the runner network.
- Keep Gitea, the runner, and the outer Docker host patched.
- Keep gVisor current; use KVM where practical.
- Put the runner on a dedicated network/host.
- Add per-job CPU/memory/disk limits if workflows are untrusted.
- Use ephemeral runners or clear `act-toolcache` between tenants.
- Treat the runner container itself as privileged infrastructure, not as a
security boundary for the host.