2021-08-23 13:14:53 +00:00
|
|
|
//go:build linux
|
2016-01-19 21:54:42 +00:00
|
|
|
|
2018-02-05 21:05:59 +00:00
|
|
|
package apparmor // import "github.com/docker/docker/profiles/apparmor"
|
2016-01-19 21:54:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2024-01-02 12:56:16 +00:00
|
|
|
"fmt"
|
2016-01-19 21:54:42 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2024-01-02 12:56:16 +00:00
|
|
|
"os/exec"
|
2016-01-19 21:54:42 +00:00
|
|
|
"path"
|
|
|
|
"strings"
|
2017-08-08 16:16:41 +00:00
|
|
|
"text/template"
|
2016-01-19 21:54:42 +00:00
|
|
|
)
|
|
|
|
|
2023-05-08 11:49:59 +00:00
|
|
|
// profileDirectory is the file store for apparmor profiles and macros.
|
|
|
|
const profileDirectory = "/etc/apparmor.d"
|
2016-01-19 21:54:42 +00:00
|
|
|
|
|
|
|
// profileData holds information about the given profile for generation.
|
|
|
|
type profileData struct {
|
|
|
|
// Name is profile name.
|
|
|
|
Name string
|
2018-04-08 10:21:30 +00:00
|
|
|
// DaemonProfile is the profile name of our daemon.
|
|
|
|
DaemonProfile string
|
2016-01-19 21:54:42 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateDefault creates an apparmor profile from ProfileData.
|
|
|
|
func (p *profileData) generateDefault(out io.Writer) error {
|
2017-08-08 16:16:41 +00:00
|
|
|
compiled, err := template.New("apparmor_profile").Parse(baseTemplate)
|
2016-01-19 21:54:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-20 07:08:20 +00:00
|
|
|
|
2016-01-19 21:54:42 +00:00
|
|
|
if macroExists("tunables/global") {
|
|
|
|
p.Imports = append(p.Imports, "#include <tunables/global>")
|
|
|
|
} else {
|
|
|
|
p.Imports = append(p.Imports, "@{PROC}=/proc/")
|
|
|
|
}
|
2016-03-20 07:08:20 +00:00
|
|
|
|
2016-01-19 21:54:42 +00:00
|
|
|
if macroExists("abstractions/base") {
|
|
|
|
p.InnerImports = append(p.InnerImports, "#include <abstractions/base>")
|
|
|
|
}
|
2016-03-20 07:08:20 +00:00
|
|
|
|
2016-12-13 06:25:57 +00:00
|
|
|
return compiled.Execute(out, p)
|
2016-01-19 21:54:42 +00:00
|
|
|
}
|
|
|
|
|
2024-03-22 06:30:22 +00:00
|
|
|
// macroExists checks if the passed macro exists.
|
2016-01-19 21:54:42 +00:00
|
|
|
func macroExists(m string) bool {
|
|
|
|
_, err := os.Stat(path.Join(profileDirectory, m))
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2016-09-29 21:05:31 +00: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 21:54:42 +00:00
|
|
|
func InstallDefault(name string) error {
|
|
|
|
p := profileData{
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
|
2018-04-08 10:21:30 +00:00
|
|
|
// Figure out the daemon profile.
|
2021-08-24 10:10:50 +00:00
|
|
|
currentProfile, err := os.ReadFile("/proc/self/attr/current")
|
2018-04-08 10:21:30 +00:00
|
|
|
if err != nil {
|
|
|
|
// If we couldn't get the daemon profile, assume we are running
|
|
|
|
// unconfined which is generally the default.
|
|
|
|
currentProfile = nil
|
|
|
|
}
|
|
|
|
daemonProfile := string(currentProfile)
|
|
|
|
// Normally profiles are suffixed by " (enforcing)" or similar. AppArmor
|
|
|
|
// profiles cannot contain spaces so this doesn't restrict daemon profile
|
|
|
|
// names.
|
|
|
|
if parts := strings.SplitN(daemonProfile, " ", 2); len(parts) >= 1 {
|
|
|
|
daemonProfile = parts[0]
|
|
|
|
}
|
|
|
|
if daemonProfile == "" {
|
|
|
|
daemonProfile = "unconfined"
|
|
|
|
}
|
|
|
|
p.DaemonProfile = daemonProfile
|
|
|
|
|
2016-07-20 12:24:55 +00:00
|
|
|
// Install to a temporary directory.
|
2021-08-24 10:10:50 +00:00
|
|
|
f, err := os.CreateTemp("", name)
|
2016-01-19 21:54:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-20 12:24:55 +00:00
|
|
|
profilePath := f.Name()
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
defer os.Remove(profilePath)
|
|
|
|
|
2016-01-19 21:54:42 +00:00
|
|
|
if err := p.generateDefault(f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-02 12:56:16 +00:00
|
|
|
return loadProfile(profilePath)
|
2016-01-19 21:54:42 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 13:10:08 +00: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 21:54:42 +00:00
|
|
|
file, err := os.Open("/sys/kernel/security/apparmor/profiles")
|
|
|
|
if err != nil {
|
2016-12-05 13:10:08 +00:00
|
|
|
return false, err
|
2016-01-19 21:54:42 +00:00
|
|
|
}
|
2016-06-25 03:57:21 +00:00
|
|
|
defer file.Close()
|
|
|
|
|
2016-01-19 21:54:42 +00:00
|
|
|
r := bufio.NewReader(file)
|
|
|
|
for {
|
|
|
|
p, err := r.ReadString('\n')
|
2016-12-05 13:10:08 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2016-01-19 21:54:42 +00:00
|
|
|
if err != nil {
|
2016-12-05 13:10:08 +00:00
|
|
|
return false, err
|
2016-01-19 21:54:42 +00:00
|
|
|
}
|
|
|
|
if strings.HasPrefix(p, name+" ") {
|
2016-12-05 13:10:08 +00:00
|
|
|
return true, nil
|
2016-01-19 21:54:42 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-05 13:10:08 +00:00
|
|
|
|
|
|
|
return false, nil
|
2016-01-19 21:54:42 +00:00
|
|
|
}
|
2024-01-02 12:56:16 +00:00
|
|
|
|
|
|
|
// loadProfile runs `apparmor_parser -Kr` on a specified apparmor profile to
|
|
|
|
// replace the profile. The `-K` is necessary to make sure that apparmor_parser
|
|
|
|
// doesn't try to write to a read-only filesystem.
|
|
|
|
func loadProfile(profilePath string) error {
|
|
|
|
c := exec.Command("apparmor_parser", "-Kr", profilePath)
|
|
|
|
c.Dir = ""
|
|
|
|
|
|
|
|
output, err := c.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("running `%s %s` failed with output: %s\nerror: %v", c.Path, strings.Join(c.Args, " "), output, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|