Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix capability detection for privileged containers #1086

Merged
merged 1 commit into from
May 6, 2022

Conversation

klutchell
Copy link
Contributor

Description

Privileged containers do not list each cap by name,
instead they lead with =eip and selectively remove
caps with cap_foo_bar-eip.

Instead we can use the --has-p flag of capsh to check
for the permitted cap.

See: https://k3a.me/linux-capabilities-in-a-nutshell/
See: https://man7.org/linux/man-pages/man1/capsh.1.html

Motivation and Context

This will allow privileged containers to not fail the fix_capabilities check as they do today.

 pihole  WARNING: Unable to set capabilities for pihole-FTL.
 pihole           Please ensure that the container has the required capabilities.
 pihole  [cont-init.d] 20-start.sh: exited 1.

How Has This Been Tested?

Confirmed on a Ubuntu desktop environment with Docker 20.10 that privileged containers do not show all the caps expected by the fix_capabilities function. Instead it adopts all caps (=eip) and specifically removes some.

$ docker run --rm -it alpine sh -c 'apk add --no-cache libcap && capsh == --print | grep Current: | grep -q cap_net_admin ; echo $?'
1
$ docker run --rm -it --cap-add NET_ADMIN alpine sh -c 'apk add --no-cache libcap && capsh == --print | grep Current: | grep -q cap_net_admin ; echo $?'
0
$ docker run --rm -it --privileged alpine sh -c 'apk add --no-cache libcap && capsh == --print | grep Current: | grep -q cap_net_admin ; echo $?'
1 # we are privileged so this should be 0

However, the --has-p flag will correctly identify the permitted capabilities.

$ docker run --rm -it alpine sh -c 'apk add --no-cache libcap && capsh --has-p=cap_net_admin ; echo $?'
cap[cap_net_admin] not permitted
1
$ docker run --rm -it --cap-add NET_ADMIN alpine sh -c 'apk add --no-cache libcap && capsh --has-p=cap_net_admin ; echo $?'
0
$ docker run --rm -it --privileged alpine sh -c 'apk add --no-cache libcap && capsh --has-p=cap_net_admin ; echo $?'
0 # much better!

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

Privileged containers do not list each cap by name,
instead they lead with =eip and selectively remove
caps with cap_foo_bar-eip.

Instead we can use the --has-p flag of capsh to check
for the permitted cap.

Signed-off-by: Kyle Harding <kyle@balena.io>
@sonarcloud
Copy link

sonarcloud bot commented May 5, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

Copy link
Member

@PromoFaux PromoFaux left a comment

Choose a reason for hiding this comment

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

Tested - and appears to be working even with privileged: true set in my docker-compose. Thanks for the PR!

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.

2 participants