Skip to content

Commit

Permalink
Handle UTF-8 validation in packet__read_string.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Oct 3, 2018
1 parent d532253 commit 400db91
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 37 deletions.
6 changes: 6 additions & 0 deletions lib/packet_datatypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
19 changes: 1 addition & 18 deletions src/handle_connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 0 additions & 5 deletions src/handle_publish.c
Original file line number Diff line number Diff line change
Expand Up @@ -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; i<context->bridge->topic_count; i++){
Expand Down
7 changes: 0 additions & 7 deletions src/handle_subscribe.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 0 additions & 7 deletions src/handle_unsubscribe.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 400db91

Please sign in to comment.