Skip to content

Commit faa3bfe

Browse files
committed
Fix F-1606 BrokerHandle_Connect static-mem error codes
1 parent 548521d commit faa3bfe

1 file changed

Lines changed: 69 additions & 1 deletion

File tree

src/mqtt_broker.c

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2699,6 +2699,25 @@ static int BrokerHandle_Connect(BrokerClient* bc, int rx_len,
26992699
word16 id_len = 0;
27002700
if (MqttDecode_Num((byte*)mc.client_id - MQTT_DATA_LEN_SIZE,
27012701
&id_len, MQTT_DATA_LEN_SIZE) == MQTT_DATA_LEN_SIZE) {
2702+
#ifdef WOLFMQTT_STATIC_MEMORY
2703+
if (id_len >= BROKER_MAX_CLIENT_ID_LEN) {
2704+
WBLOG_ERR(broker,
2705+
"broker: client_id too long (%u >= %d) sock=%d",
2706+
(unsigned)id_len, BROKER_MAX_CLIENT_ID_LEN,
2707+
(int)bc->sock);
2708+
#ifdef WOLFMQTT_V5
2709+
if (mc.protocol_level >= MQTT_CONNECT_PROTOCOL_LEVEL_5) {
2710+
ack.return_code = MQTT_REASON_CLIENT_ID_NOT_VALID;
2711+
}
2712+
else
2713+
#endif
2714+
{
2715+
ack.return_code =
2716+
MQTT_CONNECT_ACK_CODE_REFUSED_ID;
2717+
}
2718+
goto send_connack;
2719+
}
2720+
#endif
27022721
BROKER_STORE_STR(bc->client_id, mc.client_id, id_len,
27032722
BROKER_MAX_CLIENT_ID_LEN);
27042723
}
@@ -2760,6 +2779,17 @@ static int BrokerHandle_Connect(BrokerClient* bc, int rx_len,
27602779
if (mc.enable_lwt && mc.lwt_msg != NULL) {
27612780
if (mc.lwt_msg->topic_name != NULL &&
27622781
mc.lwt_msg->topic_name_len > 0) {
2782+
#ifdef WOLFMQTT_STATIC_MEMORY
2783+
if (mc.lwt_msg->topic_name_len >= BROKER_MAX_TOPIC_LEN) {
2784+
WBLOG_ERR(broker,
2785+
"broker: LWT topic too long (%u >= %d) sock=%d",
2786+
(unsigned)mc.lwt_msg->topic_name_len,
2787+
BROKER_MAX_TOPIC_LEN, (int)bc->sock);
2788+
ack.return_code =
2789+
MQTT_CONNECT_ACK_CODE_REFUSED_UNAVAIL;
2790+
goto send_connack;
2791+
}
2792+
#endif
27632793
BROKER_STORE_STR(bc->will_topic, mc.lwt_msg->topic_name,
27642794
mc.lwt_msg->topic_name_len, BROKER_MAX_TOPIC_LEN);
27652795
}
@@ -2821,6 +2851,25 @@ static int BrokerHandle_Connect(BrokerClient* bc, int rx_len,
28212851
word16 ulen = 0;
28222852
if (MqttDecode_Num((byte*)mc.username - MQTT_DATA_LEN_SIZE,
28232853
&ulen, MQTT_DATA_LEN_SIZE) == MQTT_DATA_LEN_SIZE) {
2854+
#ifdef WOLFMQTT_STATIC_MEMORY
2855+
if (ulen >= BROKER_MAX_USERNAME_LEN) {
2856+
WBLOG_ERR(broker,
2857+
"broker: username too long (%u >= %d) sock=%d",
2858+
(unsigned)ulen, BROKER_MAX_USERNAME_LEN,
2859+
(int)bc->sock);
2860+
#ifdef WOLFMQTT_V5
2861+
if (mc.protocol_level >= MQTT_CONNECT_PROTOCOL_LEVEL_5) {
2862+
ack.return_code = MQTT_REASON_BAD_USER_OR_PASS;
2863+
}
2864+
else
2865+
#endif
2866+
{
2867+
ack.return_code =
2868+
MQTT_CONNECT_ACK_CODE_REFUSED_BAD_USER_PWD;
2869+
}
2870+
goto send_connack;
2871+
}
2872+
#endif
28242873
BROKER_STORE_STR_SENSITIVE(bc->username, mc.username, ulen,
28252874
BROKER_MAX_USERNAME_LEN);
28262875
}
@@ -2829,6 +2878,25 @@ static int BrokerHandle_Connect(BrokerClient* bc, int rx_len,
28292878
word16 plen = 0;
28302879
if (MqttDecode_Num((byte*)mc.password - MQTT_DATA_LEN_SIZE,
28312880
&plen, MQTT_DATA_LEN_SIZE) == MQTT_DATA_LEN_SIZE) {
2881+
#ifdef WOLFMQTT_STATIC_MEMORY
2882+
if (plen >= BROKER_MAX_PASSWORD_LEN) {
2883+
WBLOG_ERR(broker,
2884+
"broker: password too long (%u >= %d) sock=%d",
2885+
(unsigned)plen, BROKER_MAX_PASSWORD_LEN,
2886+
(int)bc->sock);
2887+
#ifdef WOLFMQTT_V5
2888+
if (mc.protocol_level >= MQTT_CONNECT_PROTOCOL_LEVEL_5) {
2889+
ack.return_code = MQTT_REASON_BAD_USER_OR_PASS;
2890+
}
2891+
else
2892+
#endif
2893+
{
2894+
ack.return_code =
2895+
MQTT_CONNECT_ACK_CODE_REFUSED_BAD_USER_PWD;
2896+
}
2897+
goto send_connack;
2898+
}
2899+
#endif
28322900
BROKER_STORE_STR_SENSITIVE(bc->password, mc.password, plen,
28332901
BROKER_MAX_PASSWORD_LEN);
28342902
}
@@ -2937,7 +3005,7 @@ static int BrokerHandle_Connect(BrokerClient* bc, int rx_len,
29373005
}
29383006
#endif
29393007

2940-
#ifdef WOLFMQTT_BROKER_WILL
3008+
#if defined(WOLFMQTT_BROKER_WILL) || defined(WOLFMQTT_STATIC_MEMORY)
29413009
send_connack:
29423010
#endif
29433011
rc = MqttEncode_ConnectAck(bc->tx_buf, BROKER_CLIENT_TX_SZ(bc), &ack);

0 commit comments

Comments
 (0)