Improper adjustment of Lending Ledger configuration
The LendingLedger contract allows governance to adjust reward rates and block time parameters without updating all markets in the same block, causing retroactive application of these changes. This leads to over- or under-estimation of rewards for markets not updated at the exact same block.
0xsomeone Follow with carrotsmuggler
Originally disclosed on Code4rena
Submitted by 0xsomeone, also found by carrotsmuggler
https://github.com/code-423n4/2024-03-neobase/blob/main/src/LendingLedger.sol#L136-L149
https://github.com/code-423n4/2024-03-neobase/blob/main/src/LendingLedger.sol#L173-L185
Description
The LendingLedger utilizes an approximation system (to evaluate the time that has elapsed between two block numbers) which contains adjustable values. Additionally, it permits the governance to adjust the cantoPerBlock value of multiple epochs at any time.
The way these values are adjusted is insecure and thus can cause them to retroactively apply to markets that have not yet been updated.
In the case of the LendingLedger::setBlockTimeParameters, a notice exists within the LendingLedger::update_market function that warns that:
If this ever drifts significantly, the average block time and/or reference block time & number can be updated. However,
update_marketneeds to be called for all markets beforehand.
I do not consider the warning sufficient, as the LendingLedger::setRewards function illustrates that the problem is not understood accurately.
Any market that was not updated on the exact same block that either rewards or the block time parameters are adjusted will have these adjustments retroactively applied, leading to over-estimations or under-estimations of the rewards that should be attributed to the market.
Impact
Reward measurements for markets that were not updated in the exact same block that rewards and/or block-time parameters were re-configured will result in over- or under-estimations, depending on the direction of these configurations.
Severity Rationalization
Administrator mistakes usually fall under QA/Analysis reports; however, in this circumstance, the mistake is not based on input but rather on the state of the contract. Additionally, there are cases whereby the change cannot be performed securely (i.e. if the number of markets introduced would reach the gas limit if all are attempted to be updated at the same block).
Based on the above, I believe this constitutes a valid operational vulnerability that stems from an improperly coded configuration procedure for both rewards and block time parameters.
While there is a warning for the LendingLedger::setBlockTimeParameters function, it is located in an entirely different function and is insufficient, in my opinion. Even if considered sufficient, there is absolutely no warning or indication that the same restriction applies for the LendingLedger::setRewards function.
This submission may be split into two distinct ones if the reward-related and block-time-related impacts are considered distinct; however, I grouped them under the same submission as they pertain to the same operation (update of all markets) being absent albeit from two different code segments.
Proof of Concept
I have coded the following PoC in foundry for completeness. Please add the following segment after LendingLedger.t.sol::setupStateBeforeClaim:
function testInsecureRewardUpdate() public {
setupStateBeforeClaim();
// Based on `LendingLedger.t.sol::testClaimValidLenderOneBlock`, the reward of the `lender` should be `amountPerBlock - 1` at this time point
vm.roll(BLOCK_EPOCH * 5 + 1);
// We update the rewards of the epochs without updating the markets
vm.prank(governance);
uint256 newRewardPerBlock = amountPerBlock * 2;
ledger.setRewards(fromEpoch, toEpoch, newRewardPerBlock);
// We claim the `lender` rewards, should be `amountPerBlock` based on `LendingLedger.t.sol::testClaimValidLenderOneBlock`
uint256 balanceBefore = address(lender).balance;
vm.prank(lender);
vm.roll(BLOCK_EPOCH * 5 + 1);
ledger.claim(lendingMarket);
uint256 balanceAfter = address(lender).balance;
// Assertion will fail
assertEq(balanceAfter - balanceBefore, amountPerBlock - 1);
}
To execute the above test, I recommend the following CLI instruction:
forge test --match-test testInsecureRewardUpdate -vv
Recommended Mitigation Steps
I advise all markets added to a LendingLedger to be tracked and iterated whenever either of the submission’s referenced functions is executed, ensuring that all markets are indeed updated in the same block.
As a gas-optimal alternative, the total markets of the LendingLedger could be tracked as a number. The aforementioned adjustment functions could then accept an input array that would contain all markets, permitting a for loop to iterate them, ensure they are distinct (i.e. in strictly ascending order), and ensure that the total number of input markets is equal to the total number of registered markets.
Alternatively, and as a solution solely for the LendingLedger::setRewards function, the epochs that are mutated could be restricted to be future ones and thus never result in a retroactive application.
Assessed type
Governance
Referring to README:
Publicly Known Issues Mistakes by Governance: We assume that all calls that are performed by the governance address are performed with the correct parameters.
However, it’s not just about correct call parameters but also about correct call timing (
reward-related and block-time-related impacts); therefore, leaving this for sponsor review.The current duplicate #19 is more affected by the README; will reconsider during judging.
OpenCoreCH (Neobase) confirmed
Administrator mistakes usually fall under QA/Analysis reports; however, in this circumstance, the mistake is not based on input but rather on the state of the contract.
I agree with this assessment in the report.
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 contract does not enforce that all markets are updated in the same block when governance changes reward or block time parameters, so the new values retroactively affect unupdated markets.
Impact
Reward measurements for markets not updated in the exact same block as configuration changes will be over- or under-estimated, depending on the direction of the changes.
What the attacker needed
Governance role; multiple markets exist; markets not updated in same block
The fix
The contract should track all markets and update them whenever reward or block time parameters are changed, or restrict reward changes to future epochs only.
How it was found
- Set up the test state before claim.
- Advance the blockchain to a specific block.
- As governance, update the rewards for epochs without updating markets.
- As a lender, claim rewards.
- Observe that the claimed amount differs from the expected amount.
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 lets an admin change reward rates and timing settings. If the admin changes these without also updating every market's record in the same transaction, the new settings apply to past periods too, so users get wrong reward amounts. This is a configuration mistake in the contract's code.
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.