[PATCH 1/5] setupapi: Enforce a maximum device instance ID length.

Zebediah Figura z.figura12 at gmail.com
Tue Nov 27 19:55:34 CST 2018


Signed-off-by: Zebediah Figura <z.figura12 at gmail.com>
---
 dlls/setupapi/devinst.c       | 14 +++++++++-----
 dlls/setupapi/tests/devinst.c | 20 +++++++++++++++++++-
 2 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/dlls/setupapi/devinst.c b/dlls/setupapi/devinst.c
index 64c04131b0..8ce1344cda 100644
--- a/dlls/setupapi/devinst.c
+++ b/dlls/setupapi/devinst.c
@@ -1337,7 +1337,7 @@ HKEY WINAPI SetupDiCreateDevRegKeyW(HDEVINFO devinfo, SP_DEVINFO_DATA *device_da
 /***********************************************************************
  *              SetupDiCreateDeviceInfoA (SETUPAPI.@)
  */
-BOOL WINAPI SetupDiCreateDeviceInfoA(HDEVINFO DeviceInfoSet, PCSTR DeviceName,
+BOOL WINAPI SetupDiCreateDeviceInfoA(HDEVINFO DeviceInfoSet, const char *name,
         const GUID *ClassGuid, PCSTR DeviceDescription, HWND hwndParent, DWORD CreationFlags,
         PSP_DEVINFO_DATA DeviceInfoData)
 {
@@ -1345,11 +1345,15 @@ BOOL WINAPI SetupDiCreateDeviceInfoA(HDEVINFO DeviceInfoSet, PCSTR DeviceName,
     LPWSTR DeviceNameW = NULL;
     LPWSTR DeviceDescriptionW = NULL;
 
-    if (DeviceName)
+    if (!name || strlen(name) >= MAX_DEVICE_ID_LEN)
     {
-        DeviceNameW = MultiByteToUnicode(DeviceName, CP_ACP);
-        if (DeviceNameW == NULL) return FALSE;
+        SetLastError(ERROR_INVALID_DEVINST_NAME);
+        return FALSE;
     }
+
+    DeviceNameW = MultiByteToUnicode(name, CP_ACP);
+    if (DeviceNameW == NULL) return FALSE;
+
     if (DeviceDescription)
     {
         DeviceDescriptionW = MultiByteToUnicode(DeviceDescription, CP_ACP);
@@ -1407,7 +1411,7 @@ BOOL WINAPI SetupDiCreateDeviceInfoW(HDEVINFO devinfo, PCWSTR DeviceName,
             devinfo, debugstr_w(DeviceName), debugstr_guid(ClassGuid), debugstr_w(DeviceDescription),
             hwndParent, CreationFlags, device_data);
 
-    if (!DeviceName)
+    if (!DeviceName || strlenW(DeviceName) >= MAX_DEVICE_ID_LEN)
     {
         SetLastError(ERROR_INVALID_DEVINST_NAME);
         return FALSE;
diff --git a/dlls/setupapi/tests/devinst.c b/dlls/setupapi/tests/devinst.c
index 0e6a1c0a4e..338393e5f2 100644
--- a/dlls/setupapi/tests/devinst.c
+++ b/dlls/setupapi/tests/devinst.c
@@ -271,8 +271,8 @@ static void test_device_info(void)
 {
     static const GUID deadbeef = {0xdeadbeef,0xdead,0xbeef,{0xde,0xad,0xbe,0xef,0xde,0xad,0xbe,0xef}};
     SP_DEVINFO_DATA device = {0}, ret_device = {sizeof(ret_device)};
+    char id[MAX_DEVICE_ID_LEN + 2];
     HDEVINFO set;
-    char id[50];
     BOOL ret;
 
     SetLastError(0xdeadbeef);
@@ -382,6 +382,24 @@ todo_wine {
     check_device_info(set, 2, &guid, "ROOT\\LEGACY_BOGUS\\testguid");
     check_device_info(set, 3, NULL, NULL);
 
+    memset(id, 'x', sizeof(id));
+    memcpy(id, "Root\\LEGACY_BOGUS\\", strlen("Root\\LEGACY_BOGUS\\"));
+    id[MAX_DEVICE_ID_LEN + 1] = 0;
+    SetLastError(0xdeadbeef);
+    ret = SetupDiCreateDeviceInfoA(set, id, &guid, NULL, NULL, 0, NULL);
+    ok(!ret, "Expected failure.\n");
+    ok(GetLastError() == ERROR_INVALID_DEVINST_NAME, "Got unexpected error %#x.\n", GetLastError());
+
+    id[MAX_DEVICE_ID_LEN] = 0;
+    SetLastError(0xdeadbeef);
+    ret = SetupDiCreateDeviceInfoA(set, id, &guid, NULL, NULL, 0, NULL);
+    ok(!ret, "Expected failure.\n");
+    ok(GetLastError() == ERROR_INVALID_DEVINST_NAME, "Got unexpected error %#x.\n", GetLastError());
+
+    id[MAX_DEVICE_ID_LEN - 1] = 0;
+    ret = SetupDiCreateDeviceInfoA(set, id, &guid, NULL, NULL, 0, NULL);
+    ok(ret, "Failed to create device, error %#x.\n", GetLastError());
+
     SetupDiDestroyDeviceInfoList(set);
 }
 
-- 
2.14.1




More information about the wine-devel mailing list