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

Enable rootless AppArmor #19303

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions libpod/container_internal_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/containers/common/libnetwork/etchosts"
"github.com/containers/common/libnetwork/resolvconf"
"github.com/containers/common/libnetwork/types"
"github.com/containers/common/pkg/apparmor"
"github.com/containers/common/pkg/chown"
"github.com/containers/common/pkg/config"
"github.com/containers/common/pkg/subscriptions"
Expand All @@ -50,6 +49,7 @@ import (
"github.com/containers/storage/pkg/lockfile"
stypes "github.com/containers/storage/types"
securejoin "github.com/cyphar/filepath-securejoin"
runcaa "github.com/opencontainers/runc/libcontainer/apparmor"
kernelmethod marked this conversation as resolved.
Show resolved Hide resolved
runcuser "github.com/opencontainers/runc/libcontainer/user"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate"
Expand Down Expand Up @@ -224,12 +224,12 @@ func (c *Container) generateSpec(ctx context.Context) (s *spec.Spec, cleanupFunc
}

// Apply AppArmor checks and load the default profile if needed.
if len(c.config.Spec.Process.ApparmorProfile) > 0 {
updatedProfile, err := apparmor.CheckProfileAndLoadDefault(c.config.Spec.Process.ApparmorProfile)
if err != nil {
return nil, nil, err
if profile := c.config.Spec.Process.ApparmorProfile; len(profile) > 0 {
if runcaa.IsEnabled() {
g.SetProcessApparmorProfile(profile)
} else if profile != "unconfined" {
return nil, nil, fmt.Errorf("profile %q specified but AppArmor is disabled", profile)
}
g.SetProcessApparmorProfile(updatedProfile)
}

if err := c.makeBindMounts(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/specgen/generate/security_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"strings"

"github.com/containers/common/libimage"
"github.com/containers/common/pkg/apparmor"
"github.com/containers/common/pkg/capabilities"
"github.com/containers/common/pkg/config"
cutil "github.com/containers/common/pkg/util"
"github.com/containers/podman/v4/libpod"
"github.com/containers/podman/v4/libpod/define"
"github.com/containers/podman/v4/pkg/specgen"
"github.com/containers/podman/v4/pkg/util"
runcaa "github.com/opencontainers/runc/libcontainer/apparmor"
"github.com/opencontainers/runtime-tools/generate"
"github.com/opencontainers/selinux/go-selinux/label"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -61,7 +61,7 @@ func setLabelOpts(s *specgen.SpecGenerator, runtime *libpod.Runtime, pidConfig s

func setupApparmor(s *specgen.SpecGenerator, rtc *config.Config, g *generate.Generator) error {
hasProfile := len(s.ApparmorProfile) > 0
if !apparmor.IsEnabled() {
if !runcaa.IsEnabled() {
if hasProfile && s.ApparmorProfile != "unconfined" {
return fmt.Errorf("apparmor profile %q specified, but Apparmor is not enabled on this system", s.ApparmorProfile)
}
Expand Down
15 changes: 13 additions & 2 deletions test/e2e/run_apparmor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,30 @@ import (
"path/filepath"

"github.com/containers/common/pkg/apparmor"
"github.com/containers/podman/v4/pkg/rootless"
. "github.com/containers/podman/v4/test/utils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gexec"
runcaa "github.com/opencontainers/runc/libcontainer/apparmor"
)

// wip
func skipIfAppArmorEnabled() {
if apparmor.IsEnabled() {
if runcaa.IsEnabled() {
Skip("Apparmor is enabled")
}
}
func skipIfAppArmorDisabled() {
if !apparmor.IsEnabled() {
if !runcaa.IsEnabled() {
Skip("Apparmor is not enabled")
}
}
func skipIfRootless() {
Copy link
Member

Choose a reason for hiding this comment

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

there is already SkipIfRootless

if rootless.IsRootless() {
Skip("Running test without root")
}
}

var _ = Describe("Podman run", func() {

Expand All @@ -43,6 +50,8 @@ var _ = Describe("Podman run", func() {

It("podman run no apparmor --privileged", func() {
skipIfAppArmorDisabled()
// Root is required to use --privileged
kernelmethod marked this conversation as resolved.
Show resolved Hide resolved
skipIfRootless()
session := podmanTest.Podman([]string{"create", "--privileged", ALPINE, "ls"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
Expand All @@ -67,6 +76,8 @@ var _ = Describe("Podman run", func() {

It("podman run apparmor aa-test-profile", func() {
skipIfAppArmorDisabled()
// Root is required to load the AppArmor profile into the kernel with apparmor_parser
skipIfRootless()
aaProfile := `
#include <tunables/global>
profile aa-test-profile flags=(attach_disconnected,mediate_deleted) {
Expand Down
3 changes: 1 addition & 2 deletions test/test_podman_baseline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -536,11 +536,10 @@ EOF
else
echo "failed"
fi
#Expected to fail (as rootless)
sudo -u "#1000" podman run --security-opt apparmor=$aaProfile docker.io/library/alpine:latest echo hello
rc=$?
echo -n "rootless with specified AppArmor profile: "
if [ $rc != 0 ]; then
if [ $rc == 0 ]; then
echo "passed"
else
echo "failed"
Expand Down