Skip to content

Commit

Permalink
Add is_privileged.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecrs committed Nov 19, 2024
1 parent f4712e9 commit 3bf66fb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
32 changes: 32 additions & 0 deletions is_privileged.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/sh

set -eu

if [ "${DEBUG:-}" = true ]; then
set -x
fi

verbose() {
if [ "${VERBOSE:-}" = true ]; then
echo "VERBOSE(is_privileged.sh):" "$@" >&2
fi
}

# Get the capability bounding set
cap_bnd=$(grep '^CapBnd:' /proc/$$/status | awk '{print $2}')
# Convert to decimal
cap_bnd=$(printf "%d" "0x${cap_bnd}")

# Get the last capability number
last_cap=$(cat /proc/sys/kernel/cap_last_cap)

# Calculate the maximum capability value
max_cap=$(((1 << (last_cap + 1)) - 1))

if [ "${cap_bnd}" -eq "${max_cap}" ]; then
verbose "Container is running in privileged mode."
exit 0
else
verbose "Container is not running in privileged mode."
exit 1
fi
6 changes: 6 additions & 0 deletions tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ function expect() {
fi
}

## is_privileged.sh tests
"${docker_cmd[@]}" alpine sh -c './is_privileged.sh; echo $?' | expect 1
"${docker_cmd[@]}" --privileged alpine sh -c './is_privileged.sh; echo $?' | expect 0
"${docker_cmd[@]}" ubuntu sh -c './is_privileged.sh; echo $?' | expect 1
"${docker_cmd[@]}" --privileged ubuntu sh -c './is_privileged.sh; echo $?' | expect 0

## get_cpus.sh test
machine_cpus=$(grep -c ^processor /proc/cpuinfo)

Expand Down

0 comments on commit 3bf66fb

Please sign in to comment.