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
|
|
|
|
2024-06-25 20:50:27 +02:00
|
|
|
type Base struct {
|
2024-04-24 14:31:22 +02:00
|
|
|
IsLineRule bool
|
|
|
|
Comment string
|
|
|
|
NoNewPrivs bool
|
|
|
|
FileInherit bool
|
|
|
|
Optional bool
|
2024-06-29 23:20:42 +02:00
|
|
|
Paddings []string
|
2024-04-24 14:31:22 +02:00
|
|
|
}
|
|
|
|
|
2024-06-25 20:50:27 +02:00
|
|
|
func newBase(rule rule) Base {
|
2024-05-27 19:55:21 +02:00
|
|
|
comment := ""
|
|
|
|
fileInherit, noNewPrivs, optional := false, false, false
|
|
|
|
|
2024-06-20 00:22:49 +02:00
|
|
|
if len(rule) > 0 {
|
|
|
|
if len(rule.Get(0)) > 0 && rule.Get(0)[0] == '#' {
|
|
|
|
// Line rule is a comment
|
|
|
|
rule = rule[1:]
|
|
|
|
comment = rule.GetString()
|
|
|
|
} else {
|
|
|
|
// Comma rule, with comment at the end
|
|
|
|
comment = rule[len(rule)-1].comment
|
2024-05-27 19:55:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
2024-06-25 20:50:27 +02:00
|
|
|
return Base{
|
2024-05-27 19:55:21 +02:00
|
|
|
Comment: comment,
|
|
|
|
NoNewPrivs: noNewPrivs,
|
|
|
|
FileInherit: fileInherit,
|
|
|
|
Optional: optional,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-25 20:50:27 +02:00
|
|
|
func newBaseFromLog(log map[string]string) Base {
|
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-06-25 20:50:27 +02:00
|
|
|
return Base{
|
2024-04-24 14:31:22 +02:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-29 23:20:42 +02:00
|
|
|
func (r Base) Padding(i int) string {
|
|
|
|
if i >= len(r.Paddings) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return r.Paddings[i]
|
|
|
|
}
|
|
|
|
|
2024-06-25 20:50:27 +02:00
|
|
|
func (r *Base) merge(other Base) bool {
|
2024-06-29 23:20:42 +02:00
|
|
|
r.NoNewPrivs = r.NoNewPrivs || other.NoNewPrivs
|
|
|
|
r.FileInherit = r.FileInherit || other.FileInherit
|
|
|
|
r.Optional = r.Optional || other.Optional
|
2024-06-23 11:57:46 +02:00
|
|
|
if other.Comment != "" {
|
|
|
|
r.Comment += " " + other.Comment
|
|
|
|
}
|
2024-06-22 21:59:43 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-06-29 23:20:42 +02:00
|
|
|
func (r Base) addLine(other Rule) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
2024-06-19 19:34:58 +02:00
|
|
|
func (r Qualifier) Compare(o Qualifier) int {
|
|
|
|
if r := compare(r.Audit, o.Audit); r != 0 {
|
|
|
|
return r
|
2024-04-24 14:31:22 +02:00
|
|
|
}
|
2024-06-19 19:34:58 +02:00
|
|
|
return compare(r.AccessType, o.AccessType)
|
2024-04-24 14:31:22 +02:00
|
|
|
}
|
2024-06-22 21:59:43 +02:00
|
|
|
|
|
|
|
func (r Qualifier) Equal(o Qualifier) bool {
|
|
|
|
return r.Audit == o.Audit && r.AccessType == o.AccessType
|
|
|
|
}
|
2024-06-29 23:20:42 +02:00
|
|
|
|
|
|
|
func (r Qualifier) getLenAudit() int {
|
|
|
|
return length("audit", r.Audit)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Qualifier) getLenAccess() int {
|
|
|
|
lenAccess := 0
|
|
|
|
if r.AccessType != "" {
|
|
|
|
lenAccess = length("", r.AccessType)
|
|
|
|
}
|
|
|
|
return lenAccess
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Qualifier) setPaddings(max []int) []string {
|
|
|
|
return setPaddings(max,
|
|
|
|
[]string{"audit", ""},
|
|
|
|
[]any{r.Audit, r.AccessType},
|
|
|
|
)
|
|
|
|
}
|