feat(aa-log): rewrite the profile template.

This commit is contained in:
Alexandre Pujol 2023-09-25 00:15:51 +01:00
parent 422418e0e2
commit 88f275ef43
Failed to generate hash of commit
2 changed files with 210 additions and 251 deletions

View file

@ -6,21 +6,81 @@ package aa
import ( import (
_ "embed" _ "embed"
"reflect"
"strings"
"text/template" "text/template"
) )
// Default indentation for apparmor profile (2 spaces)
const indentation = " " const indentation = " "
//go:embed template.j2 var (
var tmplFileAppArmorProfile string //go:embed template.j2
tmplFileAppArmorProfile string
var tmplFunctionMap = template.FuncMap{ // tmplFunctionMap is the list of function available in the template
"indent": indent, tmplFunctionMap = template.FuncMap{
"overindent": indentDbus, "typeof": typeOf,
"join": join,
"indent": indent,
"overindent": indentDbus,
}
// The apparmor profile template
tmplAppArmorProfile = template.Must(template.New("profile").
Funcs(tmplFunctionMap).Parse(tmplFileAppArmorProfile))
// convert apparmor requested mask to apparmor access mode
// TODO: Should be a map of slice, not exhausive yet
maskToAccess = map[string]string{
"a": "w",
"c": "w",
"d": "w",
"k": "rk",
"l": "l",
"m": "rm",
"r": "r",
"ra": "rw",
"read write": "read write",
"read": "read",
"readby": "readby",
"receive": "receive",
"rm": "rm",
"rw": "rw",
"send receive": "send receive",
"send": "send",
"w": "w",
"wc": "w",
"wd": "w",
"wk": "wk",
"wr": "rw",
"wrc": "rw",
"wrd": "rw",
"write": "write",
"x": "rix",
}
)
func join(i any) string {
switch reflect.TypeOf(i).Kind() {
case reflect.Slice:
return strings.Join(i.([]string), " ")
case reflect.Map:
res := []string{}
for k, v := range i.(map[string]string) {
res = append(res, k+"="+v)
}
return strings.Join(res, " ")
default:
return i.(string)
}
} }
var tmplAppArmorProfile = template.Must(template.New("profile"). func typeOf(i any) string {
Funcs(tmplFunctionMap).Parse(tmplFileAppArmorProfile)) return strings.TrimPrefix(reflect.TypeOf(i).String(), "*aa.")
}
func indent(s string) string { func indent(s string) string {
return indentation + s return indentation + s
@ -29,31 +89,3 @@ func indent(s string) string {
func indentDbus(s string) string { func indentDbus(s string) string {
return indentation + " " + s return indentation + " " + s
} }
// TODO: Should be a map of slice, not exhausive yet
var maskToAccess = map[string]string{
"a": "w",
"c": "w",
"d": "w",
"k": "rk",
"l": "l",
"m": "rm",
"r": "r",
"ra": "rw",
"read write": "read write",
"read": "read",
"readby": "readby",
"receive": "receive",
"rm": "rm",
"rw": "rw",
"send receive": "send receive",
"send": "send",
"w": "w",
"wc": "w",
"wr": "rw",
"wrc": "rw",
"wrd": "rw",
"write": "write",
"x": "rix",
}

View file

@ -2,307 +2,234 @@
{{- /* Copyright (C) 2021-2023 Alexandre Pujol <alexandre@pujol.io> */ -}} {{- /* Copyright (C) 2021-2023 Alexandre Pujol <alexandre@pujol.io> */ -}}
{{- /* SPDX-License-Identifier: GPL-2.0-only */ -}} {{- /* SPDX-License-Identifier: GPL-2.0-only */ -}}
{{- if .Abi -}} {{- range .Abi -}}
{{- range .Abi -}} {{- if .IsMagic -}}
{{- if .IsMagic -}} {{ "abi <" }}{{ .Path }}{{ ">,\n" }}
abi <{{ .Path }}>,{{ "\n" }} {{- else -}}
{{- else -}} {{ "abi \"" }}{{ .Path }}{{ "\",\n" }}
abi "{{ .Path }}",{{ "\n" }} {{- end }}
{{- end -}} {{ end -}}
{{- end -}}
{{ "\n" }}
{{- end -}}
{{- if .Aliases -}} {{- range .Aliases -}}
{{- range .Aliases -}} {{ "alias " }}{{ .Path }}{{ " -> " }}{{ .RewrittenPath }}{{ ",\n" }}
alias {{ .Path }} -> {{ .RewrittenPath }},{{ "\n" }} {{ end -}}
{{- end -}}
{{ "\n" }}
{{- end -}}
{{- if .PreambleIncludes -}} {{- range .Includes -}}
{{- range .PreambleIncludes -}} {{- "include " -}}
{{- "include " -}} {{- if .IfExists -}}
{{- if .IfExists -}} {{- "if exists " -}}
{{- "if exists " -}}
{{- end -}}
{{- if .IsMagic -}}
<{{ .Path }}>{{ "\n" }}
{{- else -}}
"{{ .Path }}"{{ "\n" }}
{{- end -}}
{{- end -}} {{- end -}}
{{ "\n" }} {{- if .IsMagic -}}
{{- end -}} {{ "<" }}{{ .Path }}{{ ">,\n" }}
{{- else -}}
{{ "\"" }}{{ .Path }}{{ "\",\n" }}
{{- end }}
{{ end -}}
{{- if .Preamble.Variables -}} {{- range .Variables -}}
{{- range .Preamble.Variables -}} {{ "@{" }}{{ .Name }}{{ "} = " }}
{{ "@{" }}{{ .Name }}{{ "} = " }} {{- range .Values -}}
{{- range .Values -}} {{ . }}{{ " " }}
{{ . }}{{ " " }} {{- end }}
{{- end -}} {{ end -}}
{{ "\n" }}
{{- end -}}
{{- end -}}
profile {{ .Name }}{{ " " }} {{- "profile " -}}
{{- range .Attachments -}} {{- with .Name -}}
{{ . }}{{ " " }} {{ . }}{{ " " }}
{{- end -}} {{- end -}}
{{- if .Attributes -}} {{- with .Attachments -}}
{{- "xattrs=(" -}} {{ join . }}{{ " " }}
{{- range .Attributes -}}
{{ . }}{{ " " }}
{{- end -}}
{{ ") " }}
{{- end -}} {{- end -}}
{{- if .Flags -}} {{- with .Attributes -}}
{{- "flags=(" -}} {{ "xattrs=(" }}{{ join . }}{{ ") " }}
{{- range .Flags -}} {{- end -}}
{{ . }}{{ " " }} {{- with .Flags -}}
{{- end -}} {{ "flags=(" }}{{ join . }}{{ ") " }}
{{ ") " }}
{{- end -}} {{- end -}}
{{ "{\n" }} {{ "{\n" }}
{{- if .Includes -}} {{- $oldtype := "" -}}
{{- range .Includes -}} {{- range .Rules -}}
{{- if not .IfExists -}} {{- $type := typeof . -}}
{{- "include " | indent -}} {{- if and (ne $type $oldtype) (ne $oldtype "") -}}
{{- if .AbsPath -}} {{- "\n" -}}
"{{ . }}"{{ "\n" }} {{- end -}}
{{- else -}} {{- indent "" -}}
<{{ .MagicPath }}>{{ "\n" }}
{{- end -}} {{- if eq $type "Include" -}}
{{- "include " -}}
{{- if .IfExists -}}
{{ "if exists " }}
{{- end -}}
{{- if .IsMagic -}}
{{ "<" }}{{ .Path }}{{ ">" }}
{{- else -}}
{{ "\"" }}{{ .Path }}{{ "\"" }}
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}
{{ "\n" }}
{{- end -}}
{{- if .Rlimit -}} {{- if eq $type "Rlimit" -}}
{{- range .Rlimit -}} {{ "set rlimit " }}{{ .Key }} {{ .Op }} {{ .Value }}{{ "," }}
{{ "set rlimit" | indent }} {{ .Key }} {{ .Op }} {{ .Value }},{{ "\n" }}
{{- end -}} {{- end -}}
{{ "\n" }}
{{- end -}}
{{- if .Capability -}} {{- if eq $type "Capability" -}}
{{- range .Capability -}} {{ "capability " }}{{ .Name }}{{ "," }}
{{ "capability" | indent }} {{ .Name }},{{ "\n" }}
{{- end -}} {{- end -}}
{{ "\n" }}
{{- end -}}
{{- if .Network -}} {{- if eq $type "Network" -}}
{{- range .Network -}}
{{- if eq .AccessType "deny" -}} {{- if eq .AccessType "deny" -}}
{{ "deny network " | indent }} {{ "deny " }}
{{- end -}}
{{ "network " }}
{{- with .Domain -}}
{{ . }}{{ " " }}
{{- end -}}
{{- with .Type -}}
{{ . }}
{{- else -}} {{- else -}}
{{ "network " | indent }} {{- with .Protocol -}}
{{- end -}} {{ . }}
{{- if .Domain -}}
{{ .Domain }}{{ " " }}
{{- end -}}
{{- if .Type -}}
{{ .Type }}{{ " " }}
{{- else -}}
{{ if .Protocol -}}
{{ .Protocol }}{{ " " }}
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}
{{- if .Destination -}} {{- "," -}}
{{ "dst=" }}{{ .Destination }}
{{- end -}}
,{{ "\n" }}
{{- end -}} {{- end -}}
{{ "\n" }}
{{- end -}}
{{- if .Mount -}} {{- if eq $type "Mount" -}}
{{- range .Mount -}} {{- "mount " -}}
{{- "mount " | indent -}} {{- with .FsType -}}
{{- if .FsType -}} {{ "fstype=" }}{{ . }}{{ " " }}
fstype={{ .FsType }}{{ " " }}
{{- end -}} {{- end -}}
{{- if .Options -}} {{- with .Options -}}
{{- "options=(" -}} {{ "options=(" }}{{ join . }}{{ ") " }}
{{- range .Options -}}
{{ . }}{{ " " }}
{{- end -}}
{{ ") " }}
{{- end -}} {{- end -}}
{{- if .Source -}} {{- with .Source -}}
{{ .Source }}{{ " " }} {{ . }}{{ " " }}
{{- end -}} {{- end -}}
{{- if .MountPoint -}} {{- with .MountPoint -}}
{{ "-> " }}{{ .MountPoint }} {{ "-> " }}{{ . }}
{{- end -}} {{- end -}}
,{{ "\n" }} {{- "," -}}
{{- end -}} {{- end -}}
{{ "\n" }}
{{- end -}}
{{- if .Umount -}} {{- if eq $type "Umount" -}}
{{- range .Umount -}} {{- "umount " -}}
{{- "umount " | indent -}} {{- with .FsType -}}
{{- if .FsType -}} {{ "fstype=" }}{{ . }}{{ " " }}
fstype={{ .FsType }}{{ " " }}
{{- end -}} {{- end -}}
{{- if .Options -}} {{- with .Options -}}
{{- "options=(" -}} {{ "options=(" }}{{ join . }}{{ ") " }}
{{- range .Options -}}
{{ . }}{{ " " }}
{{- end -}}
{{ ") " }}
{{- end -}} {{- end -}}
{{- if .MountPoint -}} {{- with .MountPoint -}}
{{ .MountPoint }} {{ . }}
{{- end -}} {{- end -}}
,{{ "\n" }} {{- "," -}}
{{- end -}} {{- end -}}
{{ "\n" }}
{{- end -}}
{{- if .Remount -}} {{- if eq $type "Remount" -}}
{{- range .Remount -}} {{- "remount " -}}
{{- "remount " | indent -}} {{- with .FsType -}}
{{- if .FsType -}} {{ "fstype=" }}{{ . }}{{ " " }}
fstype={{ .FsType }}{{ " " }}
{{- end -}} {{- end -}}
{{- if .Options -}} {{- with .Options -}}
{{- "options=(" -}} {{ "options=(" }}{{ join . }}{{ ") " }}
{{- range .Options -}}
{{ . }}{{ " " }}
{{- end -}}
{{ ") " }}
{{- end -}} {{- end -}}
{{- if .Remount -}} {{- with .Remount -}}
{{ .Remount }} {{ . }}
{{- end -}} {{- end -}}
,{{ "\n" }} {{- "," -}}
{{- end -}} {{- end -}}
{{ "\n" }}
{{- end -}}
{{- if .Unix -}} {{- if eq $type "Unix" -}}
{{- range .Unix -}} {{- "unix " -}}
{{- "unix " | indent -}} {{- with .Access -}}
{{- if .Access -}} {{ "(" }}{{ . }}{{ ") " }}
({{ .Access }}){{ " " }}
{{- end -}} {{- end -}}
{{- if .Type -}} {{- with .Type -}}
type={{ .Type }}{{ " " }} {{ "type=" }}{{ . }}{{ " " }}
{{- end -}} {{- end -}}
{{- if .Address -}} {{- with .Address -}}
addr={{ .Address }}{{ " " }} {{ "addr=" }}{{ . }}{{ " " }}
{{- end -}} {{- end -}}
{{- if .Peer -}} {{- if .Peer -}}
{{ "peer=(label=" }}{{ .Peer }} {{ "peer=(label=" }}{{ .Peer }}
{{- if .PeerAddr -}} {{- with .PeerAddr -}}
, addr={{ .PeerAddr }} {{ ", addr="}}{{ . }}
{{- end -}} {{- end -}}
{{- ")" -}} {{- ")" -}}
{{- end -}} {{- end -}}
,{{ "\n" }} {{- "," -}}
{{- end -}} {{- end -}}
{{ "\n" }}
{{- end -}}
{{- if .Ptrace -}} {{- if eq $type "Ptrace" -}}
{{- range .Ptrace -}} {{- "ptrace " -}}
{{- "ptrace " | indent -}} {{- with .Access -}}
{{- if .Access -}} {{ "(" }}{{ . }}{{ ") " }}
({{ .Access }}){{ " " }}
{{- end -}} {{- end -}}
{{- if .Peer -}} {{- with .Peer -}}
peer={{ .Peer }} {{ "peer=" }}{{ . }}
{{- end -}} {{- end -}}
,{{ "\n" }} {{- "," -}}
{{- end -}} {{- end -}}
{{ "\n" }}
{{- end -}}
{{- if .Signal -}} {{- if eq $type "Signal" -}}
{{- range .Signal -}} {{- "signal " -}}
{{- "signal " | indent -}} {{- with .Access -}}
{{- if .Access -}} {{ "(" }}{{ . }}{{ ") " }}
({{ .Access }}){{ " " }}
{{- end -}} {{- end -}}
{{- if .Set -}} {{- with .Set -}}
set=({{ .Set }}){{ " " }} {{ "set=(" }}{{ . }}{{ ") " }}
{{- end -}} {{- end -}}
{{- if .Peer -}} {{- with .Peer -}}
peer={{ .Peer }} {{ "peer=" }}{{ . }}
{{- end -}} {{- end -}}
,{{ "\n" }} {{- "," -}}
{{- end -}} {{- end -}}
{{ "\n" }}
{{- end -}}
{{- if .Dbus -}} {{- if eq $type "Dbus" -}}
{{- range .Dbus -}} {{- "dbus " -}}
{{- "dbus " | indent -}}
{{- if eq .Access "bind" -}} {{- if eq .Access "bind" -}}
bind bus={{ .Bus }} name={{ .Name }} bind bus={{ .Bus }} name={{ .Name }}
{{- else -}} {{- else -}}
{{ .Access }} bus={{ .Bus }} path={{ .Path }}{{ "\n" }} {{ .Access }} bus={{ .Bus }} path={{ .Path }}{{ "\n" }}
{{- if .Interface -}} {{- with .Interface -}}
{{ "interface=" | overindent }}{{ .Interface }}{{ "\n" }} {{ overindent "interface=" }}{{ . }}{{ "\n" }}
{{- end -}} {{- end -}}
{{- if .Member -}} {{- with .Member -}}
{{ "member=" | overindent }}{{ .Member }}{{ " " }}{{ "\n" }} {{ overindent "member=" }}{{ . }}{{ " " }}{{ "\n" }}
{{- end -}} {{- end -}}
{{- if and .Name .Label -}} {{- if and .Name .Label -}}
{{- "peer=" | overindent -}} {{ overindent "peer=(name=" }}{{ .Name }}{{ ", label="}}{{ .Label }}{{ ")" }}
(name={{ .Name }}, label={{ .Label }})
{{- else -}} {{- else -}}
{{- if .Name }} {{- with .Name -}}
{{- "peer=" | overindent -}} {{ overindent "peer=(name=" }}{{ . }}{{ ")" }}
(name={{ .Name }})
{{- end -}} {{- end -}}
{{- if .Label -}} {{- with .Label -}}
{{- "peer=" | overindent -}} {{ overindent "peer=(label=" }}{{ . }}{{ ")" }}
(label={{ .Label }})
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}
,{{ "\n\n" }} {{- "," -}}
{{- end -}} {{- end -}}
{{- end -}}
{{- if .File -}} {{- if eq $type "File" -}}
{{- range .File -}}
{{- indent "" -}}
{{- if .Owner -}} {{- if .Owner -}}
{{- "owner " -}} {{- "owner " -}}
{{- end -}} {{- end -}}
{{ .Path }} {{ .Access }} {{ .Path }}{{ " " }}{{ .Access }}
{{- if .Target -}} {{- with .Target -}}
{{ " ->" }} {{ .Target }} {{ " -> " }}{{ . }}
{{- end -}} {{- end -}}
, {{- "," -}}
{{- if .FileInherit -}} {{- if .FileInherit -}}
{{- " # file_inherit" -}} {{- " # file_inherit" -}}
{{- end -}} {{- end -}}
{{- if .NoNewPrivs -}} {{- if .NoNewPrivs -}}
{{- " # no new privs" -}} {{- " # no new privs" -}}
{{- end -}} {{- end -}}
{{ "\n" }}
{{- end -}} {{- end -}}
{{ "\n" }}
{{- end -}}
{{- if .Includes -}} {{- "\n" -}}
{{- range .Includes -}} {{- $oldtype = $type -}}
{{- if .IfExists -}}
{{ "include if exists " | indent }}
{{- if .IsMagic -}}
<{{ .Path }}>{{ "\n" }}
{{- else -}}
"{{ .Path }}"{{ "\n" }}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}} {{- end -}}
{{- "}\n" -}} {{- "}\n" -}}