Skip to content

Commit

Permalink
sys/net/gnrc_pktbuf_static: add double free detection
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Nov 11, 2024
1 parent d86738f commit b0f2129
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions sys/net/gnrc/pktbuf_static/gnrc_pktbuf_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,10 @@ static void *_pktbuf_alloc(size_t size)
#endif
assert(0);
}
if (CONFIG_GNRC_PKTBUF_CHECK_USE_AFTER_FREE) {
/* clear out canary */
memset(ptr, ~CANARY, size);
}

return (void *)ptr;
}
Expand Down Expand Up @@ -469,6 +473,12 @@ void gnrc_pktbuf_free_internal(void *data, size_t size)
}

if (CONFIG_GNRC_PKTBUF_CHECK_USE_AFTER_FREE) {
/* check if the data has already been marked as free */
size_t chk_len = _align(size) - sizeof(*new);
if (chk_len && !memchk((uint8_t *)data + sizeof(*new), CANARY, chk_len)) {
puts("pktbuf: double free detected!");
DEBUG_BREAKPOINT(2);
}
memset(data, CANARY, _align(size));
}

Expand Down

0 comments on commit b0f2129

Please sign in to comment.