Skip to content

Commit

Permalink
[REM3-259] Authentication via remoting fail for larger requests e.g. …
Browse files Browse the repository at this point in the history
…long password

[REM3-259] Additional fix for remoting 4.0.22
  • Loading branch information
rnetuka committed May 24, 2017
1 parent 43da4c8 commit 76d85d1
Show file tree
Hide file tree
Showing 7 changed files with 522 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/jboss/remoting3/RemotingOptions.java
Expand Up @@ -52,6 +52,8 @@ private RemotingOptions() {
*/
public static final int DEFAULT_RECEIVE_BUFFER_SIZE = 8192;

public static final int MAX_RECEIVE_BUFFER_SIZE = 15000;

/**
* The size of allocated buffer regions.
*/
Expand Down
Expand Up @@ -53,6 +53,7 @@
import org.jboss.remoting3.spi.ConnectionHandlerContext;
import org.jboss.remoting3.spi.ConnectionHandlerFactory;
import org.jboss.remoting3.spi.ConnectionProviderContext;
import org.xnio.BufferAllocator;
import org.xnio.Buffers;
import org.xnio.ChannelListener;
import org.xnio.OptionMap;
Expand Down Expand Up @@ -247,8 +248,21 @@ final class Capabilities implements ChannelListener<ConnectedMessageChannel> {
}

public void handleEvent(final ConnectedMessageChannel channel) {
final Pooled<ByteBuffer> pooledReceiveBuffer = connection.allocate();
Pooled<ByteBuffer> pooledReceiveBuffer = connection.allocate();
try {
if (channel instanceof RemotingMessageChannel) {
try {
int messageLength = ((RemotingMessageChannel) channel).readMessageLength();
if (messageLength > pooledReceiveBuffer.getResource().capacity() && messageLength < RemotingOptions.MAX_RECEIVE_BUFFER_SIZE) {
pooledReceiveBuffer = Buffers.allocatedBufferPool(BufferAllocator.BYTE_BUFFER_ALLOCATOR, messageLength).allocate();
((RemotingMessageChannel) channel).adjustToMessageLength(messageLength);
}
} catch (IOException e) {
connection.handleException(e);
return;
}
}

final ByteBuffer receiveBuffer = pooledReceiveBuffer.getResource();
int res;
try {
Expand Down Expand Up @@ -480,7 +494,13 @@ public byte[] run() throws Exception {
return;
}
// Prepare the request message body
final Pooled<ByteBuffer> pooledSendBuffer = connection.allocate();
Pooled<ByteBuffer> pooledSendBuffer = connection.allocate();

if (response != null && response.length > pooledSendBuffer.getResource().capacity()) {
pooledSendBuffer = Buffers.allocatedBufferPool(BufferAllocator.BYTE_BUFFER_ALLOCATOR, response.length + 100).allocate();
connection.adjustToMessageLength(response.length + 100);
}

boolean ok = false;
try {
final ByteBuffer sendBuffer = pooledSendBuffer.getResource();
Expand Down
Expand Up @@ -90,6 +90,8 @@ final class HttpUpgradeConnectionProvider extends RemoteConnectionProvider {
public static final String SEC_JBOSS_REMOTING_ACCEPT= "sec-jbossremoting-accept";
public static final String UPGRADE = "Upgrade";

private static final int BUFFER_SIZE = 8192;

private final ProviderInterface providerInterface = new ProviderInterface();

HttpUpgradeConnectionProvider(final OptionMap optionMap, final ConnectionProviderContext connectionProviderContext) throws IOException {
Expand Down Expand Up @@ -225,10 +227,10 @@ public void adapt(final ConnectedStreamChannel channel) {
// ignore
}

Pool<ByteBuffer> messageBufferPool = RemoteConnectionProvider.USE_POOLING ? GLOBAL_POOL : Buffers.allocatedBufferPool(BufferAllocator.BYTE_BUFFER_ALLOCATOR, 8192);
Pool<ByteBuffer> messageBufferPool = RemoteConnectionProvider.USE_POOLING ? GLOBAL_POOL : Buffers.allocatedBufferPool(BufferAllocator.BYTE_BUFFER_ALLOCATOR, BUFFER_SIZE);
if (RemoteConnectionProvider.LEAK_DEBUGGING) messageBufferPool = new DebuggingBufferPool(messageBufferPool);

final FramedMessageChannel messageChannel = new FramedMessageChannel(channel, ByteBuffer.allocate(8192 + 4), ByteBuffer.allocate(8192 + 4));
final RemotingMessageChannel messageChannel = new RemotingMessageChannel(channel, ByteBuffer.allocate(BUFFER_SIZE + 4), ByteBuffer.allocate(BUFFER_SIZE + 4));
final RemoteConnection connection = new RemoteConnection(messageBufferPool, channel, messageChannel, optionMap, HttpUpgradeConnectionProvider.this);
final ServerConnectionOpenListener openListener = new ServerConnectionOpenListener(connection, getConnectionProviderContext(), authenticationProvider, optionMap, accessControlContext);
messageChannel.getWriteSetter().set(connection.getWriteListener());
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/jboss/remoting3/remote/RemoteConnection.java
Expand Up @@ -119,6 +119,22 @@ void handleException(IOException e, boolean log) {
}
}

/**
* Adjusts inner buffers to required message length. For security reason, the buffer size cannot exceed value
* specified in {@link RemotingOptions#MAX_RECEIVE_BUFFER_SIZE}
*
* @param length
* message length the buffers
*
* @throws IllegalArgumentException
* if requested length exceeds maximal allowed buffer size
*/
void adjustToMessageLength(int length) {
if (channel instanceof RemotingMessageChannel) {
((RemotingMessageChannel) channel).adjustToMessageLength(length);
}
}

void send(final Pooled<ByteBuffer> pooled) {
writeListener.send(pooled, false);
}
Expand Down
Expand Up @@ -66,7 +66,6 @@
import org.xnio.channels.AcceptingChannel;
import org.xnio.channels.ConnectedSslStreamChannel;
import org.xnio.channels.ConnectedStreamChannel;
import org.xnio.channels.FramedMessageChannel;
import org.xnio.ssl.XnioSsl;

/**
Expand All @@ -88,7 +87,9 @@ class RemoteConnectionProvider extends AbstractHandleableCloseable<ConnectionPro
LEAK_DEBUGGING = leakDebugging;
}

static final Pool<ByteBuffer> GLOBAL_POOL = new ByteBufferSlicePool(BufferAllocator.DIRECT_BYTE_BUFFER_ALLOCATOR, 8192, 2048 * 1024);
private static final int BUFFER_SIZE = 8192;

static final Pool<ByteBuffer> GLOBAL_POOL = new ByteBufferSlicePool(BufferAllocator.DIRECT_BYTE_BUFFER_ALLOCATOR, BUFFER_SIZE, 2048 * 1024);

private final ProviderInterface providerInterface = new ProviderInterface();
private final Xnio xnio;
Expand Down Expand Up @@ -174,9 +175,9 @@ public void handleEvent(final ConnectedStreamChannel channel) {
} catch (IOException e) {
// ignore
}
Pool<ByteBuffer> messageBufferPool = USE_POOLING ? GLOBAL_POOL : Buffers.allocatedBufferPool(BufferAllocator.BYTE_BUFFER_ALLOCATOR, 8192);
Pool<ByteBuffer> messageBufferPool = USE_POOLING ? GLOBAL_POOL : Buffers.allocatedBufferPool(BufferAllocator.BYTE_BUFFER_ALLOCATOR, BUFFER_SIZE);
if (LEAK_DEBUGGING) messageBufferPool = new DebuggingBufferPool(messageBufferPool);
final FramedMessageChannel messageChannel = new FramedMessageChannel(channel, ByteBuffer.allocate(8192 + 4), ByteBuffer.allocate(8192 + 4));
final RemotingMessageChannel messageChannel = new RemotingMessageChannel(channel, ByteBuffer.allocate(BUFFER_SIZE + 4), ByteBuffer.allocate(BUFFER_SIZE + 4));
final RemoteConnection remoteConnection = new RemoteConnection(messageBufferPool, channel, messageChannel, connectOptions, RemoteConnectionProvider.this);
cancellableResult.addCancelHandler(new Cancellable() {
@Override
Expand Down Expand Up @@ -326,7 +327,7 @@ private final class AcceptListener implements ChannelListener<AcceptingChannel<?
this.serverOptionMap = serverOptionMap;
this.serverAuthenticationProvider = serverAuthenticationProvider;
this.accessControlContext = accessControlContext;
Pool<ByteBuffer> pool = USE_POOLING ? GLOBAL_POOL : Buffers.allocatedBufferPool(BufferAllocator.BYTE_BUFFER_ALLOCATOR, 8192);
Pool<ByteBuffer> pool = USE_POOLING ? GLOBAL_POOL : Buffers.allocatedBufferPool(BufferAllocator.BYTE_BUFFER_ALLOCATOR, BUFFER_SIZE);
messageBufferPool = LEAK_DEBUGGING ? new DebuggingBufferPool(pool) : pool;
}

Expand All @@ -347,7 +348,7 @@ public void handleEvent(final AcceptingChannel<? extends ConnectedStreamChannel>
// ignore
}

final FramedMessageChannel messageChannel = new FramedMessageChannel(accepted, ByteBuffer.allocate(8192 + 4), ByteBuffer.allocate(8192 + 4));
final RemotingMessageChannel messageChannel = new RemotingMessageChannel(accepted, ByteBuffer.allocate(BUFFER_SIZE + 4), ByteBuffer.allocate(BUFFER_SIZE + 4));
final RemoteConnection connection = new RemoteConnection(messageBufferPool, accepted, messageChannel, serverOptionMap, RemoteConnectionProvider.this);
final ServerConnectionOpenListener openListener = new ServerConnectionOpenListener(connection, connectionProviderContext, serverAuthenticationProvider, serverOptionMap, accessControlContext);
messageChannel.getWriteSetter().set(connection.getWriteListener());
Expand Down

0 comments on commit 76d85d1

Please sign in to comment.