ETH deposited by the user may be stolen
The deposit and mint functions in LMPVaultRouterBase wrap ETH into WETH but then pull the deposit amount from the user's WETH balance, leaving the wrapped WETH in the contract. An attacker can steal this excess WETH using the public sweepToken function.
0x007 Follow with 0x3b, 0xComfyCat and 34 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-06-tokemak-judging/issues/1
Found by
0x007, 0x3b, 0xComfyCat, 0xDjango, 0xJuda, 0xSurena, 0xbepresent, 0xdeadbeef, 0xmuxyz, 0xvj, Breeje, Flora, SaharDevep, TangYuanShen, VAD37, asui, berndartmueller, bin2chen, caelumimperium, chaduke, ck, duc, enfrasico, harisnabeel, lemonmon, lodelux, n33k, nobody2018, p0wd3r, pengun, rvierdiiev, saidam017, shogoki, talfao, vagrant, warRoom, xiaoming90
Due to the fact that the WETH obtained through _processEthIn belongs to the contract, and pullToken transfers assets from msg.sender, it is possible for users to transfer excess WETH to the contract, allowing attackers to steal WETH from within the contract using sweepToken.
Both mint and deposit in LMPVaultRouterBase have this problem.
Vulnerability Detail
In the deposit function, if the user pays with ETH, it will first call _processEthIn to wrap it and then call pullToken to transfer.
/// @inheritdoc ILMPVaultRouterBase
function deposit(
ILMPVault vault,
address to,
uint256 amount,
uint256 minSharesOut
) public payable virtual override returns (uint256 sharesOut) {
// handle possible eth
_processEthIn(vault);
IERC20 vaultAsset = IERC20(vault.asset());
pullToken(vaultAsset, amount, address(this));
return _deposit(vault, to, amount, minSharesOut);
}
_processEthIn will wrap ETH into WETH, and these WETH belong to the contract itself.
function _processEthIn(ILMPVault vault) internal {
// if any eth sent, wrap it first
if (msg.value > 0) {
// if asset is not weth, revert
if (address(vault.asset()) != address(weth9)) {
revert InvalidAsset();
}
// wrap eth
weth9.deposit{ value: msg.value }();
}
}
However, pullToken transfers from msg.sender and does not use the WETH obtained in _processEthIn.
function pullToken(IERC20 token, uint256 amount, address recipient) public payable {
token.safeTransferFrom(msg.sender, recipient, amount);
}
If the user deposits 10 ETH and approves 10 WETH to the contract, when the deposit amount is 10, all of the user's 20 WETH will be transferred into the contract.
However, due to the amount being 10, only 10 WETH will be deposited into the vault, and the remaining 10 WETH can be stolen by the attacker using sweepToken.
function sweepToken(IERC20 token, uint256 amountMinimum, address recipient) public payable {
uint256 balanceToken = token.balanceOf(address(this));
if (balanceToken < amountMinimum) revert InsufficientToken();
if (balanceToken > 0) {
token.safeTransfer(recipient, balanceToken);
}
}
Both mint and deposit in LMPVaultRouterBase have this problem.
Impact
ETH deposited by the user may be stolen.
Code Snippet
- https://github.com/sherlock-audit/2023-06-tokemak/blob/main/v2-core-audit-2023-07-14/src/vault/LMPVaultRouterBase.sol#L43-L57
- https://github.com/sherlock-audit/2023-06-tokemak/blob/main/v2-core-audit-2023-07-14/src/utils/PeripheryPayments.sol#L54-L56
- https://github.com/sherlock-audit/2023-06-tokemak/blob/main/v2-core-audit-2023-07-14/src/utils/PeripheryPayments.sol#L58-L65
Tool used
Manual Review
Recommendation
Perform operations based on the size of msg.value and amount:
msg.value == amount: transfer WETH from contract notmsg.sendermsg.value > amount: transfer WETH from contract notmsg.senderand refund tomsg.sendermsg.value < amount: transfer WETH from contract and transfer remaining frommsg.sender
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 deposit logic wraps ETH into WETH but then pulls the deposit amount from the user's WETH balance instead of using the wrapped ETH, leaving the wrapped WETH in the contract where it can be swept.
What the attacker needed
User deposits ETH and approves WETH; Attacker can call sweepToken
The fix
The deposit logic should be adjusted to handle msg.value and amount correctly, such as using the wrapped WETH from the contract when msg.value equals or exceeds amount, and refunding any excess.
How it was found
- User calls deposit with ETH and approves WETH to the contract.
- The contract wraps the ETH into WETH, which belongs to the contract.
- The contract then pulls the deposit amount of WETH from the user's wallet.
- The wrapped WETH remains in the contract.
- Attacker calls sweepToken to transfer all WETH from the contract to themselves.
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.
When you deposit ETH, the contract first turns it into WETH (a token version of ETH). But then it also takes the same amount of WETH from your wallet, so the WETH from your ETH stays in the contract. Anyone can call a function to sweep all tokens from the contract, so they can take that leftover WETH.
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.