Skip to content

Commit 8c39bb0

Browse files
committed
Fix integer overflows
1 parent e404fc2 commit 8c39bb0

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,22 @@ validatePartitions(umf_os_memory_provider_params_t *params) {
359359
return UMF_RESULT_SUCCESS;
360360
}
361361

362+
static umf_result_t os_get_min_page_size(void *provider, void *ptr,
363+
size_t *page_size);
364+
365+
static umf_result_t validatePartSize(os_memory_provider_t *provider,
366+
umf_os_memory_provider_params_t *params) {
367+
size_t page_size;
368+
os_get_min_page_size(provider, NULL, &page_size);
369+
if (ALIGN_UP(params->part_size, page_size) < params->part_size) {
370+
LOG_ERR("partition size (%zu) is too big, cannot align with a page "
371+
"size (%zu)",
372+
params->part_size, page_size);
373+
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
374+
}
375+
return UMF_RESULT_SUCCESS;
376+
}
377+
362378
static void free_bitmaps(os_memory_provider_t *provider) {
363379
for (unsigned i = 0; i < provider->nodeset_len; i++) {
364380
hwloc_bitmap_free(provider->nodeset[i]);
@@ -442,6 +458,14 @@ static umf_result_t translate_params(umf_os_memory_provider_params_t *in_params,
442458
return result;
443459
}
444460

461+
if (in_params->numa_mode == UMF_NUMA_MODE_INTERLEAVE) {
462+
result = validatePartSize(provider, in_params);
463+
if (result != UMF_RESULT_SUCCESS) {
464+
LOG_ERR("incorrect partition size: %zu", in_params->part_size);
465+
return result;
466+
}
467+
}
468+
445469
int is_dedicated_node_bind = dedicated_node_bind(in_params);
446470
provider->numa_policy =
447471
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
@@ -104,6 +104,10 @@ struct provider_malloc : public provider_base_t {
104104
align = 8;
105105
}
106106

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

0 commit comments

Comments
 (0)