2023-08-18 00:05:07 +02:00
|
|
|
// apparmor.d - Full set of apparmor profiles
|
2024-02-07 00:16:21 +01:00
|
|
|
// Copyright (C) 2021-2024 Alexandre Pujol <alexandre@pujol.io>
|
2023-08-18 00:05:07 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
|
|
|
package aa
|
|
|
|
|
|
|
|
import (
|
2023-09-29 21:28:56 +02:00
|
|
|
"embed"
|
2024-04-17 19:02:41 +02:00
|
|
|
"fmt"
|
2023-09-25 01:15:51 +02:00
|
|
|
"strings"
|
2023-08-18 00:11:11 +02:00
|
|
|
"text/template"
|
2023-08-18 00:05:07 +02:00
|
|
|
)
|
|
|
|
|
2023-09-25 01:15:51 +02:00
|
|
|
var (
|
2024-04-23 22:26:09 +02:00
|
|
|
// Default indentation for apparmor profile (2 spaces)
|
2024-05-05 00:54:39 +02:00
|
|
|
Indentation = " "
|
2024-04-23 22:26:09 +02:00
|
|
|
|
|
|
|
// The current indentation level
|
2024-05-05 00:54:39 +02:00
|
|
|
IndentationLevel = 0
|
2024-04-23 22:26:09 +02:00
|
|
|
|
2023-09-29 21:28:56 +02:00
|
|
|
//go:embed templates/*.j2
|
2024-04-23 22:26:09 +02:00
|
|
|
//go:embed templates/rule/*.j2
|
2023-09-29 21:28:56 +02:00
|
|
|
tmplFiles embed.FS
|
2023-08-18 00:11:11 +02:00
|
|
|
|
2023-09-29 21:28:56 +02:00
|
|
|
// The functions available in the template
|
2023-09-25 01:15:51 +02:00
|
|
|
tmplFunctionMap = template.FuncMap{
|
2024-05-28 19:15:22 +02:00
|
|
|
"kindof": kindOf,
|
2023-09-25 01:15:51 +02:00
|
|
|
"join": join,
|
2024-04-23 22:26:09 +02:00
|
|
|
"cjoin": cjoin,
|
2023-09-25 01:15:51 +02:00
|
|
|
"indent": indent,
|
|
|
|
"overindent": indentDbus,
|
2024-04-23 22:26:09 +02:00
|
|
|
"setindent": setindent,
|
2023-09-25 01:15:51 +02:00
|
|
|
}
|
|
|
|
|
2024-04-17 19:02:41 +02:00
|
|
|
// The apparmor templates
|
2024-05-28 19:15:22 +02:00
|
|
|
tmpl = generateTemplates([]Kind{
|
2024-05-25 23:21:59 +02:00
|
|
|
// Global templates
|
2024-10-12 21:11:45 +02:00
|
|
|
"apparmor", PROFILE, HAT, "rules",
|
2024-05-25 23:21:59 +02:00
|
|
|
|
|
|
|
// Preamble templates
|
2024-10-12 21:11:45 +02:00
|
|
|
ABI, ALIAS, INCLUDE, VARIABLE, COMMENT,
|
2024-05-25 23:21:59 +02:00
|
|
|
|
|
|
|
// Rules templates
|
2024-05-28 19:15:22 +02:00
|
|
|
ALL, RLIMIT, USERNS, CAPABILITY, NETWORK,
|
|
|
|
MOUNT, REMOUNT, UMOUNT, PIVOTROOT, CHANGEPROFILE,
|
|
|
|
MQUEUE, IOURING, UNIX, PTRACE, SIGNAL, DBUS,
|
|
|
|
FILE, LINK,
|
2024-04-23 22:26:09 +02:00
|
|
|
})
|
2023-09-25 01:15:51 +02:00
|
|
|
|
|
|
|
// convert apparmor requested mask to apparmor access mode
|
2024-04-23 22:17:25 +02:00
|
|
|
maskToAccess = map[string]string{
|
2024-05-25 23:14:43 +02:00
|
|
|
"a": "w",
|
|
|
|
"c": "w",
|
|
|
|
"d": "w",
|
|
|
|
"wc": "w",
|
|
|
|
"x": "ix",
|
2023-09-25 01:15:51 +02:00
|
|
|
}
|
|
|
|
|
2023-09-25 01:17:41 +02:00
|
|
|
// The order the apparmor rules should be sorted
|
2024-05-28 19:15:22 +02:00
|
|
|
ruleAlphabet = []Kind{
|
|
|
|
INCLUDE,
|
|
|
|
ALL,
|
|
|
|
RLIMIT,
|
|
|
|
USERNS,
|
|
|
|
CAPABILITY,
|
|
|
|
NETWORK,
|
|
|
|
MOUNT,
|
|
|
|
REMOUNT,
|
|
|
|
UMOUNT,
|
|
|
|
PIVOTROOT,
|
|
|
|
CHANGEPROFILE,
|
|
|
|
MQUEUE,
|
|
|
|
IOURING,
|
|
|
|
SIGNAL,
|
|
|
|
PTRACE,
|
|
|
|
UNIX,
|
|
|
|
DBUS,
|
|
|
|
FILE,
|
|
|
|
LINK,
|
|
|
|
PROFILE,
|
|
|
|
HAT,
|
2023-09-29 22:24:15 +02:00
|
|
|
"include_if_exists",
|
2023-09-25 01:17:41 +02:00
|
|
|
}
|
2024-05-25 23:01:29 +02:00
|
|
|
ruleWeights = generateWeights(ruleAlphabet)
|
2023-09-25 01:17:41 +02:00
|
|
|
|
|
|
|
// 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
|
2024-06-20 00:30:36 +02:00
|
|
|
"@{coreutils_path}", // 2.2 coreutils
|
|
|
|
"@{open_path}", // 2.3 binaries paths
|
|
|
|
"@{bin}", // 2.3 binaries
|
|
|
|
"@{lib}", // 2.4 libraries
|
|
|
|
"/opt", // 2.5 opt binaries & libraries
|
2023-09-25 01:17:41 +02:00
|
|
|
"/usr/share", // 3. shared data
|
|
|
|
"/etc", // 4. system configuration
|
2023-09-30 14:54:04 +02:00
|
|
|
"/var", // 5.1 system read/write data
|
|
|
|
"/boot", // 5.2 boot files
|
2023-09-25 01:17:41 +02:00
|
|
|
"/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
|
2024-06-20 00:30:36 +02:00
|
|
|
"@{tmp}", // 8.1. User temporary data
|
|
|
|
"/dev/shm", // 8.2 Shared memory
|
|
|
|
"@{run}", // 8.3 Runtime data
|
2023-09-25 01:17:41 +02:00
|
|
|
"@{sys}", // 9. Sys files
|
|
|
|
"@{PROC}", // 10. Proc files
|
|
|
|
"/dev", // 11. Dev files
|
|
|
|
"deny", // 12. Deny rules
|
2024-05-05 15:19:25 +02:00
|
|
|
"profile", // 13. Subprofiles
|
2023-09-25 01:17:41 +02:00
|
|
|
}
|
2024-05-25 23:01:29 +02:00
|
|
|
fileWeights = generateWeights(fileAlphabet)
|
|
|
|
|
2024-06-20 00:30:36 +02:00
|
|
|
// Some file rule should be sorted in the same group
|
|
|
|
fileAlphabetGroups = map[string]string{
|
|
|
|
"@{exec_path}": "exec",
|
|
|
|
"@{sh_path}": "exec",
|
|
|
|
"@{coreutils_path}": "exec",
|
|
|
|
"@{open_path}": "exec",
|
|
|
|
"@{bin}": "exec",
|
|
|
|
"@{lib}": "exec",
|
|
|
|
"/opt": "exec",
|
2024-06-23 11:42:18 +02:00
|
|
|
"/home": "home",
|
|
|
|
"@{HOME}": "home",
|
2024-06-20 00:30:36 +02:00
|
|
|
"/tmp": "tmp",
|
|
|
|
"@{tmp}": "tmp",
|
|
|
|
"/dev/shm": "tmp",
|
|
|
|
}
|
|
|
|
|
2024-06-19 19:34:58 +02:00
|
|
|
// The order AARE should be sorted
|
|
|
|
stringAlphabet = []byte(
|
2024-09-26 23:15:46 +02:00
|
|
|
"!\"#$%&'*(){}[]@+,-./:;<=>?\\^_`|~0123456789abcdefghijklmnopqrstuvwxyz",
|
2024-06-19 19:34:58 +02:00
|
|
|
)
|
|
|
|
stringWeights = generateWeights(stringAlphabet)
|
|
|
|
|
2024-05-25 23:01:29 +02:00
|
|
|
// The order the rule values (access, type, domains, etc) should be sorted
|
2024-05-28 19:15:22 +02:00
|
|
|
requirements = map[Kind]requirement{}
|
|
|
|
requirementsWeights map[Kind]map[string]map[string]int
|
2023-09-25 01:15:51 +02:00
|
|
|
)
|
|
|
|
|
2024-05-25 23:01:29 +02:00
|
|
|
func init() {
|
|
|
|
requirementsWeights = generateRequirementsWeights(requirements)
|
|
|
|
}
|
|
|
|
|
2024-05-28 19:15:22 +02:00
|
|
|
func generateTemplates(names []Kind) map[Kind]*template.Template {
|
|
|
|
res := make(map[Kind]*template.Template, len(names))
|
2024-04-23 22:26:09 +02:00
|
|
|
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),
|
2024-04-17 19:02:41 +02:00
|
|
|
))
|
2024-04-23 22:26:09 +02:00
|
|
|
res[name] = t
|
2024-04-17 19:02:41 +02:00
|
|
|
}
|
2023-09-29 21:28:56 +02:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2024-05-28 19:15:22 +02:00
|
|
|
func renderTemplate(name Kind, data any) string {
|
2024-04-23 22:26:09 +02:00
|
|
|
var res strings.Builder
|
|
|
|
template, ok := tmpl[name]
|
|
|
|
if !ok {
|
2024-05-28 19:15:22 +02:00
|
|
|
panic("template '" + name.String() + "' not found")
|
2024-04-23 22:26:09 +02:00
|
|
|
}
|
|
|
|
err := template.Execute(&res, data)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return res.String()
|
|
|
|
}
|
|
|
|
|
2024-06-19 19:34:58 +02:00
|
|
|
func generateWeights[T comparable](alphabet []T) map[T]int {
|
2024-05-28 19:15:22 +02:00
|
|
|
res := make(map[T]int, len(alphabet))
|
2024-05-25 23:01:29 +02:00
|
|
|
for i, r := range alphabet {
|
|
|
|
res[r] = i
|
2023-09-25 01:17:41 +02:00
|
|
|
}
|
2024-05-25 23:01:29 +02:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2024-05-28 19:15:22 +02:00
|
|
|
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)
|
|
|
|
}
|
2023-09-25 01:17:41 +02:00
|
|
|
}
|
2024-05-25 23:01:29 +02:00
|
|
|
return res
|
2023-09-25 01:17:41 +02:00
|
|
|
}
|
2023-09-25 01:15:51 +02:00
|
|
|
|
|
|
|
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:
|
2023-09-25 01:15:51 +02:00
|
|
|
res := []string{}
|
2024-05-30 21:56:53 +02:00
|
|
|
for k, v := range i {
|
2023-09-25 01:15:51 +02:00
|
|
|
res = append(res, k+"="+v)
|
|
|
|
}
|
|
|
|
return strings.Join(res, " ")
|
|
|
|
default:
|
|
|
|
return i.(string)
|
|
|
|
}
|
2023-08-18 00:11:11 +02:00
|
|
|
}
|
|
|
|
|
2024-04-23 22:26:09 +02:00
|
|
|
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-04-23 22:26:09 +02:00
|
|
|
}
|
2024-05-30 21:56:53 +02:00
|
|
|
return "(" + strings.Join(i, " ") + ")"
|
|
|
|
case map[string]string:
|
2024-04-23 22:26:09 +02:00
|
|
|
res := []string{}
|
2024-05-30 21:56:53 +02:00
|
|
|
for k, v := range i {
|
2024-04-23 22:26:09 +02:00
|
|
|
res = append(res, k+"="+v)
|
|
|
|
}
|
|
|
|
return "(" + strings.Join(res, " ") + ")"
|
|
|
|
default:
|
|
|
|
return i.(string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-26 23:15:46 +02:00
|
|
|
func kindOf(i Rule) string {
|
2024-05-05 00:25:55 +02:00
|
|
|
if i == nil {
|
|
|
|
return ""
|
|
|
|
}
|
2024-09-26 23:15:46 +02:00
|
|
|
return i.Kind().String()
|
2023-10-01 20:00:39 +02:00
|
|
|
}
|
|
|
|
|
2024-04-23 22:26:09 +02:00
|
|
|
func setindent(i string) string {
|
|
|
|
switch i {
|
|
|
|
case "++":
|
2024-05-05 00:54:39 +02:00
|
|
|
IndentationLevel++
|
2024-04-23 22:26:09 +02:00
|
|
|
case "--":
|
2024-05-05 00:54:39 +02:00
|
|
|
IndentationLevel--
|
2024-04-23 22:26:09 +02:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-08-18 00:11:11 +02:00
|
|
|
func indent(s string) string {
|
2024-05-05 00:54:39 +02:00
|
|
|
return strings.Repeat(Indentation, IndentationLevel) + s
|
2023-08-18 00:11:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func indentDbus(s string) string {
|
2024-05-05 00:54:39 +02:00
|
|
|
return strings.Join([]string{Indentation, s}, " ")
|
2023-08-18 00:11:11 +02:00
|
|
|
}
|
2023-10-01 20:00:39 +02:00
|
|
|
|
|
|
|
func getLetterIn(alphabet []string, in string) string {
|
|
|
|
for _, letter := range alphabet {
|
|
|
|
if strings.HasPrefix(in, letter) {
|
|
|
|
return letter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|