static NTSTATUS EmulatedAddDevices( IN PVOID Context, IN HANDLE Key, IN PCHAR Name ) { PEMULATED_DEVICE Entry = *(PEMULATED_DEVICE *)Context; PCHAR Class = Entry->Class; PANSI_STRING Alias; NTSTATUS status; status = RtlStringCchPrintfA(Entry->Device, MAXIMUM_DEVICE_NAME_LENGTH, "%s", Name); ASSERT(NT_SUCCESS(status)); status = RegistryQuerySzValue(Key, Name, &Alias); if (!NT_SUCCESS(status)) goto fail1; status = RtlStringCchPrintfA(Entry->Alias, MAXIMUM_ALIAS_LENGTH, "%s", Alias[0].Buffer); ASSERT(NT_SUCCESS(status)); RegistryFreeSzValue(Alias); Entry++; status = RtlStringCchPrintfA(Entry->Class, MAXIMUM_CLASS_NAME_LENGTH, "%s", Class); ASSERT(NT_SUCCESS(status)); *(PEMULATED_DEVICE *)Context = Entry; return STATUS_SUCCESS; fail1: return status; }
NTSTATUS #pragma prefast(suppress:28152) // Does not clear DO_DEVICE_INITIALIZING AddDevice( IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT PhysicalDeviceObject ) { HANDLE ParametersKey; PWCHAR DeviceID; UNICODE_STRING Unicode; ANSI_STRING Name; PANSI_STRING Type; NTSTATUS status; ASSERT3P(DriverObject, ==, __DriverGetDriverObject()); ParametersKey = __DriverGetParametersKey(); if (ParametersKey == NULL) goto done; status = __DriverQueryId(PhysicalDeviceObject, BusQueryDeviceID, &DeviceID); if (!NT_SUCCESS(status)) goto fail1; RtlInitUnicodeString(&Unicode, DeviceID); status = RtlUnicodeStringToAnsiString(&Name, &Unicode, TRUE); if (!NT_SUCCESS(status)) goto fail2; status = RegistryQuerySzValue(ParametersKey, Name.Buffer, &Type); if (NT_SUCCESS(status)) { status = FdoCreate(PhysicalDeviceObject, &Name, Type); if (!NT_SUCCESS(status)) goto fail3; RegistryFreeSzValue(Type); } RtlFreeAnsiString(&Name); ExFreePool(DeviceID); done: return STATUS_SUCCESS; fail3: Error("fail3\n"); RegistryFreeSzValue(Type); RtlFreeAnsiString(&Name); fail2: Error("fail2\n"); ExFreePool(DeviceID); fail1: Error("fail1 (%08x)\n", status); return status; }
NTSTATUS RegistryQuerySystemStartOption( IN PCHAR Prefix, OUT PANSI_STRING *Value ) { UNICODE_STRING Unicode; HANDLE Key; PANSI_STRING Ansi; ULONG Length; PCHAR Option; PCHAR Context; NTSTATUS status; RtlInitUnicodeString(&Unicode, L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control"); status = RegistryOpenKey(NULL, &Unicode, KEY_READ, &Key); if (!NT_SUCCESS(status)) goto fail1; status = RegistryQuerySzValue(Key, "SystemStartOptions", &Ansi); if (!NT_SUCCESS(status)) goto fail2; // SystemStartOptions is a space separated list of options. // Scan it looking for the one we want. Length = (ULONG)strlen(Prefix); Option = __strtok_r(Ansi[0].Buffer, " ", &Context); if (strncmp(Prefix, Option, Length) == 0) goto found; while ((Option = __strtok_r(NULL, " ", &Context)) != NULL) if (strncmp(Prefix, Option, Length) == 0) goto found; status = STATUS_OBJECT_NAME_NOT_FOUND; goto fail3; found: *Value = __RegistryAllocate(sizeof (ANSI_STRING) * 2); status = STATUS_NO_MEMORY; if (*Value == NULL) goto fail4; Length = (ULONG)strlen(Option); (*Value)[0].MaximumLength = (USHORT)(Length + 1) * sizeof (CHAR); (*Value)[0].Buffer = __RegistryAllocate((*Value)[0].MaximumLength); status = STATUS_NO_MEMORY; if ((*Value)[0].Buffer == NULL) goto fail5; RtlCopyMemory((*Value)[0].Buffer, Option, Length * sizeof (CHAR)); (*Value)[0].Length = (USHORT)Length * sizeof (CHAR); RegistryFreeSzValue(Ansi); ZwClose(Key); return STATUS_SUCCESS; fail5: __RegistryFree(*Value); fail4: fail3: RegistryFreeSzValue(Ansi); fail2: ZwClose(Key); fail1: return status; }
NTSTATUS EmulatedUpdate( IN PXENFILT_EMULATED_INTERFACE Interface, IN PCHAR Alias ) { PXENFILT_EMULATED_CONTEXT Context = Interface->Context; PEMULATED_DEVICE Entry; HANDLE ServiceKey; HANDLE StatusKey; LONG Count; LONG Index; PANSI_STRING Old; PANSI_STRING New; ULONG Length; NTSTATUS status; if (Context->Table == NULL) goto done; for (Entry = Context->Table; strlen(Entry->Alias) != 0; Entry++) { if (strcmp(Entry->Alias, Alias) == 0) break; } if (strlen(Entry->Alias) == 0) goto done; Info("%s %s\n", Entry->Class, Entry->Device); Entry->Present = TRUE; status = RegistryOpenServiceKey(KEY_ALL_ACCESS, &ServiceKey); if (!NT_SUCCESS(status)) goto fail1; status = RegistryOpenSubKey(ServiceKey, "Status", KEY_ALL_ACCESS, &StatusKey); if (!NT_SUCCESS(status)) goto fail2; status = RegistryQuerySzValue(StatusKey, Entry->Class, &Old); if (!NT_SUCCESS(status)) Old = NULL; Count = 0; for (Index = 0; Old != NULL && Old[Index].Buffer != NULL; Index++) Count++; New = __EmulatedAllocate(sizeof (ANSI_STRING) * (Count + 2)); status = STATUS_NO_MEMORY; if (New == NULL) goto fail3; for (Index = 0; Index < Count; Index++) { Length = Old[Index].Length; New[Index].MaximumLength = (USHORT)Length + sizeof (CHAR); New[Index].Buffer = __EmulatedAllocate(New[Index].MaximumLength); status = STATUS_NO_MEMORY; if (New[Index].Buffer == NULL) goto fail4; RtlCopyMemory(New[Index].Buffer, Old[Index].Buffer, Length); New[Index].Length = (USHORT)Length; } Length = (ULONG)strlen(Entry->Device); New[Count].MaximumLength = (USHORT)Length + sizeof (CHAR); New[Count].Buffer = __EmulatedAllocate(New[Count].MaximumLength); status = STATUS_NO_MEMORY; if (New[Count].Buffer == NULL) goto fail5; RtlCopyMemory(New[Count].Buffer, Entry->Device, Length); New[Count].Length = (USHORT)Length; status = RegistryUpdateSzValue(StatusKey, Entry->Class, REG_MULTI_SZ, New); if (!NT_SUCCESS(status)) goto fail6; RegistryFreeSzValue(Old); for (Index = 0; Index < Count + 1; Index++) __EmulatedFree(New[Index].Buffer); __EmulatedFree(New); RegistryCloseKey(StatusKey); RegistryCloseKey(ServiceKey); done: return STATUS_SUCCESS; fail6: Error("fail6\n"); __EmulatedFree(New[Count].Buffer); fail5: Error("fail5\n"); Index = Count; fail4: Error("fail4\n"); while (--Index >= 0) __EmulatedFree(New[Index].Buffer); __EmulatedFree(New); fail3: Error("fail3\n"); RegistryFreeSzValue(Old); RegistryCloseKey(StatusKey); fail2: Error("fail2\n"); RegistryCloseKey(ServiceKey); fail1: Error("fail1 (%08x)\n", status); return status; }
NTSTATUS #pragma prefast(suppress:28152) // Does not clear DO_DEVICE_INITIALIZING AddDevice( IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT DeviceObject ) { HANDLE ParametersKey; PANSI_STRING ActiveDeviceInstance; BOOLEAN Active; PWCHAR DeviceID; PWCHAR InstanceID; UNICODE_STRING Unicode; ULONG Length; NTSTATUS status; ASSERT3P(DriverObject, ==, __DriverGetDriverObject()); ParametersKey = __DriverGetParametersKey(); ActiveDeviceInstance = NULL; if (ParametersKey != NULL) { status = RegistryQuerySzValue(ParametersKey, "ActiveDeviceInstance", &ActiveDeviceInstance); ASSERT(IMPLY(!NT_SUCCESS(status), ActiveDeviceInstance == NULL)); } else { ActiveDeviceInstance = NULL; } Active = FALSE; DeviceID = NULL; InstanceID = NULL; RtlZeroMemory(&Unicode, sizeof (UNICODE_STRING)); if (ActiveDeviceInstance == NULL) goto done; status = __DriverQueryId(DeviceObject, BusQueryDeviceID, &DeviceID); if (!NT_SUCCESS(status)) goto fail1; status = __DriverQueryId(DeviceObject, BusQueryInstanceID, &InstanceID); if (!NT_SUCCESS(status)) goto fail2; status = RtlAnsiStringToUnicodeString(&Unicode, ActiveDeviceInstance, TRUE); if (!NT_SUCCESS(status)) goto fail3; Length = (ULONG)wcslen(DeviceID); if (_wcsnicmp(Unicode.Buffer, DeviceID, Length) != 0) goto done; Length = (ULONG)wcslen(InstanceID); if (_wcsnicmp(Unicode.Buffer + (Unicode.Length / sizeof (WCHAR)) - Length, InstanceID, Length) != 0) goto done; Active = TRUE; RegistryFreeSzValue(ActiveDeviceInstance); done: if (Unicode.Buffer != NULL) RtlFreeUnicodeString(&Unicode); if (InstanceID != NULL) ExFreePool(InstanceID); if (DeviceID != NULL) ExFreePool(DeviceID); status = FdoCreate(DeviceObject, Active); if (!NT_SUCCESS(status)) goto fail3; return STATUS_SUCCESS; fail3: if (InstanceID != NULL) ExFreePool(InstanceID); fail2: if (DeviceID != NULL) ExFreePool(DeviceID); fail1: RegistryFreeSzValue(ActiveDeviceInstance); Error("fail1 (%08x)\n", status); return status; }
static FORCEINLINE VOID __UnplugNics( IN PXENFILT_UNPLUG_CONTEXT Context ) { HANDLE UnplugKey; PANSI_STRING ServiceNames; ULONG Index; HANDLE ServiceKey; KIRQL Irql; NTSTATUS status; UnplugKey = DriverGetUnplugKey(); ServiceKey = NULL; ServiceNames = NULL; status = RegistryQuerySzValue(UnplugKey, "NICS", &ServiceNames); if (!NT_SUCCESS(status)) goto done; for (Index = 0; ServiceNames[Index].Buffer != NULL; Index++) { PANSI_STRING ServiceName = &ServiceNames[Index]; CHAR ServiceKeyName[sizeof (SERVICES_KEY "\\XXXXXXXX")]; ULONG Count; status = RtlStringCbPrintfA(ServiceKeyName, sizeof (ServiceKeyName), SERVICES_KEY "\\%Z", ServiceName); ASSERT(NT_SUCCESS(status)); status = RegistryOpenSubKey(NULL, ServiceKeyName, KEY_READ, &ServiceKey); if (!NT_SUCCESS(status)) goto done; status = RegistryQueryDwordValue(ServiceKey, "Count", &Count); if (NT_SUCCESS(status)) { if (Count == 0) goto done; } RegistryCloseKey(ServiceKey); ServiceKey = NULL; } AcquireHighLock(&Context->Lock, &Irql); ASSERT(!Context->UnpluggedNics); __UnplugNicsLocked(); Context->UnpluggedNics = TRUE; ReleaseHighLock(&Context->Lock, Irql); done: if (ServiceKey != NULL) RegistryCloseKey(ServiceKey); if (ServiceNames != NULL) RegistryFreeSzValue(ServiceNames); }