-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
fetcher.go
233 lines (197 loc) · 5.88 KB
/
fetcher.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// 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 constantfetch holds constantfetch related files
package constantfetch
import (
"crypto/md5"
"fmt"
"hash"
"io"
manager "github.com/DataDog/ebpf-manager"
"github.com/DataDog/datadog-agent/pkg/security/seclog"
)
// ErrorSentinel is the value of an unavailable offset or size
const ErrorSentinel uint64 = ^uint64(0)
// ConstantFetcher represents a source of constants that can be used to fill up
// eBPF relocations
type ConstantFetcher interface {
fmt.Stringer
AppendSizeofRequest(id, typeName, headerName string)
AppendOffsetofRequest(id, typeName, fieldName, headerName string)
FinishAndGetResults() (map[string]uint64, error)
}
// ComposeConstantFetcher represents a composition of child constant fetchers
// It allows the usage of fallbacks if the main source is not working
type ComposeConstantFetcher struct {
hasher hash.Hash
fetchers []ConstantFetcher
requests []*composeRequest
}
// ComposeConstantFetchers creates a new ComposeConstantFetcher based on the
// passed fetchers
func ComposeConstantFetchers(fetchers []ConstantFetcher) *ComposeConstantFetcher {
return &ComposeConstantFetcher{
hasher: md5.New(),
fetchers: fetchers,
}
}
func (f *ComposeConstantFetcher) String() string {
return fmt.Sprintf("composition of %s", f.fetchers)
}
func (f *ComposeConstantFetcher) appendRequest(req *composeRequest) {
f.requests = append(f.requests, req)
_, _ = io.WriteString(f.hasher, req.id)
_, _ = io.WriteString(f.hasher, req.typeName)
_, _ = io.WriteString(f.hasher, req.fieldName)
_, _ = io.WriteString(f.hasher, req.headerName)
}
// AppendSizeofRequest appends a sizeof request
func (f *ComposeConstantFetcher) AppendSizeofRequest(id, typeName, headerName string) {
f.appendRequest(&composeRequest{
id: id,
sizeof: true,
typeName: typeName,
fieldName: "",
headerName: headerName,
value: ErrorSentinel,
})
}
// AppendOffsetofRequest appends an offset request
func (f *ComposeConstantFetcher) AppendOffsetofRequest(id, typeName, fieldName, headerName string) {
f.appendRequest(&composeRequest{
id: id,
sizeof: false,
typeName: typeName,
fieldName: fieldName,
headerName: headerName,
value: ErrorSentinel,
})
}
func (f *ComposeConstantFetcher) getHash() []byte {
return f.hasher.Sum(nil)
}
func (f *ComposeConstantFetcher) fillConstantCacheIfNeeded() {
currentHash := f.getHash()
if constantsCache.isMatching(currentHash) {
return
}
for _, fetcher := range f.fetchers {
for _, req := range f.requests {
if req.value == ErrorSentinel {
if req.sizeof {
fetcher.AppendSizeofRequest(req.id, req.typeName, req.headerName)
} else {
fetcher.AppendOffsetofRequest(req.id, req.typeName, req.fieldName, req.headerName)
}
}
}
res, err := fetcher.FinishAndGetResults()
if err != nil {
seclog.Errorf("failed to run constant fetcher: %v", err)
}
for _, req := range f.requests {
if req.value == ErrorSentinel {
if newValue, present := res[req.id]; present {
req.value = newValue
req.fetcherName = fetcher.String()
}
}
}
}
finalRes := make(map[string]ValueAndSource)
for _, req := range f.requests {
finalRes[req.id] = ValueAndSource{
ID: req.id,
Value: req.value,
FetcherName: req.fetcherName,
}
}
constantsCache = &cachedConstants{
constants: finalRes,
hash: currentHash,
}
}
// FinishAndGetResults does the actual fetching and returns the results
func (f *ComposeConstantFetcher) FinishAndGetResults() (map[string]uint64, error) {
f.fillConstantCacheIfNeeded()
return constantsCache.getConstants(), nil
}
// FinishAndGetStatus does the actual fetching and returns the status
func (f *ComposeConstantFetcher) FinishAndGetStatus() (*ConstantFetcherStatus, error) {
f.fillConstantCacheIfNeeded()
fetcherNames := make([]string, 0, len(f.fetchers))
for _, fetcher := range f.fetchers {
fetcherNames = append(fetcherNames, fetcher.String())
}
return &ConstantFetcherStatus{
Fetchers: fetcherNames,
Values: constantsCache.constants,
}, nil
}
// ConstantFetcherStatus represents the status of the constant fetcher sub-system
type ConstantFetcherStatus struct {
Fetchers []string
Values map[string]ValueAndSource
}
type composeRequest struct {
id string
sizeof bool
typeName, fieldName string
headerName string
value uint64
fetcherName string
}
// CreateConstantEditors creates constant editors based on the constants fetched
func CreateConstantEditors(constants map[string]uint64) []manager.ConstantEditor {
res := make([]manager.ConstantEditor, 0, len(constants))
for name, value := range constants {
if value == ErrorSentinel {
seclog.Errorf("failed to fetch constant for %s", name)
value = 0
}
res = append(res, manager.ConstantEditor{
Name: name,
Value: value,
})
}
return res
}
var constantsCache *cachedConstants
// ClearConstantsCache clears the constants cache
func ClearConstantsCache() {
constantsCache = nil
}
// ValueAndSource represents the required information about a constant, its id, its value and its source
type ValueAndSource struct {
ID string
Value uint64
FetcherName string
}
type cachedConstants struct {
constants map[string]ValueAndSource
hash []byte
}
func (cc *cachedConstants) isMatching(hash []byte) bool {
if cc == nil {
return false
}
if len(hash) != len(cc.hash) {
return false
}
for i, v := range cc.hash {
if v != hash[i] {
return false
}
}
return true
}
func (cc *cachedConstants) getConstants() map[string]uint64 {
res := make(map[string]uint64)
for k, v := range cc.constants {
res[k] = v.Value
}
return res
}