Skip to content
Business logic Audit finding Intermediate

BytesUtil.compare returns wrong result on some strings longer than 32 characters

The compare function in BytesUtils masks out irrelevant characters only for strings shorter than 32 characters, so when comparing strings longer than 32 characters with a specified length shorter than the string, characters beyond that length are still compared. This causes incorrect comparison results and also makes RRUtils.compareNames fail to correctly compare DNS names in certain cases.

panprog Follow

Originally disclosed on Code4rena

Submitted by panprog, also found by alan724 and GimelSec

BytesUtils.sol#L66-L70

Due to incorrect condition in ByteUtil.compare function, irrelevant characters are masked out only for strings shorter than 32 characters. However, they must be masked out for strings of all lengths in the last pass of the loop (when remainder of the string is 32 characters or less). This leads to incorrect comparision of strings longer than 32 characters where len or otherlen is smaller than string length (characters beyond provided length are still accounted for in the comparision in this case while they should be ignored).

This wrong compare behaviour also makes RRUtils.compareNames fail to correctly compare DNS names in certain cases.

While the BytesUtil.compare and RRUtils.compareNames methods are currently not used in the functions in the scope (but are used in mainnet’s DNSSECImpl.checkNsecName, which is out of scope here), they’re public library functions relied upon and can be used by the other users or the ENS project in the future. And since the functions in scope provide incorrect result, that’s a wrong (unexpected) behaviour of the smart contract. Moreover, since the problem can be seen only with the large strings (more than 32 characters), this might go unnoticed with any code that uses compare or compareNames method and can potentially lead to high security risk of any integration project or ENS itself.

Example strings to compare which give incorrect result: 01234567890123450123456789012345ab 01234567890123450123456789012345aa

Each string is 34 characters long, first 33 characters are the same, the last one is different. If we compare first 33 characters of both strings, the result should be equal (as they only differ in the 34th character), but compare will return >, because it fails to ignore the last character of both strings and simply compares strings themselves.

If we compare the first 33 characters from the first string vs all 34 characters of the second string, the result of compare will be >, while the correct result is <, because compare fails to ignore the last character of the first string.

Example dns names to compare which give incorrect result: 01234567890123456789012345678901a0.0123456789012345678901234567890123456789012345678.eth 01234567890123456789012345678901a.0123456789012345678901234567890123456789012345678.eth

The first dns name should come after the second, but dnsCompare returns -1 (the first name to come before), because the length of the 2nd domain (49 characters) is ASCII character 1 and is not correctly masked off during strings comparision.

Proof of Concept

git diff

https://gist.github.com/panprog/32adefdc853ccd0fd0f1aad85c526bea

then:

yarn test test/dnssec-oracle/TestSolidityTests.js

Recommended Mitigation Steps

In addition to the incorrect condition, the mask calculation formula: 32 - shortest + idx will also overflow since shortest can be more than 32, so addition should be performed before subtractions.

            if (shortest - idx >= 32) {
                mask = type(uint256).max;
            } else {
                mask = ~(2 ** (8 * (idx + 32 - shortest)) - 1);
            }

Arachnid (ENS) confirmed


The original report as published on Code4rena, by panprog. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the condition for masking out irrelevant characters in the final loop iteration is wrong, and the mask calculation can overflow when the shortest length exceeds 32.

Impact

The functions are not currently used in the in-scope contracts, but they are public library functions that could be relied upon by other users or ENS in the future, and incorrect results could lead to security risks in integrations.

What the attacker needed

None

The fix

The condition should be corrected so masking applies in the last loop pass for all lengths, and the mask calculation should perform addition before subtraction to avoid overflow.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A smart contract library function that compares two strings is supposed to ignore characters beyond a given length. Because of a mistake in a condition, it only ignores them for short strings, so for longer strings it compares extra characters and returns the wrong answer. This could make other code that relies on the comparison behave incorrectly.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by panprog

View all

66 more disclosed reports, best first.

More from ENS

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.