AccountingManager::resetMiddle will not behave as expected
The resetMiddle function in the AccountingManager contract does not reset the currentWithdrawGroup.totalCBAmount when resetting the withdrawal queue, so after a reset, subsequent calls to calculateWithdrawShares will increment this value again, leading to an inflated total base token amount needed for withdrawals. This can cause the protocol to withdraw more funds than intended or, if the inflated amount exceeds the protocol's TVL, block withdrawals until new deposits are made.
Submitted by Aymen0909 (View additional reports submitted by other wardens)
https://github.com/code-423n4/2024-04-noya/blob/main/contracts/accountingManager/AccountingManager.sol#L461-L468 https://github.com/code-423n4/2024-04-noya/blob/main/contracts/accountingManager/AccountingManager.sol#L328-L354
The resetMiddle function in the AccountingManager contract is designed to reset the middle index of either the deposit or withdraw queue. This function is crucial for managing the order and processing of transactions within these queues, particularly in scenarios where recalculations are necessary to ensure fairness and accuracy in the distribution of shares or handling of withdrawals.
The function will work as expected when resetting the deposit queue, but it will not work for the reset of the withdrawal queue and will cause wrong withdrawal amount accounting to occurs.
To understand the issue we must first see how the withdrawal queue middle is calculated. The middle index of the withdrawal queue is updated during the process of calculating the withdrawal shares. This occurs in the calculateWithdrawShares function, which is designed to process queued withdrawal requests and assign the corresponding amount of base tokens to each request based on the current share price.
For each withdrawal request in the queue that has not yet been processed, the function calculates the amount of base tokens equivalent to the shares requested for withdrawal. After processing the maximum allowed iterations or reaching the end of the queue, the function updates the official middle index of the withdrawal queue to the last processed index (middleTemp) and increments the total base token amount needed for the current withdraw group currentWithdrawGroup.totalCBAmount (remember this state as it will be important).
Here is the relevant part of the code that handles the updating of the middle index:
function calculateWithdrawShares(uint256 maxIterations) public onlyManager nonReentrant whenNotPaused {
uint256 middleTemp = withdrawQueue.middle;
uint64 i = 0;
...
while (withdrawQueue.last > middleTemp && i < maxIterations) {
WithdrawRequest storage data = withdrawQueue.queue[middleTemp];
uint256 assets = previewRedeem(data.shares);
data.amount = assets;
data.calculationTime = block.timestamp;
emit CalculateWithdraw(middleTemp, data.owner, data.receiver, data.shares, assets, block.timestamp);
middleTemp += 1;
i += 1;
}
currentWithdrawGroup.totalCBAmount += assetsNeededForWithdraw;
withdrawQueue.middle = middleTemp;
}
Now let’s see how the resetMiddle function will cause an issue to occur in the withdrawal process:
function resetMiddle(
uint256 newMiddle,
bool depositOrWithdraw
) public onlyManager {
if (depositOrWithdraw) {
emit ResetMiddle(newMiddle, depositQueue.middle, depositOrWithdraw);
if (
newMiddle > depositQueue.middle ||
newMiddle < depositQueue.first
) {
revert NoyaAccounting_INVALID_AMOUNT();
}
depositQueue.middle = newMiddle;
} else {
emit ResetMiddle(
newMiddle,
withdrawQueue.middle,
depositOrWithdraw
);
if (
newMiddle > withdrawQueue.middle ||
newMiddle < withdrawQueue.first ||
currentWithdrawGroup.isStarted
) {
revert NoyaAccounting_INVALID_AMOUNT();
}
withdrawQueue.middle = newMiddle;
//@audit currentWithdrawGroup.totalCBAmount is not reset
}
}
As it can be seen the function doesn’t reset the value for current withdraw group currentWithdrawGroup.totalCBAmount which will keep its value from the previous calculation which is supposed to be wrong, and thus when later on the manager will call calculateWithdrawShares to calculate the withdrawal shares once again, currentWithdrawGroup.totalCBAmount will get incremented on the previously calculated amount which will result in a bigger base token amount needed for the withdrawal to be processed than what was supposed to be.
Thus the accounting for the current withdrawal group will be wrong. This issue will cause the following problems:
- First it will force the protocol manager to withdraw more funds than intended from the connectors to reach the amount needed for withdrawal of current group represented by
currentWithdrawGroup.totalCBAmount(which is wrong -bigger- in our case after the reset has occurred), this will result in potential yield that could’ve been generated and thus a loss for funds for the protocol and its users. - The worst that can happen is if
currentWithdrawGroup.totalCBAmountafter being incremented twice for the same group (the first time and the second time after the reset call as it wasn’t updated) is bigger that the current protocol TVL (which can happen especially in early days of the vault), in that case the protocol is unable to get necessary amount of funds to execute the withdrawal and the withdrawal process and funds will remain blocked until new deposit are made & executed.
Impact
The reset function doesn’t reset the withdrawal amount requested currentWithdrawGroup.totalCBAmount which will cause problems in the withdrawal process and result a loss of funds for the protocol and all other users.
Recommended Mitigation
To address this issue, the reset function must reset the withdrawal amount requested currentWithdrawGroup.totalCBAmount (set back to 0), the function should be modified as follows:
function resetMiddle(
uint256 newMiddle,
bool depositOrWithdraw
) public onlyManager {
if (depositOrWithdraw) {
emit ResetMiddle(newMiddle, depositQueue.middle, depositOrWithdraw);
if (
newMiddle > depositQueue.middle ||
newMiddle < depositQueue.first
) {
revert NoyaAccounting_INVALID_AMOUNT();
}
depositQueue.middle = newMiddle;
} else {
emit ResetMiddle(
newMiddle,
withdrawQueue.middle,
depositOrWithdraw
);
if (
newMiddle > withdrawQueue.middle ||
newMiddle < withdrawQueue.first ||
currentWithdrawGroup.isStarted
) {
revert NoyaAccounting_INVALID_AMOUNT();
}
withdrawQueue.middle = newMiddle;
++ currentWithdrawGroup.totalCBAmount = 0;
}
}
gzeon (judge) increased severity to High
HadiEsna (NOYA) confirmed and commented:
Fixed in commit 962d297e937e82d95e20739078a61b00eea47643. If we are resetting the withdrawal queue, we have to reset all withdrawals that are calculated so far. So we can safely set the
totalCBAmountto 0.
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 resetMiddle function fails to reset the currentWithdrawGroup.totalCBAmount to zero when resetting the withdrawal queue, so subsequent calculations double-count the required base tokens.
Impact
The inflated totalCBAmount could force the protocol to withdraw more funds than intended, causing a loss of potential yield, or if it exceeds the protocol's TVL, block withdrawals until new deposits are made.
What the attacker needed
Manager role; resetMiddle called on withdrawal queue; calculateWithdrawShares called again
The fix
The resetMiddle function should set currentWithdrawGroup.totalCBAmount to zero when resetting the withdrawal queue, and this was fixed in a commit.
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 a smart contract, a queue is used to process withdrawals. The contract keeps a running total of how many tokens are needed for the current batch of withdrawals. When an admin resets the queue's middle position, the code forgets to reset that running total, so it keeps adding to the old number. This can make the contract think it needs more tokens than it actually does, which can cause problems like withdrawing too much or getting stuck.
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.