Skip to content

Commit 99f287e

Browse files
author
chengzhihao
committed
Fix heap buffer overflow
Signed-off-by: chengzhihao <[email protected]>
1 parent b706935 commit 99f287e

File tree

8 files changed

+44
-0
lines changed

8 files changed

+44
-0
lines changed

jerry-core/api/jerry-snapshot.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,6 +1582,10 @@ jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapsho
15821582
jerry_char_t *const buffer_start_p = lit_buf_p;
15831583
jerry_char_t *const buffer_end_p = lit_buf_p + lit_buf_size;
15841584

1585+
JMEM_CHECK_ARRAY_SIZE_AND_THROW(literal_count, ecma_string_t *, \
1586+
ecma_collection_destroy(lit_pool_p) \
1587+
);
1588+
15851589
JMEM_DEFINE_LOCAL_ARRAY (literal_array, literal_count, ecma_string_t *);
15861590
lit_utf8_size_t literal_idx = 0;
15871591

jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,10 @@ ecma_builtin_array_prototype_object_sort (ecma_value_t this_arg, /**< this argum
10641064

10651065
ecma_value_t ret_value = ECMA_VALUE_ERROR;
10661066
uint32_t copied_num = 0;
1067+
1068+
JMEM_CHECK_ARRAY_SIZE_AND_THROW(defined_prop_count, ecma_value_t, \
1069+
ecma_collection_free(array_index_props_p) \
1070+
);
10671071
JMEM_DEFINE_LOCAL_ARRAY (values_buffer, defined_prop_count, ecma_value_t);
10681072

10691073
ecma_value_t *buffer_p = array_index_props_p->buffer_p;

jerry-core/ecma/builtin-objects/ecma-builtin-function-prototype.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ ecma_builtin_function_prototype_object_apply (ecma_object_t *func_obj_p, /**< th
232232

233233
/* 6. */
234234
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
235+
236+
JMEM_CHECK_ARRAY_SIZE_AND_THROW(length, ecma_value_t, \
237+
);
235238
JMEM_DEFINE_LOCAL_ARRAY (arguments_list_p, length, ecma_value_t);
236239
ecma_length_t index = 0;
237240

jerry-core/ecma/builtin-objects/ecma-builtin-helpers-sort.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ ecma_builtin_helper_array_merge_sort_helper (ecma_value_t *array_p, /**< array t
8484
ecma_object_t *array_buffer_p) /**< arrayBuffer */
8585
{
8686
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
87+
88+
JMEM_CHECK_ARRAY_SIZE_AND_THROW(length, ecma_value_t, \
89+
);
8790
JMEM_DEFINE_LOCAL_ARRAY (dest_array_p, length, ecma_value_t);
8891

8992
ecma_value_t *temp_p;

jerry-core/ecma/builtin-objects/ecma-builtin-json.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,6 +1726,13 @@ ecma_builtin_json_stringify (ecma_value_t arg1, /**< value */
17261726
}
17271727
else
17281728
{
1729+
JMEM_CHECK_ARRAY_SIZE_AND_THROW(num_of_spaces, char, \
1730+
ecma_free_value(space); \
1731+
if (context.property_list_p != NULL) \
1732+
{ \
1733+
ecma_collection_free(context.property_list_p); \
1734+
} \
1735+
);
17291736
JMEM_DEFINE_LOCAL_ARRAY (space_buff, num_of_spaces, char);
17301737

17311738
memset (space_buff, LIT_CHAR_SP, (size_t) num_of_spaces);

jerry-core/ecma/builtin-objects/ecma-builtin-object.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,10 @@ ecma_builtin_object_object_define_properties (ecma_object_t *obj_p, /**< routine
832832
ecma_value_t *buffer_p = prop_names_p->buffer_p;
833833

834834
/* 4. */
835+
JMEM_CHECK_ARRAY_SIZE_AND_THROW(prop_names_p->item_count, ecma_property_descriptor_t, \
836+
ecma_collection_free(prop_names_p); \
837+
ecma_deref_object(props_p) \
838+
);
835839
JMEM_DEFINE_LOCAL_ARRAY (property_descriptors, prop_names_p->item_count, ecma_property_descriptor_t);
836840
uint32_t property_descriptor_number = 0;
837841
ecma_collection_t *enum_prop_names = ecma_new_collection ();

jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,9 @@ ecma_builtin_typedarray_prototype_sort (ecma_value_t this_arg, /**< this argumen
12451245
}
12461246

12471247
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
1248+
1249+
JMEM_CHECK_ARRAY_SIZE_AND_THROW(info_p->length, ecma_value_t, \
1250+
);
12481251
JMEM_DEFINE_LOCAL_ARRAY (values_buffer, info_p->length, ecma_value_t);
12491252

12501253
uint32_t buffer_index = 0;

jerry-core/jmem/jmem.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#include "jrt.h"
2020

21+
#include <limits.h>
22+
2123
/** \addtogroup mem Memory allocation
2224
* @{
2325
*
@@ -191,6 +193,20 @@ void jmem_heap_stats_print (void);
191193
jmem_cpointer_t JERRY_ATTR_PURE jmem_compress_pointer (const void *pointer_p);
192194
void *JERRY_ATTR_PURE jmem_decompress_pointer (uintptr_t compressed_pointer);
193195

196+
#if JERRY_CPOINTER_32_BIT
197+
198+
#define JMEM_CHECK_ARRAY_SIZE_AND_THROW(number, type, finalize) \
199+
if (UINT_MAX / sizeof(type) < (size_t)(number)) { \
200+
finalize; \
201+
return ecma_raise_range_error(ECMA_ERR_INVALID_ARRAY_LENGTH); \
202+
}
203+
204+
#else /* JERRY_CPOINTER_32_BIT */
205+
206+
#define JMEM_CHECK_ARRAY_SIZE_AND_THROW(number, type, finalize)
207+
208+
#endif /* JERRY_CPOINTER_32_BIT */
209+
194210
/**
195211
* Define a local array variable and allocate memory for the array on the heap.
196212
*

0 commit comments

Comments
 (0)