Panoptes 1.0.0
Endpoint Detection and Response
Loading...
Searching...
No Matches
event_queue.cpp
Go to the documentation of this file.
1#include "event_queue.h"
2
3#define PANO_EVENT_POOL_TAG 'vEoP' // "PoEv"
4
5// A buffered event. Data holds a PANO_EVENT_HEADER immediately followed by the
6// event body, i.e. exactly what is copied into a GET_EVENT output buffer.
7typedef struct _PANO_EVENT_NODE {
8 LIST_ENTRY ListEntry;
9 UINT32 Size; // bytes in Data
10 _Field_size_bytes_(Size) UCHAR Data[ANYSIZE_ARRAY];
12
13// Pending GET_EVENT requests. The CSQ serializes access with g_IrpLock and owns
14// cancellation; dispatch, producers and the I/O manager can touch it concurrently.
15static IO_CSQ g_Csq;
16static LIST_ENTRY g_IrpList;
17static KSPIN_LOCK g_IrpLock;
18
19// Buffered events, protected by its own lock so it can be manipulated without
20// nesting inside the CSQ lock.
21static LIST_ENTRY g_EventList;
22static KSPIN_LOCK g_EventLock;
23static ULONG g_EventCount;
24static volatile LONG g_Dropped; // events dropped since the last delivery
25
26// Handles that have sent a GET_EVENT request and not yet been cleaned up. Each
27// one is marked by setting its FileObject->FsContext, which is free for a
28// non-file-system device's own use, to PANO_CONSUMER_MARK
29static volatile LONG g_Consumers;
30static volatile LONG g_DroppedNoConsumer; // dropped since the last delivery, no consumer
31#define PANO_CONSUMER_MARK ((PVOID)(ULONG_PTR)0x50414E4F) // 'PANO'
32
33// The process of the handle that most recently became a consumer, or 0 when there is none.
34// Each consumer handle keeps its process ID in FileObject->FsContext2 (also free for our
35// use) so cleanup only clears this if it still names that handle's process
36static volatile LONG64 g_ConsumerProcessId;
37
38#pragma region Event FIFO
39
40// Removes and returns the oldest buffered event, or NULL if the FIFO is empty.
41static PPANO_EVENT_NODE DequeueEvent()
42{
43 KIRQL irql;
44 PPANO_EVENT_NODE node = NULL;
45
46 KeAcquireSpinLock(&g_EventLock, &irql);
47 if (!IsListEmpty(&g_EventList)) {
48 PLIST_ENTRY entry = RemoveHeadList(&g_EventList);
49 node = CONTAINING_RECORD(entry, PANO_EVENT_NODE, ListEntry);
50 g_EventCount--;
51 }
52 KeReleaseSpinLock(&g_EventLock, irql);
53 return node;
54}
55
56// Buffers an event at the tail of the FIFO. If the FIFO is full the event is
57// freed and counted as dropped; the oldest buffered events are kept so the
58// stream stays contiguous up to the drop point.
59static void EnqueueEvent(PPANO_EVENT_NODE node)
60{
61 KIRQL irql;
62 KeAcquireSpinLock(&g_EventLock, &irql);
63
64 // The last consumer may have left since the event was allocated. Checked under
65 // the lock: EventQueueCleanupFile drops the count before taking the lock to
66 // empty the FIFO, so an event is either emptied out with the rest or dropped here
67 if (g_Consumers == 0) {
68 KeReleaseSpinLock(&g_EventLock, irql);
69 ExFreePoolWithTag(node, PANO_EVENT_POOL_TAG);
70 InterlockedIncrement(&g_DroppedNoConsumer);
71 return;
72 }
73
74 if (g_EventCount >= PANO_MAX_QUEUED_EVENTS) {
75 KeReleaseSpinLock(&g_EventLock, irql);
76 ExFreePoolWithTag(node, PANO_EVENT_POOL_TAG);
77 InterlockedIncrement(&g_Dropped);
78 return;
79 }
80 InsertTailList(&g_EventList, &node->ListEntry);
81 g_EventCount++;
82 KeReleaseSpinLock(&g_EventLock, irql);
83}
84
85// Reinserts an event at the head of the FIFO, preserving order. Used only on the
86// rare race where a request was pulled out from under a just-buffered event.
87static void RequeueEventFront(PPANO_EVENT_NODE node)
88{
89 KIRQL irql;
90 KeAcquireSpinLock(&g_EventLock, &irql);
91 InsertHeadList(&g_EventList, &node->ListEntry);
92 g_EventCount++;
93 KeReleaseSpinLock(&g_EventLock, irql);
94}
95
96#pragma endregion
97
98#pragma region IRP delivery
99
100// Copies a buffered event into a GET_EVENT request and completes it. The request
101// was validated to hold at least PANO_MAX_EVENT_SIZE bytes, and node->Size never
102// exceeds that, so the whole event always fits. Frees the node.
103static void DeliverEventToIrp(PIRP Irp, PPANO_EVENT_NODE node)
104{
105 RtlCopyMemory(Irp->AssociatedIrp.SystemBuffer, node->Data, node->Size);
106
107 // Report (and reset) the drop counts accumulated since the previous delivery.
108 PPANO_EVENT_HEADER header = (PPANO_EVENT_HEADER)Irp->AssociatedIrp.SystemBuffer;
109 header->Dropped = (UINT32)InterlockedExchange(&g_Dropped, 0);
110 header->DroppedNoConsumer = (UINT32)InterlockedExchange(&g_DroppedNoConsumer, 0);
111
112 ULONG size = node->Size;
113 ExFreePoolWithTag(node, PANO_EVENT_POOL_TAG);
114
115 Irp->IoStatus.Status = STATUS_SUCCESS;
116 Irp->IoStatus.Information = size;
117 IoCompleteRequest(Irp, IO_NO_INCREMENT);
118}
119
120#pragma endregion
121
122#pragma region Cancel-safe queue callbacks
123
124static VOID CsqInsertIrp(PIO_CSQ Csq, PIRP Irp)
125{
126 UNREFERENCED_PARAMETER(Csq);
127 InsertTailList(&g_IrpList, &Irp->Tail.Overlay.ListEntry);
128}
129
130static VOID CsqRemoveIrp(PIO_CSQ Csq, PIRP Irp)
131{
132 UNREFERENCED_PARAMETER(Csq);
133 RemoveEntryList(&Irp->Tail.Overlay.ListEntry);
134}
135
136// PeekContext is NULL to take any request, or a PFILE_OBJECT to take only that
137// handle's requests (used when the handle is cleaned up)
138static PIRP CsqPeekNextIrp(PIO_CSQ Csq, PIRP Irp, PVOID PeekContext)
139{
140 UNREFERENCED_PARAMETER(Csq);
141
142 PLIST_ENTRY next = (Irp == NULL)
143 ? g_IrpList.Flink
144 : Irp->Tail.Overlay.ListEntry.Flink;
145
146 for (; next != &g_IrpList; next = next->Flink) {
147 PIRP candidate = CONTAINING_RECORD(next, IRP, Tail.Overlay.ListEntry);
148 if (PeekContext == NULL ||
149 IoGetCurrentIrpStackLocation(candidate)->FileObject == (PFILE_OBJECT)PeekContext) {
150 return candidate;
151 }
152 }
153 return NULL;
154}
155
156_IRQL_raises_(DISPATCH_LEVEL)
157_Acquires_lock_(g_IrpLock)
158static VOID CsqAcquireLock(PIO_CSQ Csq, PKIRQL Irql)
159{
160 UNREFERENCED_PARAMETER(Csq);
161 KeAcquireSpinLock(&g_IrpLock, Irql);
162}
163
165static VOID CsqReleaseLock(PIO_CSQ Csq, KIRQL Irql)
166{
167 UNREFERENCED_PARAMETER(Csq);
168 KeReleaseSpinLock(&g_IrpLock, Irql);
169}
170
171static VOID CsqCompleteCanceledIrp(PIO_CSQ Csq, PIRP Irp)
172{
173 UNREFERENCED_PARAMETER(Csq);
174 Irp->IoStatus.Status = STATUS_CANCELLED;
175 Irp->IoStatus.Information = 0;
176 IoCompleteRequest(Irp, IO_NO_INCREMENT);
177}
178
179#pragma endregion
180
182{
183 InitializeListHead(&g_IrpList);
184 InitializeListHead(&g_EventList);
185 KeInitializeSpinLock(&g_IrpLock);
186 KeInitializeSpinLock(&g_EventLock);
187 g_EventCount = 0;
188 g_Dropped = 0;
189 g_Consumers = 0;
190 g_DroppedNoConsumer = 0;
191
192 return IoCsqInitialize(
193 &g_Csq,
194 CsqInsertIrp,
195 CsqRemoveIrp,
196 CsqPeekNextIrp,
197 CsqAcquireLock,
198 CsqReleaseLock,
199 CsqCompleteCanceledIrp);
200}
201
203{
204 // Cancel every parked request.
205 PIRP irp;
206 while ((irp = IoCsqRemoveNextIrp(&g_Csq, NULL)) != NULL) {
207 irp->IoStatus.Status = STATUS_CANCELLED;
208 irp->IoStatus.Information = 0;
209 IoCompleteRequest(irp, IO_NO_INCREMENT);
210 }
211
212 // Drop every buffered event.
213 PPANO_EVENT_NODE node;
214 while ((node = DequeueEvent()) != NULL) {
215 ExFreePoolWithTag(node, PANO_EVENT_POOL_TAG);
216 }
217}
218
219BOOLEAN EventQueueReceiveRequest(PIRP Irp, NTSTATUS* Status)
220{
221 PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation(Irp);
222
223 // Every buffered event is <= PANO_MAX_EVENT_SIZE, so a request that can hold
224 // that much can hold any event and the copy at delivery never truncates.
225 if (irpSp->Parameters.DeviceIoControl.OutputBufferLength < PANO_MAX_EVENT_SIZE) {
226 *Status = STATUS_BUFFER_TOO_SMALL;
227 return FALSE;
228 }
229
230 // The handle's first request makes it a consumer, and events start being kept
231 if (InterlockedCompareExchangePointer(&irpSp->FileObject->FsContext, PANO_CONSUMER_MARK, NULL) == NULL) {
232 // The device has no drivers above it, so the request is dispatched in the context of
233 // the thread that sent it
234 LONG64 processId = (LONG64)(ULONG_PTR)PsGetCurrentProcessId();
235 irpSp->FileObject->FsContext2 = (PVOID)(ULONG_PTR)processId;
236 InterlockedExchange64(&g_ConsumerProcessId, processId);
237 InterlockedIncrement(&g_Consumers);
238 }
239
240 // Fast path: an event is already waiting.
241 PPANO_EVENT_NODE node = DequeueEvent();
242 if (node != NULL) {
243 DeliverEventToIrp(Irp, node);
244 *Status = STATUS_SUCCESS;
245 return TRUE;
246 }
247
248 // Park the request. IoCsqInsertIrp marks it pending for us.
249 IoCsqInsertIrp(&g_Csq, Irp, NULL);
250
251 // An event may have been buffered after our DequeueEvent and before the
252 // insert, while no request was parked to receive it. Drain it now so the
253 // request does not stall behind an event that already arrived.
254 node = DequeueEvent();
255 if (node != NULL) {
256 PIRP waiter = IoCsqRemoveNextIrp(&g_Csq, NULL);
257 if (waiter != NULL) {
258 DeliverEventToIrp(waiter, node);
259 }
260 else {
261 // Our request was already taken (by a producer); put the event back.
262 RequeueEventFront(node);
263 }
264 }
265
266 *Status = STATUS_PENDING;
267 return TRUE;
268}
269
270BOOLEAN EventQueueIsConsumerProcess(UINT64 ProcessId)
271{
272 // An aligned 64-bit read is atomic on x64
273 LONG64 consumer = g_ConsumerProcessId;
274 return consumer != 0 && (UINT64)consumer == ProcessId;
275}
276
277void EventQueueCleanupFile(PFILE_OBJECT FileObject)
278{
279 // Complete the requests this handle still has parked. A process that is exiting
280 // has had them cancelled already, but one that just closes its handle has not
281 PIRP irp;
282 while ((irp = IoCsqRemoveNextIrp(&g_Csq, FileObject)) != NULL) {
283 irp->IoStatus.Status = STATUS_CANCELLED;
284 irp->IoStatus.Information = 0;
285 IoCompleteRequest(irp, IO_NO_INCREMENT);
286 }
287
288 // Called for both IRP_MJ_CLEANUP and IRP_MJ_CLOSE; only the first unmarks the
289 // handle. IRP_MJ_CLOSE covers a GET_EVENT request that was still being
290 // dispatched when IRP_MJ_CLEANUP arrived and marked the handle afterwards
291 if (InterlockedCompareExchangePointer(&FileObject->FsContext, NULL, PANO_CONSUMER_MARK) != PANO_CONSUMER_MARK) {
292 return;
293 }
294
295 // Stop exempting this handle's process, unless a newer consumer has replaced it
296 InterlockedCompareExchange64(&g_ConsumerProcessId, 0, (LONG64)(ULONG_PTR)FileObject->FsContext2);
297 FileObject->FsContext2 = NULL;
298
299 if (InterlockedDecrement(&g_Consumers) != 0) {
300 return;
301 }
302
303 // No consumer is left. What is buffered would only reach the next service as
304 // stale events, so it is dropped and counted
305 KIRQL irql;
306 LIST_ENTRY dropped;
307 InitializeListHead(&dropped);
308 LONG count = 0;
309
310 KeAcquireSpinLock(&g_EventLock, &irql);
311 while (!IsListEmpty(&g_EventList)) {
312 InsertTailList(&dropped, RemoveHeadList(&g_EventList));
313 count++;
314 }
315 g_EventCount = 0;
316 KeReleaseSpinLock(&g_EventLock, irql);
317
318 while (!IsListEmpty(&dropped)) {
319 PPANO_EVENT_NODE node = CONTAINING_RECORD(RemoveHeadList(&dropped), PANO_EVENT_NODE, ListEntry);
320 ExFreePoolWithTag(node, PANO_EVENT_POOL_TAG);
321 }
322 InterlockedAdd(&g_DroppedNoConsumer, count);
323}
324
325// Allocates an event node large enough for PayloadSize bytes after the header,
326// fills in the header, and returns a pointer to the payload region in *Payload.
327// Returns NULL (and counts a drop) if no consumer is connected, the event is too
328// large, or allocation fails.
329static PPANO_EVENT_NODE AllocEventNode(PANO_EVENT_TYPE Type, UINT32 PayloadSize, void** Payload)
330{
331 // Nothing would read the event, so skip the allocation. EnqueueEvent checks
332 // again under the lock for a consumer that leaves after this point
333 if (g_Consumers == 0) {
334 InterlockedIncrement(&g_DroppedNoConsumer);
335 return NULL;
336 }
337
338 UINT32 size = (UINT32)sizeof(PANO_EVENT_HEADER) + PayloadSize;
339 if (size > PANO_MAX_EVENT_SIZE) {
340 InterlockedIncrement(&g_Dropped);
341 return NULL;
342 }
343
344 PPANO_EVENT_NODE node = (PPANO_EVENT_NODE)ExAllocatePool2(
345 POOL_FLAG_NON_PAGED,
346 FIELD_OFFSET(PANO_EVENT_NODE, Data) + size,
348 if (node == NULL) {
349 InterlockedIncrement(&g_Dropped);
350 return NULL;
351 }
352
353 node->Size = size;
354
355 PPANO_EVENT_HEADER header = (PPANO_EVENT_HEADER)node->Data;
356 header->Size = size;
357 header->Type = Type;
358 LARGE_INTEGER now;
359 KeQuerySystemTimePrecise(&now);
360 header->Timestamp = now.QuadPart;
361 header->Dropped = 0; // both filled in at delivery
362 header->DroppedNoConsumer = 0;
363
364 *Payload = node->Data + sizeof(PANO_EVENT_HEADER);
365 return node;
366}
367
368// Delivers a fully built event node to a pending request, or buffers it.
369static void SubmitEventNode(PPANO_EVENT_NODE node)
370{
371 // Hand straight to a waiting request if there is one.
372 PIRP irp = IoCsqRemoveNextIrp(&g_Csq, NULL);
373 if (irp != NULL) {
374 DeliverEventToIrp(irp, node);
375 return;
376 }
377
378 // No request waiting: buffer it.
379 EnqueueEvent(node);
380
381 // A request may have been parked after our RemoveNextIrp returned NULL and
382 // before we buffered the event, leaving a request waiting on a non-empty
383 // FIFO. Pair them up.
384 irp = IoCsqRemoveNextIrp(&g_Csq, NULL);
385 if (irp != NULL) {
386 PPANO_EVENT_NODE queued = DequeueEvent();
387 if (queued != NULL) {
388 DeliverEventToIrp(irp, queued);
389 }
390 else {
391 // The event we just buffered was already delivered by a concurrent
392 // request; nothing to hand this one, so put it back to wait.
393 IoCsqInsertIrp(&g_Csq, irp, NULL);
394 }
395 }
396}
397
398NTSTATUS PanoReportEvent(PANO_EVENT_TYPE Type, const void* Payload, UINT32 PayloadSize)
399{
400 void* dest;
401 PPANO_EVENT_NODE node = AllocEventNode(Type, PayloadSize, &dest);
402 if (node == NULL) {
403 return STATUS_INSUFFICIENT_RESOURCES;
404 }
405
406 if (PayloadSize != 0) {
407 RtlCopyMemory(dest, Payload, PayloadSize);
408 }
409
410 SubmitEventNode(node);
411 return STATUS_SUCCESS;
412}
413
415 PANO_EVENT_TYPE Type,
416 UINT64 ProcessId,
417 UINT64 ThreadId,
418 INT64 FileOffset,
419 UINT32 Length,
420 UINT32 Flags,
421 const WCHAR* FileName,
422 UINT32 NameLength)
423{
424 if (FileName == NULL) {
425 NameLength = 0;
426 }
427 if (NameLength > PANO_MAX_FILE_NAME_LENGTH) {
428 NameLength = (UINT32)PANO_MAX_FILE_NAME_LENGTH;
430 }
431
432 UINT32 payloadSize = (UINT32)FIELD_OFFSET(PANO_FILE_EVENT_DATA, FileName) + NameLength * (UINT32)sizeof(WCHAR);
433
434 // Assemble straight into the pooled node to avoid a large kernel-stack copy.
435 void* dest;
436 PPANO_EVENT_NODE node = AllocEventNode(Type, payloadSize, &dest);
437 if (node == NULL) {
438 return STATUS_INSUFFICIENT_RESOURCES;
439 }
440
442 data->ProcessId = ProcessId;
443 data->ThreadId = ThreadId;
444 data->FileOffset = FileOffset;
445 data->Length = Length;
446 data->Flags = Flags;
447 data->NameLength = NameLength;
448 if (NameLength != 0) {
449 RtlCopyMemory(data->FileName, FileName, NameLength * sizeof(WCHAR));
450 }
451
452 SubmitEventNode(node);
453 return STATUS_SUCCESS;
454}
455
457 UINT64 ProcessId,
458 UINT64 ThreadId,
459 UINT64 Writes,
460 UINT64 Bytes,
461 INT64 FirstWriteTime,
462 UINT32 Flags,
463 const WCHAR* FileName,
464 UINT32 NameLength)
465{
466 if (FileName == NULL) {
467 NameLength = 0;
468 }
469 if (NameLength > PANO_MAX_MODIFIED_FILE_NAME_LENGTH) {
470 NameLength = (UINT32)PANO_MAX_MODIFIED_FILE_NAME_LENGTH;
472 }
473
474 UINT32 payloadSize = (UINT32)FIELD_OFFSET(PANO_FILE_MODIFIED_EVENT_DATA, FileName) + NameLength * (UINT32)sizeof(WCHAR);
475
476 void* dest;
477 PPANO_EVENT_NODE node = AllocEventNode(PANO_EVENT_FILE_MODIFIED, payloadSize, &dest);
478 if (node == NULL) {
479 return STATUS_INSUFFICIENT_RESOURCES;
480 }
481
483 data->ProcessId = ProcessId;
484 data->ThreadId = ThreadId;
485 data->Writes = Writes;
486 data->Bytes = Bytes;
487 data->FirstWriteTime = FirstWriteTime;
488 data->Flags = Flags;
489 data->NameLength = NameLength;
490 if (NameLength != 0) {
491 RtlCopyMemory(data->FileName, FileName, NameLength * sizeof(WCHAR));
492 }
493
494 SubmitEventNode(node);
495 return STATUS_SUCCESS;
496}
497
498// Shrinks two string lengths so their sum fits in MaxTotal WCHARs. The longer string is
499// trimmed first, so a short string is never cut to make room for a long one.
500static void FitTwoStrings(UINT32* FirstLength, UINT32* SecondLength, UINT32 MaxTotal, BOOLEAN* FirstCut, BOOLEAN* SecondCut)
501{
502 const UINT32 first = *FirstLength;
503 const UINT32 second = *SecondLength;
504 *FirstCut = FALSE;
505 *SecondCut = FALSE;
506
507 // Compare in 64 bits so huge lengths cannot wrap the sum
508 if ((UINT64)first + second <= MaxTotal) {
509 return;
510 }
511
512 const UINT32 half = MaxTotal / 2;
513 if (first > half && second > half) {
514 *FirstLength = half;
515 *SecondLength = MaxTotal - half;
516 }
517 else if (first > second) {
518 *FirstLength = MaxTotal - second;
519 }
520 else {
521 *SecondLength = MaxTotal - first;
522 }
523 *FirstCut = (*FirstLength != first);
524 *SecondCut = (*SecondLength != second);
525}
526
528 UINT64 ProcessId,
529 UINT64 ThreadId,
530 UINT32 Flags,
531 const WCHAR* SourceName,
532 UINT32 SourceNameLength,
533 const WCHAR* DestinationName,
534 UINT32 DestinationNameLength)
535{
536 if (SourceName == NULL) {
537 SourceNameLength = 0;
538 }
539 if (DestinationName == NULL) {
540 DestinationNameLength = 0;
541 }
542
543 BOOLEAN sourceCut, destinationCut;
544 FitTwoStrings(&SourceNameLength, &DestinationNameLength, (UINT32)PANO_MAX_RENAME_NAMES_LENGTH, &sourceCut, &destinationCut);
545 if (sourceCut || destinationCut) {
547 }
548
549 UINT32 payloadSize = (UINT32)FIELD_OFFSET(PANO_RENAME_EVENT_DATA, Names) +
550 (SourceNameLength + DestinationNameLength) * (UINT32)sizeof(WCHAR);
551
552 void* dest;
553 PPANO_EVENT_NODE node = AllocEventNode(PANO_EVENT_FILE_RENAME, payloadSize, &dest);
554 if (node == NULL) {
555 return STATUS_INSUFFICIENT_RESOURCES;
556 }
557
559 data->ProcessId = ProcessId;
560 data->ThreadId = ThreadId;
561 data->Flags = Flags;
562 data->SourceNameLength = SourceNameLength;
563 data->DestinationNameLength = DestinationNameLength;
564 if (SourceNameLength != 0) {
565 RtlCopyMemory(data->Names, SourceName, SourceNameLength * sizeof(WCHAR));
566 }
567 if (DestinationNameLength != 0) {
568 RtlCopyMemory(data->Names + SourceNameLength, DestinationName, DestinationNameLength * sizeof(WCHAR));
569 }
570
571 SubmitEventNode(node);
572 return STATUS_SUCCESS;
573}
574
576 PANO_EVENT_TYPE Type,
577 UINT64 ProcessId,
578 UINT64 ParentProcessId,
579 UINT64 CreatingProcessId,
580 UINT64 CreatingThreadId,
581 const WCHAR* ImageName,
582 UINT32 ImageNameLength,
583 const WCHAR* CommandLine,
584 UINT32 CommandLineLength)
585{
586 UINT32 flags = 0;
587 if (ImageName == NULL) {
588 ImageNameLength = 0;
589 }
590 if (CommandLine == NULL) {
591 CommandLineLength = 0;
592 }
593
594 BOOLEAN imageCut, commandLineCut;
595 FitTwoStrings(&ImageNameLength, &CommandLineLength, (UINT32)PANO_MAX_PROCESS_STRINGS_LENGTH, &imageCut, &commandLineCut);
596 if (imageCut) {
598 }
599 if (commandLineCut) {
601 }
602
603 UINT32 payloadSize = (UINT32)FIELD_OFFSET(PANO_PROCESS_EVENT_DATA, Strings) +
604 (ImageNameLength + CommandLineLength) * (UINT32)sizeof(WCHAR);
605
606 void* dest;
607 PPANO_EVENT_NODE node = AllocEventNode(Type, payloadSize, &dest);
608 if (node == NULL) {
609 return STATUS_INSUFFICIENT_RESOURCES;
610 }
611
613 data->ProcessId = ProcessId;
614 data->ParentProcessId = ParentProcessId;
615 data->CreatingProcessId = CreatingProcessId;
616 data->CreatingThreadId = CreatingThreadId;
617 data->Flags = flags;
618 data->ImageNameLength = ImageNameLength;
619 data->CommandLineLength = CommandLineLength;
620 if (ImageNameLength != 0) {
621 RtlCopyMemory(data->Strings, ImageName, ImageNameLength * sizeof(WCHAR));
622 }
623 if (CommandLineLength != 0) {
624 RtlCopyMemory(data->Strings + ImageNameLength, CommandLine, CommandLineLength * sizeof(WCHAR));
625 }
626
627 SubmitEventNode(node);
628 return STATUS_SUCCESS;
629}
630
632 PANO_EVENT_TYPE Type,
633 UINT64 ProcessId,
634 UINT64 ThreadId,
635 UINT32 Flags,
636 UINT32 Detail,
637 UINT32 DataSize,
638 const WCHAR* KeyName,
639 UINT32 KeyNameLength,
640 const WCHAR* SecondName,
641 UINT32 SecondNameLength,
642 const void* Data,
643 UINT32 DataLength)
644{
645 if (KeyName == NULL) {
646 KeyNameLength = 0;
647 }
648 if (SecondName == NULL) {
649 SecondNameLength = 0;
650 }
651 if (Data == NULL) {
652 DataLength = 0;
653 }
654 if (DataLength > PANO_MAX_REGISTRY_DATA_SIZE) {
655 DataLength = PANO_MAX_REGISTRY_DATA_SIZE;
657 }
658
659 BOOLEAN keyCut, secondCut;
660 FitTwoStrings(&KeyNameLength, &SecondNameLength, (UINT32)PANO_MAX_REGISTRY_NAMES_LENGTH, &keyCut, &secondCut);
661 if (keyCut || secondCut) {
663 }
664
665 UINT32 payloadSize = (UINT32)FIELD_OFFSET(PANO_REGISTRY_EVENT_DATA, Strings) +
666 (KeyNameLength + SecondNameLength) * (UINT32)sizeof(WCHAR) + DataLength;
667
668 void* dest;
669 PPANO_EVENT_NODE node = AllocEventNode(Type, payloadSize, &dest);
670 if (node == NULL) {
671 return STATUS_INSUFFICIENT_RESOURCES;
672 }
673
675 data->ProcessId = ProcessId;
676 data->ThreadId = ThreadId;
677 data->Flags = Flags;
678 data->Detail = Detail;
679 data->DataSize = DataSize;
680 data->KeyNameLength = KeyNameLength;
681 data->SecondNameLength = SecondNameLength;
682 data->DataLength = DataLength;
683 if (KeyNameLength != 0) {
684 RtlCopyMemory(data->Strings, KeyName, KeyNameLength * sizeof(WCHAR));
685 }
686 if (SecondNameLength != 0) {
687 RtlCopyMemory(data->Strings + KeyNameLength, SecondName, SecondNameLength * sizeof(WCHAR));
688 }
689 if (DataLength != 0) {
690 RtlCopyMemory(data->Strings + KeyNameLength + SecondNameLength, Data, DataLength);
691 }
692
693 SubmitEventNode(node);
694 return STATUS_SUCCESS;
695}
NTSTATUS EventQueueInit()
Initializes the event queue. Call once from DriverEntry before any producer can run and before the de...
NTSTATUS PanoReportFileEvent(PANO_EVENT_TYPE Type, UINT64 ProcessId, UINT64 ThreadId, INT64 FileOffset, UINT32 Length, UINT32 Flags, const WCHAR *FileName, UINT32 NameLength)
NTSTATUS PanoReportFileModifiedEvent(UINT64 ProcessId, UINT64 ThreadId, UINT64 Writes, UINT64 Bytes, INT64 FirstWriteTime, UINT32 Flags, const WCHAR *FileName, UINT32 NameLength)
void EventQueueCleanup()
Cancels every pending GET_EVENT request and frees all buffered events. Call from the unload path afte...
BOOLEAN EventQueueIsConsumerProcess(UINT64 ProcessId)
Checks whether a process is the one reading events (the service). The filter does not report that pro...
_Releases_lock_(g_IrpLock) static VOID CsqReleaseLock(PIO_CSQ Csq
NTSTATUS PanoReportEvent(PANO_EVENT_TYPE Type, const void *Payload, UINT32 PayloadSize)
void EventQueueCleanupFile(PFILE_OBJECT FileObject)
Handles IRP_MJ_CLEANUP for a handle to the device: cancels the GET_EVENT requests still parked for it...
_IRQL_raises_(DISPATCH_LEVEL) _Acquires_lock_(g_IrpLock) static VOID CsqAcquireLock(PIO_CSQ Csq
NTSTATUS PanoReportProcessEvent(PANO_EVENT_TYPE Type, UINT64 ProcessId, UINT64 ParentProcessId, UINT64 CreatingProcessId, UINT64 CreatingThreadId, const WCHAR *ImageName, UINT32 ImageNameLength, const WCHAR *CommandLine, UINT32 CommandLineLength)
NTSTATUS PanoReportRegistryEvent(PANO_EVENT_TYPE Type, UINT64 ProcessId, UINT64 ThreadId, UINT32 Flags, UINT32 Detail, UINT32 DataSize, const WCHAR *KeyName, UINT32 KeyNameLength, const WCHAR *SecondName, UINT32 SecondNameLength, const void *Data, UINT32 DataLength)
struct _PANO_EVENT_NODE * PPANO_EVENT_NODE
BOOLEAN EventQueueReceiveRequest(PIRP Irp, NTSTATUS *Status)
Queues a GET_EVENT request. Call from the IOCTL dispatch routine. On success the IRP is either comple...
#define PANO_EVENT_POOL_TAG
struct _PANO_EVENT_NODE PANO_EVENT_NODE
PKIRQL Irql
NTSTATUS PanoReportRenameEvent(UINT64 ProcessId, UINT64 ThreadId, UINT32 Flags, const WCHAR *SourceName, UINT32 SourceNameLength, const WCHAR *DestinationName, UINT32 DestinationNameLength)
#define PANO_CONSUMER_MARK
#define PANO_MAX_QUEUED_EVENTS
Maximum number of events buffered while no GET_EVENT request is pending. Older buffered events are no...
Definition event_queue.h:25
struct _PANO_RENAME_EVENT_DATA * PPANO_RENAME_EVENT_DATA
struct _PANO_RENAME_EVENT_DATA PANO_RENAME_EVENT_DATA
The data for PANO_EVENT_FILE_RENAME.
#define PANO_MAX_MODIFIED_FILE_NAME_LENGTH
The maximum number of WCHARs in PANO_FILE_MODIFIED_EVENT_DATA::FileName so that the whole response fi...
struct _PANO_EVENT_HEADER PANO_EVENT_HEADER
The header at the start of every IOCTL_PANOPTES_GET_EVENT response.
PANO_EVENT_TYPE
The type of event, which determines the data that follows the header.
Definition pano_events.h:65
@ PANO_EVENT_FILE_RENAME
A file was renamed or moved (a PANO_RENAME_EVENT_DATA).
Definition pano_events.h:85
@ PANO_EVENT_FILE_MODIFIED
A handle that wrote to a file was closed (a PANO_FILE_MODIFIED_EVENT_DATA). One event covers every wr...
Definition pano_events.h:94
struct _PANO_FILE_EVENT_DATA * PPANO_FILE_EVENT_DATA
struct _PANO_REGISTRY_EVENT_DATA PANO_REGISTRY_EVENT_DATA
The data for PANO_EVENT_REGISTRY_* events.
#define PANO_FLAG_COMMAND_LINE_TRUNCATED
PANO_EVENT_PROCESS_CREATED: the command line was truncated to fit the event.
struct _PANO_REGISTRY_EVENT_DATA * PPANO_REGISTRY_EVENT_DATA
#define PANO_FLAG_NAME_TRUNCATED
struct _PANO_PROCESS_EVENT_DATA * PPANO_PROCESS_EVENT_DATA
struct _PANO_FILE_MODIFIED_EVENT_DATA * PPANO_FILE_MODIFIED_EVENT_DATA
#define PANO_MAX_EVENT_SIZE
The output buffer size the service passes with IOCTL_PANOPTES_GET_EVENT. The driver never returns an ...
Definition pano_events.h:59
struct _PANO_FILE_EVENT_DATA PANO_FILE_EVENT_DATA
The data for file, named pipe and mailslot events.
struct _PANO_PROCESS_EVENT_DATA PANO_PROCESS_EVENT_DATA
The data for PANO_EVENT_PROCESS_CREATED and PANO_EVENT_PROCESS_TERMINATED.
#define PANO_MAX_RENAME_NAMES_LENGTH
The maximum combined number of WCHARs in both PANO_RENAME_EVENT_DATA names so that the whole response...
struct _PANO_FILE_MODIFIED_EVENT_DATA PANO_FILE_MODIFIED_EVENT_DATA
The data for PANO_EVENT_FILE_MODIFIED.
#define PANO_MAX_REGISTRY_DATA_SIZE
The most bytes of a value's data a PANO_EVENT_REGISTRY_SET_VALUE carries. Enough for the paths and co...
#define PANO_MAX_FILE_NAME_LENGTH
The maximum number of WCHARs in PANO_FILE_EVENT_DATA::FileName so that the whole response fits in PAN...
struct _PANO_EVENT_HEADER * PPANO_EVENT_HEADER
#define PANO_FLAG_DATA_TRUNCATED
PANO_EVENT_REGISTRY_SET_VALUE / _REPLACE_KEY: Data holds only the first PANO_MAX_REGISTRY_DATA_SIZE b...
#define PANO_MAX_PROCESS_STRINGS_LENGTH
The maximum combined number of WCHARs in the PANO_PROCESS_EVENT_DATA image name and command line so t...
#define PANO_MAX_REGISTRY_NAMES_LENGTH
The maximum combined number of WCHARs in both PANO_REGISTRY_EVENT_DATA names so that the whole respon...
LIST_ENTRY ListEntry
_Field_size_bytes_(Size) UCHAR Data[ANYSIZE_ARRAY]
UINT32 Length
Length of the write, PANO_EVENT_FILE_WRITE only.
UINT32 NameLength
Length of FileName in WCHARs. FileName is not null terminated.
UINT32 Flags
PANO_FLAG_* values.
INT64 FileOffset
Offset of the write, PANO_EVENT_FILE_WRITE only.
UINT64 ProcessId
The process and thread of the most recent write.
UINT32 NameLength
Length of FileName in WCHARs. FileName is not null terminated. It is the file's name when the event w...
INT64 FirstWriteTime
When the first of those writes happened, in FILETIME units (UTC).
UINT32 Flags
PANO_FLAG_* values.
UINT64 Bytes
Bytes written by those writes.
UINT64 Writes
Number of writes through the handle covered by this event.
UINT32 Flags
PANO_FLAG_* values. PANO_FLAG_NAME_TRUNCATED refers to the image name and PANO_FLAG_COMMAND_LINE_TRUN...
WCHAR Strings[1]
The image name immediately followed by the command line. Neither is null terminated.
UINT32 CommandLineLength
Length of the command line in WCHARs. PANO_EVENT_PROCESS_CREATED only.
UINT32 ImageNameLength
Length of the image name in WCHARs. PANO_EVENT_PROCESS_CREATED only.
UINT64 ParentProcessId
PANO_EVENT_PROCESS_CREATED only.
UINT64 CreatingProcessId
The process that created the new process, which can differ from the parent. PANO_EVENT_PROCESS_CREATE...
UINT64 CreatingThreadId
The thread that created the new process. PANO_EVENT_PROCESS_CREATED only.
WCHAR Strings[1]
The key name, then the second name (neither null terminated), then DataLength bytes of data....
UINT32 KeyNameLength
Length of the key's full name (\REGISTRY\MACHINE...) in WCHARs.
UINT32 Detail
Per event type: the REG_* value type (SET_VALUE), the REG_*_FORMAT (SAVE_KEY), the SECURITY_INFORMATI...
UINT32 DataSize
The full size of Data in bytes before it was cut to fit: the value's data (SET_VALUE) or the old hive...
UINT32 Flags
PANO_FLAG_* values.
UINT32 SecondNameLength
Length of the second name in WCHARs: the value name (SET_VALUE, QUERY_VALUE), the file (SAVE_KEY,...
UINT32 DataLength
Bytes in Data, at most PANO_MAX_REGISTRY_DATA_SIZE. SET_VALUE and REPLACE_KEY only.
WCHAR Names[1]
The original name immediately followed by the new name. Neither is null terminated.
UINT32 Flags
PANO_FLAG_* values.
UINT32 SourceNameLength
Length of the original name in WCHARs.
UINT32 DestinationNameLength
Length of the new name in WCHARs.