-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
procmon.go
213 lines (183 loc) · 5.77 KB
/
procmon.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build windows
//nolint:revive // TODO(WKIT) Fix revive linter
package procmon
import (
"fmt"
"unsafe"
"github.com/DataDog/datadog-agent/pkg/util/winutil"
"github.com/DataDog/datadog-agent/pkg/windowsdriver/driver"
"github.com/DataDog/datadog-agent/pkg/windowsdriver/olreader"
)
//nolint:revive // TODO(WKIT) Fix revive linter
type ProcessStartNotification struct {
Pid uint64
PPid uint64
CreatingProcessId uint64
CreatingThreadId uint64
OwnerSidString string
ImageFile string
CmdLine string
// if this is nonzero, functions as notification to
// the probe that the buffer size isn't large enough
RequiredSize uint32
}
//nolint:revive // TODO(WKIT) Fix revive linter
type ProcessStopNotification struct {
Pid uint64
}
//nolint:revive // TODO(WKIT) Fix revive linter
type WinProcmon struct {
onStart chan *ProcessStartNotification
onStop chan *ProcessStopNotification
onError chan bool
reader *olreader.OverlappedReader
}
const (
// deviceName identifies the name and location of the windows driver
deviceName = `\\.\ddprocmon`
// driverName is the name of the driver service
driverName = "ddprocmon"
// ProcmonDefaultReceiveSize is the default size of the receive buffer
// 140k is the maximum size a notification can be, resulting in more memory usage traded off for
// not missing notifications.
ProcmonDefaultReceiveSize = (140 * 1024)
// ProcmonDefaultNumBufs is the default number of overlapped receive buffers
ProcmonDefaultNumBufs = 50
)
var (
// signature
procmonSignature = ProcmonSignature
)
// NewWinProcMon creates a new WinProcmon
//
// requires 3 channels for notification of data (one for start notifications, stop notifications, and error notifications)
//
// the bufsize and numbufs params, respectively, can be used to override the defaults for those parameters
// (if 0 is provided then defaults are used)
// Allows caller to configure the number & size of the overlapped buffers used for receiving notifications from the driver
func NewWinProcMon(onStart chan *ProcessStartNotification, onStop chan *ProcessStopNotification, onError chan bool, bufsize, numbufs int) (*WinProcmon, error) {
if bufsize == 0 {
return nil, fmt.Errorf("invalid buffer size")
}
if numbufs == 0 {
return nil, fmt.Errorf("invalid number of buffers")
}
wp := &WinProcmon{
onStart: onStart,
onStop: onStop,
onError: onError,
}
if err := driver.StartDriverService(driverName); err != nil {
return nil, err
}
reader, err := olreader.NewOverlappedReader(wp, bufsize, numbufs)
if err != nil {
return nil, err
}
err = reader.Open(deviceName)
if err != nil {
return nil, err
}
wp.reader = reader
return wp, nil
}
//nolint:revive // TODO(WKIT) Fix revive linter
func (wp *WinProcmon) OnData(data []uint8) {
var consumed uint32
returnedsize := uint32(len(data))
start, stop := decodeStruct(data[consumed:], returnedsize-consumed)
if start != nil {
wp.onStart <- start
} else if stop != nil {
wp.onStop <- stop
}
}
//nolint:revive // TODO(WKIT) Fix revive linter
func (wp *WinProcmon) OnError(err error) {
// if we get this error notification, then the driver can't continue.
// stop the notifications so that the driver can't get backed up
wp.Stop()
}
//nolint:revive // TODO(WKIT) Fix revive linter
func (wp *WinProcmon) Stop() {
// since we're stopping, if for some reason this ioctl fails, there's nothing we can
// do, we're on our way out. Closing the handle will ultimately cause the same cleanup
// to happen.
_ = wp.reader.Ioctl(ProcmonStopIOCTL,
(*byte)(unsafe.Pointer(&procmonSignature)), // inBuffer
uint32(unsafe.Sizeof(procmonSignature)),
nil,
0,
nil,
nil)
wp.reader.Stop()
_ = driver.StopDriverService(driverName, false)
}
//nolint:revive // TODO(WKIT) Fix revive linter
func (wp *WinProcmon) Start() error {
err := wp.reader.Read()
if err != nil {
return err
}
// this will initiate the driver actually sending things up
// start grabbing notifications
err = wp.reader.Ioctl(ProcmonStartIOCTL,
(*byte)(unsafe.Pointer(&procmonSignature)), // inBuffer
uint32(unsafe.Sizeof(procmonSignature)),
nil,
0,
nil,
nil)
if err != nil {
wp.reader.Stop()
}
return err
}
//nolint:revive // TODO(WKIT) Fix revive linter
func decodeStruct(data []uint8, sz uint32) (start *ProcessStartNotification, stop *ProcessStopNotification) {
if unsafe.Sizeof(DDProcessNotification{}.Size) > uintptr(sz) {
return nil, nil
}
n := *(*DDProcessNotification)(unsafe.Pointer(&data[0]))
if n.Size > uint64(sz) {
return nil, nil
}
t := DDProcessNotifyType(n.NotifyType)
if t == ProcmonNotifyStart {
var imagefile string
var cmdline string
var sidstring string
if n.ImageFileLen > 0 {
imagefile = winutil.ConvertWindowsString(data[n.ImageFileOffset : n.ImageFileOffset+n.ImageFileLen])
}
if n.CommandLineLen > 0 {
cmdline = winutil.ConvertWindowsString(data[n.CommandLineOffset : n.CommandLineOffset+n.CommandLineLen])
}
if n.SidLen > 0 {
sidstring = winutil.ConvertWindowsString(data[n.SidOffset : n.SidOffset+n.SidLen])
}
start = &ProcessStartNotification{
Pid: n.ProcessId,
PPid: n.ParentProcessId,
CreatingProcessId: n.CreatingProcessId,
CreatingThreadId: n.CreatingThreadId,
ImageFile: imagefile,
CmdLine: cmdline,
OwnerSidString: sidstring,
}
if n.SizeNeeded > n.Size {
start.RequiredSize = uint32(n.SizeNeeded)
}
return start, nil
} else if t == ProcmonNotifyStop {
stop = &ProcessStopNotification{
Pid: n.ProcessId,
}
return nil, stop
}
return nil, nil
}