Skip to content

Commit 93cb1b3

Browse files
ayaangazaliclaude
andcommitted
fix(sandbox): build exclusive create on the descriptor-relative file ops
#4931 gave UnixLocal a dir_fd plus O_NOFOLLOW file-ops layer and routed the bound-user path through a Python worker running that same module. That is the primitive this change was approximating, so use it instead. write_new is the existing write with O_TRUNC replaced by O_EXCL, which claims the name in the same syscall that creates the file and fails with EEXIST when the name is taken by a file, a directory, or a dangling symlink. The worker exits with a distinct status for an existing target so the caller can report FileExistsError rather than a generic write error. This deletes the machinery the previous approach needed: the shell script, the staging file, the hard link, and the staging cleanup. With no hard link there is nothing to fail on writable object-storage mounts or across owners under fs.protected_hardlinks, and with no staging entry there is nothing for concurrent work to replace. The shared base now documents what it actually guarantees: it rejects an occupied target but is not atomic, and a backend that can claim a name should override it. test_parent_swap_after_validation_cannot_access_outside[patch] injected at session.normalize_path, which the create path no longer calls, so its swap stopped firing. Re-pointed the patch case at the file-ops authorize boundary the create path does traverse, keeping the coverage rather than letting it pass vacuously. Verified separately that a parent symlinked outside the workspace is still refused, with the outside file untouched. Co-Authored-By: Claude Opus 5 <[email protected]>
1 parent fcd5a30 commit 93cb1b3

6 files changed

Lines changed: 135 additions & 326 deletions

File tree

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
)
2626

2727

28+
_EXISTING_TARGET_EXIT_CODE = 13
29+
30+
2831
class _FileOps:
2932
"""Operate on canonical absolute paths already authorized by the owning session."""
3033

@@ -76,6 +79,28 @@ def write(self, path: Path, stream: io.IOBase) -> None:
7679
with out:
7780
shutil.copyfileobj(stream, out)
7881

82+
def write_new(self, path: Path, stream: io.IOBase) -> None:
83+
"""Create a file that must not already exist.
84+
85+
O_EXCL fails with EEXIST when the name is taken by anything, including a directory
86+
or a dangling symlink, and it claims the name in the same syscall that creates the
87+
file, so a concurrent creator either loses the race or keeps its own content.
88+
"""
89+
with self.parent(path, for_write=True, create_parents=True) as (parent_fd, name):
90+
fd = os.open(
91+
name,
92+
os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW,
93+
0o666,
94+
dir_fd=parent_fd,
95+
)
96+
try:
97+
out = os.fdopen(fd, "wb")
98+
except BaseException:
99+
os.close(fd)
100+
raise
101+
with out:
102+
shutil.copyfileobj(stream, out)
103+
79104
def mkdir(self, path: Path, *, parents: bool) -> None:
80105
with self.parent(path, for_write=True, create_parents=parents) as (parent_fd, name):
81106
try:
@@ -167,6 +192,12 @@ def _main() -> None:
167192
path = Path(raw_path)
168193
if operation == "write":
169194
files.write(path, cast(io.IOBase, sys.stdin.buffer))
195+
elif operation == "write_new":
196+
try:
197+
files.write_new(path, cast(io.IOBase, sys.stdin.buffer))
198+
except FileExistsError:
199+
# A distinct status keeps "already exists" separable from a real write failure.
200+
sys.exit(_EXISTING_TARGET_EXIT_CODE)
170201
elif operation == "ls":
171202
print(json.dumps(files.listing(path), ensure_ascii=True))
172203
else:

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

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,48 +1061,49 @@ async def write_new_file(
10611061
*,
10621062
user: str | User | None = None,
10631063
) -> None:
1064+
payload = coerce_write_payload(path=path, data=data)
1065+
# The path is handed over unresolved. The descriptor-relative file ops authorize it
1066+
# without following symlinks and then open the leaf with O_NOFOLLOW, so a symlink at
1067+
# the target name is rejected rather than followed to its target.
10641068
if user is not None:
1065-
await super().write_new_file(path, data, user=user)
1069+
await self._write_new_stream_with_exec(Path(path), payload.stream, user=user)
10661070
return
10671071

1068-
payload = coerce_write_payload(path=path, data=data)
1069-
# Validate the parent with the normal policy so grants and symlinked parents are
1070-
# still enforced, then keep the final component unresolved. normalize_path()
1071-
# resolves symlinks, which would turn a dangling link at the target name into its
1072-
# absent target and let the write land there instead of being rejected.
1073-
requested = Path(path)
1074-
parent_path = self.normalize_path(requested.parent, for_write=True)
1075-
workspace_path = parent_path / requested.name
1076-
staging_path = parent_path / f".apply-patch-create-{uuid.uuid4().hex}"
1077-
# Classify a visible collision before staging, so a target inside a non-writable
1078-
# parent reports the collision rather than a permission failure from the staging
1079-
# write. os.path.lexists does not follow a symlink at the target name.
1080-
if os.path.lexists(workspace_path):
1081-
raise FileExistsError(str(workspace_path))
1082-
10831072
try:
1084-
# Only the link may report a collision. A parent that is a regular file also
1085-
# raises FileExistsError from mkdir, and reporting that as "the target already
1086-
# exists" would send the model to update_file for a target that is absent.
1087-
try:
1088-
parent_path.mkdir(parents=True, exist_ok=True)
1089-
with staging_path.open("wb") as staged:
1090-
shutil.copyfileobj(payload.stream, staged)
1091-
except OSError as e:
1092-
raise WorkspaceArchiveWriteError(path=workspace_path, cause=e) from e
1073+
self._files.write_new(Path(path), payload.stream)
1074+
except FileExistsError:
1075+
raise
1076+
except OSError as e:
1077+
raise WorkspaceArchiveWriteError(path=Path(path), cause=e) from e
10931078

1094-
# os.link claims the name in one step and fails with EEXIST when it is taken
1095-
# by anything, including a directory or a dangling symlink. Linking a complete
1096-
# payload means a failed write never leaves a file holding the name.
1097-
try:
1098-
os.link(staging_path, workspace_path)
1099-
except FileExistsError:
1100-
raise
1101-
except OSError as e:
1102-
raise WorkspaceArchiveWriteError(path=workspace_path, cause=e) from e
1103-
finally:
1104-
with suppress(OSError):
1105-
staging_path.unlink()
1079+
async def _write_new_stream_with_exec(
1080+
self,
1081+
path: Path,
1082+
stream: io.IOBase,
1083+
*,
1084+
user: str | User,
1085+
) -> None:
1086+
payload = stream.read()
1087+
if isinstance(payload, str):
1088+
payload = payload.encode("utf-8")
1089+
elif not isinstance(payload, bytes):
1090+
payload = bytes(payload)
1091+
try:
1092+
result = await self._run_file_operation_as_user(
1093+
"write_new", path, user=user, payload=payload
1094+
)
1095+
except OSError as e:
1096+
raise WorkspaceArchiveWriteError(path=path, cause=e) from e
1097+
if result.returncode == _unix_local_file_ops._EXISTING_TARGET_EXIT_CODE:
1098+
raise FileExistsError(str(path))
1099+
if result.returncode:
1100+
raise WorkspaceArchiveWriteError(
1101+
path=path,
1102+
context={
1103+
"stderr": result.stderr.decode("utf-8", errors="replace"),
1104+
"operation": "write_new",
1105+
},
1106+
)
11061107

11071108
async def _write_stream_with_exec(
11081109
self,
@@ -1133,14 +1134,14 @@ async def _write_stream_with_exec(
11331134

11341135
async def _run_file_operation_as_user(
11351136
self,
1136-
operation: Literal["ls", "write"],
1137+
operation: Literal["ls", "write", "write_new"],
11371138
path: Path,
11381139
*,
11391140
user: str | User,
11401141
payload: bytes = b"",
11411142
) -> subprocess.CompletedProcess[bytes]:
11421143
# Authorization is synchronous and captured for this operation before dispatch.
1143-
path = self._files.authorize(path, for_write=operation == "write")
1144+
path = self._files.authorize(path, for_write=operation != "ls")
11441145
command = self._prepare_exec_command(
11451146
"python3",
11461147
"-I",

‎src/agents/sandbox/session/base_sandbox_session.py‎

Lines changed: 14 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
import asyncio
33
import io
44
import shlex
5-
import uuid
65
from collections.abc import Awaitable, Callable, Mapping, Sequence
7-
from contextlib import suppress
86
from pathlib import Path, PurePath
97
from typing import Literal, NoReturn, TypeVar
108

@@ -151,31 +149,6 @@
151149
fi
152150
done
153151
""".strip()
154-
_EXCLUSIVE_CREATE_EXISTS_CODE = 13
155-
# Classify an already-visible target before any payload is staged. Without this the
156-
# staging write runs first, so a target inside an executable but non-writable parent
157-
# fails on permissions and the caller sees a write error instead of the collision error
158-
# that tells it to use update_file. The atomic claim below still decides real races.
159-
_TARGET_EXISTS_SCRIPT = 'target="$1"\nif [ -e "$target" ] || [ -L "$target" ]; then exit 13; fi\n'
160-
161-
# ``ln`` claims the target name and fails when that name is already taken. Linking a
162-
# fully written staging file means the content is complete before the name exists, so a
163-
# failed or cancelled upload cannot leave a file behind that holds the name. The leading
164-
# test rejects a name held by a directory, which ``ln`` would otherwise treat as a target
165-
# directory and populate; the trailing test only classifies a failure, so "already
166-
# exists" stays separable from any other error without parsing shell-specific stderr.
167-
# ``ln`` is a regular command, unlike ``:``, so its failure still reaches the explicit
168-
# exit mapping on shells where ``:`` is a special builtin. The caller creates the parent,
169-
# so this script never has to create one as a different identity.
170-
_EXCLUSIVE_CREATE_SCRIPT = (
171-
'target="$1"\n'
172-
'source="$2"\n'
173-
'if [ -e "$target" ] || [ -L "$target" ]; then exit 13; fi\n'
174-
'ln "$source" "$target" 2>/dev/null && exit 0\n'
175-
'if [ -e "$target" ] || [ -L "$target" ]; then exit 13; fi\n'
176-
"exit 14\n"
177-
)
178-
179152
_WRITE_ACCESS_CHECK_SCRIPT = (
180153
'target="$1"\n'
181154
'if [ -e "$target" ]; then\n'
@@ -981,69 +954,27 @@ async def write_new_file(
981954
) -> None:
982955
"""Write a file that must not already exist.
983956
984-
The target name is claimed in a single atomic step once the payload is complete,
985-
so a concurrent creator either loses the race or keeps its own content, and a
986-
failed write does not leave a partial file holding the name.
957+
This default checks the target and then writes, so it rejects the ordinary case of
958+
a create aimed at a path that is already occupied and leaves the existing content
959+
alone. It is not atomic: a creator that arrives between the check and the write is
960+
overwritten. A backend that can express an exclusive create should override this
961+
and claim the name in one step; ``UnixLocalSandboxSession`` does.
987962
988963
:param path: Absolute path in the container or path relative to the
989964
workspace root.
990965
:param data: A file-like object positioned at the start of the payload.
991966
:param user: Optional sandbox user to perform the write as.
992-
:raises FileExistsError: If the path already exists, including a dangling symlink.
967+
:raises FileExistsError: If the path already exists.
993968
"""
994-
# Validate the parent so grants and symlinked parents are still enforced, then
995-
# keep the final component unresolved. A path policy that resolves symlinks would
996-
# otherwise turn a dangling link at the target name into its absent target and let
997-
# the create land there instead of being rejected.
998-
requested = Path(path)
999-
parent_path = await self._validate_path_access(requested.parent, for_write=True)
1000-
workspace_path = parent_path / requested.name
1001-
path_arg = sandbox_path_str(workspace_path)
1002-
# A fixed-length staging basename. Deriving it from the destination made the
1003-
# staging name longer than the destination, so a name that fits the filesystem's
1004-
# component limit could still fail to stage.
1005-
staging_path = parent_path / f".apply-patch-create-{uuid.uuid4().hex}"
1006-
staging_arg = sandbox_path_str(staging_path)
1007-
1008-
preflight = await self.exec(
1009-
"sh", "-c", _TARGET_EXISTS_SCRIPT, "sh", path_arg, shell=False, user=user
1010-
)
1011-
if preflight.exit_code == _EXCLUSIVE_CREATE_EXISTS_CODE:
1012-
raise FileExistsError(path_arg)
1013-
969+
workspace_path = await self._validate_path_access(path, for_write=True)
1014970
try:
1015-
# Create the parent as the bound user so a fresh nested path is owned the same
1016-
# way the ordinary write path owned it.
1017-
await self.mkdir(parent_path, parents=True, user=user)
1018-
await self.write(staging_path, data, user=user)
1019-
# -c rather than -lc: this runs on a filesystem-only capability set, so it
1020-
# must not source workspace-writable shell startup files.
1021-
result = await self.exec(
1022-
"sh",
1023-
"-c",
1024-
_EXCLUSIVE_CREATE_SCRIPT,
1025-
"sh",
1026-
path_arg,
1027-
staging_arg,
1028-
shell=False,
1029-
user=user,
1030-
)
1031-
if result.exit_code == _EXCLUSIVE_CREATE_EXISTS_CODE:
1032-
raise FileExistsError(path_arg)
1033-
if not result.ok():
1034-
raise WorkspaceArchiveWriteError(
1035-
path=workspace_path,
1036-
context={
1037-
"command": ["sh", "-c", "<exclusive_create>", path_arg, staging_arg],
1038-
"stdout": result.stdout.decode("utf-8", errors="replace"),
1039-
"stderr": result.stderr.decode("utf-8", errors="replace"),
1040-
},
1041-
)
1042-
finally:
1043-
# The staging entry is an implementation detail, and removing it must not
1044-
# replace the outcome of the create.
1045-
with suppress(Exception):
1046-
await self.rm(staging_path, user=user)
971+
handle = await self.read(workspace_path, user=user)
972+
except (FileNotFoundError, WorkspaceReadNotFoundError):
973+
pass
974+
else:
975+
handle.close()
976+
raise FileExistsError(sandbox_path_str(workspace_path))
977+
await self.write(workspace_path, data, user=user)
1047978

1048979
async def _check_read_with_exec(
1049980
self, path: Path | str, *, user: str | User | None = None

‎tests/sandbox/test_apply_patch.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,9 @@ async def test_apply_patch_create_rejects_an_existing_file() -> None:
436436
class _AlwaysMissingReadApplyPatchSession(ApplyPatchSession):
437437
"""Reports every path as missing while still holding the file.
438438
439-
A create that only probed with read() would be told the path is free and would
440-
overwrite the stored content, so this pins the rejection to the write boundary.
439+
This stands in for a backend that provides an exclusive create. A create that only
440+
probed with read() would be told the path is free and would overwrite the stored
441+
content, so this pins the rejection to the backend primitive rather than to a probe.
441442
"""
442443

443444
async def read(self, path: Path, *, user: str | User | None = None) -> io.BytesIO:

0 commit comments

Comments
 (0)