summaryrefslogtreecommitdiff
path: root/TrEE/Miniport/SampleMiniport.c
blob: eae22c7c67bb85cd7b5cd82e2267dda5fb1b5f72 (plain)
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
/*++

Copyright (c) Microsoft Corporation

Module Name:

    sample.c

Abstract:

    This file demonstrates a simple TrEE miniport.

Environment:

    Kernel mode

--*/

#include <ntddk.h>
#include <wdf.h>
#include <initguid.h>
#include <wdmguid.h>
#include <ntstrsafe.h>
#include <TrustedRuntimeClx.h>

//
// Header file <TrEEVariableService.h> requires prior definition of a CHAR16.
// For now we need to define this in sample.
//
typedef UINT16 CHAR16;

#include <TrEEVariableService.h>
#include "sampleminiport.h"
#include "..\inc\SampleSecureService.h"
#include "..\inc\SampleOSService.h"

#define SDDL_SAMPLE_TEST2_SERVICE L"D:P(A;;FRFW;;;WD)(A;;FRFW;;;RC)(A;;FRFW;;;AC)"

//
// Driver entry point
//

DRIVER_INITIALIZE DriverEntry;

//
// Device callbacks
//

EVT_WDF_DRIVER_UNLOAD DriverUnload;
EVT_WDF_DRIVER_DEVICE_ADD TreeSampleEvtAddDevice;

//
// Class Extension callbacks
//

EVT_TR_CREATE_SECURE_DEVICE_CONTEXT TreeSampleCreateSecureDeviceContext;
EVT_TR_DESTROY_SECURE_DEVICE_CONTEXT TreeSampleDestroySecureDeviceContext;
EVT_TR_PREPARE_HARDWARE_SECURE_ENVIRONMENT TreeSamplePrepareHardwareSecureEnvironment;
EVT_TR_RELEASE_HARDWARE_SECURE_ENVIRONMENT TreeSampleReleaseHardwareSecureEnvironment;
EVT_TR_CONNECT_SECURE_ENVIRONMENT TreeSampleConnectSecureEnvironment;
EVT_TR_DISCONNECT_SECURE_ENVIRONMENT TreeSampleDisconnectSecureEnvironment;
EVT_TR_ENUMERATE_SECURE_SERVICES TreeSampleEnumerateSecureServices;
EVT_TR_PROCESS_OTHER_DEVICE_IO TreeSampleProcessOtherDeviceIo;
EVT_TR_CREATE_SECURE_SERVICE_CONTEXT TreeSampleCreateSecureServiceContext;
EVT_TR_QUERY_SERVICE_CALLBACKS TreeSampleQueryServiceCallbacks;

#pragma data_seg("PAGED")

TR_SECURE_DEVICE_CALLBACKS TreeSampleCallbacks = {
    TR_DEVICE_SERIALIZE_ALL_REQUESTS |
    TR_DEVICE_STACK_RESERVE_8K,

    &TreeSampleCreateSecureDeviceContext,
    &TreeSampleDestroySecureDeviceContext,

    &TreeSamplePrepareHardwareSecureEnvironment,
    &TreeSampleReleaseHardwareSecureEnvironment,

    &TreeSampleConnectSecureEnvironment,
    &TreeSampleDisconnectSecureEnvironment,

    &TreeSampleEnumerateSecureServices,
    &TreeSampleProcessOtherDeviceIo,

    &TreeSampleQueryServiceCallbacks
};

#pragma data_seg()

#pragma alloc_text(INIT, DriverEntry)
#pragma alloc_text(PAGE, DriverUnload)
#pragma alloc_text(PAGE, TreeSampleEvtAddDevice)
#pragma alloc_text(PAGE, TreeSampleCreateSecureDeviceContext)
#pragma alloc_text(PAGE, TreeSampleDestroySecureDeviceContext)
#pragma alloc_text(PAGE, TreeSamplePrepareHardwareSecureEnvironment)
#pragma alloc_text(PAGE, TreeSampleReleaseHardwareSecureEnvironment)
#pragma alloc_text(PAGE, TreeSampleEnumerateSecureServices)
#pragma alloc_text(PAGE, TreeSampleProcessOtherDeviceIo)
#pragma alloc_text(PAGE, TreeSampleQueryServiceCallbacks)

_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 TrEE miniport.

    Arguments:

        DriverObject - Supplies a pointer to driver object created by the
                       system.

        RegistryPath - Supplies a unicode string indentifying where the
                       parameters for this driver are located in the registry.

    Return Value:

        NTSTATUS code.

--*/

{

    WDFDRIVER Driver;
    WDF_DRIVER_CONFIG DriverConfig;
    NTSTATUS Status;

    Driver = NULL;

    //
    // Create the WDF driver object
    //

    WDF_DRIVER_CONFIG_INIT(&DriverConfig, TreeSampleEvtAddDevice);
    DriverConfig.EvtDriverUnload = DriverUnload;

    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
TreeSampleEvtAddDevice(
    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;
    NTSTATUS Status;

    PAGED_CODE();

    UNREFERENCED_PARAMETER(Driver);

    Status = TrSecureDeviceHandoffMasterDeviceControl(
                DeviceInit,
                &TreeSampleCallbacks,
                &Device);

    TrSecureDeviceLogMessage(Device,
                             STATUS_SEVERITY_INFORMATIONAL,
                             "Master device discovered\n");

    return Status;
}

_Use_decl_annotations_
NTSTATUS
TreeSampleCreateSecureDeviceContext(
    WDFDEVICE MasterDevice
    )

/*++

    Routine Description:

        This routine is called when the secure environment is first started.

    Arguments:

        MasterDevice - Supplies a handle to the master device object.

        DeviceContext - Supplies a pointer to store any context information
                        required for future calls.

    Return Value:

        NTSTATUS code.

--*/

{

    WDF_OBJECT_ATTRIBUTES ContextAttributes;
    PTREE_SAMPLE_DEVICE_CONTEXT MasterContext;
    NTSTATUS Status;
    DECLARE_CONST_UNICODE_STRING(SymbolicLink, L"\\DosDevices\\SampleTrEEDriver");

    WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&ContextAttributes,
                                            TREE_SAMPLE_DEVICE_CONTEXT);

    Status = WdfObjectAllocateContext(MasterDevice, &ContextAttributes, &MasterContext);
    if (!NT_SUCCESS(Status)) {
        goto TreeSampleCreateSecureDeviceContextEnd;
    }

    MasterContext->MasterDevice = MasterDevice;

    //
    // Create a symbolic link so that usermode program can access master device.
    //
    Status = WdfDeviceCreateSymbolicLink(MasterDevice, &SymbolicLink);
    if (!NT_SUCCESS(Status)) {
        goto TreeSampleCreateSecureDeviceContextEnd;
    }

TreeSampleCreateSecureDeviceContextEnd:
    return Status;
}

_Use_decl_annotations_
NTSTATUS
TreeSampleDestroySecureDeviceContext(
    WDFDEVICE MasterDevice
    )

/*++

    Routine Description:

        This routine is called when the secure environment is no longer used.

    Arguments:

        MasterDevice - Supplies a handle to the master device object.

    Return Value:

        NTSTATUS code.

--*/

{

    PTREE_SAMPLE_DEVICE_CONTEXT MasterContext;

    MasterContext = WdfObjectGet_TREE_SAMPLE_DEVICE_CONTEXT(MasterDevice);

    //
    // No member needs cleanup here. ServiceCollection will be automatically
    // reclaimed by the framework, since the parent object is going away.
    //

    return STATUS_SUCCESS;
}

_Use_decl_annotations_
NTSTATUS
TreeSamplePrepareHardwareSecureEnvironment(
    WDFDEVICE MasterDevice,
    WDFCMRESLIST RawResources,
    WDFCMRESLIST TranslatedResources
    )

/*++

    Routine Description:

        This routine is called to handle any resources used by a secure device.

    Arguments:

        MasterDevice - Supplies a handle to the master device object.

        DeviceContext - Supplies a pointer to the context.

        RawResources - Supplies a pointer to the raw resources.

        TranslatedResources - Supplies a pointer to the translated resources.

    Return Value:

        NTSTATUS code.

--*/

{

    UNREFERENCED_PARAMETER(MasterDevice);
    UNREFERENCED_PARAMETER(RawResources);
    UNREFERENCED_PARAMETER(TranslatedResources);

    return STATUS_SUCCESS;
}

_Use_decl_annotations_
NTSTATUS
TreeSampleReleaseHardwareSecureEnvironment(
    WDFDEVICE MasterDevice,
    WDFCMRESLIST TranslatedResources
    )

/*++

    Routine Description:

        This routine is called to handle the displosal of any resources used by
        a secure device.

    Arguments:

        MasterDevice - Supplies a handle to the master device object.

        DeviceContext - Supplies a pointer to the context.

        TranslatedResources - Supplies a pointer to the translated resources.

    Return Value:

        NTSTATUS code.

--*/

{

    PAGED_CODE();

    UNREFERENCED_PARAMETER(MasterDevice);
    UNREFERENCED_PARAMETER(TranslatedResources);

    return STATUS_SUCCESS;
}

_Use_decl_annotations_
NTSTATUS
TreeSampleConnectSecureEnvironment(
    WDFDEVICE MasterDevice
    )

/*++

    Routine Description:

        This routine is called when the secure environment should be prepared
        for use either the first time or after a possible power state change.

        This routine must be marked as pagable, since it is called during power
        state transition.

    Arguments:

        MasterDevice - Supplies a handle to the master device object.

        DeviceContext - Supplies a pointer to the context.

    Return Value:

        NTSTATUS code.

--*/

{

    UNREFERENCED_PARAMETER(MasterDevice);

    return STATUS_SUCCESS;
}

_Use_decl_annotations_
NTSTATUS
TreeSampleDisconnectSecureEnvironment(
    WDFDEVICE MasterDevice
    )

/*++

    Routine Description:

        This routine is called when the secure environment should be prepared
        for a possible power state change.

        This routine must be marked as pagable, since it is called during power
        state transition.

    Arguments:

        MasterDevice - Supplies a handle to the master device object.

        DeviceContext - Supplies a pointer to the context.

    Return Value:

        NTSTATUS code.

--*/

{

    UNREFERENCED_PARAMETER(MasterDevice);

    return STATUS_SUCCESS;
}

_Use_decl_annotations_
NTSTATUS
TreeSampleEnumerateSecureServices(
    WDFDEVICE MasterDevice,
    ULONG Index,
    PUCHAR SecureServiceDescription,
    ULONG *DescriptionSize
    )

/*++

    Routine Description:

        This routine is called to enumerate the supported secure services
        provided by this secure device. The zero-based Index parameter is used
        to determine which secure service information is being requested for.
        If Index is larger than the number of available services then
        STATUS_NO_MORE_ENTRIES is returned to indicate the end of the list has
        been reached.

    Arguments:

        MasterDevice - Supplies a handle to the master device object.

        DeviceContext - Supplies a pointer to the context.

        Index - Supplies the zero-based index for the secure service whose
                description is being requested.

        SecureServiceDescription - Supplies a pointer to a buffer to hold the
                                   secure service description. The description
                                   is of type TR_SECURE_SERVICE.

        DescriptionSize - Supplies a pointer to the size in bytes of
                          SecureServiceDescription on input, and holds the
                          number of bytes required on output.

    Return Value:

        NTSTATUS code.

--*/

{

    ULONG DescriptionSizeRequired;
    PTR_SECURE_SERVICE SecureService;
    PTR_SECURE_SERVICE_EXTENSION ServiceExtension;
    NTSTATUS Status;

    PAGED_CODE();

    UNREFERENCED_PARAMETER(MasterDevice);

    SecureService = (PTR_SECURE_SERVICE)SecureServiceDescription;
    switch (Index) {
    case 0:
        //
        // [0] Test service
        // Major version = 1
        // Minor version = 0
        // No OS dependencies
        // Open to all except restricted code (default)
        //
        DescriptionSizeRequired = FIELD_OFFSET(TR_SECURE_SERVICE, Dependencies);
        if (*DescriptionSize < DescriptionSizeRequired) {
            Status = STATUS_BUFFER_TOO_SMALL;
            goto TreeSampleEnumerateSecureServicesEnd;
        }

        SecureService->DescriptionSize = DescriptionSizeRequired;
        SecureService->ServiceGuid = GUID_SAMPLE_TEST_SERVICE;
        SecureService->MajorVersion = 1;
        SecureService->MinorVersion = 0;
        SecureService->ExtensionOffset = 0;
        SecureService->CountDependencies = 0;

        Status = STATUS_SUCCESS;
        break;

    case 1:
        //
        // [1] Test2 service
        // Major version = 1
        // Minor version = 0
        // OS dependency on GUID_ECHO_SERVICE and GUID_KERNEL_MEMORY_SERVICE
        // Open to all including restricted code
        //
        DescriptionSizeRequired = FIELD_OFFSET(TR_SECURE_SERVICE, Dependencies) +
                                  sizeof(TR_SECURE_DEPENDENCY_V1) * 2 +
                                  sizeof(TR_SECURE_SERVICE_EXTENSION) +
                                  sizeof(SDDL_SAMPLE_TEST2_SERVICE);

        if (*DescriptionSize < DescriptionSizeRequired) {
            Status = STATUS_BUFFER_TOO_SMALL;
            goto TreeSampleEnumerateSecureServicesEnd;
        }

        SecureService->DescriptionSize = DescriptionSizeRequired;
        SecureService->ServiceGuid = GUID_SAMPLE_TEST2_SERVICE;
        SecureService->MajorVersion = 1;
        SecureService->MinorVersion = 0;
        SecureService->ExtensionOffset = FIELD_OFFSET(TR_SECURE_SERVICE, Dependencies) +
                                         sizeof(TR_SECURE_DEPENDENCY_V1) * 2;
        SecureService->CountDependencies = 2;
        SecureService->Dependencies[0].Type = TRSecureOSDependency;
        SecureService->Dependencies[0].Id = GUID_ECHO_SERVICE;
        SecureService->Dependencies[0].MaxRequired = 1;
        SecureService->Dependencies[1].Type = TRSecureOSDependency;
        SecureService->Dependencies[1].Id = GUID_KERNEL_MEMORY_SERVICE;
        SecureService->Dependencies[1].MaxRequired = 1;
        ServiceExtension = (PTR_SECURE_SERVICE_EXTENSION)(
                                ((ULONG_PTR)SecureService) +
                                SecureService->ExtensionOffset);

        ServiceExtension->ExtensionVersion = TR_SECURE_SERVICE_EXTENSION_VERSION;
        ServiceExtension->SecurityDescriptorStringOffset = SecureService->ExtensionOffset +
                                                           sizeof(TR_SECURE_SERVICE_EXTENSION);
        RtlCopyMemory((PVOID)(((ULONG_PTR)SecureService) + ServiceExtension->SecurityDescriptorStringOffset),
                      SDDL_SAMPLE_TEST2_SERVICE,
                      sizeof(SDDL_SAMPLE_TEST2_SERVICE));

        Status = STATUS_SUCCESS;
        break;

    default:
        DescriptionSizeRequired = 0;
        Status = STATUS_NO_MORE_ENTRIES;
        break;
    }

TreeSampleEnumerateSecureServicesEnd:
    *DescriptionSize = DescriptionSizeRequired;
    return Status;
}

_Use_decl_annotations_
VOID
TreeSampleProcessOtherDeviceIo(
    WDFDEVICE MasterDevice,
    WDFREQUEST Request
    )

/*++

    Routine Description:

        This routine is called when an unrecognized IO request is made to the
        device. This can be used to process private calls directly to the
        secure device.

    Arguments:

        MasterDevice - Supplies a handle to the master device object.

        DeviceContext - Supplies a pointer to the context.

        Request - Supplies a pointer to the WDF request object.

    Return Value:

        NTSTATUS code.

--*/

{

    PWCHAR Buffer;
    size_t BufferSize;
    ULONG BytesWritten;
    WDF_REQUEST_PARAMETERS Parameters;
    NTSTATUS Status;

    PAGED_CODE();

    UNREFERENCED_PARAMETER(MasterDevice);

    WDF_REQUEST_PARAMETERS_INIT(&Parameters);
    WdfRequestGetParameters(Request, &Parameters);
    BytesWritten = 0;
    switch (Parameters.Parameters.DeviceIoControl.IoControlCode) {
    case IOCTL_SAMPLE_DBGPRINT:
        Status = WdfRequestRetrieveInputBuffer(Request,
                                               0,
                                               (PVOID*)&Buffer,
                                               &BufferSize);

        if (!NT_SUCCESS(Status)) {
            goto TreeSampleProcessOtherDeviceIoEnd;
        }

        //
        // Must be NULL-terminated
        //
        if (Buffer[BufferSize / sizeof(WCHAR) - 1] != L'\0') {
            Status = STATUS_INVALID_PARAMETER;
            goto TreeSampleProcessOtherDeviceIoEnd;
        }

        DbgPrintEx(DPFLTR_DEFAULT_ID,
                   DPFLTR_ERROR_LEVEL,
                   "[TrEEMiniportSample] %ws\n",
                   (PWSTR)Buffer);

        break;

    default:
        Status = STATUS_INVALID_DEVICE_REQUEST;
    }

TreeSampleProcessOtherDeviceIoEnd:
    WdfRequestCompleteWithInformation(Request, Status, BytesWritten);
}

_Use_decl_annotations_
PTR_SECURE_SERVICE_CALLBACKS
TreeSampleQueryServiceCallbacks(
    WDFDEVICE MasterDevice,
    LPGUID ServiceGuid
    )

/*++

    Routine Description:

        This routine is called when an unrecognized IO request is made to the
        device. This can be used to process private calls directly to the
        secure device.

    Arguments:

        MasterDevice - Supplies a handle to the master device object.

        DeviceContext - Supplies a pointer to the context.

        Request - Supplies a pointer to the WDF request object.

    Return Value:

        NTSTATUS code.

--*/

{

    PTR_SECURE_SERVICE_CALLBACKS ServiceCallbacks;

    PAGED_CODE();

    UNREFERENCED_PARAMETER(MasterDevice);

    if (IsEqualGUID(ServiceGuid, &GUID_SAMPLE_TEST_SERVICE)) {

        ServiceCallbacks = &TestServiceCallbacks;

    } else if (IsEqualGUID(ServiceGuid, &GUID_SAMPLE_TEST2_SERVICE)) {

        ServiceCallbacks = &Test2ServiceCallbacks;

    } else {

        ServiceCallbacks = NULL;
    }

    return ServiceCallbacks;
}