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-05-26 19:05:15 +02:00
|
|
|
"fmt"
|
2024-04-12 21:07:05 +02:00
|
|
|
)
|
2024-04-23 22:26:09 +02:00
|
|
|
|
|
|
|
const (
|
2024-05-28 19:15:22 +02:00
|
|
|
MOUNT Kind = "mount"
|
|
|
|
REMOUNT Kind = "remount"
|
|
|
|
UMOUNT Kind = "umount"
|
2024-04-23 22:26:09 +02:00
|
|
|
)
|
|
|
|
|
2024-05-25 23:01:29 +02:00
|
|
|
func init() {
|
2024-05-28 19:15:22 +02:00
|
|
|
requirements[MOUNT] = requirement{
|
2024-05-25 23:01:29 +02:00
|
|
|
"flags": {
|
2024-05-28 00:44:23 +02:00
|
|
|
"acl", "async", "atime", "ro", "rw", "bind", "rbind", "dev",
|
|
|
|
"diratime", "dirsync", "exec", "iversion", "loud", "mand", "move",
|
|
|
|
"noacl", "noatime", "nodev", "nodiratime", "noexec", "noiversion",
|
|
|
|
"nomand", "norelatime", "nosuid", "nouser", "private", "relatime",
|
|
|
|
"remount", "rprivate", "rshared", "rslave", "runbindable", "shared",
|
|
|
|
"silent", "slave", "strictatime", "suid", "sync", "unbindable",
|
|
|
|
"user", "verbose",
|
2024-05-25 23:01:29 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2023-09-25 01:06:07 +02:00
|
|
|
|
|
|
|
type MountConditions struct {
|
|
|
|
FsType string
|
|
|
|
Options []string
|
|
|
|
}
|
|
|
|
|
2024-06-20 00:22:49 +02:00
|
|
|
func newMountConditions(rule rule) (MountConditions, error) {
|
|
|
|
options, err := toValues(MOUNT, "flags", rule.GetValuesAsString("options"))
|
|
|
|
if err != nil {
|
|
|
|
return MountConditions{}, err
|
|
|
|
}
|
|
|
|
return MountConditions{
|
|
|
|
FsType: rule.GetValuesAsString("fstype"),
|
|
|
|
Options: options,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
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-28 19:15:22 +02:00
|
|
|
Options: Must(toValues(MOUNT, "flags", log["flags"])),
|
2023-09-25 01:09:11 +02:00
|
|
|
}
|
|
|
|
}
|
2024-02-24 17:58:38 +01:00
|
|
|
return MountConditions{FsType: log["fstype"]}
|
|
|
|
}
|
|
|
|
|
2024-05-25 23:36:39 +02:00
|
|
|
func (m MountConditions) Validate() error {
|
2024-05-28 19:15:22 +02:00
|
|
|
return validateValues(MOUNT, "flags", m.Options)
|
2024-05-25 23:36:39 +02:00
|
|
|
}
|
|
|
|
|
2024-06-19 19:34:58 +02:00
|
|
|
func (m MountConditions) Compare(other MountConditions) int {
|
|
|
|
if res := compare(m.FsType, other.FsType); res != 0 {
|
|
|
|
return res
|
2024-02-24 17:58:38 +01:00
|
|
|
}
|
2024-06-19 19:34:58 +02:00
|
|
|
return compare(m.Options, other.Options)
|
2023-09-25 01:09:11 +02:00
|
|
|
}
|
|
|
|
|
2024-06-23 11:57:46 +02:00
|
|
|
func (m *MountConditions) Merge(other MountConditions) bool {
|
2024-06-22 21:59:43 +02:00
|
|
|
if m.FsType == other.FsType {
|
|
|
|
m.Options = merge(MOUNT, "flags", m.Options, other.Options)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-09-25 01:06:07 +02:00
|
|
|
type Mount struct {
|
2024-06-25 20:50:27 +02:00
|
|
|
Base
|
2023-09-25 01:06:07 +02:00
|
|
|
Qualifier
|
|
|
|
MountConditions
|
|
|
|
Source string
|
|
|
|
MountPoint string
|
|
|
|
}
|
|
|
|
|
2024-06-20 00:22:49 +02:00
|
|
|
func newMount(q Qualifier, rule rule) (Rule, error) {
|
|
|
|
mount, src := "", ""
|
|
|
|
r := rule.GetSlice()
|
|
|
|
if len(r) > 0 {
|
|
|
|
if r[0] != tokARROW {
|
|
|
|
src = r[0]
|
|
|
|
r = r[1:]
|
|
|
|
}
|
|
|
|
if len(r) == 2 {
|
|
|
|
if r[0] != tokARROW {
|
|
|
|
return nil, fmt.Errorf("missing '%s' in rule: %s", tokARROW, rule)
|
|
|
|
}
|
|
|
|
mount = r[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
conditions, err := newMountConditions(rule)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Mount{
|
2024-06-25 20:50:27 +02:00
|
|
|
Base: newBase(rule),
|
2024-06-20 00:22:49 +02:00
|
|
|
Qualifier: q,
|
|
|
|
MountConditions: conditions,
|
|
|
|
Source: src,
|
|
|
|
MountPoint: mount,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
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-06-25 20:50:27 +02:00
|
|
|
Base: newBaseFromLog(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
|
|
|
|
2024-06-25 20:56:36 +02:00
|
|
|
func (r *Mount) Kind() Kind {
|
|
|
|
return MOUNT
|
|
|
|
}
|
|
|
|
|
2024-06-27 19:45:32 +02:00
|
|
|
func (r *Mount) Constraint() Constraint {
|
|
|
|
return BlockRule
|
2024-06-25 20:56:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Mount) String() string {
|
|
|
|
return renderTemplate(r.Kind(), r)
|
|
|
|
}
|
|
|
|
|
2024-05-25 23:36:39 +02:00
|
|
|
func (r *Mount) Validate() error {
|
|
|
|
if err := r.MountConditions.Validate(); err != nil {
|
|
|
|
return fmt.Errorf("%s: %w", r, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-19 19:34:58 +02:00
|
|
|
func (r *Mount) Compare(other Rule) int {
|
2023-09-25 01:09:11 +02:00
|
|
|
o, _ := other.(*Mount)
|
2024-06-19 19:34:58 +02:00
|
|
|
if res := compare(r.Source, o.Source); res != 0 {
|
|
|
|
return res
|
2023-09-25 01:09:11 +02:00
|
|
|
}
|
2024-06-19 19:34:58 +02:00
|
|
|
if res := compare(r.MountPoint, o.MountPoint); res != 0 {
|
|
|
|
return res
|
2024-04-15 00:58:34 +02:00
|
|
|
}
|
2024-06-19 19:34:58 +02:00
|
|
|
if res := r.MountConditions.Compare(o.MountConditions); res != 0 {
|
|
|
|
return res
|
2024-04-15 00:58:34 +02:00
|
|
|
}
|
2024-06-19 19:34:58 +02:00
|
|
|
return r.Qualifier.Compare(o.Qualifier)
|
2023-09-25 01:09:11 +02:00
|
|
|
}
|
|
|
|
|
2024-06-22 21:59:43 +02:00
|
|
|
func (r *Mount) Merge(other Rule) bool {
|
|
|
|
o, _ := other.(*Mount)
|
2024-06-23 11:57:46 +02:00
|
|
|
mc := &r.MountConditions
|
2024-06-22 21:59:43 +02:00
|
|
|
|
|
|
|
if !r.Qualifier.Equal(o.Qualifier) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if r.Source == o.Source && r.MountPoint == o.MountPoint &&
|
2024-06-23 11:57:46 +02:00
|
|
|
mc.Merge(o.MountConditions) {
|
2024-06-25 20:50:27 +02:00
|
|
|
b := &r.Base
|
|
|
|
return b.merge(o.Base)
|
2024-06-22 21:59:43 +02:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-09-25 01:06:07 +02:00
|
|
|
type Umount struct {
|
2024-06-25 20:50:27 +02:00
|
|
|
Base
|
2023-09-25 01:06:07 +02:00
|
|
|
Qualifier
|
|
|
|
MountConditions
|
|
|
|
MountPoint string
|
|
|
|
}
|
|
|
|
|
2024-06-20 00:22:49 +02:00
|
|
|
func newUmount(q Qualifier, rule rule) (Rule, error) {
|
|
|
|
mount := ""
|
|
|
|
r := rule.GetSlice()
|
|
|
|
if len(r) > 0 {
|
|
|
|
mount = r[0]
|
|
|
|
}
|
|
|
|
conditions, err := newMountConditions(rule)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Umount{
|
2024-06-25 20:50:27 +02:00
|
|
|
Base: newBase(rule),
|
2024-06-20 00:22:49 +02:00
|
|
|
Qualifier: q,
|
|
|
|
MountConditions: conditions,
|
|
|
|
MountPoint: mount,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
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-06-25 20:50:27 +02:00
|
|
|
Base: newBaseFromLog(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-25 20:56:36 +02:00
|
|
|
func (r *Umount) Kind() Kind {
|
|
|
|
return UMOUNT
|
|
|
|
}
|
|
|
|
|
2024-06-27 19:45:32 +02:00
|
|
|
func (r *Umount) Constraint() Constraint {
|
|
|
|
return BlockRule
|
2024-06-25 20:56:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Umount) String() string {
|
|
|
|
return renderTemplate(r.Kind(), r)
|
|
|
|
}
|
|
|
|
|
2024-05-25 23:36:39 +02:00
|
|
|
func (r *Umount) Validate() error {
|
|
|
|
if err := r.MountConditions.Validate(); err != nil {
|
|
|
|
return fmt.Errorf("%s: %w", r, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-19 19:34:58 +02:00
|
|
|
func (r *Umount) Compare(other Rule) int {
|
2023-09-25 01:09:11 +02:00
|
|
|
o, _ := other.(*Umount)
|
2024-06-19 19:34:58 +02:00
|
|
|
if res := compare(r.MountPoint, o.MountPoint); res != 0 {
|
|
|
|
return res
|
2023-09-25 01:09:11 +02:00
|
|
|
}
|
2024-06-19 19:34:58 +02:00
|
|
|
if res := r.MountConditions.Compare(o.MountConditions); res != 0 {
|
|
|
|
return res
|
2024-04-15 00:58:34 +02:00
|
|
|
}
|
2024-06-19 19:34:58 +02:00
|
|
|
return r.Qualifier.Compare(o.Qualifier)
|
2023-09-25 01:09:11 +02:00
|
|
|
}
|
|
|
|
|
2024-06-22 21:59:43 +02:00
|
|
|
func (r *Umount) Merge(other Rule) bool {
|
|
|
|
o, _ := other.(*Umount)
|
2024-06-23 11:57:46 +02:00
|
|
|
mc := &r.MountConditions
|
2024-06-22 21:59:43 +02:00
|
|
|
|
|
|
|
if !r.Qualifier.Equal(o.Qualifier) {
|
|
|
|
return false
|
|
|
|
}
|
2024-06-23 11:57:46 +02:00
|
|
|
if r.MountPoint == o.MountPoint && mc.Merge(o.MountConditions) {
|
2024-06-25 20:50:27 +02:00
|
|
|
b := &r.Base
|
|
|
|
return b.merge(o.Base)
|
2024-06-22 21:59:43 +02:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-09-25 01:06:07 +02:00
|
|
|
type Remount struct {
|
2024-06-25 20:50:27 +02:00
|
|
|
Base
|
2023-09-25 01:06:07 +02:00
|
|
|
Qualifier
|
|
|
|
MountConditions
|
|
|
|
MountPoint string
|
|
|
|
}
|
|
|
|
|
2024-06-20 00:22:49 +02:00
|
|
|
func newRemount(q Qualifier, rule rule) (Rule, error) {
|
|
|
|
mount := ""
|
|
|
|
r := rule.GetSlice()
|
|
|
|
if len(r) > 0 {
|
|
|
|
mount = r[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
conditions, err := newMountConditions(rule)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Remount{
|
2024-06-25 20:50:27 +02:00
|
|
|
Base: newBase(rule),
|
2024-06-20 00:22:49 +02:00
|
|
|
Qualifier: q,
|
|
|
|
MountConditions: conditions,
|
|
|
|
MountPoint: mount,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
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-06-25 20:50:27 +02:00
|
|
|
Base: newBaseFromLog(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
|
|
|
|
2024-06-25 20:56:36 +02:00
|
|
|
func (r *Remount) Kind() Kind {
|
|
|
|
return REMOUNT
|
|
|
|
}
|
|
|
|
|
2024-06-27 19:45:32 +02:00
|
|
|
func (r *Remount) Constraint() Constraint {
|
|
|
|
return BlockRule
|
2024-06-25 20:56:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Remount) String() string {
|
|
|
|
return renderTemplate(r.Kind(), r)
|
|
|
|
}
|
|
|
|
|
2024-05-25 23:36:39 +02:00
|
|
|
func (r *Remount) Validate() error {
|
|
|
|
if err := r.MountConditions.Validate(); err != nil {
|
|
|
|
return fmt.Errorf("%s: %w", r, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-19 19:34:58 +02:00
|
|
|
func (r *Remount) Compare(other Rule) int {
|
2023-09-25 01:09:11 +02:00
|
|
|
o, _ := other.(*Remount)
|
2024-06-19 19:34:58 +02:00
|
|
|
if res := compare(r.MountPoint, o.MountPoint); res != 0 {
|
|
|
|
return res
|
2023-09-25 01:09:11 +02:00
|
|
|
}
|
2024-06-19 19:34:58 +02:00
|
|
|
if res := r.MountConditions.Compare(o.MountConditions); res != 0 {
|
|
|
|
return res
|
2024-04-15 00:58:34 +02:00
|
|
|
}
|
2024-06-19 19:34:58 +02:00
|
|
|
return r.Qualifier.Compare(o.Qualifier)
|
2023-09-25 01:09:11 +02:00
|
|
|
}
|
2024-04-23 22:26:09 +02:00
|
|
|
|
2024-06-22 21:59:43 +02:00
|
|
|
func (r *Remount) Merge(other Rule) bool {
|
|
|
|
o, _ := other.(*Remount)
|
2024-06-23 11:57:46 +02:00
|
|
|
mc := &r.MountConditions
|
2024-06-22 21:59:43 +02:00
|
|
|
|
|
|
|
if !r.Qualifier.Equal(o.Qualifier) {
|
|
|
|
return false
|
|
|
|
}
|
2024-06-23 11:57:46 +02:00
|
|
|
if r.MountPoint == o.MountPoint && mc.Merge(o.MountConditions) {
|
2024-06-25 20:50:27 +02:00
|
|
|
b := &r.Base
|
|
|
|
return b.merge(o.Base)
|
2024-06-22 21:59:43 +02:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|