forked from johnjohnsp1/capsule8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kprobe.go
162 lines (140 loc) · 4.8 KB
/
kprobe.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
// Copyright 2017 Capsule8, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sensor
import (
"fmt"
"regexp"
"strings"
api "github.com/capsule8/capsule8/api/v0"
"github.com/capsule8/capsule8/pkg/expression"
"github.com/capsule8/capsule8/pkg/sys/perf"
"github.com/golang/glog"
)
type kprobeFilter struct {
symbol string
onReturn bool
arguments map[string]string
filter string
sensor *Sensor
}
var validSymbolRegex = regexp.MustCompile("^[A-Za-z_]{1}[\\w]*$")
func newKprobeFilter(kef *api.KernelFunctionCallFilter) *kprobeFilter {
// The symbol must begin with [A-Za-z_] and contain only [A-Za-z0-9_]
// We do not accept addresses or offsets
if !validSymbolRegex.MatchString(kef.Symbol) {
return nil
}
var filterString string
if kef.FilterExpression != nil {
expr, err := expression.NewExpression(kef.FilterExpression)
if err != nil {
glog.V(1).Infof("Bad kprobe filter expression: %s", err)
return nil
}
filterString = expr.KernelFilterString()
}
filter := &kprobeFilter{
symbol: kef.Symbol,
arguments: kef.Arguments,
filter: filterString,
}
switch kef.Type {
case api.KernelFunctionCallEventType_KERNEL_FUNCTION_CALL_EVENT_TYPE_ENTER:
filter.onReturn = false
case api.KernelFunctionCallEventType_KERNEL_FUNCTION_CALL_EVENT_TYPE_EXIT:
filter.onReturn = true
default:
return nil
}
return filter
}
func (f *kprobeFilter) decodeKprobe(sample *perf.SampleRecord, data perf.TraceEventSampleData) (interface{}, error) {
args := make(map[string]*api.KernelFunctionCallEvent_FieldValue)
for k, v := range data {
value := &api.KernelFunctionCallEvent_FieldValue{}
switch v := v.(type) {
case []byte:
value.FieldType = api.KernelFunctionCallEvent_BYTES
value.Value = &api.KernelFunctionCallEvent_FieldValue_BytesValue{BytesValue: v}
case string:
value.FieldType = api.KernelFunctionCallEvent_STRING
value.Value = &api.KernelFunctionCallEvent_FieldValue_StringValue{StringValue: v}
case int8:
value.FieldType = api.KernelFunctionCallEvent_SINT8
value.Value = &api.KernelFunctionCallEvent_FieldValue_SignedValue{SignedValue: int64(v)}
case int16:
value.FieldType = api.KernelFunctionCallEvent_SINT16
value.Value = &api.KernelFunctionCallEvent_FieldValue_SignedValue{SignedValue: int64(v)}
case int32:
value.FieldType = api.KernelFunctionCallEvent_SINT32
value.Value = &api.KernelFunctionCallEvent_FieldValue_SignedValue{SignedValue: int64(v)}
case int64:
value.FieldType = api.KernelFunctionCallEvent_SINT64
value.Value = &api.KernelFunctionCallEvent_FieldValue_SignedValue{SignedValue: v}
case uint8:
value.FieldType = api.KernelFunctionCallEvent_UINT8
value.Value = &api.KernelFunctionCallEvent_FieldValue_UnsignedValue{UnsignedValue: uint64(v)}
case uint16:
value.FieldType = api.KernelFunctionCallEvent_UINT16
value.Value = &api.KernelFunctionCallEvent_FieldValue_UnsignedValue{UnsignedValue: uint64(v)}
case uint32:
value.FieldType = api.KernelFunctionCallEvent_UINT32
value.Value = &api.KernelFunctionCallEvent_FieldValue_UnsignedValue{UnsignedValue: uint64(v)}
case uint64:
value.FieldType = api.KernelFunctionCallEvent_UINT64
value.Value = &api.KernelFunctionCallEvent_FieldValue_UnsignedValue{UnsignedValue: v}
}
args[k] = value
}
ev := f.sensor.NewEventFromSample(sample, data)
ev.Event = &api.TelemetryEvent_KernelCall{
KernelCall: &api.KernelFunctionCallEvent{
Arguments: args,
},
}
return ev, nil
}
func (f *kprobeFilter) fetchargs() string {
args := make([]string, 0, len(f.arguments))
for k, v := range f.arguments {
args = append(args, fmt.Sprintf("%s=%s", k, v))
}
return strings.Join(args, " ")
}
func registerKernelEvents(sensor *Sensor, eventMap subscriptionMap, events []*api.KernelFunctionCallFilter) {
for _, kef := range events {
f := newKprobeFilter(kef)
if f == nil {
glog.V(1).Infof("Invalid kprobe symbol: %s", kef.Symbol)
continue
}
f.sensor = sensor
eventID, err := sensor.monitor.RegisterKprobe(
f.symbol, f.onReturn, f.fetchargs(),
f.decodeKprobe,
perf.WithFilter(f.filter))
if err != nil {
var loc string
if f.onReturn {
loc = "return"
} else {
loc = "entry"
}
glog.V(1).Infof("Couldn't register kprobe on %s %s [%s]: %v",
f.symbol, loc, f.fetchargs(), err)
continue
}
eventMap[eventID] = &subscription{}
}
}