Possible race in mbedtls_memory_buffer_alloc_status() #302
Labels
No labels
api-spec
bug
CLA requested
CLA valid
compliance
DO NOT MERGE
duplicate
enhancement
feature branch
good first issue
HEADER FORMATTING ISSUE
help wanted
Information
invalid
Jira status: AWAITING REVIEW
Jira status: CLOSED
Jira status: DONE
Jira status: IN PROGRESS
Jira status: IN REVIEW
Jira status: OPEN
Jira status: REOPENED
Jira status: RESOLVED
mirrored
mirror internally
needs: backports
needs: ci
needs: design review
needs: preceding PR
needs: review
needs: work
notabug
open for community contribution
Product Backlog
question
ready for merge
security
stale
type: bug
type: enhancement
type: question
web pages
wontfix
writing
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-ARMmbed/mbed-crypto#302
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Description
In a firmware of mine, I have observed a crash (HardFault) when printing the status of the memory buffer using the function
mbedtls_memory_buffer_alloc_status(). The crash log is as follows:The firmware is fairly complex and features multiple threads doing handshakes or encrypting stuff. So it can definitely happen that there concurrent calls to the buffer allocation functions. However, I have also implemented the
MBEDTLS_THREADING_ALTfunctions using the mbed-os providedMutexclass and calledmbedtls_threading_set_alt()accordingly. This mutex layer works, I have tested it independently.The crash takes place (by looking at the
PC) at the last lineWhere it does a dereference of the
hdrpointer.What you can see from the log is while printing the free list of the heap, it suddenly jumps to pointers it shouldn't. Note how there's there's a break from
PTR( 536928324)toPTR( 68)(with an allocation size which exceeds the amount of memory the device has).When looking at the code it seems to me that this is a race which occurs when calling
mbedtls_memory_buffer_alloc_status(). All the allocation function lock theheap.mutexwhile modifying the heap (see e.g.buffer_alloc_calloc_mutexed()). however, the buffer alloc status function does not. It just iterates through the linked list and follows every pointer. This behavior is still in there in newest code.github.com/ARMmbed/mbed-crypto@150d577780/library/memory_buffer_alloc.c (L509-L530)Thus I think my observed crash was caused by a race in which a
free()operation took place while printing the buffer, and thus it printed a correctNEXTpointer but when assigningcur = cur->next_free;the value was already modified to some other value.I thus propse to add the following code at the beginning and end of the function:
I have tried to reproduce this exact crash but haven't succeeded. Triggering / loosing this race seems to be quite hard, but as my log file shows, possible.
Issue request type
Internal Jira reference: https://jira.arm.com/browse/IOTCRYPT-957