safeDecimals can revert causing DoS
The safeDecimals function in SafeMetadata.sol uses a length check of >= 32 before calling abi.decode, which reverts if the returned data is longer than 32 bytes. An attacker could cause a denial of service by using a token whose decimals() call returns more than 32 bytes of data, making any function that calls safeDecimals revert.
Originally disclosed on Code4rena
Submitted by sirhashalot
The safeDecimals() function, found in the SafeMetadata.sol contract and called in 3 different Timeswap Convenience contracts, can cause a revert. This is because the safeDecimals function attempts to use abi.decode to return a uint8 when data.length >= 32. However, a data.length value greater than 32 will cause abi.decode to revert.
A similar issue was found in a previoud code4rena contest: https://github.com/code-423n4/2021-05-nftx-findings/issues/46
Proof of Concept
The root cause is line 28 of the safeDecimals() function in SafeMetadata.sol
The following link shows the safeDecimals() function in the BoringCrypto library, which might be where this code was borrowed from, uses the strict equality check data.length == 32 https://github.com/boringcrypto/BoringSolidity/blob/ccb743d4c3363ca37491b87c6c9b24b1f5fa25dc/contracts/libraries/BoringERC20.sol#L54
safeDecimals() is used in multiple functions such as
Recommended Mitigation Steps
Modify the safeDecimals() function to change >= 32 to == 32 like this if (success && data.length == 32) return abi.decode(data, (uint8));
Mathepreneur (Timeswap) confirmed and resolved:
https://github.com/Timeswap-Labs/Timeswap-V1-Convenience/pull/61
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 length check uses >= 32 instead of == 32, so abi.decode is called with data longer than 32 bytes, which reverts.
Impact
An attacker could cause a denial of service by making any function that calls safeDecimals revert, potentially blocking interactions with the affected contracts.
What the attacker needed
A token whose decimals() returns more than 32 bytes
The fix
The safeDecimals function should be modified to check for exactly 32 bytes (== 32) instead of >= 32 before calling abi.decode.
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 tries to read a number (decimals) from another contract. It checks that the returned data is at least 32 bytes long, but if the data is longer than 32 bytes, the decoding step fails and the whole transaction reverts. This means an attacker can make the function unusable by providing a token that returns extra data.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.