Non view function is called with staticcall in CErc20Delegator
The CErc20Delegator contract uses a staticcall (delegateToViewImplementation) to call the non-view functions borrowRatePerBlock and supplyRatePerBlock, which now update state. After enough blocks have passed since the last update, these calls revert, making the functions unusable through the delegator.
Submitted by zzzitron
Lines of code
https://github.com/Plex-Engineer/lending-market-v2/blob/443a8c0fed3c5018e95f3881a31b81a555c42b2d/contracts/CErc20Delegator.sol#L237 https://github.com/Plex-Engineer/lending-market-v2/blob/443a8c0fed3c5018e95f3881a31b81a555c42b2d/contracts/CErc20Delegator.sol#L246
When using CToken implementation with CErc20Delegator, the functions borrowRatePerBlock and supplyRatePerBlock will revert when the underlying functions try to update some states.
Detail
The v1 of borrowRatePerBlock and supplyRatePerBlock were view functions, but they are not anymore. The CErc20Delegator is still using delegateToViewImplementation for those functions. Those functions can be used, as long as the implementation does not update any state variables, i.e. the block number increase since the last update is less or equal to the updateFrequency. However, when these functions are called after sufficient blocks are mined, they are going to revert.
Although one can still call the implementation using delegateToImplementation, it is not a good usability, especially if those functions are used for external user interface.
Proof Of Concept
The gist shows a simple test. It calls borrowRatePerBlock and supplyRatePerBlock first time, it suceeds. Then, it mines for more than 300 times, which is the updateFrequency parameter. Then it calls again then fails.
Notes on the test file:
-
The setup is taken from
tests/Treasury/Accountant.test.ts -
using
solidityfrom ethereum-waffle for chai to usereverted// in hardhat.config.js import chai from “chai”; import { solidity } from “ethereum-waffle”;chai.use(solidity);
Tools Used
hardhat
Recommended Mitigation Steps
Instead of using delegateToViewImplementation use delegateToImplementation. Alternatively, implement view functions to query these rates in NoteInterest.sol and CToken.sol. It will enable to query the rates without spending gas.
Alex the Entreprenerd (judge) commented:
The warden has shown how, due to the usage of
delegateToViewImplementationthe function call toborrowRatePerBlockandsupplyRatePerBlockwill revert, provided some time has passed.There is no more severe loss of availability than a function that doesn’t work, however, at this time, because the functions are
viewand “niche”, the actual underlying functionality is still available viadelegateToViewImplementation, and the internal accounting of the protocol is still functioning, I believe Medium Severity to be appropriate.
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
The delegator uses delegateToViewImplementation (a staticcall) for functions that are no longer view and need to update state, causing a revert when state changes are attempted.
Impact
The functions borrowRatePerBlock and supplyRatePerBlock become unusable through the delegator after sufficient blocks have passed, causing a loss of availability for external interfaces, though the underlying functionality remains accessible via delegateToImplementation.
What the attacker needed
Sufficient blocks mined since last update
The fix
The fix is to use delegateToImplementation instead of delegateToViewImplementation, or to implement separate view functions for querying the rates.
How it was found
- Call borrowRatePerBlock or supplyRatePerBlock through the delegator; it succeeds if no state update is needed.
- Mine more than 300 blocks (the updateFrequency parameter).
- Call the same functions again; they revert because they try to update state.
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 has functions that can be called in two ways: one that only reads data (view) and one that can change data. The delegator contract was set up to use the read-only way for two functions that now need to change data. So when those functions try to update something, the call fails and the function stops working.
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.