2023-04-19 18:40:40 +02:00
|
|
|
// apparmor.d - Full set of apparmor profiles
|
|
|
|
// Copyright (C) 2023 Alexandre Pujol <alexandre@pujol.io>
|
|
|
|
// 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 (
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/roddhjav/apparmor.d/pkg/aa"
|
2023-05-06 14:01:07 +02:00
|
|
|
"golang.org/x/exp/slices"
|
2023-04-19 18:40:40 +02:00
|
|
|
)
|
|
|
|
|
2023-05-06 14:01:07 +02:00
|
|
|
// Build the profiles with the following build tasks
|
|
|
|
var Builds = []BuildFunc{
|
|
|
|
BuildUserspace,
|
|
|
|
}
|
|
|
|
|
2023-04-19 18:40:40 +02:00
|
|
|
var (
|
|
|
|
regAttachments = regexp.MustCompile(`(profile .* @{exec_path})`)
|
|
|
|
regFlag = regexp.MustCompile(`flags=\(([^)]+)\)`)
|
|
|
|
regProfileHeader = regexp.MustCompile(` {`)
|
|
|
|
)
|
|
|
|
|
2023-05-06 14:01:07 +02:00
|
|
|
type BuildFunc func(string) string
|
|
|
|
|
2023-04-19 18:40:40 +02:00
|
|
|
// Set complain flag on all profiles
|
|
|
|
func BuildComplain(profile string) string {
|
|
|
|
flags := []string{}
|
|
|
|
matches := regFlag.FindStringSubmatch(profile)
|
|
|
|
if len(matches) != 0 {
|
|
|
|
flags = strings.Split(matches[1], ",")
|
2023-05-06 14:01:07 +02:00
|
|
|
if slices.Contains(flags, "complain") {
|
2023-04-19 18:40:40 +02:00
|
|
|
return profile
|
|
|
|
}
|
|
|
|
}
|
|
|
|
flags = append(flags, "complain")
|
|
|
|
strFlags := " flags=(" + strings.Join(flags, ",") + ") {"
|
|
|
|
|
|
|
|
// Remove all flags definition, then set manifest' flags
|
|
|
|
profile = regFlag.ReplaceAllLiteralString(profile, "")
|
|
|
|
return regProfileHeader.ReplaceAllLiteralString(profile, strFlags)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bypass userspace tools restriction
|
|
|
|
func BuildUserspace(profile string) string {
|
2023-07-25 23:01:07 +02:00
|
|
|
p := aa.NewAppArmorProfile()
|
|
|
|
p.ParseVariables(profile)
|
2023-04-19 18:40:40 +02:00
|
|
|
p.ResolveAttachments()
|
|
|
|
att := p.NestAttachments()
|
|
|
|
matches := regAttachments.FindAllString(profile, -1)
|
|
|
|
if len(matches) > 0 {
|
|
|
|
strheader := strings.Replace(matches[0], "@{exec_path}", att, -1)
|
|
|
|
return regAttachments.ReplaceAllLiteralString(profile, strheader)
|
|
|
|
}
|
|
|
|
return profile
|
|
|
|
}
|