/
paths_reducer.go
172 lines (154 loc) · 4.81 KB
/
paths_reducer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build linux
// Package activitytree holds activitytree related files
package activitytree
import (
"regexp"
"strconv"
"strings"
"github.com/DataDog/datadog-agent/pkg/security/common/containerutils"
"github.com/DataDog/datadog-agent/pkg/security/secl/model"
)
// PathsReducer is used to reduce the paths in an activity tree according to predefined heuristics
type PathsReducer struct {
patterns []PatternReducer
}
// PatternReducer is used to reduce the paths in an activity tree according to a given pattern
type PatternReducer struct {
Pattern *regexp.Regexp
Hint string
PreCheck func(fileEvent *model.FileEvent) bool
Callback func(ctx *callbackContext)
}
// callbackContext is the input struct for the callback function
type callbackContext struct {
groups []int
path string
fileEvent *model.FileEvent
processNode *ProcessNode
}
func (cc *callbackContext) getGroup(index int) (int, int) {
return cc.groups[index*2], cc.groups[index*2+1]
}
func (cc *callbackContext) replaceBy(start, end int, replaceBy string) {
left := cc.path[:start]
right := cc.path[end:]
var b strings.Builder
b.Grow(len(left) + len(replaceBy) + len(right))
b.WriteString(left)
b.WriteString(replaceBy)
b.WriteString(right)
cc.path = b.String()
}
// NewPathsReducer returns a new PathsReducer
func NewPathsReducer() *PathsReducer {
return &PathsReducer{
patterns: getPathsReducerPatterns(),
}
}
// ReducePath reduces a path according to the predefined heuristics
func (r *PathsReducer) ReducePath(path string, fileEvent *model.FileEvent, node *ProcessNode) string {
ctx := &callbackContext{
path: path,
fileEvent: fileEvent,
processNode: node,
}
for _, pattern := range r.patterns {
if pattern.PreCheck != nil && fileEvent != nil && !pattern.PreCheck(fileEvent) {
continue
}
if pattern.Hint != "" && !strings.Contains(ctx.path, pattern.Hint) {
continue
}
allMatches := pattern.Pattern.FindAllStringSubmatchIndex(ctx.path, -1)
for matchSet := len(allMatches) - 1; matchSet >= 0; matchSet-- {
if pattern.Callback != nil {
ctx.groups = allMatches[matchSet]
pattern.Callback(ctx)
}
}
}
return ctx.path
}
// getPathsReducerPatterns returns the patterns used to reduce the paths in an activity tree
func getPathsReducerPatterns() []PatternReducer {
return []PatternReducer{
{
Pattern: regexp.MustCompile(`/proc/(\d+)/`), // process PID
Hint: "proc",
Callback: func(ctx *callbackContext) {
start, end := ctx.getGroup(1)
// compute pid from path
pid, err := strconv.ParseUint(ctx.path[start:end], 10, 32)
if err != nil {
return
}
// replace the pid in the path between start and end with a * only if the replaced pid is not the pid of the process node
if ctx.processNode.Process.Pid == uint32(pid) {
ctx.replaceBy(start, end, "self")
} else {
ctx.replaceBy(start, end, "*")
}
},
},
{
Pattern: regexp.MustCompile(`/task/(\d+)/`), // process TID
Hint: "task",
Callback: func(ctx *callbackContext) {
start, end := ctx.getGroup(1)
ctx.replaceBy(start, end, "*")
},
},
{
Pattern: regexp.MustCompile(`kubepods-([^/]*)\.(?:slice|scope)`), // kubernetes cgroup
Hint: "kubepods",
PreCheck: func(fileEvent *model.FileEvent) bool {
return fileEvent.Filesystem == "sysfs"
},
Callback: func(ctx *callbackContext) {
start, end := ctx.getGroup(1)
ctx.replaceBy(start, end, "*")
},
},
{
Pattern: regexp.MustCompile(`cri-containerd-([^/]*)\.(?:slice|scope)`), // kubernetes cgroup
Hint: "cri-containerd",
PreCheck: func(fileEvent *model.FileEvent) bool {
return fileEvent.Filesystem == "sysfs"
},
Callback: func(ctx *callbackContext) {
start, end := ctx.getGroup(1)
ctx.replaceBy(start, end, "*")
},
},
{
Pattern: regexp.MustCompile(containerutils.ContainerIDPatternStr), // container ID
Callback: func(ctx *callbackContext) {
start, end := ctx.getGroup(0)
ctx.replaceBy(start, end, "*")
},
},
{
Pattern: regexp.MustCompile(`/sys/devices/virtual/block/(?:dm-|loop)([0-9]+)`), // block devices
Hint: "devices",
PreCheck: func(fileEvent *model.FileEvent) bool {
return fileEvent.Filesystem == "sysfs"
},
Callback: func(ctx *callbackContext) {
start, end := ctx.getGroup(1)
ctx.replaceBy(start, end, "*")
},
},
{
Pattern: regexp.MustCompile(`secrets/kubernetes\.io/serviceaccount/([0-9._]+)`), // service account token date
Hint: "serviceaccount",
Callback: func(ctx *callbackContext) {
start, end := ctx.getGroup(1)
ctx.replaceBy(start, end, "*")
},
},
}
}