Skip to content

Commit

Permalink
Allocate NX memory on Windows 8 and forward
Browse files Browse the repository at this point in the history
  • Loading branch information
dontech committed Sep 17, 2019
1 parent 355e2db commit 9afd01f
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 15 deletions.
6 changes: 3 additions & 3 deletions libusb/src/driver/driver_registry.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -373,4 +373,4 @@ VOID set_filter_interface_key(libusb_device_t *dev, ULONG id)
USBERR0("IoOpenDeviceInterfaceRegistryKey failed\n");
}
}
}
}
7 changes: 3 additions & 4 deletions libusb/src/driver/get_descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions libusb/src/driver/libusb_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
1 change: 1 addition & 0 deletions libusb/src/driver/libusb_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion libusb/src/driver/set_configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion libusb/src/driver/set_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
11 changes: 5 additions & 6 deletions libusb/src/driver/transfer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 9afd01f

Please sign in to comment.