From 8e9676c28c3917219cd5c85ff03a7b008f2e308d Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 21 Apr 2022 16:23:05 +0200 Subject: [PATCH] Kernel: Report AF_UNIX address family when accepting local sockets Previously we just wrote the local socket bind path into the sockaddr_un buffer. With this patch, we actually report the family as well. --- Kernel/Net/LocalSocket.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Kernel/Net/LocalSocket.cpp b/Kernel/Net/LocalSocket.cpp index b2c2d11db7..8de56c6545 100644 --- a/Kernel/Net/LocalSocket.cpp +++ b/Kernel/Net/LocalSocket.cpp @@ -100,14 +100,20 @@ LocalSocket::~LocalSocket() void LocalSocket::get_local_address(sockaddr* address, socklen_t* address_size) { + auto& address_un = *reinterpret_cast(address); + address_un = { + .sun_family = AF_UNIX, + .sun_path = {}, + }; + if (!m_path || m_path->is_empty()) { - size_t bytes_to_copy = min(static_cast(*address_size), sizeof(sockaddr_un)); - memset(address, 0, bytes_to_copy); - } else { - size_t bytes_to_copy = min(m_path->length(), min(static_cast(*address_size), sizeof(sockaddr_un))); - memcpy(address, m_path->characters(), bytes_to_copy); + *address_size = sizeof(address_un.sun_family); + return; } - *address_size = sizeof(sockaddr_un); + + size_t bytes_to_copy = min(m_path->length() + 1, min(static_cast(*address_size), sizeof(address_un.sun_path))); + memcpy(address_un.sun_path, m_path->characters(), bytes_to_copy); + *address_size = sizeof(address_un.sun_family) + bytes_to_copy; } void LocalSocket::get_peer_address(sockaddr* address, socklen_t* address_size)