apparmor.d/pkg/prebuild/prepare.go

250 lines
6.6 KiB
Go
Raw Normal View History

// apparmor.d - Full set of apparmor profiles
// Copyright (C) 2023-2024 Alexandre Pujol <alexandre@pujol.io>
// SPDX-License-Identifier: GPL-2.0-only
package prebuild
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/arduino/go-paths-helper"
"github.com/roddhjav/apparmor.d/pkg/logging"
"github.com/roddhjav/apparmor.d/pkg/util"
)
// Prepare the build directory with the following tasks
2023-12-15 20:14:32 +01:00
var (
Prepares = []PrepareFunc{
Synchronise,
Ignore,
Merge,
Configure,
SetFlags,
SetProfileSystemd,
2023-12-15 20:14:32 +01:00
}
PrepareMsg = map[string]string{
"Synchronise": "Initialize a new clean apparmor.d build directory",
"Ignore": "Ignore profiles and files from:",
"Merge": "Merge all profiles",
"Configure": "Set distribution specificities",
"SetFlags": "Set flags on some profiles",
"SetProfileSystemd": "Use the systemd unit file to set a profile for a given unit",
"SetEarlySystemd": "Set systemd unit drop in files to ensure some service start after apparmor",
2023-12-15 20:14:32 +01:00
"SetFullSystemPolicy": "Configure AppArmor for full system policy",
}
)
2023-12-15 20:14:32 +01:00
type PrepareFunc func() ([]string, error)
// Initialize a new clean apparmor.d build directory
2023-12-15 20:14:32 +01:00
func Synchronise() ([]string, error) {
res := []string{}
dirs := paths.PathList{RootApparmord, Root.Join("root"), Root.Join("systemd")}
for _, dir := range dirs {
if err := dir.RemoveAll(); err != nil {
2023-12-15 20:14:32 +01:00
return res, err
}
}
for _, name := range []string{"apparmor.d", "root"} {
if err := copyTo(paths.New(name), Root.Join(name)); err != nil {
2023-12-15 20:14:32 +01:00
return res, err
}
}
2023-12-15 20:14:32 +01:00
return res, nil
}
// Ignore profiles and files as defined in dists/ignore/
2023-12-15 20:14:32 +01:00
func Ignore() ([]string, error) {
res := []string{}
for _, name := range []string{"main.ignore", Distribution + ".ignore"} {
2023-04-26 00:26:04 +02:00
path := DistDir.Join("ignore", name)
if !path.Exist() {
continue
}
lines, _ := path.ReadFileAsLines()
for _, line := range lines {
if strings.HasPrefix(line, "#") || line == "" {
continue
}
profile := Root.Join(line)
if profile.NotExist() {
files, err := RootApparmord.ReadDirRecursiveFiltered(nil, paths.FilterNames(line))
if err != nil {
2023-12-15 20:14:32 +01:00
return res, err
}
for _, path := range files {
if err := path.RemoveAll(); err != nil {
2023-12-15 20:14:32 +01:00
return res, err
}
}
} else {
if err := profile.RemoveAll(); err != nil {
2023-12-15 20:14:32 +01:00
return res, err
}
}
}
2023-12-15 20:14:32 +01:00
res = append(res, path.String())
}
2023-12-15 20:14:32 +01:00
return res, nil
}
// Merge all profiles in a new apparmor.d directory
2023-12-15 20:14:32 +01:00
func Merge() ([]string, error) {
res := []string{}
dirToMerge := []string{
"groups/*/*", "groups",
"profiles-*-*/*", "profiles-*",
}
idx := 0
for idx < len(dirToMerge)-1 {
dirMoved, dirRemoved := dirToMerge[idx], dirToMerge[idx+1]
files, err := filepath.Glob(RootApparmord.Join(dirMoved).String())
if err != nil {
2023-12-15 20:14:32 +01:00
return res, err
}
for _, file := range files {
err := os.Rename(file, RootApparmord.Join(filepath.Base(file)).String())
if err != nil {
2023-12-15 20:14:32 +01:00
return res, err
}
}
files, err = filepath.Glob(RootApparmord.Join(dirRemoved).String())
if err != nil {
2023-12-15 20:14:32 +01:00
return []string{}, err
}
for _, file := range files {
if err := paths.New(file).RemoveAll(); err != nil {
2023-12-15 20:14:32 +01:00
return res, err
}
}
idx = idx + 2
}
2023-12-15 20:14:32 +01:00
return res, nil
}
// Set the distribution specificities
2023-12-15 20:14:32 +01:00
func Configure() ([]string, error) {
res := []string{}
switch Distribution {
case "arch", "opensuse":
case "ubuntu":
debianOverwriteClean()
if overwrite {
profiles := getOverwriteProfiles()
debianOverwrite(profiles)
} else {
if err := copyTo(DistDir.Join("ubuntu"), RootApparmord); err != nil {
return res, err
}
}
case "debian", "whonix":
debianOverwriteClean()
// Copy Debian specific abstractions
if err := copyTo(DistDir.Join("ubuntu"), RootApparmord); err != nil {
2023-12-15 20:14:32 +01:00
return res, err
}
default:
2023-12-15 20:14:32 +01:00
return []string{}, fmt.Errorf("%s is not a supported distribution", Distribution)
}
2023-12-15 20:14:32 +01:00
return res, nil
}
// Set flags on some profiles according to manifest defined in `dists/flags/`
2023-12-15 20:14:32 +01:00
func SetFlags() ([]string, error) {
res := []string{}
for _, name := range []string{"main.flags", Distribution + ".flags"} {
path := FlagDir.Join(name)
if !path.Exist() {
continue
}
lines, _ := path.ReadFileAsLines()
for _, line := range lines {
if strings.HasPrefix(line, "#") || line == "" {
continue
}
manifest := strings.Split(line, " ")
profile := manifest[0]
file := RootApparmord.Join(profile)
if !file.Exist() {
logging.Warning("Profile %s not found", profile)
continue
}
// If flags is set, overwrite profile flag
if len(manifest) > 1 {
flags := " flags=(" + manifest[1] + ") {"
content, err := file.ReadFile()
if err != nil {
2023-12-15 20:14:32 +01:00
return res, err
}
// Remove all flags definition, then set manifest' flags
2023-12-15 20:14:32 +01:00
out := regFlags.ReplaceAllLiteralString(string(content), "")
out = regProfileHeader.ReplaceAllLiteralString(out, flags)
if err := file.WriteFile([]byte(out)); err != nil {
return res, err
}
}
}
2023-12-15 20:14:32 +01:00
res = append(res, path.String())
}
2023-12-15 20:14:32 +01:00
return res, nil
}
// Use the systemd unit file to set a profile for a given unit
func SetProfileSystemd() ([]string, error) {
2023-12-15 20:14:32 +01:00
return []string{}, copyTo(paths.New("systemd/default/"), Root.Join("systemd"))
}
// Set systemd unit drop in files to ensure some service start after apparmor
func SetEarlySystemd() ([]string, error) {
return []string{}, copyTo(paths.New("systemd/early/"), Root.Join("systemd"))
}
// Set AppArmor for (experimental) full system policy.
// See https://apparmor.pujol.io/full-system-policy/
2023-12-15 20:14:32 +01:00
func SetFullSystemPolicy() ([]string, error) {
res := []string{}
// Install full system policy profiles
if err := copyTo(paths.New("apparmor.d/groups/_full/"), Root.Join("apparmor.d")); err != nil {
2023-12-15 20:14:32 +01:00
return res, err
}
// Set systemd profile name
path := RootApparmord.Join("tunables/multiarch.d/system")
content, err := path.ReadFile()
if err != nil {
2023-12-15 20:14:32 +01:00
return res, err
}
2023-12-15 20:14:32 +01:00
out := strings.Replace(string(content), "@{systemd}=unconfined", "@{systemd}=systemd", -1)
out = strings.Replace(out, "@{systemd_user}=unconfined", "@{systemd_user}=systemd-user", -1)
2023-12-15 20:14:32 +01:00
if err := path.WriteFile([]byte(out)); err != nil {
return res, err
}
// Fix conflicting x modifiers in abstractions - FIXME: Temporary solution
path = RootApparmord.Join("abstractions/gstreamer")
content, err = path.ReadFile()
if err != nil {
return res, err
}
out = string(content)
regFixConflictX := util.ToRegexRepl([]string{`.*gst-plugin-scanner.*`, ``})
out = regFixConflictX.Replace(out)
if err := path.WriteFile([]byte(out)); err != nil {
return res, err
}
// Set systemd unit drop-in files
2023-12-15 20:14:32 +01:00
return res, copyTo(paths.New("systemd/full/"), Root.Join("systemd"))
}