Skip to content

Serial recovery decrypt_region_inplace() uses variable length arrays #2631

@stindaNXP

Description

@stindaNXP
static int
decrypt_region_inplace(struct enc_key_data  *enc_data,
                       const struct flash_area *fap,
                       struct image_header *hdr,
                       uint32_t off, uint32_t sz)
{
    uint32_t bytes_copied;
    int chunk_sz;
    int rc;
    uint32_t tlv_off;
    size_t blk_off;
    uint16_t idx;
    uint32_t blk_sz;
    uint8_t buf[sz] __attribute__((aligned)); //<<<<<<<<<<<<<<<<<<<
    assert(sz <= sizeof buf);                 //<<<<<<<<<<<<<<<<<<<

There are several problems with this approach. IAR and Keil doesn't enable variable length arrays by default, so it compiles with the error "Error[Pe028]: expression must have a constant value". It is also forbidden by several safety standarts like MISRA.

The sz argument should match sector size which leads to stack overflow risk. If you consider that there are flashless devices what could use a flash memory with sector size like 256kB...

assert check is not enough as it is valid only during development state.

I think it woul be a good idea to rewrite it to static buffer with a size of flash page rather than a sector.

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