-
Notifications
You must be signed in to change notification settings - Fork 7.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mbedtls Dynamic Port - Memory leak (IDFGH-13555) #14444
Comments
Thank you for the issue @HikingDev, |
…if#14444) - Added rx_buffer_len() to handle correct RX buffer size - Ensured compliance with mbedtls 16KB RX buffer requirement - Prevents CORRUPT HEAP error when receiving large data (>16KB) over HTTPS
@HikingDev Is it possible to share the entire error log in this case? Actually, I think the issue (and its solution) may not be as straightforward as it seems to be. Can you please provide detailed log with log level |
@AdityaHPatwardhan the issue is, that as documented in mbedtls, the client cannot be informed about the Maximum incoming fragment size. /** \def MBEDTLS_SSL_IN_CONTENT_LEN
*
* Maximum length (in bytes) of incoming plaintext fragments.
*
* This determines the size of the incoming TLS I/O buffer in such a way
* that it is capable of holding the specified amount of plaintext data,
* regardless of the protection mechanism used.
*
* \note When using a value less than the default of 16KB on the client, it is
* recommended to use the Maximum Fragment Length (MFL) extension to
* inform the server about this limitation. On the server, there
* is no supported, standardized way of informing the client about
* restriction on the maximum size of incoming messages, and unless
* the limitation has been communicated by other means, it is recommended
* to only change the outgoing buffer size #MBEDTLS_SSL_OUT_CONTENT_LEN
* while keeping the default value of 16KB for the incoming buffer.
*
* Uncomment to set the maximum plaintext size of the incoming I/O buffer.
*/
//#define MBEDTLS_SSL_IN_CONTENT_LEN 16384 So indeed the in_msglen is used for dynamic buffer allocation but in case of a read (in a server application) the client cannot be informed that MBEDTLS_SSL_IN_CONTENT_LEN is lower than the required 16386 Bytes. I cannot set mbedtls to verbose. Since its a heap corruption, the crash doesnt happen immediatley but in a later mbedtls_ssl_read and the verbose loglevel causes too much output, floods the terminal and causes a crash. EDIT: I can try to make a simple server application, allowing to reproduce the faulty behavior. For now we use a patched version, implementing the suggested pull request. This indeed fixes our crashes. |
Ahh, I see. |
Indeed! The client doesn`t care about our buffer size.
Also interesting read the pull request to add dynamic buffers pull request dynamic buffers mbedtls Only for the in_buf we cannot save RAM. However, reducing it for the out_buf we can reduce memory usage. |
Hi @HikingDev Sorry for the delayed reply. |
Hi @AdityaHPatwardhan , |
Can you please provide a demo example where I can reproduce the issue at my end ? |
@HikingDev The thing is that, we can't accept #14614 as it is as it drastically reduces the heap saved by the dynamic_buffers feature for mbedtls. |
@AdityaHPatwardhan i understand the issue. |
Hi @HikingDev any update on the reproducible code? |
still havent found the time to make a testcase, allowing to reproduce the issue. |
Hi @AdityaHPatwardhan, I am unable to apply your patch. |
The patch was created for idf master branch. |
Unfortunately, I was unable to apply the patch to the current master branch, and I suspect this might be due to changes in the branch since the patch was created. Could you kindly provide the commit hash of the master branch that you based the patch on? I appreciate your help! |
Hi @HikingDev, I am still able to apply the patch on esp-idf. Also, |
Hi @AdityaHPatwardhan , its a little akward but i downloaded again your patch and it differs from the one i previously downloaded. |
Answers checklist.
IDF version.
v5.2-dev-3903-g66992aca7a
Espressif SoC revision.
ESP32 (revision: v3.0)
Operating System used.
Windows
How did you build your project?
Command line with idf.py
If you are using Windows, please specify command line type.
PowerShell
Development Kit.
ESP32-Wrover-Kit
Power Supply used.
External 5V
What is the expected behavior?
Running an a https Webserver with dynamic rx/tx buffers and using a Client to send Data > 16KB to the ESP32 HTTPS Server without crashing with "CORRUPT HEAP" message
What is the actual behavior?
The firmware crashes withe a "CORRUPT HEAP" message
Steps to reproduce.
Debug Logs.
More Information.
Currently the esp mmbedtls dynamic implementation uses a function tx_buffer_len(...) in both
esp_mbedtls_add_tx_buffer and esp_mbedtls_add_rx_buffer to get the buffer length used in mbedtls_calloc.
If Asymmetric in/out fragment length is chosen to reduce the size of buffers the RX buffer gets the MBEDTLS_SSL_OUT_BUFFER_LEN regardless of the config value in sdkonfig. If MBEDTLS_SSL_OUT_BUFFER_LEN is set to e.g. 4096 or if there is already data in the buffer, the RX buffer is always smaller than the by mbedtls required 16KB for the RX buffer. The documentation specifically states that the incoming buffer should be kept at 16KB, due to the fact that there is no supported way of informing the client about size restriction for incoming messagens. (See mbedtls Source
mbedtls_config.h )
There were discussions and a pull request about the issues in mbedtls:
dynamic ssl buffers
Mandate of 16kb
Currently the espressif dynamic implementation disregards the mandatory 16kb rx buffer size. I suggest adding a rx_buffer_len function:
and using it in esp_mbedtls_add_rx_buffer
The text was updated successfully, but these errors were encountered: