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

openssl: Fix return value from tls_net_read. #4100

Merged
merged 1 commit into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions src/tls/flb_tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ int flb_tls_net_read(struct flb_upstream_conn *u_conn, void *buf, size_t len)
if (ret == FLB_TLS_WANT_READ) {
goto retry_read;
}
else if (ret == FLB_TLS_WANT_WRITE) {
goto retry_read;
}
else if (ret < 0) {
return -1;
}
Expand All @@ -206,6 +209,14 @@ int flb_tls_net_read_async(struct flb_coro *co, struct flb_upstream_conn *u_conn

goto retry_read;
}
else if (ret == FLB_TLS_WANT_WRITE) {
u_conn->coro = co;

io_tls_event_switch(u_conn, MK_EVENT_WRITE);
flb_coro_yield(co, FLB_FALSE);

goto retry_read;
}
else
{
/* We want this field to hold NULL at all times unless we are explicitly
Expand Down
8 changes: 7 additions & 1 deletion src/tls/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,17 @@ static int tls_net_read(struct flb_upstream_conn *u_conn,
ctx = session->parent;
pthread_mutex_lock(&ctx->mutex);

ERR_clear_error();
ret = SSL_read(session->ssl, buf, len);
if (ret <= 0) {
ret = SSL_get_error(session->ssl, ret);
if (ret == SSL_ERROR_WANT_READ) {
ret = FLB_TLS_WANT_READ;
}
else if (ret < 0) {
Copy link
Member

Choose a reason for hiding this comment

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

here is a thing (and I still need to clarify more): the error might describe that "wants to read or write" in the case of "SSL_ERROR_WANT_WRITE", by definition:

#define SSL_ERROR_WANT_WRITE 3

that is not an error and removing the < 0 will make that case to fail. Not sure about the impact of that change.

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 understand that. However it is already a bug. tls_net_read is returning 3 today for SSL_ERROR_WANT_WRITE which is worse because it makes the http client adds 3 garbage bytes to its response buffer. It doesn't log an error but it will corrupt memory later on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the change to handle SSL_ERROR_WANT_WRITE.

else if (ret == SSL_ERROR_WANT_WRITE) {
ret = FLB_TLS_WANT_WRITE;
}
else {
ret = -1;
}
}
Expand All @@ -379,6 +383,7 @@ static int tls_net_write(struct flb_upstream_conn *u_conn,
ctx = session->parent;
pthread_mutex_lock(&ctx->mutex);

ERR_clear_error();
ret = SSL_write(session->ssl,
(unsigned char *) data + total,
len - total);
Expand Down Expand Up @@ -414,6 +419,7 @@ static int tls_net_handshake(struct flb_tls *tls, void *ptr_session)
SSL_set_tlsext_host_name(session->ssl, tls->vhost);
}

ERR_clear_error();
ret = SSL_connect(session->ssl);
if (ret != 1) {
ret = SSL_get_error(session->ssl, ret);
Expand Down