Skip to content

Furi: Detect use-after-free #3995

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

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion furi/core/memmgr_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ void vPortFree(void* pv) {
/* Add this block to the list of free blocks. */
xFreeBytesRemaining += pxLink->xBlockSize;
traceFREE(pv, pxLink->xBlockSize);
memset(pv, 0, pxLink->xBlockSize - xHeapStructSize);
memset(pv, 0xDD, pxLink->xBlockSize - xHeapStructSize);
prvInsertBlockIntoFreeList((BlockLink_t*)pxLink);
}
(void)xTaskResumeAll();
Expand Down
4 changes: 4 additions & 0 deletions furi/core/semaphore.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ FuriStatus furi_semaphore_release(FuriSemaphore* instance) {

stat = FuriStatusOk;

FURI_CRITICAL_ENTER();

if(FURI_IS_IRQ_MODE()) {
yield = pdFALSE;

Expand All @@ -123,6 +125,8 @@ FuriStatus furi_semaphore_release(FuriSemaphore* instance) {
furi_event_loop_link_notify(&instance->event_loop_link, FuriEventLoopEventIn);
}

FURI_CRITICAL_EXIT();

return stat;
}

Expand Down
47 changes: 46 additions & 1 deletion targets/f7/furi_hal/furi_hal_interrupt.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,52 @@ void MemManage_Handler(void) {
}

void BusFault_Handler(void) {
furi_crash("BusFault");
const char* crash_msg = "BusFault";

furi_log_puts("\r\n" _FURI_LOG_CLR_E "Bus fault:\r\n");
if(FURI_BIT(SCB->CFSR, SCB_CFSR_LSPERR_Pos)) {
furi_log_puts(" - lazy stacking for exception entry\r\n");
}

if(FURI_BIT(SCB->CFSR, SCB_CFSR_STKERR_Pos)) {
furi_log_puts(" - stacking for exception entry\r\n");
}

if(FURI_BIT(SCB->CFSR, SCB_CFSR_UNSTKERR_Pos)) {
furi_log_puts(" - unstacking for exception return\r\n");
}

if(FURI_BIT(SCB->CFSR, SCB_CFSR_IMPRECISERR_Pos)) {
furi_log_puts(" - imprecise data access\r\n");
}

if(FURI_BIT(SCB->CFSR, SCB_CFSR_PRECISERR_Pos)) {
furi_log_puts(" - precise data access\r\n");
}

if(FURI_BIT(SCB->CFSR, SCB_CFSR_IBUSERR_Pos)) {
furi_log_puts(" - instruction\r\n");
}

if(FURI_BIT(SCB->CFSR, SCB_CFSR_BFARVALID_Pos)) {
uint32_t busfault_address = SCB->BFAR;
furi_log_puts(" -- at 0x");

char tmp_str[] = "0xFFFFFFFF";
itoa(busfault_address, tmp_str, 16);
furi_log_puts(tmp_str);

furi_log_puts("\r\n");

if(busfault_address == (uint32_t)NULL) {
furi_log_puts(" -- NULL pointer dereference\r\n");
} else if(busfault_address >= 0xDDDDDDDD && busfault_address <= 0xDDDEDDDD) {
crash_msg = "Possible use-after-free";
}
}
furi_log_puts(_FURI_LOG_CLR_RESET);

furi_crash(crash_msg);
}

void UsageFault_Handler(void) {
Expand Down