@@ -1725,6 +1725,10 @@ int MqttClient_Connect(MqttClient *client, MqttConnect *mc_connect)
17251725 MQTT_PACKET_TYPE_CONNECT , 0 , 0 );
17261726 #endif
17271727 if (rc <= 0 ) {
1728+ /* Encode failed: tx_buf may hold partial plaintext credentials.
1729+ * Zero the full buffer before MqttWriteStop releases lockSend
1730+ * so no other thread can see residual data. */
1731+ CLIENT_FORCE_ZERO (client -> tx_buf , client -> tx_buf_len );
17281732 MqttWriteStop (client , & mc_connect -> stat );
17291733 return rc ;
17301734 }
@@ -1741,9 +1745,11 @@ int MqttClient_Connect(MqttClient *client, MqttConnect *mc_connect)
17411745 if (rc != 0 ) {
17421746 /* Save write.len before MqttWriteStop zeroes client->write */
17431747 int xfer = client -> write .len ;
1744- MqttWriteStop (client , & mc_connect -> stat );
1745- /* Clear tx_buf to remove plaintext credentials before returning */
1748+ /* Clear tx_buf to remove plaintext credentials BEFORE
1749+ * MqttWriteStop releases lockSend, so another thread cannot
1750+ * race in and repopulate tx_buf before it is scrubbed. */
17461751 CLIENT_FORCE_ZERO (client -> tx_buf , xfer );
1752+ MqttWriteStop (client , & mc_connect -> stat );
17471753 return rc ; /* Error locking client */
17481754 }
17491755 #endif
@@ -1766,11 +1772,12 @@ int MqttClient_Connect(MqttClient *client, MqttConnect *mc_connect)
17661772 return rc ;
17671773 }
17681774 #endif
1769- MqttWriteStop ( client , & mc_connect -> stat );
1770-
1771- /* Clear tx_buf to remove any plaintext credentials from memory .
1772- * Use xfer (saved before MqttWriteStop zeroes client->write) */
1775+ /* Clear tx_buf to remove any plaintext credentials from memory
1776+ * BEFORE MqttWriteStop releases lockSend, so another thread cannot
1777+ * race in and populate tx_buf before it is scrubbed .
1778+ * Use xfer (saved before MqttWriteStop zeroes client->write). */
17731779 CLIENT_FORCE_ZERO (client -> tx_buf , xfer );
1780+ MqttWriteStop (client , & mc_connect -> stat );
17741781
17751782 if (rc != xfer ) {
17761783 MqttClient_CancelMessage (client , (MqttObject * )mc_connect );
@@ -2750,6 +2757,10 @@ int MqttClient_Auth(MqttClient *client, MqttAuth* auth)
27502757 MQTT_PACKET_TYPE_AUTH , 0 , 0 );
27512758 #endif
27522759 if (rc <= 0 ) {
2760+ /* Encode failed: tx_buf may hold partial SASL auth data.
2761+ * Zero the full buffer before MqttWriteStop releases lockSend
2762+ * so no other thread can see residual data. */
2763+ CLIENT_FORCE_ZERO (client -> tx_buf , client -> tx_buf_len );
27532764 MqttWriteStop (client , & auth -> stat );
27542765 return rc ;
27552766 }
@@ -2766,9 +2777,11 @@ int MqttClient_Auth(MqttClient *client, MqttAuth* auth)
27662777 if (rc != 0 ) {
27672778 /* Save write.len before MqttWriteStop zeroes client->write */
27682779 int xfer = client -> write .len ;
2769- MqttWriteStop (client , & auth -> stat );
2770- /* Clear tx_buf to remove SASL auth data before returning */
2780+ /* Clear tx_buf to remove SASL auth data BEFORE MqttWriteStop
2781+ * releases lockSend, to prevent a racing thread from
2782+ * repopulating tx_buf before it is scrubbed. */
27712783 CLIENT_FORCE_ZERO (client -> tx_buf , xfer );
2784+ MqttWriteStop (client , & auth -> stat );
27722785 return rc ; /* Error locking client */
27732786 }
27742787 #endif
@@ -2790,11 +2803,12 @@ int MqttClient_Auth(MqttClient *client, MqttAuth* auth)
27902803 return rc ;
27912804 }
27922805 #endif
2793- MqttWriteStop ( client , & auth -> stat );
2794-
2795- /* Clear tx_buf to remove any SASL auth data from memory .
2796- * Use xfer (saved before MqttWriteStop zeroes client->write) */
2806+ /* Clear tx_buf to remove any SASL auth data from memory BEFORE
2807+ * MqttWriteStop releases lockSend, to prevent a racing thread
2808+ * from populating tx_buf before it is scrubbed .
2809+ * Use xfer (saved before MqttWriteStop zeroes client->write). */
27972810 CLIENT_FORCE_ZERO (client -> tx_buf , xfer );
2811+ MqttWriteStop (client , & auth -> stat );
27982812
27992813 if (rc != xfer ) {
28002814 MqttClient_CancelMessage (client , (MqttObject * )auth );
0 commit comments