Skip to content

Commit

Permalink
Free memory if BIO_write() fails
Browse files Browse the repository at this point in the history
  • Loading branch information
aveenismail committed Sep 4, 2024
1 parent 1883b5a commit aa61d73
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 4 additions & 0 deletions common/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ bool read_private_key(uint8_t *buf, size_t len, yh_algorithm *algo,
}

if(BIO_write(bio, buf, len) <= 0) {
BIO_free_all(bio);
return false;
}

Expand Down Expand Up @@ -697,12 +698,15 @@ bool write_file(const uint8_t *buf, size_t buf_len, FILE *fp, format_t format) {

(void) BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
if(BIO_write(bio, buf, buf_len) <= 0) {
BIO_free_all(bio);
return false;
}
if(BIO_flush(bio) != 1) {
BIO_free_all(bio);
return false;
}
if(BIO_get_mem_ptr(bio, &bufferPtr) != 1) {
BIO_free_all(bio);
return false;
}
p = (uint8_t *) bufferPtr->data;
Expand Down
17 changes: 15 additions & 2 deletions src/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,13 @@ int yh_com_get_pubkey(yubihsm_context *ctx, Argument *argv, cmd_format in_fmt,

(void) i2d_PUBKEY_bio(bio, public_key);

(void) BIO_flush(bio);
if (BIO_flush(bio) != 1) {
fprintf(stderr, "Unable to flush BIO\n");
BIO_free_all(b64);
BIO_free_all(bio);
error = true;
goto getpk_base64_cleanup;
}
(void) BIO_free_all(bio);
getpk_base64_cleanup:
if (error) {
Expand Down Expand Up @@ -1260,7 +1266,13 @@ int yh_com_get_device_pubkey(yubihsm_context *ctx, Argument *argv,

(void) i2d_PUBKEY_bio(bio, public_key);

(void) BIO_flush(bio);
if (BIO_flush(bio) != 1) {
fprintf(stderr, "Unable to flush BIO\n");
BIO_free_all(b64);
BIO_free_all(bio);
error = true;
goto getdpk_base64_cleanup;
}
(void) BIO_free_all(bio);
getdpk_base64_cleanup:
if (error) {
Expand Down Expand Up @@ -2454,6 +2466,7 @@ static bool read_rsa_pubkey(const uint8_t *buf, size_t len,

if(BIO_write(bio, buf, len) <= 0) {
fprintf(stderr, "%s: Failed to read RSA public key\n", __func__);
BIO_free_all(bio);
return false;
}

Expand Down

0 comments on commit aa61d73

Please sign in to comment.