-
Notifications
You must be signed in to change notification settings - Fork 51
/
processmon.go
430 lines (365 loc) · 11.8 KB
/
processmon.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
// Package processmon is to manage and monitor remote enforcers.
package processmon
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"sync"
"syscall"
"time"
"go.aporeto.io/trireme-lib/controller/constants"
"go.aporeto.io/trireme-lib/controller/internal/enforcer/utils/rpcwrapper"
"go.aporeto.io/trireme-lib/controller/pkg/claimsheader"
"go.aporeto.io/trireme-lib/controller/pkg/remoteenforcer"
"go.aporeto.io/trireme-lib/policy"
"go.aporeto.io/trireme-lib/utils/cache"
"go.aporeto.io/trireme-lib/utils/crypto"
"go.uber.org/zap"
)
var (
// launcher supports only a global processMon instance
launcher *processMon
)
const (
// netNSPath holds the directory to ensure ip netns command works
netNSPath = "/var/run/netns/"
processMonitorCacheName = "ProcessMonitorCache"
remoteEnforcerBuildName = "remoteenforcerd"
remoteEnforcerTempBuildPath = "/var/run/aporeto/tmp/bin/"
secretLength = 32
)
// processMon is an instance of processMonitor
type processMon struct {
// netNSPath made configurable to enable running tests
netNSPath string
// remoteEnforcerTempBuildPath made configurable to enable running tests
remoteEnforcerTempBuildPath string
// remoteEnforcerBuildName made configurable to enable running tests
remoteEnforcerBuildName string
activeProcesses *cache.Cache
childExitStatus chan exitStatus
// logToConsole stores if we should log to console.
logToConsole bool
// logWithID is the ID for for log files if logging to file.
logWithID bool
// logLevel is the level of logs for remote command.
logLevel string
logFormat string
// compressedTags instructs the remotes to use compressed tags.
compressedTags claimsheader.CompressionType
// runtimeErrorChannel is the channel to communicate errors to the policy engine.
runtimeErrorChannel chan *policy.RuntimeError
sync.Mutex
}
// processInfo stores per process information
type processInfo struct {
contextID string
RPCHdl rpcwrapper.RPCClient
process *os.Process
sync.Mutex
}
// exitStatus captures the exit status of a process
type exitStatus struct {
process int
// The contextID is optional and is primarily used by remote enforcer
// processes to represent the namespace in which the process was running
contextID string
exitStatus error
}
func init() {
// Setup new launcher
newProcessMon(netNSPath, remoteEnforcerTempBuildPath, remoteEnforcerBuildName)
}
// contextID2SocketPath returns the socket path to use for a givent context
func contextID2SocketPath(contextID string) string {
if contextID == "" {
panic("contextID is empty")
}
return filepath.Join("/var/run/", contextID+".sock")
}
// processIOReader will read from a reader and print it on the calling process
func processIOReader(fd io.Reader, contextID string) {
reader := bufio.NewReader(fd)
for {
str, err := reader.ReadString('\n')
if err != nil {
return
}
fmt.Print("[" + contextID + "]:" + str)
}
}
// newProcessMon is a method to create a new processmon
func newProcessMon(netns, remoteEnforcerPath, remoteEnforcerName string) ProcessManager {
launcher = &processMon{
remoteEnforcerTempBuildPath: remoteEnforcerPath,
remoteEnforcerBuildName: remoteEnforcerName,
netNSPath: netns,
activeProcesses: cache.NewCache(processMonitorCacheName),
childExitStatus: make(chan exitStatus, 100),
}
go launcher.collectChildExitStatus()
return launcher
}
// GetProcessManagerHdl returns a process manager handle.
func GetProcessManagerHdl() ProcessManager {
return launcher
}
// collectChildExitStatus is an async function which collects status for all launched child processes
func (p *processMon) collectChildExitStatus() {
for es := range p.childExitStatus {
data, err := p.activeProcesses.Get(es.contextID)
if err != nil {
continue
}
p.activeProcesses.Remove(es.contextID) // nolint errcheck
procInfo, ok := data.(*processInfo)
if !ok {
zap.L().Error("Internal error - wrong data structure")
continue
}
procInfo.RPCHdl.DestroyRPCClient(es.contextID)
if p.runtimeErrorChannel != nil {
if es.exitStatus != nil {
zap.L().Error("Remote enforcer exited with an error",
zap.String("nativeContextID", es.contextID),
zap.Int("pid", es.process),
zap.Error(es.exitStatus),
)
} else {
zap.L().Warn("Remote enforcer exited",
zap.String("nativeContextID", es.contextID),
zap.Int("pid", es.process),
)
}
p.runtimeErrorChannel <- &policy.RuntimeError{
ContextID: es.contextID,
Error: fmt.Errorf("Remote killed:%s", es.exitStatus),
}
}
zap.L().Debug("Remote enforcer exited normally",
zap.String("nativeContextID", es.contextID),
zap.Int("pid", es.process),
)
}
}
// SetLogParameters setups args that should be propagated to child processes
func (p *processMon) SetLogParameters(logToConsole, logWithID bool, logLevel string, logFormat string, compressedTags claimsheader.CompressionType) {
p.logToConsole = logToConsole
p.logWithID = logWithID
p.logLevel = logLevel
p.logFormat = logFormat
p.compressedTags = compressedTags
}
func (p *processMon) SetRuntimeErrorChannel(e chan *policy.RuntimeError) {
p.runtimeErrorChannel = e
}
// KillProcess sends a rpc to the process to exit failing which it will kill the process
func (p *processMon) KillProcess(contextID string) {
p.Lock()
s, err := p.activeProcesses.Get(contextID)
if err != nil {
p.Unlock()
return
}
if err := p.activeProcesses.Remove(contextID); err != nil {
zap.L().Warn("Failed to remote process from cache", zap.Error(err))
}
procInfo, ok := s.(*processInfo)
if !ok {
p.Unlock()
return
}
p.Unlock()
procInfo.Lock()
defer procInfo.Unlock()
req := &rpcwrapper.Request{}
resp := &rpcwrapper.Response{}
if procInfo.process == nil {
zap.L().Error("KillProcess failed - process is nil")
return
}
req.Payload = procInfo.process.Pid
c := make(chan error, 1)
go func() {
c <- procInfo.RPCHdl.RemoteCall(contextID, remoteenforcer.EnforcerExit, req, resp)
}()
select {
case err := <-c:
if err != nil {
zap.L().Error("Failed to stop gracefully - forcing kill",
zap.String("Remote error", err.Error()))
}
procInfo.process.Kill() // nolint
case <-time.After(5 * time.Second):
zap.L().Error("Time out on terminating remote enforcer - forcing kill")
procInfo.process.Kill() // nolint
}
procInfo.RPCHdl.DestroyRPCClient(contextID)
}
// pollStdOutAndErr polls std out and err
func (p *processMon) pollStdOutAndErr(
cmd *exec.Cmd,
contextID string,
) (err error) {
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
// Stdout/err processing
go processIOReader(stdout, contextID)
go processIOReader(stderr, contextID)
return nil
}
// getLaunchProcessCmd returns the command used to launch the enforcerd
func (p *processMon) getLaunchProcessCmd(remoteEnforcerBuildPath, remoteEnforcerName, arg string) *exec.Cmd {
cmdName := filepath.Join(remoteEnforcerBuildPath, remoteEnforcerName)
cmdArgs := []string{arg}
zap.L().Debug("Enforcer executed",
zap.String("command", cmdName),
zap.Strings("args", cmdArgs),
)
return exec.Command(cmdName, cmdArgs...)
}
// getLaunchProcessEnvVars returns a slice of env variable strings where each string is in the form of key=value
func (p *processMon) getLaunchProcessEnvVars(
procMountPoint string,
contextID string,
randomkeystring string,
statsServerSecret string,
refPid int,
refNSPath string,
) []string {
newEnvVars := []string{
constants.EnvMountPoint + "=" + procMountPoint,
constants.EnvContextSocket + "=" + contextID2SocketPath(contextID),
constants.EnvStatsChannel + "=" + rpcwrapper.StatsChannel,
constants.EnvRPCClientSecret + "=" + randomkeystring,
constants.EnvStatsSecret + "=" + statsServerSecret,
constants.EnvContainerPID + "=" + strconv.Itoa(refPid),
constants.EnvLogLevel + "=" + p.logLevel,
constants.EnvLogFormat + "=" + p.logFormat,
}
if p.compressedTags != claimsheader.CompressionTypeNone {
newEnvVars = append(newEnvVars, constants.EnvCompressedTags+"="+string(p.compressedTags))
}
if p.logToConsole {
newEnvVars = append(newEnvVars, constants.EnvLogToConsole+"="+constants.EnvLogToConsoleEnable)
}
if p.logWithID {
newEnvVars = append(newEnvVars, constants.EnvLogID+"="+contextID)
}
// If the PURuntime Specified a NSPath, then it is added as a new env var also.
if refNSPath != "" {
newEnvVars = append(newEnvVars, constants.EnvNSPath+"="+refNSPath)
}
return newEnvVars
}
// LaunchProcess prepares the environment and launches the process. If the process
// is already launched, it will notify the caller, so that it can avoid any
// new initialization.
func (p *processMon) LaunchProcess(
contextID string,
refPid int,
refNSPath string,
rpchdl rpcwrapper.RPCClient,
arg string,
statsServerSecret string,
procMountPoint string,
) (bool, error) {
// Locking here to get the procesinfo to avoid race conditions
// where multiple LaunchProcess happen for the same context.
p.Lock()
if _, err := p.activeProcesses.Get(contextID); err == nil {
p.Unlock()
return false, nil
}
procInfo := &processInfo{
contextID: contextID,
RPCHdl: rpchdl,
}
p.activeProcesses.AddOrUpdate(contextID, procInfo)
p.Unlock()
// We will lock the procInfo here, so a kill will have to wait and avoid any race.
procInfo.Lock()
defer procInfo.Unlock()
// We check if the NetNsPath was given as parameter.
// If it was we will use it. Otherwise we will determine it based on the PID.
nsPath := refNSPath
if refNSPath == "" {
nsPath = filepath.Join(procMountPoint, strconv.Itoa(refPid), "ns/net")
}
hoststat, err := os.Stat(filepath.Join(procMountPoint, "1/ns/net"))
if err != nil {
return false, err
}
pidstat, err := os.Stat(nsPath)
if err != nil {
return false, fmt.Errorf("container pid %d not found: %s", refPid, err)
}
if pidstat.Sys().(*syscall.Stat_t).Ino == hoststat.Sys().(*syscall.Stat_t).Ino {
return false, fmt.Errorf("refused to launch a remote enforcer in host namespace")
}
if _, err = os.Stat(p.netNSPath); err != nil {
err = os.MkdirAll(p.netNSPath, os.ModeDir)
if err != nil {
zap.L().Warn("could not create directory", zap.Error(err))
}
}
// A symlink is created from /var/run/netns/<context> to the NetNSPath
contextFile := filepath.Join(p.netNSPath, contextID)
// Remove the context file if it already exists.
if removeErr := os.RemoveAll(contextFile); err != nil {
zap.L().Warn("Failed to remove namespace link",
zap.Error(removeErr))
}
if err = os.Symlink(nsPath, contextFile); err != nil {
zap.L().Warn("Failed to create symlink for use by ip netns", zap.Error(err))
}
cmd := p.getLaunchProcessCmd(p.remoteEnforcerTempBuildPath, p.remoteEnforcerBuildName, arg)
if err = p.pollStdOutAndErr(cmd, contextID); err != nil {
return false, err
}
randomkeystring, err := crypto.GenerateRandomString(secretLength)
if err != nil {
// This is a more serious failure. We can't reliably control the remote enforcer
return false, fmt.Errorf("unable to generate secret: %s", err)
}
// Start command
newEnvVars := p.getLaunchProcessEnvVars(
procMountPoint,
contextID,
randomkeystring,
statsServerSecret,
refPid,
refNSPath,
)
cmd.Env = append(os.Environ(), newEnvVars...)
if err = cmd.Start(); err != nil {
// Cleanup resources
if err1 := os.Remove(contextFile); err1 != nil {
zap.L().Warn("Failed to clean up netns path", zap.Error(err1))
}
return false, fmt.Errorf("unable to start enforcer binary: %s", err)
}
procInfo.process = cmd.Process
if err := rpchdl.NewRPCClient(contextID, contextID2SocketPath(contextID), randomkeystring); err != nil {
return false, fmt.Errorf("failed to established rpc channel: %s", err)
}
go func() {
status := cmd.Wait()
p.childExitStatus <- exitStatus{
process: cmd.Process.Pid,
contextID: contextID,
exitStatus: status,
}
}()
return true, nil
}