-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtracer_linux.go
345 lines (299 loc) · 8.8 KB
/
tracer_linux.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
//go:build linux
// +build linux
package exectrace
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"log"
"runtime"
"runtime/debug"
"strings"
"sync"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/ringbuf"
"github.com/cilium/ebpf/rlimit"
"github.com/hashicorp/go-multierror"
"golang.org/x/sys/unix"
"golang.org/x/xerrors"
)
// These constants are defined in `bpf/handler.c` and must be kept in sync.
const (
arglen = 32
argsize = 1024
logfmtsize = 1024
logarglen = 3
)
var errTracerClosed = xerrors.New("tracer is closed")
// event contains details about each exec call, sent from the eBPF program to
// userspace through a perf ring buffer. This type must be kept in sync with
// `event_t` in `bpf/handler.c`.
type event struct {
// Details about the process being launched.
Filename [argsize]byte
Argv [arglen][argsize]byte
Argc uint32
UID uint32
GID uint32
PID uint32
// Name of the calling process.
Comm [argsize]byte
}
// logEntry contains each kernel log entry from the logs ringbuf. This type must
// be kept in sync with `log_entry_t` in `bpf/handler.c`.
type logEntry struct {
UID uint32
GID uint32
PID uint32
Fmt [logfmtsize]byte
// Args are uint32s but depending on the format string, they may be
// interpreted as int32s instead.
Arg [logarglen]uint32
}
type tracer struct {
opts *TracerOpts
objs *bpfObjects
tp link.Link
rbEvents *ringbuf.Reader
rbLogs *ringbuf.Reader
closeLock sync.Mutex
closed chan struct{}
}
var _ Tracer = &tracer{}
// New instantiates all of the BPF objects into the running kernel, starts
// tracing, and returns the created Tracer. After calling this successfully, the
// caller should immediately attach a for loop running `h.Read()`.
//
// The returned Tracer MUST be closed to avoid leaking kernel resources.
func New(opts *TracerOpts) (Tracer, error) {
if opts == nil {
opts = &TracerOpts{}
}
if opts.LogFn == nil {
opts.LogFn = func(uid, gid, pid uint32, logLine string) {
log.Printf("error log from exectrace tracer (uid=%v, gid=%v, pid=%v): %s", uid, gid, pid, logLine)
}
}
objs, err := loadBPFObjects()
if err != nil {
return nil, xerrors.Errorf("load BPF objects: %w", err)
}
t := &tracer{
opts: opts,
objs: objs,
tp: nil,
rbEvents: nil,
rbLogs: nil,
closeLock: sync.Mutex{},
closed: make(chan struct{}),
}
err = t.start()
if err != nil {
// Best effort.
_ = t.Close()
return nil, xerrors.Errorf("start tracer: %w", err)
}
// It could be very bad if someone forgot to close this, so we'll try to
// detect when it doesn't get closed and log a warning.
stack := debug.Stack()
runtime.SetFinalizer(t, func(t *tracer) {
err := t.Close()
if xerrors.Is(err, errTracerClosed) {
return
}
log.Printf("tracer was finalized but was not closed, created at: %s", stack)
log.Print("tracers must be closed when finished with to avoid leaked kernel resources")
if err != nil {
log.Printf("closing tracer failed: %+v", err)
}
})
return t, nil
}
func (t *tracer) FD() int {
return t.objs.EnterExecveProg.FD()
}
func (t *tracer) start() error {
// If we don't startup successfully, we need to make sure all of the stuff
// is cleaned up properly or we'll be leaking kernel resources.
ok := false
defer func() {
if !ok {
// Best effort.
_ = t.Close()
}
}()
// Allow the current process to lock memory for eBPF resources. This does
// nothing on 5.11+ kernels which don't need this.
err := rlimit.RemoveMemlock()
if err != nil {
return xerrors.Errorf("remove memlock: %w", err)
}
// Set filter options on the filters map.
if t.opts.PidNS != 0 {
err = t.objs.FiltersMap.Update(uint32(0), t.opts.PidNS, ebpf.UpdateAny)
if err != nil {
return xerrors.Errorf("apply PID NS filter to eBPF map: %w", err)
}
}
// Attach the eBPF program to the `sys_enter_execve` tracepoint, which
// is triggered at the beginning of each `execve()` syscall.
t.tp, err = link.Tracepoint("syscalls", "sys_enter_execve", t.objs.EnterExecveProg, nil)
if err != nil {
return xerrors.Errorf("open tracepoint: %w", err)
}
// Create the reader for the event ringbuf.
t.rbEvents, err = ringbuf.NewReader(t.objs.EventsMap)
if err != nil {
return xerrors.Errorf("open events ringbuf reader: %w", err)
}
// Create the reader for the log ringbuf.
t.rbLogs, err = ringbuf.NewReader(t.objs.LogsMap)
if err != nil {
return xerrors.Errorf("open logs ringbuf reader: %w", err)
}
// Start slurping up logs.
go t.readLogs(t.rbLogs, t.opts.LogFn)
ok = true
return nil
}
// Read reads an event from the eBPF program via the ringbuf, parses it and
// returns it. If the *tracer is closed during the blocked call, and error that
// wraps io.EOF will be returned.
func (t *tracer) Read() (*Event, error) {
rb := t.rbEvents
if rb == nil {
return nil, xerrors.Errorf("events ringbuf reader is not initialized: %w", io.EOF)
}
record, err := rb.Read()
if err != nil {
if errors.Is(err, ringbuf.ErrClosed) {
return nil, xerrors.Errorf("tracer closed: %w", io.EOF)
}
return nil, xerrors.Errorf("read from ringbuf: %w", err)
}
// Parse the ringbuf event entry into an event structure.
var rawEvent event
err = binary.Read(bytes.NewBuffer(record.RawSample), NativeEndian, &rawEvent)
if err != nil {
return nil, xerrors.Errorf("parse raw ringbuf entry into event struct: %w", err)
}
ev := &Event{
Filename: unix.ByteSliceToString(rawEvent.Filename[:]),
Argv: []string{}, // populated below
Truncated: rawEvent.Argc == arglen+1,
PID: rawEvent.PID,
UID: rawEvent.UID,
GID: rawEvent.GID,
Comm: unix.ByteSliceToString(rawEvent.Comm[:]),
}
// Copy only the args we're allowed to read from the array. If we read more
// than rawEvent.Argc, we could be copying non-zeroed memory.
argc := int(rawEvent.Argc)
if argc > arglen {
argc = arglen
}
for i := 0; i < argc; i++ {
str := unix.ByteSliceToString(rawEvent.Argv[i][:])
// The copy in the eBPF code only copies 1023 bytes.
if len(str) >= argsize-1 {
ev.Truncated = true
// Set final 3 bytes to "..." to indicate truncation.
str = str[:argsize-3] + "..."
}
if strings.TrimSpace(str) != "" {
ev.Argv = append(ev.Argv, str)
}
}
return ev, nil
}
func (t *tracer) readLogs(rbLogs *ringbuf.Reader, logFn func(uid, gid, pid uint32, logLine string)) {
defer func() {
if r := recover(); r != nil {
logFn(0, 0, 0, fmt.Sprintf("panic in (*tracer).readLogs() goroutine: %v", r))
_ = t.Close()
}
}()
for {
record, err := rbLogs.Read()
if err != nil {
if errors.Is(err, ringbuf.ErrClosed) {
return
}
logFn(0, 0, 0, fmt.Sprintf("read from logs ringbuf: %+v", err))
continue
}
var logEntry logEntry
err = binary.Read(bytes.NewBuffer(record.RawSample), NativeEndian, &logEntry)
if err != nil {
logFn(0, 0, 0, fmt.Sprintf("parse raw ringbuf entry into logEntry struct: %+v", err))
continue
}
// Format the log line.
// 1. Find all %u and %d directives in the string (this is all we
// support).
// 2. For each:
// 1. If it's a %u, replace it with the next uint32 in the args.
// 2. If it's a %d, cast the next uint32 to an int32 and replace.
logLine := unix.ByteSliceToString(logEntry.Fmt[:])
for i := 0; i < logarglen; i++ {
arg := logEntry.Arg[i]
// Find the next %u or %d in the log line.
uIndex := strings.Index(logLine, `%u`)
dIndex := strings.Index(logLine, `%d`)
if uIndex == -1 && dIndex == -1 {
break
}
if uIndex < dIndex || dIndex == -1 {
logLine = strings.Replace(logLine, `%u`, fmt.Sprint(arg), 1)
}
if dIndex < uIndex || uIndex == -1 {
logLine = strings.Replace(logLine, `%d`, fmt.Sprint(int32(arg)), 1)
}
}
logFn(logEntry.UID, logEntry.GID, logEntry.PID, logLine)
}
}
// Close gracefully closes and frees all resources associated with the eBPF
// tracepoints, maps and other resources. Any blocked `Read()` operations will
// return an error that wraps `io.EOF`.
func (t *tracer) Close() error {
t.closeLock.Lock()
defer t.closeLock.Unlock()
select {
case <-t.closed:
return errTracerClosed
default:
}
close(t.closed)
runtime.SetFinalizer(t, nil)
// Close everything started in h.Start() in reverse order.
var merr error
if t.rbLogs != nil {
err := t.rbLogs.Close()
if err != nil {
merr = multierror.Append(merr, xerrors.Errorf("close logs ringbuf reader: %w", err))
}
}
if t.rbEvents != nil {
err := t.rbEvents.Close()
if err != nil {
merr = multierror.Append(merr, xerrors.Errorf("close events ringbuf reader: %w", err))
}
}
if t.tp != nil {
err := t.tp.Close()
if err != nil {
merr = multierror.Append(merr, xerrors.Errorf("close tracepoint: %w", err))
}
}
if t.objs != nil {
err := t.objs.Close()
if err != nil {
merr = multierror.Append(merr, xerrors.Errorf("close eBPF objects: %w", err))
}
}
return merr
}