mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-11 14:13:58 +08:00
f3f563bc0f
Fix issue 2801
Tested on Arch Linux with the following configuration
```
[root@archlinux ~]# pacman -Q runc containerd docker linux
runc 1.0.0rc93-1
containerd 1.4.3-1
docker 1:20.10.3-1
linux 5.10.14.arch1-1
[root@archlinux ~]# cat /proc/cmdline
BOOT_IMAGE=/boot/vmlinuz-linux root=UUID=bd8aaf58-8735-4fd5-a0c1-804a998f8d57 rw net.ifnames=0 rootflags=compress-force=zstd apparmor=1 lsm=capability,lockdown,yama,bpf,apparmor
[root@archlinux ~]# cat /etc/docker/daemon.json
{
"runtimes": {
"runc-tmp": {
"path": "/tmp/runc"
}
}
}
[root@archlinux ~]# docker run -it --rm alpine
docker: Error response from daemon: OCI runtime create failed: container_linux.go:367: starting container process caused: process_linux.go:495: container init caused: apply apparmor profile: apparmor failed to apply profile: write /proc/self/attr/exec: invalid argument: unknown.
[root@archlinux ~]# docker run -it --rm --runtime=runc-tmp alpine
/ # cat /proc/self/attr/apparmor/current
docker-default (enforce)
/ # cat /proc/self/attr/current
cat: read error: Invalid argument
``
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package apparmor
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/utils"
|
|
)
|
|
|
|
// IsEnabled returns true if apparmor is enabled for the host.
|
|
func IsEnabled() bool {
|
|
if _, err := os.Stat("/sys/kernel/security/apparmor"); err == nil {
|
|
buf, err := ioutil.ReadFile("/sys/module/apparmor/parameters/enabled")
|
|
return err == nil && bytes.HasPrefix(buf, []byte("Y"))
|
|
}
|
|
return false
|
|
}
|
|
|
|
func setProcAttr(attr, value string) error {
|
|
// Under AppArmor you can only change your own attr, so use /proc/self/
|
|
// instead of /proc/<tid>/ like libapparmor does
|
|
attrPath := "/proc/self/attr/apparmor/" + attr
|
|
if _, err := os.Stat(attrPath); errors.Is(err, os.ErrNotExist) {
|
|
// fall back to the old convention
|
|
attrPath = "/proc/self/attr/" + attr
|
|
}
|
|
|
|
f, err := os.OpenFile(attrPath, os.O_WRONLY, 0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
|
|
if err := utils.EnsureProcHandle(f); err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = f.WriteString(value)
|
|
return err
|
|
}
|
|
|
|
// changeOnExec reimplements aa_change_onexec from libapparmor in Go
|
|
func changeOnExec(name string) error {
|
|
if err := setProcAttr("exec", "exec "+name); err != nil {
|
|
return fmt.Errorf("apparmor failed to apply profile: %s", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ApplyProfile will apply the profile with the specified name to the process after
|
|
// the next exec.
|
|
func ApplyProfile(name string) error {
|
|
if name == "" {
|
|
return nil
|
|
}
|
|
|
|
return changeOnExec(name)
|
|
}
|