Size uses wrong source to query available liquidity on Aave, resulting in borrow and lend operations being bricked upon mainnet deployment
Size's liquidity check queries the Aave pool contract's balance instead of the aToken contract's balance, so on a live Aave deployment the check always fails because the pool holds no liquidity. This causes borrow and lend operations to revert, bricking the protocol's main functions.
Submitted by JCN, also found by mt030d, 0xpiken, silver_eth, grearlake, elhaj, zarkk01, trachev, ilchovski, bin2chen, VAD37, and SpicyMeatball
Size queries the underlyingBorrowToken balance of the variablePool during borrowing and lending actions in order to check available liquidity. If there is not enough available liquidity for the new borrower to withdraw the borrowed assets, the function call will revert:
178: function buyCreditMarket(BuyCreditMarketParams calldata params) external payable override(ISize) whenNotPaused {
...
184: state.validateVariablePoolHasEnoughLiquidity(amount); // @audit: liquidity check
188: function sellCreditMarket(SellCreditMarketParams memory params) external payable override(ISize) whenNotPaused {
...
194: state.validateVariablePoolHasEnoughLiquidity(amount); // @audit: liquidity check
CapsLibrary::validateVariablePoolHasEnoughLiquidity
67: function validateVariablePoolHasEnoughLiquidity(State storage state, uint256 amount) public view {
68: uint256 liquidity = state.data.underlyingBorrowToken.balanceOf(address(state.data.variablePool)); // @audit: ATokens hold liquidity, not variablePool
69: if (liquidity < amount) {
70: revert Errors.NOT_ENOUGH_BORROW_ATOKEN_LIQUIDITY(liquidity, amount);
71: }
However, Aave actual holds all liquidity in the AToken contracts, not the Pool contracts. Therefore, the liquidity check above will always fail when the variablePool references a live instance of an actual Aave Pool. This is due to the fact that the variablePool balance will be 0 (does not hold any liquidity), causing borrowing and lending actions to revert on line 70 in CapsLibrary.sol.
The below code snippets are taken from out of scope contracts, but are shown to further explain why the test suite did not detect this vulnerability:
52: function executeSupply(
...
67: IERC20(params.asset).safeTransferFrom(msg.sender, reserveCache.aTokenAddress, params.amount);
As shown above, the actual implementation of the Aave pool will transfer supplied assets to the aTokenAddress on supplies. The Aave pool implementation will then transfer the assets from the AToken to the recipient during withdraws, since the liquidity is stored in the AToken contract:
106: function executeWithdraw(
...
139: IAToken(reserveCache.aTokenAddress).burn(
140: msg.sender,
141: params.to,
142: amountToWithdraw,
143: reserveCache.nextLiquidityIndex
144: );
96: function burn(
97: address from,
98: address receiverOfUnderlying,
99: uint256 amount,
100: uint256 index
101: ) external virtual override onlyPool {
102: _burnScaled(from, receiverOfUnderlying, amount, index);
103: if (receiverOfUnderlying != address(this)) {
104: IERC20(_underlyingAsset).safeTransfer(receiverOfUnderlying, amount);
However, Size uses a PoolMock contract for their test suite, which implements logic that differs from actual Aave Pool contracts:
67: function supply(address asset, uint256 amount, address onBehalfOf, uint16) external {
68: Data memory data = datas[asset];
69: IERC20Metadata(asset).transferFrom(msg.sender, address(this), amount); // @audit: underlying transferred from supplier to PoolMock contract
70: data.aToken.mint(address(this), onBehalfOf, amount, data.reserveIndex);
71: }
72:
73: function withdraw(address asset, uint256 amount, address to) external returns (uint256) {
74: Data memory data = datas[asset];
75: data.aToken.burn(msg.sender, address(data.aToken), amount, data.reserveIndex); // @audit: no assets transferred from AToken
76: IERC20Metadata(asset).safeTransfer(to, amount); // @audit: underlying transferred from PoolMock contract to supplier
77: return amount;
78: }
As shown above, the PoolMock contract transfers all supplied assets to the PoolMock contract itself, not the AToken contract. This differs from how actual Aave Pools handle liquidity on live networks.
Impact
Borrow (sellCreditMarket) and lend (buyCreditMarket) operations will be bricked when Size is deployed and integrates with Aave on a live network. This report is labeled as High due to the fact that the main functionalities of this lending protocol will be unusable upon deployment.
Proof of Concept
The following PoC simply showcases the fact that the AToken does not hold the liquidity in the test suite. See here to observe that ATokens are the contracts that hold liquidity for Aave in live networks.
Modify the file below and run with forge test --mc BuyCreditLimitTest --mt test_BuyCreditLimit_buyCreditLimit_adds_loanOffer_to_orderbook:
diff --git a/./test/local/actions/BuyCreditLimit.t.sol b/./test/local/actions/BuyCreditLimit.t.sol
index a8c6317..66dd63e 100644
--- a/./test/local/actions/BuyCreditLimit.t.sol
+++ b/./test/local/actions/BuyCreditLimit.t.sol
@@ -23,6 +23,9 @@ contract BuyCreditLimitTest is BaseTest {
assertTrue(_state().alice.user.loanOffer.isNull());
_buyCreditLimit(alice, block.timestamp + 12 days, YieldCurveHelper.pointCurve(12 days, 1.01e18));
assertTrue(!_state().alice.user.loanOffer.isNull());
+ address aToken = variablePool.getReserveData(address(usdc)).aTokenAddress;
+ assertEq(usdc.balanceOf(address(variablePool)), 100e6);
+ assertEq(usdc.balanceOf(aToken), 0);
}
Recommended Mitigation
When checking available liquidity on Aave, Size should query the underlyingBorrowToken balance of the AToken instead of the variablePool:
diff --git a/./src/libraries/CapsLibrary.sol b/./src/libraries/CapsLibrary.sol
index 7e35e90..b7ce7a1 100644
--- a/./src/libraries/CapsLibrary.sol
+++ b/./src/libraries/CapsLibrary.sol
@@ -65,9 +65,11 @@ library CapsLibrary {
/// @param state The state struct
/// @param amount The amount of cash to withdraw
function validateVariablePoolHasEnoughLiquidity(State storage state, uint256 amount) public view {
- uint256 liquidity = state.data.underlyingBorrowToken.balanceOf(address(state.data.variablePool));
+ address aToken = state.data.variablePool.getReserveData(address(state.data.underlyingBorrowToken)).aTokenAddress;
+ uint256 liquidity = state.data.underlyingBorrowToken.balanceOf(aToken);
if (liquidity < amount) {
revert Errors.NOT_ENOUGH_BORROW_ATOKEN_LIQUIDITY(liquidity, amount);
}
}
}
aviggiano (Size) confirmed and commented:
Fixed in https://github.com/SizeCredit/size-solidity/pull/127
samuraii77 (warden) commented:
Hi, the severity of this issue should be a medium. Please take a look at what a high severity issue should be:
Assets can be stolen/lost/compromised directly (or indirectly if there is a valid attack path that does not have hand-wavy hypotheticals).
This issue does not cause such an issue. In fact, it can be completely mitigated by redeploying the contract or upgrading the contract (if it is upgradeable, I am not sure if it was) whenever the issue is noticed which would be extremely quickly as it would occur on the first deposit of USDC essentially causing nothing else than some wasted time and headache.
hansfriese (judge) decreased severity to Medium and commented:
I agree. Medium is more appropriate as there is no direct fund loss.
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 code assumes the Aave pool contract holds the underlying liquidity, but in real Aave deployments the liquidity is held by the aToken contract.
Impact
Borrow and lend operations would be bricked when Size is deployed on a live network with Aave, though no funds are directly lost.
What the attacker needed
Size deployed with a live Aave pool
The fix
The check should query the aToken's balance of the underlying token instead of the pool's balance; the report says it was fixed in a pull request.
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 lending protocol called Size checks how much money is available in Aave before letting users borrow or lend. It looks in the wrong place: it checks the Aave pool contract, but Aave actually keeps the money in a separate token contract called aToken. So the check always sees zero and blocks every borrow and lend.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.