Bug in UNIX Domain Socket connect handling when addrlen
is smaller than sockaddr_un
size
#154
Labels
bug
Something isn't working
Description:
There is a bug in the handling of UNIX domain sockets in the
connect
method. According to the Linux man page for UNIX domain sockets ([man7.org - unix.7](https://man7.org/linux/man-pages/man7/unix.7.html)), thesockaddr_un
structure can be passed with a length smaller than the full size of the structure. For example, when using theconnect
system call in SSH, a length of 24 bytes is passed instead of the full size of 110 bytes forsockaddr_un
.However, the current implementation in the
connect
method assumes that theaddrlen
parameter must always be equal tosizeof(sockaddr_un)
. This can cause issues if the length is smaller than the structure size.Code Context:
In the current implementation, specifically in the UNIX socket handling part, the
addrlen
is not properly validated against the actual length of thesockaddr_un
structure:Problem:
sockaddr_un
structure can be smaller than its full size, depending on the actual address provided. For instance, SSH uses a length of 24 bytes instead of the full 110 bytes.addrlen
must match the full size ofsockaddr_un
, but this is not always true, leading to potential misbehavior when the length is smaller.Proposed Solution:
addrlen
is smaller thansizeof(sockaddr_un)
: Modify the validation logic to check thataddrlen
is at leastoffsetof(struct sockaddr_un, sun_path) + strlen(sun_path) + 1
, instead of checking for the full size of sockaddr_un.offsetof(struct sockaddr_un, sun_path)
returns 2, which is the offset ofsun_path
within the structure.strlen(sun_path)
is the actual length of the provided path, excluding the trailing null character (\0
). The+ 1
is to account for the null-terminator (\0
), which marks the end of the string.This will align the implementation with Linux's expected behavior for UNIX domain sockets.
Reference:
Additional Notes:
sockaddr_un
structure size, as specified in the man pages.addrlen
is used withsockaddr_un
.The text was updated successfully, but these errors were encountered: