DOS while dealing with erc20 when value(i.e amount*decimals) is high but less than type(uint112).max
A calculation in the Locke contract overflows when a user's staked amount is large but below the maximum allowed value, causing the exit and withdraw functions to revert. This permanently locks the user's funds.
Submitted by hack3r-0m
Impact
https://github.com/code-423n4/2021-11-streaming/blob/main/Streaming/src/Locke.sol#L229
reverts due to overflow for higher values (but strictly less than type(uint112).max) and hence when user calls exit or withdraw function it will revert and that user will not able to withdraw funds permanentaly.
Proof of Concept
Attaching diff to modify tests to reproduce behaviour:
diff --git a/Streaming/src/test/Locke.t.sol b/Streaming/src/test/Locke.t.sol
index 2be8db0..aba19ce 100644
--- a/Streaming/src/test/Locke.t.sol
+++ b/Streaming/src/test/Locke.t.sol
@@ -166,14 +166,14 @@ contract StreamTest is LockeTest {
);
testTokenA.approve(address(stream), type(uint256).max);
- stream.fundStream((10**14)*10**18);
+ stream.fundStream(1000);
- alice.doStake(stream, address(testTokenB), (10**13)*10**18);
+ alice.doStake(stream, address(testTokenB), 100);
hevm.warp(startTime + minStreamDuration / 2); // move to half done
- bob.doStake(stream, address(testTokenB), (10**13)*10**18);
+ bob.doStake(stream, address(testTokenB), 100);
hevm.warp(startTime + minStreamDuration / 2 + minStreamDuration / 10);
@@ -182,10 +182,10 @@ contract StreamTest is LockeTest {
hevm.warp(startTime + minStreamDuration + 1); // warp to end of stream
- // alice.doClaimReward(stream);
- // assertEq(testTokenA.balanceOf(address(alice)), 533*(10**15));
- // bob.doClaimReward(stream);
- // assertEq(testTokenA.balanceOf(address(bob)), 466*(10**15));
+ alice.doClaimReward(stream);
+ assertEq(testTokenA.balanceOf(address(alice)), 533);
+ bob.doClaimReward(stream);
+ assertEq(testTokenA.balanceOf(address(bob)), 466);
}
function test_stake() public {
diff --git a/Streaming/src/test/utils/LockeTest.sol b/Streaming/src/test/utils/LockeTest.sol
index eb38060..a479875 100644
--- a/Streaming/src/test/utils/LockeTest.sol
+++ b/Streaming/src/test/utils/LockeTest.sol
@@ -90,11 +90,11 @@ abstract contract LockeTest is TestHelpers {
testTokenA = ERC20(address(new TestToken("Test Token A", "TTA", 18)));
testTokenB = ERC20(address(new TestToken("Test Token B", "TTB", 18)));
testTokenC = ERC20(address(new TestToken("Test Token C", "TTC", 18)));
- write_balanceOf_ts(address(testTokenA), address(this), (10**14)*10**18);
- write_balanceOf_ts(address(testTokenB), address(this), (10**14)*10**18);
- write_balanceOf_ts(address(testTokenC), address(this), (10**14)*10**18);
- assertEq(testTokenA.balanceOf(address(this)), (10**14)*10**18);
- assertEq(testTokenB.balanceOf(address(this)), (10**14)*10**18);
+ write_balanceOf_ts(address(testTokenA), address(this), 100*10**18);
+ write_balanceOf_ts(address(testTokenB), address(this), 100*10**18);
+ write_balanceOf_ts(address(testTokenC), address(this), 100*10**18);
+ assertEq(testTokenA.balanceOf(address(this)), 100*10**18);
+ assertEq(testTokenB.balanceOf(address(this)), 100*10**18);
defaultStreamFactory = new StreamFactory(address(this), address(this));
Tools Used
Manual Review
Recommended Mitigation Steps
Consider doing arithmetic operations in two steps or upcasting to u256 and then downcasting. Alternatively, find a threshold where it breaks and add require condition to not allow total stake per user greater than threshhold.
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 an arithmetic operation in the reward calculation overflows because the result exceeds the maximum value of the data type used, even though the input is within the allowed range.
Impact
A user with a large stake could permanently lose access to their funds because exit and withdraw always revert.
What the attacker needed
A large stake amount; the stake must be below type(uint112).max
The fix
The arithmetic should be performed in two steps or by upcasting to a larger type and then downcasting, or a threshold should be enforced to prevent stakes that cause overflow.
How it was found
- Fund the stream with a small amount of tokens.
- Alice stakes a small amount of tokens.
- Advance time to halfway through the stream.
- Bob stakes a small amount of tokens.
- Advance time to the end of the stream.
- Alice and Bob claim rewards, which triggers the overflow and reverts.
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.
Smart contracts often store numbers in fixed-size types, and if a calculation produces a number too big for that type, it overflows and the transaction fails. Here, a user's large stake caused such an overflow, so they could never withdraw their funds.
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.