Skip to content

Dangerous VLA allocation In stsafea_frame_receive() #53

Closed

Description

@parmi93

A Variable Length Array (VLA) is allocated based on the response received from the STSAFE chip via I2C. However, no sanity check is performed on the response before the allocation. If the response is corrupted, for instance, due to a bit-flip caused by noise on the I2C line, it could trigger a VLA allocation of up to 65 KiB. This would almost certainly lead to a stack overflow and an application crash.

PLAT_UI8 filler[filler_size];

Activity

  1. nils-cercariolo-st commented on Jan 6, 2026

    @nils-cercariolo-st
    Contributor

    Thanks ! That's indeed a great catch, we'll review it and provide a fix. We should perform a sanity check with some bounds, we could take the upper bound as the STSAFE-AXXX I2C max buffer size. Feel free to share your thoughts if you think you have a better solution !

  2. parmi93 commented on Jan 7, 2026

    @parmi93
    ContributorAuthor

    I am not very familiar with the architectural design of STSELib, so it is difficult for me to suggest an appropriate upper bound, especially since I was not able to clearly understand the purpose of filler_size. That said, some form of sanity check would still seem advisable.

    In addition, I noticed that filler_size == 0 is considered a valid value, as noted in the comment above. It is worth mentioning that, in standard C, defining a VLA with zero elements results in undefined behavior, whereas in the GCC dialect, zero-length arrays are permitted. For this reason, it might be helpful if the STSELib documentation could clarify which language standard was used to develop the library (I assume "GCC dialect" was used), so that users can clearly understand which C compilers are supported, particularly since the library states that it works across "various host MCU/MPU ecosystems", and not all of these ecosystems rely on GCC as their compiler.

  3. myorbit commented on Jan 19, 2026

    @myorbit

    Thanks for the discussion so far. I would like to add some context from my integration scenario.

    Although STSELib is primarily validated on STM32 MCUs, I'm integrating it on another MCU family/toolchain. In this environment (C11 dialect), VLAs are not allocated on the stack but are emulated by the run-time support (RTS) library using malloc/free with a relatively large fixed internal block size (~32 KiB). While this is functionally correct, it introduces avoidable heap usage and non-deterministic behavior on memory-constrained embedded targets.

    You (@nils-cercariolo-st) mentioned the idea of defining an upper bound (e.g. a maximum I2C buffer size). Knowing a strict upper bound would allow memory to be managed explicitly rather than implicitly.

    Based on that the requirement would be:

    • All temporary buffers used by the library should have a well-defined upper bound known at compile time, so that memory usage is explicit and predictable.

    How this upper bound is backed into memory could then be an implementation choice, for example:

    • a fixed-size buffer (internal or caller provided), or
    • application-managed backing storage (e.g. a fixed-block memory pool)

    This would also be beneficial for our safety-related application because it improves determinism, makes worst-case usage analyzable and avoids hidden dynamic allocations.

  4. TofMassilia13320 commented on Jan 21, 2026

    @TofMassilia13320
    Contributor

    @myorbit
    It's plan to redesign the STSELib architecture to remove VLA in coming months.
    We'll come back with a schedule.

  5. nils-cercariolo-st commented on Jan 23, 2026

    @nils-cercariolo-st
    Contributor

    @parmi93 I proposed an upper bound check for now as long as long as VLA is there.

  6. parmi93 commented on Jan 23, 2026

    @parmi93
    ContributorAuthor

    I think this fix could be fine.
    I wonder whether pSTSE->io.BusRecvStop() should be called to signal the driver to stop receiving. I also noticed that there are several return ret; statements immediately after calls to pSTSE->io.BusRecvStart() and pSTSE->io.BusRecvContinue(), skipping the call to pSTSE->io.BusRecvStop(). Does this mean that the call to stse_platform_i2c_receive_stop() is not always guaranteed to happen? Is this behavior intentional? The documentation for stse_platform_i2c_receive_stop() is not very clear on this point.

    Finally, I noticed that in 03_LIBRARY_CONFIGURATION.md the suggested buffer length is 755 for the STSAFE-A120. What is the suggested value for the STSAFE-A110? In our project we are using the latter.
    nils-cercariolo-st@fd0ee3b#diff-9430a388f3e31a85ec1d80c5ab7094277f54a397f0e0269abb3b2483a169dabeR118

  7. nils-cercariolo-st commented on Jan 26, 2026

    @nils-cercariolo-st
    Contributor

    #58 Updated PR for the VLA allocation

    In the context of the STSE platform abstraction, BusRecvStop doesn't have to be called to stop receiving. The behavior is intentional and safe.

    In stse_platform_i2c_receive_start, the library performs the full i2c_read() and stores the result in an internal RAM buffer (I2c_buffer). By the time BusRecvStart returns, the physical I2C bus has already sent the STOP bit and is idle.

    BusRecvContinue and BusRecvStop are purely logical operations that perform memcpy from that internal RAM buffer.

  8. parmi93 commented on Jan 26, 2026

    @parmi93
    ContributorAuthor

    I noticed @TofMassilia13320 comment in the PR #58.
    This means that the I2C driver can define the buffer size using the values of the defines STSAFEA_MAXIMUM_CMD_RSP_LENGTH_Axxx + 3?
    Note that the I2C driver uses the same buffer both to transmit commands and to receive responses, so if the user defines the buffer size as STSAFEA_MAXIMUM_CMD_RSP_LENGTH_A100 + 3 (for the STSAFE-A110), then the maximum size of transmitted commands is also limited.
    I wonder whether this is the correct approach.

    #define STSAFEA_MAXIMUM_CMD_RSP_LENGTH_A100 507U
    #define STSAFEA_MAXIMUM_CMD_RSP_LENGTH_A110 507U
    #define STSAFEA_MAXIMUM_CMD_RSP_LENGTH_A120 752U
    #define STSAFEA_MAXIMUM_CMD_RSP_LENGTH_A200 507U

  9. nils-cercariolo-st commented on Jan 27, 2026

    @nils-cercariolo-st
    Contributor

    Defining the static PLAT_UI8 I2c_buffer[755U]; with your max cmd_rsp_length device specific indeed is the correct approach. In a really resource constrained environment where you know the maximum command length you need, you could even trim it more. Otherwise I do agree it's better to take the maximum + 3 (2 bytes needed for response length + 1 byte for command or response header).

    I think we can make it more clear in the documentation to point towards the stsafe_commands to give a clearer idea to the user of how to tune this buffer.

    @TofMassilia13320 What do you think about it ?

  10. TofMassilia13320 commented on Jan 28, 2026

    @TofMassilia13320
    Contributor

    Of course, if this point is unclear, it must be clarified in the documentation.

    The MW STSELib abstract layer approach consists of making the Platform layer completely independent of the MW.

    The MW has defined lengths (STSAFEA_MAXIMUM_CMD_RSP_LENGTH_Axxx) based on the used product in order to limit overflows on the RAM buffer of the Platform layer's I2C communication.

    These lengths define the maximum size of commands (in bytes) intrinsic to each STSAFE-A product line.
    Setting the I2C platform buffer to this maximum value (+3 bytes as mentioned) ensures full command coverage of the selected STSAFE-A without overflow at the I2C platform layer.

    However, if the application does not need to cover all the commands range of an STSAFE-A product, the size of the I2C platform RAM buffer can be adapted to the size of the most byte-intensive command of the application (plus +3 bytes) like @nils-cercariolo-st mentionned. This approach offers the possibility of limiting the impact on memory in cases where it is not necessary to cover all the commands range of an STSAFE-A product line.

  11. parmi93 commented on Jan 28, 2026

    @parmi93
    ContributorAuthor

    These lengths define the maximum size of commands (in bytes) intrinsic to each STSAFE-A product line.
    Setting the I2C platform buffer to this maximum value (+3 bytes as mentioned) ensures full command coverage of the selected STSAFE-A used without overflow at the I2C platform layer.

    I think this part could be copied and pasted into the documentation; it clarified all of my doubts!
    Side note: the name of the define STSAFEA_MAXIMUM_CMD_RSP_LENGTH_Axxx should be improved. The current name suggests to the user that it represents the maximum length of a response, whereas it actually also represents the maximum length of the commands accepted by the STSAFE-Axxx when sent by the host.

    the size of the I2C platform RAM buffer can be adapted to the size of the most byte-intensive command of the application

    From what I have been able to see, there is currently no documentation indicating the length of the commands and their corresponding responses, so the user cannot adapt (reduce) the I2C buffer length to the most byte-intensive command. Is there any documentation available on this?

  12. TofMassilia13320 commented on Jan 28, 2026

    @TofMassilia13320
    Contributor

    Indeed I didn't express myself clearly.
    Length STSAFEA_MAXIMUM_CMD_RSP_LENGTH_Axxx defines the maximum size of command and/or response (in bytes) intrinsic to each STSAFE-A product line.
    So, @parmi93, current name suggests to the user that it represents the maximum length of a most byte-intensive command or response.

  13. parmi93 commented on Jan 28, 2026

    @parmi93
    ContributorAuthor

    Now I get it: at first, in my head, I took the name STSAFEA_MAXIMUM_CMD_RSP_LENGTH_Axxx to mean the maximum length of a command response, but it’s actually meant to be read as the maximum length of a command or a response.
    To make this clearer, the name could be improved to something like STSAFEA_MAX_CMD_AND_RSP_LENGTH_Axxx (or similar), so the meaning is less ambiguous.

  14. TofMassilia13320 commented on Jan 28, 2026

    @TofMassilia13320
    Contributor

    What do you think about STSAFEA_MAX_FRAME_LENGTH_Axxx ?

  15. parmi93 commented on Jan 29, 2026

    @parmi93
    ContributorAuthor

    STSAFEA_MAX_FRAME_LENGTH_Axxx is even better

  16. nils-cercariolo-st commented on Feb 5, 2026

    @nils-cercariolo-st
    Contributor

    As an update, we're currently working on it (requires slightly more testing) and hopefully this will be reflected by next release. Also, feel free to reach me if you would like more support/discussion/documentation, directly by email (on my profile) !

  17. TofMassilia13320 commented on Feb 12, 2026

    @TofMassilia13320
    Contributor

    Modification done through this commit
    Integrated inside v1.1.7 release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

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