In the lend() function state updates are made after the callback
The lend() function in TimeswapPair.sol makes an external callback to the caller before updating key state variables such as totalClaims, bonds, insurance and reserves. This violates the checks-effects-interactions pattern and could allow cross-function reentrancy, potentially enabling an attacker to exploit the protocol's composability with other contracts.
Submitted by jayjonah8
In TimeswapPair.sol, the lend() function has a callback to the msg.sender in the middle of the function while there are still updates to state that take place after the callback. The lock modifier guards against reentrancy but not against cross function reentrancy. Since the protocol implements Uniswap like functionality, this can be extremely dangerous especially with regard to composability/interacting with other protocols and contracts. The callback before important state changes (updates to totalClaims bonds, insurance and reserves assets) also violates the Checks Effects Interactions best practices further widening the attack surface.
Proof of Concept
- https://github.com/code-423n4/2022-01-timeswap/blob/main/Timeswap/Timeswap-V1-Core/contracts/TimeswapPair.sol#L246
- https://fravoll.github.io/solidity-patterns/checks_effects_interactions.html
- cross function reentrancy https://medium.com/coinmonks/protect-your-solidity-smart-contracts-from-reentrancy-attacks-9972c3af7c21
Recommended Mitigation Steps
The callback Callback.lend(asset, xIncrease, data); should be placed at the end of the lend() function after all state updates have taken place.
Mathepreneur (Timeswap) confirmed and resolved:
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
Impact
The report claims this could be extremely dangerous for composability and interaction with other protocols, but it does not demonstrate a concrete exploit or loss.
What the attacker needed
Protocol interacts with external contracts
The fix
The callback should be moved to the end of the lend() function after all state updates have taken place; the project confirmed and resolved the issue.
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 can call another contract in the middle of its own work. If it does that before finishing its own record-keeping, the other contract can call back in and see or use outdated information. This report says the lend function does exactly that, which could let someone interfere with the protocol's accounting.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.