lack of access control in LendingLedger.sol#checkpoint_lender and function checkpoint_market
The checkpoint_market and checkpoint_lender functions lacked access control and allowed anyone to set the stored epoch to a past value, even zero. An attacker could call them with a zero timestamp to prevent future checkpoints from running, causing users' rewards to be lost and state updates to fail.
Note: This audit was preceded by a Code4rena Test Coverage competition. While auditing was not the purpose of the testing phase, relevant and valuable findings reported during that phase were eligible to be judged. This finding [H-07] was discovered during that period and is being included here for completeness.
User’s claim and sync_lender will be griefed at low cost.
Proof of Concept
/// @notice Trigger a checkpoint explicitly.
/// Never needs to be called explicitly, but could be used to ensure the checkpoints within the other functions consume less gas (because they need to forward less epochs)
/// @param _market Address of the market
/// @param _forwardTimestampLimit Until which epoch (provided as timestamp) should the update be applied. If it is higher than the current epoch timestamp, this will be used.
function checkpoint_market(
address _market,
uint256 _forwardTimestampLimit
) external is_valid_epoch(_forwardTimestampLimit) {
require(
lendingMarketTotalBalanceEpoch[_market] > 0,
"No deposits for this market"
);
_checkpoint_market(_market, _forwardTimestampLimit);
}
/// @param _market Address of the market
/// @param _lender Address of the lender
/// @param _forwardTimestampLimit Until which epoch (provided as timestamp) should the update be applied. If it is higher than the current epoch timestamp, this will be used.
function checkpoint_lender(
address _market,
address _lender,
uint256 _forwardTimestampLimit
) external is_valid_epoch(_forwardTimestampLimit) {
require(
lendingMarketBalancesEpoch[_market][_lender] > 0,
"No deposits for this lender in this market"
);
_checkpoint_lender(_market, _lender, _forwardTimestampLimit);
}
These two functions lack access control, the caller is never validated, meaning anyone can call this function.
The market is not validated to see if the market is whitelisted or not.
The timestamp is never validated, the is_valid_epoch(_forwardTimestampLimit) is insufficient.
/// @notice Check that a provided timestamp is a valid epoch (divisible by WEEK) or infinity
/// @param _timestamp Timestamp to check
modifier is_valid_epoch(uint256 _timestamp) {
require(
_timestamp % WEEK == 0 || _timestamp == type(uint256).max,
"Invalid timestamp"
);
_;
}
The user can just pick a past timestamp as the _forwardTimestampLimit.
For example, if we set _forwardTimestampLimit to 0.
Then for example in _checkpoint_market
function _checkpoint_market(
address _market,
uint256 _forwardTimestampLimit
) private {
uint256 currEpoch = (block.timestamp / WEEK) * WEEK;
uint256 lastMarketUpdateEpoch = lendingMarketTotalBalanceEpoch[_market];
uint256 updateUntilEpoch = Math.min(currEpoch, _forwardTimestampLimit);
if (lastMarketUpdateEpoch > 0 && lastMarketUpdateEpoch < currEpoch) {
// Fill in potential gaps in the market total balances history
uint256 lastMarketBalance = lendingMarketTotalBalance[_market][
lastMarketUpdateEpoch
];
for (
uint256 i = lastMarketUpdateEpoch;
i <= updateUntilEpoch;
i += WEEK
) {
lendingMarketTotalBalance[_market][i] = lastMarketBalance;
}
}
lendingMarketTotalBalanceEpoch[_market] = updateUntilEpoch;
}
We set the lendingMarketTotalBalanceEpoch[_market] to 0.
Then if the next call of the _checkpoint_market, the for loop would never run because the lastMarketUpdateEpoch is 0.
Over time, even when the for loop inside _checkpoint_market does run, the caller are forced to pay very high gas fee.
Same issue applies to _checkpoint_lender as well.
User can decrease lendingMarketBalancesEpoch, even to 0.
Basically, if a malicious actor call these two function with forwardTimestampLimit 0.
Then the _checkpoint_lender and _checkpoint*market would never run inside sync*ledger and claim reward.
Because user’s reward can be griefed to 0 and stated are failed to updated properly.
This was a valid issue before the auditing contest (uncovered during the testing contest and fixed before the auditing contest), pasting my comment from there for reference:
Good point. It is generally intended that everyone can call these functions (should not be necessary in practice, but may be in some edge cases where a market was inactive for years) and I do not think that this is problematic per se. However, the problem here is that users can decrease lendingMarketBalancesEpoch or lendingMarketTotalBalanceEpoch, which should never happen. So I will probably change the code like this (and the same for lenders) such that this can never happen:
if (lastMarketUpdateEpoch > 0 && lastMarketUpdateEpoch < currEpoch) {
// Fill in potential gaps in the market total balances history
uint256 lastMarketBalance = lendingMarketTotalBalance[_market][lastMarketUpdateEpoch];
for (uint256 i = lastMarketUpdateEpoch; i <= updateUntilEpoch; i += WEEK) {
lendingMarketTotalBalance[_market][i] = lastMarketBalance;
}
if (updateUntilEpoch > lastMarketUpdateEpoch) {
lendingMarketTotalBalanceEpoch[_market] = updateUntilEpoch;
}
}
The original report as published on Code4rena, by ladboy233. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
How it worked
Root cause
The functions did not restrict who could call them and did not prevent the stored epoch from being moved backwards, so a caller could set it to zero and break future checkpoint loops.
Impact
A malicious actor could grief users' rewards to zero and prevent state from updating properly, at low cost.
What the attacker needed
None
The fix
The code was fixed before the audit by only updating the stored epoch when the new value is greater than the last one.
How it was found
Set up rewards and whitelist a lending market.
Call sync_ledger for a lender to create an initial checkpoint.
Advance time and call sync_ledger again to create a gap.
Call checkpoint_market and checkpoint_lender with a forward timestamp limit of zero.
Call sync_ledger again and then claim; the reward is zero instead of 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.
Generating the AI write-up for this report.
This usually takes about 30 seconds. You can read the original meanwhile; this page updates itself.
Writing the technical write-up
Writing the version for beginners
Checking every passage against the original
These functions update a record of how much was deposited over time. Because anyone could call them and choose an old date, an attacker could reset that record to zero. After that, the system skipped the updates that calculate rewards, so users got nothing.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.