apparmor.d/pkg/aa/template.go

243 lines
5.2 KiB
Go
Raw Normal View History

// apparmor.d - Full set of apparmor profiles
// Copyright (C) 2021-2024 Alexandre Pujol <alexandre@pujol.io>
// SPDX-License-Identifier: GPL-2.0-only
package aa
import (
2023-09-29 21:28:56 +02:00
"embed"
"fmt"
"strings"
"text/template"
)
var (
// Default indentation for apparmor profile (2 spaces)
2024-05-05 00:54:39 +02:00
Indentation = " "
// The current indentation level
2024-05-05 00:54:39 +02:00
IndentationLevel = 0
2023-09-29 21:28:56 +02:00
//go:embed templates/*.j2
//go:embed templates/rule/*.j2
2023-09-29 21:28:56 +02:00
tmplFiles embed.FS
2023-09-29 21:28:56 +02:00
// The functions available in the template
tmplFunctionMap = template.FuncMap{
"kindof": kindOf,
"join": join,
"cjoin": cjoin,
"indent": indent,
"overindent": indentDbus,
"setindent": setindent,
}
// The apparmor templates
tmpl = generateTemplates([]Kind{
2024-05-25 23:21:59 +02:00
// Global templates
"apparmor",
PROFILE,
HAT,
2024-05-25 23:21:59 +02:00
"rules",
// Preamble templates
ABI,
ALIAS,
INCLUDE,
VARIABLE,
COMMENT,
2024-05-25 23:21:59 +02:00
// Rules templates
ALL, RLIMIT, USERNS, CAPABILITY, NETWORK,
MOUNT, REMOUNT, UMOUNT, PIVOTROOT, CHANGEPROFILE,
MQUEUE, IOURING, UNIX, PTRACE, SIGNAL, DBUS,
FILE, LINK,
})
// convert apparmor requested mask to apparmor access mode
maskToAccess = map[string]string{
"a": "w",
"c": "w",
"d": "w",
"wc": "w",
"x": "ix",
}
// The order the apparmor rules should be sorted
ruleAlphabet = []Kind{
INCLUDE,
ALL,
RLIMIT,
USERNS,
CAPABILITY,
NETWORK,
MOUNT,
REMOUNT,
UMOUNT,
PIVOTROOT,
CHANGEPROFILE,
MQUEUE,
IOURING,
SIGNAL,
PTRACE,
UNIX,
DBUS,
FILE,
LINK,
PROFILE,
HAT,
"include_if_exists",
}
2024-05-25 23:01:29 +02:00
ruleWeights = generateWeights(ruleAlphabet)
// The order the apparmor file rules should be sorted
fileAlphabet = []string{
"@{exec_path}", // 1. entry point
2024-03-01 00:14:01 +01:00
"@{sh_path}", // 2.1 shells
"@{bin}", // 2.1 binaries
"@{lib}", // 2.2 libraries
"/opt", // 2.3 opt binaries & libraries
"/usr/share", // 3. shared data
"/etc", // 4. system configuration
"/var", // 5.1 system read/write data
"/boot", // 5.2 boot files
"/home", // 6.1 user data
"@{HOME}", // 6.2 home files
"@{user_cache_dirs}", // 7.1 user caches
"@{user_config_dirs}", // 7.2 user config
"@{user_share_dirs}", // 7.3 user shared
"/tmp", // 8.1 Temporary data
"@{run}", // 8.2 Runtime data
"/dev/shm", // 8.3 Shared memory
"@{sys}", // 9. Sys files
"@{PROC}", // 10. Proc files
"/dev", // 11. Dev files
"deny", // 12. Deny rules
"profile", // 13. Subprofiles
}
2024-05-25 23:01:29 +02:00
fileWeights = generateWeights(fileAlphabet)
// The order the rule values (access, type, domains, etc) should be sorted
requirements = map[Kind]requirement{}
requirementsWeights map[Kind]map[string]map[string]int
)
2024-05-25 23:01:29 +02:00
func init() {
requirementsWeights = generateRequirementsWeights(requirements)
}
func generateTemplates(names []Kind) map[Kind]*template.Template {
res := make(map[Kind]*template.Template, len(names))
base := template.New("").Funcs(tmplFunctionMap)
base = template.Must(base.ParseFS(tmplFiles,
"templates/*.j2", "templates/rule/*.j2",
))
for _, name := range names {
t := template.Must(base.Clone())
t = template.Must(t.Parse(
fmt.Sprintf(`{{- template "%s" . -}}`, name),
))
res[name] = t
}
2023-09-29 21:28:56 +02:00
return res
}
func renderTemplate(name Kind, data any) string {
var res strings.Builder
template, ok := tmpl[name]
if !ok {
panic("template '" + name.String() + "' not found")
}
err := template.Execute(&res, data)
if err != nil {
panic(err)
}
return res.String()
}
func generateWeights[T Kind | string](alphabet []T) map[T]int {
res := make(map[T]int, len(alphabet))
2024-05-25 23:01:29 +02:00
for i, r := range alphabet {
res[r] = i
}
2024-05-25 23:01:29 +02:00
return res
}
func generateRequirementsWeights(requirements map[Kind]requirement) map[Kind]map[string]map[string]int {
res := make(map[Kind]map[string]map[string]int, len(requirements))
2024-05-25 23:01:29 +02:00
for rule, req := range requirements {
res[rule] = make(map[string]map[string]int, len(req))
for key, values := range req {
res[rule][key] = generateWeights(values)
}
}
2024-05-25 23:01:29 +02:00
return res
}
func join(i any) string {
2024-05-30 21:56:53 +02:00
switch i := i.(type) {
case []string:
return strings.Join(i, " ")
case map[string]string:
res := []string{}
2024-05-30 21:56:53 +02:00
for k, v := range i {
res = append(res, k+"="+v)
}
return strings.Join(res, " ")
default:
return i.(string)
}
}
func cjoin(i any) string {
2024-05-30 21:56:53 +02:00
switch i := i.(type) {
case []string:
if len(i) == 1 {
return i[0]
}
2024-05-30 21:56:53 +02:00
return "(" + strings.Join(i, " ") + ")"
case map[string]string:
res := []string{}
2024-05-30 21:56:53 +02:00
for k, v := range i {
res = append(res, k+"="+v)
}
return "(" + strings.Join(res, " ") + ")"
default:
return i.(string)
}
}
func kindOf(i any) string {
if i == nil {
return ""
}
return i.(Rule).Kind().String()
}
func setindent(i string) string {
switch i {
case "++":
2024-05-05 00:54:39 +02:00
IndentationLevel++
case "--":
2024-05-05 00:54:39 +02:00
IndentationLevel--
}
return ""
}
func indent(s string) string {
2024-05-05 00:54:39 +02:00
return strings.Repeat(Indentation, IndentationLevel) + s
}
func indentDbus(s string) string {
2024-05-05 00:54:39 +02:00
return strings.Join([]string{Indentation, s}, " ")
}
func getLetterIn(alphabet []string, in string) string {
for _, letter := range alphabet {
if strings.HasPrefix(in, letter) {
return letter
}
}
return ""
}