Skip to content

[1.5] libct: don't reset selinux labels on init error path - #5442

Merged
lifubang merged 1 commit into
opencontainers:release-1.5from
kolyshkin:1.5-5439
Sep 4, 2026
Merged

lifubang merged 1 commit into
opencontainers:release-1.5from
kolyshkin:1.5-5439

Conversation

@kolyshkin

Copy link
Copy Markdown
Contributor

Backport of #5439 to release-1.5. Original description follows.


Fixes #5438.

Both linuxStandardInit.Init and linuxSetnsInit.Init deferred a
selinux.SetKeyLabel("") / selinux.SetExecLabel("") call to reset the labels
on return. These deferred calls are useless: on success the function ends with
execve(2) and never returns, and on failure runc init exits right after, so
there is nothing left to reset.

Worse, they are actively harmful. They run after utils.UnsafeCloseFrom, which
closes every file descriptor that must not leak into the container, including
the procfs handle cached internally by libpathrs. Writing the label then
re-opens /proc, and libpathrs aborts the whole process:

thread '<unnamed>' (1) panicked at src/procfs.rs:349:22:
cached procfs handle should be valid: [...] Bad file descriptor
SIGABRT: abort

So a plain execve failure (a bad ELF, or an exec denied by policy) was
reported as a Rust panic plus a Go stack dump instead of the actual error.

Reproducer from the issue, before:

$ runc run tst
thread '<unnamed>' (1) panicked at src/procfs.rs:349:22: ...
SIGABRT: abort
[~100 lines of goroutine dump]

after:

$ runc run tst
exec /bin/broken: exec format error

We do have tests for a failing execve (runc run [execve error] in
run.bats, and its counterpart in exec.bats), but they do not set
process.selinuxLabel, so no deferred label reset is registered. The
selinux.bats tests do set a label, but all of them run a working binary,
so Init never returns. The new test covers the intersection.


The test case before the fix is being tested in #5440.

Both linuxStandardInit.Init and linuxSetnsInit.Init deferred a
selinux.SetKeyLabel("") / selinux.SetExecLabel("") call to reset the
labels on return. These deferred calls are useless: on success the
function ends with execve(2) and never returns, and on failure runc init
exits right after, so there is nothing left to reset.

Worse, they are actively harmful. They run after utils.UnsafeCloseFrom,
which closes every file descriptor that must not leak into the
container, including the procfs handle cached internally by libpathrs.
Writing the label then re-opens /proc, and libpathrs aborts the whole
process:

  thread '<unnamed>' (1) panicked at src/procfs.rs:349:22:
  cached procfs handle should be valid: [...] Bad file descriptor
  SIGABRT: abort

So a plain execve failure (a bad ELF, or an exec denied by policy) was
reported as a Rust panic plus a Go stack dump instead of the actual
error.

Drop the deferred calls, and add a test case.

Fixes opencontainers#5438

(cherry picked from commit 1056e68)
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
@kolyshkin kolyshkin added the backport/1.5-pr A backport PR to release-1.5 label Sep 3, 2026
@kolyshkin kolyshkin added this to the 1.5.2 milestone Sep 3, 2026

@thaJeztah thaJeztah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@lifubang
lifubang merged commit 32bd267 into opencontainers:release-1.5 Sep 4, 2026
56 checks passed
@kolyshkin kolyshkin mentioned this pull request Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport/1.5-pr A backport PR to release-1.5

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants