apparmor.d/pkg/aa/network.go

125 lines
3.0 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 NETWORK Kind = "network"
2024-05-25 23:01:29 +02:00
func init() {
requirements[NETWORK] = 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",
},
"domains": []string{
"unix", "inet", "ax25", "ipx", "appletalk", "netrom", "bridge",
"atmpvc", "x25", "inet6", "rose", "netbeui", "security", "key",
"netlink", "packet", "ash", "econet", "atmsvc", "rds", "sna", "irda",
"pppox", "wanpipe", "llc", "ib", "mpls", "can", "tipc", "bluetooth",
"iucv", "rxrpc", "isdn", "phonet", "ieee802154", "caif", "alg",
"nfc", "vsock", "kcm", "qipcrtr", "smc", "xdp", "mctp",
},
"type": []string{
"stream", "dgram", "seqpacket", "rdm", "raw", "packet",
},
"protocol": []string{"tcp", "udp", "icmp"},
}
}
type AddressExpr struct {
Source string
Destination string
Port string
}
2024-04-15 00:58:34 +02:00
func newAddressExprFromLog(log map[string]string) AddressExpr {
return AddressExpr{
Source: log["laddr"],
Destination: log["faddr"],
Port: log["lport"],
}
}
func (r AddressExpr) Compare(other AddressExpr) int {
if res := compare(r.Source, other.Source); res != 0 {
return res
2024-04-15 00:58:34 +02:00
}
if res := compare(r.Destination, other.Destination); res != 0 {
return res
}
return compare(r.Port, other.Port)
2024-04-15 00:58:34 +02:00
}
func (r AddressExpr) Equals(other AddressExpr) bool {
return r.Source == other.Source && r.Destination == other.Destination &&
r.Port == other.Port
}
type Network struct {
RuleBase
Qualifier
2024-04-15 00:58:34 +02:00
AddressExpr
Domain string
Type string
Protocol string
}
func newNetworkFromLog(log map[string]string) Rule {
return &Network{
RuleBase: newBaseFromLog(log),
2024-04-15 00:58:34 +02:00
Qualifier: newQualifierFromLog(log),
AddressExpr: newAddressExprFromLog(log),
Domain: log["family"],
Type: log["sock_type"],
Protocol: log["protocol"],
}
}
func (r *Network) Validate() error {
if err := validateValues(r.Kind(), "domains", []string{r.Domain}); err != nil {
return fmt.Errorf("%s: %w", r, err)
}
if err := validateValues(r.Kind(), "type", []string{r.Type}); err != nil {
return fmt.Errorf("%s: %w", r, err)
}
if err := validateValues(r.Kind(), "protocol", []string{r.Protocol}); err != nil {
return fmt.Errorf("%s: %w", r, err)
}
return nil
}
func (r *Network) Compare(other Rule) int {
o, _ := other.(*Network)
if res := compare(r.Domain, o.Domain); 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 := r.AddressExpr.Compare(o.AddressExpr); res != 0 {
return res
2024-04-15 00:58:34 +02:00
}
return r.Qualifier.Compare(o.Qualifier)
}
func (r *Network) String() string {
return renderTemplate(r.Kind(), r)
}
func (r *Network) Constraint() constraint {
return blockKind
}
func (r *Network) Kind() Kind {
return NETWORK
}