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
/*
 * COPYRIGHT:       See COPYING in the top level directory
 * PROJECT:         ReactOS Kernel Streaming
 * FILE:            drivers/wdm/audio/legacy/wdmaud/sup.c
 * PURPOSE:         Misc support routines
 * PROGRAMMER:      Andrew Greenwood
 *                  Johannes Anderwald
 */

#include "wdmaud.h"

#include <stdio.h>

#define NDEBUG
#include <debug.h>

#define TAG_WDMAUD 'DMDW'

PVOID
AllocateItem(
    IN POOL_TYPE PoolType,
    IN SIZE_T NumberOfBytes)
{
    PVOID Item = ExAllocatePoolWithTag(PoolType, NumberOfBytes, TAG_WDMAUD);
    if (!Item)
        return Item;

    RtlZeroMemory(Item, NumberOfBytes);
    return Item;
}

VOID
FreeItem(
    IN PVOID Item)
{
    ExFreePool(Item);
}



ULONG
GetSysAudioDeviceCount(
    IN  PDEVICE_OBJECT DeviceObject)
{
    PWDMAUD_DEVICE_EXTENSION DeviceExtension;
    KSPROPERTY Pin;
    ULONG Count, BytesReturned;
    NTSTATUS Status;

    /* setup the query request */
    Pin.Set = KSPROPSETID_Sysaudio;
    Pin.Id = KSPROPERTY_SYSAUDIO_DEVICE_COUNT;
    Pin.Flags = KSPROPERTY_TYPE_GET;

    DeviceExtension = (PWDMAUD_DEVICE_EXTENSION)DeviceObject->DeviceExtension;

    /* query sysaudio for the device count */
    Status = KsSynchronousIoControlDevice(DeviceExtension->FileObject, KernelMode, IOCTL_KS_PROPERTY, (PVOID)&Pin, sizeof(KSPROPERTY), (PVOID)&Count, sizeof(ULONG), &BytesReturned);
    if (!NT_SUCCESS(Status))
        return 0;

    return Count;
}


NTSTATUS
SetIrpIoStatus(
    IN PIRP Irp,
    IN NTSTATUS Status,
    IN ULONG Length)
{
    Irp->IoStatus.Information = Length;
    Irp->IoStatus.Status = Status;
    IoCompleteRequest(Irp, IO_NO_INCREMENT);
    return Status;

}

ULONG
ClosePin(
    IN  PWDMAUD_CLIENT ClientInfo,
    IN  ULONG FilterId,
    IN  ULONG PinId,
    IN  SOUND_DEVICE_TYPE DeviceType)
{
    ULONG Index;

    for(Index = 0; Index < ClientInfo->NumPins; Index++)
    {
        if (ClientInfo->hPins[Index].FilterId == FilterId && ClientInfo->hPins[Index].PinId == PinId && ClientInfo->hPins[Index].Handle && ClientInfo->hPins[Index].Type == DeviceType)
        {
            if (ClientInfo->hPins[Index].Type != MIXER_DEVICE_TYPE)
            {
                ZwClose(ClientInfo->hPins[Index].Handle);
            }
            ClientInfo->hPins[Index].Handle = NULL;
            return Index;
        }
    }
    return MAXULONG;
}

NTSTATUS
InsertPinHandle(
    IN  PWDMAUD_CLIENT ClientInfo,
    IN  ULONG FilterId,
    IN  ULONG PinId,
    IN  SOUND_DEVICE_TYPE DeviceType,
    IN  HANDLE PinHandle,
    IN  ULONG FreeIndex)
{
    PWDMAUD_HANDLE Handles;

    if (FreeIndex != MAXULONG)
    {
        /* re-use a free index */
        ClientInfo->hPins[FreeIndex].Handle = PinHandle;
        ClientInfo->hPins[FreeIndex].FilterId = FilterId;
        ClientInfo->hPins[FreeIndex].PinId = PinId;
        ClientInfo->hPins[FreeIndex].Type = DeviceType;

        return STATUS_SUCCESS;
    }

    Handles = AllocateItem(NonPagedPool, sizeof(WDMAUD_HANDLE) * (ClientInfo->NumPins+1));

    if (!Handles)
        return STATUS_INSUFFICIENT_RESOURCES;

    if (ClientInfo->NumPins)
    {
        RtlMoveMemory(Handles, ClientInfo->hPins, sizeof(WDMAUD_HANDLE) * ClientInfo->NumPins);
        FreeItem(ClientInfo->hPins);
    }

    ClientInfo->hPins = Handles;
    ClientInfo->hPins[ClientInfo->NumPins].Handle = PinHandle;
    ClientInfo->hPins[ClientInfo->NumPins].Type = DeviceType;
    ClientInfo->hPins[ClientInfo->NumPins].FilterId = FilterId;
    ClientInfo->hPins[ClientInfo->NumPins].PinId = PinId;
    ClientInfo->NumPins++;

    return STATUS_SUCCESS;
}

PKEY_VALUE_PARTIAL_INFORMATION
ReadKeyValue(
    IN HANDLE hSubKey,
    IN PUNICODE_STRING KeyName)
{
    NTSTATUS Status;
    ULONG Length;
    PKEY_VALUE_PARTIAL_INFORMATION PartialInformation;

    /* now query MatchingDeviceId key */
    Status = ZwQueryValueKey(hSubKey, KeyName, KeyValuePartialInformation, NULL, 0, &Length);

    /* check for success */
    if (Status != STATUS_BUFFER_TOO_SMALL)
        return NULL;

    /* allocate a buffer for key data */
    PartialInformation = AllocateItem(NonPagedPool, Length);

    if (!PartialInformation)
        return NULL;


    /* now query MatchingDeviceId key */
    Status = ZwQueryValueKey(hSubKey, KeyName, KeyValuePartialInformation, PartialInformation, Length, &Length);

    /* check for success */
    if (!NT_SUCCESS(Status))
    {
        FreeItem(PartialInformation);
        return NULL;
    }

    if (PartialInformation->Type != REG_SZ)
    {
        /* invalid key type */
        FreeItem(PartialInformation);
        return NULL;
    }

    return PartialInformation;
}


NTSTATUS
CompareProductName(
    IN HANDLE hSubKey,
    IN LPWSTR PnpName,
    IN ULONG ProductNameSize,
    OUT LPWSTR ProductName)
{
    PKEY_VALUE_PARTIAL_INFORMATION PartialInformation;
    UNICODE_STRING DriverDescName = RTL_CONSTANT_STRING(L"DriverDesc");
    UNICODE_STRING MatchingDeviceIdName = RTL_CONSTANT_STRING(L"MatchingDeviceId");
    ULONG Length;
    LPWSTR DeviceName;

    /* read MatchingDeviceId value */
    PartialInformation = ReadKeyValue(hSubKey, &MatchingDeviceIdName);

    if (!PartialInformation)
        return STATUS_UNSUCCESSFUL;


    /* extract last '&' */
    DeviceName = wcsrchr((LPWSTR)PartialInformation->Data, L'&');
    ASSERT(DeviceName);
    /* terminate it */
    DeviceName[0] = L'\0';

    Length = wcslen((LPWSTR)PartialInformation->Data);

    DPRINT("DeviceName %S PnpName %S Length %u\n", (LPWSTR)PartialInformation->Data, PnpName, Length);

    if (_wcsnicmp((LPWSTR)PartialInformation->Data, &PnpName[4], Length))
    {
        FreeItem(PartialInformation);
        return STATUS_NO_MATCH;
    }

    /* free buffer */
    FreeItem(PartialInformation);

    /* read DriverDescName value */
    PartialInformation = ReadKeyValue(hSubKey, &DriverDescName);

    if (!PartialInformation)
    {
        /* failed to read driver desc key */
        return STATUS_UNSUCCESSFUL;
    }

    /* copy key name */
    Length = min(ProductNameSize * sizeof(WCHAR), PartialInformation->DataLength);
    RtlMoveMemory(ProductName, (PVOID)PartialInformation->Data, Length);

    /* zero terminate it */
    ProductName[ProductNameSize-1] = L'\0';

    /* free buffer */
    FreeItem(PartialInformation);

    return STATUS_SUCCESS;
}



NTSTATUS
FindProductName(
    IN LPWSTR PnpName,
    IN ULONG ProductNameSize,
    OUT LPWSTR ProductName)
{
    UNICODE_STRING KeyName = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\Class\\{4D36E96C-E325-11CE-BFC1-08002BE10318}");

    UNICODE_STRING SubKeyName;
    WCHAR SubKey[20];
    OBJECT_ATTRIBUTES ObjectAttributes;
    HANDLE hKey, hSubKey;
    NTSTATUS Status;
    ULONG Length, Index;
    PKEY_FULL_INFORMATION KeyInformation;

    for(Index = 0; Index < wcslen(PnpName); Index++)
    {
        if (PnpName[Index] == '#')
            PnpName[Index] = L'\\';
    }


    /* initialize key attributes */
    InitializeObjectAttributes(&ObjectAttributes, &KeyName, OBJ_CASE_INSENSITIVE | OBJ_OPENIF, NULL, NULL);

    /* open the key */
    Status = ZwOpenKey(&hKey, GENERIC_READ, &ObjectAttributes);

    /* check for success */
    if (!NT_SUCCESS(Status))
        return Status;

    /* query num of subkeys */
    Status = ZwQueryKey(hKey, KeyFullInformation, NULL, 0, &Length);

    if (Status != STATUS_BUFFER_TOO_SMALL)
    {
        DPRINT1("ZwQueryKey failed with %x\n", Status);
        /* failed */
        ZwClose(hKey);
        return Status;
    }

    /* allocate key information struct */
    KeyInformation = AllocateItem(NonPagedPool, Length);
    if (!KeyInformation)
    {
        /* no memory */
        ZwClose(hKey);
        return STATUS_INSUFFICIENT_RESOURCES;
    }

    /* query num of subkeys */
    Status = ZwQueryKey(hKey, KeyFullInformation, (PVOID)KeyInformation, Length, &Length);

    if (!NT_SUCCESS(Status))
    {
        DPRINT1("ZwQueryKey failed with %x\n", Status);
        FreeItem(KeyInformation);
        ZwClose(hKey);
        return Status;
    }

    /* now iterate through all subkeys */
    for(Index = 0; Index < KeyInformation->SubKeys; Index++)
    {
        /* subkeys are always in the format 0000-XXXX */
        swprintf(SubKey, L"%04u", Index);

        /* initialize subkey name */
        RtlInitUnicodeString(&SubKeyName, SubKey);

        /* initialize key attributes */
        InitializeObjectAttributes(&ObjectAttributes, &SubKeyName, OBJ_CASE_INSENSITIVE | OBJ_OPENIF, hKey, NULL);

        /* open the sub key */
        Status = ZwOpenKey(&hSubKey, GENERIC_READ, &ObjectAttributes);

        /* check for success */
        if (NT_SUCCESS(Status))
        {
            /* compare product name */
            Status = CompareProductName(hSubKey, PnpName, ProductNameSize, ProductName);

            /* close subkey */
            ZwClose(hSubKey);

            if (NT_SUCCESS(Status))
                break;
        }
    }

    /* free buffer */
    FreeItem(KeyInformation);

    /* close key */
    ZwClose(hKey);

    /* no matching key found */
    return Status;
}

NTSTATUS
GetSysAudioDevicePnpName(
    IN  PDEVICE_OBJECT DeviceObject,
    IN  ULONG DeviceIndex,
    OUT LPWSTR * Device)
{
    ULONG BytesReturned;
    KSP_PIN Pin;
    NTSTATUS Status;
    PWDMAUD_DEVICE_EXTENSION DeviceExtension;

   /* first check if the device index is within bounds */
   if (DeviceIndex >= GetSysAudioDeviceCount(DeviceObject))
       return STATUS_INVALID_PARAMETER;

    /* setup the query request */
    Pin.Property.Set = KSPROPSETID_Sysaudio;
    Pin.Property.Id = KSPROPERTY_SYSAUDIO_DEVICE_INTERFACE_NAME;
    Pin.Property.Flags = KSPROPERTY_TYPE_GET;
    Pin.PinId = DeviceIndex;

    DeviceExtension = (PWDMAUD_DEVICE_EXTENSION)DeviceObject->DeviceExtension;

    /* query sysaudio for the device path */
    Status = KsSynchronousIoControlDevice(DeviceExtension->FileObject, KernelMode, IOCTL_KS_PROPERTY, (PVOID)&Pin, sizeof(KSPROPERTY) + sizeof(ULONG), NULL, 0, &BytesReturned);

    /* check if the request failed */
    if (Status != STATUS_BUFFER_TOO_SMALL || BytesReturned == 0)
        return STATUS_UNSUCCESSFUL;

    /* allocate buffer for the device */
    *Device = AllocateItem(NonPagedPool, BytesReturned);<--- Null pointer dereference
    if (!Device)<--- Assuming that condition '!Device' is not redundant
        return STATUS_INSUFFICIENT_RESOURCES;

    /* query sysaudio again for the device path */
    Status = KsSynchronousIoControlDevice(DeviceExtension->FileObject, KernelMode, IOCTL_KS_PROPERTY, (PVOID)&Pin, sizeof(KSPROPERTY) + sizeof(ULONG), (PVOID)*Device, BytesReturned, &BytesReturned);

    if (!NT_SUCCESS(Status))
    {
        /* failed */
        FreeItem(*Device);
        return Status;
    }

    return Status;
}

NTSTATUS
OpenDevice(
    IN LPWSTR Device,
    OUT PHANDLE DeviceHandle,
    OUT PFILE_OBJECT * FileObject)
{
    NTSTATUS Status;
    HANDLE hDevice;

    /* now open the device */
    Status = WdmAudOpenSysAudioDevice(Device, &hDevice);

    if (!NT_SUCCESS(Status))
    {
        return Status;
    }

    *DeviceHandle = hDevice;

    if (FileObject)
    {
        Status = ObReferenceObjectByHandle(hDevice, FILE_READ_DATA | FILE_WRITE_DATA, *IoFileObjectType, KernelMode, (PVOID*)FileObject, NULL);

        if (!NT_SUCCESS(Status))
        {
            ZwClose(hDevice);
        }
    }

    return Status;

}