2016-12-02 07:13:41 +00:00
|
|
|
// +build apparmor
|
|
|
|
|
2016-11-29 12:34:15 +00:00
|
|
|
package apparmor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2016-12-12 07:55:17 +00:00
|
|
|
"fmt"
|
2016-11-29 12:34:15 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/docker/utils/templates"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/apparmor"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// profileDirectory is the file store for apparmor profiles and macros.
|
|
|
|
profileDirectory = "/etc/apparmor.d"
|
|
|
|
)
|
|
|
|
|
|
|
|
// profileData holds information about the given profile for generation.
|
|
|
|
type profileData struct {
|
|
|
|
// Name is profile name.
|
|
|
|
Name string
|
|
|
|
// Imports defines the apparmor functions to import, before defining the profile.
|
|
|
|
Imports []string
|
|
|
|
// InnerImports defines the apparmor functions to import in the profile.
|
|
|
|
InnerImports []string
|
|
|
|
// Version is the {major, minor, patch} version of apparmor_parser as a single number.
|
|
|
|
Version int
|
|
|
|
}
|
|
|
|
|
2016-12-12 07:55:17 +00:00
|
|
|
// EnsureDefaultApparmorProfile loads default apparmor profile, if it is not loaded.
|
|
|
|
func EnsureDefaultApparmorProfile() error {
|
|
|
|
if apparmor.IsEnabled() {
|
|
|
|
loaded, err := IsLoaded(DefaultApparmorProfile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not check if %s AppArmor profile was loaded: %s", DefaultApparmorProfile, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to do.
|
|
|
|
if loaded {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the profile.
|
2016-12-07 11:32:50 +00:00
|
|
|
if err := InstallDefault(DefaultApparmorProfile); err != nil {
|
2016-12-12 07:55:17 +00:00
|
|
|
return fmt.Errorf("AppArmor enabled on system but the %s profile could not be loaded.", DefaultApparmorProfile)
|
2016-11-29 12:34:15 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-12 07:55:17 +00:00
|
|
|
|
|
|
|
return nil
|
2016-11-29 12:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsEnabled returns true if apparmor is enabled for the host.
|
|
|
|
func IsEnabled() bool {
|
|
|
|
return apparmor.IsEnabled()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetProfileNameFromPodAnnotations gets the name of the profile to use with container from
|
|
|
|
// pod annotations
|
|
|
|
func GetProfileNameFromPodAnnotations(annotations map[string]string, containerName string) string {
|
|
|
|
return annotations[ContainerAnnotationKeyPrefix+containerName]
|
|
|
|
}
|
|
|
|
|
|
|
|
// InstallDefault generates a default profile in a temp directory determined by
|
|
|
|
// os.TempDir(), then loads the profile into the kernel using 'apparmor_parser'.
|
|
|
|
func InstallDefault(name string) error {
|
|
|
|
p := profileData{
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install to a temporary directory.
|
|
|
|
f, err := ioutil.TempFile("", name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if err := p.generateDefault(f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-02 07:13:41 +00:00
|
|
|
return LoadProfile(f.Name())
|
2016-11-29 12:34:15 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 07:55:17 +00:00
|
|
|
// IsLoaded checks if a profile with the given name has been loaded into the
|
|
|
|
// kernel.
|
|
|
|
func IsLoaded(name string) (bool, error) {
|
2016-11-29 12:34:15 +00:00
|
|
|
file, err := os.Open("/sys/kernel/security/apparmor/profiles")
|
|
|
|
if err != nil {
|
2016-12-12 07:55:17 +00:00
|
|
|
return false, err
|
2016-11-29 12:34:15 +00:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2016-12-12 07:55:17 +00:00
|
|
|
r := bufio.NewReader(file)
|
|
|
|
for {
|
|
|
|
p, err := r.ReadString('\n')
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(p, name+" ") {
|
|
|
|
return true, nil
|
2016-11-29 12:34:15 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-12 07:55:17 +00:00
|
|
|
|
|
|
|
return false, nil
|
2016-11-29 12:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// generateDefault creates an apparmor profile from ProfileData.
|
|
|
|
func (p *profileData) generateDefault(out io.Writer) error {
|
|
|
|
compiled, err := templates.NewParse("apparmor_profile", baseTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if macroExists("tunables/global") {
|
|
|
|
p.Imports = append(p.Imports, "#include <tunables/global>")
|
|
|
|
} else {
|
|
|
|
p.Imports = append(p.Imports, "@{PROC}=/proc/")
|
|
|
|
}
|
|
|
|
|
|
|
|
if macroExists("abstractions/base") {
|
|
|
|
p.InnerImports = append(p.InnerImports, "#include <abstractions/base>")
|
|
|
|
}
|
|
|
|
|
|
|
|
ver, err := GetVersion()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.Version = ver
|
|
|
|
|
2016-12-02 07:13:41 +00:00
|
|
|
return compiled.Execute(out, p)
|
2016-11-29 12:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// macrosExists checks if the passed macro exists.
|
|
|
|
func macroExists(m string) bool {
|
|
|
|
_, err := os.Stat(path.Join(profileDirectory, m))
|
|
|
|
return err == nil
|
|
|
|
}
|