Skip to content

Commit c55f8ff

Browse files
committed
Merge pull request #1216 from pguyot/w26/fix-buffer-overflow-code-all-available
Fix buffer overflow in code:all_available/0 implementation Crash was triggered with eunit tests and gcc-13 on ubuntu-24.04. These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents eb406a8 + 978062f commit c55f8ff

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/libAtomVM/nifs.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4485,14 +4485,15 @@ static void *nif_code_all_available_fold(void *accum, const void *section_ptr, u
44854485

44864486
struct CodeAllAvailableAcc *acc = (struct CodeAllAvailableAcc *) accum;
44874487
size_t section_name_len = strlen(section_name);
4488-
if (section_name_len < 260) {
4489-
if (memcmp(".beam", section_name + section_name_len - 5, 5) == 0) {
4488+
if (flags & BEAM_CODE_FLAG && section_name_len > 5 && section_name_len < 260) {
4489+
size_t module_name_len = section_name_len - 5;
4490+
if (memcmp(".beam", section_name + module_name_len, 5) == 0) {
44904491
bool loaded;
44914492
if (acc->avmpack_data->in_use) {
44924493
// Check if module is loaded
4493-
char atom_str[section_name_len - 5];
4494-
atom_str[0] = section_name_len - 5;
4495-
memcpy(atom_str + 1, section_name, atom_str[0]);
4494+
char atom_str[module_name_len + 1];
4495+
atom_str[0] = module_name_len;
4496+
memcpy(atom_str + 1, section_name, module_name_len);
44964497
Module *loaded_module = globalcontext_get_module(acc->ctx->global, (AtomString) &atom_str);
44974498
loaded = loaded_module != NULL;
44984499
} else {
@@ -4502,7 +4503,7 @@ static void *nif_code_all_available_fold(void *accum, const void *section_ptr, u
45024503
acc->acc_count++;
45034504
if (!term_is_invalid_term(acc->result)) {
45044505
term module_tuple = term_alloc_tuple(3, &acc->ctx->heap);
4505-
term_put_tuple_element(module_tuple, 0, term_from_const_binary(section_name, section_name_len - 5, &acc->ctx->heap, acc->ctx->global));
4506+
term_put_tuple_element(module_tuple, 0, term_from_const_binary(section_name, module_name_len, &acc->ctx->heap, acc->ctx->global));
45064507
term_put_tuple_element(module_tuple, 1, UNDEFINED_ATOM);
45074508
term_put_tuple_element(module_tuple, 2, FALSE_ATOM);
45084509
acc->result = term_list_prepend(module_tuple, acc->result, &acc->ctx->heap);

0 commit comments

Comments
 (0)