Skip to content

Commit

Permalink
[agent-smith] Parse net dev and create an infringement
Browse files Browse the repository at this point in the history
  • Loading branch information
fntlnz committed Jul 1, 2021
1 parent fb31733 commit 0168e35
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 428 deletions.
21 changes: 19 additions & 2 deletions chart/templates/agent-smith-configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,26 @@ data:
"blacklists": {
"very": {
"signatures": [
{"name":"testtarget","domain":"process","kind":"elf","pattern":"YWdlbnRTbWl0aFRlc3RUYXJnZXQ=","regexp":false}
{
"name": "testtarget",
"domain": "process",
"kind": "elf",
"pattern": "YWdlbnRTbWl0aFRlc3RUYXJnZXQ=",
"regexp": false
}
]
}
},
"egressTraffic": {
"dt": "2m",
"excessive": {
"baseBudget": "300Mi",
"perDtThreshold": "100Mi"
},
"veryExcessive": {
"baseBudget": "2Gi",
"perDtThreshold": "250Mi"
}
}
}
{{- end -}}
{{- end -}}
2 changes: 1 addition & 1 deletion components/ee/agent-smith/BUILD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ scripts:
- components/ee/agent-smith/cmd/testbed:app
- components/ee/agent-smith/cmd/testtarget:app
script: |
scp vm ./components-ee-agent-smith--falco-bpf-probe/probe.o ./components-ee-agent-smith--app/agent-smith ./components-ee-agent-smith--example-config/example-config.json ./components-ee-agent-smith-cmd-testbed--app/testbed ./components-ee-agent-smith-cmd-testtarget--app/testtarget root@localhost:/
scp -P 2222 -i ~/.ssh/id_rsa_vm -o StrictHostKeyChecking=no vm ./components-ee-agent-smith--falco-bpf-probe/probe.o ./components-ee-agent-smith--app/agent-smith ./components-ee-agent-smith--example-config/example-config.json ./components-ee-agent-smith-cmd-testbed--app/testbed ./components-ee-agent-smith-cmd-testtarget--app/testtarget root@localhost:/
echo "copied agent-smith to /"
6 changes: 6 additions & 0 deletions components/ee/agent-smith/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ ssh vm

If you now go under the `/workspace` folder in the VM, you will find all your workspace stuff.

If you want to compile with leeway and have the compiled artifacts in the VM you can do

```
leeway run components/ee/agent-smith:copy-to-qemu
```

## Falco libs BPF probe development

In case you need to do development of new features or fix bugs against the
Expand Down
22 changes: 19 additions & 3 deletions components/ee/agent-smith/example-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,26 @@
"probePath": "./probe.o",
"blacklists": {
"very": {
"binaries": ["find"],
"signatures": [
{"name":"testtarget","domain":"process","kind":"elf","pattern":"YWdlbnRTbWl0aFRlc3RUYXJnZXQ=","regexp":false}
{
"name": "testtarget",
"domain": "process",
"kind": "elf",
"pattern": "YWdlbnRTbWl0aFRlc3RUYXJnZXQ=",
"regexp": false
}
]
}
},
"egressTraffic": {
"dt": "2m",
"excessive": {
"baseBudget": "300Mi",
"perDtThreshold": "100Mi"
},
"veryExcessive": {
"baseBudget": "2Gi",
"perDtThreshold": "250Mi"
}
}
}
}
31 changes: 19 additions & 12 deletions components/ee/agent-smith/pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ type Smith struct {
Config Config
GitpodAPI gitpod.APIInterface
EnforcementRules map[string]EnforcementRules
EgressTraffic *EgressTraffic
metrics *metrics

notifiedInfringements *lru.Cache
Expand Down Expand Up @@ -332,20 +331,20 @@ func (agent *Smith) Start(ctx context.Context, callback func(InfringingWorkspace
agent.pidsMap.Range(func(key, value interface{}) bool {
p := key.(int)
t := value.(time.Time)
infr, err := agent.checkEgressTrafficCallback(strconv.Itoa(p), t)
infr, err := agent.checkEgressTrafficCallback(p, t)
if err != nil {
log.WithError(err).Warnf("error checking egress for pid: %d", p)
return true
}
if infr == nil {
return true
}
var res []Infringement
v, err := getWorkspaceFromProcess(p)
if err != nil {
// this is not from a workspace, let's skip
return true
}
v.Infringements = append(v.Infringements, *infr)
res = append(res, *infr)
v.Infringements = res
ps, err := agent.Penalize(*v)
if err != nil {
log.WithError(err).WithField("infringement", v).Warn("error while reacting to infringement")
Expand Down Expand Up @@ -432,6 +431,12 @@ func (agent *Smith) cleanupDeadPidsCallback() {
return true
}

_, err = getWorkspaceFromProcess(p)
if err != nil {
agent.pidsMap.Delete(p)
return true
}

return true
})

Expand Down Expand Up @@ -796,15 +801,17 @@ func (agent *Smith) RegisterMetrics(reg prometheus.Registerer) error {
return agent.metrics.Register(reg)
}

func (agent *Smith) checkEgressTrafficCallback(pid string, pidCreationTime time.Time) (*Infringement, error) {
if agent.EgressTraffic == nil {
func (agent *Smith) checkEgressTrafficCallback(pid int, pidCreationTime time.Time) (*Infringement, error) {
if agent.Config.EgressTraffic == nil {
return nil, nil
}

podLifetime := time.Since(pidCreationTime)
resp, err := network.GetEgressTraffic(pid)
if err != nil {
return nil, err
}

if resp <= 0 {
log.WithField("total egress bytes", resp).Warn("GetEgressTraffic returned <= 0 value")
return nil, nil
Expand All @@ -815,14 +822,14 @@ func (agent *Smith) checkEgressTrafficCallback(pid string, pidCreationTime time.
T *PerLevelEgressTraffic
}
levels := make([]level, 0, 2)
if agent.EgressTraffic.VeryExcessiveLevel != nil {
levels = append(levels, level{V: GradeKind(InfringementExcessiveEgress, InfringementSeverityVery), T: agent.EgressTraffic.VeryExcessiveLevel})
if agent.Config.EgressTraffic.VeryExcessiveLevel != nil {
levels = append(levels, level{V: GradeKind(InfringementExcessiveEgress, InfringementSeverityVery), T: agent.Config.EgressTraffic.VeryExcessiveLevel})
}
if agent.EgressTraffic.ExcessiveLevel != nil {
levels = append(levels, level{V: GradeKind(InfringementExcessiveEgress, InfringementSeverityAudit), T: agent.EgressTraffic.ExcessiveLevel})
if agent.Config.EgressTraffic.ExcessiveLevel != nil {
levels = append(levels, level{V: GradeKind(InfringementExcessiveEgress, InfringementSeverityAudit), T: agent.Config.EgressTraffic.ExcessiveLevel})
}

dt := int64(podLifetime / time.Duration(agent.EgressTraffic.WindowDuration))
dt := int64(podLifetime / time.Duration(agent.Config.EgressTraffic.WindowDuration))
for _, lvl := range levels {
allowance := dt*lvl.T.Threshold.Value() + lvl.T.BaseBudget.Value()
excess := resp - allowance
Expand Down
Loading

0 comments on commit 0168e35

Please sign in to comment.