BytesUtils : compare will not revert when the offset and len exceeds the bytes lengths
The compare function in BytesUtils.sol does not validate that the provided offset and length parameters are within the bounds of the input byte arrays. An attacker could supply malformed DNS names with out-of-range offsets and lengths, causing compare to return incorrect results without reverting.
Submitted by zzzitron
Compare will return false answer without reverting when the inputs are not valid.
Proof of Concept
The compare function is used for compareNames. The names are supposed to be DNS wire format. If the strings are malformed, it is possible to give out-of-range offset, len, otheroffset, and otherlen. When it happens, the compare will return some false values, without reverting, since the validity of offset and len are not checked.
// dnssec-oracle/BytesUtils.sol::compare
// The length of self and other are not enforced
44 function compare(bytes memory self, uint offset, uint len, bytes memory other, uint otheroffset, uint otherlen) internal pure returns (int) {
45 uint shortest = len;
46 if (otherlen < len)
47 shortest = otherlen;
48
49 uint selfptr;
50 uint otherptr;
Recommended Mitigation Steps
Check whether the offset, len are within the length of self, as well as for the other.
makoto (ENS) acknowledged and commented:
It’s lacking enough test to prove the bug.
Arachnid (ENS) confirmed and commented:
compareNamesdoes not sanity check the lengths passed in, so this is a legitimate bug.
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 compare function lacks bounds checks on the offset and length parameters relative to the actual byte array lengths.
Impact
The compare function may return false results without reverting, potentially causing incorrect behaviour in name comparison logic.
What the attacker needed
Malformed DNS name input
The fix
The compare function should check that offset and len are within the length of self, and similarly for other.
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 function that compares two pieces of data does not check if the starting positions and lengths given for the comparison actually fit inside the data. If someone provides bad numbers, the function gives a wrong answer instead of stopping with an error. This could lead to incorrect decisions in the contract.
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.