From 9e2092fc66f1f1892b587da58da23e890bf6dbb7 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sun, 30 Aug 2026 20:13:38 +0200 Subject: [PATCH] capabilities: report device frame size so the client can adapt PM3_CMD_DATA_SIZE went 512 -> 624 without a capabilities bump, so a new client connects to old firmware and every oversized command dies at the device's length check with no message. Append max_cmd_data_size, bump to v9. The client now accepts an older capabilities struct - it only ever grows by appending, so an older layout is a prefix - and defaults the frame size for pre-v9 firmware. SendCommandNG bounds by the device value instead of the compile time one. Also zero init capabilities_t on the device, it leaked stack bytes. --- armsrc/appmain.c | 3 ++- client/src/comms.c | 55 +++++++++++++++++++++++++++++++++++++++++----- client/src/comms.h | 1 + include/pm3_cmd.h | 9 +++++++- 4 files changed, 60 insertions(+), 8 deletions(-) diff --git a/armsrc/appmain.c b/armsrc/appmain.c index 40e4749f4..ce1a73848 100644 --- a/armsrc/appmain.c +++ b/armsrc/appmain.c @@ -688,8 +688,9 @@ static void SendStatus(uint32_t wait) { } static void SendCapabilities(void) { - capabilities_t capabilities; + capabilities_t capabilities = {0}; capabilities.version = CAPABILITIES_VERSION; + capabilities.max_cmd_data_size = PM3_CMD_DATA_SIZE; capabilities.via_fpc = g_reply_via_fpc; capabilities.via_usb = g_reply_via_usb; capabilities.bigbuf_size = BigBuf_get_size(); diff --git a/client/src/comms.c b/client/src/comms.c index a40f8fdc1..f366b9662 100644 --- a/client/src/comms.c +++ b/client/src/comms.c @@ -40,6 +40,22 @@ static serial_port sp = NULL; communication_arg_t g_conn; capabilities_t g_pm3_capabilities; +// Largest NG payload the attached device accepts. +// +// zero means no completed a capabilities handshake yet, +// Fall back to the OLD size 512, +// Note a pre-v9 device does not reach this branch, TestProxmark() fills the field in for it. +// +// Capped by PM3_CMD_DATA_SIZE as well, since that is what sizes our own buffers. +uint16_t pm3_max_cmd_data_size(void) { + + uint16_t devmax = g_pm3_capabilities.max_cmd_data_size; + if (devmax == 0) { + devmax = CAPABILITIES_LEGACY_CMD_DATA_SIZE; + } + return MIN(devmax, (uint16_t)PM3_CMD_DATA_SIZE); +} + static pthread_t communication_thread; static pthread_t reconnect_thread; @@ -178,8 +194,9 @@ void SendCommandNG(uint16_t cmd, uint8_t *data, size_t len) { PrintAndLogEx(INFO, "Sending bytes to proxmark failed - offline"); return; } - if (len > PM3_CMD_DATA_SIZE) { - PrintAndLogEx(WARNING, "Sending " _RED_("%zu") " bytes of payload is too much, abort", len); + uint16_t maxlen = pm3_max_cmd_data_size(); + if (len > maxlen) { + PrintAndLogEx(WARNING, "Sending " _RED_("%zu") " bytes of payload is too much for this device (max " _YELLOW_("%u") "), abort", len, maxlen); return; } @@ -919,13 +936,39 @@ int TestProxmark(pm3_device_t *dev) { return PM3_ETIMEOUT; } - if ((resp.length != sizeof(g_pm3_capabilities)) || (resp.data.asBytes[0] != CAPABILITIES_VERSION)) { - PrintAndLogEx(ERR, _RED_("Capabilities structure version sent by Proxmark3 is not the same as the one used by the client!")); - PrintAndLogEx(ERR, _RED_("Please flash the Proxmark3 with the same version as the client.")); + // Firmware older than the client is fine: capabilities_t only ever grows by + // appending, so an older struct is a prefix of ours. Zero first, copy what the + // device sent, and default the rest below. + if ((resp.data.asBytes[0] > CAPABILITIES_VERSION) || + (resp.length > sizeof(g_pm3_capabilities)) || + (resp.length < offsetof(capabilities_t, max_cmd_data_size))) { + PrintAndLogEx(ERR, _RED_("Capabilities structure sent by Proxmark3 is not one this client understands!")); + PrintAndLogEx(ERR, _RED_("Device reports v%u/%u bytes, client understands up to v%u/%zu bytes."), + resp.data.asBytes[0], + resp.length, + CAPABILITIES_VERSION, + sizeof(g_pm3_capabilities) + ); + PrintAndLogEx(ERR, _RED_("Please flash the Proxmark3 with a version matching the client.")); return PM3_EDEVNOTSUPP; } - memcpy(&g_pm3_capabilities, resp.data.asBytes, sizeof(capabilities_t)); + memset(&g_pm3_capabilities, 0, sizeof(g_pm3_capabilities)); + memcpy(&g_pm3_capabilities, resp.data.asBytes, resp.length); + + if (g_pm3_capabilities.version < CAPABILITIES_VERSION) { + + // pre-v9 firmware could not report its frame size + if (g_pm3_capabilities.max_cmd_data_size == 0) { + g_pm3_capabilities.max_cmd_data_size = CAPABILITIES_LEGACY_CMD_DATA_SIZE; + } + PrintAndLogEx(WARNING, "Device capabilities v%u, client v%u. Command payloads limited to " _YELLOW_("%u") " bytes", + g_pm3_capabilities.version, + CAPABILITIES_VERSION, + g_pm3_capabilities.max_cmd_data_size + ); + } + g_conn.send_via_fpc_usart = g_pm3_capabilities.via_fpc; g_conn.uart_speed = g_pm3_capabilities.baudrate; diff --git a/client/src/comms.h b/client/src/comms.h index 45e0ea2c2..61ea8def0 100644 --- a/client/src/comms.h +++ b/client/src/comms.h @@ -106,6 +106,7 @@ size_t GetCommunicationRawReceiveNum(void); bool OpenProxmarkSilent(pm3_device_t **dev, const char *port, uint32_t speed); bool OpenProxmark(pm3_device_t **dev, const char *port, bool wait_for_port, int timeout, bool flash_mode, uint32_t speed); int TestProxmark(pm3_device_t *dev); +uint16_t pm3_max_cmd_data_size(void); void CloseProxmark(pm3_device_t *dev); void StartReconnectProxmark(void); diff --git a/include/pm3_cmd.h b/include/pm3_cmd.h index 4b3275dea..589c3d75e 100644 --- a/include/pm3_cmd.h +++ b/include/pm3_cmd.h @@ -261,8 +261,15 @@ typedef struct { bool hw_available_i2c_eeprom : 1; bool is_pm5 : 1; bool is_pm5_std_ant : 1; + + // Appended in version 9. Fields must only ever be APPENDED here: + // the client accepts a shorter struct from older firmware + // PM5 can use this to inform which size they support oo. + uint16_t max_cmd_data_size; // device side PM3_CMD_DATA_SIZE } PACKED capabilities_t; -#define CAPABILITIES_VERSION 8 +#define CAPABILITIES_VERSION 9 +// what a pre-v9 device would have used, it could not tell us +#define CAPABILITIES_LEGACY_CMD_DATA_SIZE 512 extern capabilities_t g_pm3_capabilities; typedef struct {