I am working on a large embedded system (STM32F423 CPU and CubeIDE environment on Windows, all the code is in C), and recently I found out that there must be a buffer overflow somewhere. The mbedtls library reports an error suggesting the server did not respond in the required time. The thing is that when I comment one function I added recently (completely unrelated, and not even executed before the error occurs), everything works fine. I think the presence of the function causes the memory arrangement to shift, and some other code has a buffer overflow bug, that overwrites some variable in the mbedtls library. Mbedtls then sends some invalid data to the server, and the server does not respond. That's my guess.
I tried updating mbedtls to the newest version. I also tried to find problematic pointer operations by:
- adding various compiler flags:
-Wabsolute-value
,-Warray-bounds
,-Wformat-overflow
,-Wstringpop-overflow
-fbounds-check
- this does not seem to do anything, because the compiled code has the same size with and without it,- adding access attribute to most functions that take a pointer parameter,
- adding
-fsanitize=address
to the compiler options.
The last thing is problematic, because it triggers the compiler error:
arm-none-eabi-gcc: fatal error: cannot read spec file 'libsanitizer.spec': No such file or directory
Do you know how to add the sanitizer support to the GCC in the CubeIDE toolchain?
I have also got a suggestion to use git bisect, and I am yet going to try it, although my hopes are not high, because most likely the bug has been introduced a long time ago, and the revision where it was introduced will work fine, because of different memory arrangement.
Also having the sanitizer would help finding other pointer issues I might have and not know about.