Lender.sol: Incorrect rewards accounting for RESERVE address in _transfer function
The Lender contract's _transfer function does not accrue interest before updating reward state, so the RESERVE address's share balance is stale and it loses out on some rewards. An attacker cannot directly exploit this, but the RESERVE address receives less reward than it should.
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2023-10-aloe-judging/issues/49
Found by
roguereddwarf
The RESERVE address is a special address in the Lender since it earns some of the interest that the Lender accrues.
According to the contest README, which links to the auditor quick start guide, the RESERVE address should behave normally, i.e. all accounting should be done correctly for it:
Special-cases related to the RESERVE address and couriers
We believe the RESERVE address can operate without restriction, i.e. it can call any function in the protocol without causing accounting errors. Where it needs to be limited, we believe it is. For example, Lender.deposit prevents it from having a courier. But are we missing anything? Many of our invariants in LenderHarness have special logic to account for the RESERVE address, and while we think everything is correct, we'd like to have more eyes on it.
The issue is that the Lender._transfer function, which contains the logic for share transfers, does not accrue interest.
Thereby the RESERVE's share balance is not up-to-date and it loses out on any rewards that should be earned for the accrued balance.
For all other addresses the reward accounting is performed correctly in the Lender._transfer function and according to the auditor quick start guide it is required that the same is true for the RESERVE address.
Vulnerability Detail
When interest is accrued, the RESERVE address gets minted shares.
However the Lender._transfer function does not accrue interest and so RESERVE's balance is not up to date which means the Rewards.updateUserState call operates on an incorrect balance.
The balance of RESERVE is too low which results in a loss of rewards.
Impact
As described above, the RESERVE address should have its reward accounting done correctly just like all other addresses.
Failing to do so means that the RESERVE misses out on some rewards because Lender._transfer does not update the share balance correctly and so the rewards will be accrued on a balance that is too low.
Code Snippet
Tool used
Manual Review
Recommendation
The RESERVE address should be special-cased in the Lender._transfer function. Thereby gas is saved when transfers are executed that do not involve the RESERVE address and the reward accounting is done correctly for when the RESERVE address is involved.
When the RESERVE address is involved, (Cache memory cache, ) = _load(); and _save(cache, /* didChangeBorrowBase: */ false); must be called. Also the Rewards state must be updated with this call: Rewards.updatePoolState(s, a, newTotalSupply);.
Discussion
sherlock-admin2
2 comment(s) were left on this issue during the judging contest.
panprog commented:
low, because the reward amount lost is very small and can be worked around by manually accuringInterest before transferring
MohammedRizwan commented:
valid
panprog
Escalate
This should be low, not medium.
While it's valid, the reward lost by RESERVE is very small, and developers do not guarantee exact calculations, according to https://docs.aloe.capital/aloe-ii/auditor-quick-start:
The rewards logic is intentionally approximate, i.e. we do not care about 18 decimal precision. However, it should be somewhat accurate, and if it's wrong, it should always underestimate rewards rather than pay out too much.
In this case, RESERVE receives slightly smaller reward than theoretical calculations, but this is in-line with the protocol design in that rewards don't have to be exactly correct and can be slightly smaller.
sherlock-admin2
Escalate
This should be low, not medium.
While it's valid, the reward lost by RESERVE is very small, and developers do not guarantee exact calculations, according to https://docs.aloe.capital/aloe-ii/auditor-quick-start:
The rewards logic is intentionally approximate, i.e. we do not care about 18 decimal precision. However, it should be somewhat accurate, and if it's wrong, it should always underestimate rewards rather than pay out too much.
In this case, RESERVE receives slightly smaller reward than theoretical calculations, but this is in-line with the protocol design in that rewards don't have to be exactly correct and can be slightly smaller.
You've created a valid escalation!
To remove the escalation from consideration: Delete your comment.
You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.
roguereddwarf
The auditor quick start guide says that RESERVE should behave like any other address, i.e. transfers with the RESERVE address do not update rewards correctly. For all other addresses it is done correctly.
panprog
@roguereddwarf Yes, it's incorrect. But my point is that the loss is really very small and falls within the developers intentions to award rewards not exactly in correct amounts and slightly less if error does occur. Reserve owner can also overcome this problem by manually accuring before doing transfer. So this is more low than medium.
cvetanovv
It's really on the Medium/Low borderline, but the protocol is confirmed to be Medium severity
Trumpero
Planning to reject the escalation and keep this issue as medium.
"Manually accruing before doing the transfer" is not an intended flow for the reserve owner, so it isn't considered a valid solution. RESERVE account can lose rewards if the owner isn't aware of it and transfers normally.
Czar102
Result: Medium Unique
sherlock-admin2
Escalations have been resolved successfully!
Escalation status:
- panprog: rejected
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 _transfer function does not call _load and _save to accrue interest before updating the reward state, so the RESERVE address's share balance is not up to date when rewards are calculated.
Impact
The RESERVE address misses out on some rewards because its share balance is too low when rewards are accrued; the loss is small and can be worked around by manually accruing interest before transferring.
What the attacker needed
RESERVE address involved in a transfer
The fix
The RESERVE address should be special-cased in _transfer to call _load, _save, and Rewards.updatePoolState so that reward accounting is correct.
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 this lending protocol, a special address called RESERVE earns interest like a normal user. When shares are transferred, the code should first update interest so the reward calculation uses the correct balance. For RESERVE, this update is skipped, so its balance is too low and it earns slightly less reward than it should.
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.