Skip to content

Commit 0086875

Browse files
committed
Fix integer overflows
1 parent 34d38ee commit 0086875

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

src/base_alloc/base_alloc_global.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,19 @@ void *umf_ba_global_aligned_alloc(size_t size, size_t alignment) {
155155
return NULL;
156156
}
157157

158+
if (SIZE_MAX - size < ALLOC_METADATA_SIZE) {
159+
LOG_ERR("base_alloc: allocation size (%zu) too large.", size);
160+
return NULL;
161+
}
162+
158163
// for metadata
159164
size += ALLOC_METADATA_SIZE;
160165

161166
if (alignment > ALLOC_METADATA_SIZE) {
167+
if (SIZE_MAX - size < alignment) {
168+
LOG_ERR("base_alloc: allocation size (%zu) too large.", size);
169+
return NULL;
170+
}
162171
size += alignment;
163172
}
164173

src/provider/provider_os_memory.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,15 @@ validatePartitions(umf_os_memory_provider_params_t *params) {
360360
return UMF_RESULT_SUCCESS;
361361
}
362362

363+
static umf_result_t validatePartSize(os_memory_provider_t *provider, umf_os_memory_provider_params_t *params) {
364+
size_t page_size;
365+
if (ALIGN_UP(params->part_size, os_get_min_page_size(provider, NULL, &page_size)) < params->part_size) {
366+
LOG_ERR("partition size (%zu) is too big, cannot align with a page size (%zu)", params->part_size, page_size);
367+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
368+
}
369+
return UMF_RESULT_SUCCESS;
370+
}
371+
363372
static void free_bitmaps(os_memory_provider_t *provider) {
364373
for (unsigned i = 0; i < provider->nodeset_len; i++) {
365374
hwloc_bitmap_free(provider->nodeset[i]);
@@ -443,6 +452,14 @@ static umf_result_t translate_params(umf_os_memory_provider_params_t *in_params,
443452
return result;
444453
}
445454

455+
if(in_params->numa_mode == UMF_NUMA_MODE_INTERLEAVE) {
456+
result = validatePartSize(in_params);
457+
if (result != UMF_RESULT_SUCCESS) {
458+
LOG_ERR("incorrect partition size: %zu", in_params->part_size);
459+
return result;
460+
}
461+
}
462+
446463
int is_dedicated_node_bind = dedicated_node_bind(in_params);
447464
provider->numa_policy =
448465
translate_numa_mode(in_params->numa_mode, is_dedicated_node_bind);

test/common/provider.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ struct provider_malloc : public provider_base_t {
106106
align = 8;
107107
}
108108

109+
if (SIZE_MAX - size < align) {
110+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
111+
}
112+
109113
// aligned_malloc returns a valid pointer despite not meeting the
110114
// requirement of 'size' being multiple of 'align' even though the
111115
// documentation says that it has to. AddressSanitizer returns an

0 commit comments

Comments
 (0)