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 (
|
2024-04-15 15:09:04 +02:00
|
|
|
"maps"
|
2024-04-23 22:32:58 +02:00
|
|
|
"reflect"
|
2024-04-12 21:07:05 +02:00
|
|
|
"slices"
|
2023-08-18 00:12:46 +02:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-04-23 22:26:09 +02:00
|
|
|
const (
|
|
|
|
tokATTRIBUTES = "xattrs"
|
|
|
|
tokFLAGS = "flags"
|
|
|
|
tokPROFILE = "profile"
|
|
|
|
)
|
|
|
|
|
2024-04-16 22:51:56 +02:00
|
|
|
// Profile represents a single AppArmor profile.
|
2023-09-25 01:06:07 +02:00
|
|
|
type Profile struct {
|
2024-04-19 23:43:02 +02:00
|
|
|
RuleBase
|
2024-04-15 15:09:04 +02:00
|
|
|
Header
|
|
|
|
Rules Rules
|
|
|
|
}
|
|
|
|
|
2024-04-16 22:51:56 +02:00
|
|
|
// Header represents the header of a profile.
|
2024-04-15 15:09:04 +02:00
|
|
|
type Header struct {
|
2023-09-25 01:06:07 +02:00
|
|
|
Name string
|
|
|
|
Attachments []string
|
|
|
|
Attributes map[string]string
|
|
|
|
Flags []string
|
2024-04-15 15:09:04 +02:00
|
|
|
}
|
|
|
|
|
2024-04-19 23:43:02 +02:00
|
|
|
func (p *Profile) Less(other any) bool {
|
2024-04-16 22:51:56 +02:00
|
|
|
o, _ := other.(*Profile)
|
2024-04-19 23:43:02 +02:00
|
|
|
if p.Name != o.Name {
|
|
|
|
return p.Name < o.Name
|
2024-04-16 22:51:56 +02:00
|
|
|
}
|
2024-04-19 23:43:02 +02:00
|
|
|
return len(p.Attachments) < len(o.Attachments)
|
2024-04-15 15:09:04 +02:00
|
|
|
}
|
|
|
|
|
2024-04-19 23:43:02 +02:00
|
|
|
func (p *Profile) Equals(other any) bool {
|
2024-04-15 15:09:04 +02:00
|
|
|
o, _ := other.(*Profile)
|
2024-04-19 23:43:02 +02:00
|
|
|
return p.Name == o.Name && slices.Equal(p.Attachments, o.Attachments) &&
|
|
|
|
maps.Equal(p.Attributes, o.Attributes) &&
|
|
|
|
slices.Equal(p.Flags, o.Flags)
|
2023-09-25 01:06:07 +02:00
|
|
|
}
|
2024-04-23 22:17:25 +02:00
|
|
|
|
2024-04-23 22:26:09 +02:00
|
|
|
func (p *Profile) String() string {
|
2024-05-05 00:41:47 +02:00
|
|
|
return renderTemplate(p.Kind(), p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Profile) Constraint() constraint {
|
|
|
|
return blockKind
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Profile) Kind() string {
|
|
|
|
return tokPROFILE
|
2024-04-23 22:26:09 +02:00
|
|
|
}
|
|
|
|
|
2024-04-23 22:32:58 +02:00
|
|
|
// Merge 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 *Profile) Merge() {
|
|
|
|
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--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the rules in a profile.
|
|
|
|
// Follow: https://apparmor.pujol.io/development/guidelines/#guidelines
|
|
|
|
func (p *Profile) Sort() {
|
|
|
|
p.Rules.Sort()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format the profile for better readability before printing it.
|
|
|
|
// Follow: https://apparmor.pujol.io/development/guidelines/#the-file-block
|
|
|
|
func (p *Profile) Format() {
|
|
|
|
const prefixOwner = " "
|
|
|
|
|
|
|
|
hasOwnerRule := false
|
|
|
|
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 {
|
|
|
|
hasOwnerRule = true
|
|
|
|
} else if hasOwnerRule {
|
|
|
|
p.Rules[i].(*File).Prefix = prefixOwner
|
|
|
|
}
|
|
|
|
|
|
|
|
if letterI != letterJ {
|
|
|
|
// Add a new empty line between Files rule of different type
|
|
|
|
hasOwnerRule = false
|
|
|
|
p.Rules = append(p.Rules[:i], append([]Rule{&RuleBase{}}, p.Rules[i:]...)...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-23 22:26:09 +02:00
|
|
|
|
2024-04-23 22:17:25 +02:00
|
|
|
// AddRule adds a new rule to the profile from a log map.
|
|
|
|
func (p *Profile) AddRule(log map[string]string) {
|
|
|
|
|
|
|
|
// Generate profile flags and extra rules
|
|
|
|
switch log["error"] {
|
|
|
|
case "-2":
|
|
|
|
if !slices.Contains(p.Flags, "mediate_deleted") {
|
|
|
|
p.Flags = append(p.Flags, "mediate_deleted")
|
|
|
|
}
|
|
|
|
case "-13":
|
|
|
|
if strings.Contains(log["info"], "namespace creation restricted") {
|
|
|
|
p.Rules = append(p.Rules, newUsernsFromLog(log))
|
|
|
|
} else if strings.Contains(log["info"], "disconnected path") && !slices.Contains(p.Flags, "attach_disconnected") {
|
|
|
|
p.Flags = append(p.Flags, "attach_disconnected")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
switch log["class"] {
|
|
|
|
case "rlimits":
|
|
|
|
p.Rules = append(p.Rules, newRlimitFromLog(log))
|
|
|
|
case "cap":
|
|
|
|
p.Rules = append(p.Rules, newCapabilityFromLog(log))
|
|
|
|
case "net":
|
|
|
|
if log["family"] == "unix" {
|
|
|
|
p.Rules = append(p.Rules, newUnixFromLog(log))
|
|
|
|
} else {
|
|
|
|
p.Rules = append(p.Rules, newNetworkFromLog(log))
|
|
|
|
}
|
|
|
|
case "io_uring":
|
|
|
|
p.Rules = append(p.Rules, newIOUringFromLog(log))
|
|
|
|
case "mount":
|
|
|
|
if strings.Contains(log["flags"], "remount") {
|
|
|
|
p.Rules = append(p.Rules, newRemountFromLog(log))
|
|
|
|
} else {
|
|
|
|
switch log["operation"] {
|
|
|
|
case "mount":
|
|
|
|
p.Rules = append(p.Rules, newMountFromLog(log))
|
|
|
|
case "umount":
|
|
|
|
p.Rules = append(p.Rules, newUmountFromLog(log))
|
|
|
|
case "remount":
|
|
|
|
p.Rules = append(p.Rules, newRemountFromLog(log))
|
|
|
|
case "pivotroot":
|
|
|
|
p.Rules = append(p.Rules, newPivotRootFromLog(log))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "posix_mqueue", "sysv_mqueue":
|
|
|
|
p.Rules = append(p.Rules, newMqueueFromLog(log))
|
|
|
|
case "signal":
|
|
|
|
p.Rules = append(p.Rules, newSignalFromLog(log))
|
|
|
|
case "ptrace":
|
|
|
|
p.Rules = append(p.Rules, newPtraceFromLog(log))
|
|
|
|
case "namespace":
|
|
|
|
p.Rules = append(p.Rules, newUsernsFromLog(log))
|
|
|
|
case "unix":
|
|
|
|
p.Rules = append(p.Rules, newUnixFromLog(log))
|
|
|
|
case "dbus":
|
|
|
|
p.Rules = append(p.Rules, newDbusFromLog(log))
|
|
|
|
case "file":
|
|
|
|
if log["operation"] == "change_onexec" {
|
|
|
|
p.Rules = append(p.Rules, newChangeProfileFromLog(log))
|
|
|
|
} else {
|
|
|
|
p.Rules = append(p.Rules, newFileFromLog(log))
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if strings.Contains(log["operation"], "dbus") {
|
|
|
|
p.Rules = append(p.Rules, newDbusFromLog(log))
|
|
|
|
} else if log["family"] == "unix" {
|
|
|
|
p.Rules = append(p.Rules, newUnixFromLog(log))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|