-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
437 lines (386 loc) · 10.4 KB
/
task.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package main
// #include "../defs.h"
import "C"
import (
"net"
"time"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"github.com/zhyocean/neuvector/agent/dp"
"github.com/zhyocean/neuvector/agent/probe"
"github.com/zhyocean/neuvector/share"
"github.com/zhyocean/neuvector/share/cluster"
"github.com/zhyocean/neuvector/share/fsmon"
"github.com/zhyocean/neuvector/share/utils"
)
type eventPolicyHandlerFunc func(arg interface{})
func isContainerBlocking(c *containerData) bool {
if !c.capBlock {
return false
}
if c.policyMode == "" {
c.policyMode = gInfo.policyMode
}
return c.policyMode == share.PolicyModeEnforce
}
func isContainerInline(c *containerData) bool {
if !c.capIntcp {
return false
}
if c.policyMode == "" {
c.policyMode = gInfo.policyMode
}
if c.policyMode == share.PolicyModeEnforce {
return true
} else if cfg, ok := gInfo.containerConfig[c.id]; ok {
return cfg.Wire == share.WireInline
} else {
return false
}
}
func isContainerQuarantine(c *containerData) bool {
if !c.capIntcp {
return false
}
if cfg, ok := gInfo.containerConfig[c.id]; ok {
return cfg.Quarantine
} else {
return false
}
}
// Here we compare the difference between the new config from cluster and local cache.
// In the case that the agent process restarts, we need to make sure all local states should
// be consistent with the local cache.
func taskConfigContainer(id string, newconf *share.CLUSWorkloadConfig) {
log.WithFields(log.Fields{"container": id, "config": *newconf}).Debug("")
gInfo.containerConfig[id] = newconf
if c, ok := gInfo.activeContainers[id]; ok && c.capIntcp {
inline := isContainerInline(c)
quar := isContainerQuarantine(c)
if inline != c.inline || quar != c.quar {
changeContainerWire(c, inline, quar, &newconf.QuarReason)
}
}
}
func taskAppUpdateByMAC(mac net.HardwareAddr, apps map[share.CLUSProtoPort]*share.CLUSApp) {
if c := getContainerByMAC(mac); c != nil {
mergeContainerApps(c, apps)
}
}
func mergeContainerApps(c *containerData, apps map[share.CLUSProtoPort]*share.CLUSApp) {
log.WithFields(log.Fields{"container": c.id}).Debug("")
gInfoLock()
for p, app := range apps {
if m, ok := c.appMap[p]; ok {
if app.Proto > 0 {
m.Proto = app.Proto
}
if app.Server > 0 {
m.Server = app.Server
}
if app.Application > 0 {
m.Application = app.Application
}
} else {
c.appMap[p] = app
}
}
gInfoUnlock()
ev := ClusterEvent{
event: EV_UPDATE_CONTAINER,
id: c.id,
apps: translateAppMap(c.appMap),
}
ClusterEventChan <- &ev
}
func mergeMappedPorts(c *containerData, ports map[share.CLUSProtoPort]*share.CLUSMappedPort) {
log.WithFields(log.Fields{"merge-to": c.id}).Debug("")
gInfoLock()
for p, port := range ports {
c.portMap[p] = port
}
gInfoUnlock()
ev := ClusterEvent{
event: EV_UPDATE_CONTAINER,
id: c.id,
ports: translateMappedPort(c.portMap),
}
ClusterEventChan <- &ev
}
func configKvCongestCtl(enable bool) {
if agentEnv.kvCongestCtrl == enable {
log.Debug("Skipped")
return
}
agentEnv.kvCongestCtrl = enable
log.WithFields(log.Fields{"enable": agentEnv.kvCongestCtrl}).Info()
go cluster.SetWatcherCongestionCtl(share.CLUSWorkloadProfileStore, agentEnv.kvCongestCtrl)
}
func taskConfigAgent(conf *share.CLUSAgentConfig) {
log.WithFields(log.Fields{"config": conf}).Debug("")
// debug
var hasCPath, hasConn, hasCluster, hasMonitorTrace bool
if conf.Debug == nil {
conf.Debug = make([]string, 0)
}
newDebug := utils.NewSet()
for _, d := range conf.Debug {
switch d {
case "cpath":
hasCPath = true
newDebug.Add("ctrl")
case "conn":
hasConn = true
case "cluster":
hasCluster = true
case "monitor":
hasMonitorTrace = true
default:
newDebug.Add(d)
}
}
if hasCPath {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
if hasConn {
connLog.Level = log.DebugLevel
} else {
connLog.Level = log.InfoLevel
}
prober.SetMonitorTrace(hasMonitorTrace)
fileWatcher.SetMonitorTrace(hasMonitorTrace)
if !agentEnv.runWithController {
if hasCluster {
cluster.SetLogLevel(log.DebugLevel)
} else {
cluster.SetLogLevel(log.InfoLevel)
}
}
oldDebug := utils.NewSet()
for _, d := range gInfo.agentConfig.Debug {
oldDebug.Add(d)
}
if !oldDebug.Equal(newDebug) {
// rebuild debug config because we might add 'ctrl'
i := 0
cats := make([]string, newDebug.Cardinality())
for d := range newDebug.Iter() {
cats[i] = d.(string)
i++
}
gInfo.agentConfig.Debug = cats
debug := &dp.DPDebug{Categories: cats}
dp.DPCtrlConfigAgent(debug)
}
//////
prober.SetNvProtect(conf.DisableNvProtectMode) // default: false (enabled)
configKvCongestCtl(!conf.DisableKvCongestCtl)
}
func escalToIncidentLog(e *probe.ProbeEscalation, count int, start time.Time) *share.CLUSIncidentLog {
eLog := share.CLUSIncidentLog{
HostID: Host.ID,
HostName: Host.Name,
AgentID: Agent.ID,
AgentName: Agent.Name,
WorkloadID: e.ID,
ReportedAt: time.Now().UTC(),
ProcName: e.Name,
ProcPath: e.Path,
ProcCmds: e.Cmds,
ProcRealUID: e.RUid,
ProcEffUID: e.EUid,
ProcRealUser: e.RealUser,
ProcEffUser: e.EffUser,
// parent info
ProcPName: e.ParentName,
ProcPPath: e.ParentPath,
Action: share.PolicyActionViolate,
RuleID: share.CLUSReservedUuidRootEscalation,
Msg: e.Msg,
Count: count,
StartAt: start,
}
gInfoRLock()
defer gInfoRUnlock()
if e.ID == "" {
//host privilege escalation
eLog.ID = share.CLUSIncidHostPrivilEscalate
} else if c, ok := gInfo.activeContainers[e.ID]; ok {
eLog.WorkloadName = c.name
eLog.ID = share.CLUSIncidContainerPrivilEscalate
} else {
eLog.WorkloadName = ""
eLog.ID = share.CLUSIncidContainerPrivilEscalate
}
return &eLog
}
func fileModifiedToIncidentLog(e *fsmon.MonitorMessage) *share.CLUSIncidentLog {
eLog := share.CLUSIncidentLog{
HostID: Host.ID,
HostName: Host.Name,
AgentID: Agent.ID,
AgentName: Agent.Name,
WorkloadID: e.ID,
ReportedAt: time.Now().UTC(),
FilePath: e.Path,
Msg: e.Msg,
ProcName: e.ProcName,
ProcPath: e.ProcPath,
ProcCmds: e.ProcCmds,
ProcEffUID: e.ProcEUid,
ProcEffUser: e.ProcEUser,
Count: e.Count,
StartAt: e.StartAt,
Group: e.Group,
Action: e.Action,
}
gInfoLock()
defer gInfoUnlock()
if e.ID == "" {
if e.Package {
eLog.ID = share.CLUSIncidHostPackageUpdated
// invalidate scan cache
gInfo.hostScanCache = nil
} else if e.Action == share.PolicyActionDeny {
eLog.ID = share.CLUSIncidHostFileAccessViolation
} else {
eLog.ID = share.CLUSIncidHostFileAccessViolation
}
} else if c, ok := gInfo.activeContainers[e.ID]; ok {
eLog.WorkloadName = c.name
if e.Package {
eLog.ID = share.CLUSIncidContainerPackageUpdated
// invalidate scan cache
c.scanCache = nil
} else {
eLog.ID = share.CLUSIncidContainerFileAccessViolation
}
} else {
eLog.WorkloadName = ""
if e.Package {
eLog.ID = share.CLUSIncidContainerPackageUpdated
} else {
eLog.ID = share.CLUSIncidContainerFileAccessViolation
}
}
return &eLog
}
func processToIncidentLog(s *probe.ProbeProcess, count int, start time.Time) *share.CLUSIncidentLog {
eLog := &share.CLUSIncidentLog{
HostID: Host.ID,
HostName: Host.Name,
AgentID: Agent.ID,
AgentName: Agent.Name,
WorkloadID: s.ID,
ReportedAt: time.Now().UTC(),
ProcName: s.Name,
ProcPath: s.Path,
ProcCmds: s.Cmds,
ProcEffUID: s.EUid,
ProcEffUser: s.EUser,
ConnIngress: s.ConnIngress,
ProcPName: s.PName,
ProcPPath: s.PPath,
RuleID: s.RuleID,
Group: s.Group,
Msg: s.Msg,
Count: count,
StartAt: start,
}
if s.Connection != nil {
eLog.EtherType = s.Connection.Ether
eLog.IPProto = s.Connection.Protocol
eLog.LocalIP = s.Connection.LocIP
eLog.RemoteIP = s.Connection.RemIP
eLog.LocalPort = s.Connection.LocPort
eLog.RemotePort = s.Connection.RemPort
eLog.LocalPeer = isLocalHostIP(eLog.RemoteIP)
}
gInfoRLock()
defer gInfoRUnlock()
if s.ID != "" {
if c, ok := gInfo.activeContainers[s.ID]; ok {
eLog.WorkloadName = c.name
}
}
return eLog
}
func tunnelToIncidentLog(s *probe.ProbeProcess, count int, start time.Time) *share.CLUSIncidentLog {
eLog := processToIncidentLog(s, count, start)
if s.ID == "" {
eLog.ID = share.CLUSIncidHostTunnel
} else {
eLog.ID = share.CLUSIncidContainerTunnel
}
eLog.Action = share.PolicyActionViolate
return eLog
}
func suspicToIncidentLog(s *probe.ProbeProcess, count int, start time.Time) *share.CLUSIncidentLog {
eLog := processToIncidentLog(s, count, start)
if s.ID == "" {
eLog.ID = share.CLUSIncidHostSuspiciousProcess
} else {
eLog.ID = share.CLUSIncidContainerSuspiciousProcess
}
eLog.Action = share.PolicyActionViolate
return eLog
}
func procViolationToIncidentLog(s *probe.ProbeProcess, count int, start time.Time) *share.CLUSIncidentLog {
eLog := processToIncidentLog(s, count, start)
if s.ID == "" {
eLog.ID = share.CLUSIncidHostProcessViolation
} else {
eLog.ID = share.CLUSIncidContainerProcessViolation
}
eLog.Action = share.PolicyActionViolate
return eLog
}
func procDeniedToIncidentLog(s *probe.ProbeProcess, count int, start time.Time) *share.CLUSIncidentLog {
eLog := processToIncidentLog(s, count, start)
if s.ID == "" {
eLog.ID = share.CLUSIncidHostProcessViolation
} else {
eLog.ID = share.CLUSIncidContainerProcessViolation
}
eLog.Action = share.PolicyActionDeny
return eLog
}
func reportIncident(eLog *share.CLUSIncidentLog) {
log.WithFields(log.Fields{"eLog": *eLog}).Debug("")
eLog.LogUID = uuid.New().String()
incidentMutex.Lock()
incidentLogCache = append(incidentLogCache, eLog)
incidentMutex.Unlock()
}
func logContainerAudit(name, id string, items []share.CLUSAuditBenchItem, lid share.TLogAudit) {
alog := &share.CLUSAuditLog{
ID: lid,
HostID: Host.ID,
HostName: Host.Name,
AgentID: Agent.ID,
AgentName: Agent.Name,
WorkloadID: id,
WorkloadName: name,
ReportedAt: time.Now().UTC(),
Items: items,
}
auditMutex.Lock()
auditLogCache = append(auditLogCache, alog)
auditMutex.Unlock()
}
func logHostAudit(items []share.CLUSAuditBenchItem, auditId share.TLogAudit) {
alog := &share.CLUSAuditLog{
ID: auditId,
HostID: Host.ID,
HostName: Host.Name,
AgentID: Agent.ID,
AgentName: Agent.Name,
ReportedAt: time.Now().UTC(),
Items: items,
}
auditMutex.Lock()
auditLogCache = append(auditLogCache, alog)
auditMutex.Unlock()
}