sortVaultsByDelta doesn’t work as expected
The sortVaultsByDelta function initialises its maxDelta and minDelta trackers to zero, so when every delta is positive neither the maximum nor the minimum index is ever recorded and both stay at zero. This can make the contract withdraw from the wrong vaults and revert because one computed index falls outside the valid range.
Submitted by gpersoon, also found by shw
The function sortVaultsByDelta doesn’t always work as expected.
Suppose all the delta’s are positive, and delta1 >= delta2 >= delta3 > 0. Then maxIndex = 0. And (delta < minDelta (==0) ) is never true, so minIndex = 0.
Then (assuming bigFirst==true):
vaultIndexes[0] = maxIndex = 0
vaultIndexes[2] = minIndex = 0
vaultIndexes[1] = N_COINS - maxIndex - minIndex = 3-0-0 = 3
This is clearly not what is wanted, all vaultIndexes should be different and should be in the range [0..2]. This is due to the fact that maxDelta and minDelta are initialized with the value 0. This all could results in withdrawing from the wrong vaults and reverts (because vaultIndexes[1] is out of range).
Exposure.sol L178:
function sortVaultsByDelta(bool bigFirst,uint256 unifiedTotalAssets,uint256[N_COINS] calldata unifiedAssets,uint256[N_COINS] calldata targetPercents) external pure override returns (uint256[N_COINS] memory vaultIndexes) {
uint256 maxIndex;
uint256 minIndex;
int256 maxDelta;
int256 minDelta;
for (uint256 i = 0; i < N_COINS; i++) {
// Get difference between vault current assets and vault target
int256 delta = int256(
unifiedAssets[i] - unifiedTotalAssets.mul(targetPercents[i]).div(PERCENTAGE_DECIMAL_FACTOR)
);
// Establish order
if (delta > maxDelta) {
maxDelta = delta;
maxIndex = i;
} else if (delta < minDelta) {
minDelta = delta;
minIndex = i;
}
}
if (bigFirst) {
vaultIndexes[0] = maxIndex;
vaultIndexes[2] = minIndex;
} else {
vaultIndexes[0] = minIndex;
vaultIndexes[2] = maxIndex;
}
vaultIndexes[1] = N_COINS - maxIndex - minIndex;
}
Recommend the following
-
Initializing
maxDeltaandminDelta:int256 maxDelta = -2**255; // or type(int256).min when using a newer solidity version int256 minDelta = 2**255; // or type(int256).max when using a newer solidity version -
Check that
maxIndexandminIndexare not the same -
require (
maxIndex!=minIndex);
kristian-gro (Gro) confirmed:
Confirmed and Fix has been implemented in release version.
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 maximum and minimum delta trackers are initialised to zero instead of the extreme int256 values, so positive-only or negative-only deltas never update the stored indexes.
Impact
This could result in withdrawing from the wrong vaults and in reverts when the computed index is out of range.
What the attacker needed
All deltas share the same sign; contract calls the sorting helper
The fix
The trackers should be initialised to the extreme int256 values and the code should require that maxIndex and minIndex differ; the project confirmed a fix was implemented in a release version.
How it was found
- Assume all deltas are positive and ordered delta1 >= delta2 >= delta3 > 0.
- Observe that maxIndex stays 0 because no delta exceeds the initial maxDelta of 0.
- Observe that minIndex stays 0 because no delta is below the initial minDelta of 0.
- With bigFirst true, the returned indexes become 0, 3 and 0, which is out of range.
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.
A smart contract keeps money in several vaults and has to pick which one to take from. A helper function is meant to rank the vaults by how far each is from its target amount, but it starts its comparison values at zero, so when all the differences are positive it never notices any of them and returns the same vault twice. The result is that the contract can move funds from the wrong place or crash.
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.