2016-01-19 16:54:42 -05:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package apparmor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
2016-07-20 08:24:55 -04:00
|
|
|
"io/ioutil"
|
2016-01-19 16:54:42 -05:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
2017-08-08 12:16:41 -04:00
|
|
|
"text/template"
|
2016-01-19 16:54:42 -05:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/aaparser"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// 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
|
2016-02-14 02:04:16 -05:00
|
|
|
// Version is the {major, minor, patch} version of apparmor_parser as a single number.
|
|
|
|
Version int
|
2016-01-19 16:54:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// generateDefault creates an apparmor profile from ProfileData.
|
|
|
|
func (p *profileData) generateDefault(out io.Writer) error {
|
2017-08-08 12:16:41 -04:00
|
|
|
compiled, err := template.New("apparmor_profile").Parse(baseTemplate)
|
2016-01-19 16:54:42 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-20 03:08:20 -04:00
|
|
|
|
2016-01-19 16:54:42 -05:00
|
|
|
if macroExists("tunables/global") {
|
|
|
|
p.Imports = append(p.Imports, "#include <tunables/global>")
|
|
|
|
} else {
|
|
|
|
p.Imports = append(p.Imports, "@{PROC}=/proc/")
|
|
|
|
}
|
2016-03-20 03:08:20 -04:00
|
|
|
|
2016-01-19 16:54:42 -05:00
|
|
|
if macroExists("abstractions/base") {
|
|
|
|
p.InnerImports = append(p.InnerImports, "#include <abstractions/base>")
|
|
|
|
}
|
2016-03-20 03:08:20 -04:00
|
|
|
|
|
|
|
ver, err := aaparser.GetVersion()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.Version = ver
|
|
|
|
|
2016-12-13 01:25:57 -05:00
|
|
|
return compiled.Execute(out, p)
|
2016-01-19 16:54:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// macrosExists checks if the passed macro exists.
|
|
|
|
func macroExists(m string) bool {
|
|
|
|
_, err := os.Stat(path.Join(profileDirectory, m))
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2016-09-29 17:05:31 -04:00
|
|
|
// InstallDefault generates a default profile in a temp directory determined by
|
|
|
|
// os.TempDir(), then loads the profile into the kernel using 'apparmor_parser'.
|
2016-01-19 16:54:42 -05:00
|
|
|
func InstallDefault(name string) error {
|
|
|
|
p := profileData{
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
|
2016-07-20 08:24:55 -04:00
|
|
|
// Install to a temporary directory.
|
|
|
|
f, err := ioutil.TempFile("", name)
|
2016-01-19 16:54:42 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-20 08:24:55 -04:00
|
|
|
profilePath := f.Name()
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
defer os.Remove(profilePath)
|
|
|
|
|
2016-01-19 16:54:42 -05:00
|
|
|
if err := p.generateDefault(f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-13 01:25:57 -05:00
|
|
|
return aaparser.LoadProfile(profilePath)
|
2016-01-19 16:54:42 -05:00
|
|
|
}
|
|
|
|
|
2016-12-05 08:10:08 -05:00
|
|
|
// IsLoaded checks if a profile with the given name has been loaded into the
|
|
|
|
// kernel.
|
|
|
|
func IsLoaded(name string) (bool, error) {
|
2016-01-19 16:54:42 -05:00
|
|
|
file, err := os.Open("/sys/kernel/security/apparmor/profiles")
|
|
|
|
if err != nil {
|
2016-12-05 08:10:08 -05:00
|
|
|
return false, err
|
2016-01-19 16:54:42 -05:00
|
|
|
}
|
2016-06-24 23:57:21 -04:00
|
|
|
defer file.Close()
|
|
|
|
|
2016-01-19 16:54:42 -05:00
|
|
|
r := bufio.NewReader(file)
|
|
|
|
for {
|
|
|
|
p, err := r.ReadString('\n')
|
2016-12-05 08:10:08 -05:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2016-01-19 16:54:42 -05:00
|
|
|
if err != nil {
|
2016-12-05 08:10:08 -05:00
|
|
|
return false, err
|
2016-01-19 16:54:42 -05:00
|
|
|
}
|
|
|
|
if strings.HasPrefix(p, name+" ") {
|
2016-12-05 08:10:08 -05:00
|
|
|
return true, nil
|
2016-01-19 16:54:42 -05:00
|
|
|
}
|
|
|
|
}
|
2016-12-05 08:10:08 -05:00
|
|
|
|
|
|
|
return false, nil
|
2016-01-19 16:54:42 -05:00
|
|
|
}
|