Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Added verifying of encoded RLP length to be not greater than available data size #1073

Merged
merged 3 commits into from
May 11, 2018

Conversation

zilm13
Copy link
Collaborator

@zilm13 zilm13 commented May 11, 2018

Verifying of decoded RLP object length to be not greater than data size

While primary decode method, RLP.decode2(byte[]) is protected to verify whether decoded length lies in the boundaries of remaining data length, we have several other public methods which doesn't include such inspection. One of them, RLP.decode(byte[], int) is used, for example, in Value constructor and could be attacked using incorrect RLP data. Length is int, so maximum possible size is 2+B or more than 2Gb occupied by each RLPItem from attacker's RLP. So this kind of attack could occupy all available memory.

What's done:

  • test added with data from such kind of attack
  • added verification to all RLP class methods which could occupy more than 1 byte of length of length

@zilm13 zilm13 requested a review from mkalinin May 11, 2018 09:27
@coveralls
Copy link

coveralls commented May 11, 2018

Coverage Status

Coverage increased (+0.004%) to 55.984% when pulling ee0a9f1 on fix/rlp-decode-attack into 3ac370b on develop.

Copy link
Contributor

@mkalinin mkalinin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private static void verifyLength(int suppliedLength, int availableLength) {
if (suppliedLength > availableLength) {
throw new RuntimeException(String.format("Length parsed from RLP (%s bytes) is greater " +
"than remaining size of data (%s bytes)", suppliedLength, availableLength));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be better to read something like than [probable | possible] size of data

@mkalinin
Copy link
Contributor

mkalinin commented May 11, 2018

Addressing my comment above. It's better to add the check to https://github.com/ethereum/ethereumj/pull/1073/files#diff-424bd3538481cd5c9df18504819120b6R801 method where decoding is happened for two reasons: i) it's more convenient for users of LList to get an exception when incorrect data are first touched rather than at some point in the future when that data will be accessed ii) it is possible that user will never access incorrect piece of data and thus never get that error while whole RLP would still be invalid

@zilm13
Copy link
Collaborator Author

zilm13 commented May 11, 2018

I'd also add that check to this method: https://github.com/ethereum/ethereumj/pull/1073/files#diff-424bd3538481cd5c9df18504819120b6R776

this one is encoding
plus length is limited to one byte (less than 256)

@mkalinin mkalinin merged commit 4b49ba7 into develop May 11, 2018
ligi added a commit to komputing/KEthereum that referenced this pull request May 20, 2018
Inpspired by ethereum/ethereumj#1073
Cannot really use the exact test-case there as it is lgpl and we are MIT
But the input here is basically doing the same.
@zilm13 zilm13 deleted the fix/rlp-decode-attack branch June 4, 2018 15:54
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants