ConvexSpell/CurveSpell.openPositionFarm will revert in some cases
The CurveSpell and ConvexSpell contracts approve only the borrowed amount to the Curve pool, but then supply the contract's full token balance as the liquidity amount. If anyone sends a tiny amount of the borrow token to the contract beforehand, the supplied amount exceeds the approval and the liquidity call reverts, so no one can open a position.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-07-blueberry-judging/issues/60
Found by
nobody2018
Before calling ICurvePool#add_liquidity to deposit tokens, each token needs to be approved to the pool. If the approved amount is smaller than the parameters passed into ICurvePool#add_liquidity, it will cause erc20.transferFrom revert inside the function. In this way, openPositionFarm will also revert.
In previous contest, [#47](https://github.com/sherlock-audit/2023-04-blueberry-judging/issues/47) was not fixed correctly. This issue is still exist.
Vulnerability Detail
The fix for this issue from this contest is as following:
File: blueberry-core\contracts\spell\CurveSpell.sol
098: // 2. Borrow specific amounts
099: uint256 borrowBalance = _doBorrow(
100: param.borrowToken,
101: param.borrowAmount
102: );
103:
104: // 3. Add liquidity on curve
105: address borrowToken = param.borrowToken;
106: _ensureApprove(param.borrowToken, pool, borrowBalance);
107: if (tokens.length == 2) {
108: uint256[2] memory suppliedAmts;
109: for (uint256 i = 0; i < 2; i++) {
//this 'if' check is the fix from the previous contest
110:-> if (tokens[i] == borrowToken) {
111: suppliedAmts[i] = IERC20Upgradeable(tokens[i]).balanceOf(
112: address(this)
113: );
114: break;
115: }
116: }
117: ICurvePool(pool).add_liquidity(suppliedAmts, minLPMint);
118: } else if (tokens.length == 3) {
The key to this issue is that borrowBalance may be smaller than IERC20Upgradeable(borrowToken).balanceOf(address(this)). For simplicity, assume that CurveSpell supports an lptoken which contains two tokens : A and B.
Bob transferred 1wei of A and B to the CurveSpell contract. Alice opens a position by calling BlueBerryBank#execute, and the flow is as follows:
- enter
CurveSpell#openPositionFarm. - call
_doLendto deposit isolated collaterals. - call
_doBorrowto borrow 100e18 A token. borrowBalance = 100e18. A.approve(pool, 100e18).suppliedAmts[0] = A.balance(address(this)) = 100e18+1wei,suppliedAmts[1] = 0.- call
ICurvePool(pool).add_liquidity(suppliedAmts, minLPMint), then revert because the approved amount is not enough.
Therefore, no one can successfully open a position.
Of course, bob can also transfer 1wei of borrowToken to contract by front-running openPositionFarm for a specific user or all users.
In ConvexSpell, the issue lies in [the same code](https://github.com/sherlock-audit/2023-07-blueberry/blob/main/blueberry-core/contracts/spell/ConvexSpell.sol#L110-L144) as in previous games.
Impact
ConvexSpell/CurveSpell.openPositionFarm will revert due to this issue.
Code Snippet
Tool used
Manual Review
Recommendation
The following fix is for CurveSpell, but please don't forget ConvexSpell.
Two ways for fix it:
--- a/blueberry-core/contracts/spell/CurveSpell.sol
+++ b/blueberry-core/contracts/spell/CurveSpell.sol
@@ -108,9 +108,7 @@ contract CurveSpell is BasicSpell {
uint256[2] memory suppliedAmts;
for (uint256 i = 0; i < 2; i++) {
if (tokens[i] == borrowToken) {
- suppliedAmts[i] = IERC20Upgradeable(tokens[i]).balanceOf(
- address(this)
- );
+ suppliedAmts[i] = borrowBalance;
break;
}
}
@@ -119,9 +117,7 @@ contract CurveSpell is BasicSpell {
uint256[3] memory suppliedAmts;
for (uint256 i = 0; i < 3; i++) {
if (tokens[i] == borrowToken) {
- suppliedAmts[i] = IERC20Upgradeable(tokens[i]).balanceOf(
- address(this)
- );
+ suppliedAmts[i] = borrowBalance;
break;
}
}
@@ -130,9 +126,7 @@ contract CurveSpell is BasicSpell {
uint256[4] memory suppliedAmts;
for (uint256 i = 0; i < 4; i++) {
if (tokens[i] == borrowToken) {
- suppliedAmts[i] = IERC20Upgradeable(tokens[i]).balanceOf(
- address(this)
- );
+ suppliedAmts[i] = borrowBalance;
break;
}
}
--- a/blueberry-core/contracts/spell/CurveSpell.sol
+++ b/blueberry-core/contracts/spell/CurveSpell.sol
@@ -103,7 +103,8 @@ contract CurveSpell is BasicSpell {
// 3. Add liquidity on curve
address borrowToken = param.borrowToken;
- _ensureApprove(param.borrowToken, pool, borrowBalance);
+ require(borrowBalance <= IERC20Upgradeable(borrowToken).balanceOf(address(this)), "impossible");
+ _ensureApprove(param.borrowToken, pool, IERC20Upgradeable(borrowToken).balanceOf(address(this)));
if (tokens.length == 2) {
uint256[2] memory suppliedAmts;
for (uint256 i = 0; i < 2; i++) {
Discussion
sherlock-admin2
2 comment(s) were left on this issue during the judging contest.
0xyPhilic commented:
invalid because the borrowAmount can't change mid function execution
darkart commented:
If already approven its up to Developrs to chose if they will implement it
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 code sets the supplied amount to the contract's full token balance while approving only the borrowed amount, so any extra balance makes the transfer exceed the allowance.
Impact
Opening a position through ConvexSpell or CurveSpell will revert, so users cannot open positions; the attacker only needs to send a tiny amount of tokens.
What the attacker needed
Attacker can send tokens to the contract; victim opens a position
The fix
The supplied amount should be set to the borrowed balance rather than the contract's full balance, or the approval should cover the full balance after checking it is at least the borrowed amount; the text notes the same fix is needed in ConvexSpell.
How it was found
- Bob transfers a tiny amount of the pool tokens to the CurveSpell contract.
- Alice calls execute to open a position, which enters openPositionFarm.
- The contract lends collateral and borrows the borrow token.
- It approves only the borrowed amount to the pool.
- It sets the supplied amount to the contract's full balance, which is now larger than the approval.
- The add_liquidity call reverts because the approved amount is not enough.
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 contract borrows some tokens and then puts them into a liquidity pool. It tells the pool it may take exactly the borrowed amount, but it actually asks the pool to take everything the contract holds. If someone sends even a tiny extra amount of that token to the contract first, the pool tries to take more than allowed and the whole action fails.
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.