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"
|
|
|
|
"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"
|
2023-07-21 00:45:14 +02:00
|
|
|
"golang.org/x/exp/slices"
|
2023-04-17 00:31:05 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// Resolve classic user variables
|
|
|
|
`/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}`,
|
|
|
|
`/home/[^/]+`, `@{HOME}`,
|
|
|
|
|
|
|
|
// Resolve classic system variables
|
|
|
|
`/usr/lib(|32|64|exec)`, `@{lib}`,
|
|
|
|
`/usr/(|s)bin`, `@{bin}`,
|
2023-09-01 20:19:19 +02:00
|
|
|
`[^/]+-linux-gnu[^/]?`, `@{multiarch}`,
|
2023-08-17 20:12:02 +02:00
|
|
|
`/run/`, `@{run}/`,
|
|
|
|
`user/[0-9]*/`, `user/@{uid}/`,
|
|
|
|
`/proc/`, `@{PROC}/`,
|
|
|
|
`@{PROC}/[0-9]*/`, `@{PROC}/@{pid}/`,
|
|
|
|
`@{PROC}/@{pid}/task/[0-9]*/`, `@{PROC}/@{pid}/task/@{tid}/`,
|
|
|
|
`/sys/`, `@{sys}/`,
|
|
|
|
`@{PROC}@{sys}/`, `@{PROC}/sys/`,
|
2023-10-11 00:47:31 +02:00
|
|
|
`pci[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]:[0-9a-fA-F][0-9a-fA-F]`, `@{pci_bus}`,
|
2023-08-17 20:12:02 +02:00
|
|
|
|
|
|
|
// Some system glob
|
|
|
|
`:1.[0-9]*`, `:*`, // dbus peer 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-03-19 22:26:12 +01:00
|
|
|
`[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]`, `@{hex32}`,
|
2023-10-11 00:47:31 +02:00
|
|
|
`[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][-_][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][-_][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][-_][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][-_][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]`, `@{uuid}`,
|
|
|
|
`[0-9][0-9][0-9][0-9][0-9][0-9]+`, `@{int}`,
|
2023-08-17 20:12:02 +02:00
|
|
|
|
|
|
|
// Remove basic rules from abstractions/base
|
|
|
|
`(?m)^.*/etc/[^/]+so.*$`, ``,
|
|
|
|
`(?m)^.*@{lib}/[^/]+so.*$`, ``,
|
|
|
|
`(?m)^.*@{lib}/locale/.*$`, ``,
|
2023-09-01 20:19:19 +02:00
|
|
|
`(?m)^.*/usr/share/locale[^/]?/.*$`, ``,
|
|
|
|
`(?m)^.*/usr/share/zoneinfo[^/]?/.*$`, ``,
|
|
|
|
`(?m)^.*/dev/(null|zero|full|log).*$`, ``,
|
2023-08-17 20:12:02 +02:00
|
|
|
`(?m)^.*/dev/(u|)random.*$`, ``,
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewApparmorLogs return a new ApparmorLogs list of map from a log file
|
|
|
|
func NewApparmorLogs(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)
|
|
|
|
for _, log := range logs {
|
|
|
|
quoted = false
|
|
|
|
tmp := strings.FieldsFunc(log, splitQuoted)
|
|
|
|
|
|
|
|
aa := make(AppArmorLog)
|
|
|
|
for _, item := range tmp {
|
|
|
|
kv := strings.Split(item, "=")
|
|
|
|
if len(kv) >= 2 {
|
|
|
|
aa[kv[0]] = strings.Trim(kv[1], `"`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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"]])
|
2023-07-21 00:45:14 +02:00
|
|
|
fsuid := log["fsuid"]
|
|
|
|
ouid := log["ouid"]
|
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 {
|
2023-07-21 00:45:14 +02:00
|
|
|
if key == "name" && fsuid == ouid && !strings.Contains(log["operation"], "dbus") {
|
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
|
|
|
|
func (aaLogs AppArmorLogs) ParseToProfiles() aa.AppArmorProfiles {
|
|
|
|
profiles := make(aa.AppArmorProfiles, 0)
|
|
|
|
for _, log := range aaLogs {
|
|
|
|
name := ""
|
|
|
|
if strings.Contains(log["operation"], "dbus") {
|
|
|
|
name = log["label"]
|
|
|
|
} else {
|
|
|
|
name = log["profile"]
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := profiles[name]; !ok {
|
|
|
|
profile := &aa.AppArmorProfile{}
|
|
|
|
profile.Name = name
|
|
|
|
profile.AddRule(log)
|
|
|
|
profiles[name] = profile
|
|
|
|
} else {
|
|
|
|
profiles[name].AddRule(log)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return profiles
|
|
|
|
}
|