mirror of
https://github.com/roddhjav/apparmor.d.git
synced 2025-01-30 23:05:11 +01:00
feat(aa): rewrite the toAccess function to parse, convert and verify the access values.
This commit is contained in:
parent
05de39d92a
commit
656aa15836
10 changed files with 134 additions and 53 deletions
|
@ -31,13 +31,11 @@ type Capability struct {
|
||||||
Names []string
|
Names []string
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func newCapabilityFromLog(log map[string]string) Rule {
|
func newCapabilityFromLog(log map[string]string) Rule {
|
||||||
return &Capability{
|
return &Capability{
|
||||||
RuleBase: newRuleFromLog(log),
|
RuleBase: newRuleFromLog(log),
|
||||||
Qualifier: newQualifierFromLog(log),
|
Qualifier: newQualifierFromLog(log),
|
||||||
Names: []string{log["capname"]},
|
Names: Must(toValues(tokCAPABILITY, "name", log["capname"])),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
package aa
|
package aa
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -26,6 +27,23 @@ func init() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cmpFileAccess compares two access strings for file rules.
|
||||||
|
// It is aimed to be used in slices.SortFunc.
|
||||||
|
func cmpFileAccess(i, j string) int {
|
||||||
|
if slices.Contains(requirements[tokFILE]["access"], i) &&
|
||||||
|
slices.Contains(requirements[tokFILE]["access"], j) {
|
||||||
|
return requirementsWeights[tokFILE]["access"][i] - requirementsWeights[tokFILE]["access"][j]
|
||||||
|
}
|
||||||
|
if slices.Contains(requirements[tokFILE]["transition"], i) &&
|
||||||
|
slices.Contains(requirements[tokFILE]["transition"], j) {
|
||||||
|
return requirementsWeights[tokFILE]["transition"][i] - requirementsWeights[tokFILE]["transition"][j]
|
||||||
|
}
|
||||||
|
if slices.Contains(requirements[tokFILE]["access"], i) {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
type File struct {
|
type File struct {
|
||||||
RuleBase
|
RuleBase
|
||||||
Qualifier
|
Qualifier
|
||||||
|
@ -36,12 +54,19 @@ type File struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func newFileFromLog(log map[string]string) Rule {
|
func newFileFromLog(log map[string]string) Rule {
|
||||||
|
accesses, err := toAccess("file-log", log["requested_mask"])
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("newFileFromLog(%v): %w", log, err))
|
||||||
|
}
|
||||||
|
if slices.Compare(accesses, []string{"l"}) == 0 {
|
||||||
|
return newLinkFromLog(log)
|
||||||
|
}
|
||||||
return &File{
|
return &File{
|
||||||
RuleBase: newRuleFromLog(log),
|
RuleBase: newRuleFromLog(log),
|
||||||
Qualifier: newQualifierFromLog(log),
|
Qualifier: newQualifierFromLog(log),
|
||||||
Owner: isOwner(log),
|
Owner: isOwner(log),
|
||||||
Path: log["name"],
|
Path: log["name"],
|
||||||
Access: toAccess("file-log", log["requested_mask"]),
|
Access: accesses,
|
||||||
Target: log["target"],
|
Target: log["target"],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -104,6 +129,7 @@ func newLinkFromLog(log map[string]string) Rule {
|
||||||
Target: log["target"],
|
Target: log["target"],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Link) Less(other any) bool {
|
func (r *Link) Less(other any) bool {
|
||||||
o, _ := other.(*Link)
|
o, _ := other.(*Link)
|
||||||
if r.Path != o.Path {
|
if r.Path != o.Path {
|
||||||
|
|
|
@ -25,7 +25,7 @@ func newIOUringFromLog(log map[string]string) Rule {
|
||||||
return &IOUring{
|
return &IOUring{
|
||||||
RuleBase: newRuleFromLog(log),
|
RuleBase: newRuleFromLog(log),
|
||||||
Qualifier: newQualifierFromLog(log),
|
Qualifier: newQualifierFromLog(log),
|
||||||
Access: toAccess(tokIOURING, log["requested"]),
|
Access: Must(toAccess(tokIOURING, log["requested"])),
|
||||||
Label: log["label"],
|
Label: log["label"],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,6 @@ package aa
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -37,7 +36,7 @@ func newMountConditionsFromLog(log map[string]string) MountConditions {
|
||||||
if _, present := log["flags"]; present {
|
if _, present := log["flags"]; present {
|
||||||
return MountConditions{
|
return MountConditions{
|
||||||
FsType: log["fstype"],
|
FsType: log["fstype"],
|
||||||
Options: strings.Split(log["flags"], ", "),
|
Options: Must(toValues(tokMOUNT, "flags", log["flags"])),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return MountConditions{FsType: log["fstype"]}
|
return MountConditions{FsType: log["fstype"]}
|
||||||
|
|
|
@ -40,7 +40,7 @@ func newMqueueFromLog(log map[string]string) Rule {
|
||||||
return &Mqueue{
|
return &Mqueue{
|
||||||
RuleBase: newRuleFromLog(log),
|
RuleBase: newRuleFromLog(log),
|
||||||
Qualifier: newQualifierFromLog(log),
|
Qualifier: newQualifierFromLog(log),
|
||||||
Access: toAccess(tokMQUEUE, log["requested"]),
|
Access: Must(toAccess(tokMQUEUE, log["requested"])),
|
||||||
Type: mqueueType,
|
Type: mqueueType,
|
||||||
Label: log["label"],
|
Label: log["label"],
|
||||||
Name: log["name"],
|
Name: log["name"],
|
||||||
|
|
|
@ -4,7 +4,9 @@
|
||||||
|
|
||||||
package aa
|
package aa
|
||||||
|
|
||||||
import "slices"
|
import (
|
||||||
|
"slices"
|
||||||
|
)
|
||||||
|
|
||||||
const tokPTRACE = "ptrace"
|
const tokPTRACE = "ptrace"
|
||||||
|
|
||||||
|
@ -27,7 +29,7 @@ func newPtraceFromLog(log map[string]string) Rule {
|
||||||
return &Ptrace{
|
return &Ptrace{
|
||||||
RuleBase: newRuleFromLog(log),
|
RuleBase: newRuleFromLog(log),
|
||||||
Qualifier: newQualifierFromLog(log),
|
Qualifier: newQualifierFromLog(log),
|
||||||
Access: toAccess(tokPTRACE, log["requested_mask"]),
|
Access: Must(toAccess(tokPTRACE, log["requested_mask"])),
|
||||||
Peer: log["peer"],
|
Peer: log["peer"],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,12 @@
|
||||||
|
|
||||||
package aa
|
package aa
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
tokALLOW = "allow"
|
tokALLOW = "allow"
|
||||||
tokAUDIT = "audit"
|
tokAUDIT = "audit"
|
||||||
|
@ -55,3 +61,85 @@ func (r Rules) GetVariables() []*Variable {
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Must is a helper that wraps a call to a function returning (any, error) and
|
||||||
|
// panics if the error is non-nil.
|
||||||
|
func Must[T any](v T, err error) T {
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function to convert a string to a slice of rule values according to
|
||||||
|
// the rule requirements as defined in the requirements map.
|
||||||
|
func toValues(rule string, key string, input string) ([]string, error) {
|
||||||
|
var sep string
|
||||||
|
req, ok := requirements[rule][key]
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("unrecognized requirement '%s' for rule %s", key, rule)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case strings.Contains(input, ","):
|
||||||
|
sep = ","
|
||||||
|
case strings.Contains(input, " "):
|
||||||
|
sep = " "
|
||||||
|
}
|
||||||
|
res := strings.Split(input, sep)
|
||||||
|
for _, access := range res {
|
||||||
|
if !slices.Contains(req, access) {
|
||||||
|
return nil, fmt.Errorf("unrecognized %s: %s", key, access)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
slices.SortFunc(res, func(i, j string) int {
|
||||||
|
return requirementsWeights[rule][key][i] - requirementsWeights[rule][key][j]
|
||||||
|
})
|
||||||
|
return slices.Compact(res), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function to convert an access string to a slice of access according to
|
||||||
|
// the rule requirements as defined in the requirements map.
|
||||||
|
func toAccess(rule string, input string) ([]string, error) {
|
||||||
|
var res []string
|
||||||
|
|
||||||
|
switch rule {
|
||||||
|
case tokFILE:
|
||||||
|
raw := strings.Split(input, "")
|
||||||
|
trans := []string{}
|
||||||
|
for _, access := range raw {
|
||||||
|
if slices.Contains(requirements[tokFILE]["access"], access) {
|
||||||
|
res = append(res, access)
|
||||||
|
} else {
|
||||||
|
trans = append(trans, access)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
transition := strings.Join(trans, "")
|
||||||
|
if len(transition) > 0 {
|
||||||
|
if slices.Contains(requirements[tokFILE]["transition"], transition) {
|
||||||
|
res = append(res, transition)
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("unrecognized transition: %s", transition)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case tokFILE + "-log":
|
||||||
|
raw := strings.Split(input, "")
|
||||||
|
for _, access := range raw {
|
||||||
|
if slices.Contains(requirements[tokFILE]["access"], access) {
|
||||||
|
res = append(res, access)
|
||||||
|
} else if maskToAccess[access] != "" {
|
||||||
|
res = append(res, maskToAccess[access])
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("toAccess: unrecognized file access '%s'", input)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
return toValues(rule, "access", input)
|
||||||
|
}
|
||||||
|
|
||||||
|
slices.SortFunc(res, cmpFileAccess)
|
||||||
|
return slices.Compact(res), nil
|
||||||
|
}
|
||||||
|
|
|
@ -4,7 +4,9 @@
|
||||||
|
|
||||||
package aa
|
package aa
|
||||||
|
|
||||||
import "slices"
|
import (
|
||||||
|
"slices"
|
||||||
|
)
|
||||||
|
|
||||||
const tokSIGNAL = "signal"
|
const tokSIGNAL = "signal"
|
||||||
|
|
||||||
|
@ -41,8 +43,8 @@ func newSignalFromLog(log map[string]string) Rule {
|
||||||
return &Signal{
|
return &Signal{
|
||||||
RuleBase: newRuleFromLog(log),
|
RuleBase: newRuleFromLog(log),
|
||||||
Qualifier: newQualifierFromLog(log),
|
Qualifier: newQualifierFromLog(log),
|
||||||
Access: toAccess(tokSIGNAL, log["requested_mask"]),
|
Access: Must(toAccess(tokSIGNAL, log["requested_mask"])),
|
||||||
Set: toAccess(tokSIGNAL, log["signal"]),
|
Set: []string{log["signal"]},
|
||||||
Peer: log["peer"],
|
Peer: log["peer"],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,6 +48,8 @@ var (
|
||||||
"a": "w",
|
"a": "w",
|
||||||
"c": "w",
|
"c": "w",
|
||||||
"d": "w",
|
"d": "w",
|
||||||
|
"wc": "w",
|
||||||
|
"x": "ix",
|
||||||
}
|
}
|
||||||
|
|
||||||
// The order the apparmor rules should be sorted
|
// The order the apparmor rules should be sorted
|
||||||
|
@ -230,39 +232,3 @@ func getLetterIn(alphabet []string, in string) string {
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper function to convert a access string to slice of access
|
|
||||||
func toAccess(constraint string, input string) []string {
|
|
||||||
var res []string
|
|
||||||
|
|
||||||
switch constraint {
|
|
||||||
case "file", "file-log":
|
|
||||||
raw := strings.Split(input, "")
|
|
||||||
trans := []string{}
|
|
||||||
for _, access := range raw {
|
|
||||||
if slices.Contains(fileAccess, access) {
|
|
||||||
res = append(res, access)
|
|
||||||
} else if maskToAccess[access] != "" {
|
|
||||||
res = append(res, maskToAccess[access])
|
|
||||||
trans = append(trans, access)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if constraint != "file-log" {
|
|
||||||
transition := strings.Join(trans, "")
|
|
||||||
if len(transition) > 0 {
|
|
||||||
if slices.Contains(fileExecTransition, transition) {
|
|
||||||
res = append(res, transition)
|
|
||||||
} else {
|
|
||||||
panic("unrecognized pattern: " + transition)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return res
|
|
||||||
|
|
||||||
default:
|
|
||||||
res = strings.Fields(input)
|
|
||||||
slices.Sort(res)
|
|
||||||
return slices.Compact(res)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -36,7 +36,7 @@ func newUnixFromLog(log map[string]string) Rule {
|
||||||
return &Unix{
|
return &Unix{
|
||||||
RuleBase: newRuleFromLog(log),
|
RuleBase: newRuleFromLog(log),
|
||||||
Qualifier: newQualifierFromLog(log),
|
Qualifier: newQualifierFromLog(log),
|
||||||
Access: toAccess(tokUNIX, log["requested_mask"]),
|
Access: Must(toAccess(tokUNIX, log["requested_mask"])),
|
||||||
Type: log["sock_type"],
|
Type: log["sock_type"],
|
||||||
Protocol: log["protocol"],
|
Protocol: log["protocol"],
|
||||||
Address: log["addr"],
|
Address: log["addr"],
|
||||||
|
|
Loading…
Reference in a new issue