User may not receive profit from withdrawal fee as expected and attacker can steal value from pool
When multiple withdrawal requests are processed in a batch, the withdrawal fee does not increase the value of LP tokens for the users in that batch, so they receive less profit than expected. An attacker can front-run a large batch withdrawal by minting LP tokens and then back-run to withdraw, stealing the fee value from the pool.
IronsideSec Follow with h2134
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-06-mellow-judging/issues/162
The protocol has acknowledged this issue.
Found by
Ironsidesec, h2134
Summary
When a user's withdrawal request is processed, the user is charged a withdrawal fee. The withdrawal fee increases the value of the LP tokens, and the LP token holders earns profit from it.
However, when multiple withdrawal requests are processed in a batch, the single withdrawal contributes no value to LP token until the end of the process, user may not receive profit as expected, and the value can be steal by an attacker through sandwich attack.
Vulnerability Detail
It is expected that a user earn profit from the withdrawal fee paid by other users, for example, when userA withdraws, the withdrawal fee is charged and LP token becomes more valuable, then when userB withdraws, they may withdraw more tokens than they deposited.
However, users earn no profit from withdrawal fee if their withdrawal requests are processed in a batch. When multiple withdrawal requests are processed in a batch, the state stack required for processing withdrawal requests is calculated:
function calculateStack()
public
view
returns (ProcessWithdrawalsStack memory s)
{
(address[] memory tokens, uint256[] memory amounts) = underlyingTvl();
s = ProcessWithdrawalsStack({
tokens: tokens,
ratiosX96: IRatiosOracle(configurator.ratiosOracle())
.getTargetRatiosX96(address(this), false),
erc20Balances: new uint256[](tokens.length),
@> totalSupply: totalSupply(),
totalValue: 0,
ratiosX96Value: 0,
timestamp: block.timestamp,
feeD9: configurator.withdrawalFeeD9(),
tokensHash: keccak256(abi.encode(tokens))
});
...
}
The state stack is then passed to analyzeRequest() to calculate the expected token amount for each request.
function analyzeRequest(
ProcessWithdrawalsStack memory s,
WithdrawalRequest memory request
) public pure returns (bool, bool, uint256[] memory expectedAmounts) {
...
@> uint256 value = FullMath.mulDiv(lpAmount, s.totalValue, s.totalSupply);
@> value = FullMath.mulDiv(value, D9 - s.feeD9, D9);
@> uint256 coefficientX96 = FullMath.mulDiv(value, Q96, s.ratiosX96Value);
...
for (uint256 i = 0; i < length; i++) {
uint256 ratiosX96 = s.ratiosX96[i];
@> expectedAmounts[i] = ratiosX96 == 0 ? 0 : FullMath.mulDiv(coefficientX96, ratiosX96, Q96);
if (expectedAmounts[i] >= request.minAmounts[i]) continue;
return (false, false, expectedAmounts);
}
...
return (true, true, expectedAmounts);
}
To simplify, the expected token amount is calculated as below:
expectedAmounts = ((lpAmount * s.totalValue / s.totalSupply) / s.ratiosX96Value) * ratiosX96
The problem is that s.totalSupply and s.totalValue are constant during the calculation process, indicating that no withdrawal fee is contributed to the value of the LP token, even if the fee has been charged. For instance, in the batch [userA request, userB request, userC request], userB won't receive profit from the withdrawal fee paid by userA, and userC receive no profit from the fee paid by userA and userB.
The users' LP tokens are burned at the end of the batch withdrawal process, by then the value of the LP token is increased. This can be leveraged by an attacker, who may intentionally front-run a large batch withdrawal to mint LP tokens and then back-run to withdraw, hence steal the value from the pool.
Impact
- User may receive much less profit than expected, or no profit at all;
- Malicious user can steal withdrawal fee from the pool.
Code Snippet
https://github.com/sherlock-audit/2024-06-mellow/blob/main/mellow-lrt/src/Vault.sol#L476-L504
Tool used
Manual Review
Recommendation
It is expected the withdrawal requests are processed sequentially in the registration time order, so each withdrawal should generate profit to the following ones. This can be achieved by updating s.totalSupply and s.totalValue in the process of calculating expectedAmounts.
function analyzeRequest(
ProcessWithdrawalsStack memory s,
WithdrawalRequest memory request
) public pure returns (bool, bool, uint256[] memory expectedAmounts) {
...
for (uint256 i = 0; i < length; i++) {
- if (s.erc20Balances[i] >= expectedAmounts[i]) continue;
+ if (s.erc20Balances[i] >= expectedAmounts[i]) {
+ s.totalSupply -= request.amount;
+ s.totalValue -= value;
+ continue;
+ }
return (true, false, expectedAmounts);
}
return (true, true, expectedAmounts);
}
Discussion
0xh2134
Escalate.
This issue is valid.
sherlock-admin3
Escalate.
This issue is valid.
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.
z3s
The reason I invalidated this submission was: For the first issue about fees, I think it's a design decision.
Design decisions are not valid issues. Even if the design is suboptimal, but doesn't imply any loss of funds, these issues are considered informational.
For the second issue, the protocol team answer for front-running issues was
processWithdrawalswill occur through flashbots.
In my opinion this submission is valid Low/Info.
0xh2134
@z3s Thanks for quick reply, really appreciate it. From my point of view:
- I agree it's a design decision, but it's more than suboptimal, given that withdrawal fee is one the the few (2, I believe) ways that user can earn the profit, there can be significant profit loss to the users;
- Sure they can, but unless the same is stated before or during the contest period, it should not be took into consideration, otherwise all the front-running findings can be invalidated by simply saying "invalid because admin/user can use flashbots";
- Another impact I can think of is that a large amount of collateral token can be stuck if there is a "whole withdrawal" (this is possible and can be happening for several times, especially in the early/late stage of the project): assume that there are 100 depositors, the average deposit is 10 ether and withdrawal fee is 5%, if all of them submit to withdraw and their requests are processed in a batch, the withdrawal fee is 50 ether in total and the fee is stuck in the contract because no one has LP token left. (One may argue that there is still initial deposit from admin, but from the context of the contest, the initial deposit is for setting LP price only, it's reasonable to assume the deposit won't be withdrawn).
WangSecurity
About the first impact, I believe it's the design decision to not payout the fee to other users in the withdrawal batch and the User B and User C in your example shouldn't get the fee. About the second impact, I don't think we can say the fee is stolen, because I assume the attacker won't receive the entire fee (unless he's the only one in the vault) and they will receive their value proportional to their deposit. Hence, I believe it also works as it should.
About the new impact in the above comment. Firstly, it has to be in the report, additionally, again I believe it's the way the protocol should work, if there is no one in the vault, then no one should receive the fee.
Planning to reject the escalation and leave the issue as it is.
0xh2134
@WangSecurity
- It's design decision but leads to user losses profit, I don't think it's just suboptimal;
- It's true attacker can not receive all the fees, but they will steal much more than expected, depending on the processed withdrawal in a batch, the stolen fees can be 100 times (assume 100 withdrawals processed) larger than usual, I don't believe this is expected;
- Maybe I misunderstand it but I think an issue should be judged by the root cause, not just the impact, I can see #244 mentioned the similar impact and it is escalated to be valid.
WangSecurity
It's design decision but leads to user losses profit, I don't think it's just suboptimal
So that's a tricky part that we've got a misunderstanding on, I'll try to explain my view. When the withdrawals are made in a batch, I don't think it makes sense to increase the profit of the next user in this batch. They're withdrawing from the pool in the very same transaction/block basically so why they should get the profit from it, if they are also going to exit the vault in the very same transaction. From my point of view, the users that remain in the pool should receive this fee, cause they're still in the pool after that batch withdrawal. So that's what I mean it's a design decision to reward the users that are remained in the vault and not the ones that are withdrawing in the very same transaction.
they will steal much more than expected
What do you mean exactly by more than usual? I assume even if they didn't intentionally deposit before the large withdrawal (maybe a couple of blocks before), they still get the fees proportional to their deposit. Though, I see the scenario how this accruing of value is not totally fair to other users who have been invested for longer than the attacker, I think it's the design to allocate the fees on pro rata basis to everyone invested in the vault.
Maybe I misunderstand it but I think an issue should be judged by the root cause, not just the impact, I can see https://github.com/sherlock-audit/2024-06-mellow-judging/issues/244 mentioned the similar impact and it is escalated to be valid
You're correct, I've missed both of these have the same root cause, but I'm looking to invalidate #244 as well, since the left in the contract fees can be extracted by the admin.
Hence, the decision remains the same, reject the escalation and leave the issue as it is.
0xh2134
@WangSecurity Thanks for the clarification.
What do you mean exactly by more than usual?
When it is compared to the normal (individual) withdrawal, the stolen fee can be much larger.
but I'm looking to invalidate https://github.com/sherlock-audit/2024-06-mellow-judging/issues/244 as well
Likewise, the stuck funds can be much larger than expect, so if you reconsider #244 to be valid, you may kindly reconsider this issue.
Cheers!
WangSecurity
Yep, I'm considering both the scenario in #116 and this issue, since they're pretty much the same.
WangSecurity
Upon further inspecting this scenario, I agree it's valid and allows the attacker to extract value from the protocol and steal it from the honest depositors. Planning to accept the escalation and validate the issue with Medium severity, because to extract large value the attacker has to have a large capital, has to wait for the withdrawal delay and pay the withdrawal fee. I believe these arguments together make this medium severity. The duplicate is #116. Are there any other duplicates?
10xhash
Upon further inspecting this scenario, I agree it's valid and allows the attacker to extract value from the protocol and steal it from the honest depositors. Planning to accept the escalation and validate the issue with Medium severity, because to extract large value the attacker has to have a large capital, has to wait for the withdrawal delay and pay the withdrawal fee. I believe these arguments together make this medium severity. The duplicate is #116. Are there any other duplicates?
How is this valid? This is purely a design choice which is perfectly valid
allows the attacker to extract value from the protocol and steal it from the honest depositors
If an attacker is depositing a large amount of assets, they will obtain the proportionate fees. It doesn't matter if the protocol decides to give the fees to the remaining depositors (in this case) or to include the depositors who are withdrawing in the same call. And there is no perfect logic as to how to distribute the fees
This type of design combats MEV in certain scenarios (because the attacker has to wait for processingDelay in order to gain the fees) compared to the other scenario where the fees is being distributed to the current withdrawer's too (a user having withdrawal request at the last can front run increase the deposit amount and then gain the fees in the same block)
And this type of attack is usually not profitable because the attacker has to pay the fees on the entire deposit amount while they only gain the fee from other user deposits (unless extreme scenarios like very low remaining vault amount and very high in queue withdrawals occur)
IronsideSec
How is this valid? This is purely a design choice which is perfectly valid
Design choice from sponsor was to allow funds back to vault, increasing LP vaulue by share price. Sniping that wih MEV is loss of funds. Wrong extraction of value that supposed to go to LPers.
It doesn't matter if the protocol decides to give the fees to the remaining depositors (in this case) or to include the depositors who are withdrawing in the same call. And there is no perfect logic as to how to distribute the fees
There is perfect logic here. Protocol decided to not charge fees there by incresing lp share price. But its prone to MEV extraction.
And this type of attack is usually not profitable because the attacker has to pay the fees on the entire deposit amount while they only gain the fee from other user deposits (unless extreme scenarios like very low remaining vault amount and very high in queue withdrawals occur)
very high in queue withdrawals is not exetreme low scenario, it will occur frequently. Operator's process withdrawal for example 20+ users will have huge volume of value to be extracted.
WangSecurity
My initial thought was that it is the design decision as well. But, after thinking about it more, I changed my decision because it allows to extract basically free value from the vault without actually being invested, essentially stealing value from the honest depositors. Moreover, this can be repeated on every large withdrawal, so the invested into protocol users don't get the full value they have to every time. Hence, the decision remains the same, validate this with medium severity with #116 being a duplicate.
0x3b33
I am not sure about 116, however this issue has a number of problems:
- Most obvious one - the solution would actually allow for more MEV as everything that needs to be done is for a user to FR withdraw, and schedule a big batch a the end. This way he will:
- leave without any penalties
- claim a huge portion of the withdraws (as he is the last and receiving the most LP value appreciation)
- be exposed to 0 risk
- Either the attacker needs to have a lot of assets as @10xhash pointed out
And this type of attack is usually not profitable because the attacker has to pay the fees on the entire deposit amount while they only gain the fee from other user deposits (unless extreme scenarios like very low remaining vault amount and very high in queue withdrawals occur)
Or in the case that @IronsideSec mentioned, the attacker will have a really small profit (most likely a loss due to point 3), while exposing himself to a lot of risk
very high in queue withdrawals is not extreme low scenario, it will occur frequently. Operator's process withdrawal for example 20+ users will have huge volume of value to be extracted.
- The attacker is still subjected to the withdraw fee. That combined with the increased risk makes this issue unworthy. Let me add some math:
Example - 1% withdraw fee:
Owning 50% of the pool after withdraws, will need the pool to generate 1% of our balance (or 0.5% of the pool total supply) in fees, meaning that we need to control 25% of the pool (before withdraw) and 50% to leave in order to pay 0 fee. Not even generating profit.
| prerequisites | values |
|---|---|
| Withdraw fee | 1% |
| Pool size | 20000 |
| total withdraws | 10000 |
| Attacker balance | 5000 |
| Remaining users balance | 5000 |
| Fee generated | 10k * 1% = 100 |
| Fees paid to attacker | 100 * 50% = 50 |
When owning 25% (before deposit) we would need at least 50% to leave in every fee range, be it the fee being 1%, 5% or even 10%. That is because we would also pay that fee when withdrawing.
The numbers speek for themselves. I don't think more explanations are needed.
IronsideSec
#116's profitability and POC discussed
WangSecurity
How is this valid? This is purely a design choice which is perfectly valid
It's a fair point, but I still think in that case the attacker can get the withdrawal fee almost for free. Almost is that they still have to suffer a withdrawal fee when they decide to exit and a withdrawal delay, but they don't have to be deposited into the protocol and acquire additional risks associated with it.
If an attacker is depositing a large amount of assets, they will obtain the proportionate fees. It doesn't matter if the protocol decides to give the fees to the remaining depositors (in this case) or to include the depositors who are withdrawing in the same call. And there is no perfect logic as to how to distribute the fees
Yes, the attacker will indeed suffer the proportionate withdrawal fees, but as seen in the above comment (in the POC linked in the above comment), the attack still has profit even taking the fees into account. About the solution, also a fair point, but a possible solution can be distributing rewards based on the time the depositors have spent in the vault. This way, front-running the deposit will give you zero value since the block.timestamp for the deposit and processed withdrawals would be the same. I understand it's a complex solution, still I believe it can solve this issue.
This type of design combats MEV in certain scenarios (because the attacker has to wait for processingDelay in order to gain the fees) compared to the other scenario where the fees is being distributed to the current withdrawer's too (a user having withdrawal request at the last can front run increase the deposit amount and then gain the fees in the same block) And this type of attack is usually not profitable because the attacker has to pay the fees on the entire deposit amount while they only gain the fee from other user deposits (unless extreme scenarios like very low remaining vault amount and very high in queue withdrawals occur)
This is also a fair point, but it still doesn't change the fact that the user can get the profit, essentially, stealing from other users, which again has been proven to be profitable when the user exits the protocol and pays the withdrawal fee. I agree that the withdrawal delay here is a constraint, but it doesn't eliminate the attack.
Most obvious one - the solution would actually allow for more MEV as everything that needs to be done is for a user to FR withdraw, and schedule a big batch a the end. This way he will
As I understand you refer to the solution in this report? It may not be perfect, of course. But the decision is based on the constraints and impact, not on the optimality and correctness of the solution.
Either the attacker needs to have a lot of assets as @10xhash pointed out Or in the case that @IronsideSec mentioned, the attacker will have a really small profit (most likely a loss due to point 3), while exposing himself to a lot of risk
Indeed, but the larger the capital of the attacker, the larger the profit they get and the loss other vault depositors suffer.
The attacker is still subjected to the withdraw fee. That combined with the increased risk makes this issue unworthy. Let me add some math
Yep, the withdrawal fee is a constrain here, but as proven with the coded POC, the attacker indeed can receive the profit and cause losses.
I hope that answers the questions and explains why I believe it's an issue. I see that it can be a design, but I believe it's an issue since it allows the user to gain a portion of the withdrawal fee almost for free, even taking the withdrawal fee and the delay into account, without being deposited into the protocol, contributing to it and being exposed to other risks of being deposited into the protocol, essentially stealing this portion of the fee from other users.
Hence, the decision remains the same to accept the escalation and validate this report with Medium severity and set #116 as duplicate.
10xhash
How is this valid? This is purely a design choice which is perfectly valid
It's a fair point, but I still think in that case the attacker can get the withdrawal fee almost for free. Almost is that they still have to suffer a withdrawal fee when they decide to exit and a withdrawal delay, but they don't have to be deposited into the protocol and acquire additional risks associated with it.
If an attacker is depositing a large amount of assets, they will obtain the proportionate fees. It doesn't matter if the protocol decides to give the fees to the remaining depositors (in this case) or to include the depositors who are withdrawing in the same call. And there is no perfect logic as to how to distribute the fees
Yes, the attacker will indeed suffer the proportionate withdrawal fees, but as seen in the above comment (in the POC linked in the above comment), the attack still has profit even taking the fees into account. About the solution, also a fair point, but a possible solution can be distributing rewards based on the time the depositors have spent in the vault. This way, front-running the deposit will give you zero value since the block.timestamp for the deposit and processed withdrawals would be the same. I understand it's a complex solution, still I believe it can solve this issue.
This type of design combats MEV in certain scenarios (because the attacker has to wait for processingDelay in order to gain the fees) compared to the other scenario where the fees is being distributed to the current withdrawer's too (a user having withdrawal request at the last can front run increase the deposit amount and then gain the fees in the same block) And this type of attack is usually not profitable because the attacker has to pay the fees on the entire deposit amount while they only gain the fee from other user deposits (unless extreme scenarios like very low remaining vault amount and very high in queue withdrawals occur)
This is also a fair point, but it still doesn't change the fact that the user can get the profit, essentially, stealing from other users, which again has been proven to be profitable when the user exits the protocol and pays the withdrawal fee. I agree that the withdrawal delay here is a constraint, but it doesn't eliminate the attack.
Most obvious one - the solution would actually allow for more MEV as everything that needs to be done is for a user to FR withdraw, and schedule a big batch a the end. This way he will
As I understand you refer to the solution in this report? It may not be perfect, of course. But the decision is based on the constraints and impact, not on the optimality and correctness of the solution.
Either the attacker needs to have a lot of assets as @10xhash pointed out Or in the case that @IronsideSec mentioned, the attacker will have a really small profit (most likely a loss due to point 3), while exposing himself to a lot of risk
Indeed, but the larger the capital of the attacker, the larger the profit they get and the loss other vault depositors suffer.
The attacker is still subjected to the withdraw fee. That combined with the increased risk makes this issue unworthy. Let me add some math
Yep, the withdrawal fee is a constrain here, but as proven with the coded POC, the attacker indeed can receive the profit and cause losses.
I hope that answers the questions and explains why I believe it's an issue. I see that it can be a design, but I believe it's an issue since it allows the user to gain a portion of the withdrawal fee almost for free, even taking the withdrawal fee and the delay into account, without being deposited into the protocol, contributing to it and being exposed to other risks of being deposited into the protocol, essentially stealing this portion of the fee from other users.
Hence, the decision remains the same to accept the escalation and validate this report with Medium severity and set #116 as duplicate.
Almost is that they still have to suffer a withdrawal fee when they decide to exit and a withdrawal delay, but they don't have to be deposited into the protocol and acquire additional risks associated with it. : What is the difference b/w suffering a withdrawal delay and being deposited into the protocol? The protocol is choosing the withdrawal delay according to their required convenience (ie. how much time they want the user's funds to be in the protocol before they can exit. And the estimated value was 1-7days)
About the solution, also a fair point, but a possible solution can be distributing rewards based on the time the depositors have spent in the vault. This way, front-running the deposit will give you zero value since the block.timestamp for the deposit and processed withdrawals would be the same. I understand it's a complex solution, still I believe it can solve this issue.: This is changing how the protocol is intending to work and would exactly be what is considered changing the design. The protocol is weighing on the deposit amounts rather than the time they are spending. If a user brings in 100eth and spends 2days in the protocol, he is considered more valuable to the protocol than a user who has deposited 10 eth and spent 200 days in the protocol.Uniswap gives the swap fees proportional to the concentrated liquidity provided by the user. And arguing that instead they should also factor in the time for which the deposits have been made would be fundamentally changing their design. Uniswap suffers from JIT, but here the team has decided to enforce withdrawal delay and withdrawal fee which they are comfortable with. If any user deposits a large portion of money for the withdrawal delay period, they are eligible for the fee share because they bring in that much amount of deposits to the protocol equivalent to any normal user who without any malicious intention deposits and withdraws in the said timeframe being eligible for the same amount of reward. And if there is a large amount of withdrawals compared to the remaining amounts in the protocol (or to be precise, atleast >50% of the entire vault tvl should be withdrawn in a single call which is the required condition for a user to gain a profit by performing the above sequence), the increase in LP share value would incentivize more user's to deposit which would benefit the protocol.
This is also a fair point, but it still doesn't change the fact that the user can get the profit, essentially, stealing from other users: This is not a stealing of deserved amount of assets from the other depositors. The project is not a whoever deposits longest will obtain the most rewards (from withdrawals), staking type project which is being broken down by a large deposit for a short timeframe. Apart from the core strategy of the protocol, withdrawal fees are being distributed to the depositors based on the amount of deposit that they provide which is being respected here (with the main intention of the fees being to avoid free swaps b/w the assets)
Indeed, but the larger the capital of the attacker, the larger the profit they get and the loss other vault depositors suffer
The POC meets the required condition for the required increase in lp share value (ie. >50.125% of the vault value being withdrawn) and it doesn't mean a user can simply deposit a lot of assets and gain more and more value. They are bounded by fees that they will have to pay and the amount that is being withdrawn. But the main point is the project wants to distribute the fees proportional to the deposit amount and a withdrawal delay that they are comfortable with is being enforced (ie. if a user is spending this much time in the protocol, they are eligible for the fee share)
0xh2134
What is the difference b/w suffering a withdrawal delay and being deposited into the protocol?
The difference is that attacker's funds won't be staked into Bound (In case Bond deposit limit is reached), they are considered no valuable to the Vault.
The protocol is weighing on the deposit amounts rather than the time they are spend
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
Impact
Users may receive less profit than expected, and an attacker could steal withdrawal fees from the pool, though the attacker must pay the withdrawal fee and wait for the withdrawal delay.
What the attacker needed
Attacker has large capital; attacker can front-run batch withdrawal; attacker pays withdrawal fee; attacker waits for withdrawal delay
The fix
The withdrawal requests should be processed sequentially, updating the total supply and total value after each request so that each withdrawal's fee benefits the following ones.
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.
In this system, users deposit money and get LP tokens representing their share. When someone withdraws, they pay a fee that is supposed to make the LP tokens more valuable for everyone else. But if many withdrawals happen at once, the fee is not added to the token value during the calculation, so users in that batch miss out. An attacker can quickly buy LP tokens just before a big batch withdrawal and sell right after, taking value that should have gone to honest users.
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.