[EventEngine] fix bug in unix-abstract socket URI processing (#33721)

URI should be resolved to unix abstract only if the first byte is NULL
but the path length is > 1 (i.e there is more than one byte in the
path). Otherwise treat is as unix URI.

Moreover, when the AsyncConnectionAcceptor is destroyed, if it was
managing a unix domain socket, then the socket should be unlinked to
ensure we dont leave the underlying file hanging around forever.
pull/33743/head
Vignesh Babu 1 year ago committed by GitHub
parent ff4c64e08f
commit aa7c982567
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      src/core/lib/event_engine/posix_engine/posix_engine_listener.cc
  2. 2
      src/core/lib/event_engine/posix_engine/posix_engine_listener.h
  3. 13
      src/core/lib/event_engine/tcp_socket_utils.cc

@ -182,11 +182,11 @@ void PosixEngineListenerImpl::AsyncConnectionAcceptor::NotifyOnAccept(
}
// For UNIX sockets, the accept call might not fill up the member
// sun_path of sockaddr_un, so explicitly call getsockname to get it.
// sun_path of sockaddr_un, so explicitly call getpeername to get it.
if (addr.address()->sa_family == AF_UNIX) {
socklen_t len = EventEngine::ResolvedAddress::MAX_SIZE_BYTES;
if (getsockname(fd, const_cast<sockaddr*>(addr.address()), &len) < 0) {
gpr_log(GPR_ERROR, "Closing acceptor. Failed getsockname: %s",
if (getpeername(fd, const_cast<sockaddr*>(addr.address()), &len) < 0) {
gpr_log(GPR_ERROR, "Closing acceptor. Failed getpeername: %s",
strerror(errno));
close(fd);
// Shutting down the acceptor. Unref the ref grabbed in

@ -110,6 +110,8 @@ class PosixEngineListenerImpl
}
ListenerSocketsContainer::ListenerSocket& Socket() { return socket_; }
~AsyncConnectionAcceptor() {
// If uds socket, unlink it so that the corresponding file is deleted.
UnlinkIfUnixDomainSocket(*socket_.sock.LocalAddress());
handle_->OrphanHandle(nullptr, nullptr, "");
delete notify_on_accept_;
}

@ -100,12 +100,11 @@ absl::StatusOr<std::string> ResolvedAddrToUnixPathIfPossible(
#else
int len = resolved_addr->size() - sizeof(unix_addr->sun_family) - 1;
#endif
bool abstract = (len < 0 || unix_addr->sun_path[0] == '\0');
if (len <= 0) return "";
std::string path;
if (abstract) {
if (len >= 0) {
path = std::string(unix_addr->sun_path + 1, len);
}
if (unix_addr->sun_path[0] == '\0') {
// unix-abstract socket processing.
path = std::string(unix_addr->sun_path + 1, len);
path = absl::StrCat(std::string(1, '\0'), path);
} else {
size_t maxlen = sizeof(unix_addr->sun_path);
@ -123,9 +122,9 @@ absl::StatusOr<std::string> ResolvedAddrToUriUnixIfPossible(
GRPC_RETURN_IF_ERROR(path.status());
std::string scheme;
std::string path_string;
if (path->at(0) == '\0') {
if (!path->empty() && path->at(0) == '\0' && path->length() > 1) {
scheme = "unix-abstract";
path_string = path->length() > 1 ? path->substr(1, std::string::npos) : "";
path_string = path->substr(1, std::string::npos);
} else {
scheme = "unix";
path_string = std::move(*path);

Loading…
Cancel
Save