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
|
/*++
Copyright (c) Microsoft Corporation
Module Name:
sampleservice.c
Abstract:
This file demonstrates a simple service plugin for the TREE class extension driver.
Environment:
Kernel mode
--*/
#include <ntddk.h>
#include <wdf.h>
#include <ntstrsafe.h>
#include <initguid.h>
#include <wdmguid.h>
#include <TrustedRuntimeClx.h>
#include <SampleOSService.h>
typedef enum _SAMPLE_SERVICE_TYPE {
SampleServiceEcho,
SampleServiceKernelMemory
} SAMPLE_SERVICE_TYPE;
typedef struct _SAMPLE_SERVICE_FILE_CONTEXT {
SAMPLE_SERVICE_TYPE ServiceType;
} SAMPLE_SERVICE_FILE_CONTEXT, *PSAMPLE_SERVICE_FILE_CONTEXT;
WDF_DECLARE_CONTEXT_TYPE(SAMPLE_SERVICE_FILE_CONTEXT);
typedef
NTSTATUS
SAMPLE_SERVICE_REQUEST_HANDLER(
_In_ ULONG FunctionCode,
_In_reads_bytes_(InputBufferLength) PVOID InputBuffer,
_In_ size_t InputBufferLength,
_Out_writes_bytes_to_(OutputBufferLength, *BytesWritten) PVOID OutputBuffer,
_In_ size_t OutputBufferLength,
_Out_ size_t* BytesWritten
);
//
// Driver entry point
//
DRIVER_INITIALIZE DriverEntry;
//
// Device callbacks
//
EVT_WDF_DRIVER_UNLOAD DriverUnload;
EVT_WDF_DRIVER_DEVICE_ADD SampleServiceEvtAddDevice;
EVT_WDF_DEVICE_D0_ENTRY SampleServiceEvtD0Entry;
EVT_WDF_DEVICE_D0_EXIT SampleServiceEvtD0Exit;
EVT_WDF_DEVICE_FILE_CREATE SampleServiceEvtCreateFile;
EVT_WDF_IO_QUEUE_IO_INTERNAL_DEVICE_CONTROL SampleServiceEvtInternalIoctl;
EVT_WDF_IO_QUEUE_IO_INTERNAL_DEVICE_CONTROL SampleServiceEvtQuery;
EVT_WDF_IO_QUEUE_IO_INTERNAL_DEVICE_CONTROL SampleServiceEvtExecute;
SAMPLE_SERVICE_REQUEST_HANDLER SampleServiceEchoHandler;
SAMPLE_SERVICE_REQUEST_HANDLER SampleServiceKernelMemoryHandler;
#pragma alloc_text(INIT, DriverEntry)
#pragma alloc_text(PAGE, DriverUnload)
#pragma alloc_text(PAGE, SampleServiceEvtAddDevice)
#pragma alloc_text(PAGE, SampleServiceEvtCreateFile)
#pragma alloc_text(PAGE, SampleServiceEvtInternalIoctl)
#pragma alloc_text(PAGE, SampleServiceEvtQuery)
#pragma alloc_text(PAGE, SampleServiceEvtExecute)
#pragma alloc_text(PAGE, SampleServiceEchoHandler)
#pragma alloc_text(PAGE, SampleServiceKernelMemoryHandler)
#pragma data_seg("PAGED")
DECLARE_CONST_UNICODE_STRING(GUID_ECHO_SERVICE_STRING,
L"{33C7FF13-50B0-454A-8BEB-F73EED6C0AF9}");
DECLARE_CONST_UNICODE_STRING(GUID_KERNEL_MEMORY_SERVICE_STRING,
L"{D28698A4-3B07-4F34-B65E-AE3DA6ACF2AC}");
#pragma data_seg()
volatile ULONG RandomSeed;
_Use_decl_annotations_
NTSTATUS
DriverEntry(
PDRIVER_OBJECT DriverObject,
PUNICODE_STRING RegistryPath
)
/*++
Routine Description:
This is the initialization routine for the device driver. This routine
creates the driver object for the sample OS service.
Arguments:
DriverObject - Pointer to driver object created by the system.
Return Value:
NTSTATUS code.
--*/
{
WDFDRIVER Driver;
WDF_DRIVER_CONFIG DriverConfig;
NTSTATUS Status;
Driver = NULL;
//
// Create the WDF driver object
//
WDF_DRIVER_CONFIG_INIT(&DriverConfig, SampleServiceEvtAddDevice);
DriverConfig.EvtDriverUnload = DriverUnload;
DriverConfig.DriverPoolTag = 'SOMS';
Status = WdfDriverCreate(DriverObject,
RegistryPath,
WDF_NO_OBJECT_ATTRIBUTES,
&DriverConfig,
&Driver);
if (!NT_SUCCESS(Status)) {
goto DriverEntryEnd;
}
DriverEntryEnd:
return Status;
}
_Use_decl_annotations_
VOID
DriverUnload(
WDFDRIVER Driver
)
/*++
Routine Description:
This is the cleanup function called when the driver is unloaded.
Arguments:
Driver - Supplies a handle to WDFDRIVER object created in DriverEntry.
Return Value:
None.
--*/
{
PAGED_CODE();
UNREFERENCED_PARAMETER(Driver);
}
_Use_decl_annotations_
NTSTATUS
SampleServiceEvtAddDevice(
WDFDRIVER Driver,
PWDFDEVICE_INIT DeviceInit
)
/*++
Routine Description:
This routine is called when the driver is being attached to a specific
device.
Arguments:
Driver - Supplies a handle to the framework driver object.
DeviceInit - Supplies a pointer to the device initialization parameters.
Return Value:
NTSTATUS code.
--*/
{
WDFDEVICE Device;
WDF_FILEOBJECT_CONFIG FileConfig;
WDF_OBJECT_ATTRIBUTES ObjectAttributes;
WDF_PNPPOWER_EVENT_CALLBACKS PowerCallbacks;
WDFQUEUE Queue;
WDF_IO_QUEUE_CONFIG QueueConfig;
NTSTATUS Status;
PAGED_CODE();
UNREFERENCED_PARAMETER(Driver);
WdfDeviceInitSetIoType(DeviceInit, WdfDeviceIoBuffered);
WdfDeviceInitSetDeviceType(DeviceInit, 32999);
WdfDeviceInitSetPowerNotPageable(DeviceInit);
//
// Initialize file context and register file creation handler
//
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&ObjectAttributes,
SAMPLE_SERVICE_FILE_CONTEXT);
ObjectAttributes.ExecutionLevel = WdfExecutionLevelPassive;
WDF_FILEOBJECT_CONFIG_INIT(&FileConfig,
&SampleServiceEvtCreateFile,
WDF_NO_EVENT_CALLBACK,
WDF_NO_EVENT_CALLBACK);
FileConfig.FileObjectClass = WdfFileObjectWdfCanUseFsContext;
WdfDeviceInitSetFileObjectConfig(DeviceInit,
&FileConfig,
&ObjectAttributes);
//
// Device interface will be enabled/disabled in these callbacks
//
WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&PowerCallbacks);
PowerCallbacks.EvtDeviceD0Entry = SampleServiceEvtD0Entry;
PowerCallbacks.EvtDeviceD0Exit = SampleServiceEvtD0Exit;
Status = WdfDeviceCreate(&DeviceInit, WDF_NO_OBJECT_ATTRIBUTES, &Device);
if (!NT_SUCCESS(Status)) {
goto SampleServiceEvtAddDeviceEnd;
}
//
// Create a default queue to process requests
//
WDF_OBJECT_ATTRIBUTES_INIT(&ObjectAttributes);
ObjectAttributes.ExecutionLevel = WdfExecutionLevelPassive;
WDF_IO_QUEUE_CONFIG_INIT_DEFAULT_QUEUE(&QueueConfig,
WdfIoQueueDispatchParallel);
QueueConfig.EvtIoInternalDeviceControl = SampleServiceEvtInternalIoctl;
Status = WdfIoQueueCreate(Device,
&QueueConfig,
&ObjectAttributes,
&Queue);
if (!NT_SUCCESS(Status)) {
goto SampleServiceEvtAddDeviceEnd;
}
//
// This tells TrEE driver that this OS service is available. Two services
// are served by this device, and they are distinguished by filename.
//
Status = WdfDeviceCreateDeviceInterface(Device,
&GUID_ECHO_SERVICE,
&GUID_ECHO_SERVICE_STRING);
if (!NT_SUCCESS(Status)) {
goto SampleServiceEvtAddDeviceEnd;
}
Status = WdfDeviceCreateDeviceInterface(Device,
&GUID_KERNEL_MEMORY_SERVICE,
&GUID_KERNEL_MEMORY_SERVICE_STRING);
if (!NT_SUCCESS(Status)) {
goto SampleServiceEvtAddDeviceEnd;
}
SampleServiceEvtAddDeviceEnd:
return Status;
}
_Use_decl_annotations_
NTSTATUS
SampleServiceEvtD0Entry(
WDFDEVICE Device,
WDF_POWER_DEVICE_STATE PreviousState
)
/*++
Routine Description:
This routine is called when the device is coming back from a lower
power state to D0.
Arguments:
Device - Supplies a handle to device object.
PreviousState - Supplies the power state the device was in.
Return Value:
NTSTATUS code.
--*/
{
UNREFERENCED_PARAMETER(PreviousState);
RandomSeed = (ULONG)(KeQueryInterruptTime() >> 8);
WdfDeviceSetDeviceInterfaceState(Device,
&GUID_ECHO_SERVICE,
&GUID_ECHO_SERVICE_STRING,
TRUE);
WdfDeviceSetDeviceInterfaceState(Device,
&GUID_KERNEL_MEMORY_SERVICE,
&GUID_KERNEL_MEMORY_SERVICE_STRING,
TRUE);
return STATUS_SUCCESS;
}
_Use_decl_annotations_
NTSTATUS
SampleServiceEvtD0Exit(
WDFDEVICE Device,
WDF_POWER_DEVICE_STATE TargetState
)
/*++
Routine Description:
This routine is called when the device is going down to a lower
power state.
Arguments:
Device - Supplies a handle to device object.
PreviousState - Supplies the power state the device is going to.
Return Value:
NTSTATUS code.
--*/
{
UNREFERENCED_PARAMETER(TargetState);
WdfDeviceSetDeviceInterfaceState(Device,
&GUID_ECHO_SERVICE,
&GUID_ECHO_SERVICE_STRING,
FALSE);
WdfDeviceSetDeviceInterfaceState(Device,
&GUID_KERNEL_MEMORY_SERVICE,
&GUID_KERNEL_MEMORY_SERVICE_STRING,
FALSE);
return STATUS_SUCCESS;
}
_Use_decl_annotations_
VOID
SampleServiceEvtCreateFile(
WDFDEVICE Device,
WDFREQUEST Request,
WDFFILEOBJECT FileObject
)
/*++
Routine Description:
This routine is called when the device is coming back from a lower
power state to D0.
Arguments:
Device - Supplies a handle to device object.
PreviousState - Supplies the power state the device was in.
Return Value:
NTSTATUS code.
--*/
{
PSAMPLE_SERVICE_FILE_CONTEXT FileContext;
UNICODE_STRING Filename;
NTSTATUS Status;
PAGED_CODE();
UNREFERENCED_PARAMETER(Device);
FileContext = WdfObjectGet_SAMPLE_SERVICE_FILE_CONTEXT(FileObject);
Filename = *WdfFileObjectGetFileName(FileObject);
if (Filename.Buffer[0] == L'\\') {
++Filename.Buffer;
Filename.Length -= sizeof(WCHAR);
Filename.MaximumLength -= sizeof(WCHAR);
}
//
// Record what service is this create request is for
//
if (RtlCompareUnicodeString(&Filename,
&GUID_ECHO_SERVICE_STRING,
TRUE) == 0) {
FileContext->ServiceType = SampleServiceEcho;
Status = STATUS_SUCCESS;
} else if (RtlCompareUnicodeString(&Filename,
&GUID_KERNEL_MEMORY_SERVICE_STRING,
TRUE) == 0) {
FileContext->ServiceType = SampleServiceKernelMemory;
Status = STATUS_SUCCESS;
} else {
Status = STATUS_OBJECT_NAME_NOT_FOUND;
}
WdfRequestComplete(Request, Status);
}
_Use_decl_annotations_
VOID
SampleServiceEvtInternalIoctl(
WDFQUEUE Queue,
WDFREQUEST Request,
size_t OutputBufferLength,
size_t InputBufferLength,
ULONG IoControlCode
)
/*++
Routine Description:
This routine is called when the device receives an OS service request
from TrEE class extension, which is sent as an internal device control
request.
Arguments:
Queue - Supplies a handle to the framework queue object that is
associated with the I/O request.
Request - Supplies a handle to a framework request object.
OutputBufferLength - Supplies the length of the request's output
buffer, if an output buffer is available.
InputBufferLength - Supplies the length of the request's input buffer,
if an input buffer is available.
IoControlCode - Supplies the driver-defined or system-defined I/O
control code (IOCTL) that is associated with the
request.
Return Value:
NTSTATUS code.
--*/
{
PAGED_CODE();
switch (IoControlCode) {
case IOCTL_TR_SERVICE_QUERY:
SampleServiceEvtQuery(Queue,
Request,
OutputBufferLength,
InputBufferLength,
IoControlCode);
return;
case IOCTL_TR_EXECUTE_FUNCTION:
SampleServiceEvtExecute(Queue,
Request,
OutputBufferLength,
InputBufferLength,
IoControlCode);
return;
default:
WdfRequestComplete(Request, STATUS_INVALID_PARAMETER);
}
}
_Use_decl_annotations_
VOID
SampleServiceEvtQuery(
WDFQUEUE Queue,
WDFREQUEST Request,
size_t OutputBufferLength,
size_t InputBufferLength,
ULONG IoControlCode
)
/*++
Routine Description:
This routine is called when the device receives a query request from
TrEE class extension. OS service provider fills up information about
the service.
Arguments:
Queue - Supplies a handle to the framework queue object that is
associated with the I/O request.
Request - Supplies a handle to a framework request object.
OutputBufferLength - Supplies the length of the request's output
buffer, if an output buffer is available.
InputBufferLength - Supplies the length of the request's input buffer,
if an input buffer is available.
IoControlCode - Supplies the driver-defined or system-defined I/O
control code (IOCTL) that is associated with the
request.
Return Value:
NTSTATUS code.
--*/
{
PSAMPLE_SERVICE_FILE_CONTEXT FileContext;
WDFFILEOBJECT FileObject;
PTR_SERVICE_INFORMATION ServiceInformation;
NTSTATUS Status;
PAGED_CODE();
UNREFERENCED_PARAMETER(Queue);
UNREFERENCED_PARAMETER(InputBufferLength);
UNREFERENCED_PARAMETER(OutputBufferLength);
UNREFERENCED_PARAMETER(IoControlCode);
FileObject = WdfRequestGetFileObject(Request);
FileContext = WdfObjectGet_SAMPLE_SERVICE_FILE_CONTEXT(FileObject);
Status = WdfRequestRetrieveOutputBuffer(Request,
sizeof(TR_SERVICE_INFORMATION),
(PVOID*)&ServiceInformation,
NULL);
if (!NT_SUCCESS(Status)) {
goto SampleServiceEvtInternalQueryFail;
}
ServiceInformation->InterfaceVersion = 1;
ServiceInformation->ServiceMajorVersion = 1;
ServiceInformation->ServiceMinorVersion = 1;
WdfRequestCompleteWithInformation(Request,
STATUS_SUCCESS,
sizeof(TR_SERVICE_INFORMATION));
return;
SampleServiceEvtInternalQueryFail:
WdfRequestComplete(Request, Status);
}
_Use_decl_annotations_
VOID
SampleServiceEvtExecute(
WDFQUEUE Queue,
WDFREQUEST Request,
size_t OutputBufferLength,
size_t InputBufferLength,
ULONG IoControlCode
)
/*++
Routine Description:
This routine is called when the device receives an OS service execute
request from TrEE miniport.
Arguments:
Queue - Supplies a handle to the framework queue object that is
associated with the I/O request.
Request - Supplies a handle to a framework request object.
OutputBufferLength - Supplies the length of the request's output
buffer, if an output buffer is available.
InputBufferLength - Supplies the length of the request's input buffer,
if an input buffer is available.
IoControlCode - Supplies the driver-defined or system-defined I/O
control code (IOCTL) that is associated with the
request.
Return Value:
NTSTATUS code.
--*/
{
size_t BytesWritten;
PSAMPLE_SERVICE_FILE_CONTEXT FileContext;
WDFFILEOBJECT FileObject;
PTR_SERVICE_REQUEST ServiceRequest;
PTR_SERVICE_REQUEST_RESPONSE ServiceResponse;
NTSTATUS Status;
PAGED_CODE();
UNREFERENCED_PARAMETER(Queue);
UNREFERENCED_PARAMETER(InputBufferLength);
UNREFERENCED_PARAMETER(OutputBufferLength);
UNREFERENCED_PARAMETER(IoControlCode);
FileObject = WdfRequestGetFileObject(Request);
FileContext = WdfObjectGet_SAMPLE_SERVICE_FILE_CONTEXT(FileObject);
Status = WdfRequestRetrieveInputBuffer(Request,
sizeof(TR_SERVICE_REQUEST),
(PVOID*)&ServiceRequest,
NULL);
if (!NT_SUCCESS(Status)) {
goto SampleServiceEvtInternalIoctlFail;
}
Status = WdfRequestRetrieveOutputBuffer(Request,
sizeof(TR_SERVICE_REQUEST_RESPONSE),
(PVOID*)&ServiceResponse,
NULL);
if (!NT_SUCCESS(Status)) {
goto SampleServiceEvtInternalIoctlFail;
}
switch (FileContext->ServiceType) {
case SampleServiceEcho:
Status = SampleServiceEchoHandler(ServiceRequest->FunctionCode,
ServiceRequest->InputBuffer,
(size_t)ServiceRequest->InputBufferSize,
ServiceRequest->OutputBuffer,
(size_t)ServiceRequest->OutputBufferSize,
&BytesWritten);
break;
case SampleServiceKernelMemory:
Status = SampleServiceKernelMemoryHandler(ServiceRequest->FunctionCode,
ServiceRequest->InputBuffer,
(size_t)ServiceRequest->InputBufferSize,
ServiceRequest->OutputBuffer,
(size_t)ServiceRequest->OutputBufferSize,
&BytesWritten);
break;
default:
//
// Shouldn't reach here
//
NT_ASSERT(FALSE);
Status = STATUS_INVALID_PARAMETER;
goto SampleServiceEvtInternalIoctlFail;
}
//
// Bytes written to the output buffer of the service request goes into here
//
ServiceResponse->BytesWritten = BytesWritten;
//
// Number of bytes written to WDF request's output buffer is always
// sizeof(TR_SERVICE_REQUEST_RESPONSE)
//
WdfRequestCompleteWithInformation(Request,
Status,
sizeof(TR_SERVICE_REQUEST_RESPONSE));
return;
SampleServiceEvtInternalIoctlFail:
WdfRequestComplete(Request, Status);
}
_Use_decl_annotations_
NTSTATUS
SampleServiceEchoHandler(
ULONG FunctionCode,
PVOID InputBuffer,
size_t InputBufferLength,
PVOID OutputBuffer,
size_t OutputBufferLength,
size_t* BytesWritten
)
/*++
Routine Description:
This routine handles OS service requests sent to echo service.
Arguments:
FunctionCode - Supplies the function code of the request.
InputBuffer - Supplies a pointer to the request's input buffer,
if an input buffer is available.
InputBufferLength - Supplies the length of the request's input buffer,
if an input buffer is available.
OutputBuffer - Supplies a pointer to the request's output buffer,
if an output buffer is available.
OutputBufferLength - Supplies the length of the request's output
buffer, if an output buffer is available.
BytesWritten - Supplies a pointer to the variable where number of bytes
actually written to the output buffer. In case the
output buffer is too small to contain all the data, the
required size will be written.
Return Value:
NTSTATUS code.
--*/
{
ULONG Index;
NTSTATUS Status;
PAGED_CODE();
switch (FunctionCode) {
case ECHO_SERVICE_ECHO:
if (OutputBufferLength < InputBufferLength) {
*BytesWritten = InputBufferLength;
Status = STATUS_BUFFER_OVERFLOW;
goto SampleServiceEchoHandlerEnd;
}
RtlCopyMemory(OutputBuffer, InputBuffer, InputBufferLength);
*BytesWritten = InputBufferLength;
Status = STATUS_SUCCESS;
break;
case ECHO_SERVICE_REPEAT:
*BytesWritten = 0;
while (OutputBufferLength >= InputBufferLength) {
RtlCopyMemory(OutputBuffer, InputBuffer, InputBufferLength);
OutputBuffer = (PVOID)(((ULONG_PTR)OutputBuffer) + InputBufferLength);
OutputBufferLength -= InputBufferLength;
*BytesWritten += InputBufferLength;
}
Status = STATUS_SUCCESS;
break;
case ECHO_SERVICE_REVERSE:
if (OutputBufferLength < InputBufferLength) {
Status = STATUS_BUFFER_OVERFLOW;
goto SampleServiceEchoHandlerEnd;
}
for (Index = 0; Index < InputBufferLength; ++Index) {
((PUCHAR)OutputBuffer)[Index] = ((PUCHAR)InputBuffer)[InputBufferLength - Index - 1];
}
*BytesWritten = InputBufferLength;
Status = STATUS_SUCCESS;
break;
default:
return STATUS_INVALID_PARAMETER;
}
SampleServiceEchoHandlerEnd:
return Status;
}
UCHAR ScratchMemory[1024];
NTSTATUS
SampleServiceKernelMemoryHandler(
_In_ ULONG FunctionCode,
_In_reads_bytes_(InputBufferLength) PVOID InputBuffer,
_In_ size_t InputBufferLength,
_Out_writes_bytes_to_(OuputBufferLength, *BytesWritten) PVOID OutputBuffer,
_In_ size_t OutputBufferLength,
_Out_ size_t* BytesWritten
)
/*++
Routine Description:
This routine handles OS service requests sent to kernel memory service.
Arguments:
FunctionCode - Supplies the function code of the request.
InputBuffer - Supplies a pointer to the request's input buffer,
if an input buffer is available.
InputBufferLength - Supplies the length of the request's input buffer,
if an input buffer is available.
OutputBuffer - Supplies a pointer to the request's output buffer,
if an output buffer is available.
OutputBufferLength - Supplies the length of the request's output
buffer, if an output buffer is available.
BytesWritten - Supplies a pointer to the variable where number of bytes
actually written to the output buffer. In case the
output buffer is too small to contain all the data, the
required size will be written.
Return Value:
NTSTATUS code.
--*/
{
UCHAR PreviousValue;
NTSTATUS Status;
PKERNEL_MEMORY_SAFE_RANGE SafeRange;
PKERNEL_MEMORY_WRITE_BYTE WriteByte;
PAGED_CODE();
switch (FunctionCode) {
case KERNEL_MEMORY_SERVICE_GET_SAFE_RANGE:
if (OutputBufferLength < sizeof(KERNEL_MEMORY_SAFE_RANGE)) {
*BytesWritten = sizeof(KERNEL_MEMORY_SAFE_RANGE);
Status = STATUS_BUFFER_OVERFLOW;
goto SampleServiceKernelMemoryHandlerEnd;
}
SafeRange = (PKERNEL_MEMORY_SAFE_RANGE)OutputBuffer;
SafeRange->Base = (ULONG64)(ULONG_PTR)&ScratchMemory;
SafeRange->Length = sizeof(ScratchMemory);
*BytesWritten = sizeof(KERNEL_MEMORY_SAFE_RANGE);
Status = STATUS_SUCCESS;
break;
case KERNEL_MEMORY_SERVICE_WRITE_BYTE:
if (InputBufferLength < sizeof(KERNEL_MEMORY_WRITE_BYTE)) {
Status = STATUS_INVALID_PARAMETER;
goto SampleServiceKernelMemoryHandlerEnd;
}
if (OutputBufferLength < sizeof(UCHAR)) {
*BytesWritten = sizeof(UCHAR);
Status = STATUS_BUFFER_OVERFLOW;
goto SampleServiceKernelMemoryHandlerEnd;
}
WriteByte = (PKERNEL_MEMORY_WRITE_BYTE)InputBuffer;
PreviousValue = *(PUCHAR)(ULONG_PTR)WriteByte->Address;
*(PUCHAR)(ULONG_PTR)WriteByte->Address = WriteByte->Value;
Status = STATUS_SUCCESS;
break;
case KERNEL_MEMORY_SERVICE_READ_BYTE:
if (InputBufferLength < sizeof(ULONG64)) {
Status = STATUS_INVALID_PARAMETER;
goto SampleServiceKernelMemoryHandlerEnd;
}
if (OutputBufferLength < sizeof(UCHAR)) {
*BytesWritten = sizeof(UCHAR);
Status = STATUS_BUFFER_OVERFLOW;
goto SampleServiceKernelMemoryHandlerEnd;
}
*(PUCHAR)OutputBuffer = *(PUCHAR)(ULONG_PTR)*(PULONG64)InputBuffer;
Status = STATUS_SUCCESS;
break;
default:
return STATUS_INVALID_PARAMETER;
}
SampleServiceKernelMemoryHandlerEnd:
return Status;
}
|