From d5154589735de8c627742aa71f6c3ac9c8a97601 Mon Sep 17 00:00:00 2001 From: Jonathan Yen Date: Mon, 17 Aug 2026 09:06:09 -0400 Subject: [PATCH] fix: heap overflow in pm3_grabbed_output_get pm3_grabbed_output_get null-terminated the captured output at g_grabbed_output.size, but size is the allocated capacity, not the number of bytes written -- ui.c grows it by MAX_PRINT_BUFFER at a time and tracks the used length separately in idx. Every call therefore wrote one zero byte past the end of the heap allocation. The CLI mostly survives it because it calls the function once or twice and then exits, so the corrupted malloc metadata is never reused. A long-running libpm3 client that captures output on every command does not: the damage surfaces later, far from the cause. It was found as a SIGTRAP inside an unrelated AppKit autorelease pool. Reproduced against real hardware: MALLOC_STRICT_SIZE=1 MallocGuardEdges=1 \ DYLD_INSERT_LIBRARIES=/usr/lib/libgmalloc.dylib ./pm3run "hw version" which crashes with SIGSEGV inside pm3_grabbed_output_get before this change and exits cleanly with identical captured output after it. Terminating at idx is always in bounds: the grabber guarantees at least MAX_PRINT_BUFFER bytes of headroom before each write, so idx is strictly less than size. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01AjGgtnsvWJHAMYEksAiYuf --- CHANGELOG.md | 1 + client/src/pm3.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a80c7790f..28212cd61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ All notable changes to this project will be documented in this file. This project uses the changelog in accordance with [keepchangelog](http://keepachangelog.com/). Please use this to write notable changes, which is not the same as git commit log... ## [unreleased][unreleased] +- Fixed `pm3_grabbed_output_get` - heap overflow (@jonyen) - Fixed `hf iclass sam` - removed the old implementation (@iceman1001) - Fixed Proxmark5 (PM5/AT32) failing to re-enter the bootloader (@nemanjan00) - Fixed `hw fpga config` on Proxmark5 (PM5/AT32) now powers on the FPGA 24MHz clock (@nemanjan00) diff --git a/client/src/pm3.c b/client/src/pm3.c index ff08c8b94..1c4088305 100644 --- a/client/src/pm3.c +++ b/client/src/pm3.c @@ -80,7 +80,7 @@ const char *pm3_name_get(pm3_device_t *dev) { const char *pm3_grabbed_output_get(pm3_device_t *dev) { if (g_grabbed_output.ptr != NULL) { char *tmp = g_grabbed_output.ptr; - tmp[g_grabbed_output.size] = 0; + tmp[g_grabbed_output.idx] = 0; g_grabbed_output.idx = 0; g_grabbed_output.size = 0; return tmp;