Fix use-after-free in BaseObjects destructor - #335
Open
amir-al-mohamad wants to merge 1 commit into
Open
amir-al-mohamad wants to merge 1 commit into
amir-al-mohamad wants to merge 1 commit into
Conversation
~BaseObjects() called clear() on the derived class's String buffers after they had already been destroyed; with GCC -O2 they still hold the freed pointer, so String::remove() wrote into freed memory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I was chasing random heap corruption panics on an ESP32-S3 (arduino-esp32 3.x,
IDF 5.5.4) that showed up a few seconds after every Messaging::send(), usually
as tlsf asserts in the wifi or lwip tasks. With comprehensive heap poisoning
the checker reported a single zeroed byte at the start of 3-6 freed blocks
right after my Messages::Message / Notification objects went out of scope, and
a free() journal traced every one of those blocks to BaseO8::~BaseO8 and
BaseO4::~BaseO4.
The cause is in BaseObjects:
bufferspoints at theString buf[N]array ofthe derived class, and ~BaseObjects() calls clear() on it. By then the derived
members are already destroyed. GCC at -O2 drops the stores made by the inlined
~String() (dead stores into a dying object), so the Strings still hold the
freed pointer and length, and String::remove(0, len) writes a NUL into freed
memory.
Removing the clear() call from the destructor fixes it, the String members
clean up after themselves anyway. Before the change the heap got corrupted in
5 out of 5 test runs, after it in 0 out of 6.