From 9afd01fc136f1f91d94a194889886b9ebb47be1c Mon Sep 17 00:00:00 2001 From: Peter Dons Tychsen Date: Wed, 18 Sep 2019 01:37:52 +0200 Subject: [PATCH] Allocate NX memory on Windows 8 and forward --- libusb/src/driver/driver_registry.c | 6 +++--- libusb/src/driver/get_descriptor.c | 7 +++---- libusb/src/driver/libusb_driver.c | 30 +++++++++++++++++++++++++++ libusb/src/driver/libusb_driver.h | 1 + libusb/src/driver/set_configuration.c | 2 +- libusb/src/driver/set_interface.c | 2 +- libusb/src/driver/transfer.c | 11 +++++----- 7 files changed, 44 insertions(+), 15 deletions(-) diff --git a/libusb/src/driver/driver_registry.c b/libusb/src/driver/driver_registry.c index 3b0c67d..8ec14fc 100644 --- a/libusb/src/driver/driver_registry.c +++ b/libusb/src/driver/driver_registry.c @@ -110,7 +110,7 @@ bool_t reg_get_properties(libusb_device_t *dev) pool_length = sizeof(KEY_VALUE_FULL_INFORMATION) + 512; - info = ExAllocatePool(NonPagedPool, pool_length); + info = allocate_pool(pool_length); if (!info) { ZwClose(key); @@ -330,7 +330,7 @@ NTSTATUS reg_get_custom_property(PDEVICE_OBJECT device_object, { RtlInitUnicodeString(&name, (WCHAR*)(&data_buffer[name_offset])); length = sizeof(KEY_VALUE_FULL_INFORMATION) + name.MaximumLength + data_length; - info = ExAllocatePool(NonPagedPool, length); + info = allocate_pool(length); if (info) { memset(info, 0, length); @@ -373,4 +373,4 @@ VOID set_filter_interface_key(libusb_device_t *dev, ULONG id) USBERR0("IoOpenDeviceInterfaceRegistryKey failed\n"); } } -} \ No newline at end of file +} diff --git a/libusb/src/driver/get_descriptor.c b/libusb/src/driver/get_descriptor.c index 2b5172e..3c75113 100644 --- a/libusb/src/driver/get_descriptor.c +++ b/libusb/src/driver/get_descriptor.c @@ -151,7 +151,7 @@ NTSTATUS get_descriptor(libusb_device_t *dev, PUSB_CONFIGURATION_DESCRIPTOR config_desc; size = ((PUSB_CONFIGURATION_DESCRIPTOR)buffer)->wTotalLength; - if (!( config_desc = ExAllocatePool(NonPagedPool, size))) + if (!( config_desc = allocate_pool(size))) { USBERR0("memory allocation error\n"); status = STATUS_NO_MEMORY; @@ -229,8 +229,7 @@ PUSB_CONFIGURATION_DESCRIPTOR get_config_descriptor( return NULL; } - if (!(desc = ExAllocatePool(NonPagedPool, - sizeof(USB_CONFIGURATION_DESCRIPTOR)))) + if (!(desc = allocate_pool(sizeof(USB_CONFIGURATION_DESCRIPTOR)))) { USBERR0("memory allocation error\n"); return NULL; @@ -257,7 +256,7 @@ PUSB_CONFIGURATION_DESCRIPTOR get_config_descriptor( desc_size = desc->wTotalLength; ExFreePool(desc); - if (!(desc = ExAllocatePool(NonPagedPool, desc_size))) + if (!(desc = allocate_pool(desc_size))) { USBERR0("memory allocation error\n"); break; diff --git a/libusb/src/driver/libusb_driver.c b/libusb/src/driver/libusb_driver.c index 61ebf77..d4b6347 100644 --- a/libusb/src/driver/libusb_driver.c +++ b/libusb/src/driver/libusb_driver.c @@ -969,3 +969,33 @@ Return Value: return urb.FrameNumber; } + +PVOID allocate_pool(SIZE_T bytes) +{ + ULONG major; + ULONG minor; + NTSTATUS status; + RTL_OSVERSIONINFOW version; + /* Windwos 7 and below only has normal paged pool */ + POOL_TYPE pool = NonPagedPool; + + version.dwOSVersionInfoSize = sizeof(version); + status = RtlGetVersion(&version); + if(!NT_VERIFY(NT_SUCCESS(status))) + { + major = 5; + minor = 0; + } + else + { + major = version.dwMajorVersion; + minor = version.dwMinorVersion; + } + + /* Windwos 8 needs non-executable paged pool to pass verification test */ + if((major > 6) || ((major == 6) && (minor >= 2))) + { + pool = 512; /* NonPagedPoolNx - Windows 8 and up */ + } + return ExAllocatePool(pool, bytes); +} diff --git a/libusb/src/driver/libusb_driver.h b/libusb/src/driver/libusb_driver.h index e0a5d3c..f2ca21c 100644 --- a/libusb/src/driver/libusb_driver.h +++ b/libusb/src/driver/libusb_driver.h @@ -376,6 +376,7 @@ NTSTATUS large_transfer(IN libusb_device_t* dev, ULONG get_current_frame(IN PDEVICE_EXTENSION dev, IN PIRP Irp); +PVOID allocate_pool(SIZE_T bytes); NTSTATUS control_transfer(libusb_device_t* dev, PIRP irp, diff --git a/libusb/src/driver/set_configuration.c b/libusb/src/driver/set_configuration.c index 9f2fac1..2f94271 100644 --- a/libusb/src/driver/set_configuration.c +++ b/libusb/src/driver/set_configuration.c @@ -108,7 +108,7 @@ NTSTATUS set_configuration(libusb_device_t *dev, // MEMORY ALLOCATION BEGINS interfaces = - ExAllocatePool(NonPagedPool,(configuration_descriptor->bNumInterfaces + 1) + allocate_pool((configuration_descriptor->bNumInterfaces + 1) * sizeof(USBD_INTERFACE_LIST_ENTRY)); if (!interfaces) diff --git a/libusb/src/driver/set_interface.c b/libusb/src/driver/set_interface.c index 840600e..74b96eb 100644 --- a/libusb/src/driver/set_interface.c +++ b/libusb/src/driver/set_interface.c @@ -54,7 +54,7 @@ NTSTATUS set_interface(libusb_device_t *dev, tmp_size = sizeof(struct _URB_SELECT_INTERFACE) + interface_descriptor->bNumEndpoints * sizeof(USBD_PIPE_INFORMATION); - urb = ExAllocatePool(NonPagedPool, tmp_size); + urb = allocate_pool(tmp_size); if (!urb) { diff --git a/libusb/src/driver/transfer.c b/libusb/src/driver/transfer.c index 0a02aa1..eac01a7 100644 --- a/libusb/src/driver/transfer.c +++ b/libusb/src/driver/transfer.c @@ -160,7 +160,7 @@ NTSTATUS transfer(libusb_device_t* dev, USBMSG("[%s #%d] EP%02Xh length %d\n", dispTransfer, sequenceID, endpoint->address, totalLength); } - context = ExAllocatePool(NonPagedPool, sizeof(context_t)); + context = allocate_pool(sizeof(context_t)); if (!context) { @@ -297,7 +297,7 @@ static NTSTATUS create_urb(libusb_device_t *dev, URB **urb, int direction, urb_size = sizeof(struct _URB_BULK_OR_INTERRUPT_TRANSFER); } - *urb = ExAllocatePool(NonPagedPool, urb_size); + *urb = allocate_pool(urb_size); if (!*urb) { @@ -499,8 +499,7 @@ NTSTATUS large_transfer(IN libusb_device_t* dev, // before calling them down the driver stack. // subRequestContextArray = (PSUB_REQUEST_CONTEXT *) - ExAllocatePool(NonPagedPool, - numIrps * sizeof(PSUB_REQUEST_CONTEXT)); + allocate_pool(numIrps * sizeof(PSUB_REQUEST_CONTEXT)); if (subRequestContextArray == NULL) { @@ -545,7 +544,7 @@ NTSTATUS large_transfer(IN libusb_device_t* dev, // subRequestContext = (PSUB_REQUEST_CONTEXT) - ExAllocatePool(NonPagedPool, sizeof(SUB_REQUEST_CONTEXT)); + allocate_pool(sizeof(SUB_REQUEST_CONTEXT)); if (subRequestContext == NULL) { @@ -1368,7 +1367,7 @@ static NTSTATUS allocate_suburb(USHORT urbFunction, urbSize = sizeof(struct _URB_BULK_OR_INTERRUPT_TRANSFER); } - *subUrbRef = (PURB)ExAllocatePool(NonPagedPool, urbSize); + *subUrbRef = (PURB)allocate_pool(urbSize); if ((*subUrbRef) == NULL) return STATUS_INSUFFICIENT_RESOURCES;