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 (
|
2024-04-23 22:32:58 +02:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2023-10-21 00:11:11 +02:00
|
|
|
)
|
2023-09-29 21:37:15 +02:00
|
|
|
|
2024-04-23 22:26:09 +02:00
|
|
|
const (
|
|
|
|
tokALLOW = "allow"
|
|
|
|
tokAUDIT = "audit"
|
|
|
|
tokDENY = "deny"
|
|
|
|
)
|
|
|
|
|
2024-05-05 00:41:47 +02:00
|
|
|
type constraint uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
anyKind constraint = iota // The rule can be found in either preamble or profile
|
|
|
|
preambleKind // The rule can only be found in the preamble
|
|
|
|
blockKind // The rule can only be found in a profile
|
|
|
|
)
|
|
|
|
|
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-05-05 00:41:47 +02:00
|
|
|
Constraint() constraint
|
|
|
|
Kind() 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-23 22:32:58 +02:00
|
|
|
// Sort the rules in a profile.
|
|
|
|
// Follow: https://apparmor.pujol.io/development/guidelines/#guidelines
|
|
|
|
func (r Rules) Sort() {
|
|
|
|
sort.Slice(r, func(i, j int) bool {
|
|
|
|
typeOfI := reflect.TypeOf(r[i])
|
|
|
|
typeOfJ := reflect.TypeOf(r[j])
|
|
|
|
if typeOfI != typeOfJ {
|
|
|
|
valueOfI := typeToValue(typeOfI)
|
|
|
|
valueOfJ := typeToValue(typeOfJ)
|
|
|
|
if typeOfI == reflect.TypeOf((*Include)(nil)) && r[i].(*Include).IfExists {
|
|
|
|
valueOfI = "include_if_exists"
|
|
|
|
}
|
|
|
|
if typeOfJ == reflect.TypeOf((*Include)(nil)) && r[j].(*Include).IfExists {
|
|
|
|
valueOfJ = "include_if_exists"
|
|
|
|
}
|
|
|
|
return ruleWeights[valueOfI] < ruleWeights[valueOfJ]
|
|
|
|
}
|
|
|
|
return r[i].Less(r[j])
|
|
|
|
})
|
|
|
|
}
|