Sometimes calculateBorrowerReward and calculateSupplierReward return incorrect results
Submitted by volodya
Sometimes calculateBorrowerReward and calculateSupplierReward return incorrect results.
Proof of Concept
Whenever a user wants to know pending rewards they call getPendingRewards; sometimes, it returns incorrect results.
There is a bug inside calculateBorrowerReward and calculateSupplierReward
function calculateBorrowerReward(
address vToken,
RewardsDistributor rewardsDistributor,
address borrower,
RewardTokenState memory borrowState,
Exp memory marketBorrowIndex
) internal view returns (uint256) {
Double memory borrowIndex = Double({ mantissa: borrowState.index });
Double memory borrowerIndex = Double({
mantissa: rewardsDistributor.rewardTokenBorrowerIndex(vToken, borrower)
});
// @audit
// if (borrowerIndex.mantissa == 0 && borrowIndex.mantissa >= rewardsDistributor.rewardTokenInitialIndex()) {
if (borrowerIndex.mantissa == 0 && borrowIndex.mantissa > 0) {
// Covers the case where users borrowed tokens before the market's borrow state index was set
borrowerIndex.mantissa = rewardsDistributor.rewardTokenInitialIndex();
}
Double memory deltaIndex = sub_(borrowIndex, borrowerIndex);
uint256 borrowerAmount = div_(VToken(vToken).borrowBalanceStored(borrower), marketBorrowIndex);
uint256 borrowerDelta = mul_(borrowerAmount, deltaIndex);
return borrowerDelta;
}
contracts/Lens/PoolLens.sol#L495
function calculateSupplierReward(
address vToken,
RewardsDistributor rewardsDistributor,
address supplier,
RewardTokenState memory supplyState
) internal view returns (uint256) {
Double memory supplyIndex = Double({ mantissa: supplyState.index });
Double memory supplierIndex = Double({
mantissa: rewardsDistributor.rewardTokenSupplierIndex(vToken, supplier)
});
// @audit
// if (supplierIndex.mantissa == 0 && supplyIndex.mantissa >= rewardsDistributor.rewardTokenInitialIndex()) {
if (supplierIndex.mantissa == 0 && supplyIndex.mantissa > 0) {
// Covers the case where users supplied tokens before the market's supply state index was set
supplierIndex.mantissa = rewardsDistributor.rewardTokenInitialIndex();
}
Double memory deltaIndex = sub_(supplyIndex, supplierIndex);
uint256 supplierTokens = VToken(vToken).balanceOf(supplier);
uint256 supplierDelta = mul_(supplierTokens, deltaIndex);
return supplierDelta;
}
contracts/Lens/PoolLens.sol#L516
Inside rewardsDistributor original functions are written like this:
function _distributeSupplierRewardToken(address vToken, address supplier) internal {
...
if (supplierIndex == 0 && supplyIndex >= rewardTokenInitialIndex) {
// Covers the case where users supplied tokens before the market's supply state index was set.
// Rewards the user with REWARD TOKEN accrued from the start of when supplier rewards were first
// set for the market.
supplierIndex = rewardTokenInitialIndex;
}
...
}
contracts/Rewards/RewardsDistributor.sol#L340
function _distributeBorrowerRewardToken(
address vToken,
address borrower,
Exp memory marketBorrowIndex
) internal {
...
if (borrowerIndex == 0 && borrowIndex >= rewardTokenInitialIndex) {
// Covers the case where users borrowed tokens before the market's borrow state index was set.
// Rewards the user with REWARD TOKEN accrued from the start of when borrower rewards were first
// set for the market.
borrowerIndex = rewardTokenInitialIndex;
}
...
}
Rewards/RewardsDistributor.sol#L374
Recommended Mitigation Steps
function calculateSupplierReward(
address vToken,
RewardsDistributor rewardsDistributor,
address supplier,
RewardTokenState memory supplyState
) internal view returns (uint256) {
Double memory supplyIndex = Double({ mantissa: supplyState.index });
Double memory supplierIndex = Double({
mantissa: rewardsDistributor.rewardTokenSupplierIndex(vToken, supplier)
});
- if (supplierIndex.mantissa == 0 && supplyIndex.mantissa > 0) {
+ if (supplierIndex.mantissa == 0 && supplyIndex.mantissa >= rewardsDistributor.rewardTokenInitialIndex()) {
// Covers the case where users supplied tokens before the market's supply state index was set
supplierIndex.mantissa = rewardsDistributor.rewardTokenInitialIndex();
}
Double memory deltaIndex = sub_(supplyIndex, supplierIndex);
uint256 supplierTokens = VToken(vToken).balanceOf(supplier);
uint256 supplierDelta = mul_(supplierTokens, deltaIndex);
return supplierDelta;
}
function calculateBorrowerReward(
address vToken,
RewardsDistributor rewardsDistributor,
address borrower,
RewardTokenState memory borrowState,
Exp memory marketBorrowIndex
) internal view returns (uint256) {
Double memory borrowIndex = Double({ mantissa: borrowState.index });
Double memory borrowerIndex = Double({
mantissa: rewardsDistributor.rewardTokenBorrowerIndex(vToken, borrower)
});
- if (borrowerIndex.mantissa == 0 && borrowIndex.mantissa > 0) {
+ if (borrowerIndex.mantissa == 0 && borrowIndex.mantissa >= rewardsDistributor.rewardTokenInitialIndex()) {
// Covers the case where users borrowed tokens before the market's borrow state index was set
borrowerIndex.mantissa = rewardsDistributor.rewardTokenInitialIndex();
}
Double memory deltaIndex = sub_(borrowIndex, borrowerIndex);
uint256 borrowerAmount = div_(VToken(vToken).borrowBalanceStored(borrower), marketBorrowIndex);
uint256 borrowerDelta = mul_(borrowerAmount, deltaIndex);
return borrowerDelta;
}
Assessed type
Invalid Validation
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A medium-severity business logic bug in Venus Prime. Code4rena made it public on 9 August 2023. Found in the 2023-05-venus audit contest (finding M-16).
About Business logic
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
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.