3#define PANO_EVENT_POOL_TAG 'vEoP'
16static LIST_ENTRY g_IrpList;
17static KSPIN_LOCK g_IrpLock;
21static LIST_ENTRY g_EventList;
22static KSPIN_LOCK g_EventLock;
23static ULONG g_EventCount;
24static volatile LONG g_Dropped;
29static volatile LONG g_Consumers;
30static volatile LONG g_DroppedNoConsumer;
31#define PANO_CONSUMER_MARK ((PVOID)(ULONG_PTR)0x50414E4F)
36static volatile LONG64 g_ConsumerProcessId;
38#pragma region Event FIFO
46 KeAcquireSpinLock(&g_EventLock, &irql);
47 if (!IsListEmpty(&g_EventList)) {
48 PLIST_ENTRY entry = RemoveHeadList(&g_EventList);
52 KeReleaseSpinLock(&g_EventLock, irql);
62 KeAcquireSpinLock(&g_EventLock, &irql);
67 if (g_Consumers == 0) {
68 KeReleaseSpinLock(&g_EventLock, irql);
70 InterlockedIncrement(&g_DroppedNoConsumer);
75 KeReleaseSpinLock(&g_EventLock, irql);
77 InterlockedIncrement(&g_Dropped);
80 InsertTailList(&g_EventList, &node->
ListEntry);
82 KeReleaseSpinLock(&g_EventLock, irql);
90 KeAcquireSpinLock(&g_EventLock, &irql);
91 InsertHeadList(&g_EventList, &node->
ListEntry);
93 KeReleaseSpinLock(&g_EventLock, irql);
98#pragma region IRP delivery
105 RtlCopyMemory(Irp->AssociatedIrp.SystemBuffer, node->Data, node->
Size);
109 header->Dropped = (UINT32)InterlockedExchange(&g_Dropped, 0);
110 header->DroppedNoConsumer = (UINT32)InterlockedExchange(&g_DroppedNoConsumer, 0);
112 ULONG size = node->
Size;
115 Irp->IoStatus.Status = STATUS_SUCCESS;
116 Irp->IoStatus.Information = size;
117 IoCompleteRequest(Irp, IO_NO_INCREMENT);
122#pragma region Cancel-safe queue callbacks
124static VOID CsqInsertIrp(PIO_CSQ Csq, PIRP Irp)
126 UNREFERENCED_PARAMETER(Csq);
127 InsertTailList(&g_IrpList, &Irp->Tail.Overlay.ListEntry);
130static VOID CsqRemoveIrp(PIO_CSQ Csq, PIRP Irp)
132 UNREFERENCED_PARAMETER(Csq);
133 RemoveEntryList(&Irp->Tail.Overlay.ListEntry);
138static PIRP CsqPeekNextIrp(PIO_CSQ Csq, PIRP Irp, PVOID PeekContext)
140 UNREFERENCED_PARAMETER(Csq);
142 PLIST_ENTRY next = (Irp == NULL)
144 : Irp->Tail.Overlay.ListEntry.Flink;
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) {
157_Acquires_lock_(g_IrpLock)
158static VOID CsqAcquireLock(PIO_CSQ Csq, PKIRQL
Irql)
160 UNREFERENCED_PARAMETER(Csq);
161 KeAcquireSpinLock(&g_IrpLock,
Irql);
165static VOID CsqReleaseLock(PIO_CSQ Csq, KIRQL
Irql)
167 UNREFERENCED_PARAMETER(Csq);
168 KeReleaseSpinLock(&g_IrpLock,
Irql);
171static VOID CsqCompleteCanceledIrp(PIO_CSQ Csq, PIRP Irp)
173 UNREFERENCED_PARAMETER(Csq);
174 Irp->IoStatus.Status = STATUS_CANCELLED;
175 Irp->IoStatus.Information = 0;
176 IoCompleteRequest(Irp, IO_NO_INCREMENT);
183 InitializeListHead(&g_IrpList);
184 InitializeListHead(&g_EventList);
185 KeInitializeSpinLock(&g_IrpLock);
186 KeInitializeSpinLock(&g_EventLock);
190 g_DroppedNoConsumer = 0;
192 return IoCsqInitialize(
199 CsqCompleteCanceledIrp);
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);
214 while ((node = DequeueEvent()) != NULL) {
221 PIO_STACK_LOCATION irpSp = IoGetCurrentIrpStackLocation(Irp);
226 *Status = STATUS_BUFFER_TOO_SMALL;
231 if (InterlockedCompareExchangePointer(&irpSp->FileObject->FsContext,
PANO_CONSUMER_MARK, NULL) == NULL) {
234 LONG64 processId = (LONG64)(ULONG_PTR)PsGetCurrentProcessId();
235 irpSp->FileObject->FsContext2 = (PVOID)(ULONG_PTR)processId;
236 InterlockedExchange64(&g_ConsumerProcessId, processId);
237 InterlockedIncrement(&g_Consumers);
243 DeliverEventToIrp(Irp, node);
244 *Status = STATUS_SUCCESS;
249 IoCsqInsertIrp(&g_Csq, Irp, NULL);
254 node = DequeueEvent();
256 PIRP waiter = IoCsqRemoveNextIrp(&g_Csq, NULL);
257 if (waiter != NULL) {
258 DeliverEventToIrp(waiter, node);
262 RequeueEventFront(node);
266 *Status = STATUS_PENDING;
273 LONG64 consumer = g_ConsumerProcessId;
274 return consumer != 0 && (UINT64)consumer == ProcessId;
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);
296 InterlockedCompareExchange64(&g_ConsumerProcessId, 0, (LONG64)(ULONG_PTR)FileObject->FsContext2);
297 FileObject->FsContext2 = NULL;
299 if (InterlockedDecrement(&g_Consumers) != 0) {
307 InitializeListHead(&dropped);
310 KeAcquireSpinLock(&g_EventLock, &irql);
311 while (!IsListEmpty(&g_EventList)) {
312 InsertTailList(&dropped, RemoveHeadList(&g_EventList));
316 KeReleaseSpinLock(&g_EventLock, irql);
318 while (!IsListEmpty(&dropped)) {
322 InterlockedAdd(&g_DroppedNoConsumer, count);
333 if (g_Consumers == 0) {
334 InterlockedIncrement(&g_DroppedNoConsumer);
340 InterlockedIncrement(&g_Dropped);
349 InterlockedIncrement(&g_Dropped);
359 KeQuerySystemTimePrecise(&now);
360 header->Timestamp = now.QuadPart;
362 header->DroppedNoConsumer = 0;
372 PIRP irp = IoCsqRemoveNextIrp(&g_Csq, NULL);
374 DeliverEventToIrp(irp, node);
384 irp = IoCsqRemoveNextIrp(&g_Csq, NULL);
387 if (queued != NULL) {
388 DeliverEventToIrp(irp, queued);
393 IoCsqInsertIrp(&g_Csq, irp, NULL);
403 return STATUS_INSUFFICIENT_RESOURCES;
406 if (PayloadSize != 0) {
407 RtlCopyMemory(dest, Payload, PayloadSize);
410 SubmitEventNode(node);
411 return STATUS_SUCCESS;
421 const WCHAR* FileName,
424 if (FileName == NULL) {
432 UINT32 payloadSize = (UINT32)FIELD_OFFSET(
PANO_FILE_EVENT_DATA, FileName) + NameLength * (UINT32)
sizeof(WCHAR);
438 return STATUS_INSUFFICIENT_RESOURCES;
448 if (NameLength != 0) {
449 RtlCopyMemory(data->
FileName, FileName, NameLength *
sizeof(WCHAR));
452 SubmitEventNode(node);
453 return STATUS_SUCCESS;
461 INT64 FirstWriteTime,
463 const WCHAR* FileName,
466 if (FileName == NULL) {
479 return STATUS_INSUFFICIENT_RESOURCES;
490 if (NameLength != 0) {
491 RtlCopyMemory(data->
FileName, FileName, NameLength *
sizeof(WCHAR));
494 SubmitEventNode(node);
495 return STATUS_SUCCESS;
500static void FitTwoStrings(UINT32* FirstLength, UINT32* SecondLength, UINT32 MaxTotal, BOOLEAN* FirstCut, BOOLEAN* SecondCut)
502 const UINT32 first = *FirstLength;
503 const UINT32 second = *SecondLength;
508 if ((UINT64)first + second <= MaxTotal) {
512 const UINT32 half = MaxTotal / 2;
513 if (first > half && second > half) {
515 *SecondLength = MaxTotal - half;
517 else if (first > second) {
518 *FirstLength = MaxTotal - second;
521 *SecondLength = MaxTotal - first;
523 *FirstCut = (*FirstLength != first);
524 *SecondCut = (*SecondLength != second);
531 const WCHAR* SourceName,
532 UINT32 SourceNameLength,
533 const WCHAR* DestinationName,
534 UINT32 DestinationNameLength)
536 if (SourceName == NULL) {
537 SourceNameLength = 0;
539 if (DestinationName == NULL) {
540 DestinationNameLength = 0;
543 BOOLEAN sourceCut, destinationCut;
545 if (sourceCut || destinationCut) {
550 (SourceNameLength + DestinationNameLength) * (UINT32)
sizeof(WCHAR);
555 return STATUS_INSUFFICIENT_RESOURCES;
564 if (SourceNameLength != 0) {
565 RtlCopyMemory(data->
Names, SourceName, SourceNameLength *
sizeof(WCHAR));
567 if (DestinationNameLength != 0) {
568 RtlCopyMemory(data->
Names + SourceNameLength, DestinationName, DestinationNameLength *
sizeof(WCHAR));
571 SubmitEventNode(node);
572 return STATUS_SUCCESS;
578 UINT64 ParentProcessId,
579 UINT64 CreatingProcessId,
580 UINT64 CreatingThreadId,
581 const WCHAR* ImageName,
582 UINT32 ImageNameLength,
583 const WCHAR* CommandLine,
584 UINT32 CommandLineLength)
587 if (ImageName == NULL) {
590 if (CommandLine == NULL) {
591 CommandLineLength = 0;
594 BOOLEAN imageCut, commandLineCut;
599 if (commandLineCut) {
604 (ImageNameLength + CommandLineLength) * (UINT32)
sizeof(WCHAR);
609 return STATUS_INSUFFICIENT_RESOURCES;
620 if (ImageNameLength != 0) {
621 RtlCopyMemory(data->
Strings, ImageName, ImageNameLength *
sizeof(WCHAR));
623 if (CommandLineLength != 0) {
624 RtlCopyMemory(data->
Strings + ImageNameLength, CommandLine, CommandLineLength *
sizeof(WCHAR));
627 SubmitEventNode(node);
628 return STATUS_SUCCESS;
638 const WCHAR* KeyName,
639 UINT32 KeyNameLength,
640 const WCHAR* SecondName,
641 UINT32 SecondNameLength,
645 if (KeyName == NULL) {
648 if (SecondName == NULL) {
649 SecondNameLength = 0;
659 BOOLEAN keyCut, secondCut;
661 if (keyCut || secondCut) {
666 (KeyNameLength + SecondNameLength) * (UINT32)
sizeof(WCHAR) + DataLength;
671 return STATUS_INSUFFICIENT_RESOURCES;
683 if (KeyNameLength != 0) {
684 RtlCopyMemory(data->
Strings, KeyName, KeyNameLength *
sizeof(WCHAR));
686 if (SecondNameLength != 0) {
687 RtlCopyMemory(data->
Strings + KeyNameLength, SecondName, SecondNameLength *
sizeof(WCHAR));
689 if (DataLength != 0) {
690 RtlCopyMemory(data->
Strings + KeyNameLength + SecondNameLength, Data, DataLength);
693 SubmitEventNode(node);
694 return STATUS_SUCCESS;
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
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...
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.
@ PANO_EVENT_FILE_RENAME
A file was renamed or moved (a PANO_RENAME_EVENT_DATA).
@ PANO_EVENT_FILE_MODIFIED
A handle that wrote to a file was closed (a PANO_FILE_MODIFIED_EVENT_DATA). One event covers every wr...
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 ...
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...
_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.