Skip to content

TMessageReader::Read allocates an unbounded wire-supplied length and underflows len - sizeof(TMessage) #39

Open

Description

@resetius

Problem

TMessageReader::Read reads type and len from the socket and immediately calls NewHoldedMessage<TMessage>(type, len) and reads len - sizeof(TMessage) bytes (server.cpp#L25-L37):

  • len comes from the wire and has no upper bound: a peer or client can make the node allocate up to 4 GiB per frame (new char[len] in messages.h#L211-L218), and the same for each of the Nentries payload frames read recursively (server.cpp#L38-L46).
  • len >= sizeof(TMessage) is only an assert inside NewMessage. In a release build a smaller len makes len - sizeof(TMessage) wrap around (unsigned arithmetic) and the reader tries to read about 4 GiB into a tiny buffer: a heap overflow.
  • type is not checked against the known message types.

The same reader is used for client connections (server.cpp#L125), so any client that can connect can trigger this.

Scenario

A client connects and sends 8 bytes: type = 1, len = 0. In a build without asserts the node allocates 0 bytes and then reads 0 - sizeof(TMessage) (about 4 GiB) bytes into it. With len = 0xFFFFFFFF it allocates 4 GiB and waits for the data. Either way one unauthenticated connection takes the node down or exhausts its memory.

Expected

Validate type and len (known type, len >= sizeof(TMessage), and len bounded by a sane maximum) before allocating, and fail the connection with an error instead of allocating.

Fix in PR #31

PR #31 (closed, not merged) has the reader fix in b36e5b1 (the core of that commit; the batch limits in the rest of it can follow separately). Tests that cover it: the reader cases of test/test_network_frames.cpp. Since the PR is closed, the fix would come as a separate small PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions