Skip to content

Commit b4a3f76

Browse files
luisleo526claude
andcommitted
scratch arena: PF_SCRATCH_ARENA_JAILED=1 requires a sealed memfd arena, refused with the typed arena_backing failure before any store opens (CR-267)
attach_from_environment now reads PF_SCRATCH_ARENA_JAILED: exactly "1" means jailed, unset or exactly "0" keeps today's behaviour, any other value is refused as a malformed profile. A jailed attach whose inherited fd 5 is not a sealed memfd (GROW|SHRINK|SEAL, the mask validate_backing already requires of a sealed memfd) is refused with ReportScratchIoError, reason arena_backing, EINVAL ("scratch arena seals (jailed)"), before the windows, the free bitmap or SIGXFSZ are set up; an existing attachment is re-checked on its retained backend, so a jailed re-attach cannot succeed on a non-sealed arena. Direct attach(fd, bytes) and the unjailed regular_file path are unchanged. Docs: one added paragraph in docs/pages/scratch-tables.md and header comments in scratch_arena.hpp; no sentence is replaced. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> (cherry picked from commit c6eba11)
1 parent 9f268e1 commit b4a3f76

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎docs/pages/scratch-tables.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,17 @@ execution and no TMPDIR fallback. `--spill-dir` is refused in this profile;
162162
`--report-sink-fd 5` is refused in every profile because 5 is the engine's
163163
fixed arena slot. The wrapper must charge the arena as well as the table slab.
164164

165+
The host sets `PF_SCRATCH_ARENA_JAILED=1` for jailed arena attachment. Only
166+
exactly `"1"` enables this requirement; unset or exactly `"0"` preserves
167+
the unjailed backing rules, and every other value (including an empty string)
168+
is refused as a malformed profile. A jailed arena must be a sealed memfd
169+
with at least `F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL`; ordinary files,
170+
sealing-disabled memfds and unsupported seals queries are refused before
171+
arena setup, store opening or execution with typed `ReportScratchIoError`
172+
(`report_sink_failed` / `arena_backing`, `EINVAL`). With no
173+
`PF_SCRATCH_ARENA_FD`, attachment remains a no-op; direct `attach(fd, bytes)`
174+
keeps its unjailed meaning.
175+
165176
The setter is an optional ctypes binding for old libraries in the unjailed
166177
profile. In the jailed profile, an absent setter refuses before execution as
167178
`strategy_library_incompatible` with `{"reason":"setter_missing"}`. The

‎include/pineforge/detail/scratch_arena.hpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ class ScratchFile;
7474
// S_ISREG and exactly the supplied size. On Linux, F_GET_SEALS returning
7575
// exactly SEAL (sealing disabled) or EINVAL selects a regular file;
7676
// GROW|SHRINK|SEAL selects sealed_memfd. All other seal masks are refused.
77+
// Environment attachment with PF_SCRATCH_ARENA_JAILED=1 additionally requires
78+
// sealed_memfd; direct attach(fd, bytes) keeps the backing rules above.
7779
// The wrapper preallocates regular files in a root-owned 0700 directory,
7880
// mode 0600, and passes O_RDWR. New mappings and short preads recheck size.
7981
// The one unrecoverable case is SIGBUS after a concurrent shrink invalidates
@@ -111,6 +113,9 @@ class ScratchArena final {
111113

112114
// No PF_SCRATCH_ARENA_FD => no-op (even if the size variable is present).
113115
// Otherwise require FD=5 and a positive decimal PF_SCRATCH_ARENA_BYTES.
116+
// PF_SCRATCH_ARENA_JAILED: exactly "1" requires sealed_memfd, unset or
117+
// exactly "0" keeps the regular-file path; all other values are refused.
118+
// The jailed requirement also applies to an existing attachment.
114119
// attach also reads PF_SCRATCH_WINDOW_BYTES: a total mapping cap, default
115120
// 32 MiB, rounded down to whole pages; less than one page is refused.
116121
static void attach_from_environment();

‎src/scratch_arena.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,19 @@ void ScratchArena::attach_from_environment() {
377377
arena_error(EINVAL, "scratch arena environment fd", "arena_backing");
378378
const auto bytes = decimal(std::getenv("PF_SCRATCH_ARENA_BYTES"),
379379
"scratch arena environment bytes");
380+
const char* profile = std::getenv("PF_SCRATCH_ARENA_JAILED");
381+
const bool jailed = profile && std::strcmp(profile, "1") == 0;
382+
if (profile && !jailed && std::strcmp(profile, "0") != 0)
383+
arena_error(EINVAL, "scratch arena seals (jailed)", "arena_backing");
384+
if (jailed) {
385+
// A live attachment retains its backing even if fd 5 was rebound.
386+
// Check that backend before attach's idempotent no-op.
387+
const auto backend = singleton_
388+
? singleton_->impl_->counters.backend
389+
: validate_backing(kInheritedFd, bytes);
390+
if (backend != ScratchArenaBackend::sealed_memfd)
391+
arena_error(EINVAL, "scratch arena seals (jailed)", "arena_backing");
392+
}
380393
attach(kInheritedFd, bytes);
381394
}
382395

0 commit comments

Comments
 (0)