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-05-25 23:36:39 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
2024-06-20 00:22:49 +02:00
|
|
|
"slices"
|
2024-05-25 23:36:39 +02:00
|
|
|
)
|
|
|
|
|
2024-05-28 19:15:22 +02:00
|
|
|
const NETWORK Kind = "network"
|
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[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"},
|
|
|
|
}
|
|
|
|
}
|
2024-04-23 22:26:09 +02:00
|
|
|
|
2023-09-25 01:06:07 +02:00
|
|
|
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"],
|
|
|
|
}
|
2023-09-25 01:09:11 +02:00
|
|
|
}
|
|
|
|
|
2024-06-19 19:34:58 +02:00
|
|
|
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
|
|
|
}
|
2024-06-19 19:34:58 +02:00
|
|
|
if res := compare(r.Destination, other.Destination); res != 0 {
|
|
|
|
return res
|
2023-09-25 01:09:11 +02:00
|
|
|
}
|
2024-06-19 19:34:58 +02:00
|
|
|
return compare(r.Port, other.Port)
|
2024-04-15 00:58:34 +02:00
|
|
|
}
|
|
|
|
|
2023-09-25 01:06:07 +02:00
|
|
|
type Network struct {
|
2024-06-25 20:50:27 +02:00
|
|
|
Base
|
2023-09-25 01:06:07 +02:00
|
|
|
Qualifier
|
2024-04-15 00:58:34 +02:00
|
|
|
AddressExpr
|
2023-09-25 01:06:07 +02:00
|
|
|
Domain string
|
|
|
|
Type string
|
|
|
|
Protocol string
|
|
|
|
}
|
|
|
|
|
2024-06-20 00:22:49 +02:00
|
|
|
func newNetwork(q Qualifier, rule rule) (Rule, error) {
|
|
|
|
nType, protocol, domain := "", "", ""
|
|
|
|
r := rule.GetSlice()
|
|
|
|
if len(r) > 0 {
|
|
|
|
domain = r[0]
|
|
|
|
}
|
|
|
|
if len(r) >= 2 {
|
|
|
|
if slices.Contains(requirements[NETWORK]["type"], r[1]) {
|
|
|
|
nType = r[1]
|
|
|
|
} else if slices.Contains(requirements[NETWORK]["protocol"], r[1]) {
|
|
|
|
protocol = r[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &Network{
|
2024-06-25 20:50:27 +02:00
|
|
|
Base: newBase(rule),
|
2024-06-20 00:22:49 +02:00
|
|
|
Qualifier: q,
|
|
|
|
Domain: domain,
|
|
|
|
Type: nType,
|
|
|
|
Protocol: protocol,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-04-19 23:43:02 +02:00
|
|
|
func newNetworkFromLog(log map[string]string) Rule {
|
2023-09-25 01:06:07 +02:00
|
|
|
return &Network{
|
2024-06-25 20:50:27 +02:00
|
|
|
Base: 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"],
|
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 *Network) Kind() Kind {
|
|
|
|
return NETWORK
|
|
|
|
}
|
|
|
|
|
2024-06-27 19:45:32 +02:00
|
|
|
func (r *Network) Constraint() Constraint {
|
|
|
|
return BlockRule
|
2024-06-25 20:56:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Network) String() string {
|
|
|
|
return renderTemplate(r.Kind(), r)
|
|
|
|
}
|
|
|
|
|
2024-05-25 23:36:39 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-06-19 19:34:58 +02:00
|
|
|
func (r *Network) Compare(other Rule) int {
|
2023-09-25 01:09:11 +02:00
|
|
|
o, _ := other.(*Network)
|
2024-06-19 19:34:58 +02:00
|
|
|
if res := compare(r.Domain, o.Domain); res != 0 {
|
|
|
|
return res
|
2023-09-25 01:09:11 +02:00
|
|
|
}
|
2024-06-19 19:34:58 +02:00
|
|
|
if res := compare(r.Type, o.Type); res != 0 {
|
|
|
|
return res
|
2024-04-15 00:58:34 +02:00
|
|
|
}
|
2024-06-19 19:34:58 +02:00
|
|
|
if res := compare(r.Protocol, o.Protocol); res != 0 {
|
|
|
|
return res
|
2024-04-15 00:58:34 +02:00
|
|
|
}
|
2024-06-19 19:34:58 +02:00
|
|
|
if res := r.AddressExpr.Compare(o.AddressExpr); 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-25 21:10:12 +02:00
|
|
|
|
|
|
|
func (r *Network) Merge(other Rule) bool {
|
|
|
|
return false // Never merge network
|
|
|
|
}
|