Skip to content

Conversation

@SiteRelEnby
Copy link

Summary

Fixes a use-after-free bug in read_ihex_chunks() that causes segmentation faults during firmware loading on some systems.

Bug Description

When read_ihex_chunks() needs to expand the first chunk via realloc(), it updates the backlink pointer but fails to update the root pointer passed by the caller. This leaves the caller with a dangling pointer to freed memory.

Symptoms

  • Consistent segfault on Void Linux during simulator initialization
  • Intermittent crashes on WSL Ubuntu
  • Crash occurs in memcpy() at sim_hex.c:200

Root Cause

In the realloc path (line 173-179), when backlink_p is NULL (first chunk case), only backlink_p->next is updated. The root pointer (*chunks_p) still points to the old freed memory location.

Fix

Track the root pointer separately (fw_chunk_t **root_p = chunks_p) and update it when reallocating the first chunk (backlink_p == NULL case).

if ( backlink_p ) {
    backlink_p->next = chunk;
} else {
    /* First chunk realloc - update root pointer */
    *root_p = chunk;
}

Changes in this PR

Only contains the UAF fix - 7 additions, 1 deletion to simavr/sim/sim_hex.c.

This PR is based on your add-attiny1634-core branch and does not modify any ATtiny1634 code.

Testing

  • ✅ Verified fix resolves segfaults on Void Linux
  • ✅ Eliminates intermittent crashes on WSL Ubuntu
  • ✅ Simulator initializes successfully across all tested platforms

Related

This same fix has been submitted to upstream simavr: buserror#569

🤖 Generated with Claude Code

…chunk

When read_ihex_chunks() needs to expand the first chunk via realloc(),
it updates the backlink pointer but fails to update the root pointer
passed by the caller. This leaves the caller with a dangling pointer
to freed memory, causing a segmentation fault when the freed address
is later accessed.

The bug manifests as a crash during firmware loading:
- Consistent segfault on Void Linux
- Intermittent crashes on WSL Ubuntu
- Crash occurs in memcpy() at sim_hex.c:200

Root cause:
In the realloc path (line 173-179), when backlink_p is NULL (first
chunk case), only backlink_p->next is updated. The root pointer
(*chunks_p) still points to the old freed memory location.

Fix:
Track the root pointer separately and update it when reallocating
the first chunk (backlink_p == NULL case).

Testing:
Verified fix resolves segfaults on Void Linux and eliminates
intermittent crashes on WSL Ubuntu. Simulator now initializes
successfully across all tested platforms.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant