2023-04-17 00:31:05 +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-04-17 00:31:05 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
|
|
|
package logs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"regexp"
|
2024-04-12 21:07:05 +02:00
|
|
|
"slices"
|
2023-04-17 00:31:05 +02:00
|
|
|
"strings"
|
|
|
|
|
2023-08-18 00:12:46 +02:00
|
|
|
"github.com/roddhjav/apparmor.d/pkg/aa"
|
2023-04-17 00:31:05 +02:00
|
|
|
"github.com/roddhjav/apparmor.d/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Colors
|
|
|
|
const (
|
2023-05-01 23:21:30 +02:00
|
|
|
reset = "\033[0m"
|
|
|
|
fgGreen = "\033[32m"
|
|
|
|
fgYellow = "\033[33m"
|
|
|
|
fgBlue = "\033[34m"
|
|
|
|
fgMagenta = "\033[35m"
|
|
|
|
fgCian = "\033[36m"
|
|
|
|
fgWhite = "\033[37m"
|
|
|
|
boldRed = "\033[1;31m"
|
|
|
|
boldGreen = "\033[1;32m"
|
|
|
|
boldYellow = "\033[1;33m"
|
2023-04-17 00:31:05 +02:00
|
|
|
)
|
|
|
|
|
2024-09-26 23:25:24 +02:00
|
|
|
const (
|
|
|
|
h = `[0-9a-fA-F]`
|
|
|
|
d = `[0-9]`
|
|
|
|
)
|
|
|
|
|
2023-04-17 00:31:05 +02:00
|
|
|
var (
|
|
|
|
quoted bool
|
|
|
|
isAppArmorLogTemplate = regexp.MustCompile(`apparmor=("DENIED"|"ALLOWED"|"AUDIT")`)
|
2023-08-17 20:12:02 +02:00
|
|
|
regCleanLogs = util.ToRegexRepl([]string{
|
|
|
|
// Clean apparmor log file
|
2023-07-23 18:00:52 +02:00
|
|
|
`.*apparmor="`, `apparmor="`,
|
|
|
|
`(peer_|)pid=[0-9]*\s`, " ",
|
|
|
|
`\x1d`, " ",
|
2023-08-17 20:12:02 +02:00
|
|
|
|
2024-06-20 00:39:23 +02:00
|
|
|
// Remove basic rules from abstractions/base
|
2024-09-18 19:31:24 +02:00
|
|
|
`(?m)^.*/etc/[^/]+\.so.*$`, ``,
|
|
|
|
`(?m)^.*/usr/(lib|lib32|lib64|libexec)/[^/]+\.so.*$`, ``,
|
2024-06-20 00:39:23 +02:00
|
|
|
`(?m)^.*/usr/(lib|lib32|lib64|libexec)/locale/.*$`, ``,
|
|
|
|
`(?m)^.*/usr/share/locale[^/]?/.*$`, ``,
|
|
|
|
`(?m)^.*/usr/share/zoneinfo[^/]?/.*$`, ``,
|
|
|
|
`(?m)^.*/dev/(null|zero|full|log).*$`, ``,
|
|
|
|
`(?m)^.*/dev/(u|)random.*$`, ``,
|
|
|
|
})
|
|
|
|
regResolveLogs = util.ToRegexRepl([]string{
|
2024-07-04 23:25:09 +02:00
|
|
|
// Resolve user variables
|
2023-08-17 20:12:02 +02:00
|
|
|
`/home/[^/]+/.cache`, `@{user_cache_dirs}`,
|
|
|
|
`/home/[^/]+/.config`, `@{user_config_dirs}`,
|
|
|
|
`/home/[^/]+/.local/share`, `@{user_share_dirs}`,
|
|
|
|
`/home/[^/]+/.local/state`, `@{user_state_dirs}`,
|
|
|
|
`/home/[^/]+/.local/bin`, `@{user_bin_dirs}`,
|
|
|
|
`/home/[^/]+/.local/lib`, `@{user_lib_dirs}`,
|
|
|
|
`/home/[^/]+/.ssh`, `@{HOME}/@{XDG_SSH_DIR}`,
|
|
|
|
`/home/[^/]+/.gnupg`, `@{HOME}/@{XDG_GPG_DIR}`,
|
2024-06-20 00:39:23 +02:00
|
|
|
`/home/[^/]+/`, `@{HOME}/`,
|
2023-08-17 20:12:02 +02:00
|
|
|
|
2024-07-04 23:25:09 +02:00
|
|
|
// Resolve system variables
|
2024-09-26 23:25:24 +02:00
|
|
|
`/usr/lib(32|64|exec)`, `@{lib}`,
|
|
|
|
`/usr/lib`, `@{lib}`,
|
2024-06-20 00:39:23 +02:00
|
|
|
`/usr/(bin|sbin)`, `@{bin}`,
|
2024-09-26 23:25:24 +02:00
|
|
|
`(x86_64|amd64|i386|i686)`, `@{arch}`,
|
|
|
|
`@{arch}-*linux-gnu[^/]?`, `@{multiarch}`,
|
2024-04-02 18:50:33 +02:00
|
|
|
`/usr/etc/`, `@{etc_ro}/`,
|
2024-06-20 00:39:23 +02:00
|
|
|
`/var/run/`, `@{run}/`,
|
2023-08-17 20:12:02 +02:00
|
|
|
`/run/`, `@{run}/`,
|
|
|
|
`user/[0-9]*/`, `user/@{uid}/`,
|
2024-06-20 00:39:23 +02:00
|
|
|
`/tmp/user/@{uid}/`, `@{tmp}/`,
|
2023-08-17 20:12:02 +02:00
|
|
|
`/proc/`, `@{PROC}/`,
|
2024-09-26 23:25:24 +02:00
|
|
|
`@{PROC}/1/`, `@{PROC}/one/`, // Go does not support lookahead assertions like (?!1\b)d+, so we have to use a workaround
|
2023-08-17 20:12:02 +02:00
|
|
|
`@{PROC}/[0-9]*/`, `@{PROC}/@{pid}/`,
|
2024-09-26 23:25:24 +02:00
|
|
|
`@{PROC}/one/`, `@{PROC}/1/`,
|
2023-08-17 20:12:02 +02:00
|
|
|
`@{PROC}/@{pid}/task/[0-9]*/`, `@{PROC}/@{pid}/task/@{tid}/`,
|
|
|
|
`/sys/`, `@{sys}/`,
|
|
|
|
`@{PROC}@{sys}/`, `@{PROC}/sys/`,
|
2024-09-26 23:25:24 +02:00
|
|
|
`pci` + strings.Repeat(h, 4) + `:` + strings.Repeat(h, 2), `@{pci_bus}`,
|
2024-10-02 21:48:36 +02:00
|
|
|
`@{pci_bus}/[0-9a-f:*./]*/`, `@{pci}/`,
|
2024-07-04 23:25:09 +02:00
|
|
|
`1000`, `@{uid}`,
|
2023-08-17 20:12:02 +02:00
|
|
|
|
|
|
|
// Some system glob
|
2024-09-26 23:25:24 +02:00
|
|
|
`:not.active.yet`, `@{busname}`, // dbus unique bus name
|
|
|
|
`:1.[0-9]*`, `@{busname}`, // dbus unique bus name
|
2024-03-01 00:14:01 +01:00
|
|
|
`@{bin}/(|ba|da)sh`, `@{sh_path}`, // collect all shell
|
2023-08-17 20:12:02 +02:00
|
|
|
`@{lib}/modules/[^/]+\/`, `@{lib}/modules/*/`, // strip kernel version numbers from kernel module accesses
|
|
|
|
|
2024-06-20 00:39:23 +02:00
|
|
|
// int, hex, uuid
|
2024-09-26 23:25:24 +02:00
|
|
|
strings.Repeat(h, 8) + `[-_]` + strings.Repeat(h, 4) + `[-_]` + strings.Repeat(h, 4) + `[-_]` + strings.Repeat(h, 4) + `[-_]` + strings.Repeat(h, 12), `@{uuid}`,
|
|
|
|
strings.Repeat(d, 64), `@{int64}`,
|
|
|
|
strings.Repeat(h, 64), `@{hex64}`,
|
|
|
|
strings.Repeat(h, 38), `@{hex38}`,
|
|
|
|
strings.Repeat(d, 32), `@{int32}`,
|
|
|
|
strings.Repeat(h, 32), `@{hex32}`,
|
|
|
|
strings.Repeat(d, 16), `@{int16}`,
|
|
|
|
strings.Repeat(h, 16), `@{hex16}`,
|
|
|
|
strings.Repeat(d, 10), `@{int10}`,
|
|
|
|
strings.Repeat(d, 8), `@{int8}`,
|
|
|
|
strings.Repeat(d, 6), `@{int6}`,
|
2023-07-23 18:00:52 +02:00
|
|
|
})
|
2023-04-17 00:31:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type AppArmorLog map[string]string
|
|
|
|
|
|
|
|
// AppArmorLogs describes all apparmor log entries
|
|
|
|
type AppArmorLogs []AppArmorLog
|
|
|
|
|
|
|
|
func splitQuoted(r rune) bool {
|
|
|
|
if r == '"' {
|
|
|
|
quoted = !quoted
|
|
|
|
}
|
|
|
|
return !quoted && r == ' '
|
|
|
|
}
|
|
|
|
|
|
|
|
func toQuote(str string) string {
|
|
|
|
if strings.Contains(str, " ") {
|
|
|
|
return `"` + str + `"`
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2024-09-26 23:25:24 +02:00
|
|
|
// New returns a new ApparmorLogs list of map from a log file
|
|
|
|
func New(file io.Reader, profile string) AppArmorLogs {
|
2023-09-30 16:34:30 +02:00
|
|
|
logs := GetApparmorLogs(file, profile)
|
2023-04-17 00:31:05 +02:00
|
|
|
|
|
|
|
// Parse log into ApparmorLog struct
|
|
|
|
aaLogs := make(AppArmorLogs, 0)
|
2024-06-20 00:39:23 +02:00
|
|
|
toClean := []string{"profile", "name", "target"}
|
2023-04-17 00:31:05 +02:00
|
|
|
for _, log := range logs {
|
|
|
|
quoted = false
|
|
|
|
tmp := strings.FieldsFunc(log, splitQuoted)
|
|
|
|
|
|
|
|
aa := make(AppArmorLog)
|
|
|
|
for _, item := range tmp {
|
2024-10-02 22:06:45 +02:00
|
|
|
kv := strings.FieldsFunc(item, func(r rune) bool {
|
|
|
|
if r == '"' {
|
|
|
|
quoted = !quoted
|
|
|
|
}
|
|
|
|
return !quoted && r == '='
|
|
|
|
})
|
2023-04-17 00:31:05 +02:00
|
|
|
if len(kv) >= 2 {
|
2024-06-20 00:39:23 +02:00
|
|
|
key, value := kv[0], kv[1]
|
|
|
|
if slices.Contains(toClean, key) {
|
|
|
|
value = regResolveLogs.Replace(kv[1])
|
|
|
|
}
|
|
|
|
aa[key] = strings.Trim(value, `"`)
|
2023-04-17 00:31:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
aaLogs = append(aaLogs, aa)
|
|
|
|
}
|
|
|
|
|
|
|
|
return aaLogs
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a formatted AppArmor logs string
|
|
|
|
func (aaLogs AppArmorLogs) String() string {
|
2023-04-19 20:03:47 +02:00
|
|
|
// Apparmor log states
|
|
|
|
state := map[string]string{
|
2023-05-01 23:21:30 +02:00
|
|
|
"DENIED": boldRed + "DENIED " + reset,
|
|
|
|
"ALLOWED": boldGreen + "ALLOWED" + reset,
|
|
|
|
"AUDIT": boldYellow + "AUDIT " + reset,
|
2023-04-19 20:03:47 +02:00
|
|
|
}
|
|
|
|
// Print order of impression
|
|
|
|
keys := []string{
|
|
|
|
"profile", "label", // Profile name
|
2023-07-21 00:45:14 +02:00
|
|
|
"operation", "name", "target",
|
2023-04-19 20:03:47 +02:00
|
|
|
"mask", "bus", "path", "interface", "member", // dbus
|
|
|
|
"info", "comm",
|
|
|
|
"laddr", "lport", "faddr", "fport", "family", "sock_type", "protocol",
|
2024-03-01 00:37:50 +01:00
|
|
|
"requested_mask", "denied_mask", "signal", "peer", "peer_label",
|
2023-04-19 20:03:47 +02:00
|
|
|
}
|
2023-07-21 00:45:14 +02:00
|
|
|
// Key to not print
|
|
|
|
ignore := []string{
|
|
|
|
"fsuid", "ouid", "FSUID", "OUID", "exe", "SAUID", "sauid", "terminal",
|
2024-03-01 00:13:15 +01:00
|
|
|
"UID", "AUID", "hostname", "class",
|
2023-07-21 00:45:14 +02:00
|
|
|
}
|
2023-04-19 20:03:47 +02:00
|
|
|
// Color template to use
|
2024-03-23 14:41:19 +01:00
|
|
|
template := map[string]string{
|
2023-05-01 23:21:30 +02:00
|
|
|
"profile": fgBlue,
|
|
|
|
"label": fgBlue,
|
|
|
|
"operation": fgYellow,
|
|
|
|
"name": fgMagenta,
|
2023-07-21 00:45:14 +02:00
|
|
|
"target": "-> " + fgMagenta,
|
2023-05-01 23:21:30 +02:00
|
|
|
"mask": boldRed,
|
|
|
|
"bus": fgCian + "bus=",
|
|
|
|
"path": "path=" + fgWhite,
|
|
|
|
"requested_mask": "requested_mask=" + boldRed,
|
|
|
|
"denied_mask": "denied_mask=" + boldRed,
|
|
|
|
"interface": "interface=" + fgWhite,
|
|
|
|
"member": "member=" + fgGreen,
|
2023-04-19 20:03:47 +02:00
|
|
|
}
|
2024-03-23 14:41:19 +01:00
|
|
|
var res strings.Builder
|
|
|
|
|
2023-04-17 00:31:05 +02:00
|
|
|
for _, log := range aaLogs {
|
|
|
|
seen := map[string]bool{"apparmor": true}
|
2024-03-23 14:41:19 +01:00
|
|
|
res.WriteString(state[log["apparmor"]])
|
2024-10-06 21:17:19 +02:00
|
|
|
owner := aa.IsOwner(log)
|
2023-04-17 00:31:05 +02:00
|
|
|
|
|
|
|
for _, key := range keys {
|
2024-03-23 14:41:19 +01:00
|
|
|
if item, present := log[key]; present {
|
2024-10-06 21:17:19 +02:00
|
|
|
if key == "name" && owner {
|
2024-03-23 14:41:19 +01:00
|
|
|
res.WriteString(template[key] + " owner" + reset)
|
2023-07-21 00:45:14 +02:00
|
|
|
}
|
2024-03-23 14:41:19 +01:00
|
|
|
if temp, present := template[key]; present {
|
|
|
|
res.WriteString(" " + temp + toQuote(item) + reset)
|
2023-04-17 00:31:05 +02:00
|
|
|
} else {
|
2024-03-23 14:41:19 +01:00
|
|
|
res.WriteString(" " + key + "=" + toQuote(item))
|
2023-04-17 00:31:05 +02:00
|
|
|
}
|
|
|
|
seen[key] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value := range log {
|
2023-07-21 00:45:14 +02:00
|
|
|
if slices.Contains(ignore, key) {
|
|
|
|
continue
|
|
|
|
}
|
2024-03-23 14:41:19 +01:00
|
|
|
if _, present := seen[key]; !present && value != "" {
|
|
|
|
res.WriteString(" " + key + "=" + toQuote(value))
|
2023-04-17 00:31:05 +02:00
|
|
|
}
|
|
|
|
}
|
2024-03-23 14:41:19 +01:00
|
|
|
res.WriteString("\n")
|
2023-04-17 00:31:05 +02:00
|
|
|
}
|
2024-03-23 14:41:19 +01:00
|
|
|
return res.String()
|
2023-04-17 00:31:05 +02:00
|
|
|
}
|
2023-08-18 00:12:46 +02:00
|
|
|
|
|
|
|
// ParseToProfiles convert the log data into a new AppArmorProfiles
|
2024-04-23 22:17:25 +02:00
|
|
|
func (aaLogs AppArmorLogs) ParseToProfiles() map[string]*aa.Profile {
|
|
|
|
profiles := make(map[string]*aa.Profile, 0)
|
2023-08-18 00:12:46 +02:00
|
|
|
for _, log := range aaLogs {
|
|
|
|
name := ""
|
|
|
|
if strings.Contains(log["operation"], "dbus") {
|
|
|
|
name = log["label"]
|
|
|
|
} else {
|
|
|
|
name = log["profile"]
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := profiles[name]; !ok {
|
2024-04-23 22:17:25 +02:00
|
|
|
profile := &aa.Profile{Header: aa.Header{Name: name}}
|
2023-08-18 00:12:46 +02:00
|
|
|
profile.AddRule(log)
|
|
|
|
profiles[name] = profile
|
|
|
|
} else {
|
|
|
|
profiles[name].AddRule(log)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return profiles
|
|
|
|
}
|