diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 0bf01e581..910541a4e 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -76,8 +76,7 @@ updates: # both. - package-ecosystem: "gomod" directories: - - "/src/agent_env/computers/artifacts/ubuntu-24.04/egress" - - "/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress" + - "/src/agent_env/utils/egress_proxy" schedule: interval: "weekly" open-pull-requests-limit: 5 diff --git a/.github/scripts/infra_build.py b/.github/scripts/infra_build.py index 0201208cd..d1cee0cc6 100644 --- a/.github/scripts/infra_build.py +++ b/.github/scripts/infra_build.py @@ -33,8 +33,10 @@ import platform import posixpath import re +import shutil import subprocess import sys +import tempfile import time import tomllib from collections.abc import Callable @@ -42,6 +44,8 @@ REGISTRY = "ghcr.io/scaleapi" REPO = Path(__file__).resolve().parents[2] +# What an image's shared_sources.toml names is under here, as agent_env.utils.paths.SOURCES is. +SOURCES = REPO / "src" / "agent_env" # Each image agent-env ships, by name, and the Dockerfile it's built from, with its folder as the context. IMAGES = { "agentenv-gateway": "src/agent_env/env/gateway/Dockerfile", @@ -226,12 +230,31 @@ def _build(entry: str, arch: str) -> int: for arg, value in {"ROOTFS": rootfs, "TOOLS": args["TOOLS"], "SIZE_GB": args["SIZE_GB"], "ROOTFS_CONFIG": _config(rootfs)}.items(): build_args += ["--build-arg", f"{arg}={value}"] - command = ["docker", "build", "--platform", target, "--provenance=false", "-f", image["dockerfile"], - "-t", f"{_ref(image)}-{arch}", *build_args, - "--label", f"org.opencontainers.image.source=https://github.com/{os.environ['GITHUB_REPOSITORY']}", - "--label", f"org.opencontainers.image.revision={os.environ['GITHUB_SHA']}", - "--label", f"io.agentenv.source-tag={image['tag']}", image["context"]] - return _tried_again(lambda: _streamed(command)).returncode + with tempfile.TemporaryDirectory(prefix="agentenv-context-") as staged: + context, dockerfile = image["context"], image["dockerfile"] + if (Path(context) / "shared_sources.toml").is_file(): + context = _staged(image["context"], staged) + dockerfile = os.path.join(context, os.path.relpath(image["dockerfile"], image["context"])) + command = ["docker", "build", "--platform", target, "--provenance=false", "-f", dockerfile, + "-t", f"{_ref(image)}-{arch}", *build_args, + "--label", f"org.opencontainers.image.source=https://github.com/{os.environ['GITHUB_REPOSITORY']}", + "--label", f"org.opencontainers.image.revision={os.environ['GITHUB_SHA']}", + "--label", f"io.agentenv.source-tag={image['tag']}", context] + return _tried_again(lambda: _streamed(command)).returncode + + +def _staged(context: str, staged: str) -> str: + """``context`` copied into ``staged`` with what its ``shared_sources.toml`` takes from agent-env's sources, each at + the name it gives, as agent-env's build contexts send it (``agent_env.utils.docker.context``).""" + shutil.copytree(context, staged, dirs_exist_ok=True) + declared = Path(context) / "shared_sources.toml" + for name, source in (tomllib.loads(declared.read_text()) if declared.is_file() else {}).items(): + path = SOURCES / source + if path.is_dir(): + shutil.copytree(path, Path(staged) / name) + else: + shutil.copy2(path, Path(staged) / name) + return staged def _publish(missing: str) -> int: diff --git a/.github/workflows/clock.yml b/.github/workflows/clock.yml index e34488eff..43bd1acd5 100644 --- a/.github/workflows/clock.yml +++ b/.github/workflows/clock.yml @@ -1,6 +1,6 @@ name: Clock gates -# agent-env's libfaketime (src/agent_env/computers/artifacts/ubuntu-24.04/clock), the library every computer's session programs load to read +# agent-env's libfaketime (src/agent_env/utils/faketime_clock), the library every computer's session programs load to read # universe time, built with stock 0.9.12 beside it in an Ubuntu 24.04 image and run through tst/clock's gates on x86_64 # and arm64: every wait API in C, Python, Java and Node at several rates, jumps, cancelled waits, timerfd duplicates, # condvar and semaphore stress, the waits again with futex_waitv refused, a stalled writer and a dead one, torn page @@ -10,7 +10,7 @@ name: Clock gates on: pull_request: paths: - - "src/agent_env/computers/artifacts/ubuntu-24.04/clock/**" + - "src/agent_env/utils/faketime_clock/**" - "src/agent_env/browsers/envs/chromium/chromium.py" - "tst/clock/**" - ".github/workflows/clock.yml" @@ -52,7 +52,7 @@ jobs: uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: path: ${{ env.AGENTENV_TEST_CLOCK_CACHE }} - key: clock-downloads-${{ matrix.arch }}-${{ hashFiles('src/agent_env/browsers/envs/chromium/chromium.py', 'src/agent_env/computers/artifacts/ubuntu-24.04/clock/build.sh', 'tst/clock/conftest.py') }} + key: clock-downloads-${{ matrix.arch }}-${{ hashFiles('src/agent_env/browsers/envs/chromium/chromium.py', 'src/agent_env/utils/faketime_clock/build.sh', 'tst/clock/conftest.py') }} - name: Clock gates run: .venv/bin/python -m pytest tst/clock -rA --timeout=2400 --junitxml=junit-clock-${{ matrix.arch }}.xml - name: No skips diff --git a/.github/workflows/local-backends.yml b/.github/workflows/local-backends.yml index 20149e378..8c9253afc 100644 --- a/.github/workflows/local-backends.yml +++ b/.github/workflows/local-backends.yml @@ -26,7 +26,7 @@ env: PIP_DISABLE_PIP_VERSION_CHECK: "1" jobs: - # The base computer's egress proxy (src/agent_env/computers/artifacts/ubuntu-24.04/egress/): gofmt, go vet and its + # The base computer's egress proxy (src/agent_env/utils/egress_proxy/): gofmt, go vet and its # tests under the race detector, in the Go image its Dockerfile builds with, so a failing Go test fails here rather # than as a broken base image in the slow tier. egress: diff --git a/AGENTS.md b/AGENTS.md index a2d1a8c1e..a8f5f8eba 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -44,8 +44,8 @@ or one of them only where it is used. | `make int-test-fast` | `tst/integration` minus `int_test_slow`, in parallel; needs Docker and a local OCI registry (`docker run -d -p 5000:5000 public.ecr.aws/docker/library/registry:2`) | | `make int-test-slow` | the `int_test_slow` tests, serially (they build the same Docker tags from module fixtures and race under xdist) | | `make installer-test` | `tst/installer`: `plugin add` / `remove` through the real pip, uv and pipx, offline against wheels built from the checkout, and the container user journey (`python:3.12-slim`, `--network none`) with the PEP 668 refusal; needs uv, pipx and Docker | -| `make clock-test` | `tst/clock`: agent-env's libfaketime (`src/agent_env/computers/artifacts/ubuntu-24.04/clock`) and stock 0.9.12 built in an Ubuntu 24.04 image for this machine's Docker, and its gates run there: every wait API in C, Python, Java and Node at several rates, jumps, cancelled waits, timerfd duplicates, condvar and semaphore stress, the waits again with `futex_waitv` refused (as before Linux 5.16 and under gVisor), a stalled writer, a dead one, torn reads, the marker, the environment race, forks while another thread closes a timerfd, the TLS shim's certificate checks on real time (curl, Python and `openssl s_client` at 182 days back: live sites verify, expired and untrusted certificates don't; and two copies of OpenSSL in one process, the system's 3.0 and a 1.1, each checking with its own), and Chromium launched 40 times with it and 10 with stock, side by side, lateness held to `tst/clock/bounds.py`; needs Docker, and fetches Chrome's archive, upstream's tarball and OpenSSL 1.1.1w's once into `AGENTENV_TEST_CLOCK_CACHE` | -| `make egress-test` | the base computer's egress proxy (`src/agent_env/computers/artifacts/ubuntu-24.04/egress`, Go): `gofmt`, `go vet` and `go test -race` in the Go image its Dockerfile builds with; needs Docker, not Go | +| `make clock-test` | `tst/clock`: agent-env's libfaketime (`src/agent_env/utils/faketime_clock`) and stock 0.9.12 built in an Ubuntu 24.04 image for this machine's Docker, and its gates run there: every wait API in C, Python, Java and Node at several rates, jumps, cancelled waits, timerfd duplicates, condvar and semaphore stress, the waits again with `futex_waitv` refused (as before Linux 5.16 and under gVisor), a stalled writer, a dead one, torn reads, the marker, the environment race, forks while another thread closes a timerfd, the TLS shim's certificate checks on real time (curl, Python and `openssl s_client` at 182 days back: live sites verify, expired and untrusted certificates don't; and two copies of OpenSSL in one process, the system's 3.0 and a 1.1, each checking with its own), and Chromium launched 40 times with it and 10 with stock, side by side, lateness held to `tst/clock/bounds.py`; needs Docker, and fetches Chrome's archive, upstream's tarball and OpenSSL 1.1.1w's once into `AGENTENV_TEST_CLOCK_CACHE` | +| `make egress-test` | the base computer's egress proxy (`src/agent_env/utils/egress_proxy`, Go): `gofmt`, `go vet` and `go test -race` in the Go image its Dockerfile builds with; needs Docker, not Go | | `make trl-test` | `tst/trl`: one `GRPOTrainer` step on CPU through `agent_env.training.trl`, with a tiny model from the Hub; needs trl and CPU torch, which the `dev` extra leaves out | | `make nemo-gym-test` | `tst/nemo_gym`: rollouts through `agent_env.training.nemo_gym`'s resources server in-process; needs the `nemo-gym` extra, on Python 3.13.14 or later, which the `dev` extra leaves out (`uv sync --extra dev --extra nemo-gym`) | | `make verl-test` | `tst/verl`: rows played through `agent_env.training.verl`'s agent loop on verl itself, with Qwen3's tokenizer from the Hub and a stand-in rollout server; needs verl and CPU torch, which the `dev` extra leaves out | @@ -139,7 +139,8 @@ and deserialized through a registry. | `explorer/` | Optional local web UI: `agent-env up`, binds loopback `:8234`. | | `cli/` | Click CLI with the groups `a2a-agent`, `artifact`, `bundle`, `config`, `env`, `eval`, `plugin`, `run`, `task`, `up`. `task run`, `task run-batch` and `eval run` hand their runs to the configured `[runner]` and follow them. | | `agents/` | The `agents` bundle: the agents agent-env ships, one folder each under `agents/agents/`, with a smoke task each under `agents/tasks/`. Its Claude Code, `@local/agentenv-framework/agents/claude-code`, is the default agent. An agent's Python packages are its folder's `requirements.txt`, installed in its image, never in agent-env's own environment. A run that pulls one of its agents reads the whole bundle, so `tst/unit/bundle/shipped_bundles_test.py` parses and checks every shipped bundle. | -| `computers/` | The `computers` bundle: the base computers agent-env ships as `computer` artifacts, `ubuntu-24.04` (XFCE on Xvfb, settings as `AGENTENV_SETTING_`; it starts as root, its container form's `start_as`, whose supervisord, `root.conf`, runs the clock follower as `agentenv-clock`, the egress proxy as `agentenv-egress` (`egress/`, a Go program its Dockerfile builds and tests, on real time, through which every session program goes out, so a live site loads at any universe date: it passes a site's TLS through when the universe is inside the site's certificate, else re-signs it with the computer's own CA, minted at boot and valid from 1950 to 9999, handshaking with the site as the client did, and refuses any destination that isn't global) and the session's supervisord as the session user, which runs every session program; `root/` installs them at the build, `install.sh`, and starts them at boot, `init`; `ubuntu-24.04-browser`, the base of agent-env's website browsers, has copies of `clock/`, `egress/`, `root/` and `root.conf` that a unit test keeps identical), and `omarchy-4.5`, a disk alone built on the composing machine from Omarchy's own VM image (its `[forms.disk]` `source`) by `disk/omarchy/`'s recipe, its services systemd user units of the graphical session that start through `with-seed`, booted by the launcher as a `linux-desktop` guest (`env/envs/computer/utils/omarchy_disk.py`, on `thin_disk.py`, which Windows' disks use too), aliased through `[artifacts]`. It has no tasks either. `disk/linux/` is the Linux disk form's recipe: `boot/`, whose Dockerfile makes a disk form (a GPT disk of an EFI system partition holding one UKI, Ubuntu's kernel with a busybox initramfs whose init reads a NoCloud seed and `switch_root`s into the image's entrypoint, and an ext4 root filesystem that is a container image's, plus `/etc/agentenv/vm/image.env` from its OCI config), `tools/`, the disk tools that make it unprivileged on the builder's own platform (`mkfs.ext4 -d`), and `launcher/`, the QEMU launcher that boots one under KVM. A base's `[forms.disk]` with no image is made from its container form; the composer makes each arch's disk from its composed container, keeping the base disk's EFI system partition, keyed by the container's key, the base disk and the recipe; gate 3 probes it where a provider boots disks natively (`disk_image_platforms()`), and a deploy's first boot with KVM probes one left `unverified`; a deploy naming `form = "disk"` boots it through `create_vm(image=..., env=...)`, its settings in the seed. `artifacts/ubuntu-24.04/clock/` is the computer's clock, in the base's folder since an image builds only from its own: agent-env's build of libfaketime 0.9.12 (`libfaketime.patch` and the two `agentenv_clock_*.c` files it includes, GPL-2.0-only as libfaketime is, built by `build.sh` from upstream's sha256-pinned tarball, in a stage of the base's Dockerfile of its own) and the TLS shim, which `/etc/ld.so.preload` loads into every process and which fake only the session user's: clock reads and waits for a set clock time follow the page the follower (`clock-follower`) writes (`clockpage.py`), monotonic and relative waits staying real, with its default settings (`clock.conf`); `tst/clock` gates it. The follower long-polls the gateway's `/clock/watch` under the config core writes through the root channel when a multi env deploys the computer and after a reset (`env/envs/computer/utils/clock.py`), each time waiting on the computer's `sync_time` until it follows, a warning naming why when it doesn't (arming the clock refuses such a computer), and the driver serves `clock/v1` `sync_time` where the follower is (`agentenv_protocol.computer.clock`), not on a disk guest, whose follower core can't reach. | +| `computers/` | The `computers` bundle: the base computers agent-env ships as `computer` artifacts, `ubuntu-24.04` (XFCE on Xvfb, settings as `AGENTENV_SETTING_`; it starts as root, its container form's `start_as`, whose supervisord, `root.conf`, runs the clock follower as `agentenv-clock`, the egress proxy as `agentenv-egress` (`utils/egress_proxy/`, a Go program its Dockerfile builds and tests, on real time, through which every session program goes out, so a live site loads at any universe date: it passes a site's TLS through when the universe is inside the site's certificate, else re-signs it with the computer's own CA, minted at boot and valid from 1950 to 9999, handshaking with the site as the client did, and refuses any destination that isn't global) and the session's supervisord as the session user, which runs every session program; `root/` installs them at the build, `install.sh`, and starts them at boot, `init`; `ubuntu-24.04-browser`, the base of agent-env's website browsers, which takes the same clock and egress proxy, and ubuntu-24.04's `root/` and `root.conf`, through its `shared_sources.toml`), and `omarchy-4.5`, a disk alone built on the composing machine from Omarchy's own VM image (its `[forms.disk]` `source`) by `disk/omarchy/`'s recipe, its services systemd user units of the graphical session that start through `with-seed`, booted by the launcher as a `linux-desktop` guest (`env/envs/computer/utils/omarchy_disk.py`, on `thin_disk.py`, which Windows' disks use too), aliased through `[artifacts]`. It has no tasks either. `disk/linux/` is the Linux disk form's recipe: `boot/`, whose Dockerfile makes a disk form (a GPT disk of an EFI system partition holding one UKI, Ubuntu's kernel with a busybox initramfs whose init reads a NoCloud seed and `switch_root`s into the image's entrypoint, and an ext4 root filesystem that is a container image's, plus `/etc/agentenv/vm/image.env` from its OCI config), `tools/`, the disk tools that make it unprivileged on the builder's own platform (`mkfs.ext4 -d`), and `launcher/`, the QEMU launcher that boots one under KVM. A base's `[forms.disk]` with no image is made from its container form; the composer makes each arch's disk from its composed container, keeping the base disk's EFI system partition, keyed by the container's key, the base disk and the recipe; gate 3 probes it where a provider boots disks natively (`disk_image_platforms()`), and a deploy's first boot with KVM probes one left `unverified`; a deploy naming `form = "disk"` boots it through `create_vm(image=..., env=...)`, its settings in the seed. `utils/faketime_clock/` is the computer's clock, which each base takes through its `shared_sources.toml`: agent-env's build of libfaketime 0.9.12 (`libfaketime.patch` and the two `agentenv_clock_*.c` files it includes, GPL-2.0-only as libfaketime is, built by `build.sh` from upstream's sha256-pinned tarball, in a stage of the base's Dockerfile of its own) and the TLS shim, which `/etc/ld.so.preload` loads into every process and which fake only the session user's: clock reads and waits for a set clock time follow the page the follower (`clock-follower`) writes (`clockpage.py`), monotonic and relative waits staying real, with its default settings (`clock.conf`); `tst/clock` gates it. The follower long-polls the gateway's `/clock/watch` under the config core writes through the root channel when a multi env deploys the computer and after a reset (`env/envs/computer/utils/clock.py`), each time waiting on the computer's `sync_time` until it follows, a warning naming why when it doesn't (arming the clock refuses such a computer), and the driver serves `clock/v1` `sync_time` where the follower is (`agentenv_protocol.computer.clock`), not on a disk guest, whose follower core can't reach. | +| `utils/faketime_clock/`, `utils/egress_proxy/` | Sources several shipped images share, each built into the image from its own context: `faketime_clock/` (agent-env's libfaketime, the TLS shim, the clock page and the clock follower) and `egress_proxy/` (the egress proxy). An image's folder names what it takes in its `shared_sources.toml` (`clock = "utils/faketime_clock"`, paths under `src/agent_env`, at the name its Dockerfile copies from), and its build context (`utils/docker/context.py`, and release CI's `infra_build.py`) holds each at that name, so the image still builds from one self-contained context and its source hash covers what it takes. A fix lands once, in every image that takes it. | | `browsers/` | The `browsers` bundle: the website browsers agent-env ships, today `chromium`, a `computer_app` env (Chrome for Testing at the build Playwright pins, for amd64 and arm64, set up for unattended use by managed policies in `/etc/opt/chrome_for_testing/policies/managed`, opened at boot on its `start_urls`, its profile its data). All of it, its pin, install step, policies and command, is in `envs/chromium/chromium.py`; `tst/integration/computer/chromium_app_test.py` runs it. Like `drivers`, it has no tasks. | | `drivers/` | The `drivers` bundle: the computer drivers agent-env ships as `computer_driver` envs, the reference driver (X11 on Linux, recording through `x11grab`) aliased `reference`, the page driver aliased `page`, a browser computer's, whose screen is the browser's active tab and which serves Playwright MCP's `browser_*` tools beside `computer_*` from a Playwright MCP it runs attached to the browser over a CDP address the relay issues it (`browser_evaluate` and `browser_run_code_unsafe` hidden by default; each call an entry in the browser's log, begun and ended with `browser/v1` `calls/begin` and `calls/end`, that its CDP commands name as `for`; `scripts/upgrade_playwright_mcp.py` pins its version), and trycua's cua-driver aliased `cua-driver` (its daemon from the pinned PyPI wheel, its own instructions, tools and skill resources served to agents through `@server_instructions`, `forwarded_tools` and `@resource`, its tools beside `computer_*`, `computer/v1` on its desktop-scope calls, its skill pack leading the computer skill; `tst/integration/computer/cua_driver_test.py` runs it). It has no tasks: a bundle with neither tasks nor evals is refused unless its `aliases.toml` names something for other bundles to use. The shipped `desktop` example composes it with the `ubuntu-24.04` base into a computer env, which `tst/integration/computer/` deploys, probes, records and has an agent use. | | `examples/` | The example bundles agent-env ships (`hello`, and `echo-mcp`, which needs Docker, and `desktop`, a computer env of `ubuntu-24.04` and `reference` with a probe task and an agent task). Tests run them, but they are examples for users, not fixtures. Every shipped bundle is registered under `agent_env.bundles` in `pyproject.toml`, ` = ""` shipping `src///`. | diff --git a/Makefile b/Makefile index 56fa11de7..499ad0be6 100644 --- a/Makefile +++ b/Makefile @@ -66,15 +66,15 @@ installer-test: ## Run plugin add/remove through the real pip, uv and pipx, and # The TRL contract tier trains one GRPOTrainer step on CPU through agent_env.training.trl, with a tiny model from the # Hub. trl and torch aren't in the dev extra: install them first, as .github/workflows/trl.yml does. -# The clock tier builds agent-env's libfaketime (src/agent_env/computers/artifacts/ubuntu-24.04/clock) and stock 0.9.12 +# The clock tier builds agent-env's libfaketime (src/agent_env/utils/faketime_clock) and stock 0.9.12 # in an Ubuntu 24.04 image for this machine's Docker and runs its gates there, Chromium launches included; # .github/workflows/clock.yml runs it on x86_64 and arm64. clock-test: ## Build agent-env's libfaketime and run its gates in Docker (tst/clock) $(PYTHON) -m pytest tst/clock --timeout=2400 $(PYTEST_ARGS) -# The base computer's egress proxy, in the Go image its Dockerfile builds it with, so it needs Docker, not Go. -EGRESS := src/agent_env/computers/artifacts/ubuntu-24.04/egress -EGRESS_GO_IMAGE = $(shell awk '/^FROM .*golang/ {print $$2; exit}' $(EGRESS)/../Dockerfile) +# The egress proxy (utils/egress_proxy), in the Go image ubuntu-24.04's Dockerfile builds it with, so it needs Docker, not Go. +EGRESS := src/agent_env/utils/egress_proxy +EGRESS_GO_IMAGE = $(shell awk '/^FROM .*golang/ {print $$2; exit}' src/agent_env/computers/artifacts/ubuntu-24.04/Dockerfile) egress-test: ## gofmt, go vet and go test -race for the base computer's egress proxy, in Docker docker run --rm -v "$(CURDIR)/$(EGRESS):/src" -w /src $(EGRESS_GO_IMAGE) \ diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md index fe86e7dd8..12ef9de6c 100644 --- a/THIRD_PARTY_NOTICES.md +++ b/THIRD_PARTY_NOTICES.md @@ -2546,7 +2546,7 @@ Platform-specific native binaries that npm installs for the machine running the ## libfaketime -`src/agent_env/computers/artifacts/ubuntu-24.04/clock/` holds agent-env's changes to [libfaketime](https://github.com/wolfcw/libfaketime) +`src/agent_env/utils/faketime_clock/` holds agent-env's changes to [libfaketime](https://github.com/wolfcw/libfaketime) 0.9.12: `libfaketime.patch`, `agentenv_clock_page.c` and `agentenv_clock_waits.c`. Being derived from libfaketime, those three files are licensed as it is, under the GNU General Public License version 2 (`GPL-2.0-only`); the folder's `COPYING` is that license's text. libfaketime's own source is not in the repository: the folder's `build.sh` downloads @@ -2558,7 +2558,7 @@ The folder's other files, like the rest of the repository, are licensed under th ## The egress proxy's Go modules -`src/agent_env/computers/artifacts/ubuntu-24.04/egress/` is agent-env's egress proxy, a Go program the base +`src/agent_env/utils/egress_proxy/` is agent-env's egress proxy, a Go program the base computer's Dockerfile builds, statically linked with the Go standard library and the modules below. Their source is not in the repository: the folder's `go.sum` pins each, the build downloads them, and the program runs inside computers. diff --git a/src/agent_env/bundle/ledger.py b/src/agent_env/bundle/ledger.py index 3a144e990..f519124d3 100644 --- a/src/agent_env/bundle/ledger.py +++ b/src/agent_env/bundle/ledger.py @@ -31,6 +31,7 @@ from agent_env.store.ids import local_namespace from agent_env.store.local_state import holding_locks from agent_env.task.store import TASKS_COLLECTION +from agent_env.utils.docker.context import shared_files from .authoring import build_context_files from .parse import BundleKind @@ -146,6 +147,9 @@ def digest(self, write: Write, needs: Mapping[tuple[str, str], int]) -> Digest | listing = build_context_files if built else folder_files for key, path in listing(self._plan.bundle_of(write.source.entry), write.source.entry).items(): inputs["files"][key] = _file_sha256(path) + (_mode(path) if built else "") + if built: + for key, path in shared_files(write.source.entry.path).items(): + inputs["files"][key] = _file_sha256(path) + _mode(path) if (published := self._plan.published.get(write.id)) is not None: inputs["published"] = published.pinned if write.requires: @@ -248,10 +252,11 @@ def _tracked(write: Write) -> bool: """Whether the ledger can list everything ``write`` is made from. Not yet for an env type with a ``from_toml`` of its own, which may read its folder in ways it can't see. An artifact, a skill included, is made from its toml, every file in its folder (``folder_files``) and what it references; an agent or env from its - toml and what it references. A built image is made from its build context, ``build_context_files``, and the - image published for it when there is one (``Plan.published``); what the build fetches (its base image, packages) - isn't an input. A referenced image is made from its ref's text, so a tag - that moves in its registry isn't a change. An image folder's image is also made from its description.""" + toml and what it references. A built image is made from its build context, ``build_context_files`` and what + its ``shared_sources.toml`` takes from agent-env's sources (``shared_files``), and the image published for it when + there is one (``Plan.published``); what the build fetches (its base image, packages) isn't an input. A referenced + image is made from its ref's text, so a tag that moves in its registry isn't a change. An image folder's image is + also made from its description.""" if isinstance(write.source, BundleImage): return True if write.kind in (BundleKind.ARTIFACT, BundleKind.SKILL): diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/root.conf b/src/agent_env/computers/artifacts/ubuntu-24.04-browser/root.conf deleted file mode 100644 index 97980b543..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/root.conf +++ /dev/null @@ -1,59 +0,0 @@ -; Root's supervisord, the same in every base that starts as root (a unit test keeps the copies identical), which the -; root start (root/init) runs once it has made what the session must not touch: the clock follower and the egress -; proxy, each as its own user, on real time, and the session's supervisord (supervisord.conf) as the session user, which -; runs every session program. Its socket and logs are root's alone, so the session user can neither stop nor restart -; any of them. The session's supervisord logs to the console, so a disk form's console shows each session program -; start. The follower follows nothing until core writes its config, then picks it up within a quarter second. The -; egress proxy listens where AGENTENV_EGRESS_PROXY names, else on 127.0.0.1:3128, and logs a JSON line per connection. -[supervisord] -nodaemon=true -user=root -logfile=/var/log/agentenv-root/supervisord.log -pidfile=/run/agentenv-root/supervisord.pid -childlogdir=/var/log/agentenv-root - -[unix_http_server] -file=/run/agentenv-root/supervisor.sock -chmod=0700 - -[rpcinterface:supervisor] -supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface - -[supervisorctl] -serverurl=unix:///run/agentenv-root/supervisor.sock - -[program:clock] -command=/usr/bin/python3 -I /usr/local/lib/agentenv-clock/clock-follower -user=agentenv-clock -directory=/ -priority=3 -autorestart=true -startsecs=1 -startretries=1000 -redirect_stderr=true -stdout_logfile=/var/log/agentenv-root/clock.log -environment=HOME="/nonexistent",USER="agentenv-clock",LOGNAME="agentenv-clock" - -[program:egress] -command=/usr/local/lib/agentenv-egress/egress serve -user=agentenv-egress -directory=/ -priority=3 -autorestart=true -startsecs=1 -startretries=1000 -redirect_stderr=true -stdout_logfile=/var/log/agentenv-root/egress.log -environment=HOME="/nonexistent",USER="agentenv-egress",LOGNAME="agentenv-egress" - -[program:session] -command=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf -user=user -directory=/home/user -priority=10 -autorestart=true -stopwaitsecs=30 -redirect_stderr=true -stdout_logfile=/dev/stdout -stdout_logfile_maxbytes=0 -environment=HOME="/home/user",USER="user",LOGNAME="user" diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/root/init b/src/agent_env/computers/artifacts/ubuntu-24.04-browser/root/init deleted file mode 100755 index 3645bf73d..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/root/init +++ /dev/null @@ -1,18 +0,0 @@ -#!/bin/sh -# The root start, the same in every base that starts as root (a unit test keeps the copies identical), run by the base's -# init as root at the computer's start: it sets the time zone a deploy passes as AGENTENV_SETTING_TIMEZONE for every -# program (docker exec's included), makes what the session must not touch (the clock page and file, the clock user's -# from their first byte; the egress proxy's CA, its key the egress user's alone, trusted everywhere), then starts root's -# supervisord (root.conf), which runs the clock follower and the egress proxy, each as its own user, and the session's -# supervisord as the session user. -set -eu -timezone="${AGENTENV_SETTING_TIMEZONE:-UTC}" -[ -f "/usr/share/zoneinfo/$timezone" ] || { echo "timezone '$timezone' isn't in /usr/share/zoneinfo" >&2; exit 2; } -[ "$(id -u)" = 0 ] || { echo "init starts as root, not uid $(id -u)" >&2; exit 2; } -export TZ="$timezone" -ln -sfn "/usr/share/zoneinfo/$timezone" /etc/localtime && echo "$timezone" > /etc/timezone -install -d -o agentenv-clock -g agentenv-clock -m 0755 /run/agentenv-clock -python3 -I /usr/local/lib/agentenv-clock/clockpage.py create agentenv-clock -/usr/local/lib/agentenv-egress/egress boot > /var/log/agentenv-root/egress-boot.log 2>&1 \ - || { cat /var/log/agentenv-root/egress-boot.log >&2; echo "the egress proxy's boot failed" >&2; exit 2; } -exec /usr/bin/supervisord -c /etc/supervisor/root.conf diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/root/install.sh b/src/agent_env/computers/artifacts/ubuntu-24.04-browser/root/install.sh deleted file mode 100644 index b2c14d3f1..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/root/install.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/sh -# Installs the root start, as root at the image's build, the same in every base that starts as root (a unit test keeps -# the copies identical), once the Dockerfile has copied the clock's and the egress proxy's files into place: their users, -# root's folders and the clock's config folder, the session user's to neither read nor write, agentenv's trust files, -# and the clock library, preloaded into every process. $1 is the session user. -set -eu -session=$1 -for file in /usr/local/lib/agentenv-clock/libfaketimeMT.so.1 /usr/local/lib/agentenv-clock/libtls_realtime.so \ - /usr/local/lib/agentenv-clock/clockpage.py /usr/local/lib/agentenv-clock/clock-follower \ - /etc/agentenv/clock.conf /usr/local/lib/agentenv-egress/egress /usr/local/lib/agentenv-root/init; do - [ -f "$file" ] || { echo "the root start needs $file, which the base's Dockerfile copies" >&2; exit 2; } -done -useradd --system --no-create-home --home-dir /nonexistent --shell /usr/sbin/nologin agentenv-clock -useradd --system --no-create-home --home-dir /nonexistent --shell /usr/sbin/nologin agentenv-egress -mkdir -p /var/log/agentenv-root /run/agentenv-root /etc/agentenv/clock -chmod 0700 /var/log/agentenv-root /run/agentenv-root -chown root:agentenv-clock /etc/agentenv/clock && chmod 0750 /etc/agentenv/clock -touch /etc/agentenv/ca.pem -cp /etc/ssl/certs/ca-certificates.crt /etc/agentenv/ca-bundle.pem -cp /etc/ssl/certs/ca-certificates.crt /usr/local/lib/agentenv-egress/roots.pem -sed -i -e "s/^AGENTENV_SESSION_UID=.*/AGENTENV_SESSION_UID=$(id -u "$session")/" \ - -e "s/^AGENTENV_CLOCK_PAGE_UID=.*/AGENTENV_CLOCK_PAGE_UID=$(id -u agentenv-clock)/" /etc/agentenv/clock.conf -chmod 0755 /usr/local/lib/agentenv-clock/clock-follower /usr/local/lib/agentenv-root/init -printf '%s\n' /usr/local/lib/agentenv-clock/libfaketimeMT.so.1 /usr/local/lib/agentenv-clock/libtls_realtime.so \ - > /etc/ld.so.preload diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/shared_sources.toml b/src/agent_env/computers/artifacts/ubuntu-24.04-browser/shared_sources.toml new file mode 100644 index 000000000..754947123 --- /dev/null +++ b/src/agent_env/computers/artifacts/ubuntu-24.04-browser/shared_sources.toml @@ -0,0 +1,6 @@ +# What this image's build context takes from agent-env's own sources, at the name its Dockerfile copies it from: one +# copy of each, shared with the other images that take it (utils/docker/context.py). Paths are under src/agent_env. +clock = "utils/faketime_clock" +egress = "utils/egress_proxy" +root = "computers/artifacts/ubuntu-24.04/root" +"root.conf" = "computers/artifacts/ubuntu-24.04/root.conf" diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/clock/COPYING b/src/agent_env/computers/artifacts/ubuntu-24.04/clock/COPYING deleted file mode 100644 index d159169d1..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/clock/COPYING +++ /dev/null @@ -1,339 +0,0 @@ - GNU GENERAL PUBLIC LICENSE - Version 2, June 1991 - - Copyright (C) 1989, 1991 Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The licenses for most software are designed to take away your -freedom to share and change it. By contrast, the GNU General Public -License is intended to guarantee your freedom to share and change free -software--to make sure the software is free for all its users. This -General Public License applies to most of the Free Software -Foundation's software and to any other program whose authors commit to -using it. (Some other Free Software Foundation software is covered by -the GNU Lesser General Public License instead.) You can apply it to -your programs, too. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -this service if you wish), that you receive source code or can get it -if you want it, that you can change the software or use pieces of it -in new free programs; and that you know you can do these things. - - To protect your rights, we need to make restrictions that forbid -anyone to deny you these rights or to ask you to surrender the rights. -These restrictions translate to certain responsibilities for you if you -distribute copies of the software, or if you modify it. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must give the recipients all the rights that -you have. You must make sure that they, too, receive or can get the -source code. And you must show them these terms so they know their -rights. - - We protect your rights with two steps: (1) copyright the software, and -(2) offer you this license which gives you legal permission to copy, -distribute and/or modify the software. - - Also, for each author's protection and ours, we want to make certain -that everyone understands that there is no warranty for this free -software. If the software is modified by someone else and passed on, we -want its recipients to know that what they have is not the original, so -that any problems introduced by others will not reflect on the original -authors' reputations. - - Finally, any free program is threatened constantly by software -patents. We wish to avoid the danger that redistributors of a free -program will individually obtain patent licenses, in effect making the -program proprietary. To prevent this, we have made it clear that any -patent must be licensed for everyone's free use or not licensed at all. - - The precise terms and conditions for copying, distribution and -modification follow. - - GNU GENERAL PUBLIC LICENSE - TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - - 0. This License applies to any program or other work which contains -a notice placed by the copyright holder saying it may be distributed -under the terms of this General Public License. The "Program", below, -refers to any such program or work, and a "work based on the Program" -means either the Program or any derivative work under copyright law: -that is to say, a work containing the Program or a portion of it, -either verbatim or with modifications and/or translated into another -language. (Hereinafter, translation is included without limitation in -the term "modification".) Each licensee is addressed as "you". - -Activities other than copying, distribution and modification are not -covered by this License; they are outside its scope. The act of -running the Program is not restricted, and the output from the Program -is covered only if its contents constitute a work based on the -Program (independent of having been made by running the Program). -Whether that is true depends on what the Program does. - - 1. You may copy and distribute verbatim copies of the Program's -source code as you receive it, in any medium, provided that you -conspicuously and appropriately publish on each copy an appropriate -copyright notice and disclaimer of warranty; keep intact all the -notices that refer to this License and to the absence of any warranty; -and give any other recipients of the Program a copy of this License -along with the Program. - -You may charge a fee for the physical act of transferring a copy, and -you may at your option offer warranty protection in exchange for a fee. - - 2. You may modify your copy or copies of the Program or any portion -of it, thus forming a work based on the Program, and copy and -distribute such modifications or work under the terms of Section 1 -above, provided that you also meet all of these conditions: - - a) You must cause the modified files to carry prominent notices - stating that you changed the files and the date of any change. - - b) You must cause any work that you distribute or publish, that in - whole or in part contains or is derived from the Program or any - part thereof, to be licensed as a whole at no charge to all third - parties under the terms of this License. - - c) If the modified program normally reads commands interactively - when run, you must cause it, when started running for such - interactive use in the most ordinary way, to print or display an - announcement including an appropriate copyright notice and a - notice that there is no warranty (or else, saying that you provide - a warranty) and that users may redistribute the program under - these conditions, and telling the user how to view a copy of this - License. (Exception: if the Program itself is interactive but - does not normally print such an announcement, your work based on - the Program is not required to print an announcement.) - -These requirements apply to the modified work as a whole. If -identifiable sections of that work are not derived from the Program, -and can be reasonably considered independent and separate works in -themselves, then this License, and its terms, do not apply to those -sections when you distribute them as separate works. But when you -distribute the same sections as part of a whole which is a work based -on the Program, the distribution of the whole must be on the terms of -this License, whose permissions for other licensees extend to the -entire whole, and thus to each and every part regardless of who wrote it. - -Thus, it is not the intent of this section to claim rights or contest -your rights to work written entirely by you; rather, the intent is to -exercise the right to control the distribution of derivative or -collective works based on the Program. - -In addition, mere aggregation of another work not based on the Program -with the Program (or with a work based on the Program) on a volume of -a storage or distribution medium does not bring the other work under -the scope of this License. - - 3. You may copy and distribute the Program (or a work based on it, -under Section 2) in object code or executable form under the terms of -Sections 1 and 2 above provided that you also do one of the following: - - a) Accompany it with the complete corresponding machine-readable - source code, which must be distributed under the terms of Sections - 1 and 2 above on a medium customarily used for software interchange; or, - - b) Accompany it with a written offer, valid for at least three - years, to give any third party, for a charge no more than your - cost of physically performing source distribution, a complete - machine-readable copy of the corresponding source code, to be - distributed under the terms of Sections 1 and 2 above on a medium - customarily used for software interchange; or, - - c) Accompany it with the information you received as to the offer - to distribute corresponding source code. (This alternative is - allowed only for noncommercial distribution and only if you - received the program in object code or executable form with such - an offer, in accord with Subsection b above.) - -The source code for a work means the preferred form of the work for -making modifications to it. For an executable work, complete source -code means all the source code for all modules it contains, plus any -associated interface definition files, plus the scripts used to -control compilation and installation of the executable. However, as a -special exception, the source code distributed need not include -anything that is normally distributed (in either source or binary -form) with the major components (compiler, kernel, and so on) of the -operating system on which the executable runs, unless that component -itself accompanies the executable. - -If distribution of executable or object code is made by offering -access to copy from a designated place, then offering equivalent -access to copy the source code from the same place counts as -distribution of the source code, even though third parties are not -compelled to copy the source along with the object code. - - 4. You may not copy, modify, sublicense, or distribute the Program -except as expressly provided under this License. Any attempt -otherwise to copy, modify, sublicense or distribute the Program is -void, and will automatically terminate your rights under this License. -However, parties who have received copies, or rights, from you under -this License will not have their licenses terminated so long as such -parties remain in full compliance. - - 5. You are not required to accept this License, since you have not -signed it. However, nothing else grants you permission to modify or -distribute the Program or its derivative works. These actions are -prohibited by law if you do not accept this License. Therefore, by -modifying or distributing the Program (or any work based on the -Program), you indicate your acceptance of this License to do so, and -all its terms and conditions for copying, distributing or modifying -the Program or works based on it. - - 6. Each time you redistribute the Program (or any work based on the -Program), the recipient automatically receives a license from the -original licensor to copy, distribute or modify the Program subject to -these terms and conditions. You may not impose any further -restrictions on the recipients' exercise of the rights granted herein. -You are not responsible for enforcing compliance by third parties to -this License. - - 7. If, as a consequence of a court judgment or allegation of patent -infringement or for any other reason (not limited to patent issues), -conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot -distribute so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you -may not distribute the Program at all. For example, if a patent -license would not permit royalty-free redistribution of the Program by -all those who receive copies directly or indirectly through you, then -the only way you could satisfy both it and this License would be to -refrain entirely from distribution of the Program. - -If any portion of this section is held invalid or unenforceable under -any particular circumstance, the balance of the section is intended to -apply and the section as a whole is intended to apply in other -circumstances. - -It is not the purpose of this section to induce you to infringe any -patents or other property right claims or to contest validity of any -such claims; this section has the sole purpose of protecting the -integrity of the free software distribution system, which is -implemented by public license practices. Many people have made -generous contributions to the wide range of software distributed -through that system in reliance on consistent application of that -system; it is up to the author/donor to decide if he or she is willing -to distribute software through any other system and a licensee cannot -impose that choice. - -This section is intended to make thoroughly clear what is believed to -be a consequence of the rest of this License. - - 8. If the distribution and/or use of the Program is restricted in -certain countries either by patents or by copyrighted interfaces, the -original copyright holder who places the Program under this License -may add an explicit geographical distribution limitation excluding -those countries, so that distribution is permitted only in or among -countries not thus excluded. In such case, this License incorporates -the limitation as if written in the body of this License. - - 9. The Free Software Foundation may publish revised and/or new versions -of the General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - -Each version is given a distinguishing version number. If the Program -specifies a version number of this License which applies to it and "any -later version", you have the option of following the terms and conditions -either of that version or of any later version published by the Free -Software Foundation. If the Program does not specify a version number of -this License, you may choose any version ever published by the Free Software -Foundation. - - 10. If you wish to incorporate parts of the Program into other free -programs whose distribution conditions are different, write to the author -to ask for permission. For software which is copyrighted by the Free -Software Foundation, write to the Free Software Foundation; we sometimes -make exceptions for this. Our decision will be guided by the two goals -of preserving the free status of all derivatives of our free software and -of promoting the sharing and reuse of software generally. - - NO WARRANTY - - 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY -FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN -OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES -PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED -OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS -TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE -PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, -REPAIR OR CORRECTION. - - 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR -REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, -INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING -OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED -TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY -YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER -PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE -POSSIBILITY OF SUCH DAMAGES. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -convey the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - -Also add information on how to contact you by electronic and paper mail. - -If the program is interactive, make it output a short notice like this -when it starts in an interactive mode: - - Gnomovision version 69, Copyright (C) year name of author - Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, the commands you use may -be called something other than `show w' and `show c'; they could even be -mouse-clicks or menu items--whatever suits your program. - -You should also get your employer (if you work as a programmer) or your -school, if any, to sign a "copyright disclaimer" for the program, if -necessary. Here is a sample; alter the names: - - Yoyodyne, Inc., hereby disclaims all copyright interest in the program - `Gnomovision' (which makes passes at compilers) written by James Hacker. - - , 1 April 1989 - Ty Coon, President of Vice - -This General Public License does not permit incorporating your program into -proprietary programs. If your program is a subroutine library, you may -consider it more useful to permit linking proprietary applications with the -library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/clock/README.md b/src/agent_env/computers/artifacts/ubuntu-24.04/clock/README.md deleted file mode 100644 index fa80cd9a4..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/clock/README.md +++ /dev/null @@ -1,84 +0,0 @@ -# The computer's clock: agent-env's libfaketime - -Every program on a computer reads its own clock, so a computer can't ask the gateway for universe time the way an MCP -server does. Instead each session program loads agent-env's build of [libfaketime](https://github.com/wolfcw/libfaketime) -(through `/etc/ld.so.preload`), which answers clock reads with universe time from a page a follower process keeps in -step with the gateway. - -It is part of the ubuntu-24.04 base, which builds the library and the shim from this folder in a stage of its -Dockerfile, since an image builds only from its own folder; the ubuntu-24.04-browser base has a copy, which a unit test -keeps identical. The base starts as root: the root start (`../root/init`) sets the zone, makes the clock page and file -for the follower's user, `agentenv-clock`, and starts root's supervisord (`../root.conf`), which runs the follower -(`clock-follower`) as that user and the session's supervisord as the session user. The session user can read the clock but neither write, replace nor -stop it. - -The follower long-polls the gateway's `/clock/watch`, writes each state it answers to the page and acknowledges it with -its next watch. Core writes its config, the gateway's watch URL as the computer reaches it, the computer's name and its -read token (for now its control token), through the sandbox's root channel when a multi env deploys the computer, and -again after a reset; root and the follower alone can read it. The follower reads it every quarter second and reports in -a status file that holds no token, which the driver's `clock/v1` `sync_time` reads: the computer's card serving -`sync_time` is what tells core and the gateway that it follows the clock (`agentenv_protocol.computer.clock`). - -What follows the universe, and what doesn't: - -- **Universe time:** clock reads (`CLOCK_REALTIME`, `_COARSE`, `_ALARM`, `CLOCK_TAI`, `gettimeofday`, `time`) and waits - for a set realtime clock time (`clock_nanosleep` with `TIMER_ABSTIME`, realtime futex waits, `sem_timedwait` and - `sem_clockwait`, realtime condition variables, absolute realtime timerfds and POSIX timers). A wait is planned in real - time and planned again whenever the page changes, so a jump past its deadline ends it at the jump. Where the kernel - has no `futex_waitv` (before 5.16, or gVisor), realtime futex and semaphore waits read the page between slices of - `AGENTENV_WAIT_SLICE_MS` (100 ms by default) instead, so they see a change within a slice. -- **Real time:** the monotonic and boot clocks, CPU-time clocks, and every relative sleep and timeout, at any rate. -- **Certificates:** OpenSSL checks a peer's certificates against real time, through the TLS shim (`tls_realtime.c`, - preloaded after the library), so a client that reaches a live site without the computer's egress proxy still - verifies it months from real time, and still refuses an expired or untrusted one. The shim sets - `X509_verify_cert`'s check time unless the caller set one, and calls the caller's own libcrypto. TLS stacks of - their own (Chromium's BoringSSL, GnuTLS, NSS, Java's) and static binaries aren't covered. -- **Who:** only the session user's processes (`AGENTENV_SESSION_UID`). A program started with `AGENTENV_REAL_TIME=1` - stays real; the marker is taken out of its environment block, so its children are faked. -- **Settings:** read once as the library loads, from `/etc/agentenv/clock.conf` (`clock.conf` is the default), or from - the environment where there is no such file. While the file exists the environment changes none of them; the marker - above is the one switch a program's environment has. No `getenv()` runs once the program has started: a `getenv()` - racing a program's own `setenv()` made stock libfaketime crash Chromium on about one launch in five. - -## Files - -| File | What it is | License | -|---|---|---| -| `libfaketime.patch` | agent-env's changes to upstream 0.9.12's `src/` (applied with `patch -p1`) | GPL-2.0-only | -| `agentenv_clock_page.c` | settings, who is faked, the clock page reader; included by the patched `libfaketime.c` | GPL-2.0-only | -| `agentenv_clock_waits.c` | universe-time reads and waits, the per-process helper thread; included the same way | GPL-2.0-only | -| `COPYING` | the GNU General Public License version 2, libfaketime's license | | -| `build.sh` | builds `libfaketimeMT.so.1` from upstream's sha256-pinned tarball and the files above, and the shim's `libtls_realtime.so` | Apache-2.0 | -| `tls_realtime.c` | the TLS shim: OpenSSL's certificate checks on real time, preloaded after the library | Apache-2.0 | -| `clockpage.py` | the clock page's writer, which the follower uses | Apache-2.0 | -| `clock-follower` | the follower: the gateway's watch, the page written, the status reported | Apache-2.0 | -| `clock.conf` | the library's default settings | Apache-2.0 | - -Build it on x86_64 or aarch64 Linux with a C toolchain, make, patch and curl: - - src/agent_env/computers/artifacts/ubuntu-24.04/clock/build.sh OUT [TARBALL] - -## The clock page - -An 80-byte page (default `/dev/shm/agentenv-clock`, version 2) that `clockpage.py` rewrites in place: `seq` odd, the -fields and their check, `seq` even, then `FUTEX_WAKE` on `seq`, which wakes every wait the library has planned and its -helper threads. Readers never cache it and never see a torn value: they wait on `seq` while it is odd and retry when it -changed or the check doesn't match. A write that would step the universe back by less than a second (a rate change -computed from a read a moment earlier) continues from where the page has it instead. - -## Tests - -`tst/clock` (`make clock-test`) builds this library and stock 0.9.12 in an Ubuntu 24.04 container and runs its gates -there: every wait API in C, Python, Java and Node at several rates, jumps, cancelled waits, timerfd duplicates, -condition-variable and semaphore stress, the waits again in a container that refuses `futex_waitv` (as kernels before -5.16 and gVisor do), a stalled writer, one that died mid-write, torn reads, the marker, the environment race, forks -while another thread closes a timerfd, the TLS shim (curl, Python and `openssl s_client` with the clock 182 days back: -live sites verify, two local servers show the check is on real time and expired and untrusted certificates are still -refused, and two copies of OpenSSL in one process, the system's 3.0 and a 1.1, each check with their own), and -Chromium launches. `.github/workflows/clock.yml` runs them on x86_64 and arm64. The gates hold correctness strictly -(no wait ends early, every wait returns what it should, nothing is lost or torn) and lateness to -`tst/clock/bounds.py`, a bound a broken library misses by seconds and a slow machine doesn't reach. -`tst/integration/computer/clock_desktop_test.py` composes the library into a real computer (the ubuntu-24.04 desktop, -the reference driver and agentenv's chromium, every process preloading it), which must pass the conformance probe at -full; then, its page written as root, a shell, Chrome's JavaScript and the driver's input follow the universe through -30 jumps, a rate change and a freeze, with Chrome, Xvfb and the desktop's panel running on. diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/clock/agentenv_clock_page.c b/src/agent_env/computers/artifacts/ubuntu-24.04/clock/agentenv_clock_page.c deleted file mode 100644 index 9167f7ede..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/clock/agentenv_clock_page.c +++ /dev/null @@ -1,320 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ -/* - * agent-env's additions to libfaketime 0.9.12, part 1, included by libfaketime.c (libfaketime.patch) before its - * time functions: the process's settings, read once at load from /etc/agentenv/clock.conf, or from the environment - * where there is no such file, so no getenv() runs after main() starts; who is faked (the session user, unless started with AGENTENV_REAL_TIME=1, a marker - * taken out of the environment block in place); and the clock page the follower writes, read without a cache. - * Derived from libfaketime, so licensed as it is, under the GNU General Public License version 2 (COPYING). - */ -/* - * configuration is read once, when the library loads, from AGENTENV_CLOCK_CONF (NAME=VALUE lines) when that - * file exists, else from the environment, and never with getenv() afterwards: a getenv() racing a program's setenv() - * reads a freed environ array, which is how libfaketime's pthread_cond_timedwait crashed Chromium. - */ -#ifndef AGENTENV_CLOCK_CONF -#define AGENTENV_CLOCK_CONF "/etc/agentenv/clock.conf" -#endif -#define FT_CONF_MAX 64 -static char ft_conf_text[8192]; -static const char *ft_conf_name[FT_CONF_MAX]; -static const char *ft_conf_value[FT_CONF_MAX]; -static int ft_conf_count = 0; -static bool ft_conf_loaded = false; -/* this process stays on real time: another user's than AGENTENV_SESSION_UID, or started with - AGENTENV_REAL_TIME=1. */ -static bool ft_disabled = false; -/* bumped with each new time specification parsed, so a wait sees the clock change under it. */ -static volatile unsigned long ft_spec_gen = 0; -static __thread unsigned long ft_tl_gen = 0; -/* real seconds per fake second for what is left of a wait, as of this thread's last clock read: 1 / rate, - or 1 for a frozen clock. */ -static __thread double ft_tl_scale = 1.0; -/* how long a wait that can be cut sleeps before it looks at the clock again (AGENTENV_WAIT_SLICE_MS). */ -static long ft_wait_slice_ns = 100000000L; - -static void ft_conf_add(char *name, char *value) -{ - if (ft_conf_count < FT_CONF_MAX) - { - ft_conf_name[ft_conf_count] = name; - ft_conf_value[ft_conf_count] = value; - ft_conf_count++; - } -} - -static void ft_conf_load(void) -{ - extern char **environ; - size_t used = 0; - int fd; - if (ft_conf_loaded) return; - ft_conf_loaded = true; - fd = open(AGENTENV_CLOCK_CONF, O_RDONLY | O_CLOEXEC); - if (fd >= 0) - { - ssize_t n; - char *line; - while (used < sizeof(ft_conf_text) - 1 && - (n = read(fd, ft_conf_text + used, sizeof(ft_conf_text) - 1 - used)) > 0) - used += n; - close(fd); - ft_conf_text[used] = 0; - line = ft_conf_text; - while (line != NULL && *line != 0) - { - char *next = strchr(line, '\n'); - char *eq; - if (next != NULL) *next++ = 0; - eq = strchr(line, '='); - if (line[0] != '#' && eq != NULL && eq != line) - { - *eq = 0; - ft_conf_add(line, eq + 1); - } - line = next; - } - return; - } - for (char **e = environ; e != NULL && *e != NULL; e++) - { - const char *s = *e; - size_t len; - char *copy, *eq; - if (strncmp(s, "FAKE", 4) && strncmp(s, "NO_FAKE", 7) && strncmp(s, "DONT_FAKE", 9) && strncmp(s, "HOME=", 5) - && strncmp(s, "AGENTENV_", 9)) - continue; - len = strlen(s); - if (used + len + 1 > sizeof(ft_conf_text)) break; - copy = ft_conf_text + used; - memcpy(copy, s, len + 1); - used += len + 1; - eq = strchr(copy, '='); - if (eq == NULL) continue; - *eq = 0; - ft_conf_add(copy, eq + 1); - } -} - -static char *ft_getenv(const char *name) -{ - ft_conf_load(); - for (int i = 0; i < ft_conf_count; i++) - { - if (strcmp(ft_conf_name[i], name) == 0) return (char *) ft_conf_value[i]; - } - return NULL; -} - -/* - * AGENTENV_REAL_TIME=1 taken out of the original environment block in place: its pointer out of the array - * (which is main()'s envp too) and the string itself changed to "=0", so no copy of it is the marker. unsetenv() isn't - * used: a program may define its own (bash does, and before its main() it does nothing). - */ -static void ft_scrub_marker(void) -{ - extern char **environ; - char **e = environ; - if (e == NULL) return; - while (*e != NULL) - { - if (strcmp(*e, "AGENTENV_REAL_TIME=1") == 0) - { - char **d = e; - (*e)[sizeof("AGENTENV_REAL_TIME=") - 1] = '0'; - do d[0] = d[1]; while (*d++ != NULL); - } - else - e++; - } -} - -/* whether a wait's deadline is one to translate: a valid one, in a faked process, outside libfaketime. */ -static bool ft_translates_wait(const struct timespec *abstime) -{ - return abstime != NULL && !ft_disabled && !dont_fake && abstime->tv_nsec >= 0 && abstime->tv_nsec < SEC_TO_nSEC; -} - -/* - * where a CLOCK_REALTIME deadline in fake time falls on a real clock (CLOCK_MONOTONIC or CLOCK_REALTIME): - * now, plus what is left of the fake interval times ft_tl_scale; so a frozen clock counts what is left as real time and - * nothing waits forever, and a deadline already past is now. - */ -static void ft_deadline_from_fake(clockid_t clock, const struct timespec *fake_abs, struct timespec *out) -{ - struct timespec now, real_now, fake_now, left, scaled; - DONT_FAKE_TIME((*real_clock_gettime)(clock, &now)); - DONT_FAKE_TIME((*real_clock_gettime)(CLOCK_REALTIME, &real_now)); - fake_now = real_now; - (void) fake_clock_gettime(CLOCK_REALTIME, &fake_now); - *out = now; - timespecsub(fake_abs, &fake_now, &left); - if (left.tv_sec < 0) return; - if (left.tv_sec > 100L * 365 * 86400) left.tv_sec = 100L * 365 * 86400; - if (ft_tl_scale != 1.0) - { - timespecmul(&left, ft_tl_scale, &scaled); - } - else - { - scaled = left; - } - timespecadd(&now, &scaled, out); -} - -/* the end of a wait's next slice on CLOCK_MONOTONIC; true when that is the deadline itself. */ -static bool ft_slice_end(const struct timespec *mono_abs, struct timespec *slice) -{ - struct timespec now, step = {ft_wait_slice_ns / SEC_TO_nSEC, ft_wait_slice_ns % SEC_TO_nSEC}; - DONT_FAKE_TIME((*real_clock_gettime)(CLOCK_MONOTONIC, &now)); - timespecadd(&now, &step, slice); - if (timespeccmp(slice, mono_abs, <)) return false; - *slice = *mono_abs; - return true; -} - -/* after a slice, the clock read again (the file, with no cache); a new specification since ``gen`` moves the - deadline. True when it moved. */ -static bool ft_deadline_moved(const struct timespec *fake_abs, struct timespec *mono_abs, unsigned long *gen) -{ - struct timespec now; - DONT_FAKE_TIME((*real_clock_gettime)(CLOCK_REALTIME, &now)); - (void) fake_clock_gettime(CLOCK_REALTIME, &now); - if (ft_tl_gen == *gen) return false; - *gen = ft_tl_gen; - ft_deadline_from_fake(CLOCK_MONOTONIC, fake_abs, mono_abs); - return true; -} - -/* - * the clock page. 64 bytes, little-endian, rewritten in place by the follower: seq odd while it writes, - * then even; check is the xor of the other words and a constant. A realtime read in a faked process copies it - * lock-free and computes universe + (now - anchor) * rate (rate 0: universe), so every process agrees with no shared - * start; the first thread to see a new generation also hands it to libfaketime's own state (rates of sleeps, the - * time an absolute spec gives) under the MT lock. The page must be owned by AGENTENV_CLOCK_PAGE_UID (0 by default) and - * writable by no one else, so the session user can't plant one. - */ -struct ft_clock_page -{ - uint32_t magic, version; - uint64_t seq; - int64_t universe_ns, anchor_ns; - double rate; - uint32_t armed, reserved; - uint64_t generation, check; - int64_t mono_universe_ns, mono_anchor_ns; -}; -#define FT_PAGE_MAGIC 0x4B434541u -#define FT_PAGE_VERSION 2u -static const volatile struct ft_clock_page *ft_page = NULL; -static uint64_t ft_page_applied = 0; -static time_t ft_page_last_try = 0; -static void parse_ft_string(const char *user_faked_time); - -static uint64_t ft_page_check(const struct ft_clock_page *p) -{ - uint64_t rate_bits; - memcpy(&rate_bits, &p->rate, sizeof rate_bits); - return p->seq ^ (uint64_t) p->universe_ns ^ (uint64_t) p->anchor_ns ^ rate_bits ^ p->armed ^ p->generation - ^ (uint64_t) p->mono_universe_ns ^ (uint64_t) p->mono_anchor_ns ^ 0x9E3779B97F4A7C15ull; -} - -static bool ft_page_snapshot(struct ft_clock_page *out) -{ - for (int i = 0; i < 1000; i++) - { - uint64_t s1 = __atomic_load_n(&ft_page->seq, __ATOMIC_ACQUIRE); - if (s1 & 1) continue; - out->magic = ft_page->magic; - out->seq = s1; - out->universe_ns = ft_page->universe_ns; - out->anchor_ns = ft_page->anchor_ns; - out->rate = ft_page->rate; - out->armed = ft_page->armed; - out->version = ft_page->version; - out->generation = ft_page->generation; - out->check = ft_page->check; - out->mono_universe_ns = ft_page->mono_universe_ns; - out->mono_anchor_ns = ft_page->mono_anchor_ns; - __atomic_thread_fence(__ATOMIC_ACQUIRE); - if (__atomic_load_n(&ft_page->seq, __ATOMIC_RELAXED) == s1 && out->magic == FT_PAGE_MAGIC - && out->version == FT_PAGE_VERSION - && ft_page_check(out) == out->check) - return true; - } - return false; -} - -static void ft_page_open(void) -{ - const char *path = ft_getenv("AGENTENV_CLOCK_PAGE"); - const char *owner = ft_getenv("AGENTENV_CLOCK_PAGE_UID"); - struct stat st; - void *map; - int fd; - if (path == NULL || *path == 0) return; - fd = open(path, O_RDONLY | O_CLOEXEC); - if (fd < 0) return; - if (fstat(fd, &st) != 0 || st.st_size < (off_t) sizeof(struct ft_clock_page) - || st.st_uid != (uid_t) (owner ? strtoul(owner, NULL, 10) : 0) || (st.st_mode & 022) != 0) - { - close(fd); - return; - } - map = mmap(NULL, sizeof(struct ft_clock_page), PROT_READ, MAP_SHARED, fd, 0); - close(fd); - if (map == MAP_FAILED) return; - if (((const struct ft_clock_page *) map)->magic != FT_PAGE_MAGIC - || ((const struct ft_clock_page *) map)->version != FT_PAGE_VERSION) - { - munmap(map, sizeof(struct ft_clock_page)); - return; - } - ft_page = map; -} - -/* libfaketime's own state set to the page's clock; called with the MT lock held. */ -static void ft_page_apply_locked(const struct ft_clock_page *s) -{ - char spec[160]; - if (s->generation == ft_page_applied) return; - if (!s->armed) - snprintf(spec, sizeof spec, "+0 x1"); - else - snprintf(spec, sizeof spec, "@%lld.%09lld x%.17g ^%lld.%09lld", - (long long) (s->universe_ns / 1000000000LL), (long long) (s->universe_ns % 1000000000LL), s->rate, - (long long) (s->anchor_ns / 1000000000LL), (long long) (s->anchor_ns % 1000000000LL)); - parse_ft_string(spec); - __atomic_store_n(&ft_page_applied, s->generation, __ATOMIC_RELEASE); -} - -/* "%s" times are read as a number, without strptime() and mktime(), which reads TZ with getenv(). */ -static const char *ft_parse_epoch(const char *s, struct timespec *out) -{ - char *end; - long long sec; - long nsec = 0, scale = 100000000L; - bool negative; - while (*s == ' ') s++; - negative = (*s == '-'); - sec = strtoll(s, &end, 10); - if (end == s) return NULL; - if (*end == '.') - { - end++; - while (*end >= '0' && *end <= '9') - { - nsec += (*end - '0') * scale; - scale /= 10; - end++; - } - } - if (negative && nsec > 0) - { - sec -= 1; - nsec = SEC_TO_nSEC - nsec; - } - out->tv_sec = (time_t) sec; - out->tv_nsec = nsec; - return end; -} - diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/clock/agentenv_clock_waits.c b/src/agent_env/computers/artifacts/ubuntu-24.04/clock/agentenv_clock_waits.c deleted file mode 100644 index 5d8237705..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/clock/agentenv_clock_waits.c +++ /dev/null @@ -1,1175 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ -/* - * agent-env's additions to libfaketime 0.9.12, part 2, included by libfaketime.c (libfaketime.patch) after its time - * functions: clock reads and waits for a set realtime clock time on universe time, re-planned whenever the page - * changes, and everything else (monotonic and boot clocks, relative sleeps and timeouts) the kernel's. - * Derived from libfaketime, so licensed as it is, under the GNU General Public License version 2 (COPYING). - */ -/* - * Elapsed time is real; clock reads and waits for a realtime clock time follow the universe. - */ -#include -#include -#include -#include - -#if !defined(FAKE_PTHREAD) || !defined(FAKE_TIMERS) || !defined(FAKE_SLEEP) || defined(INTERCEPT_SYSCALL) -#error agent-env waits need FAKE_PTHREAD, FAKE_TIMERS and FAKE_SLEEP, and their own syscall(), not the INTERCEPT_SYSCALL one -#endif -#ifndef SYS_futex_waitv -#define SYS_futex_waitv 449 -#endif - -/* - * the system call itself, with no libc and no libfaketime state, so syscall() below works before the - * library is initialised (Chromium's allocator calls syscall() from inside the dynamic loader). - */ -static long ft_raw_syscall6(long n, long a, long b, long c, long d, long e, long f) -{ - long ret; -#if defined(__x86_64__) - register long r10 __asm__("r10") = d; - register long r8 __asm__("r8") = e; - register long r9 __asm__("r9") = f; - __asm__ volatile("syscall" : "=a"(ret) : "0"(n), "D"(a), "S"(b), "d"(c), "r"(r10), "r"(r8), "r"(r9) - : "rcx", "r11", "memory"); -#elif defined(__aarch64__) - register long x8 __asm__("x8") = n; - register long x0 __asm__("x0") = a; - register long x1 __asm__("x1") = b; - register long x2 __asm__("x2") = c; - register long x3 __asm__("x3") = d; - register long x4 __asm__("x4") = e; - register long x5 __asm__("x5") = f; - __asm__ volatile("svc #0" : "+r"(x0) : "r"(x8), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5) : "memory"); - ret = x0; -#else -#error the agent-env syscall() needs x86_64 or aarch64 -#endif - if (ret < 0 && ret > -4096) - { - errno = (int) -ret; - return -1; - } - return ret; -} - -#define FT_NS 1000000000LL -#define FT_NEVER INT64_MAX -static __thread struct ft_clock_page ft_tl_snap; -static __thread bool ft_tl_snap_ok = false; - -/* a consistent copy of the page. While the follower holds it odd (preempted mid-write), wait for it on seq, - which it wakes when done, up to a second: a copy older than the page being written would be extrapolated past the - new page's anchor, and the clock would step back when the write lands. Only a follower that died mid-write (seq odd - for over a second, remembered) leaves this thread on its last good copy. */ -static uint64_t ft_dead_seq = 0; -static bool ft_snap(struct ft_clock_page *s) -{ - for (int i = 0; i < 64; i++) - { - if (ft_page_snapshot(s)) - { - ft_tl_snap = *s; - ft_tl_snap_ok = true; - return true; - } - sched_yield(); - } - for (int i = 0; i < 100; i++) - { - uint64_t seq = __atomic_load_n(&ft_page->seq, __ATOMIC_ACQUIRE); - if ((seq & 1) && seq == __atomic_load_n(&ft_dead_seq, __ATOMIC_RELAXED)) break; - if (seq & 1) - { - struct timespec ts = {0, 10000000}; - ft_raw_syscall6(SYS_futex, (long) &ft_page->seq, FUTEX_WAIT, (long) (uint32_t) seq, (long) &ts, 0, 0); - } - if (ft_page_snapshot(s)) - { - ft_tl_snap = *s; - ft_tl_snap_ok = true; - return true; - } - if (i == 99) __atomic_store_n(&ft_dead_seq, seq, __ATOMIC_RELAXED); - } - if (!ft_tl_snap_ok) return false; - *s = ft_tl_snap; - return true; -} - -/* the real time just read, in hand before the loads after it: a load can be carried out before an earlier read of the - CPU's counter (aarch64 and x86_64 both allow it). On aarch64 a load whose address depends on the time, then a load - barrier, as the kernel orders its own counter reads; on x86_64 an lfence. */ -static inline void ft_after_real_read(const struct timespec *tp) -{ -#if defined(__aarch64__) - uint64_t tmp; - __asm__ volatile("eor %0, %1, %1\n\tadd %0, sp, %0\n\tldr xzr, [%0]\n\tdmb ishld" - : "=&r"(tmp) : "r"((uint64_t) tp->tv_nsec) : "memory"); -#elif defined(__x86_64__) - (void) tp; - __asm__ volatile("lfence" ::: "memory"); -#else - (void) tp; - __atomic_thread_fence(__ATOMIC_SEQ_CST); -#endif -} - -/* a consistent copy of the page and the real time read while it was the page: the time is read after the copy and kept - only if the page is still the one copied. A time from before a write paired with the page it wrote (a reader that - waited out the write, or was descheduled between its time and its copy) could put the universe behind what that - reader had shown, since a write is continuous with the page it replaces only at its own real time. */ -static bool ft_snap_at(clockid_t clock, struct ft_clock_page *s, struct timespec *tp) -{ - for (int i = 0; i < 8; i++) - { - int r; - if (!ft_snap(s)) return false; - DONT_FAKE_TIME(r = (*real_clock_gettime)(clock, tp)); - if (r != 0) return false; - ft_after_real_read(tp); - if (__atomic_load_n(&ft_page->seq, __ATOMIC_ACQUIRE) == s->seq) return true; - } - return true; -} - -static inline int64_t ft_ns(const struct timespec *t) -{ - if (t->tv_sec > 9000000000LL) return FT_NEVER / 2; - if (t->tv_sec < -9000000000LL) return -(FT_NEVER / 2); - return (int64_t) t->tv_sec * FT_NS + t->tv_nsec; -} -static inline struct timespec ft_ts(int64_t ns) -{ - struct timespec t; - if (ns < 0) ns = 0; - t.tv_sec = (time_t) (ns / FT_NS); - t.tv_nsec = (long) (ns % FT_NS); - return t; -} -static inline int64_t ft_real_rt(void) -{ - struct timespec t; - (*real_clock_gettime)(CLOCK_REALTIME, &t); - return ft_ns(&t); -} -static inline bool ft_uw_active(void) -{ - return ft_page != NULL && !ft_disabled && !dont_fake; -} -static inline bool ft_faked(void) -{ - return !ft_disabled && !dont_fake; -} -static inline bool ft_valid_ts(const struct timespec *t) -{ - return t != NULL && t->tv_nsec >= 0 && t->tv_nsec < FT_NS && t->tv_sec >= 0; -} -static inline bool ft_rt_clock(clockid_t c) -{ - return c == CLOCK_REALTIME || c == CLOCK_REALTIME_ALARM; -} -/* the clocks the universe has; every other one (monotonic, boot, raw, CPU time) is real. */ -static inline bool ft_universe_clock(clockid_t c) -{ - return c == CLOCK_REALTIME || c == CLOCK_REALTIME_COARSE || c == CLOCK_REALTIME_ALARM || c == CLOCK_TAI; -} - -/* the universe's realtime at real realtime t; before the anchor, the anchor's. */ -static int64_t ft_page_rt(const struct ft_clock_page *s, int64_t t) -{ - if (!s->armed) return t; - if (s->rate == 0 || t <= s->anchor_ns) return s->universe_ns; - if (s->rate == 1.0) return s->universe_ns + (t - s->anchor_ns); - return s->universe_ns + (int64_t) ((double) (t - s->anchor_ns) * s->rate); -} -/* the real realtime at which the universe reaches ``due``; 0 (the past) once it has, FT_NEVER while frozen - short of it. */ -static int64_t ft_real_of(const struct ft_clock_page *s, int64_t due) -{ - double r; - if (!s->armed) return due < 1 ? 1 : due; - if (due <= s->universe_ns) return 1; - if (s->rate <= 0) return FT_NEVER; - r = (double) (due - s->universe_ns) / s->rate; - if (r > 4e18) return FT_NEVER; - return s->anchor_ns + (int64_t) r + 1; -} -static inline bool ft_reached(const struct ft_clock_page *s, int64_t due) -{ - return ft_page_rt(s, ft_real_rt()) >= due; -} -static inline uint32_t ft_seq32(void) -{ - return (uint32_t) __atomic_load_n(&ft_page->seq, __ATOMIC_ACQUIRE); -} - -/* ---- a lock for the tables below, with signals blocked so a handler can't deadlock on it ---- */ -static volatile int ft_tab_lock_word = 0; -static void ft_tab_lock(sigset_t *old) -{ - sigset_t all; - sigfillset(&all); - pthread_sigmask(SIG_SETMASK, &all, old); - while (__atomic_exchange_n(&ft_tab_lock_word, 1, __ATOMIC_ACQUIRE)) sched_yield(); -} -static void ft_tab_unlock(const sigset_t *old) -{ - __atomic_store_n(&ft_tab_lock_word, 0, __ATOMIC_RELEASE); - pthread_sigmask(SIG_SETMASK, old, NULL); -} - -/* - * ---- timers for a realtime clock time: timerfds and POSIX timers armed at TIMER_ABSTIME on a realtime clock. The - * kernel timer is armed at the real realtime the universe reaches the deadline at (in the past once it has, so it fires - * now), its interval at interval / rate, and disarmed while frozen short of it; the helper arms it again on each change. - * A one-shot timer that has fired is left alone. ---- - */ -#define FT_TIMER_FDS 8 -struct ft_timer -{ - int kind; /* 0 free, 1 timerfd, 2 POSIX timer */ - int tracked; /* armed for a realtime clock time */ - int nfds, fds[FT_TIMER_FDS]; /* a timerfd's descriptors: the one it was armed through and its duplicates */ - timer_t id; - int clock; - int flags; /* the caller's flags, TFD_TIMER_CANCEL_ON_SET kept */ - int real_set; /* the kernel timer is armed */ - int64_t due, interval; /* universe ns */ - struct ft_clock_page pg; /* the page it was last armed from */ -}; -#define FT_TIMERS_MAX 1024 -static struct ft_timer ft_timers[FT_TIMERS_MAX]; -static int ft_timers_top = 0; -static volatile int ft_timers_any = 0; -static int (*real_timer_create_new)(clockid_t, struct sigevent *, timer_t *); -static int (*real_timer_delete_new)(timer_t); -static int (*real_timer_settime_new)(timer_t, int, const struct itimerspec *, struct itimerspec *); -static int (*real_timer_gettime_new)(timer_t, struct itimerspec *); -#define FT_TFD_CLOCKS 65536 -static int ft_tfd_clock[FT_TFD_CLOCKS]; /* clock + 1 as timerfd_create was told; 0 unknown */ - -static struct ft_timer *ft_timer_find_fd(int fd) -{ - for (int i = 0; i < ft_timers_top; i++) - if (ft_timers[i].kind == 1) - for (int k = 0; k < ft_timers[i].nfds; k++) - if (ft_timers[i].fds[k] == fd) return &ft_timers[i]; - return NULL; -} -static struct ft_timer *ft_timer_find_id(timer_t id) -{ - for (int i = 0; i < ft_timers_top; i++) - if (ft_timers[i].kind == 2 && ft_timers[i].id == id) return &ft_timers[i]; - return NULL; -} -static struct ft_timer *ft_timer_new(void) -{ - for (int i = 0; i < FT_TIMERS_MAX; i++) - if (ft_timers[i].kind == 0) - { - memset(&ft_timers[i], 0, sizeof ft_timers[i]); - if (i >= ft_timers_top) ft_timers_top = i + 1; - return &ft_timers[i]; - } - return NULL; -} -static int ft_timer_real_get(struct ft_timer *t, struct itimerspec *cur) -{ - return t->kind == 1 ? (*real_timerfd_gettime)(t->fds[0], cur) : (*real_timer_gettime_new)(t->id, cur); -} -/* the next expiration at or after ``after``. */ -static int64_t ft_timer_next(const struct ft_timer *t, int64_t after) -{ - int64_t next = t->due; - if (t->interval > 0 && next < after) next += ((after - next + t->interval - 1) / t->interval) * t->interval; - return next; -} -/* the kernel timer set from page ``s``. A periodic timer goes on from its first expiration past the - universe's time on the page it was armed from, so a jump delivers what it passed at once. Lock held. */ -static int ft_timer_arm(struct ft_timer *t, const struct ft_clock_page *s, bool first) -{ - struct itimerspec real, cur; - double rate = s->armed ? s->rate : 1.0; - int64_t next = t->due, at; - int r; - memset(&real, 0, sizeof real); - if (!first && t->real_set && t->interval == 0 && ft_timer_real_get(t, &cur) == 0 && cur.it_value.tv_sec == 0 - && cur.it_value.tv_nsec == 0) - { - t->tracked = 0; /* fired: the expiration is the kernel's to hand over */ - return 0; - } - if (!first && t->interval > 0) next = ft_timer_next(t, ft_page_rt(&t->pg, ft_real_rt()) + 1); - at = ft_real_of(s, next); - if (at != FT_NEVER) - { - real.it_value = ft_ts(at); - if (real.it_value.tv_sec == 0 && real.it_value.tv_nsec == 0) real.it_value.tv_nsec = 1; - if (t->interval > 0) - { - int64_t iv = (int64_t) ((double) t->interval / rate); - real.it_interval = ft_ts(iv < 1 ? 1 : iv); - } - } - t->due = next; - t->pg = *s; - t->real_set = (at != FT_NEVER); - if (t->kind == 1) - r = (*real_timerfd_settime)(t->fds[0], TFD_TIMER_ABSTIME | (t->flags & TFD_TIMER_CANCEL_ON_SET), &real, NULL); - else - r = (*real_timer_settime_new)(t->id, TIMER_ABSTIME, &real, NULL); - if (r != 0) t->tracked = 0; - return r; -} -/* the timer's value in universe time. Lock held. */ -static void ft_timer_value(struct ft_timer *t, struct itimerspec *out) -{ - struct ft_clock_page s; - struct itimerspec cur; - memset(out, 0, sizeof *out); - if (!t->tracked) return; - if (t->interval == 0 && t->real_set && ft_timer_real_get(t, &cur) == 0 && cur.it_value.tv_sec == 0 - && cur.it_value.tv_nsec == 0) - { - t->tracked = 0; - return; - } - out->it_interval = ft_ts(t->interval); - if (ft_snap(&s)) - { - int64_t now = ft_page_rt(&s, ft_real_rt()), next = ft_timer_next(t, now); - out->it_value = ft_ts(next - now < 1 ? 1 : next - now); - } -} - -/* - * ---- realtime condvar waits in progress: each waits to the real time its deadline falls at; on a page change that - * moves that time, the helper broadcasts its condvar, once, and the wait returns 0 (a spurious wakeup the caller - * rechecks), so the caller waits again on the new page. A broadcast that comes before the waiter is inside glibc's wait - * is sent again until the waiter has seen the change. ---- - */ -struct ft_cw -{ - pthread_cond_t *cond; - int used, woken; - int64_t due, real_due; - uint64_t gen; /* the page generation its real_due came from; FT_CW_UNPLANNED before */ -}; -#define FT_CW_MAX 512 -#define FT_CW_UNPLANNED UINT64_MAX -static struct ft_cw ft_cws[FT_CW_MAX]; -static int ft_cws_top = 0; - -/* ---- the helper thread: one per process, started with the first such timer or wait, blocked on the page's seq ---- */ -static volatile pid_t ft_helper_pid = 0; -static volatile int ft_atfork_set = 0; -static void ft_helper_child(void) -{ - /* POSIX timers and condvar waiters belong to the parent; timerfds are shared with it and stay */ - for (int i = 0; i < ft_timers_top; i++) - if (ft_timers[i].kind == 2) ft_timers[i].kind = 0; - memset(ft_cws, 0, sizeof ft_cws); - ft_cws_top = 0; - ft_tab_lock_word = 0; - ft_helper_pid = 0; -} - -/* on a page change, every timer armed again and every condvar waiter whose real deadline moved woken; true - while a waiter is still to see it (not planned yet, or broadcast to before it was waiting). */ -static bool ft_on_change(const struct ft_clock_page *s, bool rebroadcast) -{ - sigset_t old; - bool pending = false; - ft_tab_lock(&old); - if (!rebroadcast) - for (int i = 0; i < ft_timers_top; i++) - { - struct ft_timer *t = &ft_timers[i]; - if (t->kind != 0 && t->tracked && t->pg.generation != s->generation) ft_timer_arm(t, s, false); - } - for (int i = 0; i < ft_cws_top; i++) - { - struct ft_cw *w = &ft_cws[i]; - if (!w->used || w->gen == s->generation) continue; - if (w->gen == FT_CW_UNPLANNED) - { - pending = true; - continue; - } - if (!w->woken) - { - int64_t nd = ft_real_of(s, w->due), diff; - diff = (nd == FT_NEVER || w->real_due == FT_NEVER) ? (nd == w->real_due ? 0 : FT_NEVER) : nd - w->real_due; - if (diff >= -1000000 && diff <= 1000000) - { - w->gen = s->generation; /* its real end didn't move */ - continue; - } - } - if (!w->woken || rebroadcast) pthread_cond_broadcast(w->cond); - w->woken = 1; - pending = true; - } - ft_tab_unlock(&old); - return pending; -} - -static void *ft_helper_main(void *arg) -{ - struct ft_clock_page s; - uint64_t gen = UINT64_MAX; /* the first pass catches a change made while it started */ - int backoff_ms = 0; - (void) arg; - for (;;) - { - uint32_t seq32 = ft_seq32(); - bool pending = false; - if (ft_snap(&s)) - { - if (s.generation != gen) - { - gen = s.generation; - pending = ft_on_change(&s, false); - backoff_ms = pending ? 1 : 0; - } - else if (backoff_ms > 0) - { - pending = ft_on_change(&s, true); - backoff_ms = pending ? (backoff_ms < 64 ? backoff_ms * 2 : 100) : 0; - } - } - if (backoff_ms > 0) - { - struct timespec ts = {0, (long) backoff_ms * 1000000L}; - ft_raw_syscall6(SYS_futex, (long) &ft_page->seq, FUTEX_WAIT, (long) seq32, (long) &ts, 0, 0); - } - else - ft_raw_syscall6(SYS_futex, (long) &ft_page->seq, FUTEX_WAIT, (long) seq32, 0, 0, 0); - } - return NULL; -} - -static void ft_helper_start(void) -{ - pid_t pid = getpid(); - sigset_t all, old; - pthread_t thread; - pthread_attr_t attr; - if (__atomic_load_n(&ft_helper_pid, __ATOMIC_ACQUIRE) == pid) return; - sigfillset(&all); - pthread_sigmask(SIG_SETMASK, &all, &old); - while (__atomic_exchange_n(&ft_tab_lock_word, 1, __ATOMIC_ACQUIRE)) sched_yield(); - if (ft_helper_pid != pid) - { - if (!ft_atfork_set) - { - pthread_atfork(NULL, NULL, ft_helper_child); - ft_atfork_set = 1; - } - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - pthread_attr_setstacksize(&attr, 128 * 1024); - if (pthread_create(&thread, &attr, ft_helper_main, NULL) == 0) /* inherits the all-blocked signal mask */ - { - __atomic_store_n(&ft_helper_pid, pid, __ATOMIC_RELEASE); - pthread_setname_np(thread, "agentenv-clock"); - } - pthread_attr_destroy(&attr); - } - __atomic_store_n(&ft_tab_lock_word, 0, __ATOMIC_RELEASE); - pthread_sigmask(SIG_SETMASK, &old, NULL); -} - -/* ---- the waits ---- */ - -/* the raw system calls below as the cancellation points their callers are (clock_nanosleep, sem_timedwait): - cancellation acted on at once for the call alone, as glibc's own waits were before its cancellable system call - wrapper. */ -static inline int ft_async_cancel(void) -{ - int old; - pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old); - return old; -} -static inline void ft_deferred_cancel(int old) -{ - int e = errno, ignored; - pthread_setcanceltype(old, &ignored); - errno = e; -} - -/* a sleep until the universe reaches ``due``, on the page's seq. 0, or EINTR. */ -static int ft_rt_sleep(const struct ft_clock_page *first, int64_t due) -{ - struct ft_clock_page s = *first; - pthread_testcancel(); - for (;;) - { - uint32_t seq32 = (uint32_t) s.seq; - int64_t until; - struct timespec ts; - long r; - int ct; - if (ft_reached(&s, due)) return 0; - until = ft_real_of(&s, due); - ts = ft_ts(until); - ct = ft_async_cancel(); - r = ft_raw_syscall6(SYS_futex, (long) &ft_page->seq, FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME, (long) seq32, - until == FT_NEVER ? 0 : (long) &ts, 0, FUTEX_BITSET_MATCH_ANY); - ft_deferred_cancel(ct); - if (r == -1 && errno == EINTR) return EINTR; - if (!ft_snap(&s)) return 0; - } -} - -struct ft_futex_waitv { uint64_t val, uaddr; uint32_t flags, reserved; }; -static volatile int ft_waitv_missing = 0; -enum { FT_W_WORD, FT_W_PAGE, FT_W_TIMEOUT, FT_W_INTR, FT_W_ERR }; -/* without futex_waitv (before Linux 5.16, under gVisor, or refused by a seccomp filter): the word alone, until the real - deadline or a slice (AGENTENV_WAIT_SLICE_MS) at most, and the caller reads the page again between slices. */ -static int ft_wait1(uint32_t *word, uint32_t val, bool private_word, int64_t until) -{ - int64_t end = ft_real_rt() + ft_wait_slice_ns; - struct timespec ts; - if (until < end) end = until; - ts = ft_ts(end); - if (ft_raw_syscall6(SYS_futex, (long) word, - FUTEX_WAIT_BITSET | FUTEX_CLOCK_REALTIME | (private_word ? FUTEX_PRIVATE_FLAG : 0), (long) val, - (long) &ts, 0, FUTEX_BITSET_MATCH_ANY) == 0 || errno == EAGAIN) - return FT_W_WORD; - if (errno == ETIMEDOUT) return FT_W_PAGE; /* a slice's end, or the deadline: the caller reads the page */ - return errno == EINTR ? FT_W_INTR : FT_W_ERR; -} -/* until *word isn't val, the page changes from seq32, or real realtime ``until``; FT_W_ERR with errno the kernel's - (an unaligned or unmapped word). */ -static int ft_wait2(uint32_t *word, uint32_t val, bool private_word, uint32_t seq32, int64_t until) -{ - struct ft_futex_waitv w[2]; - struct timespec ts = ft_ts(until); - long r; - if (ft_waitv_missing) return ft_wait1(word, val, private_word, until); - /* the page first: futex_waitv returns the last of the futexes woken, so a caller's wake is never taken for the page's */ - w[0].val = seq32; - w[0].uaddr = (uint64_t) (uintptr_t) &ft_page->seq; - w[0].flags = 0x02; /* FUTEX2_SIZE_U32, shared */ - w[0].reserved = 0; - w[1].val = val; - w[1].uaddr = (uint64_t) (uintptr_t) word; - w[1].flags = 0x02 | (private_word ? 128 : 0); - w[1].reserved = 0; - r = ft_raw_syscall6(SYS_futex_waitv, (long) w, 2, 0, until == FT_NEVER ? 0 : (long) &ts, CLOCK_REALTIME, 0); - if (r == 1) return FT_W_WORD; - if (r == 0) return FT_W_PAGE; - if (errno == EAGAIN) return __atomic_load_n(word, __ATOMIC_RELAXED) != val ? FT_W_WORD : FT_W_PAGE; - if (errno == ETIMEDOUT) return FT_W_TIMEOUT; - if (errno == EINTR) return FT_W_INTR; - if (errno != ENOSYS && errno != EPERM) return FT_W_ERR; /* EPERM: a seccomp filter that refuses it so */ - ft_waitv_missing = 1; - return ft_wait1(word, val, private_word, until); -} - -/* futex(2) FUTEX_WAIT_BITSET|FUTEX_CLOCK_REALTIME through syscall(): until the universe reaches the deadline, - beside the page; *handled false leaves the call to the kernel as it is. */ -static long ft_u_futex_rt(uint32_t *uaddr, int op, uint32_t val, const struct timespec *abs, bool *handled) -{ - struct ft_clock_page s; - int64_t due = ft_ns(abs); - *handled = false; - if (!ft_snap(&s)) return 0; - *handled = true; - for (;;) - { - uint32_t seq32 = (uint32_t) s.seq; - int k; - if (ft_reached(&s, due)) - { - errno = ETIMEDOUT; - return -1; - } - k = ft_wait2(uaddr, val, (op & FUTEX_PRIVATE_FLAG) != 0, seq32, ft_real_of(&s, due)); - if (k == FT_W_WORD) - { - if (__atomic_load_n(uaddr, __ATOMIC_RELAXED) != val) - { - errno = EAGAIN; - return -1; - } - return 0; - } - if (k == FT_W_INTR) - { - errno = EINTR; - return -1; - } - if (k == FT_W_ERR) return -1; - if (!ft_snap(&s)) return 0; - } -} - -/* sem_timedwait / sem_clockwait(CLOCK_REALTIME) on glibc's semaphore (its layout is part of the ABI, since - process-shared semaphores outlive glibc versions): the value in the low word of data, the waiters above it, the - futex the value word, which sem_post wakes when there are waiters; ``private`` is glibc's futex flag the other way - round (0 for a process-private semaphore, FUTEX_PRIVATE_FLAG for a shared one). */ -struct ft_new_sem { uint64_t data; int private_flag; int pad; }; -static void ft_sem_unwait(void *sem) -{ - __atomic_fetch_sub(&((struct ft_new_sem *) sem)->data, (uint64_t) 1 << 32, __ATOMIC_RELAXED); -} -/* as one of the semaphore's waiters (counted in data's high word): 0 with a token taken, or the error. A cancelled wait - leaves the waiters it joined, as glibc's does. */ -static int ft_sem_wait_slow(struct ft_new_sem *sem, struct ft_clock_page *s, int64_t due) -{ - volatile int err = 0; - pthread_cleanup_push(ft_sem_unwait, sem); - uint64_t d = __atomic_fetch_add(&sem->data, (uint64_t) 1 << 32, __ATOMIC_RELAXED); - for (;;) - { - if ((d & 0xffffffffu) == 0) - { - int k, ct; - if (ft_reached(s, due)) - { - err = ETIMEDOUT; - break; - } - ct = ft_async_cancel(); - k = ft_wait2((uint32_t *) &sem->data, 0, sem->private_flag == 0, (uint32_t) s->seq, ft_real_of(s, due)); - ft_deferred_cancel(ct); - if (k == FT_W_INTR || k == FT_W_ERR) - { - err = k == FT_W_INTR ? EINTR : errno; - break; - } - if (!ft_snap(s)) - { - err = ETIMEDOUT; - break; - } - d = __atomic_load_n(&sem->data, __ATOMIC_RELAXED); - } - else if (__atomic_compare_exchange_n(&sem->data, &d, d - 1 - ((uint64_t) 1 << 32), true, __ATOMIC_ACQUIRE, - __ATOMIC_RELAXED)) - break; - } - pthread_cleanup_pop(err != 0); - return err; -} -static int ft_u_sem_wait(sem_t *sem_, int64_t due, bool *handled) -{ - struct ft_new_sem *sem = (struct ft_new_sem *) sem_; - struct ft_clock_page s; - uint64_t d; - int err; - *handled = false; - if (sizeof(sem_t) < sizeof(struct ft_new_sem) || !ft_snap(&s)) return 0; - *handled = true; - pthread_testcancel(); - d = __atomic_load_n(&sem->data, __ATOMIC_RELAXED); - while ((d & 0xffffffffu) > 0) - if (__atomic_compare_exchange_n(&sem->data, &d, d - 1, true, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) return 0; - err = ft_sem_wait_slow(sem, &s, due); - if (err == 0) return 0; - errno = err; - return -1; -} - -static void ft_cw_release(void *w) -{ - sigset_t old; - ft_tab_lock(&old); - ((struct ft_cw *) w)->used = 0; - ft_tab_unlock(&old); -} -/* glibc's wait, a cancellation point: a cancelled wait gives up its entry, so the helper never wakes a condvar that - may be gone. */ -static int ft_cw_clockwait(struct ft_cw *w, pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *end) -{ - volatile int r; - pthread_cleanup_push(ft_cw_release, w); - r = real_pthread_cond_clockwait(cond, mutex, CLOCK_REALTIME, end); - pthread_cleanup_pop(0); - return r; -} - -/* a realtime condvar wait (see ft_cw). -1 leaves it to the caller's own path (no page, table full). */ -static int ft_u_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) -{ - struct ft_clock_page s; - struct ft_cw *w = NULL; - struct timespec end; - sigset_t old; - int64_t due = ft_ns(abstime), real_due; - int r, woken; - if (!ft_snap(&s)) return -1; - if (ft_reached(&s, due)) return ETIMEDOUT; - ft_helper_start(); - ft_tab_lock(&old); - for (int i = 0; i < FT_CW_MAX; i++) - if (!ft_cws[i].used) - { - w = &ft_cws[i]; - if (i >= ft_cws_top) ft_cws_top = i + 1; - break; - } - if (w != NULL) - { - w->cond = cond; - w->due = due; - w->woken = 0; - w->gen = FT_CW_UNPLANNED; - w->used = 1; - } - ft_tab_unlock(&old); - if (w == NULL) return -1; - if (!ft_snap(&s)) s.armed = 0; - real_due = ft_real_of(&s, due); - ft_tab_lock(&old); - w->real_due = real_due; - w->gen = s.generation; - ft_tab_unlock(&old); - end = ft_ts(real_due == FT_NEVER ? ft_real_rt() + 86400 * FT_NS : real_due); - r = ft_cw_clockwait(w, cond, mutex, &end); - ft_tab_lock(&old); - woken = w->woken; - w->used = 0; - ft_tab_unlock(&old); - /* a real timeout before the universe's deadline (the page moved it later) is a spurious wakeup; the helper's wake for - a jump that passed the deadline is the timeout */ - if (r == ETIMEDOUT) return (ft_snap(&s) && !ft_reached(&s, due)) ? 0 : ETIMEDOUT; - if (r == 0 && woken && ft_snap(&s) && ft_reached(&s, due)) return ETIMEDOUT; - return r; -} - -/* a realtime mutex wait: glibc's mutex has no way in, so slices (AGENTENV_WAIT_SLICE_MS) of a real - CLOCK_MONOTONIC wait, which loses nothing when it times out, with the page read between them. */ -static int ft_u_mutex_lock(pthread_mutex_t *mutex, int64_t due, bool *handled) -{ - struct ft_clock_page s; - *handled = false; - if (!ft_snap(&s)) return 0; - *handled = true; - for (;;) - { - struct timespec now, end; - int64_t at, sl; - int r; - if (ft_reached(&s, due)) return ETIMEDOUT; - at = ft_real_of(&s, due); - sl = (at == FT_NEVER) ? ft_wait_slice_ns : at - ft_real_rt(); - if (sl > ft_wait_slice_ns) sl = ft_wait_slice_ns; - if (sl < 1) sl = 1; - (*real_clock_gettime)(CLOCK_MONOTONIC, &now); - end = ft_ts(ft_ns(&now) + sl); - r = real_pthread_mutex_clocklock(mutex, CLOCK_MONOTONIC, &end); - if (r == EINVAL) /* a PI mutex on a kernel without FUTEX_LOCK_PI2 */ - { - struct timespec real = ft_ts(ft_real_rt() + sl); - r = real_pthread_mutex_timedlock(mutex, &real); - } - if (r != ETIMEDOUT) return r; - if (!ft_snap(&s)) return ETIMEDOUT; - } -} - -/* syscall(): futex waits for a realtime clock time follow the universe (raw futex users such as Rust's std - and GLib call it), clock_gettime reads it once the library is up; everything else goes straight to the kernel. */ -long syscall(long number, ...) -{ - va_list ap; - long a[6]; - va_start(ap, number); - for (int i = 0; i < 6; i++) a[i] = va_arg(ap, long); - va_end(ap); - if (number == SYS_futex && ft_uw_active()) - { - int op = (int) a[1]; - const struct timespec *ts = (const struct timespec *) a[3]; - if ((op & FUTEX_CMD_MASK) == FUTEX_WAIT_BITSET && (op & FUTEX_CLOCK_REALTIME) && ft_valid_ts(ts) - && (uint32_t) a[5] == FUTEX_BITSET_MATCH_ANY) - { - bool handled; - long r = ft_u_futex_rt((uint32_t *) a[0], op, (uint32_t) a[2], ts, &handled); - if (handled) return r; - } - } - else if (number == SYS_clock_gettime && ft_page != NULL && !ft_disabled && !dont_fake) - return clock_gettime((clockid_t) a[0], (struct timespec *) a[1]); - return ft_raw_syscall6(number, a[0], a[1], a[2], a[3], a[4], a[5]); -} - -/* ---- timerfds ---- */ -static int ft_u_timerfd_settime(int fd, int flags, const struct itimerspec *nv, struct itimerspec *ov, bool *handled) -{ - struct ft_clock_page s; - struct ft_timer *t = NULL; - struct itimerspec prev; - sigset_t old; - int clock, r; - bool was = false; - *handled = false; - if (fd < 0 || fd >= FT_TFD_CLOCKS || nv == NULL) return 0; - clock = ft_tfd_clock[fd] - 1; - *handled = true; - if (ft_timers_any) - { - ft_tab_lock(&old); - t = ft_timer_find_fd(fd); - if (t != NULL && t->tracked) - { - ft_timer_value(t, &prev); - was = true; - t->tracked = 0; /* the record stays, with the timer's descriptors, while any is open */ - } - ft_tab_unlock(&old); - } - if (!ft_rt_clock(clock) || !(flags & TFD_TIMER_ABSTIME) || !ft_valid_ts(&nv->it_value) - || !ft_valid_ts(&nv->it_interval) || (nv->it_value.tv_sec == 0 && nv->it_value.tv_nsec == 0) || !ft_snap(&s)) - { - r = (*real_timerfd_settime)(fd, flags, nv, ov); - if (r == 0 && ov != NULL && was) *ov = prev; - return r; - } - ft_tab_lock(&old); - if ((t = ft_timer_find_fd(fd)) == NULL && (t = ft_timer_new()) != NULL) - { - t->kind = 1; - t->nfds = 1; - t->fds[0] = fd; - } - if (t == NULL) - { - ft_tab_unlock(&old); - *handled = false; /* table full: the caller's own (translated once) path */ - return 0; - } - t->clock = clock; - t->flags = flags; - t->due = ft_ns(&nv->it_value); - t->interval = ft_ns(&nv->it_interval); - t->tracked = 1; - if (ov != NULL) - { - if (was) - *ov = prev; - else - (*real_timerfd_gettime)(fd, ov); - } - r = ft_timer_arm(t, &s, true); - if (r != 0) t->tracked = 0; - ft_timers_any = 1; - ft_tab_unlock(&old); - if (r == 0) ft_helper_start(); - return r; -} -static int ft_u_timerfd_gettime(int fd, struct itimerspec *cur, bool *handled) -{ - struct ft_timer *t; - sigset_t old; - *handled = false; - if (!ft_timers_any || cur == NULL) return 0; - ft_tab_lock(&old); - t = ft_timer_find_fd(fd); - if (t != NULL && t->tracked) - { - ft_timer_value(t, cur); - *handled = true; - } - ft_tab_unlock(&old); - return 0; -} - -/* - * ---- descriptors: a timerfd's record follows its file, not a number. close() takes a descriptor off it (the record - * goes with the last), dup() and its kin add the new one, so the helper never arms a number the program has closed or - * that now names another file. A number closed where this can't see (closefrom, a raw system call) is dropped when - * timerfd_create hands it out again; a timer with more duplicates than a record holds is left armed as it was. ---- - */ -static int (*ft_real_close)(int); -static int (*ft_real_dup)(int); -static int (*ft_real_dup2)(int, int); -static int (*ft_real_dup3)(int, int, int); -static int (*ft_real_fcntl)(int, int, ...); -static int (*ft_real_fcntl64)(int, int, ...); -static int (*ft_real_close_range)(unsigned int, unsigned int, int); -#define FT_RESOLVE(name) do { if (ft_real_##name == NULL) ft_real_##name = dlsym(RTLD_NEXT, #name); } while (0) - -static inline bool ft_tfd_known(int fd) -{ - return fd >= 0 && fd < FT_TFD_CLOCKS && __atomic_load_n(&ft_tfd_clock[fd], __ATOMIC_RELAXED) != 0; -} -/* the process whose descriptors these tables describe: set at load and in the child of each glibc fork. A child that - shares this memory without fork (vfork, as Python's subprocess uses, or a CLONE_VM clone) has a descriptor table of - its own, so what it closes or duplicates before exec changes nothing here. A fork's child also takes the tables' - lock free: another thread may have held it at the fork, and no thread of the child would ever release it. */ -static volatile pid_t ft_fd_owner = 0; -static void ft_fd_forked(void) -{ - ft_fd_owner = getpid(); - ft_tab_lock_word = 0; -} -static inline bool ft_fd_mine(void) -{ - return getpid() == ft_fd_owner; -} -/* ``fd`` closed, or about to name another file. Lock held. */ -static void ft_fd_gone_locked(int fd) -{ - struct ft_timer *t; - ft_tfd_clock[fd] = 0; - while ((t = ft_timer_find_fd(fd)) != NULL) - { - int k = 0; - while (t->fds[k] != fd) k++; - t->fds[k] = t->fds[--t->nfds]; - if (t->nfds == 0) t->kind = 0; - } -} -static void ft_fd_gone(int fd) -{ - sigset_t old; - if (!ft_tfd_known(fd) || !ft_fd_mine()) return; - ft_tab_lock(&old); - ft_fd_gone_locked(fd); - ft_tab_unlock(&old); -} -/* ``to`` now names ``from``'s file, a known timerfd. Lock held. */ -static void ft_fd_duped_locked(int from, int to) -{ - struct ft_timer *t; - if (to < 0 || to >= FT_TFD_CLOCKS) return; - ft_tfd_clock[to] = ft_tfd_clock[from]; - if ((t = ft_timer_find_fd(from)) != NULL) - { - if (t->nfds < FT_TIMER_FDS) - t->fds[t->nfds++] = to; - else - t->kind = 0; - } -} -static void ft_fd_duped(int from, int to) -{ - sigset_t old; - if (!ft_tfd_known(from) || !ft_fd_mine()) return; - ft_tab_lock(&old); - ft_fd_duped_locked(from, to); - ft_tab_unlock(&old); -} -/* dup2 or dup3 onto ``fd2``. One that replaces a timerfd (whose close doesn't block) makes the change and its - bookkeeping under the lock, so a failed call keeps the record and the helper never arms the number in between. */ -static int ft_dup_onto(int fd, int fd2, int flags, bool three) -{ - sigset_t old; - int r; - FT_RESOLVE(dup2); - FT_RESOLVE(dup3); - if (fd == fd2 || !ft_tfd_known(fd2) || !ft_fd_mine()) - { - r = three ? (*ft_real_dup3)(fd, fd2, flags) : (*ft_real_dup2)(fd, fd2); - if (r >= 0 && fd != fd2) ft_fd_duped(fd, r); - return r; - } - ft_tab_lock(&old); - r = three ? (*ft_real_dup3)(fd, fd2, flags) : (*ft_real_dup2)(fd, fd2); - if (r >= 0) - { - ft_fd_gone_locked(fd2); - if (ft_tfd_known(fd)) ft_fd_duped_locked(fd, fd2); - } - ft_tab_unlock(&old); - return r; -} - -int close(int fd) -{ - ft_fd_gone(fd); - FT_RESOLVE(close); - return (*ft_real_close)(fd); -} -int dup(int fd) -{ - int r; - FT_RESOLVE(dup); - r = (*ft_real_dup)(fd); - if (r >= 0) ft_fd_duped(fd, r); - return r; -} -int dup2(int fd, int fd2) -{ - return ft_dup_onto(fd, fd2, 0, false); -} -int dup3(int fd, int fd2, int flags) -{ - return ft_dup_onto(fd, fd2, flags, true); -} -/* the argument passed on as glibc's own fcntl reads it, a pointer-sized word whatever the command */ -int fcntl(int fd, int cmd, ...) -{ - va_list ap; - void *arg; - int r; - va_start(ap, cmd); - arg = va_arg(ap, void *); - va_end(ap); - FT_RESOLVE(fcntl); - r = (*ft_real_fcntl)(fd, cmd, arg); - if (r >= 0 && (cmd == F_DUPFD || cmd == F_DUPFD_CLOEXEC)) ft_fd_duped(fd, r); - return r; -} -int fcntl64(int fd, int cmd, ...) -{ - va_list ap; - void *arg; - int r; - va_start(ap, cmd); - arg = va_arg(ap, void *); - va_end(ap); - FT_RESOLVE(fcntl64); - r = (*ft_real_fcntl64)(fd, cmd, arg); - if (r >= 0 && (cmd == F_DUPFD || cmd == F_DUPFD_CLOEXEC)) ft_fd_duped(fd, r); - return r; -} -/* one that closes a timerfd runs under the lock, as dup2 does, so a failed call keeps the records. In practice it runs - in a child of fork before exec, where no other thread wants the lock. */ -int close_range(unsigned int first, unsigned int last, int flags) -{ - sigset_t old; - bool timerfd = false; - int r; - FT_RESOLVE(close_range); - if (!(flags & CLOSE_RANGE_CLOEXEC) && ft_fd_mine()) - for (unsigned int fd = first; fd <= last && fd < FT_TFD_CLOCKS && !timerfd; fd++) timerfd = ft_tfd_known((int) fd); - if (!timerfd) return (*ft_real_close_range)(first, last, flags); - ft_tab_lock(&old); - r = (*ft_real_close_range)(first, last, flags); - if (r == 0) - for (unsigned int fd = first; fd <= last && fd < FT_TFD_CLOCKS; fd++) - if (ft_tfd_known((int) fd)) ft_fd_gone_locked((int) fd); - ft_tab_unlock(&old); - return r; -} -/* the owner, and the real calls resolved at load, so a first close() in a signal handler doesn't call dlsym */ -static void __attribute__((constructor)) ft_fd_init(void) -{ - ft_fd_owner = getpid(); - pthread_atfork(NULL, NULL, ft_fd_forked); - FT_RESOLVE(close); - FT_RESOLVE(dup); - FT_RESOLVE(dup2); - FT_RESOLVE(dup3); - FT_RESOLVE(fcntl); - FT_RESOLVE(fcntl64); - FT_RESOLVE(close_range); -} - -/* ---- POSIX timers (unversioned: upstream's GLIBC_2.2 / 2.3.3 versions don't bind glibc 2.34's timer_settime) ---- */ -int timer_create(clockid_t clock, struct sigevent *sev, timer_t *id) -{ - int r; - ftpl_init(); - if (real_timer_create_new == NULL) real_timer_create_new = dlsym(RTLD_NEXT, "timer_create"); - if (real_timer_create_new == NULL) - { - errno = ENOSYS; - return -1; - } - r = (*real_timer_create_new)(clock, sev, id); - if (r == 0 && id != NULL && ft_rt_clock(clock) && ft_faked()) - { - sigset_t old; - struct ft_timer *t; - ft_tab_lock(&old); - t = ft_timer_find_id(*id); - if (t == NULL) t = ft_timer_new(); - if (t != NULL) - { - memset(t, 0, sizeof *t); - t->kind = 2; - t->id = *id; - t->clock = clock; - } - ft_timers_any = 1; - ft_tab_unlock(&old); - } - return r; -} -int timer_delete(timer_t id) -{ - ftpl_init(); - if (real_timer_delete_new == NULL) real_timer_delete_new = dlsym(RTLD_NEXT, "timer_delete"); - if (ft_timers_any) - { - sigset_t old; - struct ft_timer *t; - ft_tab_lock(&old); - if ((t = ft_timer_find_id(id)) != NULL) t->kind = 0; - ft_tab_unlock(&old); - } - return (*real_timer_delete_new)(id); -} -int timer_settime(timer_t id, int flags, const struct itimerspec *nv, struct itimerspec *ov) -{ - struct ft_clock_page s; - struct ft_timer *t; - struct itimerspec prev; - sigset_t old; - bool was = false; - int r; - ftpl_init(); - if (real_timer_settime_new == NULL) real_timer_settime_new = dlsym(RTLD_NEXT, "timer_settime"); - if (real_timer_gettime_new == NULL) real_timer_gettime_new = dlsym(RTLD_NEXT, "timer_gettime"); - if (!ft_timers_any || nv == NULL) return (*real_timer_settime_new)(id, flags, nv, ov); - ft_tab_lock(&old); - t = ft_timer_find_id(id); - if (t != NULL && t->tracked) - { - ft_timer_value(t, &prev); - was = true; - t->tracked = 0; - } - ft_tab_unlock(&old); - if (t == NULL || !ft_uw_active() || !(flags & TIMER_ABSTIME) || !ft_valid_ts(&nv->it_value) - || !ft_valid_ts(&nv->it_interval) || (nv->it_value.tv_sec == 0 && nv->it_value.tv_nsec == 0) || !ft_snap(&s)) - { - r = (*real_timer_settime_new)(id, flags, nv, ov); - if (r == 0 && ov != NULL && was) *ov = prev; - return r; - } - ft_tab_lock(&old); - if (ov != NULL) - { - if (was) - *ov = prev; - else - (*real_timer_gettime_new)(id, ov); - } - t->due = ft_ns(&nv->it_value); - t->interval = ft_ns(&nv->it_interval); - t->tracked = 1; - r = ft_timer_arm(t, &s, true); - ft_tab_unlock(&old); - if (r == 0) ft_helper_start(); - return r; -} -int timer_gettime(timer_t id, struct itimerspec *cur) -{ - sigset_t old; - struct ft_timer *t; - ftpl_init(); - if (real_timer_gettime_new == NULL) real_timer_gettime_new = dlsym(RTLD_NEXT, "timer_gettime"); - if (ft_timers_any && cur != NULL) - { - ft_tab_lock(&old); - t = ft_timer_find_id(id); - if (t != NULL && t->tracked) - { - ft_timer_value(t, cur); - ft_tab_unlock(&old); - return 0; - } - ft_tab_unlock(&old); - } - return (*real_timer_gettime_new)(id, cur); -} - diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/clock/build.sh b/src/agent_env/computers/artifacts/ubuntu-24.04/clock/build.sh deleted file mode 100755 index 8d7227284..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/clock/build.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/sh -# Builds agent-env's libfaketime: upstream 0.9.12 from its sha256-pinned tarball, libfaketime.patch applied and the two -# engine files beside it copied in, the multithreaded library with stat and utime faking compiled out (file times stay -# real); and the TLS shim beside it (tls_realtime.c). Writes OUT/libfaketimeMT.so.1 and OUT/libtls_realtime.so, which -# /etc/ld.so.preload lists in that order. Needs a C toolchain, make, patch and curl; builds on x86_64 and aarch64. -# build.sh OUT [TARBALL] (TARBALL: a local copy of upstream's v0.9.12 tarball, else it is downloaded) -set -eu -out=$1 -here=$(cd "$(dirname "$0")" && pwd) -work=$(mktemp -d) -trap 'rm -rf "$work"' EXIT -tarball=${2:-$work/libfaketime.tar.gz} -[ -f "$tarball" ] || curl -fsSL -o "$tarball" https://github.com/wolfcw/libfaketime/archive/refs/tags/v0.9.12.tar.gz -echo "4fc32218697c052adcdc5ee395581f2554ca56d086ac817ced2be0d6f1f8a9fa $tarball" | sha256sum -c - >/dev/null -tar xzf "$tarball" -C "$work" -src=$work/libfaketime-0.9.12 -patch -s -d "$src" -p1 < "$here/libfaketime.patch" -cp "$here/agentenv_clock_page.c" "$here/agentenv_clock_waits.c" "$src/src/" -make -s -C "$src/src" FAKETIME_COMPILE_CFLAGS="-UFAKE_STAT -UFAKE_UTIME" libfaketimeMT.so.1 -mkdir -p "$out" -cp "$src/src/libfaketimeMT.so.1" "$out/" -${CC:-cc} -std=gnu99 -O2 -fPIC -shared -Wall -Wextra -Werror -o "$out/libtls_realtime.so" "$here/tls_realtime.c" -ldl diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/clock/clock-follower b/src/agent_env/computers/artifacts/ubuntu-24.04/clock/clock-follower deleted file mode 100755 index 652431687..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/clock/clock-follower +++ /dev/null @@ -1,204 +0,0 @@ -#!/usr/bin/python3 -I -"""The computer's clock follower: holds a long poll on the gateway's ``/clock/watch`` and writes each state it learns -to the clock page (``clockpage.py``), which agent-env's libfaketime reads in every session program, so the session reads -the universe's time. It runs as its own user, under root's supervisord, so the session user can read the clock but -neither write it nor stop it. - -Core writes its config, the gateway's watch URL as this computer reaches it, the computer's name and its read token, -through the sandbox's root channel (``CONFIG``, readable by root and this user alone); never through the driver, which -runs as the session user. It reads the config every quarter second, during a watch too, and follows a changed one at -once. Until there is one, the clock stays as root made it at boot: real time. It reports how it's doing in ``STATUS``, -which anyone may read and which holds no token: the gateway it follows, whether it's connected, the version it shows, -or its last error. - -The gateway's clock is read against this machine's: its offset is four-stamp, ``((received - t1) + (sent - t4)) / 2``, -from the newest of the last 8 answers whose round trip, less the time the gateway held the watch, isn't an outlier. A -state is written anchored, universe V at this machine's real time A (the gateway's ``sent``) at rate R, so every reader -computes V + (now - A) * R. Each watch names the version it shows, which is its acknowledgement of that version.""" -import json -import os -import queue -import random -import sys -import threading -import time -import urllib.error -import urllib.parse -import urllib.request -from datetime import datetime - -sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) -import clockpage # noqa: E402 - -CONFIG = os.environ.get("AGENTENV_CLOCK_CONFIG", "/etc/agentenv/clock/follower.json") -STATUS = os.environ.get("AGENTENV_CLOCK_STATUS", "/run/agentenv-clock/status.json") -#: How long a watch may take: the gateway holds one up to 25 s (its card's ``hold_s``), plus a margin. -WATCH_TIMEOUT_S = 35.0 -#: How often the config is read, during a watch too. -CONFIG_POLL_S = 0.25 -#: A re-anchoring (the offset moved) is written only past this much, so the page doesn't churn on noise. -ADJUST_S = 0.001 -SAMPLES = 8 -#: After a failed watch it waits before asking again: RETRY_FIRST_S at first, doubled after each failure up to -#: RETRY_MAX_S, each wait a random fraction (RETRY_JITTER) of that, so computers that lost a gateway don't all return at -#: once. -RETRY_FIRST_S = 0.05 -RETRY_MAX_S = 2.0 -RETRY_JITTER = (0.5, 1.0) -OPENER = urllib.request.build_opener(urllib.request.ProxyHandler({})) - - -def log(event: str, **fields) -> None: - sys.stdout.write(json.dumps({"event": event, "real": time.time(), **fields}) + "\n") - sys.stdout.flush() - - -def atomic_write(path: str, text: str) -> None: - tmp = f"{path}.{os.getpid()}.tmp" - with open(tmp, "w") as f: - f.write(text) - os.chmod(tmp, 0o644) - os.replace(tmp, path) - - -def read_config() -> tuple[bytes | None, dict | None]: - """The config's bytes and what they say, (None, None) when there is none, (bytes, None) when it isn't one.""" - try: - with open(CONFIG, "rb") as f: - raw = f.read() - except OSError: - return None, None - try: - config = json.loads(raw) - return raw, config if isinstance(config, dict) and all( - isinstance(config.get(key), str) and config[key] for key in ("watch_url", "name", "token")) else None - except ValueError: - return raw, None - - -def parse_time(text: str) -> float: - return datetime.fromisoformat(text.replace("Z", "+00:00")).timestamp() - - -def offset(samples: list[dict]) -> tuple[float, int]: - """The offset from the newest sample whose delay isn't an outlier (over twice the median and 2 ms), and how many - were dropped.""" - delays = sorted(sample["delay"] for sample in samples) - median = delays[len(delays) // 2] - kept = [sample for sample in samples if sample["delay"] <= max(2 * median, median + 0.002)] - return (kept or samples)[-1]["theta"], len(samples) - len(kept) - - -def state_of(body: dict, theta: float) -> tuple[int, float, float, float]: - """(armed, universe at the anchor, rate, anchor on this machine's clock) for a watch's answer ``body``.""" - if not body.get("armed"): - return 0, 0.0, 1.0, time.time() - return (1, parse_time(body["virtual_time"]), float(body["virtual_seconds_per_real_second"]), - body["sent"] - theta) - - -def watch(url: str, name: str, token: str, after: str, out: queue.Queue) -> None: - """One watch, its answer (or what failed) put on ``out`` with the stamps around it.""" - t1 = time.time() - try: - request = urllib.request.Request( - f"{url}?{urllib.parse.urlencode({'after': after, 'follower': name})}", - headers={"Authorization": f"Bearer {token}"}) - with OPENER.open(request, timeout=WATCH_TIMEOUT_S) as answer: - body = json.loads(answer.read()) - out.put((t1, time.time(), body, None)) - except Exception as e: # noqa: BLE001 - away, restarting or refusing: the follower says so and asks again - out.put((t1, time.time(), None, e)) - - -class Follower: - """Follows one config until it changes.""" - - def __init__(self, raw: bytes, config: dict): - self.raw, self.url, self.name, self.token = raw, config["watch_url"], config["name"], config["token"] - self.after, self.samples, self.written = "", [], None - self.errors, self.backoff = 0, RETRY_FIRST_S - self.status = {"url": self.url, "name": self.name, "connected": False, "version": None, "armed": False, - "error": None} - - def report(self, **fields) -> None: - self.status.update(fields) - atomic_write(STATUS, json.dumps(self.status)) - - def run(self) -> None: - log("following", url=self.url, name=self.name) - self.report() - while True: - answers: queue.Queue = queue.Queue() - threading.Thread(target=watch, args=(self.url, self.name, self.token, self.after, answers), - daemon=True).start() - while True: - try: - t1, t4, body, error = answers.get(timeout=CONFIG_POLL_S) - break - except queue.Empty: - if read_config()[0] != self.raw: - return # its watch is left to end on its own; nothing it answers is applied - if error is not None: - self.failed(error) - continue - if read_config()[0] != self.raw: - return - try: - self.apply(t1, t4, body) - except (KeyError, TypeError, ValueError, AttributeError) as e: - self.failed(ValueError(f"not a clock watch's answer ({type(e).__name__}: {e})")) - - def failed(self, error: Exception) -> None: - self.errors += 1 - code = error.code if isinstance(error, urllib.error.HTTPError) else None - self.report(connected=False, error=f"{type(error).__name__}: {error}"[:300], status=code, errors=self.errors, - error_at=time.time()) - if self.errors <= 20 or self.errors % 100 == 0: - log("error", errors=self.errors, status=code, error=repr(error)[:200], backoff=self.backoff) - deadline = time.monotonic() + self.backoff * random.uniform(*RETRY_JITTER) - while time.monotonic() < deadline and read_config()[0] == self.raw: - time.sleep(min(CONFIG_POLL_S, max(0.0, deadline - time.monotonic()))) - self.backoff = min(self.backoff * 2, RETRY_MAX_S) - - def apply(self, t1: float, t4: float, body: dict) -> None: - if self.errors: - log("reconnected", after_errors=self.errors) - self.errors, self.backoff = 0, RETRY_FIRST_S - held = body["sent"] - body["received"] - self.samples = (self.samples + [{"delay": max(0.0, (t4 - t1) - held), - "theta": ((body["received"] - t1) + (body["sent"] - t4)) / 2}])[-SAMPLES:] - theta, dropped = offset(self.samples) - armed, universe, rate, anchor = state_of(body, theta) - meaning = (armed, rate, universe - rate * anchor if armed else 0.0) - changed = body["version"] != self.after - moved = (self.written is None or meaning[:2] != self.written[:2] - or abs(meaning[2] - self.written[2]) > ADJUST_S) - if changed or moved: - written = clockpage.write(armed, universe, rate, anchor) - self.written = meaning - log("applied" if changed else "adjusted", version=body["version"], armed=bool(armed), - virtual_time=body.get("virtual_time"), spec=written["spec"], theta=theta, held=held, dropped=dropped) - self.after = body["version"] - self.report(connected=True, version=self.after, armed=bool(armed), virtual_time=body.get("virtual_time"), - rate=body.get("virtual_seconds_per_real_second"), theta=theta, applied_at=time.time(), error=None, - status=None, errors=0) - - -def main() -> None: - log("start", pid=os.getpid(), uid=os.getuid()) - atomic_write(STATUS, json.dumps({"connected": False, "error": None, "waiting": "no config yet"})) - while True: - raw, config = read_config() - if config is None: - if raw is not None: - atomic_write(STATUS, json.dumps({"connected": False, "error": "its config isn't one: it needs " - "watch_url, name and token"})) - while read_config()[0] == raw: - time.sleep(CONFIG_POLL_S) - continue - Follower(raw, config).run() - - -if __name__ == "__main__": - main() diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/clock/clock.conf b/src/agent_env/computers/artifacts/ubuntu-24.04/clock/clock.conf deleted file mode 100644 index 715987d43..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/clock/clock.conf +++ /dev/null @@ -1,11 +0,0 @@ -# Read once by agent-env's libfaketime as each process loads it. While this file exists it is the only source of these -# settings: the environment changes none of them (AGENTENV_REAL_TIME=1 is the one switch a program's environment has). -# Only the session user's processes are faked; the clock comes from the page the follower writes, never from a cache. -AGENTENV_SESSION_UID=1000 -FAKETIME_TIMESTAMP_FILE=/run/agentenv-clock/clock -FAKETIME_NO_CACHE=1 -FAKETIME_DONT_FAKE_MONOTONIC=1 -FAKETIME_FMT=%s -FAKETIME_DISABLE_SHM=1 -AGENTENV_CLOCK_PAGE=/dev/shm/agentenv-clock -AGENTENV_CLOCK_PAGE_UID=0 diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/clock/clockpage.py b/src/agent_env/computers/artifacts/ubuntu-24.04/clock/clockpage.py deleted file mode 100644 index 4002bb6c2..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/clock/clockpage.py +++ /dev/null @@ -1,157 +0,0 @@ -"""The follower's side of the clock page (version 2) and of the clock file: writes the universe's realtime -clock to both. Elapsed time is real, so the page's monotonic fields are written as the real monotonic clock (identity) -and no library reads them; they stay so the page keeps its 80-byte layout. - - clockpage.py set UNIVERSE RATE [ANCHOR] armed: UNIVERSE (epoch s) at real time ANCHOR (default now), at RATE - clockpage.py clear real time - clockpage.py create OWNER as root at boot: the page, OWNER's from its first byte, unarmed - clockpage.py show - -The page (default /dev/shm/agentenv-clock, a tmpfs, so readers that mapped it see every write) is created once, 0644, -owned by whoever writes it, and rewritten in place: seq odd, the fields and their check, seq even, then FUTEX_WAKE on -seq, which wakes every wait agent-env's libfaketime has for a clock time, and its helper threads, so they plan again. -Readers never see a torn value: they retry (waiting on seq) while seq is odd or changed, or the check doesn't match. -A page a writer died writing (seq left odd) is taken up by the next write, which then writes the universe as given. - -A write that would put the universe less than CONTINUITY_NS behind where the page has it now (a rate change computed -from a universe read a moment before the write) continues it from where it is instead: the write's real time is taken -last, while seq is odd, and the universe at it read from the page being replaced, so no reader of either page ever sees -the clock step back. A larger step back is a set_time and is written as given.""" -import ctypes -import mmap -import os -import platform -import pwd -import stat -import struct -import sys -import time -from decimal import Decimal - -PAGE = os.environ.get("AGENTENV_CLOCK_PAGE", "/dev/shm/agentenv-clock") -FILE = os.environ.get("AGENTENV_CLOCK_FILE", "/run/agentenv-clock/clock") -MAGIC, VERSION = 0x4B434541, 2 -# magic version seq universe_ns anchor_ns rate armed reserved generation check mono_universe_ns mono_anchor_ns -LAYOUT = struct.Struct(" int: - rate_bits = struct.unpack(" bytes: - """The page's bytes for these fields, its check among them: what every write writes and every reader checks, - agent-env's libfaketime and the egress proxy (its egress/testdata pins them).""" - return LAYOUT.pack(MAGIC, VERSION, seq, universe_ns, anchor_ns, rate, armed, 0, generation, - check(seq, universe_ns, anchor_ns, rate, armed, generation, mono_universe_ns, mono_anchor_ns), - mono_universe_ns, mono_anchor_ns) - - -def open_page() -> mmap.mmap: - try: - fd = os.open(PAGE, os.O_RDWR | os.O_CREAT | os.O_EXCL, 0o644) - os.ftruncate(fd, LAYOUT.size) - os.write(fd, encode(0, 0, 0, 1.0, 0, 0, 0, 0)) - except FileExistsError: - fd = os.open(PAGE, os.O_RDWR) - if os.fstat(fd).st_uid != os.getuid(): - raise SystemExit(f"{PAGE} isn't ours") - if os.fstat(fd).st_size < LAYOUT.size: - os.close(fd) - raise SystemExit(f"{PAGE} is an older page") - m = mmap.mmap(fd, LAYOUT.size) - os.close(fd) - return m - - -def _wake(m: mmap.mmap) -> None: - """FUTEX_WAKE every waiter on seq's low word (a shared futex: other processes' read-only mappings are woken too).""" - if _SYS_FUTEX is None: - return - buf = (ctypes.c_char * LAYOUT.size).from_buffer(m) - try: - _libc.syscall(ctypes.c_long(_SYS_FUTEX), ctypes.c_void_p(ctypes.addressof(buf) + 8), ctypes.c_int(_FUTEX_WAKE), - ctypes.c_int(0x7FFFFFFF), None, None, ctypes.c_int(0)) - finally: - del buf - - -def _universe_at(armed, universe_ns, anchor_ns, rate, now_ns) -> int: - if not armed: - return now_ns - if rate == 0 or now_ns <= anchor_ns: - return universe_ns - return universe_ns + int((now_ns - anchor_ns) * rate) - - -def write(armed: int, universe: float, rate: float, anchor: float) -> dict: - m = open_page() - _, _, seq, o_u, o_a, o_rate, o_armed, _, generation, _, _, _ = LAYOUT.unpack(m[:LAYOUT.size]) - universe_ns, anchor_ns = int(Decimal(str(universe)) * 10**9), int(Decimal(str(anchor)) * 10**9) - if not armed: - rate = 1.0 - generation += 1 - dead = seq & 1 # a writer died mid-write: its fields may be torn, so this write goes on from the next even seq - seq += dead - m[8:16] = struct.pack(" 1 and not dead and 0 < old_now - new_now < CONTINUITY_NS - if continued: - universe_ns, anchor_ns = old_now, now_rt - body = encode(seq + 2, universe_ns, anchor_ns, rate, armed, generation, now_mono, now_mono) - m[16:] = body[16:] - m[8:16] = struct.pack(" 1 else 0, - "continued": bool(continued), "universe_ns": universe_ns, "anchor_ns": anchor_ns} - - -def create(owner: str) -> None: - """As root at boot: the page, ``owner``'s from its first byte (unarmed), replacing one anyone else made.""" - user = pwd.getpwnam(owner) - try: - st = os.lstat(PAGE) - if stat.S_ISREG(st.st_mode) and st.st_uid == user.pw_uid and st.st_size == LAYOUT.size: - return - os.unlink(PAGE) - except FileNotFoundError: - pass - fd = os.open(PAGE, os.O_RDWR | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o600) - try: - os.fchown(fd, user.pw_uid, user.pw_gid) - os.write(fd, encode(0, 0, 0, 1.0, 0, 0, 0, 0)) - os.fchmod(fd, 0o644) - finally: - os.close(fd) - - -if __name__ == "__main__": - cmd = sys.argv[1] - if cmd == "set": - anchor = float(sys.argv[4]) if len(sys.argv) > 4 else time.time() - print(write(1, float(sys.argv[2]), float(sys.argv[3]), anchor)) - elif cmd == "create": - create(sys.argv[2]) - elif cmd == "clear": - print(write(0, 0.0, 1.0, time.time())) - else: - with open(PAGE, "rb") as f: - print(LAYOUT.unpack(f.read(LAYOUT.size))) diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/clock/libfaketime.patch b/src/agent_env/computers/artifacts/ubuntu-24.04/clock/libfaketime.patch deleted file mode 100644 index d26148eed..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/clock/libfaketime.patch +++ /dev/null @@ -1,1527 +0,0 @@ -SPDX-License-Identifier: GPL-2.0-only -agent-env's changes to libfaketime 0.9.12 (src/), applied from the tarball's root with patch -p1 by build.sh, which -also copies in agentenv_clock_page.c and agentenv_clock_waits.c, included where marked below. A change to libfaketime, -so licensed as libfaketime is, under the GNU General Public License version 2 (COPYING). - -diff -ruN a/src/libfaketime.c b/src/libfaketime.c ---- a/src/libfaketime.c -+++ b/src/libfaketime.c -@@ -231,11 +231,14 @@ - static int (*real___clock_gettime) (clockid_t clk_id, struct timespec *tp); - #endif - #ifdef FAKE_PTHREAD --static int (*real_pthread_cond_timedwait_225) (pthread_cond_t *, pthread_mutex_t*, struct timespec *); --static int (*real_pthread_cond_timedwait_232) (pthread_cond_t *, pthread_mutex_t*, struct timespec *); --static int (*real_pthread_cond_init_232) (pthread_cond_t *restrict, const pthread_condattr_t *restrict); --static int (*real_pthread_cond_destroy_232) (pthread_cond_t *); --static pthread_rwlock_t monotonic_conds_lock; -+static int (*real_pthread_cond_timedwait_225) (pthread_cond_t *, pthread_mutex_t*, const struct timespec *); -+static int (*real_pthread_cond_timedwait_232) (pthread_cond_t *, pthread_mutex_t*, const struct timespec *); -+static int (*real_pthread_cond_clockwait) (pthread_cond_t *, pthread_mutex_t*, clockid_t, const struct timespec *); -+static int (*real_pthread_mutex_timedlock) (pthread_mutex_t *, const struct timespec *); -+static int (*real_pthread_mutex_clocklock) (pthread_mutex_t *, clockid_t, const struct timespec *); -+#endif -+#ifdef FAKE_TIMERS -+static int (*real_timerfd_create) (int, int); - #endif - - #ifndef __APPLE__ -@@ -334,6 +337,7 @@ - bool str_array_contains(const char *haystack, const char *needle); - void *ft_dlvsym(void *handle, const char *symbol, const char *version, const char *full_name, char *ignore_list, bool should_debug_dlsym); - -+#include "agentenv_clock_page.c" /* agent-env: settings, who is faked, the clock page */ - /** Semaphore protecting shared data */ - static sem_t *shared_sem = NULL; - -@@ -420,6 +424,29 @@ - static bool parse_config_file = true; - - static void ft_cleanup (void) __attribute__ ((destructor)); -+ -+#ifdef PTHREAD_SINGLETHREADED_TIME -+/* agentenv: the lock fake_clock_gettime holds, at file scope so a fork() can't hand the child a held lock. */ -+static pthread_mutex_t time_mutex = PTHREAD_MUTEX_INITIALIZER; -+static sigset_t ft_fork_mask; -+static void ft_atfork_prepare(void) -+{ -+ sigset_t all; -+ sigfillset(&all); -+ pthread_sigmask(SIG_SETMASK, &all, &ft_fork_mask); -+ pthread_mutex_lock(&time_mutex); -+} -+static void ft_atfork_parent(void) -+{ -+ pthread_mutex_unlock(&time_mutex); -+ pthread_sigmask(SIG_SETMASK, &ft_fork_mask, NULL); -+} -+static void ft_atfork_child(void) -+{ -+ pthread_mutex_init(&time_mutex, NULL); -+ pthread_sigmask(SIG_SETMASK, &ft_fork_mask, NULL); -+} -+#endif - static void ftpl_init (void) __attribute__ ((constructor)); - - -@@ -519,7 +546,7 @@ - - static void ft_shm_destroy(void) - { -- char sem_name[256], shm_name[256], *ft_shared_env = getenv("FAKETIME_SHARED"); -+ char sem_name[256], shm_name[256], *ft_shared_env = ft_getenv("FAKETIME_SHARED"); - - if (ft_shared_env != NULL) - { -@@ -553,7 +580,7 @@ - static void ft_shm_really_init (void) - { - int ticks_shm_fd; -- char sem_name[256], shm_name[256], *ft_shared_env = getenv("FAKETIME_SHARED"); -+ char sem_name[256], shm_name[256], *ft_shared_env = ft_getenv("FAKETIME_SHARED"); - sem_t *shared_semR = NULL; - static int nt=1; - -@@ -561,7 +588,7 @@ - if (ft_shared_env == NULL) - { - ft_shm_create(); -- ft_shared_env = getenv("FAKETIME_SHARED"); -+ ft_shared_env = ft_getenv("FAKETIME_SHARED"); - } - - /* check for stale semaphore / shared memory information */ -@@ -575,7 +602,7 @@ - if (SEM_FAILED == (shared_semR = sem_open(sem_name, 0))) /* gone stale? */ - { - ft_shm_create(); -- ft_shared_env = getenv("FAKETIME_SHARED"); -+ ft_shared_env = ft_getenv("FAKETIME_SHARED"); - } - else - { -@@ -631,7 +658,7 @@ - exit(1); - } - } -- if (getenv("FAKETIME_FLSHM") != NULL) -+ if (ft_getenv("FAKETIME_FLSHM") != NULL) - { /* force the deletion of the shm sync env variable */ - unsetenv("FAKETIME_SHARED"); - } -@@ -653,12 +680,6 @@ - sem_close(shared_sem); - shared_sem = NULL; - } --#ifdef FAKE_PTHREAD -- if (pthread_rwlock_destroy(&monotonic_conds_lock) != 0) { -- fprintf(stderr, "libfaketime: In ft_cleanup(), monotonic_conds_lock destroy failed\n"); -- exit(-1); -- } --#endif - if (shmCreator == true) ft_shm_destroy(); - } - -@@ -672,8 +693,8 @@ - static void get_fake_monotonic_setting(int* current_value) - { - char *tmp_env; -- if ((tmp_env = getenv("FAKETIME_DONT_FAKE_MONOTONIC")) != NULL -- || (tmp_env = getenv("DONT_FAKE_MONOTONIC")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_DONT_FAKE_MONOTONIC")) != NULL -+ || (tmp_env = ft_getenv("DONT_FAKE_MONOTONIC")) != NULL) - { - if (0 == strcmp(tmp_env, "1")) - { -@@ -1140,7 +1161,7 @@ - #endif /* ifndef __APPLE__ */ - #endif /* ifdef FAKE_STAT */ - --#ifdef STATX_TYPE -+#if defined STATX_TYPE && defined FAKE_STAT - static inline void fake_statx_timestamp(struct statx_timestamp* p) - { - struct timespec t = {p->tv_sec,p->tv_nsec}; -@@ -1329,6 +1350,7 @@ - * Contributed by Balint Reczey in v0.9.5 - */ - -+#include "agentenv_clock_waits.c" /* agent-env: universe-time reads and waits */ - #ifdef FAKE_SLEEP - /* - * Faked nanosleep() -@@ -1343,6 +1365,7 @@ - struct timespec real_req; - - ftpl_init(); -+ if (real_nanosleep != NULL) return (*real_nanosleep)(req, rem); - if (real_nanosleep == NULL) - { - return -1; -@@ -1395,13 +1418,39 @@ - struct timespec real_req; - - ftpl_init(); -+ if (real_clock_nanosleep != NULL && (!(flags & TIMER_ABSTIME) || !ft_rt_clock(clock_id) || !ft_faked())) -+ return (*real_clock_nanosleep)(clock_id, flags, req, rem); -+ if (ft_uw_active() && ft_valid_ts(req)) -+ { -+ struct ft_clock_page s; -+ if (ft_snap(&s)) return ft_rt_sleep(&s, ft_ns(req)); -+ } - if (real_clock_nanosleep == NULL) - { - return -1; - } - if (req != NULL) - { -- if (flags & TIMER_ABSTIME) /* sleep until absolute time */ -+ if ((flags & TIMER_ABSTIME) && clock_id == CLOCK_REALTIME && ft_translates_wait(req)) -+ { -+ struct timespec mono, slice; -+ unsigned long gen; -+ bool last; -+ ft_deadline_from_fake(CLOCK_MONOTONIC, req, &mono); -+ gen = ft_tl_gen; -+ for (;;) -+ { -+ last = ft_slice_end(&mono, &slice); -+ DONT_FAKE_TIME(result = (*real_clock_nanosleep)(CLOCK_MONOTONIC, TIMER_ABSTIME, &slice, NULL)); -+ if (result != 0) return result; -+ if (!ft_deadline_moved(req, &mono, &gen) && last) return 0; -+ } -+ } -+ if ((flags & TIMER_ABSTIME) && (ft_disabled || dont_fake || clock_id == CLOCK_REALTIME)) -+ { -+ real_req = *req; -+ } -+ else if (flags & TIMER_ABSTIME) /* sleep until absolute time */ - { - struct timespec tdiff, timeadj; - timespecsub(req, &user_faked_time_timespec, &timeadj); -@@ -1482,6 +1531,7 @@ - int result; - - ftpl_init(); -+ if (real_usleep != NULL) return (*real_usleep)(usec); - if (user_rate_set && !dont_fake) - { - struct timespec real_req; -@@ -1531,6 +1581,7 @@ - #endif - { - ftpl_init(); -+ if (real_sleep != NULL) return (*real_sleep)(seconds); - if (user_rate_set && !dont_fake) - { - if (real_nanosleep == NULL) -@@ -1551,7 +1602,7 @@ - else - { - int result; -- struct timespec real_req = {seconds, 0}, rem; -+ struct timespec real_req = {seconds, 0}, rem = {0, 0}; - timespecmul(&real_req, 1.0 / user_rate, &real_req); - #ifdef MACOS_DYLD_INTERPOSE - DONT_FAKE_TIME(result = (*nanosleep)(&real_req, &rem)); -@@ -1604,6 +1655,8 @@ - { - return -1; - } -+ (void) seconds_real; -+ return (*real_alarm)(seconds); - - #ifdef MACOS_DYLD_INTERPOSE - DONT_FAKE_TIME(ret = (*alarm)(seconds_real)); -@@ -1627,6 +1680,7 @@ - { - return -1; - } -+ return (*real_ppoll)(fds, nfds, timeout_ts, sigmask); - if (timeout_ts != NULL) - { - if (user_rate_set && !dont_fake && ((timeout_ts->tv_sec > 0) || (timeout_ts->tv_nsec > 0))) -@@ -1662,6 +1716,7 @@ - { - return -1; - } -+ return (*real_epoll_wait)(epfd, events, maxevents, timeout); - if (user_rate_set && !dont_fake && timeout > 0) - { - real_timeout = (int) timeout * 1.0/user_rate; -@@ -1686,6 +1741,7 @@ - { - return -1; - } -+ return (*real_epoll_pwait)(epfd, events, maxevents, timeout, sigmask); - if (user_rate_set && !dont_fake && timeout > 0) - { - real_timeout = (int) timeout * 1.0/user_rate; -@@ -1715,6 +1771,8 @@ - { - return -1; - } -+ (void) timeout_real; -+ return (*real_poll)(fds, nfds, timeout); - - #ifdef MACOS_DYLD_INTERPOSE - DONT_FAKE_TIME(ret = (*poll)(fds, nfds, timeout_real)); -@@ -1748,6 +1806,7 @@ - { - return -1; - } -+ return (*real_select)(nfds, readfds, writefds, errorfds, timeout); - - if (timeout != NULL) - { -@@ -1812,6 +1871,7 @@ - { - return -1; - } -+ return (*real_pselect)(nfds, readfds, writefds, errorfds, timeout, sigmask); - - if (timeout != NULL) - { -@@ -1831,98 +1891,63 @@ - } - #endif - --int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout) -+static int ft_sem_wait_until(sem_t *sem, const struct timespec *fake_abs) - { -- int result; -- struct timespec real_abs_timeout, *real_abs_timeout_pt; -- -- /* sanity check */ -- if (abs_timeout == NULL) -- { -- return -1; -- } -- -- if (!CHECK_MISSING_REAL(sem_timedwait)) return -1; -- -- if (!dont_fake) -- { -- struct timespec tdiff, timeadj; -- -- timespecsub(abs_timeout, &user_faked_time_timespec, &tdiff); -- -- if (user_rate_set) -+ struct timespec mono, slice; -+ unsigned long gen; -+ bool last; -+ int result; -+ ft_deadline_from_fake(CLOCK_MONOTONIC, fake_abs, &mono); -+ gen = ft_tl_gen; -+ for (;;) -+ { -+ last = ft_slice_end(&mono, &slice); -+ DONT_FAKE_TIME(result = (*real_sem_clockwait)(sem, CLOCK_MONOTONIC, &slice)); -+ if (result == 0 || errno != ETIMEDOUT) return result; -+ if (!ft_deadline_moved(fake_abs, &mono, &gen) && last) - { -- timespecmul(&tdiff, user_rate, &timeadj); -+ errno = ETIMEDOUT; -+ return -1; - } -- else -- { -- timeadj = tdiff; -- } -- timespecadd(&ftpl_starttime.real, &timeadj, &real_abs_timeout); -- real_abs_timeout_pt = &real_abs_timeout; -- } -- else -- { -- /* cast away constness */ -- real_abs_timeout_pt = (struct timespec *)abs_timeout; - } -- -- DONT_FAKE_TIME(result = (*real_sem_timedwait)(sem, real_abs_timeout_pt)); -- return result; - } - --/* EXPERIMENTAL */ --int sem_clockwait(sem_t *sem, clockid_t clockid, const struct timespec *abstime) -+int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout) - { - int result; -- struct timespec real_abstime, *real_abstime_pt; -- -- if ((!fake_monotonic_clock) && (clockid == CLOCK_MONOTONIC)) -+ ftpl_init(); -+ if (!CHECK_MISSING_REAL(sem_timedwait)) return -1; -+ if (ft_uw_active() && ft_translates_wait(abs_timeout)) - { -- DONT_FAKE_TIME(result = (*real_sem_clockwait)(sem, clockid, abstime)); -- return result; -+ bool handled; -+ result = ft_u_sem_wait(sem, ft_ns(abs_timeout), &handled); -+ if (handled) return result; - } -- -- /* sanity check */ -- if (abstime == NULL) -+ if (!ft_translates_wait(abs_timeout) || real_sem_clockwait == NULL) - { -- return -1; -+ DONT_FAKE_TIME(result = (*real_sem_timedwait)(sem, abs_timeout)); -+ return result; - } -+ return ft_sem_wait_until(sem, abs_timeout); -+} - -+int sem_clockwait(sem_t *sem, clockid_t clockid, const struct timespec *abstime) -+{ -+ int result; -+ ftpl_init(); - if (!CHECK_MISSING_REAL(sem_clockwait)) return -1; -- -- if (!dont_fake) -+ if (ft_uw_active() && ft_translates_wait(abstime) && clockid == CLOCK_REALTIME) - { -- struct timespec tdiff, timeadj; -- -- timespecsub(abstime, &user_faked_time_timespec, &tdiff); -- -- if (user_rate_set) -- { -- timespecmul(&tdiff, 1.0 / user_rate, &timeadj); -- } -- else -- { -- timeadj = tdiff; -- } -- if (clockid == CLOCK_REALTIME) -- { -- timespecadd(&ftpl_starttime.real, &timeadj, &real_abstime); -- } -- if (clockid == CLOCK_MONOTONIC) -- { -- timespecadd(&ftpl_starttime.mon, &timeadj, &real_abstime); -- } -- real_abstime_pt = &real_abstime; -+ bool handled; -+ result = ft_u_sem_wait(sem, ft_ns(abstime), &handled); -+ if (handled) return result; - } -- else -+ if (clockid != CLOCK_REALTIME || !ft_translates_wait(abstime)) - { -- /* cast away constness */ -- real_abstime_pt = (struct timespec *)abstime; -+ DONT_FAKE_TIME(result = (*real_sem_clockwait)(sem, clockid, abstime)); -+ return result; - } -- -- DONT_FAKE_TIME(result = (*real_sem_clockwait)(sem, clockid, real_abstime_pt)); -- return result; -+ return ft_sem_wait_until(sem, abstime); - } - #endif - -@@ -1949,6 +1974,23 @@ - * Faked timer_settime() - * Does not affect timer speed when stepping clock with each time() call. - */ -+#ifdef __linux__ -+/* agentenv: the clock each timerfd was made on (ft_tfd_clock); a record still naming the number is stale. */ -+int timerfd_create(int clockid, int flags) -+{ -+ int fd; -+ ftpl_init(); -+ if (real_timerfd_create == NULL) return -1; -+ fd = real_timerfd_create(clockid, flags); -+ if (fd >= 0 && fd < FT_TFD_CLOCKS && ft_fd_mine()) -+ { -+ ft_fd_gone(fd); -+ ft_tfd_clock[fd] = clockid + 1; -+ } -+ return fd; -+} -+#endif -+ - static int - timer_settime_common(timer_t_or_int timerid, int flags, - const struct itimerspec *new_value, -@@ -1977,18 +2019,11 @@ - { - if (flags & abstime_flag) - { -- struct timespec tdiff, timeadj; -- timespecsub(&new_value->it_value, &user_faked_time_timespec, &timeadj); -- if (user_rate_set) -- { -- timespecmul(&timeadj, 1.0/user_rate, &tdiff); -- } -+ if (ft_disabled || (compat == FT_FD && ft_tfd_known(timerid.int_member) -+ && !ft_rt_clock(ft_tfd_clock[timerid.int_member] - 1))) -+ new_real.it_value = new_value->it_value; - else -- { -- tdiff = timeadj; -- } -- /* only CLOCK_REALTIME is handled */ -- timespecadd(&ftpl_starttime.real, &tdiff, &new_real.it_value); -+ ft_deadline_from_fake(CLOCK_REALTIME, &new_value->it_value, &new_real.it_value); - } - else - { -@@ -2061,47 +2096,6 @@ - return result; - } - --/* -- * Faked timer_settime() compatible with implementation in GLIBC 2.2 -- */ --int timer_settime_22(int timerid, int flags, -- const struct itimerspec *new_value, -- struct itimerspec *old_value) --{ -- ftpl_init(); -- if (real_timer_settime_22 == NULL) -- { -- return -1; -- } -- else -- { -- timer_t_or_int temp; -- temp.int_member = timerid; -- return (timer_settime_common(temp, flags, new_value, old_value, -- FT_COMPAT_GLIBC_2_2, TIMER_ABSTIME)); -- } --} -- --/* -- * Faked timer_settime() compatible with implementation in GLIBC 2.3.3 -- */ --int timer_settime_233(timer_t timerid, int flags, -- const struct itimerspec *new_value, -- struct itimerspec *old_value) --{ -- ftpl_init(); -- if (real_timer_settime_233 == NULL) -- { -- return -1; -- } -- else -- { -- timer_t_or_int temp; -- temp.timer_t_member = timerid; -- return (timer_settime_common(temp, flags, new_value, old_value, -- FT_COMPAT_GLIBC_2_3_3, TIMER_ABSTIME)); -- } --} - - /* - * Faked timer_gettime() -@@ -2151,48 +2145,6 @@ - return result; - } - --/* -- * Faked timer_gettime() compatible with implementation in GLIBC 2.2 -- */ --int timer_gettime_22(timer_t timerid, struct itimerspec *curr_value) --{ -- ftpl_init(); -- if (real_timer_gettime_22 == NULL) -- { -- return -1; -- } -- else -- { -- timer_t_or_int temp; -- temp.timer_t_member = timerid; -- return (timer_gettime_common(temp, curr_value, -- FT_COMPAT_GLIBC_2_2)); -- } --} -- --/* -- * Faked timer_gettime() compatible with implementation in GLIBC 2.3.3 -- */ --int timer_gettime_233(timer_t timerid, struct itimerspec *curr_value) --{ -- ftpl_init(); -- if (real_timer_gettime_233 == NULL) -- { -- return -1; -- } -- else -- { -- timer_t_or_int temp; -- temp.timer_t_member = timerid; -- return (timer_gettime_common(temp, curr_value, -- FT_COMPAT_GLIBC_2_3_3)); -- } --} -- --__asm__(".symver timer_gettime_22, timer_gettime@GLIBC_2.2"); --__asm__(".symver timer_gettime_233, timer_gettime@@GLIBC_2.3.3"); --__asm__(".symver timer_settime_22, timer_settime@GLIBC_2.2"); --__asm__(".symver timer_settime_233, timer_settime@@GLIBC_2.3.3"); - - #ifdef __linux__ - /* -@@ -2203,6 +2155,14 @@ - struct itimerspec *old_value) - { - ftpl_init(); -+ if (real_timerfd_settime != NULL && ft_faked()) -+ { -+ bool handled = false; -+ int r = ft_uw_active() ? ft_u_timerfd_settime(fd, flags, new_value, old_value, &handled) : 0; -+ if (handled) return r; -+ if (!(flags & TFD_TIMER_ABSTIME) || fd < 0 || fd >= FT_TFD_CLOCKS || !ft_rt_clock(ft_tfd_clock[fd] - 1)) -+ return (*real_timerfd_settime)(fd, flags, new_value, old_value); -+ } - if (real_timerfd_settime == NULL) - { - return -1; -@@ -2222,6 +2182,13 @@ - int timerfd_gettime(int fd, struct itimerspec *curr_value) - { - ftpl_init(); -+ if (real_timerfd_gettime != NULL && ft_faked()) -+ { -+ bool handled = false; -+ int r = ft_u_timerfd_gettime(fd, curr_value, &handled); -+ if (handled) return r; -+ return (*real_timerfd_gettime)(fd, curr_value); -+ } - if (real_timerfd_gettime == NULL) - { - return -1; -@@ -2379,6 +2346,8 @@ - #endif - if (result == -1) return result; /* original function failed */ - -+ if (!ft_universe_clock(clk_id)) return result; -+ - /* pass the real current time to our faking version, overwriting it */ - if (fake_monotonic_clock || (clk_id != CLOCK_MONOTONIC && clk_id != CLOCK_MONOTONIC_RAW - #ifdef CLOCK_MONOTONIC_COARSE -@@ -2495,12 +2464,25 @@ - return; - } - -+ /* agentenv: a specification without "x" is at rate 1, not at the rate of the one before it */ -+ double prev_rate = user_rate; -+ bool prev_rate_set = user_rate_set; -+ if (user_faked_time[0] == 0) return; -+ user_rate = 1.0; -+ user_rate_set = false; -+ - /* check whether the user gave us an absolute time to fake */ - switch (user_faked_time[0]) - { - - default: /* Try and interpret this as a specified time */ - if (ft_mode != FT_NOOP) ft_mode = FT_FREEZE; -+ if (strcmp(user_faked_time_fmt, "%s") == 0) -+ { -+ if (ft_parse_epoch(user_faked_time, &user_faked_time_timespec) == NULL) goto parse_failed; -+ user_faked_time_set = true; -+ goto parse_modifiers; -+ } - user_faked_time_tm.tm_isdst = -1; - nstime_str = strptime(user_faked_time, user_faked_time_fmt, &user_faked_time_tm); - -@@ -2518,9 +2500,7 @@ - } - else - { -- fprintf(stderr, "libfaketime: In parse_ft_string(), failed to parse FAKETIME timestamp.\n" -- "Please check specification %s with format %s\n", user_faked_time, user_faked_time_fmt); -- exit(EXIT_FAILURE); -+ goto parse_failed; - } - goto parse_modifiers; - break; -@@ -2548,6 +2528,11 @@ - /* Contributed by David North, TDI in version 0.7 */ - case '@': /* Specific time, but clock along relative to that starttime */ - ft_mode = FT_START_AT; -+ if (strcmp(user_faked_time_fmt, "%s") == 0) -+ { -+ if (ft_parse_epoch(&user_faked_time[1], &user_faked_time_timespec) == NULL) goto parse_failed; -+ goto parse_anchor; -+ } - user_faked_time_tm.tm_isdst = -1; - nstime_str = strptime(&user_faked_time[1], user_faked_time_fmt, &user_faked_time_tm); - if (NULL != nstime_str) -@@ -2563,12 +2548,17 @@ - } - else - { -- fprintf(stderr, "libfaketime: In parse_ft_string(), failed to parse FAKETIME timestamp.\n"); -- exit(EXIT_FAILURE); -+ goto parse_failed; - } - -- /* Reset starttime */ -- if (NULL == getenv("FAKETIME_DONT_RESET")) -+parse_anchor: -+ /* agentenv: "^" is the real time at which the clock read the time given, so every process reads the -+ same time whenever it started, with no shared memory; without it, the process's own start, as before. */ -+ if (strchr(user_faked_time, '^') != NULL) -+ { -+ if (ft_parse_epoch(strchr(user_faked_time, '^') + 1, &ftpl_starttime.real) == NULL) goto parse_failed; -+ } -+ else if (NULL == ft_getenv("FAKETIME_DONT_RESET")) - reset_time(); - goto parse_modifiers; - break; -@@ -2577,17 +2567,17 @@ - ft_mode = FT_START_AT; - struct stat master_file_stats; - int ret; -- if (NULL == getenv("FAKETIME_FOLLOW_FILE")) -+ if (NULL == ft_getenv("FAKETIME_FOLLOW_FILE")) - { - fprintf(stderr, "libfaketime: %% operator in FAKETIME setting requires environment variable FAKETIME_FOLLOW_FILE set.\n"); - exit(1); - } - else - { -- DONT_FAKE_TIME(ret = stat(getenv("FAKETIME_FOLLOW_FILE"), &master_file_stats)); -+ DONT_FAKE_TIME(ret = stat(ft_getenv("FAKETIME_FOLLOW_FILE"), &master_file_stats)); - if (ret == -1) - { -- fprintf(stderr, "libfaketime: Cannot get timestamp of file %s as requested by %% operator.\n", getenv("FAKETIME_FOLLOW_FILE")); -+ fprintf(stderr, "libfaketime: Cannot get timestamp of file %s as requested by %% operator.\n", ft_getenv("FAKETIME_FOLLOW_FILE")); - exit(1); - } - else -@@ -2596,7 +2586,7 @@ - user_faked_time_timespec.tv_nsec = 0; - } - } -- if (NULL == getenv("FAKETIME_DONT_RESET")) -+ if (NULL == ft_getenv("FAKETIME_DONT_RESET")) - reset_time(); - goto parse_modifiers; - break; -@@ -2609,7 +2599,15 @@ - { - user_rate = atof(strchr(user_faked_time, 'x')+1); - user_rate_set = true; -- if (NULL != getenv("FAKETIME_XRESET")) { -+ if (user_rate <= 0.0) -+ { -+ /* agentenv: rate 0 is the time given, frozen; sleeps and waits then take the real time they ask for */ -+ ft_mode = FT_FREEZE; -+ user_faked_time_set = true; -+ user_rate = 1.0; -+ user_rate_set = false; -+ } -+ if (NULL != ft_getenv("FAKETIME_XRESET")) { - if (ftpl_timecache.real.tv_nsec >= 0) { - user_faked_time_timespec.tv_sec = ftpl_faketimecache.real.tv_sec; - user_faked_time_timespec.tv_nsec = ftpl_faketimecache.real.tv_nsec; -@@ -2639,9 +2637,15 @@ - - strncpy(user_faked_time_saved, user_faked_time, BUFFERLEN-1); - user_faked_time_saved[BUFFERLEN-1] = 0; -+ ft_spec_gen++; - #ifdef DEBUG - fprintf(stderr, "new FAKETIME: %s\n", user_faked_time_saved); - #endif -+ return; -+ -+parse_failed: -+ user_rate = prev_rate; -+ user_rate_set = prev_rate_set; - } - - -@@ -2666,8 +2670,8 @@ - const char *progname = __progname; - #endif - -- char *ignore_list = getenv("FAKETIME_IGNORE_SYMBOLS"); -- bool should_debug_dlsym = getenv("FAKETIME_DEBUG_DLSYM"); -+ char *ignore_list = ft_getenv("FAKETIME_IGNORE_SYMBOLS"); -+ bool should_debug_dlsym = ft_getenv("FAKETIME_DEBUG_DLSYM"); - #define dlsym(handle, symbol) ft_dlvsym(handle, symbol, NULL, symbol, ignore_list, should_debug_dlsym) - #define dlvsym(handle, symbol, version) ft_dlvsym(handle, symbol, version, symbol "@" version, ignore_list, should_debug_dlsym) - -@@ -2749,32 +2753,25 @@ - #endif - - #ifdef FAKE_PTHREAD -- - #ifdef __GLIBC__ - real_pthread_cond_timedwait_225 = dlvsym(RTLD_NEXT, "pthread_cond_timedwait", "GLIBC_2.2.5"); -- - real_pthread_cond_timedwait_232 = dlvsym(RTLD_NEXT, "pthread_cond_timedwait", "GLIBC_2.3.2"); -- real_pthread_cond_init_232 = dlvsym(RTLD_NEXT, "pthread_cond_init", "GLIBC_2.3.2"); -- real_pthread_cond_destroy_232 = dlvsym(RTLD_NEXT, "pthread_cond_destroy", "GLIBC_2.3.2"); -+ real_pthread_cond_clockwait = dlvsym(RTLD_NEXT, "pthread_cond_clockwait", "GLIBC_2.30"); -+ real_pthread_mutex_clocklock = dlvsym(RTLD_NEXT, "pthread_mutex_clocklock", "GLIBC_2.30"); - #endif -- - if (NULL == real_pthread_cond_timedwait_232) - { -- real_pthread_cond_timedwait_232 = dlsym(RTLD_NEXT, "pthread_cond_timedwait"); -+ real_pthread_cond_timedwait_232 = dlsym(RTLD_NEXT, "pthread_cond_timedwait"); - } -- if (NULL == real_pthread_cond_init_232) -+ if (NULL == real_pthread_cond_clockwait) - { -- real_pthread_cond_init_232 = dlsym(RTLD_NEXT, "pthread_cond_init"); -+ real_pthread_cond_clockwait = dlsym(RTLD_NEXT, "pthread_cond_clockwait"); - } -- if (NULL == real_pthread_cond_destroy_232) -+ if (NULL == real_pthread_mutex_clocklock) - { -- real_pthread_cond_destroy_232 = dlsym(RTLD_NEXT, "pthread_cond_destroy"); -- } -- -- if (pthread_rwlock_init(&monotonic_conds_lock,NULL) != 0) { -- fprintf(stderr,"monotonic_conds_lock init failed\n"); -- exit(-1); -+ real_pthread_mutex_clocklock = dlsym(RTLD_NEXT, "pthread_mutex_clocklock"); - } -+ real_pthread_mutex_timedlock = dlsym(RTLD_NEXT, "pthread_mutex_timedlock"); - #endif - #ifdef __APPLE__ - real_clock_get_time = dlsym(RTLD_NEXT, "clock_get_time"); -@@ -2814,6 +2811,7 @@ - #endif - #ifdef __linux__ - real_timerfd_gettime = dlsym(RTLD_NEXT, "timerfd_gettime"); -+ real_timerfd_create = dlsym(RTLD_NEXT, "timerfd_create"); - real_timerfd_settime = dlsym(RTLD_NEXT, "timerfd_settime"); - #endif - #endif -@@ -2826,16 +2824,42 @@ - #undef dlsym - #undef dlvsym - -+ /* agentenv: a process of another user than the session's, or one started with AGENTENV_REAL_TIME=1, stays on real -+ time; the marker is taken out of its environment, so what it starts is faked. */ -+ { -+ const char *uid = ft_getenv("AGENTENV_SESSION_UID"); -+ const char *marker = getenv("AGENTENV_REAL_TIME"); -+ if (uid != NULL && *uid != 0 && (uid_t) strtoul(uid, NULL, 10) != getuid()) -+ ft_disabled = true; -+ if (marker != NULL && strcmp(marker, "1") == 0) -+ { -+ ft_disabled = true; -+ ft_scrub_marker(); -+ } -+ if ((tmp_env = ft_getenv("AGENTENV_WAIT_SLICE_MS")) != NULL && atol(tmp_env) > 0) -+ ft_wait_slice_ns = atol(tmp_env) * 1000000L; -+ } -+ if (ft_disabled) -+ { -+ ft_mode = FT_NOOP; -+ dont_fake = dont_fake_final; -+ return; -+ } -+#ifdef PTHREAD_SINGLETHREADED_TIME -+ pthread_atfork(ft_atfork_prepare, ft_atfork_parent, ft_atfork_child); -+#endif -+ ft_page_open(); -+ - #ifdef FAKE_STATELESS - if (0) ft_shm_init(); - #else -- tmp_env = getenv("FAKETIME_DISABLE_SHM"); -+ tmp_env = ft_getenv("FAKETIME_DISABLE_SHM"); - if (!tmp_env || tmp_env[0] == '0') { - ft_shm_init(); - } - #endif - #ifdef FAKE_STAT -- if (getenv("NO_FAKE_STAT")!=NULL) -+ if (ft_getenv("NO_FAKE_STAT")!=NULL) - { - fake_stat_disabled = 1; //Note that this is NOT re-checked - } -@@ -2843,7 +2867,7 @@ - #if defined FAKE_FILE_TIMESTAMPS - #ifdef FAKE_UTIME - // fake_utime_disabled is 0 by default -- if ((tmp_env = getenv("FAKE_UTIME")) != NULL) //Note that this is NOT re-checked -+ if ((tmp_env = ft_getenv("FAKE_UTIME")) != NULL) //Note that this is NOT re-checked - { - if (!*tmp_env || *tmp_env == 'y' || *tmp_env == 'Y' || *tmp_env == 't' || *tmp_env == 'T') - { /* an empty string or a yes/true value turns off disabling */ -@@ -2860,11 +2884,11 @@ - #endif - #endif - -- if ((tmp_env = getenv("FAKETIME_CACHE_DURATION")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_CACHE_DURATION")) != NULL) - { - cache_duration = atoi(tmp_env); - } -- if ((tmp_env = getenv("FAKETIME_NO_CACHE")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_NO_CACHE")) != NULL) - { - if (0 == strcmp(tmp_env, "1")) - { -@@ -2875,7 +2899,7 @@ - /* Check whether we actually should be faking the returned timestamp. */ - - /* We can prevent faking time for specified commands */ -- if ((tmp_env = getenv("FAKETIME_SKIP_CMDS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_SKIP_CMDS")) != NULL) - { - char *skip_cmd, *saveptr, *tmpvar; - /* Don't mess with the env variable directly. */ -@@ -2904,12 +2928,12 @@ - } - - /* We can limit faking time to specified commands */ -- if ((tmp_env = getenv("FAKETIME_ONLY_CMDS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_ONLY_CMDS")) != NULL) - { - char *only_cmd, *saveptr, *tmpvar; - bool cmd_matched = false; - -- if (getenv("FAKETIME_SKIP_CMDS") != NULL) -+ if (ft_getenv("FAKETIME_SKIP_CMDS") != NULL) - { - fprintf(stderr, "Error: Both FAKETIME_SKIP_CMDS and FAKETIME_ONLY_CMDS can't be set.\n"); - exit(EXIT_FAILURE); -@@ -2941,44 +2965,44 @@ - } - } - -- if ((tmp_env = getenv("FAKETIME_START_AFTER_SECONDS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_START_AFTER_SECONDS")) != NULL) - { - ft_start_after_secs = atol(tmp_env); - limited_faking = true; - } -- if ((tmp_env = getenv("FAKETIME_STOP_AFTER_SECONDS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_STOP_AFTER_SECONDS")) != NULL) - { - ft_stop_after_secs = atol(tmp_env); - limited_faking = true; - } -- if ((tmp_env = getenv("FAKETIME_START_AFTER_NUMCALLS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_START_AFTER_NUMCALLS")) != NULL) - { - ft_start_after_ncalls = atol(tmp_env); - limited_faking = true; - } -- if ((tmp_env = getenv("FAKETIME_STOP_AFTER_NUMCALLS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_STOP_AFTER_NUMCALLS")) != NULL) - { - ft_stop_after_ncalls = atol(tmp_env); - limited_faking = true; - } - - /* check whether we should spawn an external command */ -- if ((tmp_env = getenv("FAKETIME_SPAWN_TARGET")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_SPAWN_TARGET")) != NULL) - { - spawnsupport = true; -- (void) strncpy(ft_spawn_target, getenv("FAKETIME_SPAWN_TARGET"), sizeof(ft_spawn_target) - 1); -+ (void) strncpy(ft_spawn_target, ft_getenv("FAKETIME_SPAWN_TARGET"), sizeof(ft_spawn_target) - 1); - ft_spawn_target[sizeof(ft_spawn_target) - 1] = 0; -- if ((tmp_env = getenv("FAKETIME_SPAWN_SECONDS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_SPAWN_SECONDS")) != NULL) - { - ft_spawn_secs = atol(tmp_env); - } -- if ((tmp_env = getenv("FAKETIME_SPAWN_NUMCALLS")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_SPAWN_NUMCALLS")) != NULL) - { - ft_spawn_ncalls = atol(tmp_env); - } - } - -- if ((tmp_env = getenv("FAKETIME_SAVE_FILE")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_SAVE_FILE")) != NULL) - { - if (-1 == (outfile = open(tmp_env, O_RDWR | O_APPEND | O_CLOEXEC | O_CREAT, - S_IWUSR | S_IRUSR))) -@@ -2989,7 +3013,7 @@ - } - - /* load file only if reading timestamps from it is not finished yet */ -- if ((tmp_env = getenv("FAKETIME_LOAD_FILE")) != NULL) -+ if ((tmp_env = ft_getenv("FAKETIME_LOAD_FILE")) != NULL) - { - int infile = -1; - struct stat sb; -@@ -3021,7 +3045,7 @@ - infile_set = true; - } - -- tmp_env = getenv("FAKETIME_FMT"); -+ tmp_env = ft_getenv("FAKETIME_FMT"); - if (tmp_env == NULL) - { - strcpy(user_faked_time_fmt, "%Y-%m-%d %T"); -@@ -3061,7 +3085,7 @@ - system_time_from_system(&ftpl_starttime); - } - /* fake time supplied as environment variable? */ -- if (NULL != (tmp_env = getenv("FAKETIME"))) -+ if (NULL != (tmp_env = ft_getenv("FAKETIME"))) - { - parse_config_file = false; - parse_ft_string(tmp_env); -@@ -3070,6 +3094,11 @@ - { - read_config_file(); - } -+ if (ft_page != NULL) -+ { -+ struct ft_clock_page s; -+ if (ft_page_snapshot(&s)) ft_page_apply_locked(&s); -+ } - - dont_fake = dont_fake_final; - } -@@ -3209,8 +3238,8 @@ - * a system-wide /etc/faketimerc present. - * The /etc/faketimerc handling has been contributed by David Burley, - * Jacob Moorman, and Wayne Davison of SourceForge, Inc. in version 0.6 */ -- (void) snprintf(custom_filename, BUFSIZ, "%s", getenv("FAKETIME_TIMESTAMP_FILE")); -- (void) snprintf(filename, BUFSIZ, "%s/.faketimerc", getenv("HOME")); -+ (void) snprintf(custom_filename, BUFSIZ, "%s", ft_getenv("FAKETIME_TIMESTAMP_FILE")); -+ (void) snprintf(filename, BUFSIZ, "%s/.faketimerc", ft_getenv("HOME")); - if ((faketimerc = open(custom_filename, O_RDONLY)) != -1 || - (faketimerc = open(filename, O_RDONLY)) != -1 || - (faketimerc = open("/etc/faketimerc", O_RDONLY)) != -1) -@@ -3248,6 +3277,48 @@ - tp_save.tv_nsec = tp->tv_nsec; - - if (dont_fake) return 0; -+ if (ft_disabled) return 0; -+ if (ft_page != NULL && (clk_id == CLOCK_REALTIME || clk_id == CLOCK_REALTIME_COARSE)) -+ { -+ struct ft_clock_page s; -+ if (ft_snap_at(clk_id, &s, tp)) -+ { -+ if (s.generation != __atomic_load_n(&ft_page_applied, __ATOMIC_ACQUIRE)) -+ { -+#ifdef PTHREAD_SINGLETHREADED_TIME -+ sigset_t all, old; -+ sigfillset(&all); -+ pthread_sigmask(SIG_SETMASK, &all, &old); -+ pthread_mutex_lock(&time_mutex); -+ ft_page_apply_locked(&s); -+ pthread_mutex_unlock(&time_mutex); -+ pthread_sigmask(SIG_SETMASK, &old, NULL); -+#else -+ ft_page_apply_locked(&s); -+#endif -+ } -+ ft_tl_gen = s.generation; -+ ft_tl_scale = (s.armed && s.rate > 0) ? 1.0 / s.rate : 1.0; -+ if (s.armed) -+ { -+ int64_t now = (int64_t) tp->tv_sec * 1000000000LL + tp->tv_nsec, fake; -+ if (s.rate == 0 || now <= s.anchor_ns) -+ fake = s.universe_ns; -+ else if (s.rate == 1.0) -+ fake = s.universe_ns + (now - s.anchor_ns); -+ else -+ fake = s.universe_ns + (int64_t) ((double) (now - s.anchor_ns) * s.rate); -+ tp->tv_sec = fake / 1000000000LL; -+ tp->tv_nsec = fake % 1000000000LL; -+ if (tp->tv_nsec < 0) -+ { -+ tp->tv_sec -= 1; -+ tp->tv_nsec += 1000000000LL; -+ } -+ } -+ return 0; -+ } -+ } - /* Per process timers are only sped up or slowed down */ - if ((clk_id == CLOCK_PROCESS_CPUTIME_ID ) || (clk_id == CLOCK_THREAD_CPUTIME_ID)) - { -@@ -3262,7 +3333,6 @@ - volatile int ret = INT_MAX; - - #ifdef PTHREAD_SINGLETHREADED_TIME -- static pthread_mutex_t time_mutex = PTHREAD_MUTEX_INITIALIZER; - - // block all signals while locked. prevents deadlocks if signal interrupts in in mid-operation. - sigset_t all_signals, original_mask; -@@ -3367,6 +3437,19 @@ - force_cache_expiration = 0; - } - -+ /* agentenv: with the clock page mapped, the file isn't read; until it is, look for it once a second */ -+ if (ft_page == NULL && ft_getenv("AGENTENV_CLOCK_PAGE") != NULL && current_ts.tv_sec != ft_page_last_try) -+ { -+ ft_page_last_try = current_ts.tv_sec; -+ ft_page_open(); -+ if (ft_page != NULL) -+ { -+ struct ft_clock_page s; -+ if (ft_page_snapshot(&s)) ft_page_apply_locked(&s); -+ } -+ } -+ if (ft_page != NULL) cache_expired = 0; -+ - if (cache_expired == 1) - { - static char user_faked_time[BUFFERLEN]; /* changed to static for caching in v0.6 */ -@@ -3377,7 +3460,7 @@ - fprintf(stderr, "***************++ Cache expired ++**************\n"); - */ - -- if (NULL != (tmp_env = getenv("FAKETIME"))) -+ if (NULL != (tmp_env = ft_getenv("FAKETIME"))) - { - strncpy(user_faked_time, tmp_env, BUFFERLEN - 1); - user_faked_time[BUFFERLEN - 1] = 0; -@@ -3401,6 +3484,9 @@ - get_fake_monotonic_setting(&fake_monotonic_clock); - } /* cache had expired */ - -+ ft_tl_gen = ft_spec_gen; -+ ft_tl_scale = (ft_mode == FT_START_AT && user_rate_set && user_rate > 0) ? 1.0 / user_rate : 1.0; -+ - if (infile_set) - { - if (load_time(tp)) -@@ -3520,6 +3606,7 @@ - { - struct timespec ts; - int ret; -+ if (ft_disabled) return 0; - ts.tv_sec = tv->tv_sec; - ts.tv_nsec = tv->tv_usec * 1000 + ftpl_starttime.real.tv_nsec % 1000; - -@@ -3627,6 +3714,8 @@ - DONT_FAKE_TIME(result = (*real___clock_gettime)(clk_id, tp)); - if (result == -1) return result; /* original function failed */ - -+ if (!ft_universe_clock(clk_id)) return result; -+ - /* pass the real current time to our faking version, overwriting it */ - if (fake_monotonic_clock || (clk_id != CLOCK_MONOTONIC && clk_id != CLOCK_MONOTONIC_RAW - #ifdef CLOCK_MONOTONIC_COARSE -@@ -3723,238 +3812,124 @@ - - #ifdef FAKE_PTHREAD - --typedef enum {FT_COMPAT_GLIBC_2_2_5, FT_COMPAT_GLIBC_2_3_2} ft_lib_compat_pthread; -- --struct pthread_cond_monotonic { -- pthread_cond_t *ptr; -- UT_hash_handle hh; --}; -- --static struct pthread_cond_monotonic *monotonic_conds = NULL; -+/* -+ * agentenv: a wait with a CLOCK_REALTIME deadline waits on CLOCK_MONOTONIC instead, until ft_deadline_from_fake's -+ * deadline. A condvar's clock is read from glibc's own condvar word (bit 1 of __wrefs, which glibc's waits read, since -+ * glibc 2.25) instead of a table kept by intercepting pthread_cond_init and pthread_cond_destroy; nothing here calls -+ * getenv() or malloc() or takes a lock. A condvar wait isn't cut into slices, since a slice ending would be a wakeup -+ * its caller must recheck, so a jump or a change of rate reaches it only when it ends; a mutex wait is cut. -+ */ -+#define FT_COND_CLOCK_MONOTONIC 2 - --int pthread_cond_init_232(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr) -+int pthread_cond_timedwait_232(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) - { -- clockid_t clock_id; -- int result; -- -+ struct timespec mono; - ftpl_init(); -- if (!CHECK_MISSING_REAL(pthread_cond_init_232)) return -1; -- result = real_pthread_cond_init_232(cond, attr); -- -- if (result != 0 || attr == NULL) -- return result; -- -- pthread_condattr_getclock(attr, &clock_id); -- -- if (clock_id == CLOCK_MONOTONIC) { -- struct pthread_cond_monotonic *e = (struct pthread_cond_monotonic*)malloc(sizeof(struct pthread_cond_monotonic)); -- e->ptr = cond; -- -- if (pthread_rwlock_wrlock(&monotonic_conds_lock) != 0) { -- fprintf(stderr,"can't acquire write monotonic_conds_lock\n"); -- exit(-1); -- } -- HASH_ADD_PTR(monotonic_conds, ptr, e); -- pthread_rwlock_unlock(&monotonic_conds_lock); -+ if (ft_uw_active() && ft_translates_wait(abstime) && real_pthread_cond_clockwait != NULL -+ && !(__atomic_load_n(&cond->__data.__wrefs, __ATOMIC_RELAXED) & FT_COND_CLOCK_MONOTONIC)) -+ { -+ int r = ft_u_cond_wait(cond, mutex, abstime); -+ if (r != -1) return r; - } -- -- return result; -+ if (!ft_translates_wait(abstime) || real_pthread_cond_clockwait == NULL -+ || (__atomic_load_n(&cond->__data.__wrefs, __ATOMIC_RELAXED) & FT_COND_CLOCK_MONOTONIC)) -+ return real_pthread_cond_timedwait_232(cond, mutex, abstime); -+ ft_deadline_from_fake(CLOCK_MONOTONIC, abstime, &mono); -+ return real_pthread_cond_clockwait(cond, mutex, CLOCK_MONOTONIC, &mono); - } - --int pthread_cond_destroy_232(pthread_cond_t *cond) -+int pthread_cond_timedwait_225(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) - { -- struct pthread_cond_monotonic* e; -- -+ struct timespec real; - ftpl_init(); -- -- if (pthread_rwlock_wrlock(&monotonic_conds_lock) != 0) { -- fprintf(stderr,"can't acquire write monotonic_conds_lock\n"); -- exit(-1); -- } -- HASH_FIND_PTR(monotonic_conds, &cond, e); -- if (e) { -- HASH_DEL(monotonic_conds, e); -- free(e); -+ if (ft_uw_active() && ft_translates_wait(abstime) && real_pthread_cond_clockwait != NULL) -+ { -+ int r = ft_u_cond_wait(cond, mutex, abstime); -+ if (r != -1) return r; - } -- pthread_rwlock_unlock(&monotonic_conds_lock); -- -- return real_pthread_cond_destroy_232(cond); -+ if (!ft_translates_wait(abstime)) -+ return real_pthread_cond_timedwait_225(cond, mutex, abstime); -+ ft_deadline_from_fake(CLOCK_REALTIME, abstime, &real); -+ return real_pthread_cond_timedwait_225(cond, mutex, &real); - } - --/* -- * Check whether we need a run-time activation of the -- * forced monotonic fix to avoid faked / unfaked timestamp -- * confusion between the application and glibc internals. -- */ --bool needs_forced_monotonic_fix(char *function_name) --{ -- bool result = false; -- char *env_var; --#ifdef __GLIBC__ -- const char *glibc_version_string = gnu_get_libc_version(); -+__asm__(".symver pthread_cond_timedwait_225, pthread_cond_timedwait@GLIBC_2.2.5"); -+#if defined __ARM_ARCH || defined FORCE_PTHREAD_NONVER -+__asm__(".symver pthread_cond_timedwait_232, pthread_cond_timedwait@@"); -+#else -+__asm__(".symver pthread_cond_timedwait_232, pthread_cond_timedwait@@GLIBC_2.3.2"); - #endif - -- if (function_name == NULL) return false; -- -- /* The forced monotonic fix can be activated by setting an -- * environment variable to 1, or disabled by setting it to 0 */ -- if ((env_var = getenv("FAKETIME_FORCE_MONOTONIC_FIX")) != NULL) -+int pthread_cond_clockwait(pthread_cond_t *cond, pthread_mutex_t *mutex, clockid_t clockid, -+ const struct timespec *abstime) -+{ -+ struct timespec mono; -+ ftpl_init(); -+ if (real_pthread_cond_clockwait == NULL) return EINVAL; -+ if (ft_uw_active() && ft_translates_wait(abstime) && clockid == CLOCK_REALTIME) - { -- if (env_var[0] == '0') -- result = false; -- else -- result = true; -+ int r = ft_u_cond_wait(cond, mutex, abstime); -+ if (r != -1) return r; - } -- else -- { --#ifdef __GLIBC__ -- /* Here we try to derive the necessity for a forced monotonic fix * -- * based on glibc version. What could possibly go wrong? */ -- -- int glibc_major, glibc_minor; -- sscanf(glibc_version_string, "%d.%d", &glibc_major, &glibc_minor); -- -- /* The following decision logic is yet purely based on experiences -- * with pthread_cond_timedwait(). The used boundaries may still be -- * imprecise. */ -- if ( (glibc_major == 2) && -- ((glibc_minor <= 24) || (glibc_minor >= 30)) ) -- { -- result = true; -- } -- else --#endif -- result = false; // avoid forced monotonic fixes unless really necessary -- } -- -- if (getenv("FAKETIME_DEBUG") != NULL) --#ifdef __GLIBC__ -- fprintf(stderr, "libfaketime: forced monotonic fix for %s = %s (glibc version %s)\n", -- function_name, result ? "yes":"no", glibc_version_string); --#else -- fprintf(stderr, "libfaketime: forced monotonic fix for %s = %s (not glibc-compiled)\n", -- function_name, result ? "yes":"no"); --#endif -- -- return result; -+ if (clockid != CLOCK_REALTIME || !ft_translates_wait(abstime)) -+ return real_pthread_cond_clockwait(cond, mutex, clockid, abstime); -+ ft_deadline_from_fake(CLOCK_MONOTONIC, abstime, &mono); -+ return real_pthread_cond_clockwait(cond, mutex, CLOCK_MONOTONIC, &mono); - } - --int pthread_cond_timedwait_common(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime, ft_lib_compat_pthread compat) -+static int ft_mutex_lock_until(pthread_mutex_t *mutex, const struct timespec *fake_abs) - { -- struct timespec tp, tdiff_actual, realtime, faketime; -- struct timespec *tf = NULL; -- struct pthread_cond_monotonic* e; -- char *tmp_env; -- int wait_ms; -- clockid_t clk_id; -- int result = 0; -- -- ftpl_init(); -- -- if (abstime != NULL) -+ struct timespec mono, slice; -+ unsigned long gen; -+ bool last; -+ int result; -+ ft_deadline_from_fake(CLOCK_MONOTONIC, fake_abs, &mono); -+ gen = ft_tl_gen; -+ for (;;) - { -- if (pthread_rwlock_rdlock(&monotonic_conds_lock) != 0) { -- fprintf(stderr,"can't acquire read monotonic_conds_lock\n"); -- exit(-1); -- } -- HASH_FIND_PTR(monotonic_conds, &cond, e); -- pthread_rwlock_unlock(&monotonic_conds_lock); -- if (e != NULL) -- clk_id = CLOCK_MONOTONIC; -- else -- clk_id = CLOCK_REALTIME; -- -- DONT_FAKE_TIME(result = (*real_clock_gettime)(clk_id, &realtime)); -- if (result == -1) -- { -- return EINVAL; -- } -- faketime = realtime; -- (void)fake_clock_gettime(clk_id, &faketime); -- -- if ((tmp_env = getenv("FAKETIME_WAIT_MS")) != NULL) -- { -- wait_ms = atol(tmp_env); -- DONT_FAKE_TIME(result = (*real_clock_gettime)(clk_id, &realtime)); -- if (result == -1) -- { -- return EINVAL; -- } -- -- tdiff_actual.tv_sec = wait_ms / 1000; -- tdiff_actual.tv_nsec = (wait_ms % 1000) * 1000000; -- timespecadd(&realtime, &tdiff_actual, &tp); -- -- tf = &tp; -+ last = ft_slice_end(&mono, &slice); -+ result = real_pthread_mutex_clocklock(mutex, CLOCK_MONOTONIC, &slice); -+ if (result == EINVAL && real_pthread_mutex_timedlock != NULL) -+ { /* a PI mutex on a kernel without FUTEX_LOCK_PI2 takes only CLOCK_REALTIME */ -+ struct timespec real; -+ ft_deadline_from_fake(CLOCK_REALTIME, fake_abs, &real); -+ return real_pthread_mutex_timedlock(mutex, &real); - } -- else -- { -- timespecsub(abstime, &faketime, &tp); -- if (user_rate_set) -- { -- timespecmul(&tp, 1.0 / user_rate, &tdiff_actual); -- } -- else -- { -- tdiff_actual = tp; -- } -- } -- -- /* For CLOCK_MONOTONIC, pthread_cond_timedwait uses clock_gettime -- internally to calculate the appropriate duration for the -- waiting time. This already uses the faked functions, hence, the -- fake time needs to be passed to pthread_cond_timedwait for -- CLOCK_MONOTONIC. */ --#ifndef __ARM_ARCH --#ifndef FORCE_MONOTONIC_FIX -- if (clk_id == CLOCK_MONOTONIC) { -- if (needs_forced_monotonic_fix("pthread_cond_timedwait") == true) { -- timespecadd(&realtime, &tdiff_actual, &tp); -- } -- else { -- timespecadd(&faketime, &tdiff_actual, &tp); -- } -- } -- else --#endif --#endif -- timespecadd(&realtime, &tdiff_actual, &tp); -- -- tf = &tp; -- } -- -- switch (compat) { -- case FT_COMPAT_GLIBC_2_3_2: -- result = real_pthread_cond_timedwait_232(cond, mutex, tf); -- break; -- case FT_COMPAT_GLIBC_2_2_5: -- result = real_pthread_cond_timedwait_225(cond, mutex, tf); -- break; -+ if (result != ETIMEDOUT) return result; -+ if (!ft_deadline_moved(fake_abs, &mono, &gen) && last) return ETIMEDOUT; - } -- return result; - } - --int pthread_cond_timedwait_225(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) -+int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime) - { -- return pthread_cond_timedwait_common(cond, mutex, abstime, FT_COMPAT_GLIBC_2_2_5); -+ ftpl_init(); -+ if (real_pthread_mutex_timedlock == NULL) return EINVAL; -+ if (ft_uw_active() && ft_translates_wait(abstime) && real_pthread_mutex_clocklock != NULL) -+ { -+ bool handled; -+ int r = ft_u_mutex_lock(mutex, ft_ns(abstime), &handled); -+ if (handled) return r; -+ } -+ if (!ft_translates_wait(abstime) || real_pthread_mutex_clocklock == NULL) -+ return real_pthread_mutex_timedlock(mutex, abstime); -+ return ft_mutex_lock_until(mutex, abstime); - } - --int pthread_cond_timedwait_232(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime) -+int pthread_mutex_clocklock(pthread_mutex_t *mutex, clockid_t clockid, const struct timespec *abstime) - { -- return pthread_cond_timedwait_common(cond, mutex, abstime, FT_COMPAT_GLIBC_2_3_2); -+ ftpl_init(); -+ if (real_pthread_mutex_clocklock == NULL) return EINVAL; -+ if (ft_uw_active() && ft_translates_wait(abstime) && clockid == CLOCK_REALTIME) -+ { -+ bool handled; -+ int r = ft_u_mutex_lock(mutex, ft_ns(abstime), &handled); -+ if (handled) return r; -+ } -+ if (clockid != CLOCK_REALTIME || !ft_translates_wait(abstime)) -+ return real_pthread_mutex_clocklock(mutex, clockid, abstime); -+ return ft_mutex_lock_until(mutex, abstime); - } - --__asm__(".symver pthread_cond_timedwait_225, pthread_cond_timedwait@GLIBC_2.2.5"); --#if defined __ARM_ARCH || defined FORCE_PTHREAD_NONVER --__asm__(".symver pthread_cond_timedwait_232, pthread_cond_timedwait@@"); --__asm__(".symver pthread_cond_init_232, pthread_cond_init@@"); --__asm__(".symver pthread_cond_destroy_232, pthread_cond_destroy@@"); --#else --__asm__(".symver pthread_cond_timedwait_232, pthread_cond_timedwait@@GLIBC_2.3.2"); --__asm__(".symver pthread_cond_init_232, pthread_cond_init@@GLIBC_2.3.2"); --__asm__(".symver pthread_cond_destroy_232, pthread_cond_destroy@@GLIBC_2.3.2"); --#endif -- - #endif - - /* -@@ -4005,15 +3980,15 @@ - /* If FAKETIME_TIMESTAMP_FILE was given in environment, - * and if FAKETIME_UPDATE_TIMESTAMP_FILE=1, then update it. - * This allows other process instances to share the same time. */ -- if ( (getenv("FAKETIME_TIMESTAMP_FILE") != NULL) -- && (*getenv("FAKETIME_TIMESTAMP_FILE") != '\0') -- && (getenv("FAKETIME_UPDATE_TIMESTAMP_FILE") != NULL) -- && (strcmp(getenv("FAKETIME_UPDATE_TIMESTAMP_FILE"), "1") == 0)) -+ if ( (ft_getenv("FAKETIME_TIMESTAMP_FILE") != NULL) -+ && (*ft_getenv("FAKETIME_TIMESTAMP_FILE") != '\0') -+ && (ft_getenv("FAKETIME_UPDATE_TIMESTAMP_FILE") != NULL) -+ && (strcmp(ft_getenv("FAKETIME_UPDATE_TIMESTAMP_FILE"), "1") == 0)) - { - const char *error = NULL; - FILE *envfile; - static char custom_filename[BUFSIZ]; -- (void) snprintf(custom_filename, BUFSIZ, "%s", getenv("FAKETIME_TIMESTAMP_FILE")); -+ (void) snprintf(custom_filename, BUFSIZ, "%s", ft_getenv("FAKETIME_TIMESTAMP_FILE")); - - if ((envfile = fopen(custom_filename, "wt")) != NULL) - { -@@ -4034,7 +4009,7 @@ - { - fprintf(stderr, "libfaketime: In clock_settime(), failed to " - "%s while updating FAKETIME_TIMESTAMP_FILE (`%s'): %s\n", -- error, getenv("FAKETIME_TIMESTAMP_FILE"), strerror(errno)); -+ error, ft_getenv("FAKETIME_TIMESTAMP_FILE"), strerror(errno)); - } - } - -@@ -4125,7 +4100,7 @@ - - /* return 0 if no FAKERANDOM_SEED was seen */ - static int bypass_randomness(void* buf, size_t buflen) { -- char *seedstring = getenv("FAKERANDOM_SEED"); -+ char *seedstring = ft_getenv("FAKERANDOM_SEED"); - char *b = buf; - - if (seedstring != NULL) { -@@ -4169,7 +4144,7 @@ - #else - pid_t getpid() { - #endif -- const char *pidstring = getenv("FAKETIME_FAKEPID"); -+ const char *pidstring = ft_getenv("FAKETIME_FAKEPID"); - if (pidstring != NULL) { - long int pid = strtol(pidstring, NULL, 0); - return (pid_t)(pid); -@@ -4270,7 +4245,7 @@ - va_list ap; - va_start(ap, number); - #ifdef FAKE_RANDOM -- if (number == __NR_getrandom && getenv("FAKERANDOM_SEED")) { -+ if (number == __NR_getrandom && ft_getenv("FAKERANDOM_SEED")) { - void *buf; - size_t buflen; - unsigned int flags; -@@ -4282,7 +4257,7 @@ - } - #endif - // static int (*real_clock_gettime) (clockid_t clk_id, struct timespec *tp); -- if (number == __NR_clock_gettime && (getenv("FAKETIME") || getenv("FAKETIME_TIMESTAMP_FILE"))) { -+ if (number == __NR_clock_gettime && (ft_getenv("FAKETIME") || ft_getenv("FAKETIME_TIMESTAMP_FILE"))) { - clockid_t clk_id; - struct timespec *tp; - clk_id = va_arg(ap, clockid_t); -diff -ruN a/src/libfaketime.map b/src/libfaketime.map ---- a/src/libfaketime.map -+++ b/src/libfaketime.map -@@ -1,21 +1,9 @@ --GLIBC_2.2 { -- global: -- -- timer_gettime; timer_settime; -- local: timer_settime_*; timer_gettime_*; --}; -- --GLIBC_2.3.3 { -- # Changed timer_t. -- timer_gettime; timer_settime; --} GLIBC_2.2; -- - GLIBC_2.2.5 { - global: pthread_cond_timedwait; - local: pthread_cond_timedwait_*; pthread_cond_init_*; pthread_cond_destroy*; - }; - - GLIBC_2.3.2 { -- pthread_cond_timedwait; pthread_cond_init; pthread_cond_destroy; -+ pthread_cond_timedwait; - } GLIBC_2.2.5; - diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/clock/tls_realtime.c b/src/agent_env/computers/artifacts/ubuntu-24.04/clock/tls_realtime.c deleted file mode 100644 index 0a9a86688..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/clock/tls_realtime.c +++ /dev/null @@ -1,115 +0,0 @@ -/* SPDX-License-Identifier: Apache-2.0 */ -/* - * agent-env's TLS shim: OpenSSL checks a peer's certificates against real time, whatever the universe's clock says. - * - * Preloaded after the clock library (/etc/ld.so.preload). A program on universe time months from real time would find - * every live site's certificate not yet valid or expired. Most clients reach live sites through the computer's egress - * proxy, which hands them certificates valid at the universe date; this is for the ones that bypass it. OpenSSL's - * libssl verifies a chain with X509_verify_cert, which this defines first in the global scope: it sets the - * verification's check time to real time, read with a raw system call the clock library doesn't intercept, unless the - * caller set one, then calls OpenSSL's own, the one in the caller's own libcrypto, so a process holding two copies of - * OpenSSL (a bundled one beside the system's) verifies each with its own. - * - * Not covered: TLS stacks of their own (Chromium's BoringSSL, GnuTLS, NSS, Java's) and static binaries. - */ -#define _GNU_SOURCE -#include -#include -#include -#include - -typedef struct x509_store_ctx_st X509_STORE_CTX; -typedef struct X509_VERIFY_PARAM_st X509_VERIFY_PARAM; - -/* OpenSSL's X509_V_FLAG_USE_CHECK_TIME (x509_vfy.h), the same in 1.1 and 3 */ -#define TLS_USE_CHECK_TIME 0x2 -#define TLS_SYSTEM_LIBCRYPTO "libcrypto.so.3" - -/* OpenSSL's functions as one copy of it has them */ -struct tls_openssl -{ - int (*verify)(X509_STORE_CTX *); - void (*set_time)(X509_STORE_CTX *, unsigned long, time_t); - X509_VERIFY_PARAM *(*get0_param)(X509_STORE_CTX *); - unsigned long (*get_flags)(const X509_VERIFY_PARAM *); -}; - -int X509_verify_cert(X509_STORE_CTX *ctx); - -/* the real time in seconds, read with the system call itself (the clock library intercepts libc's), 0 if it fails */ -static long tls_real_time(void) -{ - struct timespec ts = {0, 0}; -#if defined(__x86_64__) - long ret; - __asm__ volatile("syscall" : "=a"(ret) : "0"(228L), "D"((long) CLOCK_REALTIME), "S"(&ts) : "rcx", "r11", "memory"); - if (ret != 0) return 0; -#elif defined(__aarch64__) - register long x8 __asm__("x8") = 113; - register long x0 __asm__("x0") = CLOCK_REALTIME; - register long x1 __asm__("x1") = (long) &ts; - __asm__ volatile("svc 0" : "+r"(x0) : "r"(x8), "r"(x1) : "memory"); - if (x0 != 0) return 0; -#else -#error the TLS shim reads real time with a raw system call on x86_64 or aarch64 -#endif - return ts.tv_sec; -} - -/* ``name`` in ``scope`` (a handle, or RTLD_NEXT), never this file's own definition */ -static void *tls_lookup(void *scope, const char *name) -{ - void *found = dlsym(scope, name); - return found == (void *) X509_verify_cert ? NULL : found; -} - -static void tls_take(void *scope, struct tls_openssl *out) -{ - out->verify = (int (*)(X509_STORE_CTX *)) tls_lookup(scope, "X509_verify_cert"); - out->set_time = (void (*)(X509_STORE_CTX *, unsigned long, time_t)) tls_lookup(scope, "X509_STORE_CTX_set_time"); - out->get0_param = (X509_VERIFY_PARAM * (*)(X509_STORE_CTX *)) tls_lookup(scope, "X509_STORE_CTX_get0_param"); - out->get_flags = (unsigned long (*)(const X509_VERIFY_PARAM *)) tls_lookup(scope, "X509_VERIFY_PARAM_get_flags"); -} - -/* OpenSSL as the object at ``caller`` sees it: from that object's own dependencies (libssl's libcrypto, whichever copy - it is); for a caller that isn't a shared library depending on one (a program calling it itself), the next definition - in the global scope; else the system's libcrypto where it was loaded privately (a Python extension's). Looked up on - every call, once a handshake: a copy of OpenSSL unloaded and another loaded at its address must not be handed the - first one's functions. */ -static void tls_resolve(void *caller, struct tls_openssl *out) -{ - Dl_info info; - void *handle; - memset(out, 0, sizeof *out); - if (dladdr(caller, &info)) - { - if (info.dli_fname != NULL && info.dli_fname[0] != 0 - && (handle = dlopen(info.dli_fname, RTLD_NOW | RTLD_NOLOAD)) != NULL) - { - tls_take(handle, out); - dlclose(handle); - if (out->verify != NULL) return; - } - } - tls_take(RTLD_NEXT, out); - if (out->verify != NULL) return; - if ((handle = dlopen(TLS_SYSTEM_LIBCRYPTO, RTLD_NOW | RTLD_NOLOAD)) != NULL) - { - tls_take(handle, out); - dlclose(handle); - } -} - -int X509_verify_cert(X509_STORE_CTX *ctx) -{ - struct tls_openssl ssl; - tls_resolve(__builtin_return_address(0), &ssl); - if (ssl.verify == NULL) return -1; - if (ctx != NULL && ssl.set_time != NULL && ssl.get0_param != NULL && ssl.get_flags != NULL) - { - X509_VERIFY_PARAM *param = ssl.get0_param(ctx); - long now = tls_real_time(); - if (param != NULL && !(ssl.get_flags(param) & TLS_USE_CHECK_TIME) && now > 0) ssl.set_time(ctx, 0, (time_t) now); - } - return ssl.verify(ctx); -} diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/README.md b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/README.md deleted file mode 100644 index 72cc086d5..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/README.md +++ /dev/null @@ -1,83 +0,0 @@ -# The egress proxy - -`egress` is the `ubuntu-24.04` base computer's explicit proxy, the way every session program goes out. The session's -programs read the universe's time (the clock library in `../clock/`), so a client that checks a site's certificate with -its own TLS, Chromium, Java or an official Node build, refuses a live site once the clock is months back or ahead. The -proxy runs on real time and hands such a client a certificate that holds at the universe's date when it must. - -It is one static Go program. The base's Dockerfile builds it in a stage of its own, after `go vet` and its tests, so no -Go toolchain is left in the image. `go.sum` pins its modules, and Dependabot's `gomod` entry keeps them current. - -## What runs where - -- `egress boot` runs once, as root, in the base's init (`../start-desktop`), before root's supervisord starts: - - it mints the computer's CA once, valid from 1950 to 9999 as routed sites' certificates are, its key in - `/etc/agentenv/egress/` and readable by `agentenv-egress` alone; - - it trusts the CA in the system store, agent-env's trust files (`/etc/agentenv/ca.pem`, `ca-bundle.pem`) and the - session user's NSS database, and fails the boot naming any of these it can't; - - it trusts the CA in every certifi bundle and every JDK's store an app brings, naming any that can't take it among - its warnings, and the computer boots; - - its report is `/var/log/agentenv-root/egress-boot.log`. -- `egress serve` runs as `agentenv-egress` under root's supervisord (`../root.conf`), on `127.0.0.1:3128`, or where - `AGENTENV_EGRESS_PROXY` names, as a base whose own proxy holds that address does. The clock library leaves that - user on real time. -- The session's supervisord (`../supervisord.conf`), and a login shell through `agentenv-egress.sh`, point - `HTTP(S)_PROXY`, `ALL_PROXY`, Java's proxy properties (`JAVA_TOOL_OPTIONS`) and Node's `fetch` - (`NODE_USE_ENV_PROXY`) at it. -- agent-env's own servers in the computer, the driver and its apps' servers, run on real time and send their own - transfers to the object store directly (`agentenv_protocol.transfers.direct`); the programs they start keep the - proxy. - -## What it does with a connection - -- **Plain HTTP** (an absolute URL): the client's own head goes on with its request line and the proxy-only headers - rewritten, then bytes are relayed both ways. An upgrade keeps its `Connection` and `Upgrade` headers; any other - request closes once answered. -- **`CONNECT`**: the destination's name is resolved, and the connection is refused unless every address is global - (`notGlobal` lists the blocks that aren't). A routed site's name, in the hosts file's `# agentenv sites` block, goes - to the site proxy there. Bytes that aren't TLS, and a routed site's TLS, are relayed untouched. -- **TLS, with the clock unarmed**: relayed untouched. -- **TLS, with the clock armed**: each of the name's addresses is decided by its own probe, a handshake the proxy makes - with the client's own ClientHello, cached per address and SNI. An address already found working is tried first; one - that doesn't answer is passed over. - - The probe's site verifies, and every universe a client may check against is more than `Margin` (8 days) inside the - dates its verified chain shares: **passed through** untouched. - - The probe's handshake doesn't finish (a site asking for the client's certificate, say): **passed through**, for - the client to answer. - - Otherwise: **intercepted**. The site is verified against real time and the image's public roots, and the client is - handed a leaf for its names from the computer's CA, or from a CA trusted nowhere when the site doesn't verify. The - proxy's own handshake with the site uses uTLS, built from the client's ClientHello, so the site sees the client's - fingerprint and ALPN offer, and h2 runs end to end. - -The universes a client may check against are now, the universe at the clock's anchor, and, for `Hold` (30 s) after a -change, the universe the clock left. The universe is read from the clock page the follower writes (`clock.go`); -`testdata/` holds pages the follower's writer wrote, which both sides' tests read. - -## Its log - -One JSON line per connection, on stdout, which root's supervisord keeps in `/var/log/agentenv-root/egress.log`, out of -the session user's reach. Its fields, each there when it applies: - -| Field | What it is | -|---|---| -| `t` | When the line was written, in seconds | -| `client`, `method`, `host`, `port` | Who asked, how, and for what | -| `allowed`, `dst` | `global` or `routed`, and the address dialled | -| `mode` | `pass`, `splice`, `intercept`, `http`, `upgrade` or `refused` | -| `decision` | `real`, `inside`, `outside`, `unverified`, `probe_failed`, `routed` or `not TLS` | -| `sni`, `alpn_offered`, `alpn` | The client's server name and protocols, and the one the site picked | -| `universes`, `upstream_valid` | The universes checked, and the verified chain's shared dates, in seconds | -| `probed`, `probe_error` | Whether this connection probed, and why a probe failed | -| `verified`, `upstream_verify`, `upstream_tls`, `minted` | An interception's upstream: verified or why not, its version and cipher, a new leaf | -| `upstream_ms`, `handshake_ms` | Milliseconds to the upstream, and to the client's handshake | -| `error` | Why the connection ended early | - -## Testing it - -```bash -make egress-test # gofmt, go vet and go test -race, in the Go image the Dockerfile builds with -``` - -It needs Docker, not Go. CI runs it as the `egress` job. `tst/integration/computer/egress_test.py` runs the proxy in -a computer: the session user's reach, the refused destinations, Java and Node 182 days back, and handshakes through week -jumps. diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/agentenv-egress.sh b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/agentenv-egress.sh deleted file mode 100644 index 2840d9eb4..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/agentenv-egress.sh +++ /dev/null @@ -1,7 +0,0 @@ -# The egress proxy for a login shell, as the session's supervisord gives every session program (supervisord.conf). -export HTTP_PROXY=http://127.0.0.1:3128 HTTPS_PROXY=http://127.0.0.1:3128 ALL_PROXY=http://127.0.0.1:3128 -export http_proxy=http://127.0.0.1:3128 https_proxy=http://127.0.0.1:3128 all_proxy=http://127.0.0.1:3128 -export NO_PROXY=localhost,127.0.0.1,::1,host.docker.internal no_proxy=localhost,127.0.0.1,::1,host.docker.internal -export SSL_CERT_FILE=/etc/agentenv/ca-bundle.pem REQUESTS_CA_BUNDLE=/etc/agentenv/ca-bundle.pem -export NODE_EXTRA_CA_CERTS=/etc/agentenv/ca.pem NODE_USE_ENV_PROXY=1 -export JAVA_TOOL_OPTIONS="-Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=3128 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=3128 -Dhttp.nonProxyHosts=localhost|127.0.0.1|::1|host.docker.internal" diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot.go b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot.go deleted file mode 100644 index e1ea67894..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot.go +++ /dev/null @@ -1,285 +0,0 @@ -package main - -import ( - "crypto/x509" - "encoding/pem" - "errors" - "fmt" - "io/fs" - "os" - "os/exec" - "os/user" - "path/filepath" - "sort" - "strconv" - "strings" - "time" -) - -// bootPaths are where boot reads and writes; tests point them into a folder of their own. -type bootPaths struct { - Folder, PublicCA, Anchor string - // Bundles are the PEM bundles the CA is appended to when they exist: agent-env's trust files, which - // SSL_CERT_FILE, REQUESTS_CA_BUNDLE and NODE_EXTRA_CA_CERTS name. - Bundles []string - // UpdateTrust rebuilds the system store from its anchors, and its hooks Java's. - UpdateTrust []string - // CertifiRoots and JavaRoots are searched for certifi's cacert.pem and JDKs' lib/security/cacerts. - CertifiRoots, JavaRoots []string - EgressUser, SessionUser string - // NSS adds the CA to the session user's NSS database, which Chromium and Firefox read; nil leaves it. - NSS func(paths bootPaths) error -} - -func defaultBootPaths() bootPaths { - return bootPaths{ - Folder: Folder, PublicCA: PublicCA, Anchor: "/usr/local/share/ca-certificates/agentenv-egress.crt", - Bundles: []string{"/etc/agentenv/ca.pem", "/etc/agentenv/ca-bundle.pem"}, - UpdateTrust: []string{"update-ca-certificates"}, - CertifiRoots: []string{"/usr/lib/python3", "/usr/local/lib", "/opt"}, - JavaRoots: []string{"/usr/lib/jvm", "/opt"}, - EgressUser: EgressUser, SessionUser: SessionUser, - NSS: sessionNSS, - } -} - -// boot, as root before the session starts: the CA, minted once per computer, its key the egress user's alone, and -// trusted in the system store, agent-env's trust files, every certifi bundle, every JDK's store and the session -// user's NSS database. A step that fails fails the boot, naming it, but for a certifi bundle or a JDK's store, which -// an app brings: those are named among the report's warnings. -func boot(p bootPaths) (map[string]any, error) { - start := time.Now() - report := map[string]any{} - egress, err := user.Lookup(p.EgressUser) - if err != nil { - return report, err - } - uid, _ := strconv.Atoi(egress.Uid) - gid, _ := strconv.Atoi(egress.Gid) - if err := os.MkdirAll(p.Folder, 0o700); err != nil { - return report, err - } - caPath, keyPath := filepath.Join(p.Folder, "ca.pem"), filepath.Join(p.Folder, "ca.key") - if _, err := os.Stat(caPath); errors.Is(err, fs.ErrNotExist) { - if err := writeCA(caPath, keyPath); err != nil { - return report, fmt.Errorf("minting the CA: %w", err) - } - report["minted"] = true - } - for _, path := range []string{p.Folder, caPath, keyPath} { - if err := os.Chown(path, uid, gid); err != nil { - return report, err - } - } - if err := os.Chmod(p.Folder, 0o700); err != nil { - return report, err - } - caPEM, err := os.ReadFile(caPath) - if err != nil { - return report, err - } - for _, path := range []string{p.PublicCA, p.Anchor} { - if err := writeRootFile(path, caPEM); err != nil { - return report, err - } - } - if len(p.UpdateTrust) > 0 { - if out, err := exec.Command(p.UpdateTrust[0], p.UpdateTrust[1:]...).CombinedOutput(); err != nil { - return report, fmt.Errorf("%s: %w: %s", strings.Join(p.UpdateTrust, " "), err, truncate(string(out), ErrorLimit)) - } - } - appended := map[string]bool{} - for _, path := range p.Bundles { - added, err := appendOnce(path, caPEM) - if errors.Is(err, fs.ErrNotExist) { - continue - } - if err != nil { - return report, fmt.Errorf("trusting the CA in %s: %w", path, err) - } - appended[path] = added - } - // An app's own stores, certifi's and JDKs': one that can't take the CA is named, and the computer boots, its - // app then refusing intercepted sites as it would on a store of its own. - var warnings []string - for _, path := range certifiBundles(p.CertifiRoots) { - added, err := appendOnce(path, caPEM) - if err != nil { - warnings = append(warnings, fmt.Sprintf("trusting the CA in %s: %v", path, err)) - continue - } - appended[path] = added - } - report["bundles"] = appended - java := map[string]string{} - for _, store := range javaStores(p.JavaRoots) { - if err := trustJava(store, p.PublicCA); err != nil { - warnings = append(warnings, fmt.Sprintf("trusting the CA in %s: %v", store, err)) - continue - } - java[store] = "trusted" - } - report["java"] = java - if len(warnings) > 0 { - report["warnings"] = warnings - } - if p.NSS != nil { - if err := p.NSS(p); err != nil { - return report, fmt.Errorf("the session's NSS database: %w", err) - } - report["nss"] = "trusted" - } - report["seconds"] = time.Since(start).Seconds() - return report, nil -} - -func writeCA(caPath, keyPath string) error { - ca, err := mintCA(CAName) - if err != nil { - return err - } - key, err := x509.MarshalPKCS8PrivateKey(ca.key) - if err != nil { - return err - } - if err := writeExclusive(keyPath, pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: key}), 0o600); err != nil { - return err - } - return writeExclusive(caPath, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: ca.cert.Raw}), 0o644) -} - -func writeExclusive(path string, data []byte, mode os.FileMode) error { - f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, mode) - if err != nil { - return err - } - if _, err := f.Write(data); err != nil { - f.Close() - return err - } - return f.Close() -} - -// writeRootFile writes path whole, 0644, replacing it in one rename. -func writeRootFile(path string, data []byte) error { - if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { - return err - } - part := path + ".part" - if err := os.WriteFile(part, data, 0o644); err != nil { - return err - } - if err := os.Chmod(part, 0o644); err != nil { - return err - } - return os.Rename(part, path) -} - -// appendOnce appends pem to the bundle at path (through a symlink) unless it's there; whether it did. -func appendOnce(path string, pemData []byte) (bool, error) { - real, err := filepath.EvalSymlinks(path) - if err != nil { - return false, err - } - text, err := os.ReadFile(real) - if err != nil { - return false, err - } - block := strings.TrimSpace(string(pemData)) - if strings.Contains(string(text), block) { - return false, nil - } - f, err := os.OpenFile(real, os.O_WRONLY|os.O_APPEND, 0) - if err != nil { - return false, err - } - defer f.Close() - sep := "" - if len(text) > 0 && !strings.HasSuffix(string(text), "\n") { - sep = "\n" - } - _, err = f.WriteString(sep + block + "\n") - return err == nil, err -} - -func certifiBundles(roots []string) []string { - seen := map[string]bool{} - var out []string - for _, root := range roots { - filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { - if err != nil || d.IsDir() || d.Name() != "cacert.pem" || filepath.Base(filepath.Dir(path)) != "certifi" { - return nil - } - real, err := filepath.EvalSymlinks(path) - if err == nil && !seen[real] { - seen[real] = true - out = append(out, path) - } - return nil - }) - } - sort.Strings(out) - return out -} - -// javaStores are the JDKs' stores, lib/security/cacerts, under roots; the system store's hook builds Debian's. -func javaStores(roots []string) []string { - var out []string - for _, root := range roots { - filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { - if err != nil || d.IsDir() || d.Name() != "cacerts" { - return nil - } - if filepath.Base(filepath.Dir(path)) == "security" && filepath.Base(filepath.Dir(filepath.Dir(path))) == "lib" { - if info, err := os.Lstat(path); err == nil && info.Mode().IsRegular() { - out = append(out, path) - } - } - return nil - }) - } - sort.Strings(out) - return out -} - -// trustJava adds the CA to a JDK's store with that JDK's own keytool, else any on the PATH. -func trustJava(store, ca string) error { - keytool := filepath.Join(filepath.Dir(filepath.Dir(filepath.Dir(store))), "bin", "keytool") - if _, err := os.Stat(keytool); err != nil { - if keytool, err = exec.LookPath("keytool"); err != nil { - return errors.New("there's no keytool to add the CA with") - } - } - opened := []string{"-keystore", store, "-storepass", "changeit"} - exec.Command(keytool, append([]string{"-delete", "-alias", "agentenv-egress"}, opened...)...).Run() - out, err := exec.Command(keytool, append([]string{"-importcert", "-noprompt", "-trustcacerts", "-alias", - "agentenv-egress", "-file", ca}, opened...)...).CombinedOutput() - if err != nil { - return fmt.Errorf("keytool: %w: %s", err, truncate(string(out), ErrorLimit)) - } - return nil -} - -// sessionNSS adds the CA to the session user's NSS database, as that user, making the database if there's none. -func sessionNSS(p bootPaths) error { - session, err := user.Lookup(p.SessionUser) - if err != nil { - return err - } - db := filepath.Join(session.HomeDir, ".pki", "nssdb") - as := []string{"setpriv", "--reuid=" + p.SessionUser, "--regid=" + p.SessionUser, "--init-groups", "env", - "HOME=" + session.HomeDir} - steps := [][]string{{"mkdir", "-p", db}} - if _, err := os.Stat(filepath.Join(db, "cert9.db")); err != nil { - steps = append(steps, []string{"certutil", "-N", "-d", "sql:" + db, "--empty-password"}) - } - steps = append(steps, []string{"certutil", "-A", "-d", "sql:" + db, "-n", NSSNickname, "-t", "C,,", "-i", - p.PublicCA}) - for _, step := range steps { - argv := append(append([]string{}, as...), step...) - if out, err := exec.Command(argv[0], argv[1:]...).CombinedOutput(); err != nil { - return fmt.Errorf("%s: %w: %s", strings.Join(step[:2], " "), err, truncate(string(out), ErrorLimit)) - } - } - return nil -} diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot_test.go b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot_test.go deleted file mode 100644 index 239d8db0a..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/boot_test.go +++ /dev/null @@ -1,88 +0,0 @@ -package main - -import ( - "os" - "os/user" - "path/filepath" - "strings" - "testing" -) - -func TestBootMintsTheCAOnceItsKeyPrivateAndTrustsItInEveryBundleOnce(t *testing.T) { - dir := t.TempDir() - me, _ := user.Current() - bundle := filepath.Join(dir, "ca-bundle.pem") - os.WriteFile(bundle, []byte("-----BEGIN CERTIFICATE-----\nroot\n-----END CERTIFICATE-----"), 0o644) - certifi := filepath.Join(dir, "opt", "venv", "lib", "certifi", "cacert.pem") - os.MkdirAll(filepath.Dir(certifi), 0o755) - os.WriteFile(certifi, []byte(""), 0o644) - p := bootPaths{ - Folder: filepath.Join(dir, "egress"), PublicCA: filepath.Join(dir, "egress-ca.pem"), - Anchor: filepath.Join(dir, "anchors", "agentenv-egress.crt"), - Bundles: []string{bundle, filepath.Join(dir, "missing.pem")}, - CertifiRoots: []string{filepath.Join(dir, "opt")}, - EgressUser: me.Username, - } - report, err := boot(p) - if err != nil { - t.Fatal(err, report) - } - if report["minted"] != true { - t.Fatalf("%v", report) - } - info, _ := os.Stat(filepath.Join(p.Folder, "ca.key")) - if info.Mode().Perm() != 0o600 { - t.Fatalf("the CA key is %v", info.Mode().Perm()) - } - if info, _ := os.Stat(p.Folder); info.Mode().Perm() != 0o700 { - t.Fatalf("the folder is %v", info.Mode().Perm()) - } - ca, _ := os.ReadFile(filepath.Join(p.Folder, "ca.pem")) - public, _ := os.ReadFile(p.PublicCA) - if string(ca) != string(public) { - t.Fatal("the public CA isn't the CA") - } - if _, err := boot(p); err != nil { - t.Fatal(err) - } - again, _ := os.ReadFile(filepath.Join(p.Folder, "ca.pem")) - if string(again) != string(ca) { - t.Fatal("a second boot minted another CA") - } - for _, path := range []string{bundle, certifi} { - text, _ := os.ReadFile(path) - if n := strings.Count(string(text), strings.TrimSpace(string(ca))); n != 1 { - t.Fatalf("%s has the CA %d times", path, n) - } - } - leaves, err := LoadLeaves(p.Folder) - if err != nil || leaves.ca.cert.NotBefore != CAFrom || leaves.ca.cert.NotAfter != CAUntil { - t.Fatalf("%v", err) - } -} - -func TestAnAppsStoreThatCantTakeTheCAIsNamedAndTheBootGoesOn(t *testing.T) { - dir := t.TempDir() - me, _ := user.Current() - broken := filepath.Join(dir, "opt", "venv", "lib", "certifi") - os.MkdirAll(filepath.Join(broken, "elsewhere"), 0o755) - os.Symlink(filepath.Join(broken, "elsewhere"), filepath.Join(broken, "cacert.pem")) // a folder, not a bundle - jdk := filepath.Join(dir, "jdk", "lib", "security") - os.MkdirAll(jdk, 0o755) - os.WriteFile(filepath.Join(jdk, "cacerts"), []byte("not a keystore"), 0o644) - t.Setenv("PATH", dir) // and no keytool to add the CA with - p := bootPaths{ - Folder: filepath.Join(dir, "egress"), PublicCA: filepath.Join(dir, "egress-ca.pem"), - Anchor: filepath.Join(dir, "anchors", "agentenv-egress.crt"), - CertifiRoots: []string{filepath.Join(dir, "opt")}, JavaRoots: []string{filepath.Join(dir, "jdk")}, - EgressUser: me.Username, - } - report, err := boot(p) - if err != nil { - t.Fatalf("the boot failed: %v", err) - } - warnings, _ := report["warnings"].([]string) - if len(warnings) != 2 || !strings.Contains(strings.Join(warnings, " "), "keytool") { - t.Fatalf("%v", report) - } -} diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/chromium-policy.json b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/chromium-policy.json deleted file mode 100644 index ef2d43a7e..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/chromium-policy.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "ProxySettings": { - "ProxyMode": "fixed_servers", - "ProxyServer": "http://127.0.0.1:3128", - "ProxyBypassList": "localhost;127.0.0.1;[::1];host.docker.internal" - }, - "WebRtcIPHandling": "disable_non_proxied_udp" -} diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/clock.go b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/clock.go deleted file mode 100644 index 30cb880c1..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/clock.go +++ /dev/null @@ -1,151 +0,0 @@ -package main - -import ( - "encoding/binary" - "math" - "os" - "sync" - "syscall" - "time" -) - -// The clock page the follower writes (clockpage.py, version 2): magic version seq universe_ns anchor_ns rate armed -// reserved generation check mono_universe_ns mono_anchor_ns, little-endian, 80 bytes, seq odd while it's written. -const ( - pageSize = 80 - pageMagic = 0x4B434541 - checkSalt = 0x9E3779B97F4A7C15 -) - -// state is the universe as the session reads it: armed, the universe at the anchor (seconds), the anchor (real -// seconds) and the rate. -type state struct { - Armed bool - V float64 - Anchor float64 - Rate float64 -} - -var unarmed = state{Rate: 1} - -func (s state) at(t float64) float64 { - if !s.Armed { - return t - } - if s.Rate == 0 || t <= s.Anchor { - return s.V - } - return s.V + (t-s.Anchor)*s.Rate -} - -func pageCheck(seq uint64, u, a int64, rate float64, armed uint32, generation uint64, mu, ma int64) uint64 { - return seq ^ uint64(u) ^ uint64(a) ^ math.Float64bits(rate) ^ uint64(armed) ^ generation ^ uint64(mu) ^ - uint64(ma) ^ checkSalt -} - -// readPage reads a consistent state from the page's bytes, or false when it's being written, torn or not a page. -func readPage(b []byte) (state, bool) { - if len(b) < pageSize || binary.LittleEndian.Uint32(b[0:]) != pageMagic { - return state{}, false - } - seq := binary.LittleEndian.Uint64(b[8:]) - if seq&1 == 1 { - return state{}, false - } - u := int64(binary.LittleEndian.Uint64(b[16:])) - a := int64(binary.LittleEndian.Uint64(b[24:])) - rate := math.Float64frombits(binary.LittleEndian.Uint64(b[32:])) - armed := binary.LittleEndian.Uint32(b[40:]) - generation := binary.LittleEndian.Uint64(b[48:]) - check := binary.LittleEndian.Uint64(b[56:]) - mu := int64(binary.LittleEndian.Uint64(b[64:])) - ma := int64(binary.LittleEndian.Uint64(b[72:])) - if check != pageCheck(seq, u, a, rate, armed, generation, mu, ma) || binary.LittleEndian.Uint64(b[8:]) != seq { - return state{}, false - } - if armed == 0 { - return unarmed, true - } - return state{Armed: true, V: float64(u) / 1e9, Anchor: float64(a) / 1e9, Rate: rate}, true -} - -// Universe follows the clock page, read every PagePoll, so a change is seen and the universe it left is held for Hold. -type Universe struct { - path string - mu sync.Mutex - page []byte - current state - known bool - previous state - leftAt time.Time -} - -func NewUniverse(path string) *Universe { - u := &Universe{path: path, current: unarmed} - u.refresh(time.Now()) - return u -} - -func (u *Universe) Poll() { - for { - time.Sleep(PagePoll) - u.refresh(time.Now()) - } -} - -func (u *Universe) read() state { - if u.page == nil { - f, err := os.Open(u.path) - if err != nil { - return unarmed - } - mapped, err := syscall.Mmap(int(f.Fd()), 0, pageSize, syscall.PROT_READ, syscall.MAP_SHARED) - f.Close() - if err != nil { - return unarmed - } - u.page = mapped - } - for i := 0; i < PageReads; i++ { - if s, ok := readPage(u.page); ok { - return s - } - } - return u.current -} - -func (u *Universe) refresh(now time.Time) state { - u.mu.Lock() - defer u.mu.Unlock() - s := u.read() - if u.known && s != u.current { - u.previous, u.leftAt = u.current, now - } - u.current, u.known = s, true - return s -} - -// View is false when the session is on real time and was armed nowhere within Hold; else true, with the universes a -// client may check against: now, the universe at the anchor, and the one left within Hold when it was armed. -func (u *Universe) View(now time.Time) (bool, []float64) { - u.refresh(now) - u.mu.Lock() - defer u.mu.Unlock() - return view(u.current, u.previous, u.leftAt, now) -} - -func view(current, previous state, leftAt time.Time, now time.Time) (bool, []float64) { - held := !leftAt.IsZero() && now.Sub(leftAt) <= Hold && previous.Armed - if !current.Armed && !held { - return false, nil - } - t := float64(now.UnixNano()) / 1e9 - out := []float64{current.at(t)} - if current.Armed { - out = append(out, current.V) - } - if held { - out = append(out, previous.at(t)) - } - return true, out -} diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/clock_test.go b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/clock_test.go deleted file mode 100644 index 22bb41503..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/clock_test.go +++ /dev/null @@ -1,112 +0,0 @@ -package main - -import ( - "encoding/binary" - "encoding/hex" - "math" - "os" - "path/filepath" - "strings" - "testing" - "time" -) - -func page(seq uint64, universe, anchor time.Time, rate float64, armed bool) []byte { - b := make([]byte, pageSize) - u, a := universe.UnixNano(), anchor.UnixNano() - var on uint32 - if armed { - on = 1 - } - binary.LittleEndian.PutUint32(b[0:], pageMagic) - binary.LittleEndian.PutUint32(b[4:], 2) - binary.LittleEndian.PutUint64(b[8:], seq) - binary.LittleEndian.PutUint64(b[16:], uint64(u)) - binary.LittleEndian.PutUint64(b[24:], uint64(a)) - binary.LittleEndian.PutUint64(b[32:], math.Float64bits(rate)) - binary.LittleEndian.PutUint32(b[40:], on) - binary.LittleEndian.PutUint64(b[48:], 7) - binary.LittleEndian.PutUint64(b[56:], pageCheck(seq, u, a, rate, on, 7, a, a)) - binary.LittleEndian.PutUint64(b[64:], uint64(a)) - binary.LittleEndian.PutUint64(b[72:], uint64(a)) - return b -} - -func TestAPageReadsAsTheUniverseItHoldsAndATornOneNot(t *testing.T) { - universe, anchor := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC), time.Unix(time.Now().Unix(), 0) - s, ok := readPage(page(4, universe, anchor, 2, true)) - if !ok || !s.Armed || s.Rate != 2 || int64(s.V) != universe.Unix() { - t.Fatalf("read %+v %v", s, ok) - } - if got := s.at(float64(anchor.Unix()) + 10); int64(got) != universe.Unix()+20 { - t.Fatalf("10 s on at rate 2: %v", got) - } - if _, ok := readPage(page(5, universe, anchor, 2, true)); ok { - t.Fatal("a page being written (odd seq) read as whole") - } - torn := page(4, universe, anchor, 2, true) - torn[20] ^= 1 - if _, ok := readPage(torn); ok { - t.Fatal("a torn page read as whole") - } - if s, ok := readPage(page(2, time.Time{}, time.Time{}, 1, false)); !ok || s.Armed { - t.Fatalf("an unarmed page: %+v %v", s, ok) - } -} - -func TestTheUniverseFollowsThePageFileAndHoldsTheOneItLeft(t *testing.T) { - path := filepath.Join(t.TempDir(), "page") - if err := os.WriteFile(path, page(2, time.Time{}, time.Time{}, 1, false), 0o644); err != nil { - t.Fatal(err) - } - u := NewUniverse(path) - if armed, _ := u.View(time.Now()); armed { - t.Fatal("unarmed page viewed as armed") - } - past := time.Now().Add(-180 * 24 * time.Hour) - f, _ := os.OpenFile(path, os.O_WRONLY, 0) - f.WriteAt(page(4, past, time.Now(), 1, true), 0) - f.Close() - armed, universes := u.View(time.Now()) - if !armed || len(universes) != 2 || int64(universes[1]) != past.Unix() { - t.Fatalf("armed page: %v %v", armed, universes) - } -} - -func TestAClientMayCheckAgainstTheUniverseItLeftForHoldAfterAChange(t *testing.T) { - now := time.Now() - past := state{Armed: true, V: float64(now.Add(-100 * 24 * time.Hour).Unix()), Anchor: float64(now.Unix()), Rate: 1} - if armed, _ := view(unarmed, state{}, time.Time{}, now); armed { - t.Fatal("real time viewed as armed") - } - armed, universes := view(unarmed, past, now.Add(-Hold/2), now) - if !armed || len(universes) != 2 { - t.Fatalf("within Hold of leaving an armed universe: %v %v", armed, universes) - } - if armed, _ := view(unarmed, past, now.Add(-2*Hold), now); armed { - t.Fatal("past Hold, the universe left still counts") - } -} - -// golden is a page the follower's writer wrote (clockpage.encode), as testdata keeps it. -func golden(t *testing.T, name string) []byte { - text, err := os.ReadFile(filepath.Join("testdata", "page-"+name+".hex")) - if err != nil { - t.Fatal(err) - } - b, err := hex.DecodeString(strings.TrimSpace(string(text))) - if err != nil { - t.Fatal(err) - } - return b -} - -func TestThePagesTheFollowersWriterWritesReadAsTheUniverseTheyHold(t *testing.T) { - s, ok := readPage(golden(t, "armed")) - if !ok || !s.Armed || s.V != 1577836800 || s.Rate != 2 || math.Abs(s.Anchor-1794000000.123456789) > 1e-6 { - t.Fatalf("armed: %+v %v", s, ok) - } - if s, ok := readPage(golden(t, "unarmed")); !ok || s != unarmed { - t.Fatalf("unarmed: %+v %v", s, ok) - } -} diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/go.mod b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/go.mod deleted file mode 100644 index 7b1b97e80..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/go.mod +++ /dev/null @@ -1,12 +0,0 @@ -module agentenv/egress - -go 1.26.11 - -require github.com/refraction-networking/utls v1.8.2 - -require ( - github.com/andybalholm/brotli v1.0.6 // indirect - github.com/klauspost/compress v1.17.4 // indirect - golang.org/x/crypto v0.36.0 // indirect - golang.org/x/sys v0.31.0 // indirect -) diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/go.sum b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/go.sum deleted file mode 100644 index 3e8a6d1ef..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/go.sum +++ /dev/null @@ -1,10 +0,0 @@ -github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI= -github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= -github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4= -github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= -github.com/refraction-networking/utls v1.8.2 h1:j4Q1gJj0xngdeH+Ox/qND11aEfhpgoEvV+S9iJ2IdQo= -github.com/refraction-networking/utls v1.8.2/go.mod h1:jkSOEkLqn+S/jtpEHPOsVv/4V4EVnelwbMQl4vCWXAM= -golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34= -golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc= -golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik= -golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/hello.go b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/hello.go deleted file mode 100644 index 381ea2993..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/hello.go +++ /dev/null @@ -1,164 +0,0 @@ -package main - -import ( - "bufio" - "encoding/binary" - "errors" - "strings" - "time" -) - -// A TLS record's content type for handshake messages, a ClientHello's handshake type, and the most plaintext one -// record carries. -const ( - recordHandshake = 22 - clientHelloType = 1 - maxRecord = 16384 -) - -// clientHello is the ClientHello handshake message the client has sent, peeked from r so the handshake stays -// untouched for whoever reads it next; it may span several records. -func clientHello(r *bufio.Reader, deadline time.Time) ([]byte, error) { - need := 5 - for { - if time.Now().After(deadline) { - return nil, errors.New("ClientHello incomplete") - } - data, err := r.Peek(need) - if err != nil { - if errors.Is(err, bufio.ErrBufferFull) { - return nil, errors.New("ClientHello larger than the read buffer") - } - return nil, err - } - if data[0] != recordHandshake { - return nil, errors.New("not TLS") - } - var body []byte - for p := 0; ; { - if p+5 > len(data) { - need = p + 5 - break - } - if data[p] != recordHandshake { - return nil, errors.New("ClientHello interrupted by another record") - } - size := int(binary.BigEndian.Uint16(data[p+3:])) - if p+5+size > len(data) { - need = p + 5 + size - break - } - body = append(body, data[p+5:p+5+size]...) - p += 5 + size - if len(body) >= 4 { - if whole := 4 + (int(body[1])<<16 | int(body[2])<<8 | int(body[3])); len(body) >= whole { - return body[:whole], nil - } - } - } - } -} - -// parseHello is the server name and the ALPN protocols a ClientHello message offers. -func parseHello(hello []byte) (string, []string, error) { - p, err := helloExtensions(hello) - if err != nil { - return "", nil, err - } - if len(hello) < p+2 { - return "", nil, nil - } - end := p + 2 + int(binary.BigEndian.Uint16(hello[p:])) - if end > len(hello) { - return "", nil, errors.New("extensions overrun the ClientHello") - } - var sni string - var alpn []string - for q := p + 2; q+4 <= end; { - kind := binary.BigEndian.Uint16(hello[q:]) - size := int(binary.BigEndian.Uint16(hello[q+2:])) - body := hello[q+4 : min(q+4+size, end)] - q += 4 + size - switch { - case kind == 0 && len(body) > 5 && body[2] == 0: - n := int(binary.BigEndian.Uint16(body[3:])) - if 5+n <= len(body) { - sni = strings.ToLower(string(body[5 : 5+n])) - } - case kind == 16 && len(body) > 2: - for r := 2; r < len(body); { - n := int(body[r]) - if r+1+n > len(body) { - break - } - alpn = append(alpn, string(body[r+1:r+1+n])) - r += 1 + n - } - } - } - return sni, alpn, nil -} - -// resumption: what only the proxy's own session with the client could mean, never the site's. -var dropped = map[uint16]bool{41: true, 42: true} - -// helloRecord is the ClientHello as one handshake record, without the extensions only a resumption with the proxy put -// there, and the extensions it left out. -func helloRecord(hello []byte) ([]byte, []uint16, error) { - p, err := helloExtensions(hello) - if err != nil { - return nil, nil, err - } - body := append([]byte{}, hello[4:p]...) - var gone []uint16 - if len(hello) >= p+2 { - end := p + 2 + int(binary.BigEndian.Uint16(hello[p:])) - if end > len(hello) { - return nil, nil, errors.New("extensions overrun the ClientHello") - } - var exts []byte - for q := p + 2; q+4 <= end; { - kind := binary.BigEndian.Uint16(hello[q:]) - size := int(binary.BigEndian.Uint16(hello[q+2:])) - if q+4+size > end { - return nil, nil, errors.New("an extension overruns the ClientHello") - } - if dropped[kind] { - gone = append(gone, kind) - } else { - exts = append(exts, hello[q:q+4+size]...) - } - q += 4 + size - } - body = binary.BigEndian.AppendUint16(body, uint16(len(exts))) - body = append(body, exts...) - } - if len(body)+4 > maxRecord { - return nil, nil, errors.New("ClientHello longer than one record") - } - msg := append([]byte{clientHelloType, byte(len(body) >> 16), byte(len(body) >> 8), byte(len(body))}, body...) - return append([]byte{recordHandshake, 3, 1, byte(len(msg) >> 8), byte(len(msg))}, msg...), gone, nil -} - -// helloExtensions is where a ClientHello message's extensions start, past its version, random, session id, cipher -// suites and compression methods, each length checked against the message. -func helloExtensions(hello []byte) (int, error) { - if len(hello) < 4 || hello[0] != clientHelloType { - return 0, errors.New("not a ClientHello") - } - p := 4 + 2 + 32 - for _, width := range []int{1, 2, 1} { // session id, cipher suites, compression methods - if len(hello) < p+width { - return 0, errors.New("short ClientHello") - } - n := int(hello[p]) - if width == 2 { - n = int(binary.BigEndian.Uint16(hello[p:])) - } - p += width + n - if p > len(hello) { - return 0, errors.New("a ClientHello field overruns the message") - } - } - return p, nil -} diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/hello_test.go b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/hello_test.go deleted file mode 100644 index a3b30b2f1..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/hello_test.go +++ /dev/null @@ -1,82 +0,0 @@ -package main - -import ( - "bufio" - "bytes" - "crypto/tls" - "encoding/binary" - "net" - "reflect" - "testing" - "time" -) - -// goHello is the ClientHello message Go's TLS client sends for name, offering alpn. -func goHello(t *testing.T, name string, alpn []string) []byte { - t.Helper() - client, server := net.Pipe() - go tls.Client(client, &tls.Config{ServerName: name, NextProtos: alpn, InsecureSkipVerify: true}).Handshake() - defer client.Close() - defer server.Close() - hello, err := clientHello(bufio.NewReader(server), time.Now().Add(5*time.Second)) - if err != nil { - t.Fatal(err) - } - return hello -} - -func records(message []byte, size int) []byte { - var out []byte - for len(message) > 0 { - n := min(size, len(message)) - out = append(out, 22, 3, 1, byte(n>>8), byte(n)) - out = append(out, message[:n]...) - message = message[n:] - } - return out -} - -func TestAClientHelloIsReadWholeAcrossRecordsAndNamesItsSiteAndProtocols(t *testing.T) { - hello := goHello(t, "Example.COM", []string{"h2", "http/1.1"}) - sni, alpn, err := parseHello(hello) - if err != nil || sni != "example.com" || !reflect.DeepEqual(alpn, []string{"h2", "http/1.1"}) { - t.Fatalf("parsed %q %v %v", sni, alpn, err) - } - split := records(hello, 100) - again, err := clientHello(bufio.NewReader(bytes.NewReader(split)), time.Now().Add(time.Second)) - if err != nil || !bytes.Equal(again, hello) { - t.Fatalf("across records: %v", err) - } - if _, err := clientHello(bufio.NewReader(bytes.NewReader([]byte("GET / HTTP/1.1\r\n\r\n"))), - time.Now().Add(time.Second)); err == nil { - t.Fatal("plain HTTP read as TLS") - } -} - -func withExtension(hello []byte, kind uint16, body []byte) []byte { - p := 4 + 2 + 32 - p += 1 + int(hello[p]) - p += 2 + int(binary.BigEndian.Uint16(hello[p:])) - p += 1 + int(hello[p]) - exts := append(append([]byte{}, hello[p+2:]...), byte(kind>>8), byte(kind), byte(len(body)>>8), byte(len(body))) - exts = append(exts, body...) - out := append(append([]byte{}, hello[:p]...), byte(len(exts)>>8), byte(len(exts))) - out = append(out, exts...) - n := len(out) - 4 - out[1], out[2], out[3] = byte(n>>16), byte(n>>8), byte(n) - return out -} - -func TestTheUpstreamRecordLeavesOutWhatOnlyAResumptionWithTheProxyMeans(t *testing.T) { - hello := withExtension(goHello(t, "example.com", nil), 41, []byte{0, 1, 2}) - record, gone, err := helloRecord(hello) - if err != nil || !reflect.DeepEqual(gone, []uint16{41}) { - t.Fatalf("dropped %v %v", gone, err) - } - if record[0] != 22 || int(binary.BigEndian.Uint16(record[3:])) != len(record)-5 { - t.Fatal("not one handshake record") - } - if _, again, _ := helloRecord(record[5:]); len(again) != 0 { - t.Fatalf("the record still has %v", again) - } -} diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/leaves.go b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/leaves.go deleted file mode 100644 index 027ecac92..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/leaves.go +++ /dev/null @@ -1,188 +0,0 @@ -package main - -import ( - "crypto" - "crypto/ecdsa" - "crypto/elliptic" - "crypto/rand" - "crypto/tls" - "crypto/x509" - "crypto/x509/pkix" - "encoding/pem" - "errors" - "math/big" - "net" - "os" - "path/filepath" - "sort" - "strings" - "sync" -) - -// The longest common name X.509 allows (ub-common-name). -const commonNameLimit = 64 - -// Leaves signs the leaves an intercepted client is handed: by this computer's CA when the site verified, else by a CA -// trusted nowhere, so the client refuses it as it would the site. Each is valid over all of its CA's window, so it -// holds wherever the universe goes, and is minted once per name and its alternative names. -type Leaves struct { - ca, untrusted signer - key *ecdsa.PrivateKey - mu sync.Mutex - cache map[string]*tls.Certificate - // order is the cache's keys, oldest first, so it drops the oldest past cacheLimit; a connection keeps its own. - order []string -} - -type signer struct { - cert *x509.Certificate - key crypto.Signer -} - -func LoadLeaves(folder string) (*Leaves, error) { - certPEM, err := os.ReadFile(filepath.Join(folder, "ca.pem")) - if err != nil { - return nil, err - } - keyPEM, err := os.ReadFile(filepath.Join(folder, "ca.key")) - if err != nil { - return nil, err - } - ca, err := parseSigner(certPEM, keyPEM) - if err != nil { - return nil, err - } - return NewLeaves(ca) -} - -func NewLeaves(ca signer) (*Leaves, error) { - untrusted, err := mintCA("Untrusted upstream") - if err != nil { - return nil, err - } - key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) - if err != nil { - return nil, err - } - return &Leaves{ca: ca, untrusted: untrusted, key: key, cache: map[string]*tls.Certificate{}}, nil -} - -// For is the leaf for host and the names sans ("DNS:..." or "IP:..."), by this computer's CA when trusted, and -// whether it was minted now. -func (l *Leaves) For(host string, sans []string, trusted bool) (*tls.Certificate, bool, error) { - sorted := append([]string{}, sans...) - sort.Strings(sorted) - key := host + "|" + strings.Join(sorted, ",") - by := l.ca - if !trusted { - key, by = "untrusted|"+key, l.untrusted - } - l.mu.Lock() - defer l.mu.Unlock() - if cert, ok := l.cache[key]; ok { - return cert, false, nil - } - template := &x509.Certificate{ - SerialNumber: serial(), - Subject: pkix.Name{CommonName: truncate(host, commonNameLimit)}, - NotBefore: by.cert.NotBefore.Add(LeafInset), - NotAfter: by.cert.NotAfter.Add(-LeafInset), - KeyUsage: x509.KeyUsageDigitalSignature, - ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, - BasicConstraintsValid: true, - } - for _, name := range append([]string{host}, sans...) { - name = strings.TrimPrefix(strings.TrimPrefix(name, "DNS:"), "IP:") - if ip := net.ParseIP(strings.Trim(name, "[]")); ip != nil { - template.IPAddresses = appendIP(template.IPAddresses, ip) - } else if name != "" && !contains(template.DNSNames, name) { - template.DNSNames = append(template.DNSNames, name) - } - } - der, err := x509.CreateCertificate(rand.Reader, template, by.cert, &l.key.PublicKey, by.key) - if err != nil { - return nil, false, err - } - cert := &tls.Certificate{Certificate: [][]byte{der, by.cert.Raw}, PrivateKey: l.key} - l.cache[key] = cert - l.order = append(l.order, key) - for len(l.order) > cacheLimit { - delete(l.cache, l.order[0]) - l.order = l.order[1:] - } - return cert, true, nil -} - -// mintCA is a self-signed CA over the window, its key in memory. -func mintCA(name string) (signer, error) { - key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) - if err != nil { - return signer{}, err - } - template := &x509.Certificate{ - SerialNumber: serial(), - Subject: pkix.Name{CommonName: name}, - NotBefore: CAFrom, - NotAfter: CAUntil, - KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign | x509.KeyUsageDigitalSignature, - BasicConstraintsValid: true, - IsCA: true, - MaxPathLenZero: true, - } - der, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key) - if err != nil { - return signer{}, err - } - cert, err := x509.ParseCertificate(der) - if err != nil { - return signer{}, err - } - return signer{cert: cert, key: key}, nil -} - -func parseSigner(certPEM, keyPEM []byte) (signer, error) { - block, _ := pem.Decode(certPEM) - if block == nil { - return signer{}, errors.New("no certificate in the CA's PEM") - } - cert, err := x509.ParseCertificate(block.Bytes) - if err != nil { - return signer{}, err - } - block, _ = pem.Decode(keyPEM) - if block == nil { - return signer{}, errors.New("no key in the CA key's PEM") - } - key, err := x509.ParsePKCS8PrivateKey(block.Bytes) - if err != nil { - return signer{}, err - } - s, ok := key.(crypto.Signer) - if !ok { - return signer{}, errors.New("the CA key can't sign") - } - return signer{cert: cert, key: s}, nil -} - -func serial() *big.Int { - n, _ := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 127)) - return n -} - -func appendIP(ips []net.IP, ip net.IP) []net.IP { - for _, have := range ips { - if have.Equal(ip) { - return ips - } - } - return append(ips, ip) -} - -func contains(list []string, s string) bool { - for _, have := range list { - if strings.EqualFold(have, s) { - return true - } - } - return false -} diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/main.go b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/main.go deleted file mode 100644 index dd9760e99..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/main.go +++ /dev/null @@ -1,117 +0,0 @@ -// egress: the computer's explicit egress proxy, on real time, for the session's programs, which run on the universe's. -// -// It runs as its own user, agentenv-egress, a uid the clock library leaves on real time, under root's supervisord, and -// serves CONNECT and absolute-form HTTP on 127.0.0.1:3128, or where AGENTENV_EGRESS_PROXY names. Session programs -// reach it through HTTP(S)_PROXY and ALL_PROXY, or a proxy of the base's own that hands it their connections; nothing -// redirects or filters, so it needs no NET_ADMIN and works on any provider. -// -// A TLS connection is spliced (the client's bytes relayed untouched) when the clock is unarmed, or when every universe -// a client may check against lies inside the site's real certificate by more than Margin either side. Otherwise it is -// intercepted: the site is verified against real time and the public roots, and the client is handed a leaf from this -// computer's CA, valid over every date the clock can show. The upstream handshake is uTLS's, built from the client's -// own ClientHello, so the site sees the client's fingerprint and ALPN offer either way. -// -// egress boot as root at the computer's start: the CA, minted once, its key this user's alone, trusted everywhere -// egress serve as agentenv-egress: serve, one JSON line per connection on stdout -package main - -import ( - "encoding/json" - "fmt" - "net/url" - "os" - "strings" - "time" -) - -const ( - Listen = "127.0.0.1:3128" - ProxyEnv = "AGENTENV_EGRESS_PROXY" - EgressUser = "agentenv-egress" - SessionUser = "user" - Folder = "/etc/agentenv/egress" - PublicCA = "/etc/agentenv/egress-ca.pem" - PublicRoots = "/usr/local/lib/agentenv-egress/roots.pem" - ClockPage = "/dev/shm/agentenv-clock" - Hosts = "/etc/hosts" - HostsBegin = "# agentenv sites" - HostsEnd = "# end agentenv sites" - CAName = "agentenv egress CA" - NSSNickname = "agentenv egress" - Hold = 30 * time.Second - Margin = 8 * 24 * time.Hour - ProbeTTL = time.Hour - ProbeFailedTTL = time.Minute - FirstBytes = 300 * time.Millisecond - HelloTimeout = 10 * time.Second - DialTimeout = 10 * time.Second - - // How long a client has to send its request's head, and how long either handshake of an interception, the - // client's with the proxy and the proxy's with the site, has to finish. - HeadTimeout = 15 * time.Second - HandshakeTimeout = 15 * time.Second - - // The longest request head a client may send, and the most of an error a log row keeps, in bytes. - HeadLimit = 64 << 10 - ErrorLimit = 300 - - // How far inside the CA's dates each leaf it signs is, so no leaf outlasts its CA. - LeafInset = 2 * time.Hour - - // How often the clock page is read, and how many reads may find it mid-write before the last universe stands. - PagePoll = 20 * time.Millisecond - PageReads = 1000 -) - -// The dates the CA and every leaf it signs are valid between: the widest a certificate carries, as routed sites' -// (sites.UNIVERSE_FROM and UNIVERSE_UNTIL in agent-env's site routing), so they hold at any date the clock can show. -var ( - CAFrom = time.Date(1950, 1, 1, 0, 0, 0, 0, time.UTC) - CAUntil = time.Date(9999, 12, 31, 23, 59, 59, 0, time.UTC) -) - -func main() { - command := "" - if len(os.Args) > 1 { - command = os.Args[1] - } - switch command { - case "boot": - report, err := boot(defaultBootPaths()) - out, _ := json.Marshal(report) - fmt.Println(string(out)) - if err != nil { - fmt.Fprintln(os.Stderr, "egress boot:", err) - os.Exit(1) - } - case "serve": - listen, err := serveListen(os.Args[2:], os.Getenv(ProxyEnv)) - if err != nil { - fmt.Fprintln(os.Stderr, "egress serve:", err) - os.Exit(2) - } - if err := serveForever(listen); err != nil { - fmt.Fprintln(os.Stderr, "egress serve:", err) - os.Exit(1) - } - default: - fmt.Fprintf(os.Stderr, "usage: egress boot|serve, not %q\n", command) - os.Exit(2) - } -} - -// serveListen is the address serve listens on: the host and port of named, the URL a base whose own proxy holds Listen -// gives its proxies for this one in ProxyEnv, else Listen. -func serveListen(args []string, named string) (string, error) { - if len(args) != 0 { - return "", fmt.Errorf("usage: egress serve, not %q", args) - } - if named == "" { - return Listen, nil - } - u, err := url.Parse(named) - if err != nil || u.Scheme != "http" || u.Hostname() == "" || u.Port() == "" || strings.Trim(u.Path, "/") != "" { - return "", fmt.Errorf("%s is http://HOST:PORT, not %q", ProxyEnv, named) - } - return u.Host, nil -} diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/main_test.go b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/main_test.go deleted file mode 100644 index 77f80844e..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/main_test.go +++ /dev/null @@ -1,27 +0,0 @@ -package main - -import "testing" - -func TestServeListensWhereTheBaseNamesItElseOnListen(t *testing.T) { - for _, tc := range []struct { - args []string - named string - want string - ok bool - }{ - {nil, "", Listen, true}, - {nil, "http://127.0.0.1:3130", "127.0.0.1:3130", true}, - {nil, "http://127.0.0.1:3130/", "127.0.0.1:3130", true}, - {nil, "http://[::1]:3130", "[::1]:3130", true}, - {nil, "127.0.0.1:3130", "", false}, - {nil, "http://127.0.0.1", "", false}, - {nil, "https://127.0.0.1:3130", "", false}, - {nil, "http://127.0.0.1:3130/proxy", "", false}, - {[]string{"--listen", "127.0.0.1:3130"}, "", "", false}, - } { - got, err := serveListen(tc.args, tc.named) - if (err == nil) != tc.ok || got != tc.want { - t.Errorf("serveListen(%q, %q) = %q, %v; want %q, ok %v", tc.args, tc.named, got, err, tc.want, tc.ok) - } - } -} diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/proxy.go b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/proxy.go deleted file mode 100644 index 3755399b9..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/proxy.go +++ /dev/null @@ -1,598 +0,0 @@ -package main - -import ( - "bufio" - "context" - "crypto/tls" - "crypto/x509" - "encoding/json" - "errors" - "fmt" - "io" - "net" - "net/http" - "net/netip" - "net/url" - "os" - "strconv" - "strings" - "sync" - "time" -) - -type Proxy struct { - Leaves *Leaves - Universe interface { - View(time.Time) (bool, []float64) - } - // Roots are the public roots a site is verified against, as the image had them before any agentenv CA joined. - Roots *x509.CertPool - // Resolve, Allowed, Routed and Dial default to the system's resolver, a global address, the hosts file's agentenv - // sites block and a TCP dial; tests replace them. - Resolve func(ctx context.Context, host string) ([]netip.Addr, error) - Allowed func(netip.Addr) bool - Routed func() map[string]string - Dial func(dst string) (net.Conn, error) - Log func(row map[string]any) - probes probes -} - -func serveForever(listen string) error { - leaves, err := LoadLeaves(Folder) - if err != nil { - return fmt.Errorf("the CA (has boot run?): %w", err) - } - roots, err := os.ReadFile(PublicRoots) - if err != nil { - return err - } - pool := x509.NewCertPool() - if !pool.AppendCertsFromPEM(roots) { - return fmt.Errorf("no certificates in %s", PublicRoots) - } - universe := NewUniverse(ClockPage) - go universe.Poll() - ln, err := net.Listen("tcp", listen) - if err != nil { - return err - } - p := &Proxy{Leaves: leaves, Universe: universe, Roots: pool, Log: jsonLines(os.Stdout)} - p.Log(map[string]any{"event": "serving", "listen": listen, "uid": os.Getuid(), "pid": os.Getpid()}) - return p.Serve(ln) -} - -func jsonLines(w io.Writer) func(map[string]any) { - var mu sync.Mutex - encoder := json.NewEncoder(w) - return func(row map[string]any) { - row["t"] = float64(time.Now().UnixMilli()) / 1e3 - mu.Lock() - defer mu.Unlock() - encoder.Encode(row) - } -} - -func (p *Proxy) Serve(ln net.Listener) error { - for { - conn, err := ln.Accept() - if err != nil { - if errors.Is(err, net.ErrClosed) { - return nil - } - return err - } - go p.handle(conn) - } -} - -func (p *Proxy) log(row map[string]any) { - if p.Log != nil { - p.Log(row) - } -} - -func (p *Proxy) handle(conn net.Conn) { - start := time.Now() - row := map[string]any{"client": conn.RemoteAddr().String()} - defer func() { - if r := recover(); r != nil { - row["error"] = fmt.Sprint("panic: ", r) - p.log(row) - } - conn.Close() - }() - br := bufio.NewReaderSize(conn, HeadLimit) - conn.SetReadDeadline(start.Add(HeadTimeout)) - method, target, version, headers, err := readHead(br) - if err != nil { - row["error"] = truncate("request: "+err.Error(), ErrorLimit) - p.log(row) - return - } - row["method"] = method - if method == http.MethodConnect { - host, port := splitAuthority(target, 443) - row["host"], row["port"] = host, port - dsts, why, err := p.destination(host, port) - if err != nil { - row["mode"], row["error"] = "refused", err.Error() - reply(conn, "403 Forbidden", "egress: "+err.Error()+"\n") - p.log(row) - return - } - row["allowed"] = why - if _, err := io.WriteString(conn, "HTTP/1.1 200 Connection established\r\n\r\n"); err != nil { - return - } - p.tunnel(conn, br, host, port, dsts, why, row, start) - return - } - u, err := url.Parse(target) - if err != nil || u.Scheme != "http" || u.Host == "" { - row["mode"], row["error"] = "refused", "not an absolute http URL" - reply(conn, "400 Bad Request", "egress: CONNECT, or an absolute http:// URL\n") - p.log(row) - return - } - host, port := splitAuthority(u.Host, 80) - row["host"], row["port"], row["mode"] = host, port, "http" - dsts, why, err := p.destination(host, port) - if err != nil { - row["mode"], row["error"] = "refused", err.Error() - reply(conn, "403 Forbidden", "egress: "+err.Error()+"\n") - p.log(row) - return - } - row["allowed"] = why - up, dst, err := p.dialAny(dsts) - if err != nil { - row["error"] = truncate(err.Error(), ErrorLimit) - reply(conn, "502 Bad Gateway", "egress: "+err.Error()+"\n") - p.log(row) - return - } - defer up.Close() - row["dst"] = dst - upgrade := false - for _, h := range headers { - if headerName(h) == "upgrade" { - upgrade = true - } - } - // The client's own head, but for its request line, which names the path alone, and the headers meant for this - // proxy; a request that isn't an upgrade closes once answered, so its connection can't carry one for another site. - var head strings.Builder - head.WriteString(method + " " + u.RequestURI() + " " + version + "\r\n") - for _, h := range headers { - switch headerName(h) { - case "proxy-connection", "proxy-authorization": - continue - case "connection", "keep-alive": - if !upgrade { - continue - } - } - head.WriteString(h + "\r\n") - } - if upgrade { - row["mode"] = "upgrade" - } else { - head.WriteString("Connection: close\r\n") - } - head.WriteString("\r\n") - conn.SetReadDeadline(time.Time{}) - if _, err := io.WriteString(up, head.String()); err != nil { - row["error"] = truncate("upstream: "+err.Error(), ErrorLimit) - p.log(row) - return - } - p.log(row) - // Both ways at once: a body the client sends once told to continue, or an upgraded connection's frames. - go func() { - io.Copy(up, br) - closeWrite(up) - }() - io.Copy(conn, up) -} - -// readHead is a request's head, read through its blank line: its request line's parts and its header lines. -func readHead(r *bufio.Reader) (string, string, string, []string, error) { - var lines []string - size := 0 - for { - line, err := r.ReadString('\n') - size += len(line) - if size > HeadLimit { - return "", "", "", nil, errors.New("request head too long") - } - if err != nil { - return "", "", "", nil, err - } - line = strings.TrimRight(line, "\r\n") - if line == "" { - if len(lines) == 0 { - continue - } - break - } - lines = append(lines, line) - } - parts := strings.SplitN(lines[0], " ", 3) - if len(parts) != 3 { - return "", "", "", nil, fmt.Errorf("malformed request line %q", truncate(lines[0], 100)) - } - return parts[0], parts[1], parts[2], lines[1:], nil -} - -func headerName(line string) string { - name, _, _ := strings.Cut(line, ":") - return strings.ToLower(strings.TrimSpace(name)) -} - -// destination is the addresses to dial for host:port, in the order to try them, and why they're allowed: "routed" for -// a name the hosts file's agentenv sites block maps (the site proxy, as a client that bypasses this proxy reaches it), -// "global" for a name whose every address is global, IPv4 first. Anything else is refused. -func (p *Proxy) destination(host string, port int) ([]string, string, error) { - host = strings.TrimSuffix(strings.ToLower(strings.Trim(host, "[]")), ".") - routed := p.Routed - if routed == nil { - routed = routedSites - } - if address, ok := routed()[host]; ok { - return []string{net.JoinHostPort(address, strconv.Itoa(port))}, "routed", nil - } - resolve := p.Resolve - if resolve == nil { - resolve = func(ctx context.Context, host string) ([]netip.Addr, error) { - return net.DefaultResolver.LookupNetIP(ctx, "ip", host) - } - } - allowed := p.Allowed - if allowed == nil { - allowed = isGlobal - } - ctx, cancel := context.WithTimeout(context.Background(), DialTimeout) - defer cancel() - addresses, err := resolve(ctx, host) - if err != nil { - return nil, "", fmt.Errorf("%s doesn't resolve: %w", host, err) - } - if len(addresses) == 0 { - return nil, "", fmt.Errorf("%s has no address", host) - } - var private []string - var v4, v6 []string - for _, a := range addresses { - a = a.Unmap() - dst := netip.AddrPortFrom(a, uint16(port)).String() - switch { - case !allowed(a): - private = append(private, a.String()) - case a.Is4(): - v4 = append(v4, dst) - default: - v6 = append(v6, dst) - } - } - if len(private) > 0 { - return nil, "", fmt.Errorf("%s:%d isn't public (%s)", host, port, strings.Join(private, ", ")) - } - return append(v4, v6...), "global", nil -} - -// notGlobal are the special-purpose blocks (IANA's registries) no global address is in. -var notGlobal = func() []netip.Prefix { - var out []netip.Prefix - for _, s := range []string{ - "0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", "127.0.0.0/8", "169.254.0.0/16", "172.16.0.0/12", - "192.0.0.0/24", "192.0.2.0/24", "192.168.0.0/16", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24", - "224.0.0.0/4", "240.0.0.0/4", - "::/128", "::1/128", "64:ff9b:1::/48", "100::/64", "2001::/23", "2001:db8::/32", "fc00::/7", "fe80::/10", - "ff00::/8", - } { - out = append(out, netip.MustParsePrefix(s)) - } - return out -}() - -func isGlobal(a netip.Addr) bool { - a = a.Unmap() - if !a.IsValid() { - return false - } - for _, block := range notGlobal { - if block.Contains(a) { - return false - } - } - return true -} - -// routedSites is the hosts file's agentenv sites block, name to address: root's, so the session user can't add to it. -func routedSites() map[string]string { - out := map[string]string{} - data, err := os.ReadFile(Hosts) - if err != nil { - return out - } - inside := false - for _, line := range strings.Split(string(data), "\n") { - text := strings.TrimSpace(line) - switch { - case text == HostsBegin: - inside = true - case text == HostsEnd: - inside = false - case inside && text != "" && !strings.HasPrefix(text, "#"): - fields := strings.Fields(text) - for _, name := range fields[1:] { - out[strings.ToLower(name)] = fields[0] - } - } - } - return out -} - -func (p *Proxy) tunnel(conn net.Conn, br *bufio.Reader, host string, port int, dsts []string, why string, - row map[string]any, start time.Time) { - wait := FirstBytes - if port == 443 { - wait = HelloTimeout - } - conn.SetReadDeadline(time.Now().Add(wait)) - first, err := br.Peek(1) - if err != nil && !isTimeout(err) { - row["error"] = "closed before sending" - p.log(row) - return - } - if len(first) == 0 || first[0] != recordHandshake || why == "routed" { - row["mode"], row["decision"] = "pass", "not TLS" - if len(first) > 0 && first[0] == recordHandshake { - row["mode"], row["decision"] = "splice", "routed" - } - if !p.splice(conn, br, dsts, row, start) { - p.log(row) - } - return - } - conn.SetReadDeadline(time.Now().Add(HelloTimeout)) - hello, err := clientHello(br, time.Now().Add(HelloTimeout)) - if err != nil { - row["error"] = truncate("ClientHello: "+err.Error(), ErrorLimit) - p.log(row) - return - } - sni, offered, err := parseHello(hello) - if err != nil { - row["error"] = truncate("ClientHello: "+err.Error(), ErrorLimit) - p.log(row) - return - } - row["sni"], row["alpn_offered"] = sni, offered - verifyName := sni - if verifyName == "" { - verifyName = strings.Trim(host, "[]") - } - armed, universes := p.Universe.View(time.Now()) - if !armed { - row["decision"], row["mode"] = "real", "splice" - if !p.splice(conn, br, dsts, row, start) { - p.log(row) - } - return - } - row["universes"] = rounded(universes) - // Each address in turn, by its own probe: one that doesn't answer is passed over, so the connection goes to a - // server whose own certificate decided it. - var unreachable []string - for _, dst := range p.preferChecked(dsts, sni) { - delete(row, "probe_error") - delete(row, "upstream_valid") - seen, probed := p.probeAt(dst, sni, verifyName, hello) - if seen.unreachable { - unreachable = append(unreachable, seen.problem) - continue - } - row["probed"] = probed - decision := "outside" - switch { - case seen.failed: - // a handshake only the client can finish, as one asking for its certificate: the client decides - decision, row["probe_error"] = "probe_failed", seen.problem - case !seen.verified: - decision, row["probe_error"] = "unverified", seen.problem - case inside(seen.from, seen.until, universes): - decision = "inside" - } - if seen.verified { - row["upstream_valid"] = []int64{seen.from.Unix(), seen.until.Unix()} - } - row["decision"] = decision - var dialled bool - if decision == "inside" || decision == "probe_failed" { - row["mode"] = "splice" - dialled = p.splice(conn, br, []string{dst}, row, start) - } else { - row["mode"] = "intercept" - dialled = p.intercept(conn, br, dst, sni, verifyName, hello, row, start) - } - if dialled { - return - } - // it stopped answering since its probe: forget it, and go on to the next - p.forget(dst, sni) - unreachable = append(unreachable, fmt.Sprint(row["error"])) - delete(row, "error") - } - row["error"] = truncate("upstream: no address answered: "+strings.Join(unreachable, "; "), ErrorLimit) - p.log(row) -} - -// preferChecked is dsts with those whose fresh probe answered first, each group in its own order, so an address -// already found working isn't kept waiting behind one whose probe has to be made again. -func (p *Proxy) preferChecked(dsts []string, sni string) []string { - p.probes.mu.Lock() - defer p.probes.mu.Unlock() - var checked, rest []string - for _, dst := range dsts { - if hit, ok := p.probes.cache[dst+"|"+sni]; ok && hit.fresh() && !hit.unreachable { - checked = append(checked, dst) - } else { - rest = append(rest, dst) - } - } - return append(checked, rest...) -} - -// splice relays the connection to the first of dsts that answers, untouched; false, with the error in row, when none -// does. -func (p *Proxy) splice(conn net.Conn, br *bufio.Reader, dsts []string, row map[string]any, start time.Time) bool { - up, dst, err := p.dialAny(dsts) - if err != nil { - row["error"] = truncate("upstream: "+err.Error(), ErrorLimit) - return false - } - defer up.Close() - row["dst"] = dst - conn.SetReadDeadline(time.Time{}) - row["upstream_ms"] = time.Since(start).Milliseconds() - p.log(row) - var wg sync.WaitGroup - wg.Add(1) - go func() { - defer wg.Done() - io.Copy(up, br) - closeWrite(up) - }() - io.Copy(conn, up) - closeWrite(conn) - wg.Wait() - return true -} - -// intercept hands the client a leaf of this computer's CA and relays the plaintext to dst over a handshake of the -// proxy's own; false, with the error in row, when dst doesn't answer. -func (p *Proxy) intercept(conn net.Conn, br *bufio.Reader, dst, sni, verifyName string, hello []byte, - row map[string]any, start time.Time) bool { - uc, found, err := p.handshake([]string{dst}, sni, verifyName, hello) - var unanswered dialError - if errors.As(err, &unanswered) { - row["error"] = truncate("upstream: "+err.Error(), ErrorLimit) - return false - } - if err != nil { - row["error"] = truncate("upstream: "+err.Error(), ErrorLimit) - p.log(row) - return true - } - defer uc.NetConn().Close() - row["dst"] = dst - row["alpn"], row["verified"], row["upstream_tls"] = found.ALPN, found.Verified, []string{found.Version, found.Cipher} - row["upstream_ms"] = time.Since(start).Milliseconds() - if len(found.Dropped) > 0 { - row["dropped_extensions"] = found.Dropped - } - var sans []string - if found.Verified { - // the site's own names, as its leaf lists them, so the client pools connections as it would with the site's - // certificate - for _, name := range found.SANs { - if sni == "" || strings.HasPrefix(name, "DNS:") { - sans = append(sans, name) - } - } - if sni == "" { - sans = append(sans, "IP:"+strings.Trim(hostOf(dst), "[]")) - } - } else { - row["upstream_verify"] = truncate(found.VerifyErr, 200) - } - cert, minted, err := p.Leaves.For(verifyName, sans, found.Verified) - if err != nil { - row["error"] = truncate("leaf: "+err.Error(), ErrorLimit) - p.log(row) - return true - } - row["minted"] = minted - config := &tls.Config{Certificates: []tls.Certificate{*cert}, SessionTicketsDisabled: true} - if found.ALPN != "" { - config.NextProtos = []string{found.ALPN} - } - client := tls.Server(&peeked{Conn: conn, r: br}, config) - conn.SetDeadline(time.Now().Add(HandshakeTimeout)) - if err := client.Handshake(); err != nil { - row["error"] = truncate("client handshake: "+err.Error(), ErrorLimit) - p.log(row) - return true - } - conn.SetDeadline(time.Time{}) - row["handshake_ms"] = time.Since(start).Milliseconds() - p.log(row) - done := make(chan struct{}, 2) - go func() { - io.Copy(uc, client) - done <- struct{}{} - }() - go func() { - io.Copy(client, uc) - done <- struct{}{} - }() - <-done - client.Close() - uc.NetConn().Close() - <-done - return true -} - -// peeked is a connection whose reads come through the reader that peeked its first bytes. -type peeked struct { - net.Conn - r *bufio.Reader -} - -func (c *peeked) Read(b []byte) (int, error) { return c.r.Read(b) } - -func closeWrite(c net.Conn) { - if cw, ok := c.(interface{ CloseWrite() error }); ok { - cw.CloseWrite() - } -} - -func isTimeout(err error) bool { - var ne net.Error - return errors.As(err, &ne) && ne.Timeout() -} - -func reply(conn net.Conn, status, body string) { - fmt.Fprintf(conn, "HTTP/1.1 %s\r\nContent-Type: text/plain\r\nContent-Length: %d\r\nConnection: close\r\n\r\n%s", - status, len(body), body) -} - -func splitAuthority(authority string, port int) (string, int) { - host, p, err := net.SplitHostPort(authority) - if err != nil { - return strings.Trim(authority, "[]"), port - } - n, err := strconv.Atoi(p) - if err != nil { - return host, port - } - return host, n -} - -func hostOf(hostport string) string { - host, _, err := net.SplitHostPort(hostport) - if err != nil { - return hostport - } - return host -} - -func rounded(values []float64) []int64 { - out := make([]int64, len(values)) - for i, v := range values { - out[i] = int64(v) - } - return out -} diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/proxy_test.go b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/proxy_test.go deleted file mode 100644 index c8e57b3e7..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/proxy_test.go +++ /dev/null @@ -1,564 +0,0 @@ -package main - -import ( - "bufio" - "bytes" - "context" - "crypto/tls" - "crypto/x509" - "fmt" - "io" - "net" - "net/http" - "net/http/httptest" - "net/netip" - "net/url" - "strings" - "sync" - "testing" - "time" -) - -type fixedUniverse struct { - armed bool - universes []float64 -} - -func (f fixedUniverse) View(time.Time) (bool, []float64) { return f.armed, f.universes } - -func TestOnlyDestinationsWhoseEveryAddressIsGlobalAreDialled(t *testing.T) { - refused := []string{"10.0.0.1", "10.255.255.254", "172.16.0.1", "172.31.255.1", "192.168.1.1", "127.0.0.1", - "127.1.2.3", "0.0.0.0", "169.254.169.254", "169.254.1.1", "100.64.0.1", "100.127.255.254", "192.0.2.1", - "198.18.0.1", "224.0.0.1", "255.255.255.255", "::1", "::", "fd00::1", "fe80::1", "::ffff:10.0.0.1", - "2001:db8::1"} - if len(refused) != 22 { - t.Fatal(len(refused)) - } - p := &Proxy{Routed: func() map[string]string { return nil }} - for _, address := range refused { - p.Resolve = func(context.Context, string) ([]netip.Addr, error) { - return []netip.Addr{netip.MustParseAddr(address)}, nil - } - if _, _, err := p.destination("somewhere.example", 443); err == nil { - t.Errorf("%s dialled", address) - } - } - p.Resolve = func(context.Context, string) ([]netip.Addr, error) { - return []netip.Addr{netip.MustParseAddr("2606:4700::1111"), netip.MustParseAddr("1.1.1.1")}, nil - } - dst, why, err := p.destination("one.one.one.one", 443) - if err != nil || strings.Join(dst, " ") != "1.1.1.1:443 [2606:4700::1111]:443" || why != "global" { - t.Fatalf("a public name, IPv4 first: %q %q %v", dst, why, err) - } - p.Resolve = func(context.Context, string) ([]netip.Addr, error) { - return []netip.Addr{netip.MustParseAddr("1.1.1.1"), netip.MustParseAddr("10.0.0.1")}, nil - } - if _, _, err := p.destination("split.example", 443); err == nil || !strings.Contains(err.Error(), "10.0.0.1") { - t.Fatalf("a name with one private address: %v", err) - } - p.Routed = func() map[string]string { return map[string]string{"notes.test": "127.0.0.1"} } - if dst, why, err := p.destination("Notes.Test.", 443); err != nil || strings.Join(dst, " ") != "127.0.0.1:443" || - why != "routed" { - t.Fatalf("a routed site: %q %q %v", dst, why, err) - } -} - -// harness is a proxy in front of the site server, which it reaches as example.com, and a client whose clock is at -// clientTime. -type harness struct { - t *testing.T - site *httptest.Server - proxy *Proxy - url *url.URL - ca signer - clientCert *tls.Certificate - rows []map[string]any - rowsMu sync.Mutex -} - -func newHarness(t *testing.T, site *httptest.Server, universe fixedUniverse, trustSite bool) *harness { - t.Helper() - ca, err := mintCA(CAName) - if err != nil { - t.Fatal(err) - } - leaves, err := NewLeaves(ca) - if err != nil { - t.Fatal(err) - } - roots := x509.NewCertPool() - if trustSite && site.Certificate() != nil { - roots.AddCert(site.Certificate()) - } - h := &harness{t: t, site: site, ca: ca} - h.proxy = &Proxy{ - Leaves: leaves, Universe: universe, Roots: roots, - Resolve: func(context.Context, string) ([]netip.Addr, error) { - return []netip.Addr{netip.MustParseAddr("127.0.0.1")}, nil - }, - Allowed: func(netip.Addr) bool { return true }, - Routed: func() map[string]string { return nil }, - Log: func(row map[string]any) { - h.rowsMu.Lock() - h.rows = append(h.rows, row) - h.rowsMu.Unlock() - }, - } - ln, err := net.Listen("tcp", "127.0.0.1:0") - if err != nil { - t.Fatal(err) - } - t.Cleanup(func() { ln.Close() }) - go h.proxy.Serve(ln) - h.url, _ = url.Parse("http://" + ln.Addr().String()) - return h -} - -func (h *harness) get(scheme string, clientTime time.Time, http2 bool) (*http.Response, string, error) { - roots := x509.NewCertPool() - if h.site.Certificate() != nil { - roots.AddCert(h.site.Certificate()) - } - roots.AddCert(h.ca.cert) - config := &tls.Config{RootCAs: roots, Time: func() time.Time { return clientTime }} - if h.clientCert != nil { - config.Certificates = []tls.Certificate{*h.clientCert} - } - transport := &http.Transport{Proxy: http.ProxyURL(h.url), ForceAttemptHTTP2: http2, TLSClientConfig: config} - defer transport.CloseIdleConnections() - _, port, _ := net.SplitHostPort(h.site.Listener.Addr().String()) - resp, err := (&http.Client{Transport: transport, Timeout: 10 * time.Second}).Get( - fmt.Sprintf("%s://example.com:%s/hello", scheme, port)) - if err != nil { - return nil, "", err - } - defer resp.Body.Close() - body, err := io.ReadAll(resp.Body) - return resp, string(body), err -} - -func (h *harness) lastRow() map[string]any { - deadline := time.Now().Add(2 * time.Second) - for time.Now().Before(deadline) { - h.rowsMu.Lock() - n := len(h.rows) - h.rowsMu.Unlock() - if n > 0 { - break - } - time.Sleep(10 * time.Millisecond) - } - h.rowsMu.Lock() - defer h.rowsMu.Unlock() - if len(h.rows) == 0 { - h.t.Fatal("no row logged") - } - return h.rows[len(h.rows)-1] -} - -func site(t *testing.T, http2 bool) *httptest.Server { - s := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "hello over %s", r.Proto) - })) - s.EnableHTTP2 = http2 - s.StartTLS() - t.Cleanup(s.Close) - return s -} - -func TestOnRealTimeTheClientsBytesReachTheSiteUntouched(t *testing.T) { - s := site(t, false) - h := newHarness(t, s, fixedUniverse{}, true) - resp, body, err := h.get("https", time.Now(), false) - if err != nil || body != "hello over HTTP/1.1" { - t.Fatalf("%q %v", body, err) - } - if !resp.TLS.PeerCertificates[0].Equal(s.Certificate()) { - t.Fatal("the client didn't see the site's own certificate") - } - if row := h.lastRow(); row["mode"] != "splice" || row["decision"] != "real" { - t.Fatalf("%v", row) - } -} - -func TestAUniverseInsideTheSitesCertificateIsSplicedAndOneOutsideIntercepted(t *testing.T) { - s := site(t, true) - now := float64(time.Now().Unix()) - h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{now, now}}, true) - resp, _, err := h.get("https", time.Now(), true) - if err != nil || !resp.TLS.PeerCertificates[0].Equal(s.Certificate()) { - t.Fatalf("inside: %v", err) - } - if row := h.lastRow(); row["mode"] != "splice" || row["decision"] != "inside" { - t.Fatalf("%v", row) - } - - // httptest's certificate ends in 2084: a universe in 2090 is outside it. - later := time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC) - u := float64(later.Unix()) - h = newHarness(t, s, fixedUniverse{armed: true, universes: []float64{u, u}}, true) - resp, body, err := h.get("https", later, true) - if err != nil || body != "hello over HTTP/2.0" { - t.Fatalf("outside: %q %v", body, err) - } - leaf := resp.TLS.PeerCertificates[0] - if leaf.CheckSignatureFrom(h.ca.cert) != nil || !leaf.NotAfter.After(later) || leaf.NotBefore.Year() != 1950 { - t.Fatalf("the client's leaf: issuer %s, %s to %s", leaf.Issuer, leaf.NotBefore, leaf.NotAfter) - } - row := h.lastRow() - if row["mode"] != "intercept" || row["decision"] != "outside" || row["alpn"] != "h2" || row["verified"] != true { - t.Fatalf("%v", row) - } -} - -func TestASiteThatDoesntVerifyIsHandedOnUnderACATrustedNowhere(t *testing.T) { - s := site(t, false) - u := float64(time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC).Unix()) - h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{u}}, false) - _, _, err := h.get("https", time.Now(), false) - if err == nil || !strings.Contains(err.Error(), "certificate") { - t.Fatalf("the client took it: %v", err) - } - if row := h.lastRow(); row["decision"] != "unverified" { - t.Fatalf("%v", row) - } -} - -func TestPlainHTTPInAbsoluteFormIsForwarded(t *testing.T) { - s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "plain %s %s ua=%q", r.Method, r.URL.Path, r.UserAgent()) - })) - t.Cleanup(s.Close) - h := newHarness(t, s, fixedUniverse{}, false) - _, body, err := h.get("http", time.Now(), false) - if err != nil || !strings.HasPrefix(body, "plain GET /hello") { - t.Fatalf("%q %v", body, err) - } -} - -func TestARefusedDestinationIsAnswered403(t *testing.T) { - s := site(t, false) - h := newHarness(t, s, fixedUniverse{}, true) - h.proxy.Allowed = isGlobal - _, _, err := h.get("https", time.Now(), false) - if err == nil || !strings.Contains(err.Error(), "Forbidden") { - t.Fatalf("%v", err) - } - if row := h.lastRow(); row["mode"] != "refused" { - t.Fatalf("%v", row) - } -} - -func TestEveryCheckedAddressIsTriedInTurn(t *testing.T) { - s := site(t, false) - h := newHarness(t, s, fixedUniverse{}, true) - h.proxy.Resolve = func(context.Context, string) ([]netip.Addr, error) { - return []netip.Addr{netip.MustParseAddr("127.0.0.2"), netip.MustParseAddr("127.0.0.1")}, nil - } - _, body, err := h.get("https", time.Now(), false) - if err != nil || body != "hello over HTTP/1.1" { - t.Fatalf("%q %v", body, err) - } - if row := h.lastRow(); !strings.HasPrefix(fmt.Sprint(row["dst"]), "127.0.0.1:") { - t.Fatalf("%v", row) - } -} - -func TestAnUpgradeOverPlainHTTPRelaysBothWays(t *testing.T) { - s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.Header.Get("Upgrade") != "echo" || !strings.Contains(strings.ToLower(r.Header.Get("Connection")), "upgrade") { - w.WriteHeader(http.StatusBadRequest) - return - } - conn, rw, _ := w.(http.Hijacker).Hijack() - defer conn.Close() - rw.WriteString("HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nUpgrade: echo\r\n\r\n") - rw.Flush() - line, _ := rw.ReadString('\n') - rw.WriteString(line) - rw.Flush() - })) - t.Cleanup(s.Close) - h := newHarness(t, s, fixedUniverse{}, false) - _, port, _ := net.SplitHostPort(s.Listener.Addr().String()) - conn, err := net.Dial("tcp", h.url.Host) - if err != nil { - t.Fatal(err) - } - defer conn.Close() - conn.SetDeadline(time.Now().Add(5 * time.Second)) - fmt.Fprintf(conn, "GET http://example.com:%s/ws HTTP/1.1\r\nHost: example.com:%s\r\nConnection: Upgrade\r\n"+ - "Upgrade: echo\r\n\r\n", port, port) - br := bufio.NewReader(conn) - resp, err := http.ReadResponse(br, nil) - if err != nil || resp.StatusCode != http.StatusSwitchingProtocols { - t.Fatalf("%v %v", resp, err) - } - fmt.Fprint(conn, "ping\n") - if line, err := br.ReadString('\n'); err != nil || line != "ping\n" { - t.Fatalf("%q %v", line, err) - } -} - -func TestAnUploadThatWaitsFor100ContinueGoesThroughAtOnce(t *testing.T) { - s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - body, _ := io.ReadAll(r.Body) - fmt.Fprint(w, len(body)) - })) - t.Cleanup(s.Close) - h := newHarness(t, s, fixedUniverse{}, false) - _, port, _ := net.SplitHostPort(s.Listener.Addr().String()) - req, _ := http.NewRequest("POST", "http://example.com:"+port+"/up", bytes.NewReader(make([]byte, 100000))) - req.Header.Set("Expect", "100-continue") - transport := &http.Transport{Proxy: http.ProxyURL(h.url), ExpectContinueTimeout: 10 * time.Second} - defer transport.CloseIdleConnections() - started := time.Now() - resp, err := (&http.Client{Transport: transport, Timeout: 20 * time.Second}).Do(req) - if err != nil { - t.Fatal(err) - } - body, _ := io.ReadAll(resp.Body) - resp.Body.Close() - if string(body) != "100000" || time.Since(started) > 5*time.Second { - t.Fatalf("%q after %s", body, time.Since(started)) - } -} - -func TestEveryCertificateOfTheVerifiedChainBoundsPassingThrough(t *testing.T) { - year := func(y int) time.Time { return time.Date(y, 1, 1, 0, 0, 0, 0, time.UTC) } - leaf := &x509.Certificate{NotBefore: year(2020), NotAfter: year(2030)} - intermediate := &x509.Certificate{NotBefore: year(2019), NotAfter: year(2025)} - root := &x509.Certificate{NotBefore: year(2000), NotAfter: year(2040)} - from, until := chainWindow([][]*x509.Certificate{{leaf, intermediate, root}}) - if !from.Equal(year(2020)) || !until.Equal(year(2025)) { - t.Fatalf("%s to %s", from, until) - } - if inside(from, until, []float64{float64(year(2027).Unix())}) { - t.Fatal("a universe past the intermediate's end passed through") - } -} - -func TestASiteAskingForTheClientsCertificateIsPassedThroughForTheClientToAnswer(t *testing.T) { - s := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "hello %d certificate", len(r.TLS.PeerCertificates)) - })) - s.TLS = &tls.Config{ClientAuth: tls.RequireAnyClientCert, MaxVersion: tls.VersionTLS12} - s.StartTLS() - t.Cleanup(s.Close) - u := float64(time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC).Unix()) - h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{u}}, true) - client, err := mintCA("client") - if err != nil { - t.Fatal(err) - } - h.clientCert = &tls.Certificate{Certificate: [][]byte{client.cert.Raw}, PrivateKey: client.key} - _, body, err := h.get("https", time.Now(), false) - if err != nil || body != "hello 1 certificate" { - t.Fatalf("%q %v", body, err) - } - if row := h.lastRow(); row["mode"] != "splice" || row["decision"] != "probe_failed" { - t.Fatalf("%v", row) - } -} - -func TestTheProbeAndLeafCachesStayWithinTheirLimit(t *testing.T) { - limit := cacheLimit - cacheLimit = 3 - defer func() { cacheLimit = limit }() - ca, _ := mintCA(CAName) - leaves, _ := NewLeaves(ca) - for i := 0; i < 10; i++ { - if _, _, err := leaves.For(fmt.Sprintf("site%d.test", i), nil, true); err != nil { - t.Fatal(err) - } - } - if len(leaves.cache) != 3 || leaves.cache["site9.test|"] == nil { - t.Fatalf("%d leaves kept", len(leaves.cache)) - } - p := &Proxy{Dial: func(string) (net.Conn, error) { return nil, fmt.Errorf("unreachable") }} - for i := 0; i < 10; i++ { - p.probeAt(fmt.Sprintf("192.0.2.%d:443", i), "", "x", goHello(t, "x", nil)) - } - if len(p.probes.cache) != 3 { - t.Fatalf("%d probes kept", len(p.probes.cache)) - } -} - -func TestAConnectionTheProbeLetsThroughGoesToTheServerItProbed(t *testing.T) { - s := site(t, false) - now := float64(time.Now().Unix()) - h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{now}}, true) - h.proxy.Resolve = func(context.Context, string) ([]netip.Addr, error) { - return []netip.Addr{netip.MustParseAddr("127.0.0.2"), netip.MustParseAddr("127.0.0.1")}, nil - } - var mu sync.Mutex - var dialled []string - h.proxy.Dial = func(dst string) (net.Conn, error) { - mu.Lock() - dialled = append(dialled, dst) - mu.Unlock() - return net.DialTimeout("tcp", dst, DialTimeout) - } - if _, _, err := h.get("https", time.Now(), false); err != nil { - t.Fatal(err) - } - row := h.lastRow() - mu.Lock() - defer mu.Unlock() - // the probe tried 127.0.0.2, then reached 127.0.0.1; the connection went straight to 127.0.0.1 - if row["decision"] != "inside" || len(dialled) != 3 || !strings.HasPrefix(dialled[2], "127.0.0.1:") { - t.Fatalf("%v %v", row, dialled) - } - if hit := h.proxy.probes.cache[dialled[0]+"|example.com"]; !hit.unreachable || hit.verified { - t.Fatalf("the address that didn't answer is cached as %+v", hit) - } -} - -func TestASlowProbeHoldsUpNoOtherSite(t *testing.T) { - release := make(chan struct{}) - p := &Proxy{Dial: func(dst string) (net.Conn, error) { - if strings.HasPrefix(dst, "192.0.2.1:") { - <-release - } - return nil, fmt.Errorf("unreachable") - }} - hello := goHello(t, "x", nil) - p.probeAt("192.0.2.2:443", "", "x", hello) - go p.probeAt("192.0.2.1:443", "", "x", hello) - time.Sleep(50 * time.Millisecond) - started := time.Now() - if _, probed := p.probeAt("192.0.2.2:443", "", "x", hello); probed || time.Since(started) > 100*time.Millisecond { - t.Fatalf("a cached site waited %s behind a slow one (probed again: %v)", time.Since(started), probed) - } - close(release) -} - -// standIns resolves example.com to its addresses and dials each to the site unless it's down; both may change -// between requests. -type standIns struct { - mu sync.Mutex - addresses []string - down map[string]bool -} - -func (s *standIns) set(down []string, addresses ...string) { - s.mu.Lock() - defer s.mu.Unlock() - s.addresses, s.down = addresses, map[string]bool{} - for _, a := range down { - s.down[a] = true - } -} - -func newStandIns(h *harness, addresses ...string) *standIns { - s := &standIns{addresses: addresses, down: map[string]bool{}} - h.proxy.Resolve = func(context.Context, string) ([]netip.Addr, error) { - s.mu.Lock() - defer s.mu.Unlock() - var out []netip.Addr - for _, a := range s.addresses { - out = append(out, netip.MustParseAddr(a)) - } - return out, nil - } - h.proxy.Dial = func(dst string) (net.Conn, error) { - host, _, _ := net.SplitHostPort(dst) - s.mu.Lock() - down := s.down[host] - s.mu.Unlock() - if down { - return nil, fmt.Errorf("%s is down", host) - } - return net.DialTimeout("tcp", h.site.Listener.Addr().String(), DialTimeout) - } - return s -} - -func TestAnAddressThatStopsAnsweringAfterItsProbeGivesWayToTheNext(t *testing.T) { - s := site(t, false) - now := float64(time.Now().Unix()) - h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{now}}, true) - addresses := newStandIns(h, "198.51.100.1", "198.51.100.2") - if _, _, err := h.get("https", time.Now(), false); err != nil { - t.Fatal(err) - } - if row := h.lastRow(); !strings.HasPrefix(fmt.Sprint(row["dst"]), "198.51.100.1:") { - t.Fatalf("%v", row) - } - addresses.set([]string{"198.51.100.1"}, "198.51.100.1", "198.51.100.2") - if _, _, err := h.get("https", time.Now(), false); err != nil { - t.Fatal(err) - } - if row := h.lastRow(); !strings.HasPrefix(fmt.Sprint(row["dst"]), "198.51.100.2:") || row["decision"] != "inside" { - t.Fatalf("%v", row) - } -} - -func TestAnAddressThatDidntAnswerLeavesANewAddressToItsOwnProbe(t *testing.T) { - s := site(t, false) - u := float64(time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC).Unix()) - h := newHarness(t, s, fixedUniverse{armed: true, universes: []float64{u}}, true) - addresses := newStandIns(h) - addresses.set([]string{"198.51.100.1"}, "198.51.100.1") - if _, _, err := h.get("https", time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC), false); err == nil { - t.Fatal("reached a site none of whose addresses answer") - } - addresses.set([]string{"198.51.100.1"}, "198.51.100.1", "198.51.100.2") - if _, _, err := h.get("https", time.Date(2090, 6, 1, 0, 0, 0, 0, time.UTC), false); err != nil { - t.Fatal(err) - } - row := h.lastRow() - if !strings.HasPrefix(fmt.Sprint(row["dst"]), "198.51.100.2:") || row["mode"] != "intercept" || row["decision"] != "outside" { - t.Fatalf("the new address wasn't judged by its own certificate: %v", row) - } -} - -func TestAClientHelloWhoseFieldsOverrunItIsRefusedNotPanickedOn(t *testing.T) { - hello := goHello(t, "example.com", nil) - p := 4 + 2 + 32 - p += 1 + int(hello[p]) - p += 2 + int(hello[p])<<8 | int(hello[p+1]) - bad := append([]byte{}, hello...) - bad[p] = 0xff // compression methods' length, past the message's end - bad = bad[:p+3] - if _, _, err := parseHello(bad); err == nil { - t.Fatal("parseHello took it") - } - if _, _, err := helloRecord(bad); err == nil { - t.Fatal("helloRecord took it") - } -} - -func TestAProbeThatPanicsLeavesNoOneWaitingOnIt(t *testing.T) { - p := &Proxy{Dial: func(string) (net.Conn, error) { panic("dial") }} - hello := goHello(t, "x", nil) - probe := func() (panicked bool) { - defer func() { panicked = recover() != nil }() - p.probeAt("192.0.2.9:443", "", "x", hello) - return false - } - if !probe() { - t.Fatal("the probe didn't panic") - } - done := make(chan bool) - go func() { done <- probe() }() - select { - case <-done: - case <-time.After(2 * time.Second): - t.Fatal("a second probe waited on the one that panicked") - } -} - -func TestAnAddressAlreadyFoundWorkingIsTriedBeforeOnesNotYetProbed(t *testing.T) { - p := &Proxy{} - p.probes.cache = map[string]probe{ - "[2001:db8::1]:443|x": {verified: true, at: time.Now()}, - "192.0.2.1:443|x": {unreachable: true, at: time.Now().Add(-2 * ProbeFailedTTL)}, - "192.0.2.3:443|x": {unreachable: true, at: time.Now()}, - } - got := p.preferChecked([]string{"192.0.2.1:443", "192.0.2.2:443", "192.0.2.3:443", "[2001:db8::1]:443"}, "x") - want := []string{"[2001:db8::1]:443", "192.0.2.1:443", "192.0.2.2:443", "192.0.2.3:443"} - if strings.Join(got, " ") != strings.Join(want, " ") { - t.Fatalf("%v", got) - } -} diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/testdata/page-armed.hex b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/testdata/page-armed.hex deleted file mode 100644 index 2aecf27e8..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/testdata/page-armed.hex +++ /dev/null @@ -1 +0,0 @@ -4145434b02000000040000000000000000008ab9359ae51515cd68be7e91e51800000000000000400100000000000000070000000000000002b1a878f27237d300f2052a0100000000f2052a01000000 diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/testdata/page-unarmed.hex b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/testdata/page-unarmed.hex deleted file mode 100644 index 7f6a95f54..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/testdata/page-unarmed.hex +++ /dev/null @@ -1 +0,0 @@ -4145434b02000000020000000000000000000000000000000000000000000000000000000000f03f00000000000000000300000000000000147c4a7fb979c7a100000000000000000000000000000000 diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/upstream.go b/src/agent_env/computers/artifacts/ubuntu-24.04/egress/upstream.go deleted file mode 100644 index ccf32704f..000000000 --- a/src/agent_env/computers/artifacts/ubuntu-24.04/egress/upstream.go +++ /dev/null @@ -1,268 +0,0 @@ -package main - -import ( - "crypto/x509" - "errors" - "fmt" - "net" - "strings" - "sync" - "time" - - utls "github.com/refraction-networking/utls" -) - -// upstream is what a handshake with a site found: the protocol it picked, its leaf's validity and names, and whether -// that leaf verifies against real time and the public roots. -type upstream struct { - // Dst is the address the handshake reached. - Dst string - ALPN string - Verified bool - VerifyErr string - NotBefore time.Time - NotAfter time.Time - SANs []string - Version string - Cipher string - Dropped []uint16 -} - -// handshake is uTLS's handshake with the site at dst, built from the client's own ClientHello, so the site sees the -// client's fingerprint and ALPN offer. Records go full-size from the start and the connection closes without a -// close_notify, as Chromium's BoringSSL does. -func (p *Proxy) handshake(dsts []string, sni, verifyName string, hello []byte) (*utls.UConn, upstream, error) { - var found upstream - record, gone, err := helloRecord(hello) - if err != nil { - return nil, found, err - } - found.Dropped = gone - spec, err := (&utls.Fingerprinter{AllowBluntMimicry: true}).FingerprintClientHello(record) - if err != nil { - return nil, found, fmt.Errorf("fingerprint: %w", err) - } - raw, dst, err := p.dialAny(dsts) - if err != nil { - return nil, found, dialError{err} - } - found.Dst = dst - uc := utls.UClient(raw, &utls.Config{ServerName: sni, InsecureSkipVerify: true, OmitEmptyPsk: true, - DynamicRecordSizingDisabled: true}, utls.HelloCustom) - if err := uc.ApplyPreset(spec); err != nil { - raw.Close() - return nil, found, fmt.Errorf("apply: %w", err) - } - raw.SetDeadline(time.Now().Add(HandshakeTimeout)) - if err := uc.Handshake(); err != nil { - raw.Close() - return nil, found, fmt.Errorf("handshake: %w", err) - } - raw.SetDeadline(time.Time{}) - st := uc.ConnectionState() - found.ALPN, found.Version, found.Cipher = st.NegotiatedProtocol, fmt.Sprintf("%#04x", st.Version), - utls.CipherSuiteName(st.CipherSuite) - if len(st.PeerCertificates) == 0 { - found.VerifyErr = "no certificate" - return uc, found, nil - } - leaf := st.PeerCertificates[0] - found.NotBefore, found.NotAfter = leaf.NotBefore, leaf.NotAfter - for _, name := range leaf.DNSNames { - found.SANs = append(found.SANs, "DNS:"+name) - } - for _, ip := range leaf.IPAddresses { - found.SANs = append(found.SANs, "IP:"+ip.String()) - } - intermediates := x509.NewCertPool() - for _, c := range st.PeerCertificates[1:] { - intermediates.AddCert(c) - } - chains, err := leaf.Verify(x509.VerifyOptions{Roots: p.Roots, Intermediates: intermediates, DNSName: verifyName, - CurrentTime: time.Now()}) - if err != nil { - found.VerifyErr = err.Error() - } else { - found.Verified = true - found.NotBefore, found.NotAfter = chainWindow(chains) - } - return uc, found, nil -} - -// chainWindow is the dates every certificate of every verified chain is valid between: a client building any of -// those chains takes the site only then, so passing it through is safe only inside them. -func chainWindow(chains [][]*x509.Certificate) (time.Time, time.Time) { - var from, until time.Time - for _, chain := range chains { - for _, cert := range chain { - if from.IsZero() || cert.NotBefore.After(from) { - from = cert.NotBefore - } - if until.IsZero() || cert.NotAfter.Before(until) { - until = cert.NotAfter - } - } - } - return from, until -} - -// probe is what a handshake of the proxy's own found of one address of a site: the dates its verified chain shares, -// or that it didn't answer, or that its handshake didn't finish. It's cached per (address, SNI): a finished one for -// ProbeTTL, the others for ProbeFailedTTL, since they may be a moment's trouble. -type probe struct { - from, until time.Time - verified bool - // failed is a handshake that didn't finish; unreachable, an address that didn't answer. - failed, unreachable bool - at time.Time - problem string -} - -func (hit probe) fresh() bool { - if hit.failed || hit.unreachable { - return time.Since(hit.at) < ProbeFailedTTL - } - return time.Since(hit.at) < ProbeTTL -} - -// probes caches each address's probe, up to cacheLimit entries, the stale ones dropped first once it's full. An -// address's concurrent probes wait on the one under way; no lock is held while one is. -type probes struct { - mu sync.Mutex - cache map[string]probe - underway map[string]*underway -} - -type underway struct { - done chan struct{} - hit probe - ok bool -} - -// cacheLimit bounds the probe and leaf caches, so a computer that visits many sites keeps a bounded memory. -var cacheLimit = 4096 - -// dialError is a handshake's that never reached the site. -type dialError struct{ error } - -// probeAt is the probe of dst for sni, cached or new, and whether it probed now. -func (p *Proxy) probeAt(dst, sni, verifyName string, hello []byte) (probe, bool) { - key := dst + "|" + sni - p.probes.mu.Lock() - if p.probes.cache == nil { - p.probes.cache, p.probes.underway = map[string]probe{}, map[string]*underway{} - } - if hit, ok := p.probes.cache[key]; ok && hit.fresh() { - p.probes.mu.Unlock() - return hit, false - } - if running, ok := p.probes.underway[key]; ok { - p.probes.mu.Unlock() - <-running.done - if !running.ok { - return probe{at: time.Now(), failed: true, problem: "the probe it waited on failed"}, false - } - return running.hit, false - } - running := &underway{done: make(chan struct{})} - p.probes.underway[key] = running - p.probes.mu.Unlock() - defer func() { - p.probes.mu.Lock() - if running.ok { - p.store(key, running.hit) - } - delete(p.probes.underway, key) - p.probes.mu.Unlock() - close(running.done) - }() - - hit := probe{at: time.Now()} - uc, found, err := p.handshake([]string{dst}, sni, verifyName, hello) - var unanswered dialError - switch { - case errors.As(err, &unanswered): - hit.unreachable, hit.problem = true, truncate(err.Error(), 200) - case err != nil: - hit.failed, hit.problem = true, truncate(err.Error(), 200) - case !found.Verified: - hit.problem = truncate(found.VerifyErr, 200) - default: - hit.from, hit.until, hit.verified = found.NotBefore, found.NotAfter, true - } - if uc != nil { - uc.NetConn().Close() - } - running.hit, running.ok = hit, true - return hit, true -} - -// store keeps hit for key, dropping the stale entries, then any others, once the cache is past cacheLimit. -func (p *Proxy) store(key string, hit probe) { - p.probes.cache[key] = hit - if len(p.probes.cache) <= cacheLimit { - return - } - for k, v := range p.probes.cache { - if !v.fresh() { - delete(p.probes.cache, k) - } - } - for k := range p.probes.cache { - if len(p.probes.cache) <= cacheLimit { - break - } - if k != key { - delete(p.probes.cache, k) - } - } -} - -// forget drops dst's probe for sni, for an address that stopped answering since. -func (p *Proxy) forget(dst, sni string) { - p.probes.mu.Lock() - delete(p.probes.cache, dst+"|"+sni) - p.probes.mu.Unlock() -} - -// inside is whether every universe lies inside [from + Margin, until - Margin]. -func inside(from, until time.Time, universes []float64) bool { - lo := float64(from.Add(Margin).UnixNano()) / 1e9 - hi := float64(until.Add(-Margin).UnixNano()) / 1e9 - for _, u := range universes { - if u < lo || u > hi { - return false - } - } - return true -} - -// dialAny dials each address in turn until one answers: which, or every one's error. -func (p *Proxy) dialAny(dsts []string) (net.Conn, string, error) { - var problems []string - for _, dst := range dsts { - conn, err := p.dial(dst) - if err == nil { - return conn, dst, nil - } - problems = append(problems, err.Error()) - } - if len(problems) == 0 { - return nil, "", errors.New("no address to dial") - } - return nil, "", errors.New(strings.Join(problems, "; ")) -} - -func (p *Proxy) dial(dst string) (net.Conn, error) { - if p.Dial != nil { - return p.Dial(dst) - } - return net.DialTimeout("tcp", dst, DialTimeout) -} - -func truncate(s string, n int) string { - if len(s) > n { - return s[:n] - } - return s -} diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04/shared_sources.toml b/src/agent_env/computers/artifacts/ubuntu-24.04/shared_sources.toml new file mode 100644 index 000000000..dc9d15a01 --- /dev/null +++ b/src/agent_env/computers/artifacts/ubuntu-24.04/shared_sources.toml @@ -0,0 +1,4 @@ +# What this image's build context takes from agent-env's own sources, at the name its Dockerfile copies it from: one +# copy of each, shared with the other images that take it (utils/docker/context.py). Paths are under src/agent_env. +clock = "utils/faketime_clock" +egress = "utils/egress_proxy" diff --git a/src/agent_env/utils/docker/build.py b/src/agent_env/utils/docker/build.py index 9a521e000..605b3b76c 100644 --- a/src/agent_env/utils/docker/build.py +++ b/src/agent_env/utils/docker/build.py @@ -4,10 +4,13 @@ from __future__ import annotations import subprocess +import tempfile from collections.abc import Mapping from dataclasses import dataclass from pathlib import Path +from agent_env.utils.docker.context import SHARED_SOURCES_FILE, BuildContext, extract + DEFAULT_BUILD_PLATFORM = "linux/amd64" # the remote sandbox VMs DOCKER_INFO_TIMEOUT_SECONDS = 10 @@ -35,7 +38,23 @@ class DockerBuildError(RuntimeError): def build_image(dockerfile: Path, context: Path, tag: str, *, platform: str | None, build_args: Mapping[str, str] | None = None, labels: Mapping[str, str] | None = None) -> None: """Build ``context`` with ``dockerfile`` into the local image ``tag``, for ``platform``, or for this host's - platform when it's None or empty. Raises DockerBuildError, with the build's output, when it fails.""" + platform when it's None or empty. A context that takes from agent-env's sources (``SHARED_SOURCES_FILE``) is built + as its build context sends it, what it takes included. Raises DockerBuildError, with the build's output, when it + fails.""" + if not (context / SHARED_SOURCES_FILE).is_file(): + _build(dockerfile, context, tag, platform, build_args, labels) + return + with tempfile.TemporaryDirectory(prefix="agentenv-context-") as staged: + archive, assembled = Path(staged) / "context.tar.gz", Path(staged) / "context" + BuildContext.of(context, dockerfile).write(archive) + extract(archive, assembled) + inside = dockerfile.is_relative_to(context) + _build(assembled / dockerfile.relative_to(context) if inside else dockerfile, assembled, tag, platform, + build_args, labels) + + +def _build(dockerfile: Path, context: Path, tag: str, platform: str | None, build_args: Mapping[str, str] | None, + labels: Mapping[str, str] | None) -> None: argv = ["docker", "build", *(["--platform", platform] if platform else [])] for name, value in (build_args or {}).items(): argv += ["--build-arg", f"{name}={value}"] diff --git a/src/agent_env/utils/docker/context.py b/src/agent_env/utils/docker/context.py index 2683407d8..2b07a2ca2 100644 --- a/src/agent_env/utils/docker/context.py +++ b/src/agent_env/utils/docker/context.py @@ -10,17 +10,21 @@ import posixpath import stat import tarfile +import tomllib import unicodedata from dataclasses import dataclass from pathlib import Path, PurePosixPath from modal import FilePatternMatcher -from agent_env.utils.paths import LEAVINGS +from agent_env.utils.paths import LEAVINGS, SOURCES, in_sources # Bumped when what source_digest covers changes, so a digest is never compared across meanings. _DIGEST_SCHEME = 1 _READ_CHUNK_BYTES = 1 << 20 +# Names what a context takes from agent-env's own sources, and at which name: the images agent-env ships share sources +# (``utils/faketime_clock``, ``utils/egress_proxy``) this way and still build from one self-contained context. +SHARED_SOURCES_FILE = "shared_sources.toml" @dataclass(frozen=True) @@ -40,8 +44,11 @@ def content(self) -> str | None: @dataclass(frozen=True) class BuildContext: """The files ``docker build`` sends from a folder: all but what its ignore file excludes, the Dockerfile and the - ignore file always kept, and what the OS or Python leaves behind always dropped. A link is sent as a link, never - followed, as docker sends it. Modes are git's two: 0755 when the owner may execute, else 0644.""" + ignore file always kept, and what the OS or Python leaves behind always dropped, and what its + ``shared_sources.toml`` takes from agent-env's own sources, each at the name it gives, in place of the file itself, + which says how to make the context and isn't sent: a context written out and unpacked builds as it is. A link is + sent as a link, never followed, as docker sends it. Modes are git's two: 0755 when the owner may execute, else + 0644.""" dockerfile: str | None # its path in the context, None when it's outside it entries: tuple[_Entry, ...] @@ -63,6 +70,7 @@ def of(cls, root: Path, dockerfile: Path | None = None) -> BuildContext: matcher = ignore_matcher(ignores.read_text(encoding="utf-8")) if ignores else None walk = _Walk(top, matcher, kept) walk.run() + walk.take(top / SHARED_SOURCES_FILE) if walk.problems: raise ValueError(f"build context {root}: " + "; ".join(walk.problems)) return cls(dockerfile=named, entries=walk.entries()) @@ -100,6 +108,15 @@ def write(self, out: Path) -> None: raise RuntimeError(f"{entry.name} changed while its build context was being written") +def shared_files(folder: Path) -> dict[str, Path]: + """Each file ``folder``'s ``shared_sources.toml`` takes from agent-env's sources, by its name in the build context, + the ignore file aside: what a build of the folder is made from beyond its own files. What it can't take is left for + ``BuildContext.of`` to refuse.""" + walk = _Walk(Path(os.path.realpath(folder)), None, set()) + walk.take(walk.top / SHARED_SOURCES_FILE) + return {entry.name: entry.path for entry in walk.entries() if entry.path is not None} + + def extract(archive: Path, out: Path) -> None: """Unpack a build context's tar.gz into the folder ``out``: its files, folders, links and hard links. A link that leads out of ``out`` is left out, as nothing there is the build's. Raises ValueError on anything else, which no @@ -120,8 +137,8 @@ def __init__(self, top: Path, matcher: FilePatternMatcher | None, kept: set[str] self.names: set[str] = set() self.problems: list[str] = [] - def run(self) -> None: - pending = [(self.top, ())] + def run(self, start: Path | None = None, parts: tuple[str, ...] = ()) -> None: + pending = [(start or self.top, parts)] while pending: folder, parts = pending.pop() try: @@ -131,7 +148,7 @@ def run(self) -> None: self.problems.append(f"{_show(parts)}: {e.strerror or e}") continue for child in children: - if child.name in LEAVINGS: + if child.name in LEAVINGS or (not parts and child.name == SHARED_SOURCES_FILE): continue child_parts = (*parts, unicodedata.normalize("NFC", child.name)) name = "/".join(child_parts) @@ -151,6 +168,32 @@ def run(self) -> None: else: self.problems.append(f"{name}: neither a regular file, a folder nor a link") + def take(self, declared: Path) -> None: + """What ``declared`` (``SHARED_SOURCES_FILE``), when the folder has one, takes from agent-env's sources: each + folder or file at the name it gives, which the folder mustn't have itself.""" + if not declared.is_file(): + return + try: + taken = tomllib.loads(declared.read_text(encoding="utf-8")) + except (OSError, UnicodeDecodeError, tomllib.TOMLDecodeError) as e: + self.problems.append(f"{SHARED_SOURCES_FILE}: {e}") + return + for name, source in sorted(taken.items()): + path = Path(os.path.realpath(os.path.join(SOURCES, source))) if isinstance(source, str) else None + excluded = name not in self.kept and self._excluded(name) + if path is None or not in_sources(path) or not path.exists(): + self.problems.append(f"{SHARED_SOURCES_FILE}: {name} = {source!r} names nothing in agent-env's sources") + elif name in self.names: + self.problems.append(f"{name}: the folder has it and {SHARED_SOURCES_FILE} takes it too; keep one") + elif path.is_dir(): + if (excluded and self._prunable(name)) or not self._claim(name): + continue + if not excluded: + self.folders.add(name) + self.run(path, (name,)) + elif not excluded: + self._file(name, path) + def entries(self) -> tuple[_Entry, ...]: folders = set(self.folders) for name in self.found: diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/README.md b/src/agent_env/utils/egress_proxy/README.md similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/README.md rename to src/agent_env/utils/egress_proxy/README.md diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/agentenv-egress.sh b/src/agent_env/utils/egress_proxy/agentenv-egress.sh similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/agentenv-egress.sh rename to src/agent_env/utils/egress_proxy/agentenv-egress.sh diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/boot.go b/src/agent_env/utils/egress_proxy/boot.go similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/boot.go rename to src/agent_env/utils/egress_proxy/boot.go diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/boot_test.go b/src/agent_env/utils/egress_proxy/boot_test.go similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/boot_test.go rename to src/agent_env/utils/egress_proxy/boot_test.go diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/chromium-policy.json b/src/agent_env/utils/egress_proxy/chromium-policy.json similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/chromium-policy.json rename to src/agent_env/utils/egress_proxy/chromium-policy.json diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/clock.go b/src/agent_env/utils/egress_proxy/clock.go similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/clock.go rename to src/agent_env/utils/egress_proxy/clock.go diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/clock_test.go b/src/agent_env/utils/egress_proxy/clock_test.go similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/clock_test.go rename to src/agent_env/utils/egress_proxy/clock_test.go diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/go.mod b/src/agent_env/utils/egress_proxy/go.mod similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/go.mod rename to src/agent_env/utils/egress_proxy/go.mod diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/go.sum b/src/agent_env/utils/egress_proxy/go.sum similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/go.sum rename to src/agent_env/utils/egress_proxy/go.sum diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/hello.go b/src/agent_env/utils/egress_proxy/hello.go similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/hello.go rename to src/agent_env/utils/egress_proxy/hello.go diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/hello_test.go b/src/agent_env/utils/egress_proxy/hello_test.go similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/hello_test.go rename to src/agent_env/utils/egress_proxy/hello_test.go diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/leaves.go b/src/agent_env/utils/egress_proxy/leaves.go similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/leaves.go rename to src/agent_env/utils/egress_proxy/leaves.go diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/main.go b/src/agent_env/utils/egress_proxy/main.go similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/main.go rename to src/agent_env/utils/egress_proxy/main.go diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/main_test.go b/src/agent_env/utils/egress_proxy/main_test.go similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/main_test.go rename to src/agent_env/utils/egress_proxy/main_test.go diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/proxy.go b/src/agent_env/utils/egress_proxy/proxy.go similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/proxy.go rename to src/agent_env/utils/egress_proxy/proxy.go diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/proxy_test.go b/src/agent_env/utils/egress_proxy/proxy_test.go similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/proxy_test.go rename to src/agent_env/utils/egress_proxy/proxy_test.go diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/testdata/page-armed.hex b/src/agent_env/utils/egress_proxy/testdata/page-armed.hex similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/testdata/page-armed.hex rename to src/agent_env/utils/egress_proxy/testdata/page-armed.hex diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/testdata/page-unarmed.hex b/src/agent_env/utils/egress_proxy/testdata/page-unarmed.hex similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/testdata/page-unarmed.hex rename to src/agent_env/utils/egress_proxy/testdata/page-unarmed.hex diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/upstream.go b/src/agent_env/utils/egress_proxy/upstream.go similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/egress/upstream.go rename to src/agent_env/utils/egress_proxy/upstream.go diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/COPYING b/src/agent_env/utils/faketime_clock/COPYING similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/COPYING rename to src/agent_env/utils/faketime_clock/COPYING diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/README.md b/src/agent_env/utils/faketime_clock/README.md similarity index 98% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/README.md rename to src/agent_env/utils/faketime_clock/README.md index fa80cd9a4..8dcbb5ece 100644 --- a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/README.md +++ b/src/agent_env/utils/faketime_clock/README.md @@ -56,7 +56,7 @@ What follows the universe, and what doesn't: Build it on x86_64 or aarch64 Linux with a C toolchain, make, patch and curl: - src/agent_env/computers/artifacts/ubuntu-24.04/clock/build.sh OUT [TARBALL] + src/agent_env/utils/faketime_clock/build.sh OUT [TARBALL] ## The clock page diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/agentenv_clock_page.c b/src/agent_env/utils/faketime_clock/agentenv_clock_page.c similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/agentenv_clock_page.c rename to src/agent_env/utils/faketime_clock/agentenv_clock_page.c diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/agentenv_clock_waits.c b/src/agent_env/utils/faketime_clock/agentenv_clock_waits.c similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/agentenv_clock_waits.c rename to src/agent_env/utils/faketime_clock/agentenv_clock_waits.c diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/build.sh b/src/agent_env/utils/faketime_clock/build.sh similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/build.sh rename to src/agent_env/utils/faketime_clock/build.sh diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/clock-follower b/src/agent_env/utils/faketime_clock/clock-follower similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/clock-follower rename to src/agent_env/utils/faketime_clock/clock-follower diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/clock.conf b/src/agent_env/utils/faketime_clock/clock.conf similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/clock.conf rename to src/agent_env/utils/faketime_clock/clock.conf diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/clockpage.py b/src/agent_env/utils/faketime_clock/clockpage.py similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/clockpage.py rename to src/agent_env/utils/faketime_clock/clockpage.py diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/libfaketime.patch b/src/agent_env/utils/faketime_clock/libfaketime.patch similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/libfaketime.patch rename to src/agent_env/utils/faketime_clock/libfaketime.patch diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/tls_realtime.c b/src/agent_env/utils/faketime_clock/tls_realtime.c similarity index 100% rename from src/agent_env/computers/artifacts/ubuntu-24.04-browser/clock/tls_realtime.c rename to src/agent_env/utils/faketime_clock/tls_realtime.c diff --git a/src/agent_env/utils/paths.py b/src/agent_env/utils/paths.py index 620d89522..72e0b6bc2 100644 --- a/src/agent_env/utils/paths.py +++ b/src/agent_env/utils/paths.py @@ -8,6 +8,14 @@ # What the OS or Python writes into a folder on its own. In an artifact or skill folder, everything # else is content, dot files included. LEAVINGS = frozenset({".DS_Store", "Thumbs.db", "desktop.ini", "__pycache__"}) +# agent-env's own sources, from which a shipped image's ``shared_sources.toml`` takes what several images share +# (``utils/faketime_clock``, ``utils/egress_proxy``) into its build context. +SOURCES = os.path.realpath(os.path.join(os.path.dirname(__file__), os.pardir)) + + +def in_sources(path: str | os.PathLike) -> bool: + """Whether ``path`` resolves to somewhere in agent-env's own sources.""" + return os.path.commonpath([SOURCES, os.path.realpath(path)]) == SOURCES def validate_relative_filename(filename: str) -> None: diff --git a/tst/clock/conftest.py b/tst/clock/conftest.py index bbc799cb9..2f2abea4d 100644 --- a/tst/clock/conftest.py +++ b/tst/clock/conftest.py @@ -1,4 +1,4 @@ -"""The clock tier: agent-env's libfaketime (src/agent_env/computers/artifacts/ubuntu-24.04/clock) built in an Ubuntu 24.04 image with stock +"""The clock tier: agent-env's libfaketime (src/agent_env/utils/faketime_clock) built in an Ubuntu 24.04 image with stock 0.9.12 beside it and the gate programs, each gate run in a container of that image by harness.py. Needs Docker; the image is for the Docker daemon's own arch, and Chrome is the browsers bundle's pinned build for it. Chrome's archive, upstream libfaketime's tarball and OpenSSL 1.1.1w's are fetched once into AGENTENV_TEST_CLOCK_CACHE @@ -18,7 +18,7 @@ HERE = Path(__file__).resolve().parent REPO = HERE.parents[1] -RECIPE = REPO / "src" / "agent_env" / "computers" / "artifacts" / "ubuntu-24.04" / "clock" +RECIPE = REPO / "src" / "agent_env" / "utils" / "faketime_clock" CHROMIUM_APP = REPO / "src" / "agent_env" / "browsers" / "envs" / "chromium" / "chromium.py" IMAGE = "agentenv-clock-gates:test" #: Tries at the downloads and the image build (its apt-get), which fail now and then on a network's hiccup. diff --git a/tst/unit/bundle/ledger_test.py b/tst/unit/bundle/ledger_test.py index 255bf886b..a936a8128 100644 --- a/tst/unit/bundle/ledger_test.py +++ b/tst/unit/bundle/ledger_test.py @@ -494,6 +494,29 @@ def test_a_built_image_is_made_from_every_file_of_its_folder_the_toml_too(bundle assert not rebuilt[agent].unchanged +def test_a_built_image_is_made_from_what_its_shared_sources_toml_takes_too(bundle_dir, tmp_path, monkeypatch): + sources = tmp_path / "sources" + (sources / "utils/clock").mkdir(parents=True) + library = sources / "utils/clock/clock.c" + library.write_text("int now;\n") + for module in ("agent_env.utils.paths", "agent_env.utils.docker.context"): + monkeypatch.setattr(f"{module}.SOURCES", str(sources.resolve())) + monkeypatch.setattr(plan_module, "published_bundle_image", lambda _: None) + (bundle_dir / "agents/solver").mkdir(parents=True) + (bundle_dir / "agents/solver/Dockerfile").write_text("FROM scratch\nCOPY clock/ /clock/\n") + (bundle_dir / "agents/solver/shared_sources.toml").write_text('clock = "utils/clock"\n') + (bundle_dir / "tasks/t.json").write_text( + _steps({"id": "agent", "type": "deploy_agent", "env_ids": ["tickets"], "a2a_agent_id": "solver"})) + image = f"{ROOT}/solver__agent_image" + assert _run(bundle_dir)[image].reasons == ("new",) + + library.write_text("int later;\n") + changed = _run(bundle_dir) + + assert changed[image].reasons == ("files changed: clock/clock.c",) + assert _run(bundle_dir)[image].unchanged + + def test_a_built_images_permission_bits_are_inputs_and_a_file_artifacts_arent(bundle_dir): (bundle_dir / "agents/solver").mkdir(parents=True) (bundle_dir / "agents/solver/Dockerfile").write_text("FROM scratch\nCOPY run.sh /\n") diff --git a/tst/unit/bundle/shipped_computers_test.py b/tst/unit/bundle/shipped_computers_test.py index 7cec4bc5b..7881af8a2 100644 --- a/tst/unit/bundle/shipped_computers_test.py +++ b/tst/unit/bundle/shipped_computers_test.py @@ -2,8 +2,10 @@ Dockerfile, or once a release has published it, the published image, read back from its registry; and the start script falls back to the defaults artifact.toml declares.""" +import io import json import re +import tarfile import tomllib from pathlib import Path @@ -17,6 +19,7 @@ from agent_env.bundle.materialize import materialize from agent_env.bundle.plan import AliasUse, plan_bundle from agent_env.bundle.resolve import resolve_bundle +from agent_env.config import get_config from agent_env.entity.refs import EntityKind from agent_env.env.published_images import COMPUTER_IMAGES from tst.unit.bundle._support import layout, local_builds @@ -45,13 +48,14 @@ def test_a_task_naming_ubuntu_24_04_writes_the_shipped_base_over_its_folders_ima assert (computer.arch, computer.os.version, computer.session.user) == (["amd64", "arm64"], "24.04", "user") assert image.id == f"{UBUNTU}__forms_container_image" and image.dockerfile_path == "Dockerfile" built = next(call["build"] for call in builds if call["build"][2] == image.image_name) - assert built[1] == ["Dockerfile", "artifact.toml", *(f"clock/{name}" for name in ( - "COPYING", "README.md", "agentenv_clock_page.c", "agentenv_clock_waits.c", "build.sh", "clock-follower", - "clock.conf", "clockpage.py", "libfaketime.patch", "tls_realtime.c")), *(f"egress/{name}" for name in ( - "README.md", "agentenv-egress.sh", "boot.go", "boot_test.go", "chromium-policy.json", "clock.go", - "clock_test.go", "go.mod", "go.sum", "hello.go", "hello_test.go", "leaves.go", "main.go", "main_test.go", "proxy.go", - "proxy_test.go", "testdata/page-armed.hex", "testdata/page-unarmed.hex", "upstream.go")), "root.conf", - "root/init", "root/install.sh", "start-desktop", "supervisord.conf", "xfwm4.xml"] + assert built[1] == ["Dockerfile", "artifact.toml", "root.conf", "root/init", "root/install.sh", + "shared_sources.toml", "start-desktop", "supervisord.conf", "xfwm4.xml"] + stored = get_config().get_object_store_at(image.build_context_object_url).get(image.build_context_object_url) + with tarfile.open(fileobj=io.BytesIO(stored), mode="r:gz") as context: + sent = set(context.getnames()) + assert {f"clock/{name}" for name in ("build.sh", "clock-follower", "clock.conf", "clockpage.py", "libfaketime.patch")} <= sent + assert {"egress/main.go", "egress/go.mod", "egress/testdata/page-armed.hex", "root/init"} <= sent + assert "shared_sources.toml" not in sent assert computer.metadata["validated_images"]["container"]["arm64"]["status"] == "uninspected" diff --git a/tst/unit/env/envs/test_computer_browser_clock_base.py b/tst/unit/env/envs/test_computer_browser_clock_base.py index a663ba1ca..e9a9c7afa 100644 --- a/tst/unit/env/envs/test_computer_browser_clock_base.py +++ b/tst/unit/env/envs/test_computer_browser_clock_base.py @@ -1,5 +1,6 @@ -"""The ubuntu-24.04-browser base on the universe clock: its copies of the clock library, the egress proxy, the root -start and root's supervisord are ubuntu-24.04's, byte for byte, so a change to any lands in both; it starts as root, +"""The ubuntu-24.04-browser base on the universe clock: through its shared_sources.toml it takes the clock library and +the egress proxy from utils, as ubuntu-24.04 does, and ubuntu-24.04's root start and root's supervisord, so a change +to any lands in both; it starts as root, its init the root start; the egress proxy listens where the variable its site proxies read names, which the base sets to an address the stand-in site proxy doesn't hold; the stand-in keeps real time; and X has no screensaver or DPMS, whose timeouts would run on the universe's clock.""" @@ -17,15 +18,11 @@ import agent_env from agent_env.env.envs.computer.utils import site_proxy +from tst.util.build_context import sent ARTIFACTS = Path(agent_env.__file__).parent / "computers" / "artifacts" DESKTOP, BROWSER = ARTIFACTS / "ubuntu-24.04", ARTIFACTS / "ubuntu-24.04-browser" - - -def _files(folder: Path) -> dict[str, bytes]: - """The folder's files by path, but Python's caches, which importing a module there leaves.""" - return {str(path.relative_to(folder)): path.read_bytes() for path in sorted(folder.rglob("*")) - if path.is_file() and "__pycache__" not in path.parts} +SOURCES = Path(agent_env.__file__).parent def _stand_in(): @@ -44,22 +41,20 @@ def _env() -> dict[str, str]: def _conf(name: str) -> configparser.ConfigParser: conf = configparser.ConfigParser(interpolation=None) - conf.read_string((BROWSER / name).read_text()) + conf.read_string(sent(BROWSER, name).read_text()) return conf -@pytest.mark.parametrize("part", ["clock", "egress", "root"]) -def test_the_browser_bases_copy_is_the_desktops_byte_for_byte(part): - desktop, browser = _files(DESKTOP / part), _files(BROWSER / part) - - assert sorted(browser) == sorted(desktop), f"{part}/ differs in its files: copy ubuntu-24.04/{part}/ over it" - assert [name for name in desktop if browser[name] != desktop[name]] == [], ( - f"{part}/ differs from ubuntu-24.04's: copy ubuntu-24.04/{part}/ over it") - +@pytest.mark.parametrize(("base", "part", "shared"), [ + (DESKTOP, "clock", SOURCES / "utils" / "faketime_clock"), (DESKTOP, "egress", SOURCES / "utils" / "egress_proxy"), + (BROWSER, "clock", SOURCES / "utils" / "faketime_clock"), (BROWSER, "egress", SOURCES / "utils" / "egress_proxy"), + (BROWSER, "root", DESKTOP / "root"), (BROWSER, "root.conf", DESKTOP / "root.conf")]) +def test_what_the_bases_share_is_taken_from_its_one_copy(base, part, shared): + taken = tomllib.loads((base / "shared_sources.toml").read_text()) -def test_the_browser_bases_root_supervisord_is_the_desktops_byte_for_byte(): - assert (BROWSER / "root.conf").read_bytes() == (DESKTOP / "root.conf").read_bytes(), ( - "root.conf differs from ubuntu-24.04's: copy ubuntu-24.04/root.conf over it") + assert not (base / part).exists(), (f"{base.name}/{part} is a copy: take it in {base.name}/shared_sources.toml " + "instead") + assert (SOURCES / taken[part]).resolve() == shared.resolve() def test_it_starts_as_root_its_init_the_root_start_whose_supervisord_runs_the_session_as_the_session_user(): @@ -74,7 +69,7 @@ def test_it_starts_as_root_its_init_the_root_start_whose_supervisord_runs_the_se def test_the_egress_proxy_listens_where_the_variable_its_site_proxies_read_names(): - named = re.search(r'ProxyEnv\s*=\s*"([^"]+)"', (BROWSER / "egress" / "main.go").read_text()).group(1) + named = re.search(r'ProxyEnv\s*=\s*"([^"]+)"', sent(BROWSER, "egress/main.go").read_text()).group(1) assert named == site_proxy.EGRESS_PROXY_ENV and named in _env() assert shlex.split(_conf("root.conf")["program:egress"]["command"]) == [ diff --git a/tst/unit/env/envs/test_computer_clock_base.py b/tst/unit/env/envs/test_computer_clock_base.py index b93915891..5e750839b 100644 --- a/tst/unit/env/envs/test_computer_clock_base.py +++ b/tst/unit/env/envs/test_computer_clock_base.py @@ -12,6 +12,7 @@ import pytest import agent_env +from tst.util.build_context import sent from agentenv_protocol.computer import clock COMPUTERS = Path(agent_env.__file__).parent / "computers" @@ -26,12 +27,12 @@ def _folder(base: str) -> Path: def _settings(base: str) -> dict[str, str]: - return dict(line.split("=", 1) for line in (_folder(base) / "clock" / "clock.conf").read_text().splitlines() + return dict(line.split("=", 1) for line in sent(_folder(base), "clock/clock.conf").read_text().splitlines() if "=" in line and not line.startswith("#")) def _page_writer(base: str): - spec = importlib.util.spec_from_file_location("clockpage_defaults", _folder(base) / "clock" / "clockpage.py") + spec = importlib.util.spec_from_file_location("clockpage_defaults", sent(_folder(base), "clock/clockpage.py")) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module @@ -41,7 +42,7 @@ def _page_writer(base: str): def test_the_bases_dockerfile_installs_the_follower_and_makes_its_user_and_config_folder_as_the_protocol_names_them( base): dockerfile = (_folder(base) / "Dockerfile").read_text() - install = (_folder(base) / "root" / "install.sh").read_text() + install = sent(_folder(base), "root/install.sh").read_text() config_folder = PurePosixPath(clock.CONFIG).parent assert f"clock/clock-follower {PurePosixPath(clock.FOLLOWER).parent}/\n" in dockerfile @@ -54,8 +55,8 @@ def test_the_bases_dockerfile_installs_the_follower_and_makes_its_user_and_confi @base def test_roots_supervisord_runs_the_follower_as_its_user_and_init_makes_its_status_folder_and_page_its_own(base): root = configparser.ConfigParser(interpolation=None) - root.read_string((_folder(base) / "root.conf").read_text()) - init = (_folder(base) / "root" / "init").read_text() + root.read_string(sent(_folder(base), "root.conf").read_text()) + init = sent(_folder(base), "root/init").read_text() assert (root["program:clock"]["command"], root["program:clock"]["user"]) == ( f"/usr/bin/python3 -I {clock.FOLLOWER}", USER) diff --git a/tst/unit/env/envs/test_computer_clock_follower.py b/tst/unit/env/envs/test_computer_clock_follower.py index 367a7af41..9415e2a8b 100644 --- a/tst/unit/env/envs/test_computer_clock_follower.py +++ b/tst/unit/env/envs/test_computer_clock_follower.py @@ -21,7 +21,7 @@ import agent_env from agentenv_protocol.computer import clock as protocol_clock -CLOCK = Path(agent_env.__file__).parent / "computers" / "artifacts" / "ubuntu-24.04" / "clock" +CLOCK = Path(agent_env.__file__).parent / "utils" / "faketime_clock" FOLLOWER = CLOCK / "clock-follower" diff --git a/tst/unit/env/envs/test_computer_egress_base.py b/tst/unit/env/envs/test_computer_egress_base.py index db09631f6..3d102165d 100644 --- a/tst/unit/env/envs/test_computer_egress_base.py +++ b/tst/unit/env/envs/test_computer_egress_base.py @@ -18,9 +18,10 @@ import agent_env from agent_env.env.envs.computer.utils import site_proxy, sites from agent_env.providers.sandbox_providers.local_docker.provider import LOCAL_TRUST_ENV +from tst.util.build_context import sent BASE = Path(agent_env.__file__).parent / "computers" / "artifacts" / "ubuntu-24.04" -EGRESS = BASE / "egress" +EGRESS = sent(BASE, "egress/main.go").parent def _go_date(name: str) -> datetime: @@ -80,7 +81,7 @@ def test_the_session_goes_out_through_the_address_the_proxy_listens_on(): def test_the_pages_the_proxy_is_tested_on_are_the_ones_the_followers_writer_writes(): - spec = importlib.util.spec_from_file_location("clockpage_written", BASE / "clock" / "clockpage.py") + spec = importlib.util.spec_from_file_location("clockpage_written", sent(BASE, "clock/clockpage.py")) page = importlib.util.module_from_spec(spec) spec.loader.exec_module(page) golden = {name: bytes.fromhex((EGRESS / "testdata" / f"page-{name}.hex").read_text().strip()) diff --git a/tst/unit/test_infra_build_script.py b/tst/unit/test_infra_build_script.py index 9b61486e9..6b34b9a76 100644 --- a/tst/unit/test_infra_build_script.py +++ b/tst/unit/test_infra_build_script.py @@ -196,6 +196,29 @@ def test_build_builds_an_image_for_its_platform_under_its_tag_with_its_source_on assert runs[0] == runs[1] and waits == [script._RETRY_WAITS_SECONDS[0]] +def test_build_builds_a_base_with_a_shared_sources_toml_from_a_context_holding_what_it_takes( + script, monkeypatch, workflow): + built = [] + + def streamed(command): + context = Path(command[-1]) + built.append((Path(command[command.index("-f") + 1]).relative_to(context).as_posix(), + {path.relative_to(context).as_posix() for path in context.rglob("*") if path.is_file()})) + return subprocess.CompletedProcess(command, 0, "#1 DONE\n", "") + + monkeypatch.setattr(script, "_streamed", streamed) + monkeypatch.chdir(REPO) + base = "src/agent_env/computers/artifacts/ubuntu-24.04" + + assert _run(script, monkeypatch, "build", _gateway(name="agentenv-ubuntu-24.04", context=base, + dockerfile=f"{base}/Dockerfile", + repository=f"{REGISTRY}/agentenv-ubuntu-24.04"), "amd64") == 0 + + [(dockerfile, files)] = built + assert dockerfile == "Dockerfile" + assert {"clock/build.sh", "egress/main.go", "root/init", "shared_sources.toml"} <= files + + def test_build_makes_a_disk_form_of_its_bases_image_of_the_arch_with_that_images_config_the_tools_and_its_size( script, monkeypatch, workflow): runs, _ = _attempts(script, monkeypatch, [("pulled\n", 0), ("#1 DONE\n", 0)]) diff --git a/tst/unit/test_published_image.py b/tst/unit/test_published_image.py index 49fc7eaf1..4067ebd68 100644 --- a/tst/unit/test_published_image.py +++ b/tst/unit/test_published_image.py @@ -23,7 +23,8 @@ def docker(monkeypatch): def run(argv, **kwargs): state["calls"].append(argv[1:]) - return subprocess.CompletedProcess(argv, 1 if argv[1] in state["fails"] else 0, "", state["fails"].get(argv[1], "")) + failed = state["fails"].get(argv[1], "") + return subprocess.CompletedProcess(argv, 1 if argv[1] in state["fails"] else 0, failed, failed) monkeypatch.setattr(published_image.subprocess, "run", run) monkeypatch.setattr(published_image, "published_pinned", lambda image: state["served"]) @@ -86,5 +87,5 @@ def test_a_failed_build_raises_with_dockers_output(docker): docker["served"] = None docker["fails"]["build"] = "no such file: Dockerfile" - with pytest.raises(RuntimeError, match="docker build .* failed: no such file: Dockerfile"): + with pytest.raises(RuntimeError, match=r"docker build of env-gateway failed \(exit 1\):\nno such file: Dockerfile"): _make() diff --git a/tst/unit/utils/docker/build_test.py b/tst/unit/utils/docker/build_test.py index 929b94a71..236215726 100644 --- a/tst/unit/utils/docker/build_test.py +++ b/tst/unit/utils/docker/build_test.py @@ -76,6 +76,25 @@ def run(argv, **_): build_image(Path("Dockerfile"), Path("."), "my-image", platform=None) +def test_a_folder_with_a_shared_sources_toml_is_built_from_a_context_holding_what_it_takes(monkeypatch, tmp_path): + (tmp_path / "Dockerfile").write_text("FROM scratch\nCOPY clock/ /clock/\n") + (tmp_path / "shared_sources.toml").write_text('clock = "utils/faketime_clock"\n') + built = [] + + def run(argv, **kwargs): + context = Path(argv[-1]) + built.append((argv[argv.index("-f") + 1], context, {p.relative_to(context).as_posix() for p in context.rglob("*")})) + return SimpleNamespace(returncode=0, stdout="") + + monkeypatch.setattr(docker_build.subprocess, "run", run) + + build_image(tmp_path / "Dockerfile", tmp_path, "base", platform=None) + + [(dockerfile, context, files)] = built + assert context != tmp_path and dockerfile == str(context / "Dockerfile") + assert {"clock/build.sh", "clock/clock-follower"} <= files and "shared_sources.toml" not in files + + def test_importing_it_pulls_neither_cli_nor_providers(): code = ( "import sys; import agent_env.utils.docker.build; " diff --git a/tst/unit/utils/docker/context_test.py b/tst/unit/utils/docker/context_test.py index 40b25a547..571fbc74f 100644 --- a/tst/unit/utils/docker/context_test.py +++ b/tst/unit/utils/docker/context_test.py @@ -3,13 +3,18 @@ import gzip import io import os +import shutil import tarfile import unicodedata from pathlib import Path import pytest -from agent_env.utils.docker.context import BuildContext, extract, ignore_file +import agent_env +from agent_env.utils.docker.context import SHARED_SOURCES_FILE, BuildContext, extract, ignore_file +from tst.util.scripts import load_script + +SOURCES = Path(agent_env.__file__).parent def _tree(root: Path, files: dict[str, str], executable: tuple[str, ...] = ()) -> Path: @@ -123,6 +128,68 @@ def test_a_link_the_dockerignore_excludes_is_left_out(tmp_path): assert not any(entry.name.startswith(".venv") for entry in BuildContext.of(root, root / "Dockerfile").entries) +def test_what_shared_sources_toml_takes_from_agent_envs_sources_is_sent_at_the_name_it_gives(tmp_path): + taking = 'clock = "utils/faketime_clock"\nfollower = "utils/faketime_clock/clock-follower"\n' + root = _tree(tmp_path / "ctx", {**SERVER, SHARED_SOURCES_FILE: taking}) + held = _tree(tmp_path / "held", SERVER) + shutil.copytree(SOURCES / "utils" / "faketime_clock", held / "clock") + shutil.copy2(SOURCES / "utils" / "faketime_clock" / "clock-follower", held / "follower") + + taken = BuildContext.of(root, root / "Dockerfile") + members = {m.name: m for m in _members(_written(taken, tmp_path))} + + assert members["clock/build.sh"].isreg() and members["follower"].mode == 0o755 + assert SHARED_SOURCES_FILE not in members + assert taken.source_digest(None) == BuildContext.of(held, held / "Dockerfile").source_digest(None) + + +def test_the_ignore_file_applies_to_what_shared_sources_toml_takes_as_to_the_folders_own(tmp_path): + ignore = "clock\n!clock/clock.conf\n" + root = _tree(tmp_path / "ctx", {**SERVER, ".dockerignore": ignore, + SHARED_SOURCES_FILE: 'clock = "utils/faketime_clock"\n'}) + held = _tree(tmp_path / "held", {**SERVER, ".dockerignore": ignore}) + shutil.copytree(SOURCES / "utils" / "faketime_clock", held / "clock") + + taken = BuildContext.of(root, root / "Dockerfile") + + assert [entry.name for entry in taken.entries if entry.name.startswith("clock")] == ["clock", "clock/clock.conf"] + assert taken.source_digest(None) == BuildContext.of(held, held / "Dockerfile").source_digest(None) + + +@pytest.mark.parametrize(("folder", "taken", "problem"), [ + ({"clock/own.txt": ""}, 'clock = "utils/faketime_clock"\n', + "clock: the folder has it and shared_sources.toml takes it too"), + ({}, 'clock = "../../../etc"\n', "names nothing in agent-env's sources"), + ({}, 'clock = "utils/nowhere"\n', "names nothing in agent-env's sources"), +], ids=["a-name-the-folder-has", "a-path-outside-the-sources", "a-path-that-isnt-there"]) +def test_shared_sources_toml_takes_only_whats_in_agent_envs_sources_at_a_name_the_folder_doesnt_have( + tmp_path, folder, taken, problem): + root = _tree(tmp_path / "ctx", {**SERVER, **folder, SHARED_SOURCES_FILE: taken}) + + with pytest.raises(ValueError, match=problem): + BuildContext.of(root, root / "Dockerfile") + + +@pytest.mark.parametrize("base", ["ubuntu-24.04", "ubuntu-24.04-browser"]) +def test_a_shipped_base_context_written_out_and_unpacked_is_the_same_context(tmp_path, base): + folder = SOURCES / "computers" / "artifacts" / base + sent = BuildContext.of(folder, folder / "Dockerfile") + sent.write(tmp_path / "ctx.tar.gz") + extract(tmp_path / "ctx.tar.gz", tmp_path / "out") + + assert BuildContext.of(tmp_path / "out", tmp_path / "out" / "Dockerfile").source_digest(None) == sent.source_digest(None) + + +@pytest.mark.parametrize("base", ["ubuntu-24.04", "ubuntu-24.04-browser"]) +def test_release_ci_stages_a_shipped_base_with_what_its_build_context_sends(tmp_path, base): + folder, staged = SOURCES / "computers" / "artifacts" / base, tmp_path / base + load_script("infra_build.py")._staged(str(folder), str(staged)) + (staged / SHARED_SOURCES_FILE).unlink() + + assert (BuildContext.of(folder, folder / "Dockerfile").source_digest(None) + == BuildContext.of(staged, staged / "Dockerfile").source_digest(None)) + + def test_a_special_file_is_named(tmp_path): root = _tree(tmp_path / "ctx", SERVER) os.mkfifo(root / "pipe") diff --git a/tst/util/build_context.py b/tst/util/build_context.py new file mode 100644 index 000000000..f105d2f7b --- /dev/null +++ b/tst/util/build_context.py @@ -0,0 +1,14 @@ +"""The file an image's build context sends under a name, its own or one its ``shared_sources.toml`` takes from +agent-env's sources: what a test of a shipped image reads, as its build reads it.""" + +from pathlib import Path + +from agent_env.utils.docker.context import BuildContext + + +def sent(folder: Path, name: str) -> Path: + """The file ``folder``'s build context sends as ``name``.""" + paths = {entry.name: entry.path for entry in BuildContext.of(folder, folder / "Dockerfile").entries} + if paths.get(name) is None: + raise AssertionError(f"{folder.name}'s build context sends no file {name}") + return paths[name] diff --git a/tst/util/published_image.py b/tst/util/published_image.py index bcc495ab7..806e5e9fd 100644 --- a/tst/util/published_image.py +++ b/tst/util/published_image.py @@ -11,6 +11,7 @@ from agent_env.env.bootstrap import PUBLISHED_IMAGES from agent_env.env.published_images import published_pinned from agent_env.store.image_store.registry_api import read_manifest +from agent_env.utils.docker.build import build_image logger = logging.getLogger(__name__) @@ -31,7 +32,7 @@ def pull_published_else_build(name: str, dockerfile: Path, context: Path, tag: s except (RuntimeError, ValueError) as e: logger.warning(f"Pulling {name}'s published image failed, so it's built: {e}") logger.info(f"Building {name} Docker image...") - _docker("build", "--platform", platform, "-f", str(dockerfile), "-t", tag, str(context)) + build_image(dockerfile, context, tag, platform=platform) def _platform_image(pinned: str | None, platform: str) -> str | None: