Deposit of stETH fails due to LIDOs - wei corno
The depositStETH function moves the user's stETH into the depositor contract and then asks the vault to pull the same amount, but Lido's stETH balance maths can leave 1-2 wei behind on transfer, so the second step can fail. An attacker or ordinary user could see deposits revert randomly, meaning the contract does not deliver its promised deposit behaviour.
Originally disclosed on Immunefi
- Disclosed after under an hour
28663 - [SC - Low] Deposit of stETH fails due to LIDOs - wei corno...
Submitted on Feb 23rd 2024 at 10:22:59 UTC by @codesentry for Boost | Puffer Finance
Report ID: #28663
Report type: Smart Contract
Report severity: Low
Target: https://etherscan.io/address/0x7276925e42f9c4054afa2fad80fa79520c453d6a
Impacts:
- Contract fails to deliver promised returns, but doesn't lose value
Description
Brief/Intro
depositStETH method of PufferDepositor contract transfer stETH from msg.sender to PufferDepositor and then PufferVault transfer it from PufferDepositor. Overall depositStETH may fails randomly because of random 1 wei cornor issue in LIDO's stETH.
Vulnerability Details
stETH balance calculation includes integer division, and there is a common case when the whole stETH balance can't be transferred from the account while leaving the last 1-2 wei on the sender's account. The same thing can actually happen at any transfer or deposit transaction. This issue is documented here(https://github.com/lidofinance/lido-dao/issues/442) and still an valid issue. Same is documented in LIDO's official document(https://docs.lido.fi/guides/lido-tokens-integration-guide/) also.
Below is the code snippet that has bug.
function depositStETH(Permit calldata permitData) external restricted returns (uint256 pufETHAmount) {
try ERC20Permit(address(_ST_ETH)).permit({
owner: msg.sender,
spender: address(this),
value: permitData.amount,
deadline: permitData.deadline,
v: permitData.v,
s: permitData.s,
r: permitData.r
}) { } catch { }
SafeERC20.safeTransferFrom(IERC20(address(_ST_ETH)), msg.sender, address(this), permitData.amount);
return PUFFER_VAULT.deposit(permitData.amount, msg.sender);
}
Assume user is depositing 2stETH. safeTransferFrom transfers 2 stETH but PufferDepositor contract get 2stETH minus 1 wei.
PUFFER_VAULT.deposit(2e18, msg.sender) would fail because contract don't have enough stETH due to 1 wei corner issue .
This issues is an rounding issue and can happens randomly.
Impact Details
depositStETH() fails . Contract fails to perform intended functionality.
References
https://github.com/lidofinance/lido-dao/issues/442 https://docs.lido.fi/guides/lido-tokens-integration-guide/
Proof of concept
Bug in this line of code
SafeERC20.safeTransferFrom(IERC20(address(_ST_ETH)), msg.sender, address(this), permitData.amount);
return PUFFER_VAULT.deposit(permitData.amount, msg.sender);
As documented in LIDO, this is rounding issue . This may happen randomly. POC would be successful only when rounding issue occurs. I believe this is straight forward issue and hence POC may not require . Still
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 code assumes the amount received equals the amount requested, ignoring stETH's documented rounding behaviour where a transfer can leave 1-2 wei on the sender.
Impact
depositStETH can revert randomly, so the contract fails to perform its intended deposit functionality, though no funds are lost.
What the attacker needed
User holds stETH; rounding shortfall occurs on transfer
The fix
The deposit should measure the actual balance received rather than trusting the requested amount, or otherwise tolerate the 1-2 wei shortfall before calling the vault.
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.
Some tokens like Lido's stETH calculate balances with division, so a transfer can arrive one tiny unit (a wei, the smallest amount of ether) short of what was sent. This contract first receives the tokens and then asks another contract to take exactly the original amount, so that missing wei makes the second step fail. The result is that a normal deposit sometimes just reverts.
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.