2023-08-18 00:00:52 +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:00:52 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
|
|
|
package aa
|
|
|
|
|
2023-10-21 00:11:11 +02:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
2023-09-29 21:37:15 +02:00
|
|
|
|
2024-04-23 22:26:09 +02:00
|
|
|
const (
|
|
|
|
tokALL = "all"
|
|
|
|
tokALLOW = "allow"
|
|
|
|
tokAUDIT = "audit"
|
|
|
|
tokDENY = "deny"
|
|
|
|
)
|
|
|
|
|
2024-04-19 23:43:02 +02:00
|
|
|
// Rule generic interface for all AppArmor rules
|
|
|
|
type Rule interface {
|
2024-04-16 22:51:56 +02:00
|
|
|
Less(other any) bool
|
|
|
|
Equals(other any) bool
|
2024-04-23 22:26:09 +02:00
|
|
|
String() string
|
2024-04-16 22:51:56 +02:00
|
|
|
}
|
|
|
|
|
2024-04-19 23:43:02 +02:00
|
|
|
type Rules []Rule
|
2024-04-16 22:51:56 +02:00
|
|
|
|
2024-04-23 22:26:09 +02:00
|
|
|
func (r Rules) String() string {
|
|
|
|
return renderTemplate("rules", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-19 23:43:02 +02:00
|
|
|
type RuleBase struct {
|
2023-10-01 20:06:27 +02:00
|
|
|
Comment string
|
|
|
|
NoNewPrivs bool
|
|
|
|
FileInherit bool
|
2023-10-01 20:00:39 +02:00
|
|
|
Prefix string
|
|
|
|
Padding string
|
2024-04-15 00:58:34 +02:00
|
|
|
Optional bool
|
2023-08-18 00:00:52 +02:00
|
|
|
}
|
|
|
|
|
2024-04-19 23:43:02 +02:00
|
|
|
func newRuleFromLog(log map[string]string) RuleBase {
|
2023-09-29 21:07:29 +02:00
|
|
|
fileInherit := false
|
|
|
|
if log["operation"] == "file_inherit" {
|
|
|
|
fileInherit = true
|
|
|
|
}
|
2023-10-21 00:11:11 +02:00
|
|
|
|
2023-09-29 21:07:29 +02:00
|
|
|
noNewPrivs := false
|
2023-10-21 00:11:11 +02:00
|
|
|
optional := false
|
|
|
|
msg := ""
|
|
|
|
switch log["error"] {
|
|
|
|
case "-1":
|
|
|
|
if strings.Contains(log["info"], "optional:") {
|
|
|
|
optional = true
|
|
|
|
msg = strings.Replace(log["info"], "optional: ", "", 1)
|
|
|
|
} else {
|
|
|
|
noNewPrivs = true
|
|
|
|
}
|
|
|
|
case "-13":
|
|
|
|
ignoreProfileInfo := []string{"namespace", "disconnected path"}
|
|
|
|
for _, info := range ignoreProfileInfo {
|
|
|
|
if strings.Contains(log["info"], info) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
msg = log["info"]
|
|
|
|
default:
|
2023-09-29 21:07:29 +02:00
|
|
|
}
|
2023-10-21 00:11:11 +02:00
|
|
|
|
2024-04-19 23:43:02 +02:00
|
|
|
return RuleBase{
|
2024-04-15 00:58:34 +02:00
|
|
|
Comment: msg,
|
2023-08-18 00:05:07 +02:00
|
|
|
NoNewPrivs: noNewPrivs,
|
|
|
|
FileInherit: fileInherit,
|
2023-10-21 00:11:11 +02:00
|
|
|
Optional: optional,
|
2023-08-18 00:05:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-19 23:43:02 +02:00
|
|
|
func (r RuleBase) Less(other any) bool {
|
2024-04-15 00:58:34 +02:00
|
|
|
return false
|
2023-08-18 00:05:07 +02:00
|
|
|
}
|
|
|
|
|
2024-04-19 23:43:02 +02:00
|
|
|
func (r RuleBase) Equals(other any) bool {
|
2024-04-15 00:58:34 +02:00
|
|
|
return false
|
2023-08-18 00:05:07 +02:00
|
|
|
}
|
|
|
|
|
2024-04-23 22:26:09 +02:00
|
|
|
func (r RuleBase) String() string {
|
|
|
|
return renderTemplate("comment", r)
|
|
|
|
}
|
|
|
|
|
2024-04-15 00:58:34 +02:00
|
|
|
type Qualifier struct {
|
|
|
|
Audit bool
|
|
|
|
AccessType string
|
|
|
|
}
|
2023-09-25 01:06:07 +02:00
|
|
|
|
2024-04-15 00:58:34 +02:00
|
|
|
func newQualifierFromLog(log map[string]string) Qualifier {
|
|
|
|
audit := false
|
|
|
|
if log["apparmor"] == "AUDIT" {
|
|
|
|
audit = true
|
|
|
|
}
|
|
|
|
return Qualifier{Audit: audit}
|
2023-09-25 01:06:07 +02:00
|
|
|
}
|
|
|
|
|
2024-04-15 00:58:34 +02:00
|
|
|
func (r Qualifier) Less(other Qualifier) bool {
|
|
|
|
if r.Audit != other.Audit {
|
|
|
|
return r.Audit
|
2023-09-25 01:06:07 +02:00
|
|
|
}
|
2024-04-15 00:58:34 +02:00
|
|
|
return r.AccessType < other.AccessType
|
2023-09-25 01:06:07 +02:00
|
|
|
}
|
|
|
|
|
2024-04-15 00:58:34 +02:00
|
|
|
func (r Qualifier) Equals(other Qualifier) bool {
|
|
|
|
return r.Audit == other.Audit && r.AccessType == other.AccessType
|
2023-09-25 01:06:07 +02:00
|
|
|
}
|
|
|
|
|
2024-04-15 00:58:34 +02:00
|
|
|
type All struct {
|
2024-04-19 23:43:02 +02:00
|
|
|
RuleBase
|
2023-09-25 01:06:07 +02:00
|
|
|
}
|
|
|
|
|
2024-04-15 00:58:34 +02:00
|
|
|
func (r *All) Less(other any) bool {
|
|
|
|
return false
|
2023-09-25 01:06:07 +02:00
|
|
|
}
|
|
|
|
|
2024-04-15 00:58:34 +02:00
|
|
|
func (r *All) Equals(other any) bool {
|
|
|
|
return false
|
2023-08-18 00:05:07 +02:00
|
|
|
}
|
2024-04-23 22:26:09 +02:00
|
|
|
|
|
|
|
func (r *All) String() string {
|
|
|
|
return renderTemplate(tokALL, r)
|
|
|
|
}
|