You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Dangerous VLA allocation In stsafea_frame_receive() #53
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.
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 !
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.
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.
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.
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.
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.
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.
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.
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?
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.
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.
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) !
Activity
nils-cercariolo-st commentedon Jan 6, 2026
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 !
parmi93 commentedon Jan 7, 2026
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 == 0is 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.myorbit commentedon Jan 19, 2026
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:
How this upper bound is backed into memory could then be an implementation choice, for example:
This would also be beneficial for our safety-related application because it improves determinism, makes worst-case usage analyzable and avoids hidden dynamic allocations.
TofMassilia13320 commentedon Jan 21, 2026
@myorbit
It's plan to redesign the STSELib architecture to remove VLA in coming months.
We'll come back with a schedule.
[services/frame_transfer] added upper bound for received length STMic…
[doc] added i2c buffer size in stse_conf STMicroelectronics#53
nils-cercariolo-st commentedon Jan 23, 2026
@parmi93 I proposed an upper bound check for now as long as long as VLA is there.
parmi93 commentedon Jan 23, 2026
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 severalreturn ret;statements immediately after calls topSTSE->io.BusRecvStart()andpSTSE->io.BusRecvContinue(), skipping the call topSTSE->io.BusRecvStop(). Does this mean that the call tostse_platform_i2c_receive_stop()is not always guaranteed to happen? Is this behavior intentional? The documentation forstse_platform_i2c_receive_stop()is not very clear on this point.Finally, I noticed that in
03_LIBRARY_CONFIGURATION.mdthe suggested buffer length is755for 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
nils-cercariolo-st commentedon Jan 26, 2026
#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.
parmi93 commentedon Jan 26, 2026
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.
STSELib/services/stsafea/stsafea_commands.h
Lines 50 to 53 in c196d23
nils-cercariolo-st commentedon Jan 27, 2026
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 ?
TofMassilia13320 commentedon Jan 28, 2026
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.
parmi93 commentedon Jan 28, 2026
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_Axxxshould 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.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?
TofMassilia13320 commentedon Jan 28, 2026
Indeed I didn't express myself clearly.
Length
STSAFEA_MAXIMUM_CMD_RSP_LENGTH_Axxxdefines 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.
parmi93 commentedon Jan 28, 2026
Now I get it: at first, in my head, I took the name
STSAFEA_MAXIMUM_CMD_RSP_LENGTH_Axxxto 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.TofMassilia13320 commentedon Jan 28, 2026
What do you think about
STSAFEA_MAX_FRAME_LENGTH_Axxx?parmi93 commentedon Jan 29, 2026
STSAFEA_MAX_FRAME_LENGTH_Axxxis even betternils-cercariolo-st commentedon Feb 5, 2026
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) !
TofMassilia13320 commentedon Feb 12, 2026
Modification done through this commit
Integrated inside v1.1.7 release