Skip to content

Commit c641e39

Browse files
authored
fix(sandbox): reject privileged storage with read-only host grants (#5117)
1 parent f3a15f6 commit c641e39

2 files changed

Lines changed: 166 additions & 1 deletion

File tree

‎src/agents/sandbox/sandboxes/docker.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
from .._mount_security import (
3333
_manifest_has_configured_mount_authority,
34+
_mark_mount_validation_error,
3435
redact_mount_error_data,
3536
)
3637
from ..entries import (
@@ -48,6 +49,7 @@
4849
ExecTimeoutError,
4950
ExecTransportError,
5051
ExposedPortUnavailableError,
52+
MountConfigError,
5153
WorkspaceArchiveReadError,
5254
WorkspaceArchiveWriteError,
5355
)
@@ -1864,6 +1866,17 @@ def _docker_mount_path_identity(path: str | PurePath) -> PurePosixPath:
18641866

18651867

18661868
def _validate_docker_path_grants(manifest: Manifest) -> None:
1869+
if any(
1870+
grant.host_path is not None and grant.read_only for grant in manifest.extra_path_grants
1871+
) and (_manifest_requires_fuse(manifest) or _manifest_requires_sys_admin(manifest)):
1872+
# SYS_ADMIN can allow sandbox processes to remount a read-only host bind writable.
1873+
error = MountConfigError(
1874+
message="Docker read-only host_path grants cannot be combined with in-container "
1875+
"storage mounts that require SYS_ADMIN; remove the host grant or use a "
1876+
"storage strategy that does not require container mount privileges"
1877+
)
1878+
_mark_mount_validation_error(error)
1879+
raise error
18671880
root = _docker_mount_path_identity(manifest.root)
18681881
seen_targets: set[str] = set()
18691882
explicit_targets: set[str] = set()

‎tests/sandbox/test_docker.py‎

Lines changed: 153 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
WorkspaceReadNotFoundError,
5757
)
5858
from agents.sandbox.files import EntryKind, FileEntry
59-
from agents.sandbox.manifest import Manifest
59+
from agents.sandbox.manifest import Environment, Manifest
6060
from agents.sandbox.materialization import MaterializedFile
6161
from agents.sandbox.sandboxes.docker import (
6262
DockerSandboxClient,
@@ -2011,6 +2011,158 @@ async def test_docker_create_container_mounts_explicit_host_path(
20112011
]
20122012

20132013

2014+
@pytest.mark.asyncio
2015+
@pytest.mark.parametrize("operation", ["create", "resume"])
2016+
@pytest.mark.parametrize(
2017+
"storage_mount",
2018+
[
2019+
AzureBlobMount(
2020+
account="account",
2021+
container="container",
2022+
mount_strategy=InContainerMountStrategy(pattern=FuseMountPattern()),
2023+
),
2024+
S3Mount(
2025+
bucket="bucket",
2026+
mount_strategy=InContainerMountStrategy(pattern=MountpointMountPattern()),
2027+
),
2028+
S3Mount(
2029+
bucket="bucket", mount_strategy=InContainerMountStrategy(pattern=RcloneMountPattern())
2030+
),
2031+
S3Mount(
2032+
bucket="bucket",
2033+
mount_strategy=InContainerMountStrategy(pattern=RcloneMountPattern(mode="nfs")),
2034+
),
2035+
S3FilesMount(
2036+
file_system_id="fs-1234567890abcdef0",
2037+
mount_strategy=InContainerMountStrategy(pattern=S3FilesMountPattern()),
2038+
),
2039+
],
2040+
ids=["fuse", "mountpoint", "rclone-fuse", "rclone-nfs", "s3-files"],
2041+
)
2042+
async def test_docker_rejects_read_only_host_grant_with_privileged_storage_before_side_effects(
2043+
tmp_path: Path,
2044+
monkeypatch: pytest.MonkeyPatch,
2045+
operation: str,
2046+
storage_mount: Mount,
2047+
) -> None:
2048+
container = _StartedContainer()
2049+
docker_client = _FakeCreateDockerClient(container)
2050+
client = DockerSandboxClient(docker_client=cast(object, docker_client))
2051+
manifest = Manifest(
2052+
entries={"nested": Dir(children={"data": storage_mount})},
2053+
extra_path_grants=(
2054+
SandboxPathGrant(path="/mnt/shared-data", host_path=str(tmp_path), read_only=True),
2055+
),
2056+
)
2057+
if isinstance(storage_mount.mount_strategy.pattern, MountpointMountPattern):
2058+
manifest = manifest.with_in_container_mount_credential_exposure_acknowledged("nested/data")
2059+
else:
2060+
manifest = manifest.with_in_container_mount_broad_credential_exposure_acknowledged(
2061+
"nested/data"
2062+
)
2063+
state = DockerSandboxSessionState(
2064+
manifest=manifest,
2065+
snapshot=NoopSnapshot(id="snapshot"),
2066+
image=DEFAULT_PYTHON_SANDBOX_IMAGE,
2067+
container_id="existing-container",
2068+
workspace_root_ready=True,
2069+
)
2070+
original_session_id = state.session_id
2071+
2072+
def unexpected_provider_access(*args: object, **kwargs: object) -> None:
2073+
pytest.fail("incompatible grants must fail before Docker access or cleanup")
2074+
2075+
async def unexpected_environment_resolution(self: Environment) -> dict[str, str]:
2076+
pytest.fail("incompatible grants must fail before environment resolution")
2077+
2078+
monkeypatch.setattr(client, "image_exists", unexpected_provider_access)
2079+
monkeypatch.setattr(client, "get_container", unexpected_provider_access)
2080+
monkeypatch.setattr(client, "_cleanup_failed_create_resources", unexpected_provider_access)
2081+
monkeypatch.setattr(Environment, "resolve", unexpected_environment_resolution)
2082+
2083+
with pytest.raises(MountConfigError, match="read-only host_path grants.*SYS_ADMIN"):
2084+
if operation == "create":
2085+
await client.create(
2086+
manifest=manifest,
2087+
options=DockerSandboxClientOptions(image=DEFAULT_PYTHON_SANDBOX_IMAGE),
2088+
)
2089+
else:
2090+
await client.resume(state)
2091+
2092+
assert docker_client.containers.calls == []
2093+
assert container.start_calls == 0
2094+
assert state.session_id == original_session_id
2095+
assert state.container_id == "existing-container"
2096+
assert state.workspace_root_ready is True
2097+
2098+
2099+
@pytest.mark.asyncio
2100+
@pytest.mark.parametrize(
2101+
("storage", "explicit_host_path", "read_only"),
2102+
[
2103+
("none", True, True),
2104+
("in-container", True, False),
2105+
("in-container", False, True),
2106+
("docker-volume", True, True),
2107+
],
2108+
ids=["ordinary-read-only-bind", "writable-bind", "path-only-grant", "external-storage"],
2109+
)
2110+
async def test_docker_create_preserves_compatible_host_grants_and_storage(
2111+
tmp_path: Path,
2112+
monkeypatch: pytest.MonkeyPatch,
2113+
storage: str,
2114+
explicit_host_path: bool,
2115+
read_only: bool,
2116+
) -> None:
2117+
container = _StartedContainer()
2118+
docker_client = _FakeCreateDockerClient(container)
2119+
client = DockerSandboxClient(docker_client=cast(object, docker_client))
2120+
manifest = Manifest(
2121+
extra_path_grants=(
2122+
SandboxPathGrant(
2123+
path="/mnt/shared-data",
2124+
host_path=str(tmp_path) if explicit_host_path else None,
2125+
read_only=read_only,
2126+
),
2127+
),
2128+
)
2129+
if storage != "none":
2130+
manifest.entries["data"] = S3Mount(
2131+
bucket="bucket",
2132+
mount_strategy=(
2133+
InContainerMountStrategy(pattern=RcloneMountPattern())
2134+
if storage == "in-container"
2135+
else DockerVolumeMountStrategy(driver="rclone")
2136+
),
2137+
)
2138+
monkeypatch.setattr(client, "image_exists", lambda _image: True)
2139+
2140+
session = await client.create(
2141+
manifest=manifest,
2142+
options=DockerSandboxClientOptions(image=DEFAULT_PYTHON_SANDBOX_IMAGE),
2143+
)
2144+
2145+
assert isinstance(session._inner, DockerSandboxSession)
2146+
assert container.start_calls == 1
2147+
kwargs = docker_client.containers.calls[0]
2148+
mounts = cast(list[dict[str, object]], kwargs.get("mounts", []))
2149+
binds = [mount for mount in mounts if mount["Type"] == "bind"]
2150+
assert binds == (
2151+
[
2152+
{
2153+
"Target": "/mnt/shared-data",
2154+
"Source": str(tmp_path),
2155+
"Type": "bind",
2156+
"ReadOnly": read_only,
2157+
}
2158+
]
2159+
if explicit_host_path
2160+
else []
2161+
)
2162+
assert kwargs.get("cap_add") == (["SYS_ADMIN"] if storage == "in-container" else None)
2163+
assert any(mount["Type"] == "volume" for mount in mounts) == (storage == "docker-volume")
2164+
2165+
20142166
@pytest.mark.asyncio
20152167
async def test_docker_create_container_keeps_path_only_grant_unmounted(
20162168
monkeypatch: pytest.MonkeyPatch,

0 commit comments

Comments
 (0)