ZivoeYDL::distributeYield() will revert if protocolRecipients recipients length is smaller than residualRecipients
The distributeYield function incorrectly uses the protocol earnings array when emitting an event for residual recipients, causing an out-of-bounds access and revert if the residual recipients list is longer than the protocol recipients list and includes a reward contract. This prevents yield distribution from succeeding.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-03-zivoe-judging/issues/15
Found by
BiasedMerc
Summary
ZivoeYDL::distributeYield incorrectly accesses _protocol[i] instead of _residual[i], and there are no checks anywhere within the code that ensures that both arrays are of the same length. Meaning that it is likely that this will cause distributeYield() to revert in the case where protocolRecipients length is less than residualRecipients.
Vulnerability Detail
ZivoeYDL::updateRecipients allows the timelock contract to update the protocolRecipients or residualRecipients variables:
function updateRecipients(address[] memory recipients, uint256[] memory proportions, bool protocol) external {
require(_msgSender() == IZivoeGlobals_YDL(GBL).TLC(), "ZivoeYDL::updateRecipients() _msgSender() != TLC()");
require(
recipients.length == proportions.length && recipients.length > 0,
"ZivoeYDL::updateRecipients() recipients.length != proportions.length || recipients.length == 0"
);
require(unlocked, "ZivoeYDL::updateRecipients() !unlocked");
... SKIP!...
if (protocol) {
emit UpdatedProtocolRecipients(recipients, proportions);
protocolRecipients = Recipients(recipients, proportions);
}
else {
emit UpdatedResidualRecipients(recipients, proportions);
residualRecipients = Recipients(recipients, proportions);
}
}
It's important to note this function only sets one of those 2 variables at a time, and the length checks on recipients and proportions is to ensure that the Recipients() struct inputs have the same length, NOT to ensure that protocolRecipients and residualRecipients are the same length.
ZivoeYDL::distributeYield calculates protocol earnings and distributes the yield to both the protocolRecipients and residualRecipients. It does so by looping through the recipients and the earnings:
function distributeYield() external nonReentrant {
require(unlocked, "ZivoeYDL::distributeYield() !unlocked");
require(
block.timestamp >= lastDistribution + daysBetweenDistributions * 86400,
"ZivoeYDL::distributeYield() block.timestamp < lastDistribution + daysBetweenDistributions * 86400"
);
// Calculate protocol earnings.
uint256 earnings = IERC20(distributedAsset).balanceOf(address(this));
uint256 protocolEarnings = protocolEarningsRateBIPS * earnings / BIPS;
uint256 postFeeYield = earnings.floorSub(protocolEarnings);
// Update timeline.
distributionCounter += 1;
lastDistribution = block.timestamp;
// Calculate yield distribution (trancheuse = "slicer" in French).
(
uint256[] memory _protocol, uint256 _seniorTranche, uint256 _juniorTranche, uint256[] memory _residual
) = earningsTrancheuse(protocolEarnings, postFeeYield);
... SKIP!...
// Distribute protocol earnings.
for (uint256 i = 0; i < protocolRecipients.recipients.length; i++) {
address _recipient = protocolRecipients.recipients[i];
if (_recipient == IZivoeGlobals_YDL(GBL).stSTT() ||_recipient == IZivoeGlobals_YDL(GBL).stJTT()) {
IERC20(distributedAsset).safeIncreaseAllowance(_recipient, _protocol[i]);
IZivoeRewards_YDL(_recipient).depositReward(distributedAsset, _protocol[i]);
emit YieldDistributedSingle(distributedAsset, _recipient, _protocol[i]);
}
... SKIP!...
// Distribute residual earnings.
for (uint256 i = 0; i < residualRecipients.recipients.length; i++) {
if (_residual[i] > 0) {
address _recipient = residualRecipients.recipients[i];
if (_recipient == IZivoeGlobals_YDL(GBL).stSTT() ||_recipient == IZivoeGlobals_YDL(GBL).stJTT()) {
IERC20(distributedAsset).safeIncreaseAllowance(_recipient, _residual[i]);
IZivoeRewards_YDL(_recipient).depositReward(distributedAsset, _residual[i]);
emit YieldDistributedSingle(distributedAsset, _recipient, _protocol[i]);
}
The function loops over protocolRecipients and residualRecipients seperately and within each loop indexed positions are accessed in _protocol[] and _residual[], these 2 arrays are initated as follows:
function earningsTrancheuse(uint256 yP, uint256 yD) public view returns (
uint256[] memory protocol, uint256 senior, uint256 junior, uint256[] memory residual
) {
protocol = new uint256[](protocolRecipients.recipients.length);
residual = new uint256[](residualRecipients.recipients.length);
Their lengths will depend on the lengths of protocolRecipients and residualRecipients respectively, meaning their lengths can differ.
Finally, the 2nd for loop in distributeYield itterates over residualRecipients and
incorrectly emits the YieldDistributedSingle event by accessing _protocol[i], whilst it should be using _residual[i]. This will cause the function to revert due to an out of bound index access if:
residualRecipients.recipients.length > protocolRecipients.recipients.length
AND
atleast one of the recipients in residualRecipients.recipients is:
IZivoeGlobals_YDL(GBL).stSTT() || IZivoeGlobals_YDL(GBL).stJTT()
which are the reward contracts: ZivoeYDL.sol#L20-L24
/// @notice Returns the address of the ZivoeRewards ($zSTT) contract.
function stSTT() external view returns (address);
/// @notice Returns the address of the ZivoeRewards ($zJTT) contract.
function stJTT() external view returns (address);
meaning yield will not be able to be successfully distributed and the function will always revert as long as this condition is met.
Impact
Core functionality of ZivoeYDL will be unaccessible due to the function always reverting as long as the above condition is met, and this condition can easily be met through normal protocol use.
This can be fixed by ensuring that both array are of the same length, however this means that appropriate fees cannot be distributed to the correct parties.
Code Snippet
ZivoeYDL.sol#L392-L416 ZivoeYDL.sol#L213-L286 ZivoeYDL.sol#L447-L451 ZivoeYDL.sol#L20-L24
Tool used
Manual Review
Recommendation
Change the incorrect emit to access from _residual[i] rather than _protocol[i]:
ZivoeYDL.sol#L286
- emit YieldDistributedSingle(distributedAsset, _recipient, _protocol[i]);
+ emit YieldDistributedSingle(distributedAsset, _recipient, _residual[i]);
Discussion
pseudonaut
Valid
sherlock-admin3
1 comment(s) were left on this issue during the judging contest.
panprog commented:
medium, yield distribution always reverts in some cases if protocols recepients < residual recepients
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/Zivoe/zivoe-core-foundry/pull/253
sherlock-admin2
The Lead Senior Watson signed off on the fix.
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 developer used the wrong array variable (_protocol instead of _residual) in an event emission within a loop over residualRecipients, without ensuring the arrays have equal length.
Impact
Yield distribution could always revert when the residual recipients list is longer than the protocol recipients list and contains a reward contract, making core functionality inaccessible.
What the attacker needed
ResidualRecipients length > protocolRecipients length; at least one residual recipient is stSTT or stJTT
The fix
The incorrect event emission was fixed to use _residual[i] instead of _protocol[i].
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, arrays have a fixed length, and accessing an index beyond that length causes an error. Here, the code mistakenly uses the wrong array when logging an event, so if one list is longer than the other, the function fails and yield cannot be distributed.
What is DoS?
A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.