xtimer: Fix race condition in xtimer_msg_receive_timeout [backport 2021.04]#16376
Merged
kaspar030 merged 1 commit intoRIOT-OS:2021.04-branchfrom Apr 26, 2021
Merged
Conversation
3f65b75 to
9bb21e2
Compare
(cherry picked from commit dee65e7)
9bb21e2 to
30f4486
Compare
maribu
approved these changes
Apr 26, 2021
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.
Backport of #16374
Contribution description
This PR fixes a rare race-condition in
xtimer_msg_receive_timeoutwhich can lead to corruption of the timer list and subsequent hard faults.The race condition is triggered when:
msg_send.xtimerwhich sents the timeout message expires and executes beforextimer_remove(t)is called. This will cause the message queue for the thread to contain first a real message and second the timeout message. The timer will not be queued anymore, but the timeout message will still be in the queue.xtimer_msg_receive_timeoutfunction is called again. This will queue a new xtimer while the timeout message of the previous timer is still in the buffer._msg_waitwill see the old timeout message, think that the current xtimer has already expired and will not remove the timer. Whenxtimer_msg_receive_timeoutreturns, the timer will still be queued. However, as it is allocated on the stack it is no longer valid. This causes thetimer_list_headto now point to invalid memory. Crashes ensue.Testing procedure
This bug was found, validated, and fixed using a proprietary application. I have not written a separate example application which exhibits the problem which I could publish.