/
linux.go
225 lines (183 loc) · 5.21 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
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
package extractors
import (
"crypto/md5"
"debug/elf"
"encoding/hex"
"fmt"
"io"
"net"
"os"
"os/user"
"strconv"
"strings"
"time"
"github.com/aporeto-inc/trireme-lib/common"
"github.com/aporeto-inc/trireme-lib/policy"
"github.com/aporeto-inc/trireme-lib/utils/cgnetcls"
portspec "github.com/aporeto-inc/trireme-lib/utils/portspec"
"github.com/shirou/gopsutil/process"
)
// 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, common.LinuxProcessPU, options), nil
}
// SystemdEventMetadataExtractor is a systemd based metadata extractor
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)
}
runtimeTags.AppendKeyValue("@usr:"+parts[0], parts[1])
}
userdata := ProcessInfo(event.PID)
for _, u := range userdata {
runtimeTags.AppendKeyValue("@sys:"+u, "true")
}
runtimeTags.AppendKeyValue("@sys:hostname", findFQDN(time.Second))
if fileMd5, err := computeFileMd5(event.Name); err == nil {
runtimeTags.AppendKeyValue("@sys:filechecksum", hex.EncodeToString(fileMd5))
}
depends := libs(event.Name)
for _, lib := range depends {
runtimeTags.AppendKeyValue("@sys:lib:"+lib, "true")
}
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)
runtimeIps := policy.ExtendedMap{"bridge": "0.0.0.0/0"}
return policy.NewPURuntime(event.Name, int(event.PID), "", runtimeTags, runtimeIps, common.LinuxProcessPU, &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
}
// computeFileMd5 computes the Md5 of a file
func computeFileMd5(filePath string) ([]byte, error) {
var result []byte
file, err := os.Open(filePath)
if err != nil {
return result, err
}
defer file.Close() //nolint : errcheck
hash := md5.New()
if _, err := io.Copy(hash, file); err != nil {
return result, err
}
return hash.Sum(result), nil
}
func findFQDN(expiration time.Duration) string {
hostname, err := os.Hostname()
if err != nil {
return "unknown"
}
// Try to find FQDN
globalHostname := make(chan string, 1)
go func() {
addrs, err := net.LookupIP(hostname)
if err != nil {
globalHostname <- hostname
return
}
for _, addr := range addrs {
if ipv4 := addr.To4(); ipv4 != nil {
ip, err := ipv4.MarshalText()
if err != nil {
globalHostname <- hostname
return
}
hosts, err := net.LookupAddr(string(ip))
if err != nil || len(hosts) == 0 {
globalHostname <- hostname
return
}
fqdn := hosts[0]
globalHostname <- strings.TrimSuffix(fqdn, ".") // return fqdn without trailing dot
}
}
}()
// Use OS hostname if we dont hear back in a second
select {
case <-time.After(expiration):
return hostname
case name := <-globalHostname:
return name
}
}
// 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
}