2023-04-19 18:40:40 +02:00
|
|
|
// apparmor.d - Full set of apparmor profiles
|
2024-02-07 00:16:21 +01:00
|
|
|
// Copyright (C) 2023-2024 Alexandre Pujol <alexandre@pujol.io>
|
2023-04-19 18:40:40 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
2023-05-06 14:01:07 +02:00
|
|
|
package prebuild
|
2023-04-19 18:40:40 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/arduino/go-paths-helper"
|
|
|
|
"github.com/roddhjav/apparmor.d/pkg/logging"
|
|
|
|
)
|
|
|
|
|
2023-05-06 14:01:07 +02:00
|
|
|
// Prepare the build directory with the following tasks
|
2023-12-15 20:14:32 +01:00
|
|
|
var (
|
|
|
|
Prepares = []PrepareFunc{
|
|
|
|
Synchronise,
|
|
|
|
Ignore,
|
|
|
|
Merge,
|
|
|
|
Configure,
|
|
|
|
SetFlags,
|
|
|
|
}
|
|
|
|
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",
|
|
|
|
"SetDefaultSystemd": "Set systemd unit drop in files to ensure some service start after apparmor",
|
|
|
|
"SetFullSystemPolicy": "Configure AppArmor for full system policy",
|
|
|
|
}
|
|
|
|
)
|
2023-05-06 14:01:07 +02:00
|
|
|
|
2023-12-15 20:14:32 +01:00
|
|
|
type PrepareFunc func() ([]string, error)
|
2023-05-06 14:01:07 +02:00
|
|
|
|
2023-04-19 18:40:40 +02:00
|
|
|
// Initialize a new clean apparmor.d build directory
|
2023-12-15 20:14:32 +01:00
|
|
|
func Synchronise() ([]string, error) {
|
|
|
|
res := []string{}
|
2023-11-19 15:47:55 +01:00
|
|
|
dirs := paths.PathList{RootApparmord, Root.Join("root"), Root.Join("systemd")}
|
2023-04-19 18:40:40 +02:00
|
|
|
for _, dir := range dirs {
|
|
|
|
if err := dir.RemoveAll(); err != nil {
|
2023-12-15 20:14:32 +01:00
|
|
|
return res, err
|
2023-04-19 18:40:40 +02:00
|
|
|
}
|
|
|
|
}
|
2023-11-19 15:47:55 +01:00
|
|
|
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-04-19 18:40:40 +02:00
|
|
|
}
|
|
|
|
}
|
2023-12-15 20:14:32 +01:00
|
|
|
return res, nil
|
2023-04-19 18:40:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore profiles and files as defined in dists/ignore/
|
2023-12-15 20:14:32 +01:00
|
|
|
func Ignore() ([]string, error) {
|
|
|
|
res := []string{}
|
2023-04-19 18:40:40 +02:00
|
|
|
for _, name := range []string{"main.ignore", Distribution + ".ignore"} {
|
2023-04-26 00:26:04 +02:00
|
|
|
path := DistDir.Join("ignore", name)
|
2023-04-19 18:40:40 +02:00
|
|
|
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
|
2023-04-19 18:40:40 +02:00
|
|
|
}
|
|
|
|
for _, path := range files {
|
|
|
|
if err := path.RemoveAll(); err != nil {
|
2023-12-15 20:14:32 +01:00
|
|
|
return res, err
|
2023-04-19 18:40:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := profile.RemoveAll(); err != nil {
|
2023-12-15 20:14:32 +01:00
|
|
|
return res, err
|
2023-04-19 18:40:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-15 20:14:32 +01:00
|
|
|
res = append(res, path.String())
|
2023-04-19 18:40:40 +02:00
|
|
|
}
|
2023-12-15 20:14:32 +01:00
|
|
|
return res, nil
|
2023-04-19 18:40:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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{
|
2023-04-19 18:40:40 +02:00
|
|
|
"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
|
2023-04-19 18:40:40 +02:00
|
|
|
}
|
|
|
|
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
|
2023-04-19 18:40:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
files, err = filepath.Glob(RootApparmord.Join(dirRemoved).String())
|
|
|
|
if err != nil {
|
2023-12-15 20:14:32 +01:00
|
|
|
return []string{}, err
|
2023-04-19 18:40:40 +02:00
|
|
|
}
|
|
|
|
for _, file := range files {
|
|
|
|
if err := paths.New(file).RemoveAll(); err != nil {
|
2023-12-15 20:14:32 +01:00
|
|
|
return res, err
|
2023-04-19 18:40:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
idx = idx + 2
|
|
|
|
}
|
2023-12-15 20:14:32 +01:00
|
|
|
return res, nil
|
2023-04-19 18:40:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the distribution specificities
|
2023-12-15 20:14:32 +01:00
|
|
|
func Configure() ([]string, error) {
|
|
|
|
res := []string{}
|
2023-04-19 18:40:40 +02:00
|
|
|
switch Distribution {
|
2023-07-09 16:09:32 +02:00
|
|
|
case "arch", "opensuse":
|
2023-04-19 18:40:40 +02:00
|
|
|
|
2023-04-24 01:17:00 +02:00
|
|
|
case "debian", "ubuntu", "whonix":
|
2023-04-26 00:25:01 +02:00
|
|
|
// Copy Ubuntu specific profiles
|
|
|
|
if err := copyTo(DistDir.Join("ubuntu"), RootApparmord); err != nil {
|
2023-12-15 20:14:32 +01:00
|
|
|
return res, err
|
2023-04-19 18:40:40 +02:00
|
|
|
}
|
2023-04-26 00:25:01 +02:00
|
|
|
|
2023-04-19 18:40:40 +02:00
|
|
|
default:
|
2023-12-15 20:14:32 +01:00
|
|
|
return []string{}, fmt.Errorf("%s is not a supported distribution", Distribution)
|
2023-04-19 18:40:40 +02:00
|
|
|
|
|
|
|
}
|
2023-12-15 20:14:32 +01:00
|
|
|
return res, nil
|
2023-04-19 18:40:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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{}
|
2023-04-19 18:40:40 +02:00
|
|
|
for _, name := range []string{"main.flags", Distribution + ".flags"} {
|
2023-07-23 21:36:48 +02:00
|
|
|
path := FlagDir.Join(name)
|
2023-04-19 18:40:40 +02:00
|
|
|
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
|
2023-04-19 18:40:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-04-19 18:40:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-15 20:14:32 +01:00
|
|
|
res = append(res, path.String())
|
2023-04-19 18:40:40 +02:00
|
|
|
}
|
2023-12-15 20:14:32 +01:00
|
|
|
return res, nil
|
2023-04-19 18:40:40 +02:00
|
|
|
}
|
|
|
|
|
2023-11-19 15:20:14 +01:00
|
|
|
// Set systemd unit drop in files to ensure some service start after apparmor
|
2023-12-15 20:14:32 +01:00
|
|
|
func SetDefaultSystemd() ([]string, error) {
|
|
|
|
return []string{}, copyTo(paths.New("systemd/default/"), Root.Join("systemd"))
|
2023-11-19 15:20:14 +01:00
|
|
|
}
|
|
|
|
|
2023-11-11 21:25:27 +01:00
|
|
|
// Set AppArmor for (experimental) full system policy.
|
2024-01-30 16:45:03 +01:00
|
|
|
// See https://apparmor.pujol.io/full-system-policy/
|
2023-12-15 20:14:32 +01:00
|
|
|
func SetFullSystemPolicy() ([]string, error) {
|
|
|
|
res := []string{}
|
2023-11-19 12:14:31 +01:00
|
|
|
// Install full system policy profiles
|
2023-11-26 22:19:16 +01:00
|
|
|
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
|
2023-04-19 18:40:40 +02:00
|
|
|
}
|
2023-11-19 12:14:31 +01:00
|
|
|
|
|
|
|
// Set systemd profile name
|
2023-12-10 15:30:47 +01:00
|
|
|
path := RootApparmord.Join("tunables/multiarch.d/system")
|
2023-11-19 12:14:31 +01:00
|
|
|
content, err := path.ReadFile()
|
|
|
|
if err != nil {
|
2023-12-15 20:14:32 +01:00
|
|
|
return res, err
|
2023-11-19 12:14:31 +01:00
|
|
|
}
|
2023-12-15 20:14:32 +01:00
|
|
|
out := strings.Replace(string(content), "@{systemd}=unconfined", "@{systemd}=systemd", -1)
|
2024-02-15 00:58:18 +01:00
|
|
|
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
|
2023-11-19 12:14:31 +01:00
|
|
|
}
|
|
|
|
|
2023-11-19 15:32:57 +01:00
|
|
|
// Set systemd unit drop-in files
|
2023-12-15 20:14:32 +01:00
|
|
|
return res, copyTo(paths.New("systemd/full/"), Root.Join("systemd"))
|
2023-04-19 18:40:40 +02:00
|
|
|
}
|