-
Notifications
You must be signed in to change notification settings - Fork 381
/
Copy pathproc_reader.go
440 lines (383 loc) · 11.5 KB
/
proc_reader.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
438
439
440
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon
package procevents
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strconv"
"time"
"unicode/utf8"
"github.com/cilium/tetragon/pkg/api"
"github.com/cilium/tetragon/pkg/api/ops"
"github.com/cilium/tetragon/pkg/api/processapi"
"github.com/cilium/tetragon/pkg/bpf"
"github.com/cilium/tetragon/pkg/kernels"
"github.com/cilium/tetragon/pkg/logger"
"github.com/cilium/tetragon/pkg/observer"
"github.com/cilium/tetragon/pkg/option"
"github.com/cilium/tetragon/pkg/reader/caps"
"github.com/cilium/tetragon/pkg/reader/namespace"
"github.com/cilium/tetragon/pkg/reader/proc"
"github.com/cilium/tetragon/pkg/sensors/base"
"github.com/cilium/tetragon/pkg/sensors/exec/execvemap"
)
const (
maxMapRetries = 4
mapRetryDelay = 1
kernelPid = uint32(0)
)
func stringToUTF8(s []byte) []byte {
var utf8Cursor int
var i int
for i < len(s) {
r, size := utf8.DecodeRune(s[i:])
utf8Cursor += utf8.EncodeRune(s[utf8Cursor:], r)
i += size
}
return s
}
type Procs struct {
psize uint32
ppid uint32
pnspid uint32
pflags uint32
pktime uint64
pargs []byte
size uint32
uid uint32
pid uint32
nspid uint32
auid uint32
flags uint32
ktime uint64
args []byte
effective uint64
inheritable uint64
permitted uint64
uts_ns uint32
ipc_ns uint32
mnt_ns uint32
pid_ns uint32
pid_for_children_ns uint32
net_ns uint32
time_ns uint32
time_for_children_ns uint32
cgroup_ns uint32
user_ns uint32
}
func procKernel() Procs {
kernelArgs := []byte("<kernel>\u0000")
return Procs{
psize: uint32(processapi.MSG_SIZEOF_EXECVE + len(kernelArgs) + processapi.MSG_SIZEOF_CWD),
ppid: kernelPid,
pnspid: 0,
pflags: api.EventProcFS,
pktime: 1,
pargs: kernelArgs,
size: uint32(processapi.MSG_SIZEOF_EXECVE + len(kernelArgs) + processapi.MSG_SIZEOF_CWD),
uid: 0,
pid: kernelPid,
nspid: 0,
auid: 0,
flags: api.EventProcFS,
ktime: 1,
args: kernelArgs,
effective: 0,
inheritable: 0,
permitted: 0,
}
}
func getCWD(pid uint32) (string, uint32) {
flags := uint32(0)
pidstr := fmt.Sprint(pid)
if pid == 0 {
return "", flags
}
cwd, err := os.Readlink(filepath.Join(option.Config.ProcFS, pidstr, "cwd"))
if err != nil {
flags |= api.EventRootCWD | api.EventErrorCWD
return " ", flags
}
if cwd == "/" {
cwd = " "
flags |= api.EventRootCWD
}
return cwd, flags
}
func pushExecveEvents(p Procs, pushExecve, writeMaps bool) {
var err error
args, filename := procsFilename(p.args)
cwd, flags := getCWD(p.pid)
if (flags & api.EventRootCWD) == 0 {
args = args + " " + cwd
}
m := processapi.MsgExecveEventUnix{}
m.Common.Op = ops.MSG_OP_EXECVE
m.Common.Size = processapi.MsgUnixSize + p.psize + p.size
m.Kube.NetNS = 0
m.Kube.Cid = 0
m.Kube.Cgrpid = 0
m.Kube.Docker, err = procsDockerId(p.pid)
if err != nil {
logger.GetLogger().WithError(err).Warn("Procfs execve event pods/ identifier error")
}
m.Parent.Pid = p.ppid
m.Parent.Ktime = p.pktime
m.Capabilities.Permitted = p.permitted
m.Capabilities.Effective = p.effective
m.Capabilities.Inheritable = p.inheritable
m.Namespaces.UtsInum = p.uts_ns
m.Namespaces.IpcInum = p.ipc_ns
m.Namespaces.MntInum = p.mnt_ns
m.Namespaces.PidInum = p.pid_ns
m.Namespaces.PidChildInum = p.pid_for_children_ns
m.Namespaces.NetInum = p.net_ns
m.Namespaces.TimeInum = p.time_ns
m.Namespaces.TimeChildInum = p.time_for_children_ns
m.Namespaces.CgroupInum = p.cgroup_ns
m.Namespaces.UserInum = p.user_ns
m.Process.Size = p.size
m.Process.PID = p.pid
m.Process.NSPID = p.nspid
m.Process.UID = p.uid
m.Process.AUID = p.auid
m.Process.Flags = p.flags | flags
m.Process.Ktime = p.ktime
m.Common.Ktime = p.ktime
m.Process.Filename = filename
m.Process.Args = args
if pushExecve {
observer.AllListeners(&m)
}
}
func writeExecveMap(procs []Procs) {
mapDir := bpf.MapPrefixPath()
execveMap := base.GetExecveMap()
if execveMap.PinState.IsDisabled() {
logger.GetLogger().Infof("tetragon, map %s is disabled, skipping.", execveMap.Name)
return
}
m, err := bpf.OpenMap(filepath.Join(mapDir, execveMap.Name))
for i := 0; err != nil; i++ {
m, err = bpf.OpenMap(filepath.Join(mapDir, execveMap.Name))
if err != nil {
time.Sleep(mapRetryDelay * time.Second)
}
if i > maxMapRetries {
panic(err)
}
}
for _, p := range procs {
k := &execvemap.ExecveKey{Pid: p.pid}
v := &execvemap.ExecveValue{}
v.Parent.Pid = p.ppid
v.Parent.Ktime = p.pktime
v.Process.Pid = p.pid
v.Process.Ktime = p.ktime
v.Flags = 0
v.Nspid = p.nspid
v.Buffer = 0
m.Update(k, v)
}
// In order for kprobe events from kernel ctx to not abort we need the
// execve lookup to map to a valid entry. So to simplify the kernel side
// and avoid having to add another branch of logic there to handle pid==0
// case we simply add it here.
m.Update(&execvemap.ExecveKey{Pid: kernelPid}, &execvemap.ExecveValue{
Parent: processapi.MsgExecveKey{
Pid: kernelPid,
Ktime: 1},
Process: processapi.MsgExecveKey{
Pid: kernelPid,
Ktime: 1,
},
Flags: 0,
Nspid: 0,
Buffer: 0,
})
m.Close()
}
func pushEvents(procs []Procs, pushExecve, writeMaps bool) {
if writeMaps {
writeExecveMap(procs)
}
sort.Slice(procs, func(i, j int) bool {
return procs[i].ppid < procs[j].ppid
})
procs = append(procs, procKernel())
for _, p := range procs {
pushExecveEvents(p, pushExecve, writeMaps)
}
}
func GetRunningProcs(write, push bool) []Procs {
var procs []Procs
procFS, err := ioutil.ReadDir(option.Config.ProcFS)
if err != nil {
logger.GetLogger().WithError(err).Errorf("Could not read directory %s", option.Config.ProcFS)
return nil
}
kernelVer, _, _ := kernels.GetKernelVersion(option.Config.KernelVersion, option.Config.ProcFS)
// time and time_for_children namespaces introduced in kernel 5.6
hasTimeNs := (int64(kernelVer) >= kernels.KernelStringToNumeric("5.6.0"))
for _, d := range procFS {
var pcmdline []byte
var pstats []string
var pktime uint64
var pexecPath string
var pnspid uint32
if d.IsDir() == false {
continue
}
pathName := filepath.Join(option.Config.ProcFS, d.Name())
cmdline, err := ioutil.ReadFile(filepath.Join(pathName, "cmdline"))
if err != nil {
continue
}
if string(cmdline) == "" {
continue
}
pid, err := proc.GetProcPid(d.Name())
if err != nil {
logger.GetLogger().WithError(err).Warnf("pid read error")
continue
}
stats, err := proc.GetProcStatStrings(pathName)
if err != nil {
logger.GetLogger().WithError(err).Warnf("stats read error")
continue
}
ppid := stats[3]
_ppid, err := strconv.ParseUint(ppid, 10, 32)
if err != nil {
_ppid = 0 // 0 pid indicates no known parent
}
ktime, err := proc.GetStatsKtime(stats)
if err != nil {
logger.GetLogger().WithError(err).Warnf("ktime read error")
}
nspid, permitted, effective, inheritable := caps.GetPIDCaps(filepath.Join(option.Config.ProcFS, d.Name(), "status"))
uts_ns := namespace.GetPidNsInode(uint32(pid), "uts")
ipc_ns := namespace.GetPidNsInode(uint32(pid), "ipc")
mnt_ns := namespace.GetPidNsInode(uint32(pid), "mnt")
pid_ns := namespace.GetPidNsInode(uint32(pid), "pid")
pid_for_children_ns := namespace.GetPidNsInode(uint32(pid), "pid_for_children")
net_ns := namespace.GetPidNsInode(uint32(pid), "net")
time_ns := uint32(0)
time_for_children_ns := uint32(0)
if hasTimeNs {
time_ns = namespace.GetPidNsInode(uint32(pid), "time")
time_for_children_ns = namespace.GetPidNsInode(uint32(pid), "time_for_children")
}
cgroup_ns := namespace.GetPidNsInode(uint32(pid), "cgroup")
user_ns := namespace.GetPidNsInode(uint32(pid), "user")
// On error procsDockerId zeros dockerId so we can ignore any errors.
dockerId, _ := procsDockerId(uint32(pid))
if dockerId == "" {
nspid = 0
}
if _ppid != 0 {
var err error
parentPath := filepath.Join(option.Config.ProcFS, ppid)
pcmdline, err = ioutil.ReadFile(filepath.Join(parentPath, "cmdline"))
if err != nil {
logger.GetLogger().WithError(err).WithField("path", parentPath).Warn("parent cmdline error")
continue
}
pstats, err = proc.GetProcStatStrings(string(parentPath))
if err != nil {
logger.GetLogger().WithError(err).Warnf("parent stats read error")
continue
}
pktime, err = proc.GetStatsKtime(pstats)
if err != nil {
logger.GetLogger().WithError(err).Warnf("parent ktime read error")
}
if dockerId != "" {
pnspid, _, _, _ = caps.GetPIDCaps(filepath.Join(option.Config.ProcFS, ppid, "status"))
}
} else {
pcmdline = nil
pstats = nil
pktime = 0
pnspid = 0
}
execPath, err := os.Readlink(filepath.Join(option.Config.ProcFS, d.Name(), "exe"))
if err == nil {
cmdline = proc.PrependPath(execPath, cmdline)
}
if _ppid != 0 {
pexecPath, err = os.Readlink(filepath.Join(option.Config.ProcFS, ppid, "exe"))
if err == nil {
pcmdline = proc.PrependPath(pexecPath, pcmdline)
}
} else {
pexecPath = ""
}
pcmdsUTF := stringToUTF8(pcmdline)
cmdsUTF := stringToUTF8(cmdline)
p := Procs{
ppid: uint32(_ppid), pnspid: pnspid, pargs: pcmdsUTF,
pflags: api.EventProcFS | api.EventNeedsCWD | api.EventNeedsAUID,
pktime: pktime,
pid: uint32(pid), nspid: nspid, args: cmdsUTF,
flags: api.EventProcFS | api.EventNeedsCWD | api.EventNeedsAUID,
ktime: ktime,
permitted: permitted,
effective: effective,
inheritable: inheritable,
uts_ns: uts_ns,
ipc_ns: ipc_ns,
mnt_ns: mnt_ns,
pid_ns: pid_ns,
pid_for_children_ns: pid_for_children_ns,
net_ns: net_ns,
time_ns: time_ns,
time_for_children_ns: time_for_children_ns,
cgroup_ns: cgroup_ns,
user_ns: user_ns,
}
p.size = uint32(processapi.MSG_SIZEOF_EXECVE + len(p.args) + processapi.MSG_SIZEOF_CWD)
p.psize = uint32(processapi.MSG_SIZEOF_EXECVE + len(p.pargs) + processapi.MSG_SIZEOF_CWD)
/* If we can't fit this in the buffer lets trim some parts and
* make it fit.
*/
if p.size+p.psize > processapi.MSG_SIZEOF_BUFFER {
var deduct uint32
var need int32
need = int32((p.size + p.psize) - processapi.MSG_SIZEOF_BUFFER)
// First consume CWD space from parent because this speculative extra space
// next try to consume CWD space from child and finally start truncating args
// if necessary.
deduct = processapi.MSG_SIZEOF_CWD
p.pflags = p.pflags & ^uint32(api.EventNeedsCWD)
p.pflags = p.pflags | api.EventNoCWDSupport
p.psize -= deduct
need -= int32(deduct)
if need > 0 {
deduct = processapi.MSG_SIZEOF_CWD
p.size -= deduct
p.flags = p.flags & ^uint32(api.EventNeedsCWD)
p.flags = p.flags | api.EventNoCWDSupport
need -= int32(deduct)
}
for i := int32(0); i < need; i++ {
if len(p.pargs) > len(p.args) {
p.pflags |= api.EventTruncArgs
p.pargs = p.pargs[:len(p.pargs)-1]
p.psize--
} else {
p.flags |= api.EventTruncArgs
p.args = p.args[:len(p.args)-1]
p.size--
}
}
}
procs = append(procs, p)
}
logger.GetLogger().Infof("Read ProcFS %s appended %d/%d entries", option.Config.ProcFS, len(procs), len(procFS))
pushEvents(procs, push, write)
return procs
}