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

Fix leak in early data #6957

Merged
merged 1 commit into from
Jul 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions iocore/net/SSLUtils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1828,10 +1828,6 @@ SSLAccept(SSL *ssl)

if (SSLConfigParams::server_max_early_data > 0 && !netvc->early_data_finish) {
size_t nread;
if (netvc->early_data_buf == nullptr) {
netvc->early_data_buf = new_MIOBuffer(BUFFER_SIZE_INDEX_16K);
netvc->early_data_reader = netvc->early_data_buf->alloc_reader();
}

while (true) {
IOBufferBlock *block = new_IOBufferBlock();
Expand All @@ -1840,9 +1836,14 @@ SSLAccept(SSL *ssl)

if (ret == SSL_READ_EARLY_DATA_ERROR) {
Debug("ssl_early_data", "SSL_READ_EARLY_DATA_ERROR");
block->free();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like a common pattern in our code that we call a free() member function to deallocate class instances. Maybe we should add a utility to itscore that uses std::unique_ptr to call free(). https://godbolt.org/z/G-g7CH It would help avoid accidental leaks like this in future code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this idea.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how that's useful here, but maybe I'm missing something. You can't free things here when it goes out of scope, and you don't want to let it sit there if it's not going to be used either. Once used, it'll have to be freed up later, when it's no longer needed (you are collecting this early data as part of the handshake, to be used later by the HttpSM).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

netvc->early_data_buf->append_block(block); would have to change to netvc->early_data_buf->append_block(block.release());

break;
} else {
if (nread > 0) {
if (netvc->early_data_buf == nullptr) {
netvc->early_data_buf = new_MIOBuffer(BUFFER_SIZE_INDEX_16K);
netvc->early_data_reader = netvc->early_data_buf->alloc_reader();
}
block->fill(nread);
netvc->early_data_buf->append_block(block);
SSL_INCREMENT_DYN_STAT(ssl_early_data_received_count);
Expand All @@ -1851,13 +1852,15 @@ SSLAccept(SSL *ssl)
std::string early_data_str(reinterpret_cast<char *>(block->buf()), nread);
Debug("ssl_early_data_show_received", "Early data buffer: \n%s", early_data_str.c_str());
}
} else {
block->free();
}

if (ret == SSL_READ_EARLY_DATA_FINISH) {
netvc->early_data_finish = true;
Debug("ssl_early_data", "SSL_READ_EARLY_DATA_FINISH: size = %lu", nread);

if (netvc->early_data_reader->read_avail() == 0) {
if (netvc->early_data_reader == nullptr || netvc->early_data_reader->read_avail() == 0) {
Debug("ssl_early_data", "no data in early data buffer");
ERR_clear_error();
ret = SSL_accept(ssl);
Expand Down