Skip to content

runc: add CommandFile for fd-based binary execution#113

Draft
dmcgowan wants to merge 1 commit into
containerd:mainfrom
dmcgowan:exec-fd
Draft

runc: add CommandFile for fd-based binary execution#113
dmcgowan wants to merge 1 commit into
containerd:mainfrom
dmcgowan:exec-fd

Conversation

@dmcgowan
Copy link
Copy Markdown
Member

Add Runc.CommandFile *os.File as a Linux-only alternative to Command. When set, every invocation executes the binary via /proc/self/fd/ rather than by filesystem path, which is semantically equivalent to execveat(fd, "", argv, env, AT_EMPTY_PATH): the binary is identified solely by its open file descriptor, so the original path need not remain accessible after the file is opened.

The implementation adds finalizeCommand(), called from startCommand() just before cmd.Start(). It appends CommandFile to cmd.ExtraFiles (so Go's fork+exec inherits the fd) and sets cmd.Path to /proc/self/fd/. The file is always appended last, so any pre-existing ExtraFiles keep their fd positions and --preserve-fds / --status-fd accounting is unaffected.

Tests cover basic execution, execution after the binary path is unlinked (the core guarantee), and the fd numbering invariants.

Ideally in the future Go will support execveat and path resolution can be completely avoided in this case. Experimenting with this for Nerdbox, to avoid needing to have runc/crun available in the rootfs.

Add Runc.CommandFile *os.File as a Linux-only alternative to Command.
When set, every invocation executes the binary via /proc/self/fd/<n>
rather than by filesystem path, which is semantically equivalent to
execveat(fd, "", argv, env, AT_EMPTY_PATH): the binary is identified
solely by its open file descriptor, so the original path need not remain
accessible after the file is opened.

The implementation adds finalizeCommand(), called from startCommand()
just before cmd.Start(). It appends CommandFile to cmd.ExtraFiles (so
Go's fork+exec inherits the fd) and sets cmd.Path to /proc/self/fd/<n>.
The file is always appended last, so any pre-existing ExtraFiles keep
their fd positions and --preserve-fds / --status-fd accounting is
unaffected.

Tests cover basic execution, execution after the binary path is unlinked
(the core guarantee), and the fd numbering invariants.

Signed-off-by: Derek McGowan <derek@mcg.dev>
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.

1 participant