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-09-29 21:12:00 +02:00
|
|
|
"github.com/roddhjav/apparmor.d/pkg/util"
|
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 (
|
2023-11-22 21:52:25 +01:00
|
|
|
regAttachments = regexp.MustCompile(`(profile .* @{exec_path})`)
|
|
|
|
regFlags = regexp.MustCompile(`flags=\(([^)]+)\)`)
|
|
|
|
regProfileHeader = regexp.MustCompile(` {`)
|
|
|
|
regFullSystemPolicy = util.ToRegexRepl([]string{
|
|
|
|
`r(PU|U)x,`, `rPx,`,
|
|
|
|
})
|
|
|
|
regAbi4To3 = util.ToRegexRepl([]string{ // Currently Abi3 -> Abi4
|
2023-11-19 12:19:24 +01:00
|
|
|
`abi/3.0`, `abi/4.0`,
|
|
|
|
`# userns,`, `userns,`,
|
2023-12-05 21:47:32 +01:00
|
|
|
`# mqueue`, `mqueue`,
|
2023-09-29 21:12:00 +02:00
|
|
|
})
|
2023-04-19 18:40:40 +02:00
|
|
|
)
|
|
|
|
|
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{}
|
2023-09-05 20:44:36 +02:00
|
|
|
matches := regFlags.FindStringSubmatch(profile)
|
2023-04-19 18:40:40 +02:00
|
|
|
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
|
2023-09-05 20:44:36 +02:00
|
|
|
profile = regFlags.ReplaceAllLiteralString(profile, "")
|
|
|
|
return regProfileHeader.ReplaceAllLiteralString(profile, strFlags)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set all profiles in enforce mode
|
|
|
|
func BuildEnforce(profile string) string {
|
|
|
|
matches := regFlags.FindStringSubmatch(profile)
|
|
|
|
if len(matches) == 0 {
|
|
|
|
return profile
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := strings.Split(matches[1], ",")
|
|
|
|
idx := slices.Index(flags, "complain")
|
|
|
|
if idx == -1 {
|
|
|
|
return profile
|
|
|
|
}
|
|
|
|
flags = slices.Delete(flags, idx, idx+1)
|
|
|
|
strFlags := "{"
|
|
|
|
if len(flags) >= 1 {
|
|
|
|
strFlags = " flags=(" + strings.Join(flags, ",") + ") {"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all flags definition, then set new flags
|
|
|
|
profile = regFlags.ReplaceAllLiteralString(profile, "")
|
2023-04-19 18:40:40 +02:00
|
|
|
return regProfileHeader.ReplaceAllLiteralString(profile, strFlags)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bypass userspace tools restriction
|
|
|
|
func BuildUserspace(profile string) string {
|
2023-08-18 00:00:52 +02:00
|
|
|
p := aa.DefaultTunables()
|
2023-07-25 23:01:07 +02:00
|
|
|
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
|
|
|
|
}
|
2023-09-29 21:12:00 +02:00
|
|
|
|
|
|
|
func BuildABI3(profile string) string {
|
|
|
|
for _, abi4t3 := range regAbi4To3 {
|
|
|
|
profile = abi4t3.Regex.ReplaceAllLiteralString(profile, abi4t3.Repl)
|
|
|
|
}
|
|
|
|
return profile
|
|
|
|
}
|
2023-11-22 21:52:25 +01:00
|
|
|
|
|
|
|
func BuildFullSystemPolicy(profile string) string {
|
|
|
|
for _, full := range regFullSystemPolicy {
|
|
|
|
profile = full.Regex.ReplaceAllString(profile, full.Repl)
|
|
|
|
}
|
|
|
|
return profile
|
|
|
|
}
|