2024-04-24 14:31:22 +02:00
|
|
|
// apparmor.d - Full set of apparmor profiles
|
|
|
|
// Copyright (C) 2021-2024 Alexandre Pujol <alexandre@pujol.io>
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
|
|
|
package aa
|
|
|
|
|
2024-05-05 15:19:25 +02:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
2024-04-24 14:31:22 +02:00
|
|
|
|
|
|
|
type RuleBase struct {
|
|
|
|
IsLineRule bool
|
|
|
|
Comment string
|
|
|
|
NoNewPrivs bool
|
|
|
|
FileInherit bool
|
|
|
|
Prefix string
|
|
|
|
Padding string
|
|
|
|
Optional bool
|
|
|
|
}
|
|
|
|
|
2024-05-27 19:55:21 +02:00
|
|
|
func newRule(rule []string) RuleBase {
|
|
|
|
comment := ""
|
|
|
|
fileInherit, noNewPrivs, optional := false, false, false
|
|
|
|
|
|
|
|
idx := 0
|
|
|
|
for idx < len(rule) {
|
2024-05-28 19:15:22 +02:00
|
|
|
if rule[idx] == COMMENT.Tok() {
|
2024-05-27 19:55:21 +02:00
|
|
|
comment = " " + strings.Join(rule[idx+1:], " ")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case strings.Contains(comment, "file_inherit"):
|
|
|
|
fileInherit = true
|
|
|
|
comment = strings.Replace(comment, "file_inherit ", "", 1)
|
|
|
|
case strings.HasPrefix(comment, "no new privs"):
|
|
|
|
noNewPrivs = true
|
|
|
|
comment = strings.Replace(comment, "no new privs ", "", 1)
|
|
|
|
case strings.Contains(comment, "optional:"):
|
|
|
|
optional = true
|
|
|
|
comment = strings.Replace(comment, "optional: ", "", 1)
|
|
|
|
}
|
|
|
|
return RuleBase{
|
|
|
|
Comment: comment,
|
|
|
|
NoNewPrivs: noNewPrivs,
|
|
|
|
FileInherit: fileInherit,
|
|
|
|
Optional: optional,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-24 14:31:22 +02:00
|
|
|
func newRuleFromLog(log map[string]string) RuleBase {
|
2024-05-25 23:03:16 +02:00
|
|
|
comment := ""
|
|
|
|
fileInherit, noNewPrivs, optional := false, false, false
|
|
|
|
|
2024-04-24 14:31:22 +02:00
|
|
|
if log["operation"] == "file_inherit" {
|
|
|
|
fileInherit = true
|
|
|
|
}
|
2024-05-25 23:03:16 +02:00
|
|
|
if log["error"] == "-1" {
|
2024-04-24 14:31:22 +02:00
|
|
|
if strings.Contains(log["info"], "optional:") {
|
|
|
|
optional = true
|
2024-05-25 23:03:16 +02:00
|
|
|
comment = strings.Replace(log["info"], "optional: ", "", 1)
|
2024-04-24 14:31:22 +02:00
|
|
|
} else {
|
|
|
|
noNewPrivs = true
|
|
|
|
}
|
|
|
|
}
|
2024-05-25 23:03:16 +02:00
|
|
|
if log["info"] != "" {
|
|
|
|
comment += " " + log["info"]
|
|
|
|
}
|
2024-04-24 14:31:22 +02:00
|
|
|
return RuleBase{
|
|
|
|
IsLineRule: false,
|
2024-05-25 23:03:16 +02:00
|
|
|
Comment: comment,
|
2024-04-24 14:31:22 +02:00
|
|
|
NoNewPrivs: noNewPrivs,
|
|
|
|
FileInherit: fileInherit,
|
|
|
|
Optional: optional,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r RuleBase) Less(other any) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r RuleBase) Equals(other any) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r RuleBase) String() string {
|
2024-05-28 19:15:22 +02:00
|
|
|
return renderTemplate(r.Kind(), r)
|
2024-04-24 14:31:22 +02:00
|
|
|
}
|
|
|
|
|
2024-05-05 00:41:47 +02:00
|
|
|
func (r RuleBase) Constraint() constraint {
|
|
|
|
return anyKind
|
|
|
|
}
|
|
|
|
|
2024-05-28 19:15:22 +02:00
|
|
|
func (r RuleBase) Kind() Kind {
|
|
|
|
return COMMENT
|
2024-05-05 00:41:47 +02:00
|
|
|
}
|
|
|
|
|
2024-04-24 14:31:22 +02:00
|
|
|
type Qualifier struct {
|
|
|
|
Audit bool
|
|
|
|
AccessType string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newQualifierFromLog(log map[string]string) Qualifier {
|
|
|
|
audit := false
|
|
|
|
if log["apparmor"] == "AUDIT" {
|
|
|
|
audit = true
|
|
|
|
}
|
|
|
|
return Qualifier{Audit: audit}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Qualifier) Less(other Qualifier) bool {
|
|
|
|
if r.Audit != other.Audit {
|
|
|
|
return r.Audit
|
|
|
|
}
|
|
|
|
return r.AccessType < other.AccessType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Qualifier) Equals(other Qualifier) bool {
|
|
|
|
return r.Audit == other.Audit && r.AccessType == other.AccessType
|
|
|
|
}
|