Skip to content

Server memory overflow due to unbounded caching of all incoming frames until SETUP/RESUME are received. #884

Closed

Description

@mostroverkhov

ClientServerInputMultiplexer does not check first frame type, stores (1), (2) all inbound frames indefinitely until SETUP/RESUME is received. Rogue client may trivially trigger server out-of-memory error by just sending any frames except setup/resume once connection is established. Reproducible with code below

 @Test
    void setupOom() {

        MonoProcessor<Void> rSocketEstablished = MonoProcessor.create();

        TestServerTransport testTransport = new TestServerTransport();
        Closeable server = RSocketServer.create()
                .acceptor((setup, sendingSocket) -> {
                    rSocketEstablished.onComplete();
                    return Mono.just(new RSocket() {
                    });
                })
                .bind(testTransport)
                .block();

        byte[] bytes = bytes(16_000_000);
        TestDuplexConnection testDuplexConnection = testTransport.connect();
        for (int i = 0; i < 200; i++) {
            testDuplexConnection.addToReceivedBuffer(RequestResponseFrameCodec.encode(ByteBufAllocator.DEFAULT, 1, false,
                    Unpooled.EMPTY_BUFFER, ByteBufAllocator.DEFAULT.buffer(bytes.length).writeBytes(bytes)));
        }
        rSocketEstablished.mergeWith(testDuplexConnection.onClose())
                .as(StepVerifier::create)
                .expectComplete()
                .verify(Duration.ofSeconds(30));
    }

    private static final Random random = new Random();

    static byte[] bytes(int size) {
        byte[] data = new byte[size];
        random.nextBytes(data);
        return data;
    }

That fails with

Caused by: java.lang.OutOfMemoryError: Direct buffer memory
	at java.base/java.nio.Bits.reserveMemory(Bits.java:175)
	at java.base/java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:118)
	at java.base/java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:317)
	at io.netty.buffer.PoolArena$DirectArena.allocateDirect(PoolArena.java:755)
	at io.netty.buffer.PoolArena$DirectArena.newChunk(PoolArena.java:731)
	at io.netty.buffer.PoolArena.allocateNormal(PoolArena.java:247)
	at io.netty.buffer.PoolArena.allocate(PoolArena.java:227)
	at io.netty.buffer.PoolArena.allocate(PoolArena.java:147)
	at io.netty.buffer.PooledByteBufAllocator.newDirectBuffer(PooledByteBufAllocator.java:356)
	at io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:187)
	at io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:178)
	at io.netty.buffer.AbstractByteBufAllocator.buffer(AbstractByteBufAllocator.java:115)
	at io.rsocket.SetupOomTest.setupOom(SetupOomTest.java:56)

Activity

  1. added this to the 1.x Backlog milestone on Jul 4, 2020
  2. OlegDokuka commented on Jul 27, 2020

    @OlegDokuka
    Member

    Will be adding a boolean flag to check that the first received frame is SETUP/RESUME. Otherwise, REJECT terminating the connection

  3. modified the milestones: 1.x Backlog, 1.0.2 on Jul 27, 2020
  4. self-assigned this
    on Jul 27, 2020
  5. added 3 commits that reference this issue on Aug 3, 2020
    183405c
    a9dcc51
    d1e0507
  6. linked a pull request that will close this issueReject any frames before SETUP/LEASE #904on Aug 3, 2020
  7. removed this from the 1.0.2 milestone on Aug 3, 2020
  8. added
    superseded
    on Aug 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugsuperseded

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions