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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
|
/*++
Copyright (c) 2011 Microsoft Corporation
Module Name:
userscan.c
Abstract:
The implementation of user space scanning module. You have to install filter driver first, and
have filter manager load the minifilter. When filter driver is in its position, after calling
UserScanInit(...) all the subsequent CreateFile or CloseHandle would trigger the data scan if
the file is dirty.
Before the user space scanner exit, it must call UserScanFinalize(...) to cleanup the data structure
and close the listening threads.
Environment:
User mode
--*/
#include <stdio.h>
#include <assert.h>
#include "userscan.h"
#include "utility.h"
#define USER_SCAN_THREAD_COUNT 6 // the number of scanning worker threads.
typedef struct _SCANNER_MESSAGE {
//
// Required structure header.
//
FILTER_MESSAGE_HEADER MessageHeader;
//
// Private scanner-specific fields begin here.
//
AV_SCANNER_NOTIFICATION Notification;
//
// Overlapped structure: this is not really part of the message
// However we embed it here so that when we get pOvlp in
// GetQueuedCompletionStatus(...), we can restore the message
// via CONTAINING_RECORD macro.
//
OVERLAPPED Ovlp;
} SCANNER_MESSAGE, *PSCANNER_MESSAGE;
#define SCANNER_MESSAGE_SIZE (sizeof(FILTER_MESSAGE_HEADER) + sizeof(AV_SCANNER_NOTIFICATION))
typedef struct _SCANNER_REPLY_MESSAGE {
//
// Required structure header.
//
FILTER_REPLY_HEADER ReplyHeader;
//
// Private scanner-specific fields begin here.
//
ULONG ThreadId;
} SCANNER_REPLY_MESSAGE, *PSCANNER_REPLY_MESSAGE;
#define SCANNER_REPLY_MESSAGE_SIZE (sizeof(FILTER_REPLY_HEADER) + sizeof(ULONG))
//
// Local routines
//
AVSCAN_RESULT
UserScanMemoryStream(
_In_reads_bytes_(Size) PUCHAR StartingAddress,
_In_ SIZE_T Size,
_Inout_ PBOOLEAN pAbort
);
HRESULT
UserScanHandleStartScanMsg(
_In_ PUSER_SCAN_CONTEXT Context,
_In_ PSCANNER_MESSAGE Message,
_In_ PSCANNER_THREAD_CONTEXT ThreadCtx
);
HRESULT
UserScanWorker (
_Inout_ PUSER_SCAN_CONTEXT Context
);
HRESULT
UserScanListenAbortProc (
_Inout_ PUSER_SCAN_CONTEXT Context
);
DWORD
WaitForAll (
_In_ PSCANNER_THREAD_CONTEXT ScanThreadCtxes
);
HRESULT
UserScanGetThreadContextById (
_In_ DWORD ThreadId,
_In_ PUSER_SCAN_CONTEXT Context,
_Out_ PSCANNER_THREAD_CONTEXT *ScanThreadCtx
);
VOID
UserScanSynchronizedCancel (
_In_ PUSER_SCAN_CONTEXT Context
);
HRESULT
UserScanClosePorts (
_In_ PUSER_SCAN_CONTEXT Context
);
HRESULT
UserScanCleanup (
_In_ PUSER_SCAN_CONTEXT Context
);
//
// Implementation of exported routines.
// Declared in userscan.h
//
HRESULT
UserScanInit (
_Inout_ PUSER_SCAN_CONTEXT Context
)
/*++
Routine Description:
This routine initializes all the necessary data structures and forks listening threads.
The caller thread is responsible for calling UserScanFinalize(...) to cleanup the
data structures and close the listening threads.
Arguments:
Context - User scan context, please see userscan.h
Return Value:
S_OK if successful. Otherwise, it returns a HRESULT error value.
--*/
{
HRESULT hr = S_OK;
ULONG i = 0;
HANDLE hEvent = NULL;
PSCANNER_THREAD_CONTEXT scanThreadCtxes = NULL;
HANDLE hListenAbort = NULL;
AV_CONNECTION_CONTEXT connectionCtx = {0};
if (NULL == Context) {
return MAKE_HRESULT(SEVERITY_ERROR, 0, E_POINTER);
}
//
// Create the abort listening thead.
// This thread is particularly listening the abortion event.
//
hListenAbort = CreateThread( NULL,
0,
(LPTHREAD_START_ROUTINE)UserScanListenAbortProc,
Context,
CREATE_SUSPENDED,
NULL );
if (NULL == hListenAbort) {
hr = HRESULT_FROM_WIN32(GetLastError());
goto Cleanup;
}
//
// Initialize scan thread contexts.
//
scanThreadCtxes = HeapAlloc(GetProcessHeap(), 0, sizeof(SCANNER_THREAD_CONTEXT) * USER_SCAN_THREAD_COUNT);
if (NULL == scanThreadCtxes) {
hr = MAKE_HRESULT(SEVERITY_ERROR, 0, E_OUTOFMEMORY);
goto Cleanup;
}
ZeroMemory(scanThreadCtxes, sizeof(SCANNER_THREAD_CONTEXT) * USER_SCAN_THREAD_COUNT);
//
// Create scan listening threads.
//
for (i = 0;
i < USER_SCAN_THREAD_COUNT;
i ++ ) {
scanThreadCtxes[i].Handle = CreateThread( NULL,
0,
(LPTHREAD_START_ROUTINE)UserScanWorker,
Context,
CREATE_SUSPENDED,
&scanThreadCtxes[i].ThreadId );
if (NULL == scanThreadCtxes[i].Handle) {
hr = HRESULT_FROM_WIN32(GetLastError());
goto Cleanup;
}
InitializeCriticalSection(&(scanThreadCtxes[i].Lock));
}
//
// Prepare the scan communication port.
//
connectionCtx.Type = AvConnectForScan;
hr = FilterConnectCommunicationPort( AV_SCAN_PORT_NAME,
0,
&connectionCtx,
sizeof(AV_CONNECTION_CONTEXT),
NULL,
&Context->ConnectionPort );
if (FAILED(hr)) {
Context->ConnectionPort = NULL;
goto Cleanup;
}
//
// Create the IO completion port for asynchronous message passing.
//
Context->Completion = CreateIoCompletionPort( Context->ConnectionPort,
NULL,
0,
USER_SCAN_THREAD_COUNT );
if ( NULL == Context->Completion ) {
hr = HRESULT_FROM_WIN32(GetLastError());
goto Cleanup;
}
Context->ScanThreadCtxes = scanThreadCtxes;
Context->AbortThreadHandle = hListenAbort;
//
// Resume all the scanning threads.
//
for (i = 0;
i < USER_SCAN_THREAD_COUNT;
i ++ ) {
if ( ResumeThread( scanThreadCtxes[i].Handle ) == -1) {
fprintf(stderr, "[UserScanInit]: ResumeThread scan listening thread failed.\n");
hr = HRESULT_FROM_WIN32(GetLastError());
goto Cleanup;
}
}
//
// Resume abort listening thread.
//
if ( ResumeThread( hListenAbort ) == -1 ) {
fprintf(stderr, "[UserScanInit]: ResumeThread abort listening thread failed.\n");
hr = HRESULT_FROM_WIN32(GetLastError());
goto Cleanup;
}
//
// Pump messages into queue of completion port.
//
for (i = 0;
i < USER_SCAN_THREAD_COUNT;
i ++ ) {
PSCANNER_MESSAGE msg = HeapAlloc( GetProcessHeap(), 0, sizeof( SCANNER_MESSAGE ) );
if (NULL == msg) {
hr = MAKE_HRESULT(SEVERITY_ERROR, 0, E_OUTOFMEMORY);
goto Cleanup;
}
FillMemory( &msg->Ovlp, sizeof(OVERLAPPED), 0);
hr = FilterGetMessage( Context->ConnectionPort,
&msg->MessageHeader,
FIELD_OFFSET( SCANNER_MESSAGE, Ovlp ),
&msg->Ovlp );
if (hr == HRESULT_FROM_WIN32( ERROR_IO_PENDING )) {
hr = S_OK;
} else {
fprintf(stderr, "[UserScanInit]: FilterGetMessage failed.\n");
DisplayError(hr);
HeapFree(GetProcessHeap(), 0, msg );
goto Cleanup;
}
}
return hr;
Cleanup:
if (Context->Completion && !CloseHandle(Context->Completion)) {
fprintf(stderr, "[UserScanInit] Error! Close completion port failed.\n");
DisplayError(HRESULT_FROM_WIN32(GetLastError()));
}
if (Context->ConnectionPort && !CloseHandle(Context->ConnectionPort)) {
fprintf(stderr, "[UserScanInit] Error! Close connection port failed.\n");
DisplayError(HRESULT_FROM_WIN32(GetLastError()));
}
if (scanThreadCtxes) {
for (i = 0;
i < USER_SCAN_THREAD_COUNT;
i ++ ) {
if (scanThreadCtxes[i].Handle && !CloseHandle(scanThreadCtxes[i].Handle)) {
fprintf(stderr, "[UserScanInit] Error! Close scan thread failed.\n");
DisplayError(HRESULT_FROM_WIN32(GetLastError()));
}
DeleteCriticalSection(&(scanThreadCtxes[i].Lock));
}
HeapFree(GetProcessHeap(), 0, scanThreadCtxes);
}
if (hListenAbort && !CloseHandle(hListenAbort)) {
fprintf(stderr, "[UserScanInit] Error! Close listen abort thread failed.\n");
DisplayError(HRESULT_FROM_WIN32(GetLastError()));
}
if (hEvent && !CloseHandle(hEvent)) {
fprintf(stderr, "[UserScanInit] Error! Close event handle failed.\n");
DisplayError(HRESULT_FROM_WIN32(GetLastError()));
}
return hr;
}
HRESULT
UserScanFinalize (
_In_ PUSER_SCAN_CONTEXT Context
)
/*++
Routine Description:
This routine cleans up all the necessary data structures and closes listening threads.
It does the following things:
1) Cancel all the scanning threads and wait for them to terminate.
2) Close all the thread handles
3) Close all the port handles
4) Free memory of scan thread contexts.
Arguments:
Context - User scan context, please see userscan.h
Return Value:
S_OK if successful. Otherwise, it returns a HRESULT error value.
--*/
{
HRESULT hr = S_OK;
printf("=================finalize\n");
UserScanSynchronizedCancel( Context );
printf("[UserScanFinalize]: Closing connection port\n");
hr = UserScanCleanup( Context );
return hr;
}
//
// Implementation of local routines
//
DWORD
WaitForAll (
_In_ PSCANNER_THREAD_CONTEXT ScanThreadCtxes
)
/*++
Routine Description:
A local helper function that enable the caller to wair for all the scan threads.
Arguments:
ScanThreadCtxes - Scan thread contextes.
Return Value:
Please consult WaitForMultipleObjects(...)
--*/
{
ULONG i = 0;
HANDLE hScanThreads[USER_SCAN_THREAD_COUNT] = {0};
for (i = 0;
i < USER_SCAN_THREAD_COUNT;
i ++ ) {
hScanThreads[i] = ScanThreadCtxes[i].Handle;
}
return WaitForMultipleObjects(USER_SCAN_THREAD_COUNT, hScanThreads, TRUE, INFINITE);
}
HRESULT
UserScanGetThreadContextById (
_In_ DWORD ThreadId,
_In_ PUSER_SCAN_CONTEXT Context,
_Out_ PSCANNER_THREAD_CONTEXT *ScanThreadCtx
)
/*++
Routine Description:
This routine search for the scan thread context by its thread id.
Arguments:
ThreadId - The thread id to be searched.
Context - The user scan context.
ScanThreadCtx - Output scan thread context.
Return Value:
S_OK if found, otherwise not found.
--*/
{
HRESULT hr = S_OK;
ULONG i;
PSCANNER_THREAD_CONTEXT scanThreadCtx = Context->ScanThreadCtxes;
*ScanThreadCtx = NULL;
for (i = 0;
i < USER_SCAN_THREAD_COUNT;
i ++ ) {
if ( ThreadId == scanThreadCtx[i].ThreadId ) {
*ScanThreadCtx = (scanThreadCtx + i);
return hr;
}
}
return MAKE_HRESULT(SEVERITY_ERROR,0,E_FAIL);
}
VOID
UserScanSynchronizedCancel (
_In_ PUSER_SCAN_CONTEXT Context
)
/*++
Routine Description:
This routine tries to abort all the scanning threads and wait for them to terminate.
Arguments:
Context - User scan context, please see userscan.h
Return Value:
Please consult WaitForMultipleObjects(...)
--*/
{
ULONG i;
PSCANNER_THREAD_CONTEXT scanThreadCtxes = Context->ScanThreadCtxes;
if (NULL == scanThreadCtxes) {
fprintf(stderr, "Scan thread contexes are NOT suppoed to be NULL.\n");
return;
}
//
// Tell all scanning threads that the program is going to exit.
//
Context->Finalized = TRUE;
//
// Signal cancellation events for all scanning threads.
//
for (i = 0;
i < USER_SCAN_THREAD_COUNT;
i ++ ) {
scanThreadCtxes[i].Aborted = TRUE;
}
//
// Wake up the listening thread if it is waiting for message
// via GetQueuedCompletionStatus()
//
CancelIoEx(Context->ConnectionPort, NULL);
//
// Wait for all scan threads to complete cancellation,
// so we will be able to close the connection port and etc.
//
WaitForAll(scanThreadCtxes);
return;
}
HRESULT
UserScanClosePorts (
_In_ PUSER_SCAN_CONTEXT Context
)
/*++
Routine Description:
This routine cleans up all the necessary data structures and closes listening threads.
It does closing the scanning communication port and completion port.
Arguments:
Context - User scan context, please see userscan.h
Return Value:
S_OK if successful. Otherwise, it returns a HRESULT error value.
--*/
{
HRESULT hr = S_OK;
if (!CloseHandle(Context->ConnectionPort)) {
fprintf(stderr, "[UserScanFinalize]: Failed to close the connection port.\n");
hr = HRESULT_FROM_WIN32(GetLastError());
}
Context->ConnectionPort = NULL;
if (!CloseHandle(Context->Completion)) {
fprintf(stderr, "[UserScanFinalize]: Failed to close the completion port.\n");
hr = HRESULT_FROM_WIN32(GetLastError());
}
Context->Completion = NULL;
return hr;
}
HRESULT
UserScanCleanup (
_In_ PUSER_SCAN_CONTEXT Context
)
/*++
Routine Description:
This routine cleans up all the necessary data structures and closes listening threads.
It does closing abort thread handle and all scanning threads. It also closes the ports
by calling UserScanClosePorts(...).
Arguments:
Context - User scan context, please see userscan.h
Return Value:
S_OK if successful. Otherwise, it returns a HRESULT error value.
--*/
{
ULONG i = 0;
HRESULT hr = S_OK;
PSCANNER_THREAD_CONTEXT scanThreadCtxes = Context->ScanThreadCtxes;
if (NULL == scanThreadCtxes) {
fprintf(stderr, "Scan thread contexes are NOT suppoed to be NULL.\n");
return E_POINTER;
}
if (Context->AbortThreadHandle) {
CloseHandle( Context->AbortThreadHandle );
}
hr = UserScanClosePorts( Context );
//
// Clean up scan thread contexts
//
for (i = 0;
i < USER_SCAN_THREAD_COUNT;
i ++ ) {
if (scanThreadCtxes[i].Handle && !CloseHandle(scanThreadCtxes[i].Handle)) {
fprintf(stderr, "[UserScanInit] Error! Close scan thread failed.\n");
DisplayError(HRESULT_FROM_WIN32(GetLastError()));
}
DeleteCriticalSection(&(scanThreadCtxes[i].Lock));
}
HeapFree( GetProcessHeap(), 0, scanThreadCtxes );
Context->ScanThreadCtxes = NULL;
return hr;
}
AVSCAN_RESULT
UserScanMemoryStream(
_In_reads_bytes_(Size) PUCHAR StartingAddress,
_In_ SIZE_T Size,
_Inout_ PBOOLEAN pAbort
)
/*++
Routine Description:
This routine is a naive search for virus signiture.
Note that this function is by no means efficient and scalable, but
this is not the focus of this example. Thus, an anti-virus
vendor may want to focus on and expand this function.
It will reset the abort flag if it is aborted.
Arguments:
StartingAddress - The starting address of the memory to be searched.
Size - The size of the memory.
pAbort - A pointer to a boolean that notifies the scanning should be canceled..
Infected - TRUE if this file is infected. FALSE, otherwise.
Return Value:
S_OK.
--*/
{
ULONG i;
UCHAR targetString[AV_DEFAULT_SEARCH_PATTERN_SIZE] = {0};
SIZE_T searchStringLength = AV_DEFAULT_SEARCH_PATTERN_SIZE-1;
ULONG ind;
PUCHAR p;
PUCHAR start = StartingAddress;
PUCHAR end = start + Size - searchStringLength;
//
// Decode the target pattern. We could decode only once and cache it.
//
CopyMemory( (PVOID) targetString,
AV_DEFAULT_SEARCH_PATTERN,
AV_DEFAULT_SEARCH_PATTERN_SIZE );
for (ind = 0;
ind < searchStringLength;
ind++) {
targetString[ind] = ((UCHAR)targetString[ind]) ^ AV_DEFAULT_PATTERN_XOR_KEY;
}
targetString[searchStringLength] = '\0';
//
// Scan the memory stream for the target pattern.
// If not cancelled.
//
for (p = start, i = 1;
p <= end ;
p++, i++) {
//
// If (*pAbort == TRUE), then we abort the scanning in the loop.
//
if ( *pAbort ) {
*pAbort = FALSE;
return AvScanResultUndetermined;
}
if ( !memcmp( p, targetString, searchStringLength )) {
return AvScanResultInfected;
}
}
return AvScanResultClean;
}
HRESULT
UserScanHandleStartScanMsg(
_In_ PUSER_SCAN_CONTEXT Context,
_In_ PSCANNER_MESSAGE Message,
_In_ PSCANNER_THREAD_CONTEXT ThreadCtx
)
/*++
Routine Description:
After receiving the scan request from the kernel. This routine is
the main function that handle the scan request.
This routine does not know which file it is scanning because it
does not need to know.
Its main job includes:
1) Send message to the filter to create a section object.
2) Map the view of the section.
3) Scan the memory
4) Send message to tell the filter the result of the scan
and close the section object.
Arguments:
Context - The user scan context.
Message - The message recieved from the kernel.
ThreadCtx - The scan thread context.
Return Value:
S_OK.
--*/
{
HRESULT hr = S_OK;
ULONG bytesReturned = 0;
HANDLE sectionHandle = NULL;
PVOID scanAddress = NULL;
MEMORY_BASIC_INFORMATION memoryInfo;
PAV_SCANNER_NOTIFICATION notification = &Message->Notification;
COMMAND_MESSAGE commandMessage = {0};
DWORD flags = 0;
//
// Send the message to the filter to create a section object for data scan.
// If success, we would get section handle.
//
// We just have to transparently pass ScanContextId to filter, which we
// obtained from the filter previously.
//
commandMessage.Command = AvCmdCreateSectionForDataScan;
commandMessage.ScanId = notification->ScanId;
commandMessage.ScanThreadId = ThreadCtx->ThreadId;
hr = FilterSendMessage( Context->ConnectionPort,
&commandMessage,
sizeof( COMMAND_MESSAGE ),
§ionHandle,
sizeof( HANDLE ),
&bytesReturned );
if (FAILED(hr)) {
fprintf(stderr,
"[UserScanHandleStartScanMsg]: Failed to send message SendMessageToCreateSection to the minifilter.\n");
DisplayError(hr);
return hr;
}
scanAddress = MapViewOfFile( sectionHandle,
FILE_MAP_READ,
0L,
0L,
0 );
if (scanAddress == NULL) {
fprintf(stderr, "[UserScanHandleStartScanMsg]: Failed to map the view.\n");
DisplayError(HRESULT_FROM_WIN32(GetLastError()));
goto Cleanup;
}
if( !VirtualQuery( scanAddress, &memoryInfo, sizeof(memoryInfo) )) {
fprintf(stderr, "[UserScanHandleStartScanMsg]: Failed to query the view.\n");
DisplayError(HRESULT_FROM_WIN32(GetLastError()));
goto Cleanup;
}
//
// Data scan here.
//
commandMessage.ScanResult = UserScanMemoryStream( (PUCHAR)scanAddress,
memoryInfo.RegionSize,
&ThreadCtx->Aborted );
//
// If scanning on file open, give the pages a transient boost
// since they may soon be accessed in read operations on the
// file.
//
if (notification->Reason == AvScanOnOpen) {
flags = MEM_UNMAP_WITH_TRANSIENT_BOOST;
}
Cleanup:
if (scanAddress != NULL) {
if (!UnmapViewOfFileEx( scanAddress, flags )) {
fprintf(stderr, "[UserScanHandleStartScanMsg]: Failed to unmap the view.\n");
DisplayError(HRESULT_FROM_WIN32(GetLastError()));
}
}
//
// We have to close the section handle after we finish using it.
// It is required to close the section handle here in user mode.
//
if (!CloseHandle(sectionHandle)) {
fprintf(stderr, "[UserScanHandleStartScanMsg]: Failed to close the section handle.\n");
DisplayError(HRESULT_FROM_WIN32(GetLastError()));
}
//
// Send the message to tell filter to close the section object.
// This call will set the file clean or infected depending on the scan result, and
// also trigger events and release the waiting I/O request thread.
//
commandMessage.Command = AvCmdCloseSectionForDataScan;
hr = FilterSendMessage( Context->ConnectionPort,
&commandMessage,
sizeof( COMMAND_MESSAGE ),
NULL,
0,
&bytesReturned );
if (FAILED(hr)) {
fprintf(stderr,
"[UserScanHandleStartScanMsg]: Failed to close message SendMessageToCreateSection to the minifilter.\n");
DisplayError( hr );
return hr;
}
return hr;
}
HRESULT
UserScanWorker (
_Inout_ PUSER_SCAN_CONTEXT Context
)
/*++
Routine Description:
This routine is the scanning worker thread procedure.
The pseudo-code of this function is as follows,
while(TRUE) {
1) Get a overlap structure from the completion port.
2) Obtain message from overlap structure.
3) Process the message via calling UserScanHandleStartScanMsg(...)
4) Pump overlap structure into completion port using FilterGetMessage(...)
}
Arguments:
Context - The user scan context.
Return Value:
S_OK if no error occurs; Otherwise, it would return appropriate HRESULT.
--*/
{
HRESULT hr = S_OK;
PSCANNER_MESSAGE message = NULL;
SCANNER_REPLY_MESSAGE replyMsg;
LPOVERLAPPED pOvlp = NULL;
DWORD outSize;
ULONG_PTR key;
BOOL success = FALSE;
PSCANNER_THREAD_CONTEXT threadCtx = NULL;
hr = UserScanGetThreadContextById( GetCurrentThreadId(), Context, &threadCtx );
if (FAILED(hr)) {
fprintf(stderr,
"[UserScanWorker]: Failed to get thread context.\n");
return hr;
}
ZeroMemory( &replyMsg, SCANNER_REPLY_MESSAGE_SIZE );
printf("Current thread handle %p, id:%u\n", threadCtx->Handle, threadCtx->ThreadId);
//
// This thread is waiting for scan message from the driver
//
for(;;) {
message = NULL;
//
// Get overlapped structure asynchronously, the overlapped structure
// was previously pumped by FilterGetMessage(...)
//
success = GetQueuedCompletionStatus( Context->Completion, &outSize, &key, &pOvlp, INFINITE );
if (!success) {
hr = HRESULT_FROM_WIN32(GetLastError());
//
// The completion port handle associated with it is closed
// while the call is outstanding, the function returns FALSE,
// *lpOverlapped will be NULL, and GetLastError will return ERROR_ABANDONED_WAIT_0
//
if (hr == E_HANDLE) {
printf("Completion port becomes unavailable.\n");
hr = S_OK;
} else if (hr == HRESULT_FROM_WIN32(ERROR_ABANDONED_WAIT_0)) {
printf("Completion port was closed.\n");
hr = S_OK;
}
break;
}
//
// Recover message strcuture from overlapped structure.
// Remember we embedded overlapped structure inside SCANNER_MESSAGE.
// This is because the overlapped structure obtained from GetQueuedCompletionStatus(...)
// is asynchronously and not guranteed in order.
//
message = CONTAINING_RECORD( pOvlp, SCANNER_MESSAGE, Ovlp );
if (AvMsgStartScanning == message->Notification.Message) {
//
// Reset the abort flag since this is a new scan request and remember
// the scan context ID. This ID will allow us to match a cancel request
// with a given scan task.
//
EnterCriticalSection(&(threadCtx->Lock));
threadCtx->Aborted = FALSE;
threadCtx->ScanId = message->Notification.ScanId;
LeaveCriticalSection(&(threadCtx->Lock));
//
// Reply the scanning worker thread handle to the filter
// This is important because the filter will also wait for the scanning thread
// in case that the scanning thread is killed before telling filter
// the scan is done or aborted.
//
ZeroMemory( &replyMsg, SCANNER_REPLY_MESSAGE_SIZE );
replyMsg.ReplyHeader.MessageId = message->MessageHeader.MessageId;
replyMsg.ThreadId = threadCtx->ThreadId;
hr = FilterReplyMessage( Context->ConnectionPort,
&replyMsg.ReplyHeader,
SCANNER_REPLY_MESSAGE_SIZE );
if (FAILED(hr)) {
fprintf(stderr,
"[UserScanWorker]: Failed to reply thread handle to the minifilter\n");
DisplayError(hr);
break;
}
hr = UserScanHandleStartScanMsg( Context, message, threadCtx );
} else {
assert( FALSE ); // This thread should not receive other kinds of message.
}
if (FAILED(hr)) {
fprintf(stderr,
"[UserScanWorker]: Failed to handle the message.\n");
}
//
// If fianlized flag is set from main thread,
// then it would break the while loop.
//
if (Context->Finalized) {
break;
}
//
// After we process the message, pump a overlapped structure into completion port again.
//
hr = FilterGetMessage( Context->ConnectionPort,
&message->MessageHeader,
FIELD_OFFSET( SCANNER_MESSAGE, Ovlp ),
&message->Ovlp );
if (hr == HRESULT_FROM_WIN32(ERROR_OPERATION_ABORTED)) {
printf("FilterGetMessage aborted.\n");
break;
} else if (hr != HRESULT_FROM_WIN32( ERROR_IO_PENDING )) {
fprintf(stderr,
"[UserScanWorker]: Failed to get message from the minifilter. \n0x%x, 0x%x\n",
hr, HRESULT_FROM_WIN32(GetLastError()));
DisplayError(hr);
break;
}
} // end of while(TRUE)
if (message) {
//
// Free the memory, which originally allocated at UserScanInit(...)
//
HeapFree(GetProcessHeap(), 0, message);
}
printf("***Thread id %u exiting\n", threadCtx->ThreadId);
return hr;
}
HRESULT
UserScanListenAbortProc (
_Inout_ PUSER_SCAN_CONTEXT Context
)
/*++
Routine Description:
This routine is the abort listening thread procedure.
This thread is particularly listening the abortion notifcation from the filter.
The pseudo-code of this function is as follows,
while(TRUE) {
1) Wair for and get a message from the filter via FilterGetMessage(...)
2) Find the scan thread context by its thread id.
3) Set the cancel flag to be TRUE.
}
Arguments:
Context - The user scan context.
Return Value:
S_OK if no error occurs; Otherwise, it would return appropriate HRESULT.
--*/
{
HRESULT hr = S_OK;
HANDLE abortPort = NULL; // A port for listening the abort notification from driver.
SCANNER_MESSAGE message;
DWORD dwThisThread = GetCurrentThreadId();
SCANNER_REPLY_MESSAGE replyMsg;
AV_CONNECTION_CONTEXT connectionCtx = {0};
PSCANNER_THREAD_CONTEXT threadCtx = NULL;
ZeroMemory( &message, SCANNER_MESSAGE_SIZE );
//
// Prepare the abort communication port.
//
connectionCtx.Type = AvConnectForAbort;
hr = FilterConnectCommunicationPort( AV_ABORT_PORT_NAME,
0,
&connectionCtx,
sizeof(AV_CONNECTION_CONTEXT),
NULL,
&abortPort );
if (FAILED(hr)) {
abortPort = NULL;
return hr;
}
//
// This thread is listening an scan abortion notifcation or filter unloading from the kernel
// If it receives notification, its type must be AvMsgAbortScanning or AvMsgFilterUnloading
//
for(;;) {
//
// Wait until an abort command is sent from filter.
//
hr = FilterGetMessage( abortPort,
&message.MessageHeader,
SCANNER_MESSAGE_SIZE,
NULL );
if (hr == HRESULT_FROM_WIN32(ERROR_OPERATION_ABORTED)) {
printf("[UserScanListenAbortProc]: FilterGetMessage aborted.\n");
hr = S_OK;
break;
} else if (FAILED(hr)) {
fprintf(stderr,
"[UserScanListenAbortProc]: Failed to get message from the minifilter.\n" );
DisplayError(hr);
continue;
}
printf("[UserScanListenAbortProc]: Got message %llu. \n", message.MessageHeader.MessageId);
if (AvMsgAbortScanning == message.Notification.Message) {
//
// After this thread receives AvMsgAbortScanning
// it does
// 1) Find the user scan thread context
// 2) Set Aborted flag to be TRUE
//
hr = UserScanGetThreadContextById(message.Notification.ScanThreadId,
Context,
&threadCtx);
if (SUCCEEDED(hr)) {
printf("[UserScanListenAbortProc]: User Set AvMsgAbortScanning\n");
//
// Without critical section here, we cannot prevent the scanner thread from
// proceeding to the next task after we check the Id and before we set the abort flag.
// In short, without critical section, it will be a TOCTTOU bug.
//
EnterCriticalSection(&(threadCtx->Lock));
if (threadCtx->ScanId == message.Notification.ScanId) {
threadCtx->Aborted = TRUE;
printf("[UserScanListenAbortProc]: %lld aborted\n", message.Notification.ScanId);
} else {
printf("[UserScanListenAbortProc]: tried to abort %lld, but current scan in this thread is %lld\n",
message.Notification.ScanId,
threadCtx->ScanId);
}
LeaveCriticalSection(&(threadCtx->Lock));
} else {
fprintf(stderr, "[UserScanListenAbortProc]: Error! UserScanGetThreadContextById failed.\n");
}
} else if (AvMsgFilterUnloading == message.Notification.Message) {
//
// After this thread receives AvMsgFilterUnloading
// it does
// 1) Cancell all the scanning threads
// 2) Wait for them to finish the cancel.
// 3) Reply to filter so that the filter can know it can close the server ports.
// 4) Close scan port, completion port, and abortion port.
// 5) Exit the process
//
UserScanSynchronizedCancel( Context );
printf("The filter is unloading, exit!\n");
ZeroMemory( &replyMsg, SCANNER_REPLY_MESSAGE_SIZE );
replyMsg.ReplyHeader.MessageId = message.MessageHeader.MessageId;
replyMsg.ThreadId = dwThisThread;
hr = FilterReplyMessage( abortPort,
&replyMsg.ReplyHeader,
SCANNER_REPLY_MESSAGE_SIZE );
if (FAILED(hr)) {
fprintf(stderr, "[UserScanListenAbortProc]: Error! FilterReplyMessage failed.\n");
}
UserScanClosePorts( Context );
CloseHandle( abortPort );
ExitProcess( 0 );
break;
} else {
assert( FALSE ); // This thread should not receive other kinds of message.
}
if (FAILED(hr)) {
fprintf(stderr, "[UserScanListenAbortProc]: Failed to handle the message.\n");
DisplayError(HRESULT_FROM_WIN32(GetLastError()));
}
} // end of while(TRUE)
if (!CloseHandle(abortPort)) {
fprintf(stderr, "[UserScanListenAbortProc]: Failed to close the connection port.\n");
}
abortPort = NULL;
return hr;
}
|