Skip to content

Commit a457d9c

Browse files
authored
Merge pull request containerd#3860 from apostasie/fix-3859
Fix apparmor test not running
2 parents 0f9f062 + 2ecc394 commit a457d9c

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

Dockerfile.d/test-integration-rootless.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
set -eux -o pipefail
1818
if [[ "$(id -u)" = "0" ]]; then
19+
# Ensure securityfs is mounted for apparmor to work
20+
if ! mountpoint -q /sys/kernel/security; then
21+
mount -tsecurityfs securityfs /sys/kernel/security
22+
fi
1923
if [ -e /sys/kernel/security/apparmor/profiles ]; then
2024
# Load the "nerdctl-default" profile for TestRunApparmor
2125
nerdctl apparmor load

hack/test-integration.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ set -o errexit -o errtrace -o functrace -o nounset -o pipefail
1919
root="$(cd "$(dirname "${BASH_SOURCE[0]:-$PWD}")" 2>/dev/null 1>&2 && pwd)"
2020
readonly root
2121

22+
if [[ "$(id -u)" = "0" ]]; then
23+
# Ensure securityfs is mounted for apparmor to work
24+
if ! mountpoint -q /sys/kernel/security; then
25+
mount -tsecurityfs securityfs /sys/kernel/security
26+
fi
27+
fi
28+
2229
readonly timeout="60m"
2330
readonly retries="2"
2431
readonly needsudo="${WITH_SUDO:-}"

pkg/apparmorutil/apparmorutil_linux.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,26 @@ import (
2525

2626
"github.com/moby/sys/userns"
2727

28-
"github.com/containerd/containerd/v2/pkg/apparmor"
2928
"github.com/containerd/log"
3029
)
3130

31+
var (
32+
appArmorSupported bool
33+
checkAppArmor sync.Once
34+
)
35+
36+
// hostSupports returns true if apparmor is enabled for the host
37+
func hostSupports() bool {
38+
checkAppArmor.Do(func() {
39+
// see https://github.com/opencontainers/runc/blob/0d49470392206f40eaab3b2190a57fe7bb3df458/libcontainer/apparmor/apparmor_linux.go
40+
if _, err := os.Stat("/sys/kernel/security/apparmor"); err == nil {
41+
buf, err := os.ReadFile("/sys/module/apparmor/parameters/enabled")
42+
appArmorSupported = err == nil && len(buf) > 1 && buf[0] == 'Y'
43+
}
44+
})
45+
return appArmorSupported
46+
}
47+
3248
// CanLoadNewProfile returns whether the current process can load a new AppArmor profile.
3349
//
3450
// CanLoadNewProfile needs root.
@@ -37,7 +53,7 @@ import (
3753
//
3854
// Related: https://gitlab.com/apparmor/apparmor/-/blob/v3.0.3/libraries/libapparmor/src/kernel.c#L311
3955
func CanLoadNewProfile() bool {
40-
return !userns.RunningInUserNS() && os.Geteuid() == 0 && apparmor.HostSupports()
56+
return !userns.RunningInUserNS() && os.Geteuid() == 0 && hostSupports()
4157
}
4258

4359
var (

0 commit comments

Comments
 (0)