Market adapter removal corrupts withdraw sequence
The removeAdapter function deletes an entry from the withdrawSeq array using an index that was found for the moneyMarkets array, but the two arrays have independent orderings. This can corrupt the withdraw sequence and make withdrawals unavailable for the whole protocol.
Source: https://github.com/sherlock-audit/2023-02-union-judging/issues/24
Found by
hyh
Summary
Withdraw sequence entry is being deleted in AssetManager's removeAdapter() using incorrect index, so the sequence array becomes corrupted and withdrawals can end up unavailable.
Vulnerability Detail
index used for adapter removal is determined for moneyMarkets array, but applied to withdrawSeq as well, while in there indices do differ.
Impact
As withdrawals logic are based on withdraw sequence array, the withdrawals can end up unavailable. For example, if 95% of the funds are held in a market, whose entry corresponded to the index removed, withdrawals will be frozen for an arbitrary time for the whole protocol until withdrawSeq be manually restored.
Code Snippet
removeAdapter() determines the index for moneyMarkets array:
/**
* @dev Remove a adapter for the underlying lending protocol
* @param adapterAddress adapter address
*/
function removeAdapter(address adapterAddress) external override onlyAdmin {
bool isExist = false;
uint256 index = 0;
uint256 moneyMarketsLength = moneyMarkets.length;
for (uint256 i = 0; i < moneyMarketsLength; i++) {
if (adapterAddress == address(moneyMarkets[i])) {
isExist = true;
index = i;
break;
}
}
if (isExist) {
for (uint256 i = 0; i < supportedTokensList.length; i++) {
if (moneyMarkets[index].getSupply(supportedTokensList[i]) >= 10000) revert RemainingFunds(); //ignore the dust
}
moneyMarkets[index] = moneyMarkets[moneyMarketsLength - 1];
moneyMarkets.pop();
withdrawSeq[index] = withdrawSeq[withdrawSeq.length - 1];
withdrawSeq.pop();
_removeTokenApprovals(adapterAddress);
}
}
But withdrawSeq is a permutation of moneyMarkets, so their indices are independent and do not correspond to each other:
/**
* @dev Set withdraw sequence
* @param newSeq priority sequence of money market indices to be used while withdrawing
*/
function setWithdrawSequence(uint256[] calldata newSeq) external override onlyAdmin {
if (newSeq.length != moneyMarkets.length) revert NotParity();
withdrawSeq = newSeq;
}
So index in withdrawSeq being deleted do not generally correspond to the adapter, i.e. some other market end up being removed:
withdrawSeq[index] = withdrawSeq[withdrawSeq.length - 1];
withdrawSeq.pop();
Tool used
Manual Review
Recommendation
Consider repeating the entry finding logic for withdrawSeq, for example:
/**
* @dev Remove a adapter for the underlying lending protocol
* @param adapterAddress adapter address
*/
function removeAdapter(address adapterAddress) external override onlyAdmin {
bool isExist = false;
uint256 index = 0;
+ uint256 indexSeq = 0;
uint256 moneyMarketsLength = moneyMarkets.length;
for (uint256 i = 0; i < moneyMarketsLength; i++) {
if (adapterAddress == address(moneyMarkets[i])) {
isExist = true;
index = i;
break;
}
}
if (isExist) {
+ for (uint256 i = 0; i < moneyMarketsLength; i++) {
+ if (adapterAddress == address(withdrawSeq[i])) {
+ indexSeq = i;
+ break;
+ }
+ }
for (uint256 i = 0; i < supportedTokensList.length; i++) {
if (moneyMarkets[index].getSupply(supportedTokensList[i]) >= 10000) revert RemainingFunds(); //ignore the dust
}
moneyMarkets[index] = moneyMarkets[moneyMarketsLength - 1];
moneyMarkets.pop();
- withdrawSeq[index] = withdrawSeq[withdrawSeq.length - 1];
+ withdrawSeq[indexSeq] = withdrawSeq[withdrawSeq.length - 1];
withdrawSeq.pop();
_removeTokenApprovals(adapterAddress);
}
}
Discussion
kingjacob
24 -https://github.com/unioncredit/union-v2-contracts/pull/108
dmitriia
Looks ok given that setWithdrawSequence() is always set duplicate-free withdrawSeq
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 index in moneyMarkets also applies to withdrawSeq, but withdrawSeq is a separate permutation and its indices are independent.
Impact
Withdrawals can become unavailable for an arbitrary time until the withdraw sequence is manually restored, especially if most funds are in the market whose entry was incorrectly removed.
What the attacker needed
Admin calls removeAdapter; withdrawSeq order differs from moneyMarkets
The fix
The fix should find the correct index in withdrawSeq for the adapter being removed and use that index when updating the array.
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.
Smart contracts often keep lists of items. Here, two lists are kept: one of lending markets and one of the order in which to withdraw from them. When removing a market, the code uses the position from the first list to delete from the second list, but the positions do not match. This can break the withdrawal order and stop people from getting their funds.
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.