Skip to content

Commit 73630d5

Browse files
authored
[Support] Error if SocketPath is too long (llvm#148903)
If the path is longer than sockaddr_un's buffer, it will be truncated, at which point it may become indistinguishable from similar truncated paths. This will cause `bind` to fail with an "Address already in use" error. There is some existing code that checks `fs::exists` to catch these errors, but since `fs::exists` compares the full un-truncated paths, a too-long path will prevent those checks from working. rdar://154397133
1 parent b117ccf commit 73630d5

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

llvm/lib/Support/raw_socket_stream.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ ListeningSocket::ListeningSocket(ListeningSocket &&LS)
119119
Expected<ListeningSocket> ListeningSocket::createUnix(StringRef SocketPath,
120120
int MaxBacklog) {
121121

122+
// If SocketPath is too long, the path will be truncated, and there may be
123+
// collisions with other truncated addresses that the fs::exists check below
124+
// will be unable to detect.
125+
if (SocketPath.size() >= sizeof(sockaddr_un::sun_path))
126+
return llvm::make_error<StringError>(
127+
std::make_error_code(std::errc::filename_too_long),
128+
"SocketPath too long");
129+
122130
// Handle instances where the target socket address already exists and
123131
// differentiate between a preexisting file with and without a bound socket
124132
//

0 commit comments

Comments
 (0)