Skip to content
Audit finding Advanced

borrow() function has state updates after a callback to msg.sender

The borrow() function in TimeswapPair.sol makes an external callback to msg.sender before completing state updates, violating the checks-effects-interactions pattern. This could enable cross-function reentrancy attacks, potentially allowing an attacker to manipulate collateral, debt, or reserves.

jayjonah8 Follow

Originally disclosed on Code4rena

Submitted by jayjonah8

In TimeswapPair.sol, the borrow() 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 collateral, totalDebtCreated and reserves assets) also violates the Checks Effects Interactions best practices further widening the attack surface.

Proof of Concept

Recommended Mitigation Steps

The callback Callback.borrow(collateral, dueOut.collateral, data); should be placed at the end of the borrow() function after all state updates have taken place.

Mathepreneur (Timeswap) confirmed and resolved:

https://github.com/Timeswap-Labs/Timeswap-V1-Core/pull/105

The original report as published on Code4rena, by jayjonah8. 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.

How it worked

Impact

This could enable cross-function reentrancy attacks, which may be extremely dangerous especially with regard to composability and interaction with other protocols and contracts.

What the attacker needed

None

The fix

The callback should be moved to the end of the borrow() 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 function calls an outside contract before it finishes updating its own records. That outside contract can call back into the first contract and see or change things while they are half-updated, which can be abused to steal funds or break the protocol.

More by jayjonah8

View all

18 more disclosed reports, best first.

More from Timeswap contest

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.