/
linux.go
162 lines (128 loc) · 3.94 KB
/
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
package extractors
import (
"debug/elf"
"fmt"
"os/user"
"strconv"
"strings"
"time"
"github.com/shirou/gopsutil/process"
"go.aporeto.io/trireme-lib/common"
"go.aporeto.io/trireme-lib/policy"
"go.aporeto.io/trireme-lib/utils/cgnetcls"
portspec "go.aporeto.io/trireme-lib/utils/portspec"
)
// LinuxMetadataExtractorType is a type of Linux metadata extractors
type LinuxMetadataExtractorType func(event *common.EventInfo) (*policy.PURuntime, error)
// DefaultHostMetadataExtractor is a host specific metadata extractor
func DefaultHostMetadataExtractor(event *common.EventInfo) (*policy.PURuntime, error) {
runtimeTags := policy.NewTagStore()
for _, tag := range event.Tags {
parts := strings.SplitN(tag, "=", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid tag: %s", tag)
}
runtimeTags.AppendKeyValue("@usr:"+parts[0], parts[1])
}
options := &policy.OptionsType{
CgroupName: event.PUID,
CgroupMark: strconv.FormatUint(cgnetcls.MarkVal(), 10),
Services: event.Services,
}
runtimeIps := policy.ExtendedMap{"bridge": "0.0.0.0/0"}
return policy.NewPURuntime(event.Name, int(event.PID), "", runtimeTags, runtimeIps, event.PUType, options), nil
}
// SystemdEventMetadataExtractor is a systemd based metadata extractor
// TODO: Remove OLDTAGS
func SystemdEventMetadataExtractor(event *common.EventInfo) (*policy.PURuntime, error) {
runtimeTags := policy.NewTagStore()
for _, tag := range event.Tags {
parts := strings.SplitN(tag, "=", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid tag: %s", tag)
}
key, value := parts[0], parts[1]
if strings.HasPrefix(key, "@app:linux:") {
runtimeTags.AppendKeyValue(key, value)
continue
}
runtimeTags.AppendKeyValue("@usr:"+key, value)
}
userdata := ProcessInfo(event.PID)
for _, u := range userdata {
runtimeTags.AppendKeyValue("@sys:"+u, "true")
runtimeTags.AppendKeyValue("@app:linux:"+u, "true")
}
runtimeTags.AppendKeyValue("@sys:hostname", findFQDN(time.Second))
runtimeTags.AppendKeyValue("@os:hostname", findFQDN(time.Second))
options := policy.OptionsType{}
for index, s := range event.Services {
if s.Port != 0 && s.Ports == nil {
if pspec, err := portspec.NewPortSpec(s.Port, s.Port, nil); err == nil {
event.Services[index].Ports = pspec
event.Services[index].Port = 0
} else {
return nil, fmt.Errorf("Invalid Port Spec %s", err)
}
}
}
options.Services = event.Services
options.UserID, _ = runtimeTags.Get("@usr:originaluser")
options.CgroupMark = strconv.FormatUint(cgnetcls.MarkVal(), 10)
options.AutoPort = event.AutoPort
runtimeIps := policy.ExtendedMap{"bridge": "0.0.0.0/0"}
return policy.NewPURuntime(event.Name, int(event.PID), "", runtimeTags, runtimeIps, event.PUType, &options), nil
}
// ProcessInfo returns all metadata captured by a process
func ProcessInfo(pid int32) []string {
userdata := []string{}
p, err := process.NewProcess(pid)
if err != nil {
return userdata
}
uids, err := p.Uids()
if err != nil {
return userdata
}
groups, err := p.Gids()
if err != nil {
return userdata
}
username, err := p.Username()
if err != nil {
return userdata
}
for _, uid := range uids {
userdata = append(userdata, "uid:"+strconv.Itoa(int(uid)))
}
for _, gid := range groups {
userdata = append(userdata, "gid:"+strconv.Itoa(int(gid)))
}
userdata = append(userdata, "username:"+username)
userid, err := user.Lookup(username)
if err != nil {
return userdata
}
gids, err := userid.GroupIds()
if err != nil {
return userdata
}
for i := 0; i < len(gids); i++ {
userdata = append(userdata, "gids:"+gids[i])
group, err := user.LookupGroupId(gids[i])
if err != nil {
continue
}
userdata = append(userdata, "groups:"+group.Name)
}
return userdata
}
// Libs returns the list of dynamic library dependencies of an executable
func Libs(binpath string) []string {
f, err := elf.Open(binpath)
if err != nil {
return []string{}
}
libraries, _ := f.ImportedLibraries()
return libraries
}