Submitted by jasonxiale, also found by bin2chen
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
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 cant benefit from the mitigation, he can cause other normal users to lose assets.
Because of absorber and gate use the same mitigation, I will take gate as example.
Suppose the yangs 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:
And gate.get_total_assets_helper is using asset.balance_of:
So to sum up, in the current implementation:
Suppose in a case that the asset is DAI (18 decimals):
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:
VIM
In gate.get_total_assets_helper, dont use balance_of to calculate the amount; instead, define a new variables and record the deposited asset amount by the variables
ERC4626
tserg (Opus) confirmed via duplicate issue #196
0xsomeone (judge) commented:
