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-05-25 23:36:39 +02:00
|
|
|
"fmt"
|
2024-04-15 15:09:04 +02:00
|
|
|
"maps"
|
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 (
|
2024-05-28 19:15:22 +02:00
|
|
|
PROFILE Kind = "profile"
|
|
|
|
|
2024-04-23 22:26:09 +02:00
|
|
|
tokATTRIBUTES = "xattrs"
|
|
|
|
tokFLAGS = "flags"
|
|
|
|
)
|
|
|
|
|
2024-05-25 23:36:39 +02:00
|
|
|
func init() {
|
2024-05-28 19:15:22 +02:00
|
|
|
requirements[PROFILE] = requirement{
|
2024-05-25 23:36:39 +02:00
|
|
|
tokFLAGS: {
|
|
|
|
"enforce", "complain", "kill", "default_allow", "unconfined",
|
|
|
|
"prompt", "audit", "mediate_deleted", "attach_disconnected",
|
|
|
|
"attach_disconneced.path=", "chroot_relative", "debug",
|
|
|
|
"interruptible", "kill", "kill.signal=",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-05-27 19:55:21 +02:00
|
|
|
func newHeader(rule []string) (Header, error) {
|
|
|
|
if len(rule) == 0 {
|
|
|
|
return Header{}, nil
|
|
|
|
}
|
|
|
|
if rule[len(rule)-1] == "{" {
|
|
|
|
rule = rule[:len(rule)-1]
|
|
|
|
}
|
2024-05-28 19:15:22 +02:00
|
|
|
if rule[0] == PROFILE.Tok() {
|
2024-05-27 19:55:21 +02:00
|
|
|
rule = rule[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
delete := []int{}
|
|
|
|
flags := []string{}
|
|
|
|
attributes := make(map[string]string)
|
|
|
|
for idx, token := range rule {
|
|
|
|
if item, ok := strings.CutPrefix(token, tokFLAGS+"="); ok {
|
|
|
|
flags = tokenToSlice(item)
|
|
|
|
delete = append(delete, idx)
|
|
|
|
} else if item, ok := strings.CutPrefix(token, tokATTRIBUTES+"="); ok {
|
|
|
|
for _, m := range tokenToSlice(item) {
|
|
|
|
kv := strings.SplitN(m, "=", 2)
|
|
|
|
attributes[kv[0]] = kv[1]
|
|
|
|
}
|
|
|
|
delete = append(delete, idx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := len(delete) - 1; i >= 0; i-- {
|
|
|
|
rule = slices.Delete(rule, delete[i], delete[i]+1)
|
|
|
|
}
|
|
|
|
|
|
|
|
name, attachments := "", []string{}
|
|
|
|
if len(rule) >= 1 {
|
|
|
|
name = rule[0]
|
|
|
|
if len(rule) > 1 {
|
|
|
|
attachments = rule[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Header{
|
|
|
|
Name: name,
|
|
|
|
Attachments: attachments,
|
|
|
|
Attributes: attributes,
|
|
|
|
Flags: flags,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-05-25 23:36:39 +02:00
|
|
|
func (r *Profile) Validate() error {
|
|
|
|
if err := validateValues(r.Kind(), tokFLAGS, r.Flags); err != nil {
|
|
|
|
return fmt.Errorf("profile %s: %w", r.Name, err)
|
|
|
|
}
|
|
|
|
return r.Rules.Validate()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-05-28 19:15:22 +02:00
|
|
|
func (p *Profile) Kind() Kind {
|
|
|
|
return PROFILE
|
2024-04-23 22:26:09 +02:00
|
|
|
}
|
|
|
|
|
2024-04-23 22:32:58 +02:00
|
|
|
func (p *Profile) Merge() {
|
2024-05-28 19:20:52 +02:00
|
|
|
slices.Sort(p.Flags)
|
|
|
|
p.Flags = slices.Compact(p.Flags)
|
|
|
|
p.Rules = p.Rules.Merge()
|
2024-04-23 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Profile) Sort() {
|
2024-05-28 19:20:52 +02:00
|
|
|
p.Rules = p.Rules.Sort()
|
2024-04-23 22:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Profile) Format() {
|
2024-05-28 19:20:52 +02:00
|
|
|
p.Rules = p.Rules.Format()
|
2024-04-23 22:32:58 +02:00
|
|
|
}
|
2024-04-23 22:26:09 +02:00
|
|
|
|
2024-05-05 15:09:00 +02:00
|
|
|
// GetAttachments return a nested attachment string
|
|
|
|
func (p *Profile) GetAttachments() string {
|
|
|
|
if len(p.Attachments) == 0 {
|
|
|
|
return ""
|
|
|
|
} else if len(p.Attachments) == 1 {
|
|
|
|
return p.Attachments[0]
|
|
|
|
} else {
|
|
|
|
res := []string{}
|
|
|
|
for _, attachment := range p.Attachments {
|
|
|
|
if strings.HasPrefix(attachment, "/") {
|
|
|
|
res = append(res, attachment[1:])
|
|
|
|
} else {
|
|
|
|
res = append(res, attachment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "/{" + strings.Join(res, ",") + "}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
newLogMap = map[string]func(log map[string]string) Rule{
|
|
|
|
"rlimits": newRlimitFromLog,
|
|
|
|
"cap": newCapabilityFromLog,
|
|
|
|
"io_uring": newIOUringFromLog,
|
|
|
|
"signal": newSignalFromLog,
|
|
|
|
"ptrace": newPtraceFromLog,
|
|
|
|
"namespace": newUsernsFromLog,
|
|
|
|
"unix": newUnixFromLog,
|
|
|
|
"dbus": newDbusFromLog,
|
|
|
|
"posix_mqueue": newMqueueFromLog,
|
|
|
|
"sysv_mqueue": newMqueueFromLog,
|
|
|
|
"mount": func(log map[string]string) Rule {
|
|
|
|
if strings.Contains(log["flags"], "remount") {
|
|
|
|
return newRemountFromLog(log)
|
|
|
|
}
|
|
|
|
newRule := newLogMountMap[log["operation"]]
|
|
|
|
return newRule(log)
|
|
|
|
},
|
|
|
|
"net": func(log map[string]string) Rule {
|
|
|
|
if log["family"] == "unix" {
|
|
|
|
return newUnixFromLog(log)
|
|
|
|
} else {
|
|
|
|
return newNetworkFromLog(log)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"file": func(log map[string]string) Rule {
|
|
|
|
if log["operation"] == "change_onexec" {
|
|
|
|
return newChangeProfileFromLog(log)
|
|
|
|
} else {
|
|
|
|
return newFileFromLog(log)
|
|
|
|
}
|
|
|
|
},
|
2024-05-30 13:34:10 +02:00
|
|
|
"exec": newFileFromLog,
|
|
|
|
"file_inherit": newFileFromLog,
|
|
|
|
"file_perm": newFileFromLog,
|
|
|
|
"open": newFileFromLog,
|
2024-05-05 15:09:00 +02:00
|
|
|
}
|
|
|
|
newLogMountMap = map[string]func(log map[string]string) Rule{
|
|
|
|
"mount": newMountFromLog,
|
|
|
|
"umount": newUmountFromLog,
|
|
|
|
"remount": newRemountFromLog,
|
|
|
|
"pivotroot": newPivotRootFromLog,
|
|
|
|
}
|
|
|
|
)
|
2024-04-23 22:17:25 +02:00
|
|
|
|
2024-05-05 15:09:00 +02:00
|
|
|
func (p *Profile) AddRule(log map[string]string) {
|
2024-04-23 22:17:25 +02:00
|
|
|
// 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:
|
|
|
|
}
|
|
|
|
|
2024-05-30 13:34:10 +02:00
|
|
|
done := false
|
|
|
|
for _, key := range []string{"class", "family", "operation"} {
|
|
|
|
if newRule, ok := newLogMap[log[key]]; ok {
|
|
|
|
p.Rules = append(p.Rules, newRule(log))
|
|
|
|
done = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !done {
|
2024-04-23 22:17:25 +02:00
|
|
|
if strings.Contains(log["operation"], "dbus") {
|
|
|
|
p.Rules = append(p.Rules, newDbusFromLog(log))
|
2024-05-05 15:09:00 +02:00
|
|
|
} else {
|
2024-05-30 13:34:10 +02:00
|
|
|
fmt.Printf("unknown log type: %s", log)
|
2024-04-23 22:17:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|