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) 2021-2024 Alexandre Pujol <alexandre@pujol.io>
|
2023-04-19 18:40:40 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
|
|
|
package aa
|
2023-08-18 00:12:46 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-09-25 01:17:41 +02:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2023-08-18 00:12:46 +02:00
|
|
|
"strings"
|
|
|
|
|
2024-04-02 18:48:03 +02:00
|
|
|
"github.com/arduino/go-paths-helper"
|
2023-08-18 00:12:46 +02:00
|
|
|
"golang.org/x/exp/slices"
|
|
|
|
)
|
|
|
|
|
2024-04-02 18:48:03 +02:00
|
|
|
// Default Apparmor magic directory: /etc/apparmor.d/.
|
|
|
|
var MagicRoot = paths.New("/etc/apparmor.d")
|
|
|
|
|
2023-08-18 00:00:52 +02:00
|
|
|
// AppArmorProfiles represents a full set of apparmor profiles
|
|
|
|
type AppArmorProfiles map[string]*AppArmorProfile
|
2023-04-19 18:40:40 +02:00
|
|
|
|
2023-08-18 00:00:52 +02:00
|
|
|
// ApparmorProfile represents a full apparmor profile.
|
|
|
|
// Warning: close to the BNF grammar of apparmor profile but not exactly the same (yet):
|
|
|
|
// - Some rules are not supported yet (subprofile, hat...)
|
2023-09-01 20:26:52 +02:00
|
|
|
// - The structure is simplified as it only aims at writing profile, not parsing it.
|
2023-04-19 18:40:40 +02:00
|
|
|
type AppArmorProfile struct {
|
2023-08-18 00:00:52 +02:00
|
|
|
Preamble
|
|
|
|
Profile
|
2023-04-19 18:40:40 +02:00
|
|
|
}
|
|
|
|
|
2023-09-25 01:06:07 +02:00
|
|
|
// Preamble section of a profile
|
|
|
|
type Preamble struct {
|
|
|
|
Abi []Abi
|
|
|
|
Includes []Include
|
|
|
|
Aliases []Alias
|
|
|
|
Variables []Variable
|
|
|
|
}
|
|
|
|
|
|
|
|
// Profile section of a profile
|
|
|
|
type Profile struct {
|
|
|
|
Name string
|
|
|
|
Attachments []string
|
|
|
|
Attributes map[string]string
|
|
|
|
Flags []string
|
|
|
|
Rules Rules
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApparmorRule generic interface
|
|
|
|
type ApparmorRule interface {
|
|
|
|
Less(other any) bool
|
|
|
|
Equals(other any) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type Rules []ApparmorRule
|
|
|
|
|
2023-07-25 23:01:07 +02:00
|
|
|
func NewAppArmorProfile() *AppArmorProfile {
|
2023-08-18 00:00:52 +02:00
|
|
|
return &AppArmorProfile{}
|
2023-04-19 18:40:40 +02:00
|
|
|
}
|
2023-08-18 00:11:11 +02:00
|
|
|
|
|
|
|
// String returns the formatted representation of a profile as a string
|
|
|
|
func (p *AppArmorProfile) String() string {
|
|
|
|
var res bytes.Buffer
|
|
|
|
err := tmplAppArmorProfile.Execute(&res, p)
|
|
|
|
if err != nil {
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
return res.String()
|
|
|
|
}
|
|
|
|
|
2023-08-18 00:12:46 +02:00
|
|
|
// AddRule adds a new rule to the profile from a log map
|
|
|
|
func (p *AppArmorProfile) AddRule(log map[string]string) {
|
2023-10-21 00:11:11 +02:00
|
|
|
// Generate profile flags and extra rules
|
2023-08-18 00:12:46 +02:00
|
|
|
switch log["error"] {
|
|
|
|
case "-2":
|
|
|
|
if !slices.Contains(p.Flags, "mediate_deleted") {
|
|
|
|
p.Flags = append(p.Flags, "mediate_deleted")
|
|
|
|
}
|
|
|
|
case "-13":
|
2023-10-21 00:11:11 +02:00
|
|
|
if strings.Contains(log["info"], "namespace creation restricted") {
|
|
|
|
p.Rules = append(p.Rules, UsernsFromLog(log))
|
|
|
|
} else if strings.Contains(log["info"], "disconnected path") && !slices.Contains(p.Flags, "attach_disconnected") {
|
2023-08-18 00:12:46 +02:00
|
|
|
p.Flags = append(p.Flags, "attach_disconnected")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
switch log["class"] {
|
|
|
|
case "cap":
|
2023-09-29 21:07:29 +02:00
|
|
|
p.Rules = append(p.Rules, CapabilityFromLog(log))
|
2023-08-18 00:12:46 +02:00
|
|
|
case "net":
|
2024-02-29 01:20:37 +01:00
|
|
|
if log["family"] == "unix" {
|
|
|
|
p.Rules = append(p.Rules, UnixFromLog(log))
|
|
|
|
} else {
|
|
|
|
p.Rules = append(p.Rules, NetworkFromLog(log))
|
|
|
|
}
|
2023-09-25 01:10:12 +02:00
|
|
|
case "mount":
|
2024-03-01 00:12:19 +01:00
|
|
|
if strings.Contains(log["flags"], "remount") {
|
2023-11-19 11:59:46 +01:00
|
|
|
p.Rules = append(p.Rules, RemountFromLog(log))
|
2024-03-01 00:12:19 +01:00
|
|
|
} else {
|
|
|
|
switch log["operation"] {
|
|
|
|
case "mount":
|
|
|
|
p.Rules = append(p.Rules, MountFromLog(log))
|
|
|
|
case "umount":
|
|
|
|
p.Rules = append(p.Rules, UmountFromLog(log))
|
|
|
|
case "remount":
|
|
|
|
p.Rules = append(p.Rules, RemountFromLog(log))
|
|
|
|
case "pivotroot":
|
|
|
|
p.Rules = append(p.Rules, PivotRootFromLog(log))
|
|
|
|
}
|
2023-11-19 11:59:46 +01:00
|
|
|
}
|
2023-12-05 21:47:32 +01:00
|
|
|
case "posix_mqueue", "sysv_mqueue":
|
2023-09-29 21:07:29 +02:00
|
|
|
p.Rules = append(p.Rules, MqueueFromLog(log))
|
2023-08-18 00:12:46 +02:00
|
|
|
case "signal":
|
2023-09-29 21:07:29 +02:00
|
|
|
p.Rules = append(p.Rules, SignalFromLog(log))
|
2023-08-18 00:12:46 +02:00
|
|
|
case "ptrace":
|
2023-09-29 21:07:29 +02:00
|
|
|
p.Rules = append(p.Rules, PtraceFromLog(log))
|
2023-09-25 01:10:12 +02:00
|
|
|
case "namespace":
|
2023-09-29 21:07:29 +02:00
|
|
|
p.Rules = append(p.Rules, UsernsFromLog(log))
|
2023-08-18 00:12:46 +02:00
|
|
|
case "unix":
|
2023-09-29 21:07:29 +02:00
|
|
|
p.Rules = append(p.Rules, UnixFromLog(log))
|
2023-09-25 01:10:12 +02:00
|
|
|
case "file":
|
2023-11-19 11:59:46 +01:00
|
|
|
if log["operation"] == "change_onexec" {
|
|
|
|
p.Rules = append(p.Rules, ChangeProfileFromLog(log))
|
|
|
|
} else {
|
|
|
|
p.Rules = append(p.Rules, FileFromLog(log))
|
|
|
|
}
|
2023-08-18 00:12:46 +02:00
|
|
|
default:
|
|
|
|
if strings.Contains(log["operation"], "dbus") {
|
2023-09-29 21:07:29 +02:00
|
|
|
p.Rules = append(p.Rules, DbusFromLog(log))
|
2023-08-18 00:12:46 +02:00
|
|
|
} else if log["family"] == "unix" {
|
2023-09-29 21:07:29 +02:00
|
|
|
p.Rules = append(p.Rules, UnixFromLog(log))
|
2023-08-18 00:12:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 01:17:41 +02:00
|
|
|
// Sort the rules in the profile
|
2023-10-01 20:00:39 +02:00
|
|
|
// Follow: https://apparmor.pujol.io/development/guidelines/#guidelines
|
2023-09-25 01:17:41 +02:00
|
|
|
func (p *AppArmorProfile) Sort() {
|
|
|
|
sort.Slice(p.Rules, func(i, j int) bool {
|
|
|
|
typeOfI := reflect.TypeOf(p.Rules[i])
|
|
|
|
typeOfJ := reflect.TypeOf(p.Rules[j])
|
|
|
|
if typeOfI != typeOfJ {
|
|
|
|
valueOfI := typeToValue(typeOfI)
|
|
|
|
valueOfJ := typeToValue(typeOfJ)
|
2023-09-29 22:24:15 +02:00
|
|
|
if typeOfI == reflect.TypeOf((*Include)(nil)) && p.Rules[i].(*Include).IfExists {
|
|
|
|
valueOfI = "include_if_exists"
|
|
|
|
}
|
|
|
|
if typeOfJ == reflect.TypeOf((*Include)(nil)) && p.Rules[j].(*Include).IfExists {
|
|
|
|
valueOfJ = "include_if_exists"
|
|
|
|
}
|
2023-09-25 01:17:41 +02:00
|
|
|
return ruleWeights[valueOfI] < ruleWeights[valueOfJ]
|
|
|
|
}
|
|
|
|
return p.Rules[i].Less(p.Rules[j])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// MergeRules merge similar rules together
|
|
|
|
// Steps:
|
|
|
|
// - Remove identical rules
|
|
|
|
// - Merge rule access. Eg: for same path, 'r' and 'w' becomes 'rw'
|
|
|
|
//
|
|
|
|
// Note: logs.regCleanLogs helps a lot to do a first cleaning
|
|
|
|
func (p *AppArmorProfile) MergeRules() {
|
|
|
|
for i := 0; i < len(p.Rules); i++ {
|
|
|
|
for j := i + 1; j < len(p.Rules); j++ {
|
|
|
|
typeOfI := reflect.TypeOf(p.Rules[i])
|
|
|
|
typeOfJ := reflect.TypeOf(p.Rules[j])
|
|
|
|
if typeOfI != typeOfJ {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If rules are identical, merge them
|
|
|
|
if p.Rules[i].Equals(p.Rules[j]) {
|
|
|
|
p.Rules = append(p.Rules[:j], p.Rules[j+1:]...)
|
|
|
|
j--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-01 20:00:39 +02:00
|
|
|
|
|
|
|
// Format the profile for better readability before printing it
|
|
|
|
// Follow: https://apparmor.pujol.io/development/guidelines/#the-file-block
|
|
|
|
func (p *AppArmorProfile) Format() {
|
2024-04-03 22:06:28 +02:00
|
|
|
const prefixOwner = " "
|
|
|
|
hasOwnerRule := false
|
2023-10-01 20:00:39 +02:00
|
|
|
for i := len(p.Rules) - 1; i > 0; i-- {
|
|
|
|
j := i - 1
|
|
|
|
typeOfI := reflect.TypeOf(p.Rules[i])
|
|
|
|
typeOfJ := reflect.TypeOf(p.Rules[j])
|
|
|
|
|
|
|
|
// File rule
|
|
|
|
if typeOfI == reflect.TypeOf((*File)(nil)) && typeOfJ == reflect.TypeOf((*File)(nil)) {
|
|
|
|
letterI := getLetterIn(fileAlphabet, p.Rules[i].(*File).Path)
|
|
|
|
letterJ := getLetterIn(fileAlphabet, p.Rules[j].(*File).Path)
|
|
|
|
|
|
|
|
// Add prefix before rule path to align with other rule
|
|
|
|
if p.Rules[i].(*File).Owner {
|
2024-04-03 22:06:28 +02:00
|
|
|
hasOwnerRule = true
|
|
|
|
} else if hasOwnerRule {
|
|
|
|
p.Rules[i].(*File).Prefix = prefixOwner
|
2023-10-01 20:00:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if letterI != letterJ {
|
|
|
|
// Add a new empty line between Files rule of different type
|
2024-04-03 22:06:28 +02:00
|
|
|
hasOwnerRule = false
|
2023-10-01 20:00:39 +02:00
|
|
|
p.Rules = append(p.Rules[:i], append([]ApparmorRule{&Rule{}}, p.Rules[i:]...)...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|