Skip to content

Commit

Permalink
Fix mismatched alloc/frees
Browse files Browse the repository at this point in the history
  • Loading branch information
ralight committed Apr 25, 2024
1 parent 48eddd5 commit db6ab68
Show file tree
Hide file tree
Showing 25 changed files with 79 additions and 84 deletions.
68 changes: 34 additions & 34 deletions common/password_mosq.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ static int pw__create_argon2id(struct mosquitto_pw *pw, const char *password)
size_t encoded_len = argon2_encodedlen(MOSQ_ARGON2_T, MOSQ_ARGON2_M, MOSQ_ARGON2_P,
(uint32_t)pw->params.argon2id.salt_len, sizeof(pw->params.argon2id.password_hash), Argon2_id);

free(pw->encoded_password);
pw->encoded_password = calloc(1, encoded_len+1);
mosquitto_free(pw->encoded_password);
pw->encoded_password = mosquitto_calloc(1, encoded_len+1);

rc = argon2id_hash_encoded(MOSQ_ARGON2_T, MOSQ_ARGON2_M, MOSQ_ARGON2_P,
password, strlen(password),
Expand Down Expand Up @@ -131,10 +131,10 @@ static int pw__verify_argon2id(struct mosquitto_pw *pw, const char *password)
static int pw__decode_argon2id(struct mosquitto_pw *pw, const char *password)
{
#ifdef WITH_ARGON2
char *new_password = strdup(password);
char *new_password = mosquitto_strdup(password);

if(new_password){
free(pw->encoded_password);
mosquitto_free(pw->encoded_password);
pw->encoded_password = new_password;
return MOSQ_ERR_SUCCESS;
}else{
Expand Down Expand Up @@ -227,19 +227,19 @@ static int pw__encode_sha512_pbkdf2(struct mosquitto_pw *pw)

rc = mosquitto_base64_encode(pw->params.sha512_pbkdf2.password_hash, sizeof(pw->params.sha512_pbkdf2.password_hash), &hash64);
if(rc){
free(salt64);
mosquitto_free(salt64);
return MOSQ_ERR_UNKNOWN;
}

free(pw->encoded_password);
mosquitto_free(pw->encoded_password);
size_t len = strlen("$6$$") + strlen("1,000,000,000,000") + strlen(salt64) + strlen(hash64) + 1;
pw->encoded_password = calloc(1, len);
pw->encoded_password = mosquitto_calloc(1, len);
if(!pw->encoded_password) return MOSQ_ERR_NOMEM;

snprintf(pw->encoded_password, len, "$%d$%d$%s$%s", pw->hashtype, pw->params.sha512_pbkdf2.iterations, salt64, hash64);

free(salt64);
free(hash64);
mosquitto_free(salt64);
mosquitto_free(hash64);

return MOSQ_ERR_SUCCESS;
#else
Expand All @@ -257,51 +257,51 @@ static int pw__decode_sha512_pbkdf2(struct mosquitto_pw *pw, const char *salt_pa
unsigned int salt_len, password_len;
int rc;

sp_heap = strdup(salt_password);
sp_heap = mosquitto_strdup(salt_password);
if(!sp_heap) return MOSQ_ERR_NOMEM;

iterations_s = strtok_r(sp_heap, "$", &saveptr);
if(iterations_s == NULL){
free(sp_heap);
mosquitto_free(sp_heap);
return MOSQ_ERR_INVAL;
}
pw->params.sha512_pbkdf2.iterations = atoi(iterations_s);
if(pw->params.sha512_pbkdf2.iterations < 1){
free(sp_heap);
mosquitto_free(sp_heap);
return MOSQ_ERR_INVAL;
}

salt_b64 = strtok_r(NULL, "$", &saveptr);
if(salt_b64 == NULL){
free(sp_heap);
mosquitto_free(sp_heap);
return MOSQ_ERR_INVAL;
}

rc = mosquitto_base64_decode(salt_b64, &salt, &salt_len);
if(rc != MOSQ_ERR_SUCCESS || (salt_len != 12 && salt_len != HASH_LEN)){
free(sp_heap);
free(salt);
mosquitto_free(sp_heap);
mosquitto_free(salt);
return MOSQ_ERR_INVAL;
}
memcpy(pw->params.sha512_pbkdf2.salt, salt, salt_len);
free(salt);
mosquitto_free(salt);
pw->params.sha512_pbkdf2.salt_len = salt_len;

password_b64 = strtok_r(NULL, "$", &saveptr);
if(password_b64 == NULL){
free(sp_heap);
mosquitto_free(sp_heap);
return MOSQ_ERR_INVAL;
}

rc = mosquitto_base64_decode(password_b64, &password, &password_len);
free(sp_heap);
mosquitto_free(sp_heap);

if(rc != MOSQ_ERR_SUCCESS || password_len != HASH_LEN){
free(password);
mosquitto_free(password);
return MOSQ_ERR_INVAL;
}
memcpy(pw->params.sha512_pbkdf2.password_hash, password, password_len);
free(password);
mosquitto_free(password);

return MOSQ_ERR_SUCCESS;
#else
Expand Down Expand Up @@ -400,15 +400,15 @@ static int pw__encode_sha512(struct mosquitto_pw *pw)
return MOSQ_ERR_UNKNOWN;
}

free(pw->encoded_password);
mosquitto_free(pw->encoded_password);
size_t len = strlen("$6$$") + strlen(salt64) + strlen(hash64) + 1;
pw->encoded_password = calloc(1, len);
pw->encoded_password = mosquitto_calloc(1, len);
if(!pw->encoded_password) return MOSQ_ERR_NOMEM;

snprintf(pw->encoded_password, len, "$%d$%s$%s", pw->hashtype, salt64, hash64);

free(salt64);
free(hash64);
mosquitto_free(salt64);
mosquitto_free(hash64);

return MOSQ_ERR_SUCCESS;
#else
Expand All @@ -425,40 +425,40 @@ static int pw__decode_sha512(struct mosquitto_pw *pw, const char *salt_password)
unsigned int salt_len, password_len;
int rc;

sp_heap = strdup(salt_password);
sp_heap = mosquitto_strdup(salt_password);
if(!sp_heap) return MOSQ_ERR_NOMEM;

salt_b64 = strtok_r(sp_heap, "$", &saveptr);
if(salt_b64 == NULL){
free(sp_heap);
mosquitto_free(sp_heap);
return MOSQ_ERR_INVAL;
}

rc = mosquitto_base64_decode(salt_b64, &salt, &salt_len);
if(rc != MOSQ_ERR_SUCCESS || (salt_len != 12 && salt_len != HASH_LEN)){
free(sp_heap);
free(salt);
mosquitto_free(sp_heap);
mosquitto_free(salt);
return MOSQ_ERR_INVAL;
}
memcpy(pw->params.sha512.salt, salt, salt_len);
free(salt);
mosquitto_free(salt);
pw->params.sha512.salt_len = salt_len;

password_b64 = strtok_r(NULL, "$", &saveptr);
if(password_b64 == NULL){
free(sp_heap);
mosquitto_free(sp_heap);
return MOSQ_ERR_INVAL;
}

rc = mosquitto_base64_decode(password_b64, &password, &password_len);
free(sp_heap);
mosquitto_free(sp_heap);

if(rc != MOSQ_ERR_SUCCESS || password_len != HASH_LEN){
free(password);
mosquitto_free(password);
return MOSQ_ERR_INVAL;
}
memcpy(pw->params.sha512.password_hash, password, password_len);
free(password);
mosquitto_free(password);

return MOSQ_ERR_SUCCESS;
#else
Expand Down Expand Up @@ -541,7 +541,7 @@ int pw__decode(struct mosquitto_pw *pw, const char *password)
void pw__cleanup(struct mosquitto_pw *pw)
{
if(pw){
free(pw->encoded_password);
mosquitto_free(pw->encoded_password);
pw->encoded_password = NULL;
}
}
2 changes: 1 addition & 1 deletion lib/handle_connack.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ int handle__connack(struct mosquitto *mosq)
if(mosq->id){
/* We've been sent a client identifier but already have one. This
* shouldn't happen. */
SAFE_FREE(clientid);
mosquitto_FREE(clientid);
mosquitto_property_free_all(&properties);
return MOSQ_ERR_PROTOCOL;
}else{
Expand Down
4 changes: 2 additions & 2 deletions lib/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ libmosq_EXPORT int mosquitto_subscribe_simple(

*messages = NULL;

userdata.messages = calloc(sizeof(struct mosquitto_message), (size_t)msg_count);
userdata.messages = mosquitto_calloc(sizeof(struct mosquitto_message), (size_t)msg_count);
if(!userdata.messages){
return MOSQ_ERR_NOMEM;
}
Expand All @@ -137,7 +137,7 @@ libmosq_EXPORT int mosquitto_subscribe_simple(
for(i=0; i<msg_count; i++){
mosquitto_message_free_contents(&userdata.messages[i]);
}
SAFE_FREE(userdata.messages);
mosquitto_FREE(userdata.messages);
return rc;
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/http_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ int http_c__context_init(struct mosquitto *context)
"Sec-WebSocket-Protocol: mqtt\r\n"
"Sec-WebSocket-Version: 13\r\n"
"\r\n", path, context->host, key);
SAFE_FREE(key);
mosquitto_FREE(key);
packet->packet_length += WS_PACKET_OFFSET;
packet->to_process = packet->packet_length;
context->http_request[0] = '\0';
Expand All @@ -86,7 +86,7 @@ int http_c__context_init(struct mosquitto *context)

int http_c__context_cleanup(struct mosquitto *context)
{
SAFE_FREE(context->wsd.accept_key);
mosquitto_FREE(context->wsd.accept_key);
mosquitto_FREE(context->http_request);
return MOSQ_ERR_SUCCESS;
}
Expand Down
6 changes: 3 additions & 3 deletions libcommon/base64_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int mosquitto_base64_encode(const unsigned char *in, size_t in_len, char **encod

if(BIO_flush(b64) == 1){
BIO_get_mem_ptr(b64, &bptr);
*encoded = malloc(bptr->length+1);
*encoded = mosquitto_malloc(bptr->length+1);
if(*encoded){
memcpy(*encoded, bptr->data, bptr->length);
(*encoded)[bptr->length] = '\0';
Expand Down Expand Up @@ -79,15 +79,15 @@ int mosquitto_base64_decode(const char *in, unsigned char **decoded, unsigned in
BIO_write(bmem, in, (int)slen);

if(BIO_flush(bmem) == 1){
*decoded = calloc(slen, 1);
*decoded = mosquitto_calloc(slen, 1);

if(*decoded){
len = BIO_read(b64, *decoded, (int)slen);
if(len > 0){
*decoded_len = (unsigned int)len;
rc = 0;
}else{
free(*decoded);
mosquitto_free(*decoded);
*decoded = NULL;
}
}
Expand Down
22 changes: 11 additions & 11 deletions libcommon/property_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void mosquitto_property_free(mosquitto_property **property)
break;
}

free(*property);
mosquitto_free(*property);
*property = NULL;
}

Expand Down Expand Up @@ -819,7 +819,7 @@ BROKER_EXPORT const mosquitto_property *mosquitto_property_read_binary(const mos
if(value){
*len = p->value.bin.len;
if(p->value.bin.len){
*value = calloc(1, *len + 1U);
*value = mosquitto_calloc(1, *len + 1U);
if(!(*value)) return NULL;

memcpy(*value, p->value.bin.v, *len);
Expand Down Expand Up @@ -852,7 +852,7 @@ BROKER_EXPORT const mosquitto_property *mosquitto_property_read_string(const mos

if(value){
if(p->value.s.len){
*value = calloc(1, (size_t)p->value.s.len+1);
*value = mosquitto_calloc(1, (size_t)p->value.s.len+1);
if(!(*value)) return NULL;

memcpy(*value, p->value.s.v, p->value.s.len);
Expand All @@ -879,7 +879,7 @@ BROKER_EXPORT const mosquitto_property *mosquitto_property_read_string_pair(cons

if(name){
if(p->name.len){
*name = calloc(1, (size_t)p->name.len+1);
*name = mosquitto_calloc(1, (size_t)p->name.len+1);
if(!(*name)) return NULL;
memcpy(*name, p->name.v, p->name.len);
}else{
Expand All @@ -889,10 +889,10 @@ BROKER_EXPORT const mosquitto_property *mosquitto_property_read_string_pair(cons

if(value){
if(p->value.s.len){
*value = calloc(1, (size_t)p->value.s.len+1);
*value = mosquitto_calloc(1, (size_t)p->value.s.len+1);
if(!(*value)){
if(name){
free(*name);
mosquitto_free(*name);
*name = NULL;
}
return NULL;
Expand Down Expand Up @@ -944,7 +944,7 @@ BROKER_EXPORT int mosquitto_property_copy_all(mosquitto_property **dest, const m
*dest = NULL;

while(src){
pnew = calloc(1, sizeof(mosquitto_property));
pnew = mosquitto_calloc(1, sizeof(mosquitto_property));
if(!pnew){
mosquitto_property_free_all(dest);
return MOSQ_ERR_NOMEM;
Expand Down Expand Up @@ -978,7 +978,7 @@ BROKER_EXPORT int mosquitto_property_copy_all(mosquitto_property **dest, const m

case MQTT_PROP_TYPE_STRING:
pnew->value.s.len = src->value.s.len;
pnew->value.s.v = src->value.s.v ? strdup(src->value.s.v) : (char*)calloc(1,1);
pnew->value.s.v = src->value.s.v ? mosquitto_strdup(src->value.s.v) : (char*)mosquitto_calloc(1,1);
if(!pnew->value.s.v){
mosquitto_property_free_all(dest);
return MOSQ_ERR_NOMEM;
Expand All @@ -988,7 +988,7 @@ BROKER_EXPORT int mosquitto_property_copy_all(mosquitto_property **dest, const m
case MQTT_PROP_TYPE_BINARY:
pnew->value.bin.len = src->value.bin.len;
if(src->value.bin.len){
pnew->value.bin.v = malloc(pnew->value.bin.len);
pnew->value.bin.v = mosquitto_malloc(pnew->value.bin.len);
if(!pnew->value.bin.v){
mosquitto_property_free_all(dest);
return MOSQ_ERR_NOMEM;
Expand All @@ -999,14 +999,14 @@ BROKER_EXPORT int mosquitto_property_copy_all(mosquitto_property **dest, const m

case MQTT_PROP_TYPE_STRING_PAIR:
pnew->value.s.len = src->value.s.len;
pnew->value.s.v = src->value.s.v ? strdup(src->value.s.v) : (char*)calloc(1,1);
pnew->value.s.v = src->value.s.v ? mosquitto_strdup(src->value.s.v) : (char*)mosquitto_calloc(1,1);
if(!pnew->value.s.v){
mosquitto_property_free_all(dest);
return MOSQ_ERR_NOMEM;
}

pnew->name.len = src->name.len;
pnew->name.v = src->name.v ? strdup(src->name.v) : (char*)calloc(1,1);
pnew->name.v = src->name.v ? mosquitto_strdup(src->name.v) : (char*)mosquitto_calloc(1,1);
if(!pnew->name.v){
mosquitto_property_free_all(dest);
return MOSQ_ERR_NOMEM;
Expand Down
8 changes: 4 additions & 4 deletions plugins/dynamic-security/groups.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ int dynsec_groups__config_load(struct dynsec__data *data, cJSON *tree)
const char *textname;
if(json_get_string(j_group, "textname", &textname, false) == MOSQ_ERR_SUCCESS){
if(textname){
group->text_name = strdup(textname);
group->text_name = mosquitto_strdup(textname);
if(group->text_name == NULL){
mosquitto_free(group);
continue;
Expand All @@ -242,7 +242,7 @@ int dynsec_groups__config_load(struct dynsec__data *data, cJSON *tree)
const char *textdescription;
if(json_get_string(j_group, "textdescription", &textdescription, false) == MOSQ_ERR_SUCCESS){
if(textdescription){
group->text_description = strdup(textdescription);
group->text_description = mosquitto_strdup(textdescription);
if(group->text_description == NULL){
mosquitto_free(group->text_name);
mosquitto_free(group);
Expand Down Expand Up @@ -411,15 +411,15 @@ int dynsec_groups__process_create(struct dynsec__data *data, struct mosquitto_co
}
strncpy(group->groupname, groupname, groupname_len+1);
if(text_name){
group->text_name = strdup(text_name);
group->text_name = mosquitto_strdup(text_name);
if(group->text_name == NULL){
mosquitto_control_command_reply(cmd, "Internal error");
group__free_item(data, group);
return MOSQ_ERR_NOMEM;
}
}
if(text_description){
group->text_description = strdup(text_description);
group->text_description = mosquitto_strdup(text_description);
if(group->text_description == NULL){
mosquitto_control_command_reply(cmd, "Internal error");
group__free_item(data, group);
Expand Down

0 comments on commit db6ab68

Please sign in to comment.