From 400db91166f7bf594f5c62a99d507528727153d6 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Wed, 3 Oct 2018 15:04:24 +0100 Subject: [PATCH] Handle UTF-8 validation in packet__read_string. --- lib/packet_datatypes.c | 6 ++++++ src/handle_connect.c | 19 +------------------ src/handle_publish.c | 5 ----- src/handle_subscribe.c | 7 ------- src/handle_unsubscribe.c | 7 ------- 5 files changed, 7 insertions(+), 37 deletions(-) diff --git a/lib/packet_datatypes.c b/lib/packet_datatypes.c index 3065f124..9e31974f 100644 --- a/lib/packet_datatypes.c +++ b/lib/packet_datatypes.c @@ -108,6 +108,12 @@ int packet__read_string(struct mosquitto__packet *packet, char **str, int *lengt return MOSQ_ERR_NOMEM; } + if(mosquitto_validate_utf8(*str, slen)){ + mosquitto__free(*str); + *str = NULL; + return MOSQ_ERR_MALFORMED_UTF8; + } + *length = slen; return MOSQ_ERR_SUCCESS; } diff --git a/src/handle_connect.c b/src/handle_connect.c index 2384baab..e2780daf 100644 --- a/src/handle_connect.c +++ b/src/handle_connect.c @@ -292,11 +292,6 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context) } } - if(mosquitto_validate_utf8(client_id, slen) != MOSQ_ERR_SUCCESS){ - rc = 1; - goto handle_connect_error; - } - if(will){ will_struct = mosquitto__calloc(1, sizeof(struct mosquitto_message)); if(!will_struct){ @@ -311,14 +306,6 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context) rc = 1; goto handle_connect_error; } - if(mosquitto_validate_utf8(will_topic, slen)){ - log__printf(NULL, MOSQ_LOG_INFO, - "Malformed UTF-8 in will topic string from %s, disconnecting.", - client_id); - - rc = 1; - goto handle_connect_error; - } if(context->listener->mount_point){ slen = strlen(context->listener->mount_point) + strlen(will_topic) + 1; @@ -368,12 +355,8 @@ int handle__connect(struct mosquitto_db *db, struct mosquitto *context) if(username_flag){ rc = packet__read_string(&context->in_packet, &username, &slen); if(rc == MOSQ_ERR_SUCCESS){ - if(mosquitto_validate_utf8(username, slen) != MOSQ_ERR_SUCCESS){ - rc = MOSQ_ERR_PROTOCOL; - goto handle_connect_error; - } - if(password_flag){ + /* FIXME - MQTT 5 this is binary data */ rc = packet__read_string(&context->in_packet, &password, &slen); if(rc == MOSQ_ERR_NOMEM){ rc = MOSQ_ERR_NOMEM; diff --git a/src/handle_publish.c b/src/handle_publish.c index a6a10d4f..e9aa7016 100644 --- a/src/handle_publish.c +++ b/src/handle_publish.c @@ -70,11 +70,6 @@ int handle__publish(struct mosquitto_db *db, struct mosquitto *context) return 1; } - if(mosquitto_validate_utf8(topic, slen) != MOSQ_ERR_SUCCESS){ - mosquitto__free(topic); - return 1; - } - #ifdef WITH_BRIDGE if(context->bridge && context->bridge->topics && context->bridge->topic_remapping){ for(i=0; ibridge->topic_count; i++){ diff --git a/src/handle_subscribe.c b/src/handle_subscribe.c index 6f3286e1..e2f49e37 100644 --- a/src/handle_subscribe.c +++ b/src/handle_subscribe.c @@ -73,13 +73,6 @@ int handle__subscribe(struct mosquitto_db *db, struct mosquitto *context) mosquitto__free(payload); return 1; } - if(mosquitto_validate_utf8(sub, slen)){ - log__printf(NULL, MOSQ_LOG_INFO, - "Malformed UTF-8 in subscription string from %s, disconnecting.", - context->id); - mosquitto__free(sub); - return 1; - } if(packet__read_byte(&context->in_packet, &qos)){ mosquitto__free(sub); diff --git a/src/handle_unsubscribe.c b/src/handle_unsubscribe.c index 4a5bf680..3bb8bd4a 100644 --- a/src/handle_unsubscribe.c +++ b/src/handle_unsubscribe.c @@ -74,13 +74,6 @@ int handle__unsubscribe(struct mosquitto_db *db, struct mosquitto *context) mosquitto__free(sub); return 1; } - if(mosquitto_validate_utf8(sub, slen)){ - log__printf(NULL, MOSQ_LOG_INFO, - "Malformed UTF-8 in unsubscription string from %s, disconnecting.", - context->id); - mosquitto__free(sub); - return 1; - } log__printf(NULL, MOSQ_LOG_DEBUG, "\t%s", sub); sub__remove(db, context, sub, db->subs);