ERC4626 inflate issue mitigation is not sufficient
The ERC4626 inflation mitigation in the gate and absorber contracts is insufficient because the total assets are calculated using the contract's token balance, which can be inflated by a direct transfer. An attacker can donate a large amount of tokens to the contract, causing subsequent depositors to receive far fewer shares and lose funds when they withdraw.
jasonxiale Follow with bin2chen
Originally disclosed on Code4rena
Submitted by jasonxiale, also found by bin2chen
Lines of code
https://github.com/code-423n4/2024-01-opus/blob/4720e9481a4fb20f4ab4140f9cc391a23ede3817/src/core/gate.cairo#L191-L223 https://github.com/code-423n4/2024-01-opus/blob/4720e9481a4fb20f4ab4140f9cc391a23ede3817/src/core/absorber.cairo#L667-L705
Impact
Both absorber and gate use the same mitigation for ERC4626 first depositor front-running vulnerability, but current implementation is not sufficient. By abusing the flaw, even though malicious attacker can’t benefit from the mitigation, he can cause other normal users to lose assets.
Proof of Concept
Because of absorber and gate use the same mitigation, I will take gate as example.
Suppose the yang’s decimals is 18:
When sentinel.add_yang is called to add yang to shrine, initial_yang_amt is passed to shrine.add_yang as mitigation to the inflate issue. And initial_yang_amt is set as INITIAL_DEPOSIT_AMT which is const INITIAL_DEPOSIT_AMT: u128 = 1000;.
In sentinel.cairo#L204-L206, yang_erc20.transfer_from is called to transfer 1000 wei yang_erc from caller to gate.
And then the code flow will fall into shrine.add_yang. When the function is called, initial_yang_amt is still 1000.
In shrine.add_yang, the yang_total will be set to 1000 in shrine.cairo#L591.
So when the admin (which is the first depositor) calls sentinel.add_yang, he will transfer 1000 wei yang_asset and will receive 1000 yang_amt yang.
After that, when the second user calls abbot.open_trove, gate.convert_to_yang_helper will calculate the yang_amt by gate.cairo#L220:
fn convert_to_yang_helper(self: @ContractState, asset_amt: u128) -> Wad {
let asset: IERC20Dispatcher = self.asset.read();
let total_yang: Wad = self.get_total_yang_helper(asset.contract_address);
if total_yang.is_zero() {
let decimals: u8 = asset.decimals();
// Otherwise, scale `asset_amt` up by the difference to match `Wad`
// precision of yang. If asset is of `Wad` precision, then the same
// value is returned
fixed_point_to_wad(asset_amt, decimals)
} else {
(asset_amt.into() * total_yang) / get_total_assets_helper(asset).into() <<<--- the second user calcuates the `yang_amout` using this code
}
}
And gate.get_total_assets_helper is using asset.balance_of:
#[inline(always)]
fn get_total_assets_helper(asset: IERC20Dispatcher) -> u128 {
asset.balance_of(get_contract_address()).try_into().unwrap() <<<--- balance_of is used here
}
So to sum up, in the current implementation:
- The first depositor (which is also the admin) will deposit 1000 wei ERC, and will receive 1000
yang_amt. - The second depositor will get his
yang_amtbased off(asset_amt.into() * total_yang) / get_total_assets_helper(asset).into().
Suppose in a case that the asset is DAI (18 decimals):
- When
yang_daiis created, the admin callsadd_yangto add DAI into the protocol, and he will transfer 1000 wei DAI togate, and will receive 1000yang_amt. - A malicious tranfers (
100000 * 1e18 - 1000wei) DAI (which is worth 100000 USD) togate. - Alice, a normal user, deposits
199 * 1e18DAI (which is worth 199 USD) by callingabbot.open_trove, based on gate.cairo#L220. Alice will get199 * 1e18 * 1000 / (100000 * 1e18)= 1 wei. - If Alice wants to close her trove,
gate.convert_to_assetswill be used to calculate the asset amount, according to gate.cairo#L191-L204.
gate.cairo#L202 will be used: ((yang_amt * get_total_assets_helper(asset).into()) / total_yang).val, because Alice has 1 wei yang_amout, and get_total_assets_helper() is (100000 + 199) * 1e18 and total_yang will be 1001.
So Alice will get 1 * (100000 * 1e18 + 199 * 1e18) / (1000 + 1) = 100.0989010989011 * 1e18, which is 100 USD. And the remaining 99 * 1e18 DAI will be in the gate.
In the above case, there will be two issues:
- Alice will lose her assets.
- After Alice closes her trove, the system’s balance will be
(100000 + 99) *1e18, which is becoming larger. And the balance will keep growing with time going, which will cause users to lose more assets.
Tools Used
VIM
Recommended Mitigation Steps
In gate.get_total_assets_helper, don’t use balance_of to calculate the amount; instead, define a new variables and record the deposited asset amount by the variables
Assessed type
ERC4626
tserg (Opus) confirmed via duplicate issue #196
This submission details the vulnerability with a greater impact than #196, and has thus been selected as primary.
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 contract calculates total assets using its token balance (balance_of) instead of tracking deposits internally, allowing an attacker to inflate the balance with a direct transfer.
Impact
Normal users can lose a significant portion of their deposited assets, and the system's balance grows over time, causing further losses.
What the attacker needed
Attacker can transfer tokens directly to the contract; victim deposits after the inflation
How it was found
- The admin adds a new asset (yang) by calling add_yang, depositing 1000 wei and receiving 1000 yang_amt.
- A malicious user transfers a large amount of the asset directly to the gate contract.
- A normal user deposits a significant amount by calling abbot.open_trove, but receives only 1 wei of yang_amt due to the inflated total assets.
- When the user closes their trove, they receive only about half of their deposit, losing the rest.
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.
This is about a smart contract that holds tokens and gives out shares in return. The contract's total token balance is used to decide how many shares a new deposit gets. An attacker can send extra tokens directly to the contract, making the balance look huge, so the next person's deposit is worth almost nothing in shares. When that person withdraws, they get back much less than they put in.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.