Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
24 changes: 24 additions & 0 deletions libcontainer/init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/containerd/console"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/selinux/go-selinux"
"github.com/sirupsen/logrus"
"github.com/vishvananda/netlink"
"golang.org/x/sys/unix"
Expand All @@ -24,6 +25,7 @@ import (
"github.com/opencontainers/runc/internal/cmsg"
"github.com/opencontainers/runc/internal/linux"
"github.com/opencontainers/runc/internal/pathrs"
"github.com/opencontainers/runc/libcontainer/apparmor"
"github.com/opencontainers/runc/libcontainer/capabilities"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/system"
Expand Down Expand Up @@ -735,3 +737,25 @@ func setupPidfd(socket *os.File, initType string) error {
}
return unix.Close(pidFd)
}

// applyMACLabels configures the SELinux exec label and AppArmor profile to be
// applied on the next execve.
func applyMACLabels(config *initConfig) (closer func(), err error) {
// initialises the labeling system
selinux.GetEnabled()

if config.ProcessLabel != "" {
if err := selinux.SetExecLabel(config.ProcessLabel); err != nil {
return nil, fmt.Errorf("can't set process label: %w", err)
}
closer = func() {
selinux.SetExecLabel("") //nolint: errcheck
}
}
Comment thread
lifubang marked this conversation as resolved.

if err := apparmor.ApplyProfile(config.AppArmorProfile); err != nil {
return closer, fmt.Errorf("unable to apply apparmor profile: %w", err)
}

return closer, nil
}
20 changes: 10 additions & 10 deletions libcontainer/setns_init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"golang.org/x/sys/unix"

"github.com/opencontainers/runc/internal/linux"
"github.com/opencontainers/runc/libcontainer/apparmor"
"github.com/opencontainers/runc/libcontainer/keys"
"github.com/opencontainers/runc/libcontainer/seccomp"
"github.com/opencontainers/runc/libcontainer/system"
Expand Down Expand Up @@ -51,6 +50,14 @@ func (l *linuxSetnsInit) Init() error {
}
}

closer, err := applyMACLabels(l.config)
if closer != nil {
defer closer()
}
if err != nil {
return err
}

if l.config.CreateConsole {
if err := setupConsole(l.consoleSocket, l.config, false); err != nil {
return err
Expand Down Expand Up @@ -98,12 +105,7 @@ func (l *linuxSetnsInit) Init() error {
if err := syncParentReady(l.pipe); err != nil {
return fmt.Errorf("sync ready: %w", err)
}
if l.config.ProcessLabel != "" {
if err := selinux.SetExecLabel(l.config.ProcessLabel); err != nil {
return err
}
defer selinux.SetExecLabel("") //nolint: errcheck
}

// Without NoNewPrivileges seccomp is a privileged operation, so we need to
// do this before dropping capabilities; otherwise do it as late as possible
// just before execve so as few syscalls take place after it as possible.
Expand All @@ -119,9 +121,7 @@ func (l *linuxSetnsInit) Init() error {
if err := finalizeNamespace(l.config); err != nil {
return err
}
if err := apparmor.ApplyProfile(l.config.AppArmorProfile); err != nil {
return err
}

// Check for the arg early to make sure it exists.
name, err := exec.LookPath(l.config.Args[0])
if err != nil {
Expand Down
28 changes: 12 additions & 16 deletions libcontainer/standard_init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/opencontainers/runc/internal/linux"
"github.com/opencontainers/runc/internal/pathrs"
"github.com/opencontainers/runc/internal/sys"
"github.com/opencontainers/runc/libcontainer/apparmor"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/keys"
"github.com/opencontainers/runc/libcontainer/seccomp"
Expand Down Expand Up @@ -79,17 +78,26 @@ func (l *linuxStandardInit) Init() error {
}
}

closer, err := applyMACLabels(l.config)
if closer != nil {
defer closer()
}
if err != nil {
return err
}

if err := setupNetwork(l.config); err != nil {
return err
}
if err := setupRoute(l.config.Config); err != nil {
return err
}

// initialises the labeling system
selinux.GetEnabled()
if err := sys.WriteSysctls(l.config.Config.Sysctl); err != nil {
return err
}

err := prepareRootfs(l.pipe, l.config)
err = prepareRootfs(l.pipe, l.config)
if err != nil {
return err
}
Expand Down Expand Up @@ -129,13 +137,7 @@ func (l *linuxStandardInit) Init() error {
return &os.SyscallError{Syscall: "setdomainname", Err: err}
}
}
if err := apparmor.ApplyProfile(l.config.AppArmorProfile); err != nil {
return fmt.Errorf("unable to apply apparmor profile: %w", err)
}

if err := sys.WriteSysctls(l.config.Config.Sysctl); err != nil {
return err
}
for _, path := range l.config.Config.ReadonlyPaths {
if err := readonlyPath(path); err != nil {
return fmt.Errorf("can't make %q read-only: %w", path, err)
Expand Down Expand Up @@ -181,12 +183,6 @@ func (l *linuxStandardInit) Init() error {
if err := syncParentReady(l.pipe); err != nil {
return fmt.Errorf("sync ready: %w", err)
}
if l.config.ProcessLabel != "" {
if err := selinux.SetExecLabel(l.config.ProcessLabel); err != nil {
return fmt.Errorf("can't set process label: %w", err)
}
defer selinux.SetExecLabel("") //nolint: errcheck
}
// Without NoNewPrivileges seccomp is a privileged operation, so we need to
// do this before dropping capabilities; otherwise do it as late as possible
// just before execve so as few syscalls take place after it as possible.
Expand Down
Loading