No check for `initialEth` in `GoatV1Pair.takeOverPool()`
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-03-goat-trading-judging/issues/43
Found by
whitehair0330
Summary
GoatV1Pair.takeOverPool() only checks the amount of token for initialization, not initialETH.
Vulnerability Detail
function takeOverPool(GoatTypes.InitParams memory initParams) external {
if (_vestingUntil != _MAX_UINT32) {
revert GoatErrors.ActionNotAllowed();
}
GoatTypes.InitialLPInfo memory initialLpInfo = _initialLPInfo;
GoatTypes.LocalVariables_TakeOverPool memory localVars;
address to = msg.sender;
localVars.virtualEthOld = _virtualEth;
localVars.bootstrapEthOld = _bootstrapEth;
localVars.initialTokenMatchOld = _initialTokenMatch;
(localVars.tokenAmountForPresaleOld, localVars.tokenAmountForAmmOld) = _tokenAmountsForLiquidityBootstrap(
localVars.virtualEthOld,
localVars.bootstrapEthOld,
initialLpInfo.initialWethAdded,
localVars.initialTokenMatchOld
);
// new token amount for bootstrap if no swaps would have occured
(localVars.tokenAmountForPresaleNew, localVars.tokenAmountForAmmNew) = _tokenAmountsForLiquidityBootstrap(
initParams.virtualEth, initParams.bootstrapEth, initParams.initialEth, initParams.initialTokenMatch
);
// team needs to add min 10% more tokens than the initial lp to take over
localVars.minTokenNeeded =
((localVars.tokenAmountForPresaleOld + localVars.tokenAmountForAmmOld) * 11000) / 10000;
481 if ((localVars.tokenAmountForAmmNew + localVars.tokenAmountForPresaleNew) < localVars.minTokenNeeded) {
revert GoatErrors.InsufficientTakeoverTokenAmount();
}
localVars.reserveEth = _reserveEth;
// Actual token amounts needed if the reserves have updated after initial lp mint
(localVars.tokenAmountForPresaleNew, localVars.tokenAmountForAmmNew) = _tokenAmountsForLiquidityBootstrap(
initParams.virtualEth, initParams.bootstrapEth, localVars.reserveEth, initParams.initialTokenMatch
);
localVars.reserveToken = _reserveToken;
// amount of tokens transferred by the new team
uint256 tokenAmountIn = IERC20(_token).balanceOf(address(this)) - localVars.reserveToken;
if (
tokenAmountIn
< (
localVars.tokenAmountForPresaleOld + localVars.tokenAmountForAmmOld - localVars.reserveToken
+ localVars.tokenAmountForPresaleNew + localVars.tokenAmountForAmmNew
)
) {
revert GoatErrors.IncorrectTokenAmount();
}
localVars.pendingLiquidityFees = _pendingLiquidityFees;
localVars.pendingProtocolFees = _pendingProtocolFees;
// amount of weth transferred by the new team
uint256 wethAmountIn = IERC20(_weth).balanceOf(address(this)) - localVars.reserveEth
- localVars.pendingLiquidityFees - localVars.pendingProtocolFees;
if (wethAmountIn < localVars.reserveEth) {
revert GoatErrors.IncorrectWethAmount();
}
_handleTakeoverTransfers(
IERC20(_weth), IERC20(_token), initialLpInfo.liquidityProvider, localVars.reserveEth, localVars.reserveToken
);
uint256 lpBalance = balanceOf(initialLpInfo.liquidityProvider);
_burn(initialLpInfo.liquidityProvider, lpBalance);
// new lp balance
lpBalance = Math.sqrt(uint256(initParams.virtualEth) * initParams.initialTokenMatch) - MINIMUM_LIQUIDITY;
_mint(to, lpBalance);
_updateStateAfterTakeover(
initParams.virtualEth,
initParams.bootstrapEth,
initParams.initialTokenMatch,
wethAmountIn,
tokenAmountIn,
lpBalance,
to,
initParams.initialEth
);
}
Although there is a check for the amount of token at L481, if the caller sets initParams.initialEth to 0, it can easily pass L481 because a smaller initParams.initialEth results in a larger localVars.tokenAmountForAmmNew + localVars.tokenAmountForPresaleNew.
This is due to the fact that the former initial provider's initialEth does not have any effect in preventing takeovers.
Impact
A pool could be unfairly taken over because the former initial provider's initialEth does not have any effect in preventing takeovers.
Code Snippet
Tool used
Manual Review
Recommendation
There should be a check for initParams.initialEth.
Discussion
chiranz
Even if check is bypassed L481 it will revert here L510-L515
chiranz
Even if check is bypassed L481 it will revert here L510-L515
I misread your finding.. It's valid when initialEth used by someone is non zero but the one who is taking over can pass initialEth as 0 and take over pool.
zzykxx
Escalate
I'm not 100% sure about this, but there is a good chance this is a duplicate of #46. Escalating for further discussion.
sherlock-admin2
Escalate
I'm not 100% sure about this, but there is a good chance this is a duplicate of #46. Escalating for further discussion.
You've created a valid escalation!
To remove the escalation from consideration: Delete your comment.
You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.
whitehair0330
In issue #46, they also mentioned L481 a bit; however, it only discussed checking virtualEth and not initialEth.
chiranz
I agree that this bug is independent of #46. So, not a duplicate.
cvetanovv
I agree with the @chiranz
whitehair0330
Hello everyone, please review this issue again. I think that it is a high severity issue.
Evert0x
Planning to reject escalation and keep issue state as is
Evert0x
Result: Medium Unique
sherlock-admin3
Escalations have been resolved successfully!
Escalation status:
- zzykxx: rejected
sherlock-admin4
The protocol team fixed this issue in the following PRs/commits: https://github.com/inedibleX/goat-trading/pull/6
sherlock-admin4
The Lead Senior Watson signed off on the fix.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A medium-severity business logic bug in Goat Trading. Sherlock made it public on 6 April 2024. Found in the 178 audit contest (finding M-2).
About Business logic
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
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.