Skip to content

capability: don't use /proc/sys/kernel/cap_last_cap - #269

Merged
thaJeztah merged 1 commit into
moby:mainfrom
kolyshkin:capability-lastcap-prctl
Oct 23, 2026
Merged

thaJeztah merged 1 commit into
moby:mainfrom
kolyshkin:capability-lastcap-prctl

Conversation

@kolyshkin

@kolyshkin kolyshkin commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

The value of /proc/sys/kernel/cap_last_cap is used as the upper bound
in a few loops, and as an index into [2]uint32 capability sets, so a
bogus value (e.g. if something is mounted over /proc) can lead to a
panic or an infinite loop. In addition, /proc may not be mounted.

Instead, ask the kernel directly using prctl(PR_CAPBSET_READ), which
returns EINVAL for a capability the kernel does not know about.

Start from the last known capability (CAP_CHECKPOINT_RESTORE), which is
the last one since Linux 5.9, and go up or down from it until the
validity changes. For most kernels, this takes two syscalls. The upper
limit is 63, which is the highest capability that can be represented
by the kernel capability ABI v3. The lower limit is CAP_WAKE_ALARM,
which is always supported, given the minimum kernel version required
by Go.

Since LastCap no longer reads /proc/sys/kernel/cap_last_cap, change
TestLastCap to check that the two values agree, rather than checking
that LastCap is within a fixed range. Similarly, change
TestListSupported to check the exact list length based on LastCap.

Thanks to Anthropic and Ada Logics who found and validated this.

Fixes: #265
Reported-by: Adam Korczynski [email protected]

@kolyshkin
kolyshkin force-pushed the capability-lastcap-prctl branch from 8e852e8 to ad25fc0 Compare October 21, 2026 03:16
Comment thread capability/capability_linux.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The bounded probing and updated assertions are consistent with the supported kernel baseline, with no blocking issues identified.

No open issues 🎉

What changed in this PR

Removes capability detection’s dependency on /proc, allowing capability handling in root filesystems without it and preventing unsafe bounds from bogus file contents.

Changes:

  • Uses bounded PR_CAPBSET_READ probing to detect the highest supported capability.
  • Updates tests to compare against /proc when available and verify the supported-list length.
File Description
capability/​capability_test.go Updates capability detection and list-length assertions.
capability/​capability_linux.go Replaces /proc reads with bounded kernel capability probing.

Review effort: Balanced

The value of /proc/sys/kernel/cap_last_cap is used as the upper bound
in a few loops, and as an index into [2]uint32 capability sets, so a
bogus value (e.g. if something is mounted over /proc) can lead to a
panic or an infinite loop. In addition, /proc may not be mounted.

Instead, ask the kernel directly using prctl(PR_CAPBSET_READ), which
returns EINVAL for a capability the kernel does not know about.

Start from the last known capability (CAP_CHECKPOINT_RESTORE), which is
the last one since Linux 5.9, and go up or down from it until the
validity changes. For most kernels, this takes two syscalls. The upper
limit is 63, which is the highest capability that can be represented
by the kernel capability ABI v3. The lower limit is CAP_WAKE_ALARM,
which is always supported, given the minimum kernel version required
by Go.

Since LastCap no longer reads /proc/sys/kernel/cap_last_cap, change
TestLastCap to check that the two values agree, rather than checking
that LastCap is within a fixed range. Similarly, change
TestListSupported to check the exact list length based on LastCap.

Thanks to Anthropic and Ada Logics who found and validated this.

Fixes: moby#265
Reported-by: Adam Korczynski <[email protected]>
Signed-off-by: Kir Kolyshkin <[email protected]>

@vvoland vvoland 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.

LGTM

@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, nice!

@thaJeztah
thaJeztah merged commit a76f9f9 into moby:main Oct 23, 2026
25 checks passed
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.

capability.LastCap fails when /proc/sys/kernel/cap_last_cap is unavailable

5 participants