Visitar URL original
Throw exceptions when rule violating UDS paths been passed in. (#11663) · netty/netty@a53eb80 · GitHub
Skip to content

Commit a53eb80

Browse files
authored
Throw exceptions when rule violating UDS paths been passed in. (#11663)
Motivation: Currently, Netty is silently truncating all over the limit UDS paths and ignoring the `sun_path`'s null-termination role, which hurts compatibility with other UDS clients and servers. Modifications: Adding a validation in the JNI code, if the UDS path is not satisfying the system limit or Linux spec throw a NativeIoException. Result: All UDS paths Netty can successfully bind are connectable by other programs.
1 parent a329857 commit a53eb80

2 files changed

Lines changed: 38 additions & 5 deletions

File tree

‎transport-native-epoll/src/test/java/io/netty/channel/epoll/LinuxSocketTest.java‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
*/
1616
package io.netty.channel.epoll;
1717

18+
import io.netty.channel.unix.DomainSocketAddress;
19+
import io.netty.channel.unix.Errors.NativeIoException;
20+
import io.netty.channel.unix.Socket;
21+
import java.nio.charset.Charset;
22+
import java.util.Random;
23+
import java.util.UUID;
24+
import org.junit.jupiter.api.Assertions;
1825
import org.junit.jupiter.api.BeforeAll;
1926

2027
import java.io.IOException;
@@ -65,4 +72,28 @@ public void execute() throws Throwable {
6572
socket.close();
6673
}
6774
}
75+
76+
@Test
77+
public void testUnixDomainSocketTooLongPathFails() throws IOException {
78+
// Most systems has a limit for UDS path of 108, 255 is generally too long.
79+
StringBuilder socketPath = new StringBuilder("/tmp/");
80+
while (socketPath.length() < 255) {
81+
socketPath.append(UUID.randomUUID());
82+
}
83+
84+
final DomainSocketAddress domainSocketAddress = new DomainSocketAddress(
85+
socketPath.toString());
86+
final Socket socket = Socket.newSocketDomain();
87+
try {
88+
Exception exception = Assertions.assertThrows(NativeIoException.class, new Executable() {
89+
@Override
90+
public void execute() throws Throwable {
91+
socket.bind(domainSocketAddress);
92+
}
93+
});
94+
Assertions.assertTrue(exception.getMessage().contains("too long"));
95+
} finally {
96+
socket.close();
97+
}
98+
}
6899
}

‎transport-native-unix-common/src/main/c/netty_unix_socket.c‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -764,12 +764,13 @@ static jint netty_unix_socket_bindDomainSocket(JNIEnv* env, jclass clazz, jint f
764764

765765
jbyte* socket_path = (*env)->GetByteArrayElements(env, socketPath, 0);
766766
jint socket_path_len = (*env)->GetArrayLength(env, socketPath);
767-
if (socket_path_len > sizeof(addr.sun_path)) {
768-
socket_path_len = sizeof(addr.sun_path);
767+
768+
if (socket_path_len > sizeof(addr.sun_path) || (socket_path_len == sizeof(addr.sun_path) && socket_path[socket_path_len] != '\0')) {
769+
return -ENAMETOOLONG;
769770
}
770771
memcpy(addr.sun_path, socket_path, socket_path_len);
771772

772-
if (unlink((const char*) socket_path) == -1 && errno != ENOENT) {
773+
if (unlink((const char*) addr.sun_path) == -1 && errno != ENOENT) {
773774
return -errno;
774775
}
775776

@@ -791,8 +792,9 @@ static jint netty_unix_socket_connectDomainSocket(JNIEnv* env, jclass clazz, jin
791792

792793
jbyte* socket_path = (*env)->GetByteArrayElements(env, socketPath, 0);
793794
socket_path_len = (*env)->GetArrayLength(env, socketPath);
794-
if (socket_path_len > sizeof(addr.sun_path)) {
795-
socket_path_len = sizeof(addr.sun_path);
795+
796+
if (socket_path_len > sizeof(addr.sun_path) || (socket_path_len == sizeof(addr.sun_path) && socket_path[socket_path_len] != '\0')) {
797+
return -ENAMETOOLONG;
796798
}
797799
memcpy(addr.sun_path, socket_path, socket_path_len);
798800

0 commit comments

Comments
 (0)