Skip to content

Commit d5b49be

Browse files
committed
Fixes from review
1 parent 1940eb3 commit d5b49be

2 files changed

Lines changed: 46 additions & 23 deletions

File tree

src/mqtt_broker.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3393,11 +3393,6 @@ static int BrokerHandle_PublishRel(BrokerClient* bc, int rx_len)
33933393
(int)bc->sock, resp.packet_id);
33943394
rc = MqttPacket_Write(&bc->client, bc->tx_buf, rc);
33953395
}
3396-
#ifdef WOLFMQTT_V5
3397-
if (resp.props) {
3398-
(void)MqttProps_Free(resp.props);
3399-
}
3400-
#endif
34013396
return rc;
34023397
}
34033398

@@ -3434,11 +3429,6 @@ static int BrokerHandle_PublishRec(BrokerClient* bc, int rx_len)
34343429
(int)bc->sock, resp.packet_id);
34353430
rc = MqttPacket_Write(&bc->client, bc->tx_buf, rc);
34363431
}
3437-
#ifdef WOLFMQTT_V5
3438-
if (resp.props) {
3439-
(void)MqttProps_Free(resp.props);
3440-
}
3441-
#endif
34423432
return rc;
34433433
}
34443434

@@ -3875,6 +3865,25 @@ int MqttBroker_Start(MqttBroker* broker)
38753865

38763866
#ifdef WOLFMQTT_BROKER_AUTH
38773867
if (broker->auth_user || broker->auth_pass) {
3868+
/* Reject configured credentials that would be silently rejected
3869+
* by BrokerStrCompare's cmp_len guard. Catching this at startup
3870+
* avoids a confusing state where every client auth fails. */
3871+
if (broker->auth_user &&
3872+
XSTRLEN(broker->auth_user) >= BROKER_MAX_USERNAME_LEN) {
3873+
WBLOG_ERR(broker,
3874+
"broker: auth_user length %u >= BROKER_MAX_USERNAME_LEN (%d)",
3875+
(unsigned)XSTRLEN(broker->auth_user),
3876+
BROKER_MAX_USERNAME_LEN);
3877+
return MQTT_CODE_ERROR_BAD_ARG;
3878+
}
3879+
if (broker->auth_pass &&
3880+
XSTRLEN(broker->auth_pass) >= BROKER_MAX_PASSWORD_LEN) {
3881+
WBLOG_ERR(broker,
3882+
"broker: auth_pass length %u >= BROKER_MAX_PASSWORD_LEN (%d)",
3883+
(unsigned)XSTRLEN(broker->auth_pass),
3884+
BROKER_MAX_PASSWORD_LEN);
3885+
return MQTT_CODE_ERROR_BAD_ARG;
3886+
}
38783887
WBLOG_INFO(broker, "broker: auth enabled user=%s",
38793888
broker->auth_user ? broker->auth_user : "(null)");
38803889
#ifdef ENABLE_MQTT_TLS
@@ -4187,7 +4196,7 @@ int wolfmqtt_broker(int argc, char** argv)
41874196
if (rc == MQTT_CODE_CONTINUE) {
41884197
BROKER_SLEEP_MS(10);
41894198
}
4190-
else if (rc < 0 && rc != MQTT_CODE_CONTINUE) {
4199+
else if (rc < 0) {
41914200
break;
41924201
}
41934202
}

src/mqtt_client.c

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)