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

upstream: Null-deref on TCP health checker if setsockopt fails #6793

Merged
merged 13 commits into from
May 10, 2019
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
12 changes: 8 additions & 4 deletions source/common/upstream/health_checker_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ TcpHealthCheckerImpl::TcpActiveHealthCheckSession::~TcpActiveHealthCheckSession(

void TcpHealthCheckerImpl::TcpActiveHealthCheckSession::onDeferredDelete() {
if (client_) {
expect_close_ = true;
client_->close(Network::ConnectionCloseType::NoFlush);
}
}
Expand All @@ -371,6 +372,7 @@ void TcpHealthCheckerImpl::TcpActiveHealthCheckSession::onData(Buffer::Instance&
data.drain(data.length());
handleSuccess(false);
if (!parent_.reuse_connection_) {
expect_close_ = true;
client_->close(Network::ConnectionCloseType::NoFlush);
}
} else {
Expand All @@ -379,12 +381,11 @@ void TcpHealthCheckerImpl::TcpActiveHealthCheckSession::onData(Buffer::Instance&
}

void TcpHealthCheckerImpl::TcpActiveHealthCheckSession::onEvent(Network::ConnectionEvent event) {
if (event == Network::ConnectionEvent::RemoteClose) {
handleFailure(envoy::data::core::v2alpha::HealthCheckFailureType::NETWORK);
}

if (event == Network::ConnectionEvent::RemoteClose ||
event == Network::ConnectionEvent::LocalClose) {
if (!expect_close_) {
handleFailure(envoy::data::core::v2alpha::HealthCheckFailureType::NETWORK);
}
parent_.dispatcher_.deferredDelete(std::move(client_));
}

Expand All @@ -403,6 +404,7 @@ void TcpHealthCheckerImpl::TcpActiveHealthCheckSession::onEvent(Network::Connect
// TODO(mattklein123): In the case that a user configured bytes to write, they will not be
// be written, since we currently have no way to know if the bytes actually get written via
// the connection interface. We might want to figure out how to handle this better later.
expect_close_ = true;
client_->close(Network::ConnectionCloseType::NoFlush);
handleSuccess(false);
}
Expand All @@ -416,6 +418,7 @@ void TcpHealthCheckerImpl::TcpActiveHealthCheckSession::onInterval() {
client_->addConnectionCallbacks(*session_callbacks_);
client_->addReadFilter(session_callbacks_);

expect_close_ = false;
client_->connect();
client_->noDelay(true);
}
Expand All @@ -431,6 +434,7 @@ void TcpHealthCheckerImpl::TcpActiveHealthCheckSession::onInterval() {
}

void TcpHealthCheckerImpl::TcpActiveHealthCheckSession::onTimeout() {
expect_close_ = true;
host_->setActiveHealthFailureType(Host::ActiveHealthFailureType::TIMEOUT);
client_->close(Network::ConnectionCloseType::NoFlush);
}
Expand Down
3 changes: 3 additions & 0 deletions source/common/upstream/health_checker_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ class TcpHealthCheckerImpl : public HealthCheckerImplBase {
TcpHealthCheckerImpl& parent_;
Network::ClientConnectionPtr client_;
std::shared_ptr<TcpSessionCallbacks> session_callbacks_;
// If true, stream close was initiated by us, not e.g. remote close or TCP reset.
// In this case healthcheck status already reported, only state cleanup required.
bool expect_close_{};
};

typedef std::unique_ptr<TcpActiveHealthCheckSession> TcpActiveHealthCheckSessionPtr;
Expand Down
Loading