Skip to content

Commit 04d553d

Browse files
RHEL-183408 viostor: fix overflow in bump allocator bounds checks
Signed-off-by: rachael-george <rgeorge@redhat.com>
1 parent b14d5bc commit 04d553d

2 files changed

Lines changed: 26 additions & 17 deletions

File tree

viostor/virtio_pci.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,23 @@ static void WriteVirtIODeviceWord(ULONG_PTR ulRegister, u16 wValue)
122122
static void *mem_alloc_contiguous_pages(void *context, size_t size)
123123
{
124124
PADAPTER_EXTENSION adaptExt = (PADAPTER_EXTENSION)context;
125-
PVOID ptr = (PVOID)((ULONG_PTR)adaptExt->pageAllocationVa + adaptExt->pageOffset);
125+
SIZE_T pageOffset = adaptExt->pageOffset;
126+
SIZE_T pageSize = adaptExt->pageAllocationSize;
127+
SIZE_T alignedSize;
128+
PVOID ptr;
126129

127-
if ((adaptExt->pageOffset + size) <= adaptExt->pageAllocationSize)
128-
{
129-
size = ROUND_TO_PAGES(size);
130-
adaptExt->pageOffset += size;
131-
RtlZeroMemory(ptr, size);
132-
return ptr;
133-
}
134-
else
130+
alignedSize = ROUND_TO_PAGES(size);
131+
132+
if (alignedSize < size || alignedSize > pageSize || pageOffset > pageSize - alignedSize)
135133
{
136134
RhelDbgPrint(TRACE_LEVEL_FATAL, " Ran out of memory in (%Id)\n", size);
137135
return NULL;
138136
}
137+
138+
ptr = (PVOID)((ULONG_PTR)adaptExt->pageAllocationVa + pageOffset);
139+
adaptExt->pageOffset = (ULONG)(pageOffset + alignedSize);
140+
RtlZeroMemory(ptr, alignedSize);
141+
return ptr;
139142
}
140143

141144
static void mem_free_contiguous_pages(void *context, void *virt)

viostor/virtio_stor.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2464,17 +2464,23 @@ PVOID
24642464
VioStorPoolAlloc(IN PVOID DeviceExtension, IN SIZE_T size)
24652465
{
24662466
PADAPTER_EXTENSION adaptExt = (PADAPTER_EXTENSION)DeviceExtension;
2467-
PVOID ptr = (PVOID)((ULONG_PTR)adaptExt->poolAllocationVa + adaptExt->poolOffset);
2467+
SIZE_T poolOffset = adaptExt->poolOffset;
2468+
SIZE_T poolSize = adaptExt->poolAllocationSize;
2469+
SIZE_T alignedSize;
2470+
PVOID ptr;
24682471

2469-
if ((adaptExt->poolOffset + size) <= adaptExt->poolAllocationSize)
2472+
alignedSize = ROUND_TO_CACHE_LINES(size);
2473+
2474+
if (alignedSize < size || alignedSize > poolSize || poolOffset > poolSize - alignedSize)
24702475
{
2471-
size = ROUND_TO_CACHE_LINES(size);
2472-
adaptExt->poolOffset += (ULONG)size;
2473-
RtlZeroMemory(ptr, size);
2474-
return ptr;
2476+
RhelDbgPrint(TRACE_LEVEL_FATAL, "Ran out of memory in VioStorPoolAlloc(%Id)\n", size);
2477+
return NULL;
24752478
}
2476-
RhelDbgPrint(TRACE_LEVEL_FATAL, "Ran out of memory in VioStorPoolAlloc(%Id)\n", size);
2477-
return NULL;
2479+
2480+
ptr = (PVOID)((ULONG_PTR)adaptExt->poolAllocationVa + poolOffset);
2481+
adaptExt->poolOffset = (ULONG)(poolOffset + alignedSize);
2482+
RtlZeroMemory(ptr, alignedSize);
2483+
return ptr;
24782484
}
24792485

24802486
UCHAR FirmwareRequest(IN PVOID DeviceExtension, IN PSRB_TYPE Srb)

0 commit comments

Comments
 (0)