Skip to content

Commit 3271c99

Browse files
committed
Fixes from review
1 parent ddda148 commit 3271c99

4 files changed

Lines changed: 50 additions & 30 deletions

File tree

src/mqtt_broker.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,19 @@
3535

3636
#ifdef WOLFMQTT_BROKER
3737

38-
#define BROKER_FORCE_ZERO(mem, len) Mqtt_ForceZero(mem, (word32)(len))
38+
/* Secure memory zeroing - uses volatile pointer to prevent the compiler
39+
* from optimizing away the stores (dead-store elimination). */
40+
static void MqttBroker_ForceZero(void* mem, word32 len)
41+
{
42+
volatile byte* p = (volatile byte*)mem;
43+
word32 i;
44+
for (i = 0; i < len; i++) {
45+
p[i] = 0;
46+
}
47+
}
48+
49+
#define BROKER_FORCE_ZERO(mem, len) \
50+
MqttBroker_ForceZero(mem, (word32)(len))
3951

4052
/* -------------------------------------------------------------------------- */
4153
/* Platform includes */
@@ -2693,6 +2705,13 @@ static int BrokerHandle_Connect(BrokerClient* bc, int rx_len,
26932705
return rc;
26942706
}
26952707

2708+
#ifdef WOLFMQTT_V5
2709+
/* Initialize early so every `goto send_connack` below produces a CONNACK
2710+
* matching the client's protocol level (v5 CONNACK has a Properties
2711+
* length and uses v5 reason codes). */
2712+
ack.protocol_level = mc.protocol_level;
2713+
#endif
2714+
26962715
/* Store client ID */
26972716
#ifdef WOLFMQTT_STATIC_MEMORY
26982717
bc->client_id[0] = '\0';
@@ -2909,7 +2928,6 @@ static int BrokerHandle_Connect(BrokerClient* bc, int rx_len,
29092928
ack.flags = 0;
29102929
ack.return_code = MQTT_CONNECT_ACK_CODE_ACCEPTED;
29112930
#ifdef WOLFMQTT_V5
2912-
ack.protocol_level = mc.protocol_level;
29132931
ack.props = NULL;
29142932
#endif
29152933

src/mqtt_client.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,19 @@
2626

2727
#include "wolfmqtt/mqtt_client.h"
2828

29-
#define CLIENT_FORCE_ZERO(mem, len) Mqtt_ForceZero(mem, (word32)(len))
29+
/* Secure memory zeroing - uses volatile pointer to prevent the compiler
30+
* from optimizing away the stores (dead-store elimination). */
31+
static void MqttClient_ForceZero(void* mem, word32 len)
32+
{
33+
volatile byte* p = (volatile byte*)mem;
34+
word32 i;
35+
for (i = 0; i < len; i++) {
36+
p[i] = 0;
37+
}
38+
}
39+
40+
#define CLIENT_FORCE_ZERO(mem, len) \
41+
MqttClient_ForceZero(mem, (word32)(len))
3042

3143
/* DOCUMENTED BUILD OPTIONS:
3244
*
@@ -2918,6 +2930,7 @@ int MqttClient_NetDisconnect(MqttClient *client)
29182930
{
29192931
#ifdef WOLFMQTT_MULTITHREAD
29202932
MqttPendResp *tmpResp;
2933+
MqttPendResp *nextResp;
29212934
int rc;
29222935
#endif
29232936

@@ -2932,7 +2945,6 @@ int MqttClient_NetDisconnect(MqttClient *client)
29322945
#ifdef WOLFMQTT_DEBUG_CLIENT
29332946
PRINTF("Net Disconnect: Removing pending responses");
29342947
#endif
2935-
MqttPendResp *nextResp;
29362948
for (tmpResp = client->firstPendResp;
29372949
tmpResp != NULL;
29382950
tmpResp = nextResp) {

src/mqtt_packet.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2741,19 +2741,24 @@ int MqttDecode_Auth(byte *rx_buf, int rx_buf_len, MqttAuth *auth)
27412741
if (tmp < 0)
27422742
return tmp;
27432743
rx_payload += tmp;
2744-
}
2745-
else if (auth->reason_code != MQTT_REASON_SUCCESS) {
2746-
/* The Reason Code and Property Length can be omitted if the
2747-
Reason Code is 0x00 (Success) and there are no Properties.
2748-
In this case the AUTH has a Remaining Length of 0. */
2749-
return MQTT_TRACE_ERROR(MQTT_CODE_ERROR_MALFORMED_DATA);
2750-
}
2751-
if (auth->props != NULL) {
2752-
/* Must have Authentication Method */
2744+
}
2745+
else if (auth->reason_code != MQTT_REASON_SUCCESS) {
2746+
/* The Reason Code and Property Length can be omitted if
2747+
the Reason Code is 0x00 (Success) and there are no
2748+
Properties. In this case the AUTH has a Remaining
2749+
Length of 0. */
2750+
return MQTT_TRACE_ERROR(MQTT_CODE_ERROR_MALFORMED_DATA);
2751+
}
2752+
if (auth->props != NULL) {
2753+
/* Must have Authentication Method */
27532754

2754-
/* Must have Authentication Data */
2755+
/* Must have Authentication Data */
27552756

2756-
/* May have zero or more User Property pairs */
2757+
/* May have zero or more User Property pairs */
2758+
}
2759+
else {
2760+
return MQTT_TRACE_ERROR(MQTT_CODE_ERROR_MALFORMED_DATA);
2761+
}
27572762
}
27582763
else {
27592764
return MQTT_TRACE_ERROR(MQTT_CODE_ERROR_MALFORMED_DATA);
@@ -2762,10 +2767,6 @@ int MqttDecode_Auth(byte *rx_buf, int rx_buf_len, MqttAuth *auth)
27622767
else {
27632768
return MQTT_TRACE_ERROR(MQTT_CODE_ERROR_MALFORMED_DATA);
27642769
}
2765-
}
2766-
else {
2767-
return MQTT_TRACE_ERROR(MQTT_CODE_ERROR_MALFORMED_DATA);
2768-
}
27692770
}
27702771
else {
27712772
/* Per MQTT 5.0 section 3.15.2: Remaining Length of 0 implies

wolfmqtt/mqtt_types.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -358,17 +358,6 @@ enum MqttPacketResponseCodes {
358358
#define WOLFMQTT_NORETURN
359359
#endif
360360

361-
/* Secure memory zeroing - uses volatile pointer to prevent compiler
362-
* from optimizing away the stores (dead-store elimination). */
363-
static INLINE void Mqtt_ForceZero(void* mem, word32 len)
364-
{
365-
volatile byte* p = (volatile byte*)mem;
366-
word32 i;
367-
for (i = 0; i < len; i++) {
368-
p[i] = 0;
369-
}
370-
}
371-
372361
/* Logging / Tracing */
373362
#ifdef WOLFMQTT_NO_STDIO
374363
#undef WOLFMQTT_DEBUG_CLIENT

0 commit comments

Comments
 (0)