apparmor.d/pkg/aa/unix.go

103 lines
2.1 KiB
Go
Raw Normal View History

// 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
import (
"fmt"
)
const UNIX Kind = "unix"
2024-05-25 23:01:29 +02:00
func init() {
requirements[UNIX] = requirement{
2024-05-25 23:01:29 +02:00
"access": []string{
"create", "bind", "listen", "accept", "connect", "shutdown",
"getattr", "setattr", "getopt", "setopt", "send", "receive",
"r", "w", "rw",
},
}
}
type Unix struct {
RuleBase
Qualifier
Access []string
2024-04-15 00:58:34 +02:00
Type string
Protocol string
Address string
Label string
Attr string
Opt string
PeerLabel string
PeerAddr string
}
func newUnixFromLog(log map[string]string) Rule {
return &Unix{
RuleBase: newRuleFromLog(log),
2024-04-15 00:58:34 +02:00
Qualifier: newQualifierFromLog(log),
Access: Must(toAccess(UNIX, log["requested_mask"])),
Type: log["sock_type"],
Protocol: log["protocol"],
Address: log["addr"],
2024-04-15 00:58:34 +02:00
Label: log["label"],
Attr: log["attr"],
Opt: log["opt"],
2024-04-15 00:58:34 +02:00
PeerLabel: log["peer"],
PeerAddr: log["peer_addr"],
}
}
func (r *Unix) Validate() error {
if err := validateValues(r.Kind(), "access", r.Access); err != nil {
return fmt.Errorf("%s: %w", r, err)
}
return nil
}
func (r *Unix) Compare(other Rule) int {
o, _ := other.(*Unix)
if res := compare(r.Access, o.Access); res != 0 {
return res
}
if res := compare(r.Type, o.Type); res != 0 {
return res
2024-04-15 00:58:34 +02:00
}
if res := compare(r.Protocol, o.Protocol); res != 0 {
return res
2024-04-15 00:58:34 +02:00
}
if res := compare(r.Address, o.Address); res != 0 {
return res
2024-04-15 00:58:34 +02:00
}
if res := compare(r.Label, o.Label); res != 0 {
return res
2024-04-15 00:58:34 +02:00
}
if res := compare(r.Attr, o.Attr); res != 0 {
return res
2024-04-15 00:58:34 +02:00
}
if res := compare(r.Opt, o.Opt); res != 0 {
return res
2024-04-15 00:58:34 +02:00
}
if res := compare(r.PeerLabel, o.PeerLabel); res != 0 {
return res
2024-04-15 00:58:34 +02:00
}
if res := compare(r.PeerAddr, o.PeerAddr); res != 0 {
return res
2024-04-15 00:58:34 +02:00
}
return r.Qualifier.Compare(o.Qualifier)
}
func (r *Unix) String() string {
return renderTemplate(r.Kind(), r)
}
func (r *Unix) Constraint() constraint {
return blockKind
}
func (r *Unix) Kind() Kind {
return UNIX
}