This repository has been archived by the owner on Aug 18, 2023. It is now read-only.
/
from_log.go
97 lines (77 loc) · 1.98 KB
/
from_log.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
package seccomp
import (
"bufio"
"io"
"regexp"
"strconv"
specs "github.com/opencontainers/runtime-spec/specs-go"
)
// SyscallsFromLog represents a syscalls source from syslog files.
type SyscallsFromLog struct {
action specs.LinuxSeccompAction
reader io.Reader
processID int
}
// NewSyscallsFromLog initialises and returns a new SyscallsFromLog
func NewSyscallsFromLog(reader io.Reader, processID int) *SyscallsFromLog {
return &SyscallsFromLog{
specs.ActAllow,
reader,
processID,
}
}
// GetSystemCalls returns all system calls found in the syslog for a given processID.
func (s *SyscallsFromLog) GetSystemCalls() (*specs.LinuxSyscall, error) {
syscalls, err := s.getSystemCallsFromLog()
if err != nil {
return nil, err
}
if len(syscalls) == 0 {
return nil, nil
}
r := specs.LinuxSyscall{
Action: s.action,
Names: make([]string, 0, len(syscalls)),
}
for _, name := range syscalls {
r.Names = append(r.Names, name)
}
return &r, nil
}
func (s *SyscallsFromLog) getSystemCallsFromLog() ([]string, error) {
scan := bufio.NewScanner(s.reader)
scan.Split(bufio.ScanLines)
trackUnique := make(map[int]string)
syscalls := make([]string, 0)
for scan.Scan() {
id := s.extractSyscallID(scan.Text(), s.processID)
if id > -1 {
if _, ok := trackUnique[id]; !ok {
trackUnique[id] = ""
name, err := getSyscallName(id)
if err != nil {
return nil, err
}
syscalls = append(syscalls, name)
}
}
}
return syscalls, nil
}
func (*SyscallsFromLog) extractSyscallID(logLine string, processID int) int {
syscallID := -1
if logLine == "" {
return syscallID
}
pidRegex := strconv.Itoa(processID)
re := regexp.MustCompile(`(?:audit:.+pid=` + pidRegex + `\b).+syscall=(\b\d+\b)`)
captures := re.FindStringSubmatch(logLine)
if captures != nil && len(captures) > 1 {
if captures[len(captures)-1] != "" {
if tmpValue, err := strconv.Atoi(captures[len(captures)-1]); err == nil {
syscallID = tmpValue
}
}
}
return syscallID
}