Skip to content

Potential double-free vulnerability in j2k.c #1498

@20urc3

Description

@20urc3

Hi, If I'm not wrong there might be a potential double-free vulnerability in openjpeg/src/lib/openjp2/j2k.c

OPJ_BYTE *new_header_tile_data = (OPJ_BYTE *) opj_realloc(p_j2k->m_specific_param.m_encoder.m_header_tile_data, l_mco_size);

if (! new_header_tile_data) {
    opj_free(p_j2k->m_specific_param.m_encoder.m_header_tile_data);
    p_j2k->m_specific_param.m_encoder.m_header_tile_data = NULL;
    p_j2k->m_specific_param.m_encoder.m_header_tile_data_size = 0;
    opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to write MCO marker\n");
    return OPJ_FALSE;
}

The code is attempting to reallocate memory for the p_j2k pointer using the opj_realloc function. If the allocation is successful, then the opj_realloc call will free the old memory pointed to by p_j2k. However, the subsequent opj_free call will attempt to free the same memory that was just freed by opj_realloc. This will result in a double free vulnerability, as the memory will be freed twice and will no longer be accessible.

To fix this bug, the opj_free call should verify that p_j2k isn't null before freeing it again.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions