Skip to content

Add OCI image support - #191

Open
henrybear327 wants to merge 11 commits into
sysprog21:mainfrom
henrybear327:oci/setup
Open

Add OCI image support#191
henrybear327 wants to merge 11 commits into
sysprog21:mainfrom
henrybear327:oci/setup

Conversation

@henrybear327

@henrybear327 henrybear327 commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

Prior work: #34
Tracking issue: #31

We introduce elfuse-oci, a standalone Go companion binary that owns the OCI image pipeline: pull, unpack, inspect, run, list (alias images), rmi, and prune, backed by a real OCI image-layout store. elfuse itself stays a pure Linux syscall-to-Darwin runtime with no OCI awareness; the two binaries meet only at the existing elfuse --sysroot <rootfs> <entrypoint> <args> launch path.

OCI images are used here strictly as a distribution format for Linux root filesystems — a reproducible replacement for hand-built --sysroot trees. This is not a container runtime. There is no isolation layer: the guest shares the host network identity, PID space, and clock, and unresolved guest paths fall back to host truth. A workload that needs namespaces, cgroups, port mapping, exec into a running container, or a daemon needs a real container runtime, not an ELF personality.

Why a separate Go binary

The OCI ecosystem is a Go ecosystem. Rather than grow the C runtime with an image pipeline, the acquisition/lifecycle half is ~4.4k lines of Go (plus ~7.3k lines of tests) built on github.com/google/go-containerregistry, and validated for on-disk conformance against crane, skopeo, and umoci. Keeping it out of the C tree keeps both sides small and lets each lean on its native tooling.

Design

docs/oci-design.md is the source of truth: the C/Go boundary, the image-layout store and its refs.json pin table, hardened layer application, the run paths, the concurrency/locking model, and an explicit accounting of which OCI features are and are not implemented. docs/usage.md covers the commands; docs/testing.md the offline/CI split; docs/internals.md the host-literal path fallback.

Highlights:

  • Store: spec-shape OCI image-layout other tools can read; refs pinned by digest; an exclusive flock serializes metadata writes so concurrent pulls cannot lose pins.
  • Unpack: os.Root-bounded extraction (no symlink/hardlink escape), correct whiteout/opaque handling, staged temp-dir + atomic rename so readers never see a partial tree.
  • run: resolves Entrypoint/Cmd/Env/User/WorkingDir with env(1)/Docker precedence, guarantees a PATH, resolves symbolic --user against the image's own /etc/passwd+/etc/group (no-follow), injects host /etc/{resolv.conf,hosts,hostname}, then execs elfuse in place so signals and the pid pass through.
  • macOS rootfs: per-digest case-sensitive APFS sparsebundle with per-run clonefile COW; liveness/lifecycle decided by per-digest advisory flocks (attach.lock/run.lock), not pids. --plain-rootfs remains available.
  • Lifecycle GC: rmi/prune use reachability GC (shared blobs survive while any ref reaches them) and never reclaim a cache a live run still holds — the run-lock rides through the exec into elfuse and releases exactly on guest exit.

Try it

make elfuse elfuse-oci

build/elfuse-oci pull alpine:3
build/elfuse-oci inspect alpine:3
build/elfuse-oci list

build/elfuse-oci run alpine:3 /bin/sh -c 'echo hello from elfuse'
build/elfuse-oci run --entrypoint /usr/local/bin/python3 python:3.12 \
  -c 'import json,math; print(json.dumps({"pi":round(math.pi,5),"ok":True}))'

build/elfuse-oci rmi --force alpine:3
build/elfuse-oci prune --cache --all

Images are stored under $ELFUSE_OCI_STORE, or ~/.local/share/elfuse/oci by default.

CI

Split by runner capability

  • a Linux job exercises the pure-Go store paths (pull, unpack, inspect, lifecycle) plus image-layout conformance and cross-tool interop (crane/skopeo/umoci) with no Hypervisor.framework
  • a hosted macOS job builds and tests the darwin sparsebundle code and a run-less lifecycle smoke

Summary by cubic

Adds OCI image support with a new Go CLI, elfuse-oci, an extracted elfuse_launch, and a macOS case‑sensitive APFS sparsebundle rootfs with per‑run COW. Delivers a full pull → inspect/unpack → run → rmi/prune lifecycle with stronger path, symlink, exec, AF_UNIX, and inotify handling.

  • New Features

    • OCI CLI/store: pull, unpack, inspect/list --json, run, rmi, prune on a spec‑shape OCI image‑layout; refs pinned by digest; reachability GC; --platform (default linux/arm64); timed keychain; built on github.com/google/go-containerregistry.
    • Run: resolves Entrypoint/Cmd/Env/User/WorkingDir; adds --user/--workdir/--env/--clear-env; guarantees PATH; creates missing WorkingDir; resolves symbolic users from in‑image passwd/group (no‑follow); injects host /etc/{resolv.conf,hosts,hostname}; forwards INT/TERM/QUIT/HUP; uses -- before guest argv; execs elfuse --sysroot.
    • macOS rootfs: per‑digest case‑sensitive APFS sparsebundles with per‑run clonefile COW; attach/run flock locks; --keep sidecars; --plain-rootfs retained; safe prune/rmi that skip busy caches and detach stale mounts.
    • Filenames/paths: case‑escape codec + case‑exact walk; clamps .. at guest root; guest‑namespace symlink targets; PT_INTERP resolution; /proc/self/{exe,fd} reverse‑map; pathname AF_UNIX translate/shorten and reverse‑map; inotify path translate/name decode; enforce O_NOFOLLOW/O_NONBLOCK; unify /tmp//var/tmp/~/.ccache redirects.
    • C runtime: extracted elfuse_launch; new launch flags (--user/--workdir/--env/--clear-env) with staged initial IDs and absolute Workdir validation.
  • Docs & CI

    • Docs: added docs/oci-design.md; updates to docs/usage.md, docs/testing.md, docs/internals.md.
    • Tests/CI: store conformance and interop (crane/skopeo/umoci); sparsebundle round‑trip; exec checks; end‑to‑end lifecycle; per‑image workloads (python/node/go/jvm/c). Linux runs pure‑Go store paths; hosted macOS builds/tests darwin code; self‑hosted Apple Silicon runs full HVF flows. New hvf-elfuse-setup composite action.

Written for commit 8b632a3. Summary will update on new commits.

Review in cubic

cubic-dev-ai[bot]

This comment was marked as resolved.

jserv

This comment was marked as resolved.

@jserv

jserv commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Commands to try out

build/elfuse oci list
build/elfuse oci pull alpine:3
build/elfuse oci inspect alpine:3
build/elfuse oci list
build/elfuse oci rmi --force e7a1a92a5bfe 
build/elfuse oci list
build/elfuse-container run --entrypoint /usr/local/bin/python3 \\n    python:3.12 -c 'import json,math; print(json.dumps({"pi":round(math.pi,5),"ok":True}))'
build/elfuse oci run alpine:3 /bin/sh -c 'echo hello from elfuse'

Leveraging existing Go-based tools and packages is a great step toward full OCI image support. I agree with this change.

I suggest repositioning the build/elfuse binary as an efficient Linux syscall-to-macOS/Darwin runtime, while implementing build/elfuse-container in Go using OCI-related packages.

In other words, build/elfuse oci should not be considered a valid command. OCI-specific functionality should reside in elfuse-container, not in the elfuse executable.

@henrybear327
henrybear327 force-pushed the oci/setup branch 2 times, most recently from c18f864 to ed17166 Compare July 10, 2026 21:56
henrybear327 added a commit to henrybear327/elfuse that referenced this pull request Jul 11, 2026
Runtime file injection de-symlinked only the /etc directory itself; the
per-file os.WriteFile still followed an image-shipped symlink at
etc/{hostname,hosts,resolv.conf}, letting a malicious image redirect
the write outside the rootfs. Named --user resolution had the same
flaw on the read side: a symlinked etc/passwd or etc/group made
lookupPasswd/lookupGroup read host account files.

Route both through os.OpenRoot (the same containment the layer
unpacker already uses): injection unlinks the existing entry and
recreates it O_EXCL, and passwd/group opens are rootfs-bounded.
Regression tests pin both behaviors.

Reported by cubic review on PR sysprog21#191.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
henrybear327 added a commit to henrybear327/elfuse that referenced this pull request Jul 11, 2026
Mode finalization ran only when an entry carried setuid/setgid/sticky
bits, but the creation modes passed to os.Root.OpenFile and MkdirAll
are masked by the process umask: under e.g. umask 0077 a layer's
0755/0644 entries unpacked as 0700/0600 and were never corrected.

Chmod every created file and directory entry to its exact tar mode
(applyMode), and split the ensure-parent path out (ensureParent) so
finalizing an entry cannot reset the mode of an already-unpacked
parent directory to the 0755 default. Regression test unpacks under
umask 0077 and checks exact modes, including a 0700 parent that a
later child entry must not widen.

Reported by cubic review on PR sysprog21#191.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
henrybear327 added a commit to henrybear327/elfuse that referenced this pull request Jul 11, 2026
Both run paths infer "already unpacked" from the rootfs path existing
(csrun.go and the plain-directory path in commands.go), but unpackImage
created the destination before applying layers and left it behind on
failure. A run after a failed unpack therefore skipped the unpack and
executed against the truncated tree.

Delete the destination on unpack failure. The cleanup applies only
when unpackImage created the directory itself, so an explicit
pre-existing `unpack --rootfs DIR` target is never removed. Regression
test drives a two-layer image whose second layer fails and checks both
the cleanup and the pre-existing-directory guard.

Reported by cubic review on PR sysprog21#191.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
henrybear327 added a commit to henrybear327/elfuse that referenced this pull request Jul 11, 2026
pin/rmi updated refs.json with an unlocked load-modify-save cycle and
a fixed temp filename, so two concurrent pulls (or a pull racing an
rmi) could clobber each other's temp file and drop a just-recorded pin
-- a later unpack/run then reports the image as not pulled even though
its pull succeeded. index.json has the same read-modify-write shape
inside the layout package.

Add an exclusive flock on <store>/.lock held across pin's cycle,
addImage's check-append-pin, and rmi's whole resolve-modify-GC
sequence, and give savePins a unique temp name. A 16-writer
concurrent-pin regression test asserts no entry is lost.

Reported by cubic review on PR sysprog21#191.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
henrybear327 added a commit to henrybear327/elfuse that referenced this pull request Jul 11, 2026
cmdRun treated every s.image failure as "not pulled" and fell into the
auto-pull path, so a corrupt refs.json or unreadable layout triggered a
surprise network pull instead of reporting the store problem.

Introduce an errNotPulled sentinel wrapped by digestFor and
resolvePinnedTarget (user-facing message text is unchanged) and gate
the auto-pull on errors.Is. A regression test pins the two error
kinds apart.

Reported by cubic review on PR sysprog21#191.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
henrybear327 added a commit to henrybear327/elfuse that referenced this pull request Jul 11, 2026
Two GC robustness fixes:

DirEntry.Info() returning ENOENT (a blob reclaimed by a concurrent
rmi/prune between ReadDir and Info) aborted the whole GC pass; skip
the vanished entry instead, matching the IsNotExist tolerance already
used elsewhere in gc.go.

A last-pin rmi committed the pin removal to refs.json before removing
the manifest descriptor from index.json. If descriptor removal then
failed, the image was stranded: no ref resolves to it, the descriptor
keeps every blob live, and prune never removes descriptors. Reorder
the writes so the same failure window leaves a stale pin over a
removed descriptor, which a retried rmi resolves and completes
(RemoveDescriptors is a filter; re-removal is a no-op). Regression
test forces the descriptor write to fail and checks the pin survives
and the retry finishes.

Reported by cubic review on PR sysprog21#191.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
henrybear327 added a commit to henrybear327/elfuse that referenced this pull request Jul 11, 2026
pruneCSBundle ran the crash-recovery sweep -- including an
unconditional hdiutil detach -force of any attached volume -- before
checking whether the digest was still pinned, so a non---all
`prune --cache` could rip the rootfs out from under an active run
(the sweep cannot tell a crashed leftover mount from a live one by
mount state alone).

Two guards fix this. The live[key] pin check now runs before the
sweep, so a plain prune never touches a pinned bundle; a crashed
pinned bundle is recovered by the next run's provision or by --all.
And sweepCSBundle now reports a volume busy instead of detaching when
a run-<pid> clone of a live process remains inside it, protecting the
--all and legacy/unpinned paths too.

The gated darwin round-trip now covers the busy path, and folds in two
review fixes of its own: the orphan clone uses a never-assignable pid
(999999999 > kern.maxpid) instead of a reaped pid that the OS could
reuse mid-test, and a t.Cleanup force-detach keeps failed runs from
leaking an attached volume.

Reported by cubic review on PR sysprog21#191.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
henrybear327 added a commit to henrybear327/elfuse that referenced this pull request Jul 11, 2026
clearDir followed a symlink at the mount-point path, so pre-attach
cleanup of a corrupt or tampered store could empty a directory outside
the OCI cache. Reject a symlinked mount dir with an error instead.

Also drop csMount.imagePath: it was set but never read in production
(every consumer derives <bundle>/rootfs.sparsebundle itself), so the
field was state to maintain with no behavior behind it.

Reported by cubic review on PR sysprog21#191.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
henrybear327 added a commit to henrybear327/elfuse that referenced this pull request Jul 11, 2026
runMainSubprocess read the stdout pipe to EOF before touching the
stderr pipe -- the sequential-read pattern the os/exec docs warn can
deadlock once the unread stream fills its ~64KB buffer. Hand both
streams to exec.Cmd as bytes.Buffers instead, which the package drains
concurrently; less code and no deadlock potential as outputs grow.

Reported by cubic review on PR sysprog21#191.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
henrybear327 added a commit to henrybear327/elfuse that referenced this pull request Jul 11, 2026
The jq expression comparing registry truth against the store pin
returned every manifest matching os/arch, so a manifest list with two
matching entries (several variants, or a future extra descriptor)
produced a multi-line string and failed the equality check on a valid
image. Wrap the selection in first(...) and exclude BuildKit
attestation manifests, mirroring what crane.Pull(WithPlatform)
resolves.

Reported by cubic review on PR sysprog21#191.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
henrybear327 added a commit to henrybear327/elfuse that referenced this pull request Jul 11, 2026
The prune summary presented one number as a uniform on-disk-allocation
figure, but blob bytes come from logical file sizes while cache-dir
bytes come from st_blocks allocation. Say so in the pruneReport doc and
mark the user-facing total approximate rather than pretending to a
single metric.

Reported by cubic review on PR sysprog21#191.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
henrybear327 added a commit to henrybear327/elfuse that referenced this pull request Jul 11, 2026
v1.18.7 fixes an out-of-bounds read in s2.NewDict. The dependency is
indirect (via go-containerregistry) and nothing here imports the s2
package, so there is no reachable exposure -- this is dependency
hygiene while the report is fresh.

Reported by cubic review on PR sysprog21#191.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
henrybear327 added a commit to henrybear327/elfuse that referenced this pull request Jul 11, 2026
Every pre-launch failure path unlinks a FUSE-materialized temporary
ELF (elf_host_temp) before returning; the --env/--clear-env OOM branch
was the lone omission and leaked the temp file on disk. Mirror the
sibling paths.

Reported by cubic review on PR sysprog21#191.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
cubic-dev-ai[bot]

This comment was marked as resolved.

@henrybear327
henrybear327 marked this pull request as draft July 11, 2026 12:21
@henrybear327

This comment was marked as outdated.

jserv

This comment was marked as resolved.

@henrybear327

This comment was marked as outdated.

@henrybear327

Copy link
Copy Markdown
Collaborator Author

unpack fails on any image that ships setuid/setgid files when the host process is unprivileged, which includes stock Debian-based images:

$ elfuse-container unpack --rootfs /tmp/gcc-rootfs gcc:14
elfuse-container: unpack: layer 0: entry "usr/bin/chage": chmodat usr/bin/chage: operation not permitted

gcc:14, ubuntu:24.04, and anything else carrying the shadow suite (chage, su, passwd, ...) cannot be unpacked at all; the alpine/slim images used by the CI smoke tests just happen to contain no setuid entries, so the path is never exercised there. The run cache fill goes through the same applyMode, so those images fail to run, too.

Root cause: applyMode applies the tar header mode verbatim via root.Chmod(name, perm|special). The unpacked tree is owned by the invoking user rather than the tar's root/shadow owners, and an unprivileged chmod that sets setuid/setgid on such files is rejected with EPERM on macOS, so the whole unpack aborts on the first such entry.

Since elfuse remaps uid/gid and does not implement setuid-exec semantics, the special bits carry no meaning at runtime today. I'd suggest degrading gracefully instead of failing the unpack — retry with the plain permission bits, and only for this specific case so genuine chmod failures still surface

Thanks for testing and reporting the bug. I will address the issue by emitting an error message such that we are not silently dropping special bits without the user knowing about it.

@henrybear327

Copy link
Copy Markdown
Collaborator Author

After several major iterations, I am not sure if elfuse-container is a good name, as I was also a bit misled by going down trying to support container execution.

Alternatively, name it elfuse-oci.

Renamed accordingly and updated the PR description.

@henrybear327
henrybear327 force-pushed the oci/setup branch 3 times, most recently from 4dbe1dd to b2484ce Compare July 18, 2026 22:15
@henrybear327

Copy link
Copy Markdown
Collaborator Author

unpack fails on any image that ships setuid/setgid files when the host process is unprivileged, which includes stock Debian-based images:

$ elfuse-container unpack --rootfs /tmp/gcc-rootfs gcc:14
elfuse-container: unpack: layer 0: entry "usr/bin/chage": chmodat usr/bin/chage: operation not permitted

gcc:14, ubuntu:24.04, and anything else carrying the shadow suite (chage, su, passwd, ...) cannot be unpacked at all; the alpine/slim images used by the CI smoke tests just happen to contain no setuid entries, so the path is never exercised there. The run cache fill goes through the same applyMode, so those images fail to run, too.
Root cause: applyMode applies the tar header mode verbatim via root.Chmod(name, perm|special). The unpacked tree is owned by the invoking user rather than the tar's root/shadow owners, and an unprivileged chmod that sets setuid/setgid on such files is rejected with EPERM on macOS, so the whole unpack aborts on the first such entry.
Since elfuse remaps uid/gid and does not implement setuid-exec semantics, the special bits carry no meaning at runtime today. I'd suggest degrading gracefully instead of failing the unpack — retry with the plain permission bits, and only for this specific case so genuine chmod failures still surface

Thanks for testing and reporting the bug. I will address the issue by emitting an error message such that we are not silently dropping special bits without the user knowing about it.

Addressed.

Also added 5 workload images listed in #224 to the CI as a test.

Will make the CI tasks simpler for now, as the Go test currently failed due to it hitting elfuse's fixed thread-table cap, and the python test triggers an unhandled guest CPU exception in elfuse.

henrybear327 added a commit to henrybear327/elfuse that referenced this pull request Jul 18, 2026
Apply a stored image's layers into a rootfs directory, whiteouts and
all, so `unpack` (and later `run`) can materialize a filesystem tree
from the store without any external tool.

Layer application is hardened against hostile archives: extraction is
bounded by os.Root so no entry, symlink, or hardlink escapes the
destination; parent components are Lstat'd and a symlinked or non-dir
intermediate is replaced with a real directory (containerd/Docker
behavior); opaque whiteouts clear through a real directory only, are
order-independent within a layer, and an invalid bare .wh. entry
fails extraction instead of deleting its parent; a directory entry
replaces a lower-layer non-directory. File bodies are bounded by the
tar header size and short bodies are an error, and permissions are
finalized with an explicit chmod so the host umask cannot skew modes.

setuid/setgid/sticky bits are re-applied where the host allows it, and
degrade gracefully where it does not. An unprivileged chmod that sets
setuid/setgid is rejected with EPERM on macOS when the unpacked file's
inherited group is one the invoking user is not in (a new file takes
its parent directory's group under BSD semantics, e.g. wheel under
/tmp, not the tar's root/shadow owner). Since the rootfs is owned by
the invoking user those bits could not be honored at runtime there
anyway, so they are dropped with a warning naming the lost bit rather
than aborting the whole unpack, which is what lets Debian-family
images and their shadow suite (chage, passwd, ...) unpack at all.
Reported at
sysprog21#191 (comment)

Unpack stages into a temp sibling directory and renames into the
final cache path (keyed by manifest digest under <store>/rootfs/), so
a concurrent reader never observes a partial tree and the loser of a
rename race adopts the winner's complete one. A failed unpack removes
only what it created.

Tests drive whiteouts, opaque ordering, parent-symlink replacement,
hardlink identity, exact-size reads, mode preservation, the special-
bit degrade decision, and the staged-rename semantics over synthetic
layer tarballs.
@henrybear327
henrybear327 force-pushed the oci/setup branch 2 times, most recently from 89b2dc4 to 031824e Compare July 18, 2026 23:21
@henrybear327
henrybear327 marked this pull request as draft July 20, 2026 00:11
henrybear327 added a commit to henrybear327/elfuse that referenced this pull request Jul 20, 2026
Apply a stored image's layers into a rootfs directory, whiteouts and
all, so `unpack` (and later `run`) can materialize a filesystem tree
from the store without any external tool.

Layer application is hardened against hostile archives: extraction is
bounded by os.Root so no entry, symlink, or hardlink escapes the
destination; parent components are Lstat'd and a symlinked or non-dir
intermediate is replaced with a real directory (containerd/Docker
behavior); opaque whiteouts clear through a real directory only, are
order-independent within a layer, and an invalid bare .wh. entry
fails extraction instead of deleting its parent; a directory entry
replaces a lower-layer non-directory. File bodies are bounded by the
tar header size and short bodies are an error, and permissions are
finalized with an explicit chmod so the host umask cannot skew modes.

setuid/setgid/sticky bits are re-applied where the host allows it, and
degrade gracefully where it does not. An unprivileged chmod that sets
setuid/setgid is rejected with EPERM on macOS when the unpacked file's
inherited group is one the invoking user is not in (a new file takes
its parent directory's group under BSD semantics, e.g. wheel under
/tmp, not the tar's root/shadow owner). Since the rootfs is owned by
the invoking user those bits could not be honored at runtime there
anyway, so they are dropped with a warning naming the lost bit rather
than aborting the whole unpack, which is what lets Debian-family
images and their shadow suite (chage, passwd, ...) unpack at all.
Reported at
sysprog21#191 (comment)

Unpack stages into a temp sibling directory and renames into the
final cache path (keyed by manifest digest under <store>/rootfs/), so
a concurrent reader never observes a partial tree and the loser of a
rename race adopts the winner's complete one. A failed unpack removes
only what it created.

Tests drive whiteouts, opaque ordering, parent-symlink replacement,
hardlink identity, exact-size reads, mode preservation, the special-
bit degrade decision, and the staged-rename semantics over synthetic
layer tarballs.
cubic-dev-ai[bot]

This comment was marked as resolved.

cubic-dev-ai[bot]

This comment was marked as resolved.

@henrybear327

This comment was marked as outdated.

@jserv jserv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of the OCI pipeline and the C runtime boundary. Three findings are inline below. One more is worth flagging but sits in pre-existing code this PR does not modify, so it cannot be anchored to the diff:

src/syscall/net-absock.c abstract-socket bind slot reservation (pre-existing, adjacent to this PR's changes): absock_register_locked populates a table slot but leaves active=false, and absock_lock is dropped before the actual bind(); the slot is only marked live later in absock_bind_commit. A concurrent guest bind() scans for !active, reuses the same slot, and overwrites its guest_fd/name/fs_path, and the duplicate-name check (absock_lookup_locked, active-only) misses the in-flight name. Two guest fds then alias one slot, so unregister leaks one socket file and its reverse-map fails. Reserving the slot under the lock in register_locked (and clearing it in absock_bind_rollback) closes the window. Since this PR reworks the same file, it may be a good time to fix it.

Comment thread cmd/elfuse-oci/gc.go
// first so index.json never keeps referencing a manifest whose blobs this
// sweep reclaims; a dry run skips that but, rooting here at pins too, still
// reports exactly the blobs a real run would reclaim.
func (s *store) liveBlobDigests() (map[string]bool, error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blob GC can delete blobs a live run still needs. liveBlobDigests roots reachability only at current refs.json pins. resolveImageForUse drops the store lock once it holds the per-digest rootfs run-lock, so a concurrent pull can repin the ref to a new digest; a following prune/rmi blob sweep then removes the old digest's manifest/config/layers while a resolved-but-not-yet-unpacked run is still reading them. The cache sweep already skips busy caches via rootfsCacheBusy (gc.go:367), but this blob reachability pass does not. Also root liveness at digests whose rootfs run-lock is busy, the same way the cache sweep does, and add a test that combines a repull with prune while a run-lock is held.

Comment thread cmd/elfuse-oci/unpack.go
// Ownership is not applied here: elfuse runs as the host user and overrides
// identity at runtime via --user, so the rootfs carries only mode bits.
func unpackImage(img v1.Image, dest string) error {
if _, statErr := os.Lstat(dest); statErr == nil {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A symlink can redirect extraction (and execution) out of the store. os.Lstat(dest) treats any pre-existing entry as a mergeable rootfs and unpackInto then calls os.OpenRoot(dest), which follows a symlinked dest; the run path's os.Stat(rf.rootfs) (commands.go:250) follows it too. A symlink planted at the digest-keyed cache path <store>/rootfs/sha256/<hex> redirects extraction writes, or run --plain-rootfs execution, to an arbitrary directory. For store-managed paths, reject a dest that is not a real directory (i.e. a symlink) before OpenRoot and the cache-exists check; keep symlink-following only for an explicit --rootfs if that is intended.

Comment thread src/syscall/net-absock.c Outdated
# plus the end-to-end OCI run and image-lifecycle checks
# oci-conformance : OCI image-layout conformance + cross-tool interop
# (crane/skopeo/umoci) on Linux
# oci-macos : elfuse-oci darwin build, unit tests, sparsebundle

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oci-macos is ambiguous, as OCI instances built on macOS could also be abbreviated as oci-macos. Clarify.

@sunxiaoguang

Copy link
Copy Markdown
Contributor

Really looking forward to seeing this improvement. We will be able to run artifacts easier this way.

@jserv jserv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebase latest main branch and resolve conflicts.

elfuse_launch owns guest bring-up: guest_bootstrap_prepare, the
FUSE-temp unlink, the sysroot casefold probe,
vCPU creation, GDB init/sync/wait, the run loop, gdb_stub_shutdown,
the shim counter and syscall histogram dumps, and guest_destroy.
main() retains the original CLI argv (proctitle rewriting), option
parsing, sysroot provisioning, the shebang loop, the --gdb x86_64
guard, host cwd, and the heap resource cleanup, and now hands off
through launch_args_t so other launchers (the OCI run helper) can
share one bring-up path.

The launch_args_t envp field generalizes the old hard-coded environ:
NULL keeps the host environment, so main()'s behavior is unchanged.
Bring-up failures unwind through a single fail label instead of
repeating the guest_destroy-plus-unlink tail at every error site.

Ownership of the FUSE-materialized temp ELF moves with the bring-up:
elfuse_launch owns the unlink from the prepare call onward (teardown
and the post-prepare error paths), and main() drops its claim before
handing off, so main's shared goto unwind cannot double-unlink a path
whose ownership has been transferred.

The embedded shim blob include moves along with its only consumer, so
shim_bin has a single object definition site.
An OCI image front end needs to set the guest identity, working
directory, and environment without patching the runtime; the new
flags map onto launch_args_t fields and `elfuse-oci run` drives
exactly this interface.

The --user identity is staged before bring-up (proc_set_initial_ids)
so the auxv AT_UID/AT_GID snapshot taken by build_linux_stack matches
what getuid()/getgid() later report. --workdir rejects non-absolute
paths up front instead of silently resolving them against the host
cwd, and is applied by elfuse_launch after the casefold probe so the
translation sees the sysroot's real case behavior. --env/--clear-env
build the guest environment with env(1) semantics: KEY=VAL sets,
bare KEY inherits from the host environ, --clear-env starts empty;
with neither flag given envp stays NULL and the host environ is used
unchanged.

The new heap resources join main()'s shared goto unwind: envp,
workdir, and the raw --env override array are released at the single
cleanup label on every exit path.
elfuse-oci is a standalone Go binary that owns the OCI image pipeline;
elfuse itself stays a pure Linux syscall-to-Darwin runtime with no OCI
commands. This first slice is the acquisition half: an OCI image-layout
store plus the pull and inspect commands, built on go-containerregistry
($ELFUSE_OCI_STORE or ~/.local/share/elfuse/oci by default).

The store is a spec-shape image layout other tools can read, with a
refs.json pin table mapping references to manifest digests. An exclusive
flock serializes refs.json/index.json updates so concurrent pulls cannot
lose pins (the cold-store bootstrap of oci-layout and index.json runs
under the same lock, double-checked so a warm store skips it), pin
persistence syncs the temp file and directory around the rename, and a
nil-object refs.json is rejected as corrupt instead of treated as empty.
addImage distinguishes genuinely-absent from unreadable descriptors by
scanning index membership, so store corruption surfaces rather than
duplicating entries. digestFor returns a distinct errNotPulled for a
missing ref so later callers can tell "not pulled" from "store broken".

Two helpers keep the plumbing in one place: every subcommand opens the
store through commonFlags.openResolvedStore (resolve the store path,
then open the layout), and store.withLock scopes lock-held sections; pin
and addImage wrap their load-modify-save cycles in it so a critical
section cannot leak its lock on an error path.

pull resolves the requested platform (default linux/arm64) and validates
--platform shape up front; inspect prints the manifest and config
summary (or --json) and propagates digest/size and writer errors instead
of reporting partial output as success.

Credentials come from the ambient default keychain, but its resolution
is time-bounded: it shells out to whatever helper the Docker config
names, and go-containerregistry drops the context around that exec, so a
wedged helper would otherwise hang the pull with no output. A wrapper
keychain caps the wait and fails with an explanation and the
DOCKER_CONFIG escape hatch instead; a progress line is printed before
the pull so it is never silent.

Tests cover the pin table, store locking, error kinds, flag parsing, and
the command dispatch, with cranePull as a swappable seam so no test
touches the network.
Apply a stored image's layers into a rootfs directory, whiteouts and
all, so `unpack` (and later `run`) can materialize a filesystem tree
from the store without any external tool.

Layer application is hardened against hostile archives: extraction is
bounded by os.Root so no entry, symlink, or hardlink escapes the
destination; member names and hard-link targets archived absolute
(GNU tar -P builders) are applied root-relative, as other OCI
consumers do; parent components are Lstat'd and a symlinked or non-dir
intermediate is replaced with a real directory (containerd/Docker
behavior); opaque whiteouts clear through a real directory only, are
order-independent within a layer, and an invalid bare .wh. entry
fails extraction instead of deleting its parent; a directory entry
replaces a lower-layer non-directory. File bodies are bounded by the
tar header size and short bodies are an error, and permissions are
finalized with an explicit chmod so the host umask cannot skew modes.

setuid/setgid/sticky bits are re-applied where the host allows it, and
degrade gracefully where it does not. An unprivileged chmod that sets
setuid/setgid is rejected with EPERM on macOS when the unpacked file's
inherited group is one the invoking user is not in (a new file takes
its parent directory's group under BSD semantics, e.g. wheel under
/tmp, not the tar's root/shadow owner). Since the rootfs is owned by
the invoking user those bits could not be honored at runtime there
anyway, so they are dropped with a warning naming the lost bit rather
than aborting the whole unpack, which is what lets Debian-family
images and their shadow suite (chage, passwd, ...) unpack at all.
Reported at
sysprog21#191 (comment)

Unpack stages into a temp sibling directory and renames into the
final cache path (keyed by manifest digest under <store>/rootfs/), so
a concurrent reader never observes a partial tree and the loser of a
rename race adopts the winner's complete one. A failed unpack removes
only what it created.

Tests drive whiteouts, opaque ordering, parent-symlink replacement,
hardlink identity, exact-size reads, mode preservation, the special-
bit degrade decision, and the staged-rename semantics over synthetic
layer tarballs.
run is the last pipeline stage: resolve the image config into a concrete
runspec, materialize the rootfs, and exec the existing `elfuse --sysroot
<rootfs> ...` positional launch path, reusing elfuse's HVF bring-up,
shebang, and dynamic-linker plumbing rather than reinventing guest
launch. elfuse is located as a sibling binary ($ELFUSE_BIN overrides for
tests) and replaced via exec so the shell reaps the same pid and
terminal signals reach the guest directly.

The runspec resolves Entrypoint/Cmd/Env/User/WorkingDir with the usual
precedence (--entrypoint drops image Cmd; --env and --clear-env follow
env(1) semantics; --workdir must be guest-absolute). A PATH is
guaranteed: when neither the image config nor --env supplies one,
Docker's conventional default is appended, so a guest whose image omits
PATH still has a search path after the --clear-env launch. A relative
path command resolves against the working directory and a bare name
against the merged PATH inside the image rootfs, following Docker's
exec-form rules (elfuse resolves the initial ELF before applying
--workdir and does no PATH lookup, so the launcher must); a config-only
image WorkingDir no layer ships is created at run time, as runc does. A
symbolic --user or image User is resolved against the image's own
/etc/passwd and /etc/group through os.Root-bounded, no-follow opens, so
a crafted rootfs cannot redirect resolution to host account files.

run auto-pulls only when the ref is genuinely absent (errNotPulled) and
surfaces store corruption instead of masking it behind a network pull;
an explicit --platform must match the pinned image so a ref pulled for
another architecture is not silently launched. Before exec, host-truth
/etc/{resolv.conf,hosts,hostname} are injected into the rootfs through
the same os.Root bounds.

Tests cover runspec resolution and precedence, symbolic user lookup,
runtime-file injection, and the exec argv shape via the execElfuseForRun
seam and a subprocess exec probe.
A plain directory rootfs on the default case-insensitive APFS volume
folds Linux filenames that differ only by case. Default runs now use
a case-sensitive APFS sparsebundle per pinned manifest digest, with a
per-run clonefile COW rootfs so guest writes never mutate the warm
base tree and repeated runs skip the unpack.

Liveness and lifecycle transitions are decided by per-digest advisory
flocks in the bundle directory (bundlelock.go), not by pids or
directory scans: every live run holds run.lock shared from before the
volume is attached until guest exit, so a killed run cannot leak
liveness, and attach.lock serializes provisioning (always acquired
before run.lock, making the exclusive-to-shared downgrade in
provision race-free). Provisioning reuses a mount a live run still
holds and only force-detaches one proven stale by an exclusive
run.lock probe, so a second run of the same digest can never rip the
rootfs out from under a live guest. --keep clones carry an
.elfuse-keep marker so sweeps preserve them.

The plain-rootfs path remains available with --plain-rootfs, and the
non-Darwin stub keeps elfuse-oci buildable for
pull/inspect/unpack tests on Linux.

Darwin tests drive runCaseSensitive through seams for provisioning,
clonefile, spawn, cleanup, and exit, and cover sparsebundle
provisioning, mount/detach, attach-failure teardown, and the lock
protocol with a fake hdiutil; the mount-probe and force-detach hooks
are function variables so tests need no real disk images.

The spawn path intercepts SIGHUP alongside INT/TERM/QUIT and installs
the handler before the child starts, so a hangup or an early signal
still flows through the forward/reap/teardown path. elfuse is invoked
with a "--" separator before the guest command, so an image
Entrypoint beginning with "-" cannot steer the host launcher. hdiutil
attach failures keep stderr in the error message (stdout stays clean
for the plist parse), and the parsed mount path is XML-entity-decoded
so a store path containing "&" or quotes still round-trips.
Add list/images, rmi, and prune on top of the OCI image-layout store.
rmi and prune use reachability GC: shared manifests, configs, and layers
stay on disk while any remaining ref reaches them.

Cache cleanup handles plain rootfs caches and macOS sparsebundle caches
through platform-specific seams, with safety rules for active workloads:
a prune without --all never touches a still-pinned digest's cache, and a
cache a live run still uses is never reclaimed. Liveness is the same
flock discipline on both cache kinds: the bundle's run.lock, and for the
plain digest-keyed rootfs a sibling <hex>.lock that a run holds shared
from before the existence probe until guest exit. The plain run path
execs elfuse in place, so the lock descriptor is made exec-survivable:
the kernel releases it exactly when the elfuse process exits, SIGKILL
included. prune takes each lock exclusively non-blocking and skips busy
caches (dry runs never advertise them); rmi refuses a busy cache even
with --force, before any pin or descriptor is touched. The lock is a
sibling of the cache dir, not inside it, because the dir is the guest's
/ and its existence is unpackImage's atomic-rename publication signal.

prune's GC sweep and list's snapshot run under the store lock, closing
the windows where a concurrent pull's fresh blobs could be reclaimed
before their descriptor lands or a listing could observe a half-removed
ref. prune scopes its GC-plus-cache sweep with store.withLock and drops
the lock before the reporting output; list and rmi keep the explicit
acquire-and-defer because their entire body is the critical section, so
a closure would only add indentation. list reports the full
os/arch/variant platform.

Two corrections to the unpack path come with the locking, because both
are what the lock discipline needs to be true. unpackImage now takes the
caller's already-resolved image rather than re-resolving a ref: the
caller keys the cache by the digest it resolved, so a re-resolution
could observe a different pin from a concurrent repull and fill digest
A's cache with image B's content. resolveImageForUse pairs the
resolution with the digest lock, which is why the fix lands here rather
than in the commit that introduced unpackImage. Layer application also
tracks the ancestors of each entry, so an opaque whiteout arriving after
an implicit parent directory no longer clears content the same layer
just added.

Tests cover reachability GC (shared blobs, stale temp blobs, digest
prefixes), the prune and rmi busy semantics for both cache kinds (live
holder, stale lock file, dry-run visibility), lock survival across the
exec boundary (FD_CLOEXEC clearing plus a child-process flock-lifetime
proxy), which runs hold the lock at exec time, and end-to-end command
wrappers over pull/run/list/rmi/prune. The darwin cache sweep is driven
through the isMountPointFn and detachForce seams with
reapSweepableClones deciding the reap set, so the sweep is testable
without real disk images.
The on-disk store is the contract: pulls must produce a valid OCI
image-layout that other tools can read and that agrees with registry
truth on the manifest digest. Conformance tests pin the layout shape,
and scripts/oci-interop.sh cross-checks the store with crane, skopeo,
and umoci.

CI splits by runner capability: a Linux job runs the pure-Go store
paths (pull, unpack, inspect, lifecycle) without Hypervisor.framework,
a hosted macOS job builds and tests the darwin sparsebundle code and
drives a run-less pull/inspect/list/rmi/prune lifecycle smoke, and the
self-hosted release leg boots guests end to end under HVF: the
alpine:3 default-entrypoint smoke plus a full pull -> inspect -> list
-> run -> rmi -> prune lifecycle that runs python:3.12-slim with an
--entrypoint override and asserts the teardown half of the lifecycle.

The lifecycle teardown covers all three reclamation guardrails: a
plain rmi reclaims the cold cache with the image, run --keep output
refuses rmi without --force, and a live --plain-rootfs guest parked
in sleep pins its cache; prune --cache --all must skip it and rmi
must refuse until the guest exits, the only end-to-end exercise of
the run-lock descriptor riding through the exec into elfuse.
Cover the two-binary model in README and docs: usage.md documents the
elfuse-oci commands and flags, testing.md the offline/CI
validation split, internals.md the host-literal path fallback
semantics, and oci-design.md records the design rationale (the C/Go
boundary, the image-layout store with its refs.json pin table, layer
application, run paths, and lifecycle GC), plus an explicit
scope-and-limitations accounting of which OCI features are and are
not implemented.

oci-design.md also records the concurrency model: store metadata is
lock-serialized, per-digest sparsebundle state is coordinated by the
attach.lock/run.lock pair, and the plain digest-keyed rootfs cache is
guarded by a sibling per-digest lock a run holds across the exec into
elfuse, so prune skips and rmi refuses a cache a live guest still
uses. usage.md notes the resolv.conf fallback nameserver and its
split-DNS implication.

README states the positioning up front: OCI images are consumed as a
distribution vehicle for Linux root filesystems, replacing hand-built
--sysroot trees, and the non-isolation limitations (host-path
fallback, shared network identity, PID space, and clock) are called
out explicitly rather than implied.
Issue sysprog21#224 profiled five real images (python:3.12-slim, node:22-alpine,
golang:1.23-alpine, eclipse-temurin:21, gcc:14). Add one CI job per
image that boots the image under HVF via `elfuse-oci run` and drives
that image's operations, so a change that breaks any of them is caught
on the PR rather than by hand.

A shared driver, scripts/ci/oci-workload.sh <key>, maps each key to its
image and guest workload under scripts/ci/workloads/ and asserts a
per-image sentinel token:

- python: a single-threaded SQLite insert plus an aggregate query, a
  small file write/read/checksum fan-out, and a JSON round-trip.
- node: in-guest compute (fs/crypto/zlib/JSON) plus an HTTP server the
  job reaches over the host loopback; elfuse maps guest sockets to host
  sockets and does no network-namespace isolation, so a guest bound to
  127.0.0.1 is reachable host-side.
- go: gofmt over a tree of misformatted fixtures the workload writes
  itself, asserting the file set it names, the bytes it produces, and
  that a second pass names nothing. The toolchain binaries are Go
  programs, so this drives the runtime's own scheduler and raw syscalls;
  it compiles nothing, because the guest compiler dies on SIGHUP before
  finishing a package.
- jvm: javac + java exercising collections, file I/O, SHA-256, an
  8-thread pool, and a subprocess.
- c: a small multi-file make project plus a heavier single translation
  unit compiled with gcc -O1.

The go and python lanes stay inside the runtime's current limits;
heavier variants that stress those limits, including an in-guest Go
build, are submitted separately.

The jobs run only on self-hosted Apple Silicon because `run` needs
Hypervisor.framework. They share a composite action that fetches the
elfuse binary from build-macos and builds elfuse-oci, and each keeps a
warm per-key store on the runner's persistent disk so only the first run
pulls over the network. gcc:14 and eclipse-temurin:21 ship the shadow
suite, so those jobs also exercise the unpack setuid/setgid degrade end
to end.

The existing python workload moves under scripts/ci/workloads/;
oci-lifecycle.sh follows the new path.
The run smoke proves an image boots and computes; these checks cross
the guest-execution seams it does not. A pathname AF_UNIX socket is
bound inside a guest-created directory with a getsockname round-trip:
the runtime translates sun_path into the sysroot on the way in and
must reverse-map it on the way out, and the sparsebundle clone's deep
host path additionally forces the over-length shortening indirection.
A cold-provision boot (blobs cloned into an ephemeral store with the
cs/ bundles dropped, so no network) must report the unpack, and the
following warm re-attach of the same digest must boot without
unpacking again. A dynamically linked from-image binary runs under an
explicit entrypoint so PT_INTERP and its shared-object closure must
resolve inside the rootfs.

Wired as a runtime-macos Release-leg step beside the run smoke,
sharing its warm store (python:3.12-slim joins alpine and debian
there).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants