Sqrt function silently reverts the entire control flow when a packed float of 0 value is passed
The square root function in a Solidity fixed-point library uses the Yul stop() instruction when given a zero input, which silently halts execution instead of returning zero. This could cause unexpected reverts in protocols that rely on the library, potentially leading to financial consequences.
YouCrossTheLineAlfie Follow with 0x23r0, 0xbrett8571 and 17 others
Originally disclosed on Code4rena
Submitted by YouCrossTheLineAlfie, also found by 0x23r0, 0xbrett8571, 0xpetern, Akxai, bareli, CodexBugmenot, Dest1ny_rs, djshan_eden, felconsec, Franfran, Illoy-Scizceneghposter, JuggerNaut63, maxzuvex, maze, micklondonjr, rmrf480, Tezei, TheCarrot, and who_rp
Finding description
The Float128::sqrt function is used to find the square root of the given packed float.
Mathematically, intuitively and as per most other libraries in different programming languages, it is ideal to say that square root of 0 should return 0.
However, the implementation of sqrt function stops the executing when the give packed float is 0 via stop().
function sqrt(packedFloat a) internal pure returns (packedFloat r) {
uint s;
int aExp;
uint x;
uint aMan;
uint256 roundedDownResult;
bool aL;
assembly {
if and(a, MANTISSA_SIGN_MASK) {
let ptr := mload(0x40) // Get free memory pointer
mstore(ptr, 0x08c379a000000000000000000000000000000000000000000000000000000000) // Selector for method Error(string)
mstore(add(ptr, 0x04), 0x20) // String offset
mstore(add(ptr, 0x24), 32) // Revert reason length
mstore(add(ptr, 0x44), "float128: squareroot of negative")
revert(ptr, 0x64) // Revert data length is 4 bytes for selector and 3 slots of 0x20 bytes
}
if iszero(a) {
stop() <<@ -- // Stops the execution flow entirely
}
This can lead to serious issues where the code execution just stops mid-way silently reverting the control flow.
Impact
Serious financial consequences can happen in protocols using this library as the entire code execution reverts due to the stop().
Recommended mitigation steps
It is recommended to return 0 instead of using the stop():
function sqrt(packedFloat a) internal pure returns (packedFloat r) {
uint s;
int aExp;
uint x;
uint aMan;
uint256 roundedDownResult;
bool aL;
assembly {
if and(a, MANTISSA_SIGN_MASK) {
let ptr := mload(0x40) // Get free memory pointer
mstore(ptr, 0x08c379a000000000000000000000000000000000000000000000000000000000) // Selector for method Error(string)
mstore(add(ptr, 0x04), 0x20) // String offset
mstore(add(ptr, 0x24), 32) // Revert reason length
mstore(add(ptr, 0x44), "float128: squareroot of negative")
revert(ptr, 0x64) // Revert data length is 4 bytes for selector and 3 slots of 0x20 bytes
}
if iszero(a) {
- stop()
+ r := 0
+ leave
}
Proof of Concept
Add a file named test.t.sol inside the /test folder:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "forge-std/Test.sol";
import "forge-std/console2.sol";
import "src/Float128.sol";
contract TestingContract is Test {
using Float128 for packedFloat;
using Float128 for int256;
function testSqrtSilentRevert() public {
console2.log("Test started");
// First test with a non-zero value to show normal behavior
packedFloat nonZero = Float128.toPackedFloat(1, 0);
packedFloat nonZeroResult = Float128.sqrt(nonZero);
console2.log("Non-zero sqrt completed");
packedFloat zero = Float128.toPackedFloat(0, 0);
// Should silently revert as per the solidity's yul docs.
packedFloat zeroResult = Float128.sqrt(zero);
console.log("Zero sqrt completed" ); // Never printed
assertEq(false, true, "This will never terminate as the control never reaches here due to silent termination"); // Test would pass successfully which it shouldn't have had.
}
}
oscarserna (Forte) confirmed
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
The sqrt function uses the Yul stop() instruction for zero input, which halts execution entirely rather than returning a value.
Impact
Protocols using this library could experience unexpected reverts, potentially leading to financial consequences.
What the attacker needed
Contract uses Float128 library; sqrt called with zero input
The fix
The function should return zero instead of using stop().
How it was found
- Call the sqrt function with a packed float representing zero.
- Observe that the function does not return and execution stops silently.
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 library for handling decimal numbers in smart contracts has a square root function. When you ask it for the square root of zero, instead of giving you zero back, it just stops the whole program without any error message. This can break other code that expects a normal answer.
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.