2023-09-25 01:06:07 +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-09-25 01:06:07 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
|
|
|
package aa
|
|
|
|
|
2024-02-24 17:58:38 +01:00
|
|
|
import (
|
2024-04-12 21:07:05 +02:00
|
|
|
"slices"
|
|
|
|
)
|
2024-04-23 22:26:09 +02:00
|
|
|
|
|
|
|
const (
|
|
|
|
tokMOUNT = "mount"
|
|
|
|
tokREMOUNT = "remount"
|
|
|
|
tokUMOUNT = "umount"
|
|
|
|
)
|
|
|
|
|
2024-05-25 23:01:29 +02:00
|
|
|
func init() {
|
|
|
|
requirements[tokMOUNT] = requirement{
|
|
|
|
"flags": {
|
|
|
|
"acl", "async", "atime", "bind", "dev", "diratime", "dirsync", "exec",
|
|
|
|
"iversion", "loud", "mand", "move", "noacl", "noatime", "nodev",
|
|
|
|
"nodiratime", "noexec", "noiversion", "nomand", "norelatime", "nosuid",
|
|
|
|
"nouser", "private", "rbind", "relatime", "remount", "ro", "rprivate",
|
|
|
|
"rshared", "rslave", "runbindable", "rw", "shared", "silent", "slave",
|
|
|
|
"strictatime", "suid", "sync", "unbindable", "user", "verbose",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2023-09-25 01:06:07 +02:00
|
|
|
|
|
|
|
type MountConditions struct {
|
|
|
|
FsType string
|
|
|
|
Options []string
|
|
|
|
}
|
|
|
|
|
2024-04-15 00:58:34 +02:00
|
|
|
func newMountConditionsFromLog(log map[string]string) MountConditions {
|
2024-02-24 17:58:38 +01:00
|
|
|
if _, present := log["flags"]; present {
|
|
|
|
return MountConditions{
|
|
|
|
FsType: log["fstype"],
|
2024-05-25 23:14:43 +02:00
|
|
|
Options: Must(toValues(tokMOUNT, "flags", log["flags"])),
|
2023-09-25 01:09:11 +02:00
|
|
|
}
|
|
|
|
}
|
2024-02-24 17:58:38 +01:00
|
|
|
return MountConditions{FsType: log["fstype"]}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m MountConditions) Less(other MountConditions) bool {
|
2024-04-15 00:58:34 +02:00
|
|
|
if m.FsType != other.FsType {
|
|
|
|
return m.FsType < other.FsType
|
2024-02-24 17:58:38 +01:00
|
|
|
}
|
2024-04-15 00:58:34 +02:00
|
|
|
return len(m.Options) < len(other.Options)
|
2023-09-25 01:09:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m MountConditions) Equals(other MountConditions) bool {
|
2024-02-24 17:58:38 +01:00
|
|
|
return m.FsType == other.FsType && slices.Equal(m.Options, other.Options)
|
2023-09-25 01:09:11 +02:00
|
|
|
}
|
|
|
|
|
2023-09-25 01:06:07 +02:00
|
|
|
type Mount struct {
|
2024-04-19 23:43:02 +02:00
|
|
|
RuleBase
|
2023-09-25 01:06:07 +02:00
|
|
|
Qualifier
|
|
|
|
MountConditions
|
|
|
|
Source string
|
|
|
|
MountPoint string
|
|
|
|
}
|
|
|
|
|
2024-04-19 23:43:02 +02:00
|
|
|
func newMountFromLog(log map[string]string) Rule {
|
2023-09-25 01:06:07 +02:00
|
|
|
return &Mount{
|
2024-04-19 23:43:02 +02:00
|
|
|
RuleBase: newRuleFromLog(log),
|
2024-04-15 00:58:34 +02:00
|
|
|
Qualifier: newQualifierFromLog(log),
|
|
|
|
MountConditions: newMountConditionsFromLog(log),
|
2024-02-24 17:58:38 +01:00
|
|
|
Source: log["srcname"],
|
|
|
|
MountPoint: log["name"],
|
2023-09-25 01:06:07 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-25 01:09:11 +02:00
|
|
|
|
|
|
|
func (r *Mount) Less(other any) bool {
|
|
|
|
o, _ := other.(*Mount)
|
2024-04-15 00:58:34 +02:00
|
|
|
if r.Source != o.Source {
|
2023-09-25 01:09:11 +02:00
|
|
|
return r.Source < o.Source
|
|
|
|
}
|
2024-04-15 00:58:34 +02:00
|
|
|
if r.MountPoint != o.MountPoint {
|
|
|
|
return r.MountPoint < o.MountPoint
|
|
|
|
}
|
|
|
|
if r.MountConditions.Equals(o.MountConditions) {
|
|
|
|
return r.MountConditions.Less(o.MountConditions)
|
|
|
|
}
|
2023-09-25 01:09:11 +02:00
|
|
|
return r.Qualifier.Less(o.Qualifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Mount) Equals(other any) bool {
|
|
|
|
o, _ := other.(*Mount)
|
|
|
|
return r.Source == o.Source && r.MountPoint == o.MountPoint &&
|
|
|
|
r.MountConditions.Equals(o.MountConditions) &&
|
|
|
|
r.Qualifier.Equals(o.Qualifier)
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:26:09 +02:00
|
|
|
func (r *Mount) String() string {
|
2024-05-05 00:41:47 +02:00
|
|
|
return renderTemplate(r.Kind(), r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Mount) Constraint() constraint {
|
|
|
|
return blockKind
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Mount) Kind() string {
|
|
|
|
return tokMOUNT
|
2024-04-23 22:26:09 +02:00
|
|
|
}
|
|
|
|
|
2023-09-25 01:06:07 +02:00
|
|
|
type Umount struct {
|
2024-04-19 23:43:02 +02:00
|
|
|
RuleBase
|
2023-09-25 01:06:07 +02:00
|
|
|
Qualifier
|
|
|
|
MountConditions
|
|
|
|
MountPoint string
|
|
|
|
}
|
|
|
|
|
2024-04-19 23:43:02 +02:00
|
|
|
func newUmountFromLog(log map[string]string) Rule {
|
2023-09-25 01:06:07 +02:00
|
|
|
return &Umount{
|
2024-04-19 23:43:02 +02:00
|
|
|
RuleBase: newRuleFromLog(log),
|
2024-04-15 00:58:34 +02:00
|
|
|
Qualifier: newQualifierFromLog(log),
|
|
|
|
MountConditions: newMountConditionsFromLog(log),
|
2024-02-24 17:58:38 +01:00
|
|
|
MountPoint: log["name"],
|
2023-09-25 01:06:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 01:09:11 +02:00
|
|
|
func (r *Umount) Less(other any) bool {
|
|
|
|
o, _ := other.(*Umount)
|
2024-04-15 00:58:34 +02:00
|
|
|
if r.MountPoint != o.MountPoint {
|
2023-09-25 01:09:11 +02:00
|
|
|
return r.MountPoint < o.MountPoint
|
|
|
|
}
|
2024-04-15 00:58:34 +02:00
|
|
|
if r.MountConditions.Equals(o.MountConditions) {
|
|
|
|
return r.MountConditions.Less(o.MountConditions)
|
|
|
|
}
|
2023-09-25 01:09:11 +02:00
|
|
|
return r.Qualifier.Less(o.Qualifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Umount) Equals(other any) bool {
|
|
|
|
o, _ := other.(*Umount)
|
|
|
|
return r.MountPoint == o.MountPoint &&
|
|
|
|
r.MountConditions.Equals(o.MountConditions) &&
|
|
|
|
r.Qualifier.Equals(o.Qualifier)
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:26:09 +02:00
|
|
|
func (r *Umount) String() string {
|
2024-05-05 00:41:47 +02:00
|
|
|
return renderTemplate(r.Kind(), r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Umount) Constraint() constraint {
|
|
|
|
return blockKind
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Umount) Kind() string {
|
|
|
|
return tokUMOUNT
|
2024-04-23 22:26:09 +02:00
|
|
|
}
|
|
|
|
|
2023-09-25 01:06:07 +02:00
|
|
|
type Remount struct {
|
2024-04-19 23:43:02 +02:00
|
|
|
RuleBase
|
2023-09-25 01:06:07 +02:00
|
|
|
Qualifier
|
|
|
|
MountConditions
|
|
|
|
MountPoint string
|
|
|
|
}
|
|
|
|
|
2024-04-19 23:43:02 +02:00
|
|
|
func newRemountFromLog(log map[string]string) Rule {
|
2023-09-25 01:06:07 +02:00
|
|
|
return &Remount{
|
2024-04-19 23:43:02 +02:00
|
|
|
RuleBase: newRuleFromLog(log),
|
2024-04-15 00:58:34 +02:00
|
|
|
Qualifier: newQualifierFromLog(log),
|
|
|
|
MountConditions: newMountConditionsFromLog(log),
|
2024-02-24 17:58:38 +01:00
|
|
|
MountPoint: log["name"],
|
2023-09-25 01:06:07 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-25 01:09:11 +02:00
|
|
|
|
|
|
|
func (r *Remount) Less(other any) bool {
|
|
|
|
o, _ := other.(*Remount)
|
2024-04-15 00:58:34 +02:00
|
|
|
if r.MountPoint != o.MountPoint {
|
2023-09-25 01:09:11 +02:00
|
|
|
return r.MountPoint < o.MountPoint
|
|
|
|
}
|
2024-04-15 00:58:34 +02:00
|
|
|
if r.MountConditions.Equals(o.MountConditions) {
|
|
|
|
return r.MountConditions.Less(o.MountConditions)
|
|
|
|
}
|
2023-09-25 01:09:11 +02:00
|
|
|
return r.Qualifier.Less(o.Qualifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Remount) Equals(other any) bool {
|
|
|
|
o, _ := other.(*Remount)
|
|
|
|
return r.MountPoint == o.MountPoint &&
|
|
|
|
r.MountConditions.Equals(o.MountConditions) &&
|
|
|
|
r.Qualifier.Equals(o.Qualifier)
|
|
|
|
}
|
2024-04-23 22:26:09 +02:00
|
|
|
|
|
|
|
func (r *Remount) String() string {
|
2024-05-05 00:41:47 +02:00
|
|
|
return renderTemplate(r.Kind(), r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Remount) Constraint() constraint {
|
|
|
|
return blockKind
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Remount) Kind() string {
|
|
|
|
return tokREMOUNT
|
2024-04-23 22:26:09 +02:00
|
|
|
}
|