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
|
|
|
|
|
2024-05-25 23:14:43 +02:00
|
|
|
import (
|
|
|
|
"slices"
|
|
|
|
)
|
|
|
|
|
2024-05-25 23:01:29 +02:00
|
|
|
type requirement map[string][]string
|
|
|
|
|
2024-06-27 19:45:32 +02:00
|
|
|
type Constraint uint
|
2024-05-05 00:41:47 +02:00
|
|
|
|
|
|
|
const (
|
2024-06-27 19:45:32 +02:00
|
|
|
AnyRule Constraint = iota // The rule can be found in either preamble or profile
|
|
|
|
PreambleRule // The rule can only be found in the preamble
|
|
|
|
BlockRule // The rule can only be found in a profile
|
2024-05-05 00:41:47 +02:00
|
|
|
)
|
|
|
|
|
2024-05-28 19:15:22 +02:00
|
|
|
// Kind represents an AppArmor rule kind.
|
|
|
|
type Kind string
|
|
|
|
|
|
|
|
func (k Kind) String() string {
|
|
|
|
return string(k)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k Kind) Tok() string {
|
|
|
|
if t, ok := tok[k]; ok {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
return string(k)
|
|
|
|
}
|
|
|
|
|
2024-04-19 23:43:02 +02:00
|
|
|
// Rule generic interface for all AppArmor rules
|
|
|
|
type Rule interface {
|
2024-06-29 23:20:42 +02:00
|
|
|
Kind() Kind // Kind of the rule
|
|
|
|
Constraint() Constraint // Where the rule can be found (preamble, profile, any)
|
|
|
|
String() string // Render the rule as a string
|
|
|
|
Validate() error // Validate the rule. Return an error if the rule is invalid
|
|
|
|
Compare(other Rule) int // Compare two rules. Return 0 if they are identical
|
|
|
|
Merge(other Rule) bool // Merge rules of same kind together. Return true if merged
|
|
|
|
Padding(i int) string // Padding for rule items at index i
|
|
|
|
Lengths() []int // Length of each item in the rule
|
|
|
|
setPaddings(max []int) // Set paddings for each item in the rule
|
|
|
|
addLine(other Rule) bool // Check either a new line should be added before the rule
|
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-05-25 23:36:39 +02:00
|
|
|
func (r Rules) Validate() error {
|
|
|
|
for _, rule := range r {
|
2024-05-29 22:17:21 +02:00
|
|
|
if rule == nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-05-25 23:36:39 +02:00
|
|
|
if err := rule.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:26:09 +02:00
|
|
|
func (r Rules) String() string {
|
|
|
|
return renderTemplate("rules", r)
|
|
|
|
}
|
2024-05-05 15:11:00 +02:00
|
|
|
|
2024-05-28 19:07:32 +02:00
|
|
|
// Index returns the index of the first occurrence of rule rin r, or -1 if not present.
|
2024-05-29 22:17:21 +02:00
|
|
|
func (r Rules) Index(item Rule) int {
|
|
|
|
for idx, rule := range r {
|
|
|
|
if rule == nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-06-19 19:34:58 +02:00
|
|
|
if rule.Kind() == item.Kind() && rule.Compare(item) == 0 {
|
2024-05-26 19:05:15 +02:00
|
|
|
return idx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2024-05-28 19:07:32 +02:00
|
|
|
// Replace replaces the elements r[i] by the given rules, and returns the
|
|
|
|
// modified slice.
|
|
|
|
func (r Rules) Replace(i int, rules ...Rule) Rules {
|
|
|
|
return append(r[:i], append(rules, r[i+1:]...)...)
|
2024-05-26 19:05:15 +02:00
|
|
|
}
|
|
|
|
|
2024-05-28 19:07:32 +02:00
|
|
|
// Insert inserts the rules into r at index i, returning the modified slice.
|
|
|
|
func (r Rules) Insert(i int, rules ...Rule) Rules {
|
|
|
|
return append(r[:i], append(rules, r[i:]...)...)
|
2024-05-26 19:05:15 +02:00
|
|
|
}
|
|
|
|
|
2024-05-28 19:07:32 +02:00
|
|
|
// Delete removes the elements r[i] from r, returning the modified slice.
|
|
|
|
func (r Rules) Delete(i int) Rules {
|
|
|
|
return append(r[:i], r[i+1:]...)
|
2024-05-26 19:05:15 +02:00
|
|
|
}
|
|
|
|
|
2024-05-28 19:15:22 +02:00
|
|
|
func (r Rules) DeleteKind(kind Kind) Rules {
|
2024-05-26 19:05:15 +02:00
|
|
|
res := make(Rules, 0)
|
|
|
|
for _, rule := range r {
|
2024-05-29 22:17:21 +02:00
|
|
|
if rule == nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-05-26 19:05:15 +02:00
|
|
|
if rule.Kind() != kind {
|
|
|
|
res = append(res, rule)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2024-05-28 19:15:22 +02:00
|
|
|
func (r Rules) Filter(filter Kind) Rules {
|
2024-05-05 15:11:00 +02:00
|
|
|
res := make(Rules, 0)
|
|
|
|
for _, rule := range r {
|
2024-05-29 22:17:21 +02:00
|
|
|
if rule == nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-05-26 19:05:15 +02:00
|
|
|
if rule.Kind() != filter {
|
2024-05-05 15:11:00 +02:00
|
|
|
res = append(res, rule)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Rules) GetVariables() []*Variable {
|
|
|
|
res := make([]*Variable, 0)
|
|
|
|
for _, rule := range r {
|
2024-05-30 13:28:12 +02:00
|
|
|
switch rule := rule.(type) {
|
2024-05-05 15:11:00 +02:00
|
|
|
case *Variable:
|
2024-05-30 13:28:12 +02:00
|
|
|
res = append(res, rule)
|
2024-05-05 15:11:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2024-05-25 23:14:43 +02:00
|
|
|
|
2024-05-25 23:21:59 +02:00
|
|
|
func (r Rules) GetIncludes() []*Include {
|
|
|
|
res := make([]*Include, 0)
|
|
|
|
for _, rule := range r {
|
2024-05-30 13:28:12 +02:00
|
|
|
switch rule := rule.(type) {
|
2024-05-25 23:21:59 +02:00
|
|
|
case *Include:
|
2024-05-30 13:28:12 +02:00
|
|
|
res = append(res, rule)
|
2024-05-25 23:21:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2024-06-29 23:31:36 +02:00
|
|
|
// Merge merge similar rules together:
|
2024-05-28 19:20:52 +02:00
|
|
|
// - Remove identical rules
|
|
|
|
// - Merge rule access. Eg: for same path, 'r' and 'w' becomes 'rw'
|
|
|
|
//
|
|
|
|
// Note: logs.regCleanLogs helps a lot to do a first cleaning
|
|
|
|
func (r Rules) Merge() Rules {
|
|
|
|
for i := 0; i < len(r); i++ {
|
|
|
|
for j := i + 1; j < len(r); j++ {
|
2024-06-20 00:30:36 +02:00
|
|
|
if r[i] == nil && r[j] == nil {
|
|
|
|
r = r.Delete(j)
|
|
|
|
j--
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if r[i] == nil || r[j] == nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-06-21 00:23:39 +02:00
|
|
|
if r[i].Kind() != r[j].Kind() {
|
2024-05-28 19:20:52 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-06-20 23:29:22 +02:00
|
|
|
// If rules are identical, merge them. Ignore comments
|
2024-06-21 00:23:39 +02:00
|
|
|
if r[i].Kind() != COMMENT && r[i].Compare(r[j]) == 0 {
|
2024-05-28 19:20:52 +02:00
|
|
|
r = r.Delete(j)
|
|
|
|
j--
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-06-21 00:23:39 +02:00
|
|
|
if r[i].Merge(r[j]) {
|
|
|
|
r = r.Delete(j)
|
|
|
|
j--
|
2024-05-28 19:20:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the rules according to the guidelines:
|
|
|
|
// https://apparmor.pujol.io/development/guidelines/#guidelines
|
|
|
|
func (r Rules) Sort() Rules {
|
|
|
|
slices.SortFunc(r, func(a, b Rule) int {
|
|
|
|
kindOfA := a.Kind()
|
|
|
|
kindOfB := b.Kind()
|
|
|
|
if kindOfA != kindOfB {
|
|
|
|
if kindOfA == INCLUDE && a.(*Include).IfExists {
|
|
|
|
kindOfA = "include_if_exists"
|
|
|
|
}
|
|
|
|
if kindOfB == INCLUDE && b.(*Include).IfExists {
|
|
|
|
kindOfB = "include_if_exists"
|
|
|
|
}
|
|
|
|
return ruleWeights[kindOfA] - ruleWeights[kindOfB]
|
|
|
|
}
|
2024-06-19 19:34:58 +02:00
|
|
|
return a.Compare(b)
|
2024-05-28 19:20:52 +02:00
|
|
|
})
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2024-06-29 23:31:36 +02:00
|
|
|
// setPaddings set paddings for each element in each rules
|
|
|
|
func (r *Rules) setPaddings(paddingsIndex map[Kind][]int, paddingsMaxLen map[Kind][]int) {
|
|
|
|
for kind, index := range paddingsIndex {
|
|
|
|
if len(index) <= 1 {
|
2024-06-20 00:30:36 +02:00
|
|
|
continue
|
|
|
|
}
|
2024-06-29 23:31:36 +02:00
|
|
|
for _, i := range index {
|
|
|
|
(*r)[i].setPaddings(paddingsMaxLen[kind])
|
2024-06-20 00:30:36 +02:00
|
|
|
}
|
|
|
|
}
|
2024-06-29 23:31:36 +02:00
|
|
|
}
|
2024-05-28 19:20:52 +02:00
|
|
|
|
2024-06-29 23:31:36 +02:00
|
|
|
// Format the rules for better readability before printing it. Format supposes
|
|
|
|
// the rules are merged and sorted.
|
|
|
|
// Follow: https://apparmor.pujol.io/development/guidelines/#the-file-block
|
|
|
|
func (r Rules) Format() Rules {
|
|
|
|
// Insert new line between rule of different type/subtype.
|
2024-06-20 00:30:36 +02:00
|
|
|
for i := len(r) - 1; i >= 0; i-- {
|
2024-06-29 23:31:36 +02:00
|
|
|
j := i - 1
|
|
|
|
if j < 0 || r[j] == nil {
|
2024-06-20 00:30:36 +02:00
|
|
|
continue
|
|
|
|
}
|
2024-06-29 23:31:36 +02:00
|
|
|
if r[i].addLine(r[j]) {
|
|
|
|
r = r.Insert(i, nil)
|
|
|
|
}
|
|
|
|
}
|
2024-05-28 19:20:52 +02:00
|
|
|
|
2024-06-29 23:31:36 +02:00
|
|
|
// Find max paddings for each element in each rules
|
|
|
|
paddingsIndex := map[Kind][]int{}
|
|
|
|
paddingsMaxLen := map[Kind][]int{}
|
|
|
|
for i, rule := range r {
|
|
|
|
if rule == nil {
|
|
|
|
r.setPaddings(paddingsIndex, paddingsMaxLen)
|
|
|
|
paddingsIndex = map[Kind][]int{}
|
|
|
|
paddingsMaxLen = map[Kind][]int{}
|
|
|
|
continue
|
|
|
|
}
|
2024-05-28 19:20:52 +02:00
|
|
|
|
2024-06-29 23:31:36 +02:00
|
|
|
lengths := rule.Lengths()
|
|
|
|
paddingsIndex[rule.Kind()] = append(paddingsIndex[rule.Kind()], i)
|
|
|
|
for idx, length := range lengths {
|
|
|
|
if _, ok := paddingsMaxLen[rule.Kind()]; !ok {
|
|
|
|
paddingsMaxLen[rule.Kind()] = make([]int, len(lengths))
|
2024-05-28 19:20:52 +02:00
|
|
|
}
|
2024-06-29 23:31:36 +02:00
|
|
|
paddingsMaxLen[rule.Kind()][idx] = max(paddingsMaxLen[rule.Kind()][idx], length)
|
2024-05-28 19:20:52 +02:00
|
|
|
}
|
|
|
|
}
|
2024-06-29 23:31:36 +02:00
|
|
|
r.setPaddings(paddingsIndex, paddingsMaxLen)
|
2024-05-28 19:20:52 +02:00
|
|
|
return r
|
|
|
|
}
|