[VIDEOPRT]

Add missing check
Don't assume UNICODE_STRING is NULL-terminated

svn path=/trunk/; revision=51163
This commit is contained in:
Rafal Harabien 2011-03-26 17:49:55 +00:00
parent 291012914d
commit 299f6a7826

View file

@ -83,27 +83,43 @@ IntCreateRegistryPath(
static WCHAR ControlSet[] = L"CONTROLSET"; static WCHAR ControlSet[] = L"CONTROLSET";
static WCHAR Insert1[] = L"Hardware Profiles\\Current\\System\\CurrentControlSet\\"; static WCHAR Insert1[] = L"Hardware Profiles\\Current\\System\\CurrentControlSet\\";
static WCHAR Insert2[] = L"\\Device0"; static WCHAR Insert2[] = L"\\Device0";
LPWSTR ProfilePath = NULL;
BOOLEAN Valid; BOOLEAN Valid;
PWCHAR AfterControlSet; UNICODE_STRING AfterControlSet;
Valid = (0 == _wcsnicmp(DriverRegistryPath->Buffer, RegistryMachineSystem, AfterControlSet = *DriverRegistryPath;
wcslen(RegistryMachineSystem))); /* Check if path begins with \\REGISTRY\\MACHINE\\SYSTEM\\ */
Valid = (DriverRegistryPath->Length > sizeof(RegistryMachineSystem) &&
0 == _wcsnicmp(DriverRegistryPath->Buffer, RegistryMachineSystem,
wcslen(RegistryMachineSystem)));
if (Valid)
{ {
AfterControlSet = DriverRegistryPath->Buffer + wcslen(RegistryMachineSystem); AfterControlSet.Buffer += wcslen(RegistryMachineSystem);
if (0 == _wcsnicmp(AfterControlSet, CurrentControlSet, wcslen(CurrentControlSet))) AfterControlSet.Length -= sizeof(RegistryMachineSystem) - sizeof(UNICODE_NULL);
/* Check if path contains CURRENTCONTROLSET */
if (AfterControlSet.Length > sizeof(CurrentControlSet) &&
0 == _wcsnicmp(AfterControlSet.Buffer, CurrentControlSet, wcslen(CurrentControlSet)))
{ {
AfterControlSet += wcslen(CurrentControlSet); AfterControlSet.Buffer += wcslen(CurrentControlSet);
AfterControlSet.Length -= sizeof(CurrentControlSet) - sizeof(UNICODE_NULL);
} }
else if (0 == _wcsnicmp(AfterControlSet, ControlSet, wcslen(ControlSet))) /* Check if path contains CONTROLSETnum */
else if (AfterControlSet.Length > sizeof(ControlSet) &&
0 == _wcsnicmp(AfterControlSet.Buffer, ControlSet, wcslen(ControlSet)))
{ {
AfterControlSet += wcslen(ControlSet); AfterControlSet.Buffer += wcslen(ControlSet);
while (L'0' <= *AfterControlSet && L'9' <= *AfterControlSet) AfterControlSet.Length -= sizeof(ControlSet) - sizeof(UNICODE_NULL);
while (AfterControlSet.Length > 0 &&
L'0' <= *AfterControlSet.Buffer &&
L'9' <= *AfterControlSet.Buffer)
{ {
AfterControlSet++; AfterControlSet.Buffer++;
AfterControlSet.Length -= sizeof(WCHAR);
} }
Valid = (L'\\' == *AfterControlSet); Valid = (AfterControlSet.Length > 0 && L'\\' == *AfterControlSet.Buffer);
AfterControlSet++; AfterControlSet.Buffer++;
AfterControlSet.Length -= sizeof(WCHAR);
AfterControlSet.MaximumLength = AfterControlSet.Length;
} }
else else
{ {
@ -113,18 +129,26 @@ IntCreateRegistryPath(
if (Valid) if (Valid)
{ {
ProfilePath = ExAllocatePoolWithTag(PagedPool, DeviceRegistryPath->MaximumLength = DriverRegistryPath->Length + sizeof(Insert1) + sizeof(Insert2);
(wcslen(DriverRegistryPath->Buffer) + DeviceRegistryPath->Buffer = ExAllocatePoolWithTag(PagedPool,
wcslen(Insert1) + wcslen(Insert2) + 1) * sizeof(WCHAR), DeviceRegistryPath->MaximumLength,
TAG_VIDEO_PORT); TAG_VIDEO_PORT);
if (NULL != ProfilePath) if (NULL != DeviceRegistryPath->Buffer)
{ {
wcsncpy(ProfilePath, DriverRegistryPath->Buffer, AfterControlSet - DriverRegistryPath->Buffer); /* Build device path */
wcscpy(ProfilePath + (AfterControlSet - DriverRegistryPath->Buffer), Insert1); wcsncpy(DeviceRegistryPath->Buffer,
wcscat(ProfilePath, AfterControlSet); DriverRegistryPath->Buffer,
wcscat(ProfilePath, Insert2); AfterControlSet.Buffer - DriverRegistryPath->Buffer);
DeviceRegistryPath->Length = (AfterControlSet.Buffer - DriverRegistryPath->Buffer) * sizeof(WCHAR);
Valid = NT_SUCCESS(RtlCheckRegistryKey(RTL_REGISTRY_ABSOLUTE, ProfilePath)); RtlAppendUnicodeToString(DeviceRegistryPath, Insert1);
RtlAppendUnicodeStringToString(DeviceRegistryPath, &AfterControlSet);
RtlAppendUnicodeToString(DeviceRegistryPath, Insert2);
/* Check if registry key exists */
Valid = NT_SUCCESS(RtlCheckRegistryKey(RTL_REGISTRY_ABSOLUTE, DriverRegistryPath->Buffer));
if(!Valid)
ExFreePoolWithTag(DeviceRegistryPath->Buffer, TAG_VIDEO_PORT);
} }
else else
{ {
@ -136,28 +160,22 @@ IntCreateRegistryPath(
WARN_(VIDEOPRT, "Unparsable registry path %wZ", DriverRegistryPath); WARN_(VIDEOPRT, "Unparsable registry path %wZ", DriverRegistryPath);
} }
if (Valid) /* If path doesn't point to *ControlSet*, use DriverRegistryPath directly */
if (!Valid)
{ {
RtlInitUnicodeString(DeviceRegistryPath, ProfilePath); DeviceRegistryPath->MaximumLength = DriverRegistryPath->Length + sizeof(Insert2);
}
else
{
if (ProfilePath)
ExFreePoolWithTag(ProfilePath, TAG_VIDEO_PORT);
DeviceRegistryPath->Length =
DeviceRegistryPath->MaximumLength =
DriverRegistryPath->Length + (9 * sizeof(WCHAR));
DeviceRegistryPath->Length -= sizeof(WCHAR);
DeviceRegistryPath->Buffer = ExAllocatePoolWithTag( DeviceRegistryPath->Buffer = ExAllocatePoolWithTag(
NonPagedPool, NonPagedPool,
DeviceRegistryPath->MaximumLength, DeviceRegistryPath->MaximumLength,
TAG_VIDEO_PORT); TAG_VIDEO_PORT);
if (!DeviceRegistryPath->Buffer) if (!DeviceRegistryPath->Buffer)
return STATUS_NO_MEMORY; return STATUS_NO_MEMORY;
swprintf(DeviceRegistryPath->Buffer, L"%s\\Device0",
DriverRegistryPath->Buffer); RtlCopyUnicodeString(DeviceRegistryPath, DriverRegistryPath);
RtlAppendUnicodeToString(DeviceRegistryPath, Insert2);
} }
return STATUS_SUCCESS; return STATUS_SUCCESS;
} }