Function refinanceFromLoanExecutionData() does not check executionData.tokenId == loan.nftCollateralTokenId
The refinance function did not verify that the token ID in the new loan execution data matched the token ID of the NFT already held as collateral. An attacker could refinance using a different NFT ID that lenders had approved, while the original NFT remained in escrow.
minhquanym Follow with oakcobalt and bin2chen
Originally disclosed on Code4rena
Submitted by minhquanym, also found by oakcobalt and bin2chen
The refinanceFromLoanExecutionData() function is used to refinance a loan from LoanExecutionData. It allows borrowers to use outstanding offers for new loans to refinance their current loan. This function essentially combines two actions: it processes the repayment for the previous loan and then emits a new loan.
The key difference is that the same NFT is used as collateral for the new loan, so it does not need to be transferred out of the protocol and then transferred back in. However, there is no check to ensure that the NFT id of the old loan matches the NFT id of the new execution data.
Therefore, the new loan may have a collateral NFT that does not match the NFT that the lender requested in their offers.
/// @dev We first process the incoming offers so borrower gets the capital. After that, we process repayments.
/// NFT doesn't need to be transferred (it was already in escrow)
(uint256 newLoanId, uint256[] memory offerIds, Loan memory loan, uint256 totalFee) =
_processOffersFromExecutionData(
borrower,
executionData.principalReceiver,
principalAddress,
nftCollateralAddress,
executionData.tokenId, // @audit No check if matched with loan.nftCollateralTokenId
executionData.duration,
offerExecution
);
Proof of Concept
As we can see, executionData.tokenId is passed to the _processOffersFromExecutionData() function instead of loan.nftCollateralTokenId. This function performs all the checks to ensure that the lenders’ offers accept this NFT.
function _processOffersFromExecutionData(
address _borrower,
address _principalReceiver,
address _principalAddress,
address _nftCollateralAddress,
uint256 _tokenId,
uint256 _duration,
OfferExecution[] calldata _offerExecution
) private returns (uint256, uint256[] memory, Loan memory, uint256) {
...
_validateOfferExecution(
thisOfferExecution,
_tokenId,
offer.lender,
offer.lender,
thisOfferExecution.lenderOfferSignature,
protocolFee.fraction,
totalAmount
);
...
}
Eventually, it calls the _checkValidators() function to check the NFT token ID.
function _checkValidators(LoanOffer calldata _loanOffer, uint256 _tokenId) private {
uint256 offerTokenId = _loanOffer.nftCollateralTokenId;
if (_loanOffer.nftCollateralTokenId != 0) {
if (offerTokenId != _tokenId) {
revert InvalidCollateralIdError();
}
} else {
uint256 totalValidators = _loanOffer.validators.length;
if (totalValidators == 0 && _tokenId != 0) {
revert InvalidCollateralIdError();
} else if ((totalValidators == 1) && (_loanOffer.validators[0].validator == address(0))) {
return;
}
for (uint256 i = 0; i < totalValidators;) {
IBaseLoan.OfferValidator memory thisValidator = _loanOffer.validators[i];
IOfferValidator(thisValidator.validator).validateOffer(_loanOffer, _tokenId, thisValidator.arguments);
unchecked {
++i;
}
}
}
}
However, since executionData.tokenId is passed in, an attacker could pass in a valid tokenId (an NFT id that will be accepted by all lender offers). But in reality, the loan.nftCollateralTokenId will be the NFT kept in escrow.
Recommended Mitigation Steps
Add a check to ensure that executionData.tokenId is equal to loan.nftCollateralTokenId.
Assessed type
Invalid Validation
0xend (Gondi) confirmed via duplicate Issue #14
Added
tokenIdCheck.
Status: Mitigation confirmed. Full details in reports from oakcobalt, minhquanym and bin2chen.
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 function passed executionData.tokenId to the offer validation logic instead of the existing loan's collateral token ID, so the check compared offers against the wrong NFT.
Impact
A borrower could refinance with a collateral NFT that does not match the one lenders approved, potentially leaving the original NFT in escrow while the new loan uses a different token.
What the attacker needed
Borrower has an existing loan; outstanding offers for new loans
The fix
The mitigation added a check that executionData.tokenId equals loan.nftCollateralTokenId.
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.
When you refinance a loan, you replace it with a new one. The code should make sure the new loan uses the same collateral item (like a digital collectible) as the old loan. Because it did not check, someone could trick the system into accepting a different item that lenders had agreed to, while the original item stayed locked up.
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.