Skip to content

Commit e3b79ca

Browse files
quangnh89aquynh
authored andcommitted
provide a validity check to prevent against Integer overflow conditions (#870)
* provide a validity check to prevent against Integer overflow conditions * fix some style issues.
1 parent 4b86d85 commit e3b79ca

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

windows/winkernel_mm.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "winkernel_mm.h"
55
#include <ntddk.h>
6+
#include <Ntintsafe.h>
67

78
// A pool tag for memory allocation
89
static const ULONG CS_WINKERNEL_POOL_TAG = 'kwsC';
@@ -33,8 +34,16 @@ void * CAPSTONE_API cs_winkernel_malloc(size_t size)
3334

3435
// FP; a use of NonPagedPool is required for Windows 7 support
3536
#pragma prefast(suppress : 30030) // Allocating executable POOL_TYPE memory
36-
CS_WINKERNEL_MEMBLOCK *block = (CS_WINKERNEL_MEMBLOCK *)ExAllocatePoolWithTag(
37-
NonPagedPool, size + sizeof(CS_WINKERNEL_MEMBLOCK), CS_WINKERNEL_POOL_TAG);
37+
size_t number_of_bytes = 0;
38+
CS_WINKERNEL_MEMBLOCK *block = NULL;
39+
// A specially crafted size value can trigger the overflow.
40+
// If the sum in a value that overflows or underflows the capacity of the type,
41+
// the function returns NULL.
42+
if (!NT_SUCCESS(RtlSizeTAdd(size, sizeof(CS_WINKERNEL_MEMBLOCK), &number_of_bytes))) {
43+
return NULL;
44+
}
45+
block = (CS_WINKERNEL_MEMBLOCK *)ExAllocatePoolWithTag(
46+
NonPagedPool, number_of_bytes, CS_WINKERNEL_POOL_TAG);
3847
if (!block) {
3948
return NULL;
4049
}

0 commit comments

Comments
 (0)