-
Notifications
You must be signed in to change notification settings - Fork 83
/
mouclass_input_injection.cpp
1426 lines (1157 loc) · 37.1 KB
/
mouclass_input_injection.cpp
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*++
Copyright (c) 2019 changeofpace. All rights reserved.
Use of this source code is governed by the MIT license. See the 'LICENSE' file
for more information.
--*/
#include "mouclass_input_injection.h"
#include <kbdmou.h>
#include <ntddmou.h>
#include "debug.h"
#include "log.h"
#include "mouclass.h"
#include "mouhid_hook_manager.h"
#include "mouse_input_validation.h"
#include "nt.h"
#include "../Common/time.h"
//=============================================================================
// Constants
//=============================================================================
#define MODULE_TITLE "MouClass Input Injection"
#define DEVICE_RESOLUTION_TIMEOUT_SECONDS 15
//=============================================================================
// Private Types
//=============================================================================
/*++
Type Name:
MOUSE_CLASS_BUTTON_DEVICE
Remarks:
'Mouse class button device' refers to the mouse class device object which
receives mouse input data packets that contain button data.
--*/
typedef struct _MOUSE_CLASS_BUTTON_DEVICE {
//
// The connect data information used by the MouHid driver to copy mouse
// input data packets to the mouse class button device object.
//
CONNECT_DATA ConnectData;
//
// The 'UnitId' field value of all mouse input data packets sent to the
// mouse class button device.
//
USHORT UnitId;
} MOUSE_CLASS_BUTTON_DEVICE, *PMOUSE_CLASS_BUTTON_DEVICE;
/*++
Type Name:
MOUSE_CLASS_MOVEMENT_DEVICE
Remarks:
'Mouse class movement device' refers to the mouse class device object which
receives mouse input data packets that contain movement data.
--*/
typedef struct _MOUSE_CLASS_MOVEMENT_DEVICE {
//
// The connect data information used by the MouHid driver to copy mouse
// input data packets to the mouse class movement device object.
//
CONNECT_DATA ConnectData;
//
// The 'UnitId' field value of all mouse input data packets sent to the
// mouse class movement device.
//
USHORT UnitId;
//
// These fields reflect the 'Flags' field of all mouse input data packets
// sent to the mouse class movement device.
//
BOOLEAN AbsoluteMovement;
BOOLEAN VirtualDesktop;
} MOUSE_CLASS_MOVEMENT_DEVICE, *PMOUSE_CLASS_MOVEMENT_DEVICE;
/*++
Type Name:
MOUSE_DEVICE_STACK_CONTEXT
Type Description:
Contains the mouse class button device and the mouse class movement device
for the HID USB mouse device stack(s).
Remarks:
This context is effectively a snapshot of the mouse class device objects
when the context is initialized. PnP events for mouse devices and mouse
device configuration changes invalidate all mouse device stack contexts
initialized before the event.
In a standard Windows environment, the mouse class button device and the
mouse class movement device are represented by a single device object
created by the MouClass driver. If a third party mouse filter driver is
installed in a mouse device stack then one or both of these devices may be
maintained by the third party driver. In this scenario, the third party
driver has hooked into the input stream and may be filtering, modifying, or
injecting input packets in(to) one or both of the class data queues.
--*/
typedef struct _MOUSE_DEVICE_STACK_CONTEXT {
MOUSE_CLASS_BUTTON_DEVICE ButtonDevice;
MOUSE_CLASS_MOVEMENT_DEVICE MovementDevice;
} MOUSE_DEVICE_STACK_CONTEXT, *PMOUSE_DEVICE_STACK_CONTEXT;
typedef struct _DEVICE_RESOLUTION_CONTEXT {
KSPIN_LOCK Lock;
//
// Callback statistics.
//
_Guarded_by_(Lock) SIZE_T NumberOfPacketsProcessed;
//
// The returned ntstatus code of the device resolution callback.
//
_Guarded_by_(Lock) NTSTATUS NtStatus;
//
// Callback progression bookkeeping.
//
_Guarded_by_(Lock) BOOLEAN ButtonDeviceResolved;
_Guarded_by_(Lock) BOOLEAN MovementDeviceResolved;
_Guarded_by_(Lock) BOOLEAN ResolutionComplete;
//
// Signalled when the device resolution callback is complete, regardless of
// the returned ntstatus code.
//
_Guarded_by_(Lock) KEVENT CompletionEvent;
//
// If the device resolution callback is successful then this pointer
// returns the initialized mouse device stack context.
//
_Guarded_by_(Lock) PMOUSE_DEVICE_STACK_CONTEXT DeviceStackContext;
} DEVICE_RESOLUTION_CONTEXT, *PDEVICE_RESOLUTION_CONTEXT;
typedef struct _MOUCLASS_INPUT_INJECTION_MANAGER {
HANDLE MousePnpNotificationHandle;
POINTER_ALIGNMENT ERESOURCE Resource;
_Guarded_by_(Resource) PMOUSE_DEVICE_STACK_CONTEXT DeviceStackContext;
} MOUCLASS_INPUT_INJECTION_MANAGER, *PMOUCLASS_INPUT_INJECTION_MANAGER;
//=============================================================================
// Module Globals
//=============================================================================
EXTERN_C static MOUCLASS_INPUT_INJECTION_MANAGER g_MiiManager = {};
//=============================================================================
// Private Prototypes
//=============================================================================
_Requires_lock_not_held_(g_MiiManager.Resource)
EXTERN_C
static
MOUSE_PNP_NOTIFICATION_CALLBACK_ROUTINE
MiipMousePnpNotificationCallbackRoutine;
__drv_allocatesMem(Mem)
_IRQL_requires_(PASSIVE_LEVEL)
_IRQL_requires_same_
_Check_return_
EXTERN_C
static
PDEVICE_RESOLUTION_CONTEXT
MiipCreateDeviceResolutionContext();
_IRQL_requires_(PASSIVE_LEVEL)
_IRQL_requires_same_
EXTERN_C
VOID
MiipFreeMouseDeviceStackContext(
_Pre_notnull_ __drv_freesMem(Mem)
PMOUSE_DEVICE_STACK_CONTEXT pDeviceStackContext
);
_IRQL_requires_(PASSIVE_LEVEL)
_IRQL_requires_same_
EXTERN_C
VOID
MiipFreeDeviceResolutionContext(
_Pre_notnull_ __drv_freesMem(Mem)
PDEVICE_RESOLUTION_CONTEXT pDeviceResolutionContext
);
#if defined(DBG)
_IRQL_requires_(PASSIVE_LEVEL)
_IRQL_requires_same_
EXTERN_C
static
VOID
MiipPrintMouseDeviceStackContext(
_In_ PMOUSE_DEVICE_STACK_CONTEXT pDeviceStackContext
);
#endif
_IRQL_requires_(PASSIVE_LEVEL)
_IRQL_requires_same_
EXTERN_C
static
VOID
MiipInitializeMouseDeviceStackInformation(
_In_ PMOUSE_DEVICE_STACK_CONTEXT pDeviceStackContext,
_Out_ PMOUSE_DEVICE_STACK_INFORMATION pDeviceStackInformation
);
EXTERN_C
static
MHK_HOOK_CALLBACK_ROUTINE
MiipHookCallback;
_Requires_shared_lock_held_(g_MiiManager.Resource)
_IRQL_requires_(PASSIVE_LEVEL)
_IRQL_requires_same_
_Check_return_
EXTERN_C
static
NTSTATUS
MiipAttachProcessInjectInputPacket(
_In_ HANDLE ProcessId,
_In_ PCONNECT_DATA pConnectData,
_In_ PMOUSE_INPUT_DATA pInputPacket
);
_Requires_shared_lock_held_(g_MiiManager.Resource)
_IRQL_requires_(PASSIVE_LEVEL)
_IRQL_requires_same_
_Check_return_
EXTERN_C
static
ULONG
MiipInjectInputPackets(
_In_ PCONNECT_DATA pConnectData,
_In_reads_(nInputPackets) PMOUSE_INPUT_DATA pInputDataStart,
_In_ ULONG nInputPackets
);
//=============================================================================
// Meta Interface
//=============================================================================
_Use_decl_annotations_
EXTERN_C
NTSTATUS
MiiDriverEntry()
/*++
Routine Description:
Initializes the MouClass Input Injection module.
Required Modules:
MouClass Manager
Remarks:
If successful, the caller must call MiiDriverUnload when the driver is
unloaded.
--*/
{
BOOLEAN fResourceInitialized = FALSE;
HANDLE MousePnpNotificationHandle = NULL;
BOOLEAN fCallbackRegistered = FALSE;
NTSTATUS ntstatus = STATUS_SUCCESS;
DBG_PRINT("Loading %s.", MODULE_TITLE);
//
// NOTE We must initialize the resource before registering the mouse
// notification callback because the notification callback uses the
// resource.
//
ntstatus = ExInitializeResourceLite(&g_MiiManager.Resource);
if (!NT_SUCCESS(ntstatus))
{
ERR_PRINT("ExInitializeResourceLite failed: 0x%X", ntstatus);
goto exit;
}
//
fResourceInitialized = TRUE;
ntstatus = MclRegisterMousePnpNotificationCallback(
MiipMousePnpNotificationCallbackRoutine,
NULL,
&MousePnpNotificationHandle);
if (!NT_SUCCESS(ntstatus))
{
ERR_PRINT("MclRegisterMousePnpNotificationCallback failed: 0x%X",
ntstatus);
goto exit;
}
//
fCallbackRegistered = TRUE;
//
// Initialize the global context.
//
g_MiiManager.MousePnpNotificationHandle = MousePnpNotificationHandle;
DBG_PRINT("%s loaded.", MODULE_TITLE);
exit:
if (!NT_SUCCESS(ntstatus))
{
if (fCallbackRegistered)
{
MclUnregisterMousePnpNotificationCallback(
MousePnpNotificationHandle);
}
if (fResourceInitialized)
{
VERIFY(ExDeleteResourceLite(&g_MiiManager.Resource));
}
}
return ntstatus;
}
_Use_decl_annotations_
EXTERN_C
VOID
MiiDriverUnload()
{
DBG_PRINT("Unloading %s.", MODULE_TITLE);
MclUnregisterMousePnpNotificationCallback(
g_MiiManager.MousePnpNotificationHandle);
if (g_MiiManager.DeviceStackContext)
{
MiipFreeMouseDeviceStackContext(g_MiiManager.DeviceStackContext);
g_MiiManager.DeviceStackContext = NULL;
}
VERIFY(ExDeleteResourceLite(&g_MiiManager.Resource));
DBG_PRINT("%s unloaded.", MODULE_TITLE);
}
//=============================================================================
// Public Interface
//=============================================================================
_Use_decl_annotations_
EXTERN_C
NTSTATUS
MiiInitializeMouseDeviceStackContext(
PMOUSE_DEVICE_STACK_INFORMATION pDeviceStackInformation
)
/*++
Routine Description:
Initializes the mouse device stack context in the global context.
Parameters:
pDeviceStackInformation - Returns information about the newly initialized
mouse device stack context.
Remarks:
If successful, the 'DeviceStackContext' field of the global context is
initialized for the active HID USB mouse device stack(s). The caller must
free the 'DeviceStackContext' field by calling
MiipFreeMouseDeviceStackContext.
NOTE This routine must be invoked and succeed before the input injection
interface can be used.
--*/
{
PDEVICE_RESOLUTION_CONTEXT pDeviceResolutionContext = NULL;
BOOLEAN fResourceAcquired = FALSE;
HANDLE RegistrationHandle = NULL;
BOOLEAN fCallbacksRegistered = FALSE;
LARGE_INTEGER TimeoutInterval = {};
NTSTATUS waitstatus = STATUS_SUCCESS;
NTSTATUS ntstatus = STATUS_SUCCESS;
//
// Zero out parameters.
//
RtlSecureZeroMemory(
pDeviceStackInformation,
sizeof(*pDeviceStackInformation));
DBG_PRINT("Initializing the mouse device stack context.");
pDeviceResolutionContext = MiipCreateDeviceResolutionContext();
if (!pDeviceResolutionContext)
{
ERR_PRINT("MiipCreateDeviceResolutionContext failed: 0x%X", ntstatus);
ntstatus = STATUS_INSUFFICIENT_RESOURCES;
goto exit;
}
ExEnterCriticalRegionAndAcquireResourceExclusive(&g_MiiManager.Resource);
fResourceAcquired = TRUE;
//
// Reset the current device stack context if necessary.
//
if (g_MiiManager.DeviceStackContext)
{
MiipFreeMouseDeviceStackContext(g_MiiManager.DeviceStackContext);
g_MiiManager.DeviceStackContext = NULL;
}
//
// Install an MHK hook callback to resolve the mouse class device objects
// in the HID USB mouse device stack(s).
//
// NOTE The user must provide button input and movement input while the
// MHK hook callback is active.
//
// NOTE We do not specify an MHK notification callback because this module
// registers a mouse PnP notification callback directly during module
// initialization.
//
ntstatus = MhkRegisterCallbacks(
MiipHookCallback,
NULL,
pDeviceResolutionContext,
&RegistrationHandle);
if (!NT_SUCCESS(ntstatus))
{
ERR_PRINT("MhkRegisterCallbacks failed: 0x%X", ntstatus);
goto exit;
}
//
fCallbacksRegistered = TRUE;
//
// Wait for the user to provide mouse input.
//
MakeRelativeIntervalSeconds(
&TimeoutInterval,
DEVICE_RESOLUTION_TIMEOUT_SECONDS);
DBG_PRINT(
"Waiting %u seconds for user to provide button and movement input.",
DEVICE_RESOLUTION_TIMEOUT_SECONDS);
waitstatus = KeWaitForSingleObject(
&pDeviceResolutionContext->CompletionEvent,
Executive,
KernelMode,
FALSE,
&TimeoutInterval);
//
// Unregister the MHK callback hook immediately after the wait returns so
// that the callback cannot modify the device resolution context.
//
ntstatus = MhkUnregisterCallbacks(RegistrationHandle);
if (!NT_SUCCESS(ntstatus))
{
ERR_PRINT("MhkUnregisterCallbacks failed: 0x%X", ntstatus);
goto exit;
}
//
fCallbacksRegistered = FALSE;
DBG_PRINT("Wait complete. Processed %Iu mouse input data packets.",
pDeviceResolutionContext->NumberOfPacketsProcessed);
switch (waitstatus)
{
case STATUS_SUCCESS:
break;
case STATUS_TIMEOUT:
ERR_PRINT("Device resolution callback timed out.");
ntstatus = STATUS_IO_OPERATION_TIMEOUT;
goto exit;
default:
ERR_PRINT("KeWaitForSingleObject failed: 0x%X (Unexpected)");
ntstatus = STATUS_INTERNAL_ERROR;
goto exit;
}
//
if (!NT_SUCCESS(pDeviceResolutionContext->NtStatus))
{
ERR_PRINT("Device resolution callback failed: 0x%X",
pDeviceResolutionContext->NtStatus);
ntstatus = pDeviceResolutionContext->NtStatus;
goto exit;
}
#if defined(DBG)
MiipPrintMouseDeviceStackContext(
pDeviceResolutionContext->DeviceStackContext);
#endif
//
// Set out parameters.
//
MiipInitializeMouseDeviceStackInformation(
pDeviceResolutionContext->DeviceStackContext,
pDeviceStackInformation);
//
// Update the global context.
//
g_MiiManager.DeviceStackContext =
pDeviceResolutionContext->DeviceStackContext;
pDeviceResolutionContext->DeviceStackContext = NULL;
DBG_PRINT("Mouse device stack context initialized.");
exit:
if (fCallbacksRegistered)
{
VERIFY(MhkUnregisterCallbacks(RegistrationHandle));
}
if (fResourceAcquired)
{
ExReleaseResourceAndLeaveCriticalRegion(&g_MiiManager.Resource);
}
if (pDeviceResolutionContext)
{
MiipFreeDeviceResolutionContext(pDeviceResolutionContext);
}
return ntstatus;
}
_Use_decl_annotations_
EXTERN_C
NTSTATUS
MiiInjectMouseButtonInput(
HANDLE ProcessId,
USHORT ButtonFlags,
USHORT ButtonData
)
/*++
Routine Description:
Injects a mouse input data packet reflecting the specified input data into
the input stream in the process context of the specified process id.
Parameters:
ProcessId - The process id of the process context in which the input
injection occurs.
ButtonFlags - The button flags to be injected.
ButtonData - The button data to be injected.
Remarks:
This routine validates the specified input data.
--*/
{
BOOLEAN fResourceAcquired = FALSE;
MOUSE_INPUT_DATA InputPacket = {};
NTSTATUS ntstatus = STATUS_SUCCESS;
DBG_PRINT(
"Injecting mouse button input data. "
" (ProcessId = 0x%IX, ButtonFlags = 0x%03hX, ButtonData = 0x%04hX)",
ProcessId,
ButtonFlags,
ButtonData);
ntstatus = MivValidateButtonInput(ButtonFlags, ButtonData);
if (!NT_SUCCESS(ntstatus))
{
ERR_PRINT("MivValidateMovementInput failed: 0x%X", ntstatus);
goto exit;
}
ExEnterCriticalRegionAndAcquireResourceShared(&g_MiiManager.Resource);
fResourceAcquired = TRUE;
if (!g_MiiManager.DeviceStackContext)
{
ERR_PRINT("Unexpected mouse device stack context.");
ntstatus = STATUS_REINITIALIZATION_NEEDED;
goto exit;
}
InputPacket.UnitId = g_MiiManager.DeviceStackContext->ButtonDevice.UnitId;
InputPacket.ButtonFlags = ButtonFlags;
InputPacket.ButtonData = ButtonData;
ntstatus = MiipAttachProcessInjectInputPacket(
ProcessId,
&g_MiiManager.DeviceStackContext->ButtonDevice.ConnectData,
&InputPacket);
if (!NT_SUCCESS(ntstatus))
{
ERR_PRINT("MiipAttachProcessInjectInputPacket failed: 0x%X", ntstatus);
goto exit;
}
exit:
if (fResourceAcquired)
{
ExReleaseResourceAndLeaveCriticalRegion(&g_MiiManager.Resource);
}
return ntstatus;
}
_Use_decl_annotations_
EXTERN_C
NTSTATUS
MiiInjectMouseMovementInput(
HANDLE ProcessId,
USHORT IndicatorFlags,
LONG MovementX,
LONG MovementY
)
/*++
Routine Description:
Injects a mouse input data packet reflecting the specified input data into
the input stream in the process context of the specified process id.
Parameters:
ProcessId - The process id of the process context in which the input
injection occurs.
IndicatorFlags - The indicator flags to be injected.
MovementX - The X direction movement data to be injected.
MovementY - The Y direction movement data to be injected.
Remarks:
This routine validates the specified input data.
--*/
{
BOOLEAN fResourceAcquired = FALSE;
MOUSE_INPUT_DATA InputPacket = {};
NTSTATUS ntstatus = STATUS_SUCCESS;
DBG_PRINT(
"Injecting mouse movement input data. "
" (ProcessId = 0x%IX, IndicatorFlags = 0x%03hX, MovementX = %d,"
" MovementY = %d)",
ProcessId,
IndicatorFlags,
MovementX,
MovementY);
ntstatus = MivValidateMovementInput(IndicatorFlags, MovementX, MovementY);
if (!NT_SUCCESS(ntstatus))
{
ERR_PRINT("MivValidateMovementInput failed: 0x%X", ntstatus);
goto exit;
}
ExEnterCriticalRegionAndAcquireResourceShared(&g_MiiManager.Resource);
fResourceAcquired = TRUE;
if (!g_MiiManager.DeviceStackContext)
{
ERR_PRINT("Unexpected mouse device stack context.");
ntstatus = STATUS_REINITIALIZATION_NEEDED;
goto exit;
}
//
// Device-specific validation.
//
if (g_MiiManager.DeviceStackContext->MovementDevice.AbsoluteMovement)
{
if (!(MOUSE_MOVE_ABSOLUTE & IndicatorFlags))
{
ERR_PRINT("Unexpected indicator flags. (movement type)");
ntstatus = STATUS_UNSUCCESSFUL;
goto exit;
}
}
else
{
if (MOUSE_MOVE_ABSOLUTE & IndicatorFlags)
{
ERR_PRINT("Unexpected indicator flags. (movement type)");
ntstatus = STATUS_UNSUCCESSFUL;
goto exit;
}
}
if (g_MiiManager.DeviceStackContext->MovementDevice.VirtualDesktop)
{
if (!(MOUSE_VIRTUAL_DESKTOP & IndicatorFlags))
{
ERR_PRINT("Unexpected indicator flags. (virtual desktop)");
ntstatus = STATUS_UNSUCCESSFUL;
goto exit;
}
}
else
{
if (MOUSE_VIRTUAL_DESKTOP & IndicatorFlags)
{
ERR_PRINT("Unexpected indicator flags. (virtual desktop)");
ntstatus = STATUS_UNSUCCESSFUL;
goto exit;
}
}
//
// Initialize the packet.
//
InputPacket.UnitId =
g_MiiManager.DeviceStackContext->MovementDevice.UnitId;
InputPacket.Flags = IndicatorFlags;
InputPacket.LastX = MovementX;
InputPacket.LastY = MovementY;
ntstatus = MiipAttachProcessInjectInputPacket(
ProcessId,
&g_MiiManager.DeviceStackContext->MovementDevice.ConnectData,
&InputPacket);
if (!NT_SUCCESS(ntstatus))
{
ERR_PRINT("MiipAttachProcessInjectInputPacket failed: 0x%X", ntstatus);
goto exit;
}
exit:
if (fResourceAcquired)
{
ExReleaseResourceAndLeaveCriticalRegion(&g_MiiManager.Resource);
}
return ntstatus;
}
_Use_decl_annotations_
EXTERN_C
NTSTATUS
MiiInjectMouseInputPacketUnsafe(
HANDLE ProcessId,
BOOLEAN fUseButtonDevice,
PMOUSE_INPUT_DATA pInputPacket
)
/*++
Routine Description:
Injects the specified mouse input data packet into the input stream in the
process context of the specified process id.
Parameters:
ProcessId - The process id of the process context in which the input
injection occurs.
fUseButtonDevice - Indicates whether the input packet should be injected
using the mouse button device or the mouse movement device.
pInputPacket - The mouse input packet to be injected.
Remarks:
WARNING This routine does not validate the specified input data.
--*/
{
MOUSE_INPUT_DATA InputPacketNonPaged = {};
PCONNECT_DATA pConnectData = NULL;
NTSTATUS ntstatus = STATUS_SUCCESS;
//
// Copy the specified input packet to a local variable so that it cannot be
// paged out during injection.
//
RtlCopyMemory(
&InputPacketNonPaged,
pInputPacket,
sizeof(MOUSE_INPUT_DATA));
ExEnterCriticalRegionAndAcquireResourceShared(&g_MiiManager.Resource);
if (!g_MiiManager.DeviceStackContext)
{
ERR_PRINT("Unexpected mouse device stack context.");
ntstatus = STATUS_REINITIALIZATION_NEEDED;
goto exit;
}
if (fUseButtonDevice)
{
pConnectData =
&g_MiiManager.DeviceStackContext->ButtonDevice.ConnectData;
}
else
{
pConnectData =
&g_MiiManager.DeviceStackContext->MovementDevice.ConnectData;
}
DBG_PRINT(
"Injecting mouse input data packet. "
"(SC=%p DO=%p ID=%hu IF=0x%03hX BF=0x%03hX BD=0x%04hX RB=0x%X EX=0x%X"
" LX=%d LY=%d",
pConnectData->ClassService,
pConnectData->ClassDeviceObject,
pInputPacket->UnitId,
pInputPacket->Flags,
pInputPacket->ButtonFlags,
pInputPacket->ButtonData,
pInputPacket->RawButtons,
pInputPacket->ExtraInformation,
pInputPacket->LastX,
pInputPacket->LastY);
ntstatus = MiipAttachProcessInjectInputPacket(
ProcessId,
pConnectData,
&InputPacketNonPaged);
if (!NT_SUCCESS(ntstatus))
{
ERR_PRINT("MiipAttachProcessInjectInputPacket failed: 0x%X", ntstatus);
goto exit;
}
exit:
ExReleaseResourceAndLeaveCriticalRegion(&g_MiiManager.Resource);
return ntstatus;
}
//=============================================================================
// Private Interface
//=============================================================================
_Use_decl_annotations_
EXTERN_C
static
VOID
MiipMousePnpNotificationCallbackRoutine(
MOUSE_PNP_NOTIFICATION_EVENT Event,
PVOID pContext
)
{
UNREFERENCED_PARAMETER(pContext);
#if defined(DBG)
if (MousePnpNotificationEventArrival == Event)
{
DBG_PRINT("Received mouse PnP notification. (Arrival)");
}
else if (MousePnpNotificationEventRemoval == Event)
{
DBG_PRINT("Received mouse PnP notification. (Removal)");
}
else
{
ERR_PRINT("Received mouse PnP notification. (Unknown)");
DEBUG_BREAK;
}
#else
UNREFERENCED_PARAMETER(Event);
#endif
ExEnterCriticalRegionAndAcquireResourceExclusive(&g_MiiManager.Resource);
//
// This PnP event has invalidated our view of the mouse device stack so
// reset the device stack context in the global context.
//
if (g_MiiManager.DeviceStackContext)
{
MiipFreeMouseDeviceStackContext(g_MiiManager.DeviceStackContext);
g_MiiManager.DeviceStackContext = NULL;
DBG_PRINT("Mouse device stack context reset. (PnP)");
}
ExReleaseResourceAndLeaveCriticalRegion(&g_MiiManager.Resource);
}
_Use_decl_annotations_
EXTERN_C
static
PDEVICE_RESOLUTION_CONTEXT
MiipCreateDeviceResolutionContext()
/*++
Routine Description:
Returns an allocated and initialized device resolution context.
Remarks:
If successful, the caller must free the returned device resolution context
by calling MiipFreeDeviceResolutionContext.
--*/
{
PMOUSE_DEVICE_STACK_CONTEXT pDeviceStackContext = NULL;
PDEVICE_RESOLUTION_CONTEXT pDeviceResolutionContext = NULL;
pDeviceStackContext = (PMOUSE_DEVICE_STACK_CONTEXT)ExAllocatePool(
NonPagedPool,
sizeof(*pDeviceStackContext));
if (!pDeviceStackContext)
{
goto exit;
}
//
RtlSecureZeroMemory(pDeviceStackContext, sizeof(*pDeviceStackContext));
pDeviceResolutionContext = (PDEVICE_RESOLUTION_CONTEXT)ExAllocatePool(
NonPagedPool,
sizeof(*pDeviceResolutionContext));
if (!pDeviceResolutionContext)
{
goto exit;
}
//
RtlSecureZeroMemory(
pDeviceResolutionContext,
sizeof(*pDeviceResolutionContext));
//
// Initialize the device resolution context.
//
KeInitializeSpinLock(&pDeviceResolutionContext->Lock);
pDeviceResolutionContext->NtStatus = STATUS_INTERNAL_ERROR;
KeInitializeEvent(
&pDeviceResolutionContext->CompletionEvent,
SynchronizationEvent,
FALSE);
pDeviceResolutionContext->DeviceStackContext = pDeviceStackContext;
exit:
if (!pDeviceResolutionContext)
{
if (pDeviceStackContext)
{
ExFreePool(pDeviceStackContext);
}
}
return pDeviceResolutionContext;
}
_Use_decl_annotations_
EXTERN_C
VOID
MiipFreeMouseDeviceStackContext(
PMOUSE_DEVICE_STACK_CONTEXT pDeviceStackContext
)
{