Skip to content

Commit

Permalink
Fix a leak in SSL_clear()
Browse files Browse the repository at this point in the history
SSL_clear() was resetting numwpipes to 0, but not freeing any allocated
memory for existing write buffers.

Fixes #2026

Reviewed-by: Rich Salz <rsalz@openssl.org>
  • Loading branch information
mattcaswell committed Dec 12, 2016
1 parent 82e0893 commit 4bf0860
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
6 changes: 1 addition & 5 deletions ssl/record/rec_layer_s3.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ void RECORD_LAYER_init(RECORD_LAYER *rl, SSL *s)

void RECORD_LAYER_clear(RECORD_LAYER *rl)
{
unsigned int pipes;

rl->rstate = SSL_ST_READ_HEADER;

/*
Expand All @@ -62,9 +60,7 @@ void RECORD_LAYER_clear(RECORD_LAYER *rl)
rl->wpend_buf = NULL;

SSL3_BUFFER_clear(&rl->rbuf);
for (pipes = 0; pipes < rl->numwpipes; pipes++)
SSL3_BUFFER_clear(&rl->wbuf[pipes]);
rl->numwpipes = 0;
ssl3_release_write_buffer(rl->s);
rl->numrpipes = 0;
SSL3_RECORD_clear(rl->rrec, SSL_MAX_PIPELINES);

Expand Down
12 changes: 8 additions & 4 deletions ssl/record/ssl3_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,17 @@ int ssl3_setup_write_buffer(SSL *s, size_t numwpipes, size_t len)

wb = RECORD_LAYER_get_wbuf(&s->rlayer);
for (currpipe = 0; currpipe < numwpipes; currpipe++) {
if (wb[currpipe].buf == NULL) {
if ((p = OPENSSL_malloc(len)) == NULL) {
SSL3_BUFFER *thiswb = &wb[currpipe];

if (thiswb->buf == NULL) {
p = OPENSSL_malloc(len);
if (p == NULL) {
s->rlayer.numwpipes = currpipe;
goto err;
}
wb[currpipe].buf = p;
wb[currpipe].len = len;
memset(thiswb, 0, sizeof(SSL3_BUFFER));
thiswb->buf = p;
thiswb->len = len;
}
}

Expand Down
10 changes: 9 additions & 1 deletion test/sslapitest.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,16 @@ static int execute_test_large_message(const SSL_METHOD *smeth,
goto end;
}

testresult = 1;
/*
* Calling SSL_clear() first is not required but this tests that SSL_clear()
* doesn't leak (when using enable-crypto-mdebug).
*/
if (!SSL_clear(serverssl)) {
printf("Unexpected failure from SSL_clear()\n");
goto end;
}

testresult = 1;
end:
X509_free(chaincert);
SSL_free(serverssl);
Expand Down

0 comments on commit 4bf0860

Please sign in to comment.