Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
# `/usr/local/bin/scratchpad-boot.sh` (single cell) inside a gVisor pod running as UID 1000.
FROM python:3.12-slim

# uv is required at RUNTIME: scratchpad_boot shells out to `uv pip install` for missing packages
# via ANTON_UV_PATH=/usr/local/bin/uv, so uv must be present at exactly that path.
# uv is required at RUNTIME: each pad's venv is created and installed into with it, through
# ANTON_UV_PATH=/usr/local/bin/uv, so uv must be present at exactly that path.
COPY --from=ghcr.io/astral-sh/uv:0.6.14 /uv /usr/local/bin/uv

# The version, supplied by the caller because hatch-vcs cannot derive it here:
Expand All @@ -21,7 +21,10 @@ COPY --from=ghcr.io/astral-sh/uv:0.6.14 /uv /usr/local/bin/uv
# downstream.
ARG ANTON_VERSION

# PYTHONNOUSERSITE: the runtime user's home is writable by cell code, so Python
# must not load startup files from its user site-packages.
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONNOUSERSITE=1 \
PYTHONUNBUFFERED=1 \
SETUPTOOLS_SCM_PRETEND_VERSION=${ANTON_VERSION} \
VIRTUAL_ENV=/opt/anton-venv \
Expand All @@ -31,8 +34,9 @@ ENV PYTHONDONTWRITEBYTECODE=1 \
WORKDIR /app
COPY . /app

# Install anton into a venv owned by UID 1000 so the non-root runtime can also `uv pip install`
# missing packages at turn time. `uv sync --locked` installs the exact versions
# Install anton into a root-owned venv. Cell code runs as UID 1000, the same user as the
# agent process, so a venv it could write would let a cell change the code the agent
# loads. Packages a pad installs go into that pad's own venv. `uv sync --locked` installs the exact versions
# from uv.lock; a bare `uv pip install .` re-resolves at build time, which is
# what silently pulled anthropic 1.x (httpx2) into pods while the lockfile
# still pinned 0.x. The scratchpad group includes the cloud SDK in the same
Expand Down Expand Up @@ -60,8 +64,7 @@ RUN test -n "$SETUPTOOLS_SCM_PRETEND_VERSION" \
&& uv venv "$VIRTUAL_ENV" \
&& UV_PROJECT_ENVIRONMENT="$VIRTUAL_ENV" uv sync --locked --no-dev --group scratchpad --no-cache \
&& python -m compileall -q -j 0 -x '/(test|tests|idle_test)/' \
/usr/local/lib/python3.12 "$VIRTUAL_ENV" /app/anton \
&& chown -R 1000:1000 "$VIRTUAL_ENV"
/usr/local/lib/python3.12 "$VIRTUAL_ENV" /app/anton

# scratchpad-boot.sh: the single-cell entrypoint the controller execs (reads code + delimiter on stdin).
RUN printf '#!/bin/sh\nexec python -m anton.core.backends.scratchpad_boot\n' \
Expand Down
10 changes: 10 additions & 0 deletions anton/cloud_turn/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
ask_user_event,
)
from anton.cloud_turn.history_rows import split_turn_into_rows
from anton.cloud_turn.launcher import forbid_same_user_access
from anton.cloud_turn.session import (
build_cloud_chat_session,
build_turn_content,
Expand Down Expand Up @@ -570,6 +571,15 @@ async def _heartbeat() -> None:


def main(argv: list[str] | None = None) -> int:
# Before the request is read: it carries the turn's credentials, and cell
# code runs as this process's user. A turn the launcher hands to the
# resident already has this; one exec'd directly has not, since exec resets it.
if sys.platform == "linux":
try:
forbid_same_user_access()
except OSError as exc:
# The launcher makes the same choice: losing it is not worth losing the turn.
print(f"cloud turn: could not close /proc access: {exc}", file=sys.stderr, flush=True)
with _isolated_protocol_stdout() as emit:
# A private reader over a dup of fd 0, never `sys.stdin` (why: see the
# cloud_turn/stdin.py docstring).
Expand Down
55 changes: 45 additions & 10 deletions docker/image_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,29 +190,63 @@ def check_scratchpad_boot() -> list[str]:


def check_runtime_uv() -> list[str]:
"""scratchpad_boot shells out to `uv pip install` for a package a cell imports.
"""Each pad's venv is created and installed into with uv.

ANTON_UV_PATH points at this exact path, so a uv that moved or lost its
execute bit turns every missing-package recovery into a failed cell.
execute bit breaks every pad package install.
"""
uv = "/usr/local/bin/uv"
if not os.access(uv, os.X_OK):
return [f"runtime uv: {uv} is missing or not executable"]
return []


def check_venv_writable() -> list[str]:
"""That same install writes into the venv as uid 1000.
#: Code the agent process loads. Cell code runs as the same uid, so any of it
#: that uid can write is code a cell can change under the agent.
#: ``/app`` is the turn's working directory, which ``python -m`` puts first on
#: ``sys.path``. ``/usr/local/bin`` holds Python, uv and the entrypoint scripts.
AGENT_CODE_ROOTS = ("/app", "/usr/local/lib/python3.12", "/usr/local/bin")
#: Listing more than this many writable paths adds nothing to the verdict.
MAX_WRITABLE_REPORTED = 10


def writable_paths(roots) -> list[str]:
"""Every file and folder under *roots* this process can write, links skipped."""
found: list[str] = []
for root in roots:
if os.access(root, os.W_OK):
found.append(root)
for folder, dirs, files in os.walk(root):
for name in dirs + files:
path = os.path.join(folder, name)
if not os.path.islink(path) and os.access(path, os.W_OK):
found.append(path)
return found


def check_agent_code_read_only() -> list[str]:
"""The runtime user can write nothing the agent process loads.

The Dockerfile chowns the venv for exactly this. A chown that stops covering
it leaves an image that serves turns normally right up until a cell imports
something that is not installed.
That covers the venv, ``/app`` (anton's source and the turn's working
directory), the standard library and the binaries the pod runs. Pads
install into venvs of their own under the workspace, so nothing at turn
time needs to write here.
"""
venv = os.environ.get("VIRTUAL_ENV", "")
if not venv:
return ["venv: VIRTUAL_ENV is not set in the image"]
if not os.access(venv, os.W_OK):
return [f"venv: {venv} is not writable by uid {os.getuid()}"]
found = writable_paths((venv, *AGENT_CODE_ROOTS))
if not found:
return []
shown = "\n ".join(found[:MAX_WRITABLE_REPORTED])
more = f"\n ... and {len(found) - MAX_WRITABLE_REPORTED} more" if len(found) > MAX_WRITABLE_REPORTED else ""
return [f"agent code: {len(found)} path(s) writable by uid {os.getuid()}:\n {shown}{more}"]


def check_user_site_disabled() -> list[str]:
"""Python loads no startup files from the runtime user's home, which cells can write."""
if not sys.flags.no_user_site:
return ["user site: Python would load packages from the runtime user's home (set PYTHONNOUSERSITE)"]
return []


Expand Down Expand Up @@ -247,7 +281,8 @@ def check_bytecode() -> list[str]:
check_launcher_through_resident,
check_scratchpad_boot,
check_runtime_uv,
check_venv_writable,
check_agent_code_read_only,
check_user_site_disabled,
check_bytecode,
)

Expand Down
65 changes: 65 additions & 0 deletions tests/test_cloud_turn_proc_access.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""A cloud turn closes itself to the pod's other processes before it reads its request.

Cell code runs as the same user as the turn, and the request carries the turn's
credentials. Here the test process stands in for a process a cell left behind:
same user, reading the turn's environment through /proc while the turn waits
for its request line.
"""

from __future__ import annotations

import os
import subprocess
import sys
import time
import uuid
from pathlib import Path

import pytest

from anton.cloud_turn import launcher

pytestmark = pytest.mark.skipif(sys.platform != "linux", reason="/proc and prctl are Linux only")

#: Covers the turn's imports on a slow runner.
CLOSED_WITHIN_SECONDS = 60.0


def _closed_to_this_user(pid: int) -> bool:
try:
Path(f"/proc/{pid}/environ").read_bytes()
except PermissionError:
return True
return False


@pytest.mark.parametrize(
"args",
[
pytest.param(["-m", "anton.cloud_turn"], id="direct"),
pytest.param(
["-I", "-S", launcher.__file__, "--socket", f"anton-test-{uuid.uuid4().hex}"],
id="launcher-without-resident",
),
],
)
def test_the_turn_is_closed_before_its_request_arrives(args, tmp_path):
env = os.environ.copy()
env["ANTON_CLOUD_WORKSPACE_PATH"] = str(tmp_path)
proc = subprocess.Popen(
[sys.executable, *args], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env,
)
try:
deadline = time.monotonic() + CLOSED_WITHIN_SECONDS
closed = False
while time.monotonic() < deadline and proc.poll() is None:
if _closed_to_this_user(proc.pid):
closed = True
break
time.sleep(0.05)
# Still waiting for its request: nothing has been sent yet.
assert proc.poll() is None, proc.stderr.read().decode(errors="replace")[-2000:]
assert closed
finally:
proc.kill()
proc.communicate(timeout=10)
73 changes: 73 additions & 0 deletions tests/test_image_smoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@

import importlib.util
import json
import os
import subprocess
import sys
from pathlib import Path
from types import SimpleNamespace

import pytest

Expand Down Expand Up @@ -180,3 +182,74 @@ def test_bytecode_passes_when_every_module_has_a_pyc(smoke, monkeypatch):
def test_bytecode_names_the_modules_without_a_pyc(smoke, monkeypatch):
monkeypatch.setattr(smoke.os.path, "exists", lambda path: "openai" not in path)
assert smoke.check_bytecode() == ["bytecode: no compiled .pyc for openai"]


def _agent_code_tree(tmp_path, monkeypatch, smoke):
venv = tmp_path / "venv"
(venv / "lib" / "site-packages").mkdir(parents=True)
(venv / "lib" / "site-packages" / "mod.py").write_text("x = 1\n")
monkeypatch.setenv("VIRTUAL_ENV", str(venv))
monkeypatch.setattr(smoke, "AGENT_CODE_ROOTS", ())
return venv


def _read_only(root):
for folder, dirs, files in os.walk(root, topdown=False):
for name in files:
os.chmod(os.path.join(folder, name), 0o444)
os.chmod(folder, 0o555)


def _writable_again(root):
for folder, dirs, files in os.walk(root):
os.chmod(folder, 0o755)
for name in files:
os.chmod(os.path.join(folder, name), 0o644)


@pytest.mark.skipif(os.name != "posix" or os.geteuid() == 0, reason="root can write a read-only file")
def test_agent_code_passes_when_the_runtime_user_can_write_none_of_it(smoke, monkeypatch, tmp_path):
venv = _agent_code_tree(tmp_path, monkeypatch, smoke)
_read_only(venv)
try:
assert smoke.check_agent_code_read_only() == []
finally:
_writable_again(venv)


@pytest.mark.skipif(os.name != "posix" or os.geteuid() == 0, reason="root can write a read-only file")
def test_agent_code_names_a_file_the_runtime_user_can_write(smoke, monkeypatch, tmp_path):
"""One writable module is enough for a cell to change what the agent runs."""
venv = _agent_code_tree(tmp_path, monkeypatch, smoke)
_read_only(venv)
module = venv / "lib" / "site-packages" / "mod.py"
os.chmod(module, 0o644)
try:
(failure,) = smoke.check_agent_code_read_only()
finally:
_writable_again(venv)
assert "1 path(s) writable" in failure
assert str(module) in failure


def test_agent_code_fails_on_the_image_layout_this_change_removed(smoke, monkeypatch, tmp_path):
"""The venv the runtime user owned and could write must fail the build."""
_agent_code_tree(tmp_path, monkeypatch, smoke)
(failure,) = smoke.check_agent_code_read_only()
assert "writable by uid" in failure


def test_agent_code_fails_without_a_venv(smoke, monkeypatch):
monkeypatch.delenv("VIRTUAL_ENV", raising=False)
assert smoke.check_agent_code_read_only() == ["venv: VIRTUAL_ENV is not set in the image"]


def test_user_site_fails_when_python_would_load_it(smoke, monkeypatch):
monkeypatch.setattr(smoke, "sys", SimpleNamespace(flags=SimpleNamespace(no_user_site=0)))
(failure,) = smoke.check_user_site_disabled()
assert "PYTHONNOUSERSITE" in failure


def test_user_site_passes_when_python_skips_it(smoke, monkeypatch):
monkeypatch.setattr(smoke, "sys", SimpleNamespace(flags=SimpleNamespace(no_user_site=1)))
assert smoke.check_user_site_disabled() == []
Loading