Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PS-9673 fix of bug with double memory freeing in case of an error #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions libkmip/src/kmip_bio.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ int kmip_bio_create_symmetric_key(BIO *bio,
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(encode_result);
}
Expand All @@ -129,6 +130,7 @@ int kmip_bio_create_symmetric_key(BIO *bio,
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_IO_FAILURE);
}
Expand All @@ -155,6 +157,7 @@ int kmip_bio_create_symmetric_key(BIO *bio,
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_IO_FAILURE);
}
Expand All @@ -169,6 +172,7 @@ int kmip_bio_create_symmetric_key(BIO *bio,
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_EXCEED_MAX_MESSAGE_SIZE);
}
Expand All @@ -187,6 +191,7 @@ int kmip_bio_create_symmetric_key(BIO *bio,
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_IO_FAILURE);
}
Expand All @@ -200,6 +205,7 @@ int kmip_bio_create_symmetric_key(BIO *bio,
{
kmip_free_response_message(&ctx, &resp_m);
kmip_free_buffer(&ctx, encoding, buffer_total_size);
kmip_set_buffer(&ctx, NULL, 0);
encoding = NULL;
kmip_destroy(&ctx);
return(decode_result);
Expand All @@ -209,6 +215,7 @@ int kmip_bio_create_symmetric_key(BIO *bio,
{
kmip_free_response_message(&ctx, &resp_m);
kmip_free_buffer(&ctx, encoding, buffer_total_size);
kmip_set_buffer(&ctx, NULL, 0);
encoding = NULL;
kmip_destroy(&ctx);
return(KMIP_MALFORMED_RESPONSE);
Expand Down Expand Up @@ -364,6 +371,7 @@ int kmip_bio_register_symmetric_key(BIO *bio,
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(encode_result);
}
Expand All @@ -373,6 +381,7 @@ int kmip_bio_register_symmetric_key(BIO *bio,
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_IO_FAILURE);
}
Expand All @@ -399,6 +408,7 @@ int kmip_bio_register_symmetric_key(BIO *bio,
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_IO_FAILURE);
}
Expand All @@ -413,6 +423,7 @@ int kmip_bio_register_symmetric_key(BIO *bio,
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_EXCEED_MAX_MESSAGE_SIZE);
}
Expand All @@ -431,6 +442,7 @@ int kmip_bio_register_symmetric_key(BIO *bio,
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_IO_FAILURE);
}
Expand All @@ -443,8 +455,9 @@ int kmip_bio_register_symmetric_key(BIO *bio,
if(decode_result != KMIP_OK)
{
kmip_free_response_message(&ctx, &resp_m);
//kmip_free_buffer(&ctx, encoding, buffer_total_size);
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(decode_result);
}
Expand All @@ -453,6 +466,7 @@ int kmip_bio_register_symmetric_key(BIO *bio,
{
kmip_free_response_message(&ctx, &resp_m);
kmip_free_buffer(&ctx, encoding, buffer_total_size);
kmip_set_buffer(&ctx, NULL, 0);
encoding = NULL;
kmip_destroy(&ctx);
return(KMIP_MALFORMED_RESPONSE);
Expand Down Expand Up @@ -590,6 +604,7 @@ int kmip_bio_destroy_symmetric_key(BIO *bio, char *uuid, int uuid_size)
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(encode_result);
}
Expand All @@ -599,6 +614,7 @@ int kmip_bio_destroy_symmetric_key(BIO *bio, char *uuid, int uuid_size)
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_IO_FAILURE);
}
Expand All @@ -625,6 +641,7 @@ int kmip_bio_destroy_symmetric_key(BIO *bio, char *uuid, int uuid_size)
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_IO_FAILURE);
}
Expand All @@ -639,6 +656,7 @@ int kmip_bio_destroy_symmetric_key(BIO *bio, char *uuid, int uuid_size)
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_EXCEED_MAX_MESSAGE_SIZE);
}
Expand All @@ -649,6 +667,7 @@ int kmip_bio_destroy_symmetric_key(BIO *bio, char *uuid, int uuid_size)
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_MEMORY_ALLOC_FAILED);
}
Expand All @@ -668,6 +687,7 @@ int kmip_bio_destroy_symmetric_key(BIO *bio, char *uuid, int uuid_size)
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_IO_FAILURE);
}
Expand Down Expand Up @@ -704,6 +724,9 @@ int kmip_bio_destroy_symmetric_key(BIO *bio, char *uuid, int uuid_size)
kmip_free_response_message(&ctx, &resp_m);
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
// ctx->buffer is set to encoding, and encoding just freed, so we have to set ctx-buffer to NULL
// to avoid double freeing by the reference ctx->buffer
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(decode_result);
}
Expand All @@ -713,6 +736,7 @@ int kmip_bio_destroy_symmetric_key(BIO *bio, char *uuid, int uuid_size)
kmip_free_response_message(&ctx, &resp_m);
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_MALFORMED_RESPONSE);
}
Expand Down Expand Up @@ -814,6 +838,7 @@ int kmip_bio_activate_symmetric_key(BIO *bio, char *id, int id_size) {
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(encode_result);
}
Expand All @@ -823,6 +848,7 @@ int kmip_bio_activate_symmetric_key(BIO *bio, char *id, int id_size) {
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_IO_FAILURE);
}
Expand All @@ -849,6 +875,7 @@ int kmip_bio_activate_symmetric_key(BIO *bio, char *id, int id_size) {
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_IO_FAILURE);
}
Expand All @@ -863,6 +890,7 @@ int kmip_bio_activate_symmetric_key(BIO *bio, char *id, int id_size) {
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_EXCEED_MAX_MESSAGE_SIZE);
}
Expand All @@ -883,6 +911,7 @@ int kmip_bio_activate_symmetric_key(BIO *bio, char *id, int id_size) {
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_IO_FAILURE);
}
Expand All @@ -896,6 +925,7 @@ int kmip_bio_activate_symmetric_key(BIO *bio, char *id, int id_size) {
kmip_free_response_message(&ctx, &resp_m);
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(decode_result);
}
Expand Down Expand Up @@ -1028,6 +1058,7 @@ int kmip_bio_get_symmetric_key(BIO *bio,
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(encode_result);
}
Expand All @@ -1037,6 +1068,7 @@ int kmip_bio_get_symmetric_key(BIO *bio,
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_IO_FAILURE);
}
Expand All @@ -1063,6 +1095,7 @@ int kmip_bio_get_symmetric_key(BIO *bio,
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_IO_FAILURE);
}
Expand All @@ -1076,7 +1109,7 @@ int kmip_bio_get_symmetric_key(BIO *bio,
if(length > ctx.max_message_size)
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_EXCEED_MAX_MESSAGE_SIZE);
}
Expand All @@ -1097,6 +1130,7 @@ int kmip_bio_get_symmetric_key(BIO *bio,
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_IO_FAILURE);
}
Expand All @@ -1111,6 +1145,7 @@ int kmip_bio_get_symmetric_key(BIO *bio,
kmip_free_response_message(&ctx, &resp_m);
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(decode_result);
}
Expand Down Expand Up @@ -2412,6 +2447,7 @@ int kmip_bio_get_name_attribute(BIO *bio,
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(encode_result);
}
Expand All @@ -2421,6 +2457,7 @@ int kmip_bio_get_name_attribute(BIO *bio,
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_IO_FAILURE);
}
Expand All @@ -2447,6 +2484,7 @@ int kmip_bio_get_name_attribute(BIO *bio,
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_IO_FAILURE);
}
Expand All @@ -2461,6 +2499,7 @@ int kmip_bio_get_name_attribute(BIO *bio,
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_EXCEED_MAX_MESSAGE_SIZE);
}
Expand All @@ -2481,6 +2520,7 @@ int kmip_bio_get_name_attribute(BIO *bio,
{
kmip_free_buffer(&ctx, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&ctx, NULL, 0);
kmip_destroy(&ctx);
return(KMIP_IO_FAILURE);
}
Expand Down