Remaining collateral used by ERC721Pool is missed in Auctions take and bucketTake return structures
In the ERC721 pool, the auction take and bucketTake functions never set the remaining collateral field in their result structures, so it stays zero. When an auction settles, the rebalancing logic then strips every collateral token id from the borrower and moves them into the pool's claimable list, where they are not tracked by any bucket and become permanently frozen.
Source: https://github.com/sherlock-audit/2023-01-ajna-judging/issues/103
Found by
hyh
Summary
ERC721Pool's take() and bucketTake() use remaining collateral variable to adjust borrower's collateral ids array after their debt is settled in an auction. In both cases this variable isn't initialized in Auctions's take() and bucketTake() and all collateral of the borrower ends up being frozen in the pool.
Vulnerability Detail
Being run with uninitialized zero result.remainingCollateral, _rebalanceTokens() will remove all the collateral ids from the borrower and place them into bucketTokenIds, i.e. move all borrowers collateral to the LP's cumulative collateral account. Those funds will be permanently frozen as other accounting parts will not have them recorded in any bucket, so no LP be able to withdraw extra funds.
Impact
Borrower's collateral funds will be permanently frozen in full whenever take() and bucketTake() result in auction settlement.
This takes place as after those ids was removed from borrowerTokenIds any operation of the borrower that should result in collateral being returned to them will be reverted instead.
Code Snippet
While being used in _rebalanceTokens(borrowerAddress_, result.remainingCollateral) call, the result.remainingCollateral isn't initialized and is always zero:
https://github.com/sherlock-audit/2023-01-ajna/blob/main/contracts/src/ERC721Pool.sol#L405-L460
function take(
address borrowerAddress_,
uint256 collateral_,
address callee_,
bytes calldata data_
) external override nonReentrant {
PoolState memory poolState = _accruePoolInterest();
TakeResult memory result = Auctions.take(
auctions,
buckets,
deposits,
loans,
poolState,
borrowerAddress_,
Maths.wad(collateral_),
1
);
// update pool balances state
uint256 t0PoolDebt = poolBalances.t0Debt;
uint256 t0DebtInAuction = poolBalances.t0DebtInAuction;
if (result.t0DebtPenalty != 0) {
t0PoolDebt += result.t0DebtPenalty;
t0DebtInAuction += result.t0DebtPenalty;
}
t0PoolDebt -= result.t0RepayAmount;
t0DebtInAuction -= result.t0DebtInAuctionChange;
poolBalances.t0Debt = t0PoolDebt;
poolBalances.t0DebtInAuction = t0DebtInAuction;
poolBalances.pledgedCollateral -= result.collateralAmount;
// update pool interest rate state
poolState.debt = result.poolDebt;
poolState.collateral -= result.collateralAmount;
_updateInterestState(poolState, result.newLup);
// transfer rounded collateral from pool to taker
uint256[] memory tokensTaken = _transferFromPoolToAddress(
callee_,
borrowerTokenIds[borrowerAddress_],
result.collateralAmount / 1e18
);
if (data_.length != 0) {
IERC721Taker(callee_).atomicSwapCallback(
tokensTaken,
result.quoteTokenAmount / _getArgUint256(QUOTE_SCALE),
data_
);
}
if (result.settledAuction) _rebalanceTokens(borrowerAddress_, result.remainingCollateral);
The reason is take doesn't fill result.remainingCollateral = borrower.collateral in the end of function:
function take(
AuctionsState storage auctions_,
mapping(uint256 => Bucket) storage buckets_,
DepositsState storage deposits_,
LoansState storage loans_,
PoolState memory poolState_,
address borrowerAddress_,
uint256 collateral_,
uint256 collateralScale_
) external returns (TakeResult memory result_) {
Borrower memory borrower = loans_.borrowers[borrowerAddress_];
// revert if borrower's collateral is 0 or if maxCollateral to be taken is 0
if (borrower.collateral == 0 || collateral_ == 0) revert InsufficientCollateral();
(
result_.collateralAmount,
result_.quoteTokenAmount,
result_.t0RepayAmount,
borrower.t0Debt,
result_.t0DebtPenalty,
result_.excessQuoteToken
) = _take(
auctions_,
TakeParams({
borrower: borrowerAddress_,
collateral: borrower.collateral,
t0Debt: borrower.t0Debt,
takeCollateral: collateral_,
inflator: poolState_.inflator,
poolType: poolState_.poolType,
collateralScale: collateralScale_
})
);
borrower.collateral -= result_.collateralAmount;
if (result_.t0DebtPenalty != 0) {
poolState_.debt += Maths.wmul(result_.t0DebtPenalty, poolState_.inflator);
}
(
result_.poolDebt,
result_.newLup,
result_.t0DebtInAuctionChange,
result_.settledAuction
) = _takeLoan(
auctions_,
buckets_,
deposits_,
loans_,
poolState_,
borrower,
borrowerAddress_,
result_.t0RepayAmount
);
}
Same for bucketTake(), it's used, but not filled, so result.remainingCollateral is always uninitialized zero:
https://github.com/sherlock-audit/2023-01-ajna/blob/main/contracts/src/ERC721Pool.sol#L476-L518
function bucketTake(
address borrowerAddress_,
bool depositTake_,
uint256 index_
) external override nonReentrant {
PoolState memory poolState = _accruePoolInterest();
BucketTakeResult memory result = Auctions.bucketTake(
auctions,
buckets,
deposits,
loans,
poolState,
borrowerAddress_,
depositTake_,
index_,
1
);
// update pool balances state
uint256 t0PoolDebt = poolBalances.t0Debt;
uint256 t0DebtInAuction = poolBalances.t0DebtInAuction;
if (result.t0DebtPenalty != 0) {
t0PoolDebt += result.t0DebtPenalty;
t0DebtInAuction += result.t0DebtPenalty;
}
t0PoolDebt -= result.t0RepayAmount;
t0DebtInAuction -= result.t0DebtInAuctionChange;
poolBalances.t0Debt = t0PoolDebt;
poolBalances.t0DebtInAuction = t0DebtInAuction;
poolBalances.pledgedCollateral -= result.collateralAmount;
// update pool interest rate state
poolState.debt = result.poolDebt;
poolState.collateral -= result.collateralAmount;
_updateInterestState(poolState, result.newLup);
if (result.settledAuction) _rebalanceTokens(borrowerAddress_, result.remainingCollateral);
}
/**
* @notice Performs bucket take collateral on an auction, rewards taker and kicker (if case) and updates loan info (settles auction if case).
* @dev reverts on:
* - insufficient collateral InsufficientCollateral()
* @param borrowerAddress_ Borrower address to take.
* @param depositTake_ If true then the take will happen at an auction price equal with bucket price. Auction price is used otherwise.
* @param index_ Index of a bucket, likely the HPB, in which collateral will be deposited.
* @return result_ BucketTakeResult struct containing details of take.
*/
function bucketTake(
AuctionsState storage auctions_,
mapping(uint256 => Bucket) storage buckets_,
DepositsState storage deposits_,
LoansState storage loans_,
PoolState memory poolState_,
address borrowerAddress_,
bool depositTake_,
uint256 index_,
uint256 collateralScale_
) external returns (BucketTakeResult memory result_) {
Borrower memory borrower = loans_.borrowers[borrowerAddress_];
if (borrower.collateral == 0) revert InsufficientCollateral(); // revert if borrower's collateral is 0
(
result_.collateralAmount,
result_.t0RepayAmount,
borrower.t0Debt,
result_.t0DebtPenalty
) = _takeBucket(
auctions_,
buckets_,
deposits_,
BucketTakeParams({
borrower: borrowerAddress_,
collateral: borrower.collateral,
t0Debt: borrower.t0Debt,
inflator: poolState_.inflator,
depositTake: depositTake_,
index: index_,
collateralScale: collateralScale_
})
);
borrower.collateral -= result_.collateralAmount;
if (result_.t0DebtPenalty != 0) {
poolState_.debt += Maths.wmul(result_.t0DebtPenalty, poolState_.inflator);
}
(
result_.poolDebt,
result_.newLup,
result_.t0DebtInAuctionChange,
result_.settledAuction
) = _takeLoan(
auctions_,
buckets_,
deposits_,
loans_,
poolState_,
borrower,
borrowerAddress_,
result_.t0RepayAmount
);
}
As a result _rebalanceTokens() will leave only borrowerCollateral_ = result.remainingCollateral = 0 in the borrower's collateral ids account:
https://github.com/sherlock-audit/2023-01-ajna/blob/main/contracts/src/ERC721Pool.sol#L533-L550
function _rebalanceTokens(
address borrowerAddress_,
uint256 borrowerCollateral_
) internal {
// rebalance borrower's collateral, transfer difference to floor collateral from borrower to pool claimable array
uint256[] storage borrowerTokens = borrowerTokenIds[borrowerAddress_];
uint256 noOfTokensPledged = borrowerTokens.length;
uint256 noOfTokensToTransfer = borrowerCollateral_ != 0 ? noOfTokensPledged - borrowerCollateral_ / 1e18 : noOfTokensPledged;
for (uint256 i = 0; i < noOfTokensToTransfer;) {
uint256 tokenId = borrowerTokens[--noOfTokensPledged]; // start with moving the last token pledged by borrower
borrowerTokens.pop(); // remove token id from borrower
bucketTokenIds.push(tokenId); // add token id to pool claimable tokens
unchecked { ++i; }
}
}
Tool used
Manual Review
Recommendation
Consider filling the variable with the resulting collateral of the borrower by placing result.remainingCollateral = borrower.collateral in the very end of Auctions's take() and bucketTake().
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 Auctions library's take and bucketTake functions omit assigning result.remainingCollateral = borrower.collateral before returning, so the caller's rebalancing step always sees zero.
Impact
Whenever take or bucketTake settles an auction, the borrower's remaining collateral is permanently frozen in full, and later operations that should return collateral to the borrower revert.
What the attacker needed
An ERC721 pool; a loan in auction; auction settled via take or bucketTake
The fix
The result's remaining collateral field should be set to the borrower's resulting collateral at the end of both Auctions take and bucketTake.
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.
This is a smart contract for lending against NFTs (unique digital tokens). When a loan goes bad, the NFT collateral is sold off in an auction, and afterwards the contract is meant to keep only the tokens still owed to the borrower. Because a value telling it how much collateral remains was never filled in, it treated the remainder as zero and moved all the borrower's tokens into a shared pool list. Those tokens are then stuck, since no accounting entry lets anyone withdraw them.
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.