Skip to content

Commit 9ee129a

Browse files
authored
NioServer: retain links by address string to minimize resource leak (#3525)
Every time a client connects, the NioServer will retain the link against the InetSocketAddress object. If the same agent/client reconnects, it will grow older links over time and in case of denial of service attack or a client/script/monitoring-service reconnecting aggressively against port 8250 will cause the `_links` weak hashmap to grow over time and very quickly. The fix will ensure that only one Link gets weakly retained for an incoming client based on its address string. Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
1 parent 294813e commit 9ee129a

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

utils/src/main/java/com/cloud/utils/nio/Link.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ private static HandshakeHolder doHandshakeUnwrap(final SocketChannel socketChann
489489
try {
490490
sslEngine.closeInbound();
491491
} catch (SSLException e) {
492-
s_logger.warn("This SSL engine was forced to close inbound due to end of stream.");
492+
s_logger.warn("This SSL engine was forced to close inbound due to end of stream.", e);
493493
}
494494
sslEngine.closeOutbound();
495495
// After closeOutbound the engine will be set to WRAP state,

utils/src/main/java/com/cloud/utils/nio/NioServer.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.nio.channels.SelectionKey;
2626
import java.nio.channels.ServerSocketChannel;
2727
import java.nio.channels.spi.SelectorProvider;
28+
import java.util.Map;
2829
import java.util.WeakHashMap;
2930

3031
import org.apache.cloudstack.framework.ca.CAService;
@@ -36,13 +37,13 @@ public class NioServer extends NioConnection {
3637
protected InetSocketAddress _localAddr;
3738
private ServerSocketChannel _serverSocket;
3839

39-
protected WeakHashMap<InetSocketAddress, Link> _links;
40+
protected Map<String, Link> _links;
4041

4142
public NioServer(final String name, final int port, final int workers, final HandlerFactory factory, final CAService caService) {
4243
super(name, port, workers, factory);
4344
setCAService(caService);
4445
_localAddr = null;
45-
_links = new WeakHashMap<InetSocketAddress, Link>(1024);
46+
_links = new WeakHashMap<String, Link>(10240);
4647
}
4748

4849
public int getPort() {
@@ -61,7 +62,7 @@ protected void init() throws IOException {
6162

6263
_serverSocket.register(_selector, SelectionKey.OP_ACCEPT, null);
6364

64-
s_logger.info("NioConnection started and listening on " + _serverSocket.socket().getLocalSocketAddress());
65+
s_logger.info("NioServer started and listening on " + _serverSocket.socket().getLocalSocketAddress());
6566
}
6667

6768
@Override
@@ -75,12 +76,12 @@ public void cleanUp() throws IOException {
7576

7677
@Override
7778
protected void registerLink(final InetSocketAddress addr, final Link link) {
78-
_links.put(addr, link);
79+
_links.put(addr.getAddress().toString(), link);
7980
}
8081

8182
@Override
8283
protected void unregisterLink(final InetSocketAddress saddr) {
83-
_links.remove(saddr);
84+
_links.remove(saddr.getAddress().toString());
8485
}
8586

8687
/**
@@ -93,7 +94,7 @@ protected void unregisterLink(final InetSocketAddress saddr) {
9394
* @return null if not sent. attach object in link if sent.
9495
*/
9596
public Object send(final InetSocketAddress saddr, final byte[] data) throws ClosedChannelException {
96-
final Link link = _links.get(saddr);
97+
final Link link = _links.get(saddr.getAddress().toString());
9798
if (link == null) {
9899
return null;
99100
}

0 commit comments

Comments
 (0)