Neither sellCreditMarket() nor compensate() checks whether the credit position to be sold is allowed for sale
The sellCreditMarket() and compensate() functions did not check whether a credit position was marked as not for sale, allowing its owner to sell or use it to compensate debt. A buyer could be forced to acquire a not-for-sale credit position that they could not resell, potentially causing a loss.
Submitted by 0xpiken, also found by adeolu, hyh, and Shield
https://github.com/code-423n4/2024-06-size/blob/main/src/libraries/actions/SellCreditMarket.sol#L51-L122 https://github.com/code-423n4/2024-06-size/blob/main/src/libraries/actions/Compensate.sol#L42-L101
Impact
A credit buyer could be forced to buy a not for sale credit position with no way to sell it passively, potentially suffering a loss due to missing an arbitrage opportunity.
Proof of Concept
Any user is allowed to lend or borrow szaUSDC through Size protocol.
- A borrower can create a limit borrowing order by calling
sellCreditLimit(), anyone can lend theirszaUSDCto this borrower by callingbuyCreditMarket(). - A lender can create a limit lending order by calling
buyCreditLimit(), anyone can borrowszaUSDCfrom this lender by callingsellCreditMarket(). - A existing credit position can be sold by its owner by
sellCreditMarket()by default. - A existing credit position by default could be bought by another borrower as long as the owner of credit position has created a limit borrowing order.
A lender can set their allCreditPositionsForSaleDisabled to true, which prevents any of their credit positions from being sold. Alternatively, a specific credit position can be marked as not for sale, making it impossible to sell and for anyone to buy it.
While no one can buy a not for sale credit position, their owner can force to sell it or use it compensate their debt. Once sold, no one can buy it again due to its not for sale status, resulting the new owner potentially suffering a loss.
Copy below codes to SellCreditMarketTest.t.sol and run forge test --match-test test_SellCreditMarket_sellNotForSaleCreditPosition:
function test_SellCreditMarket_sellNotForSaleCreditPosition() public {
_deposit(bob, weth, 100e18);
_deposit(alice, usdc, 100e6);
_deposit(candy, usdc, 100e6);
_deposit(james, usdc, 200e6);
_buyCreditLimit(alice, block.timestamp + 365 days, YieldCurveHelper.pointCurve(365 days, 0.2e18));
//@audit-info candy is willing to lend USDC with 20% APR
_buyCreditLimit(candy, block.timestamp + 365 days, YieldCurveHelper.pointCurve(365 days, 0.2e18));
//@audit-info candy is willing to borrow USDC with 10% APR
_sellCreditLimit(candy, YieldCurveHelper.pointCurve(365 days, 0.1e18));
uint256 tenor = 365 days;
vm.startPrank(bob);
uint256 apr = size.getLoanOfferAPR(alice, tenor);
//@audit-info bob sell 120e6 credit to alice for szaUSDC
size.sellCreditMarket(
SellCreditMarketParams({
lender: alice,
creditPositionId: RESERVED_ID,
amount: 120e6,
tenor: tenor,
deadline: block.timestamp,
maxAPR: apr,
exactAmountIn: true
})
);
vm.stopPrank();
uint256[] memory creditPositionIds = new uint256[](1);
creditPositionIds[0] = CREDIT_POSITION_ID_START;
_setUserConfiguration(alice, 1.7e18, true, false, creditPositionIds);
//@audit-info the credit position is marked not for sale
assertTrue(!size.getCreditPosition(CREDIT_POSITION_ID_START).forSale);
//@audit-info however, alice can sell her `not for sale` credit position to candy
vm.startPrank(alice);
size.sellCreditMarket(
SellCreditMarketParams({
lender: candy,
creditPositionId: CREDIT_POSITION_ID_START,
amount: 120e6,
tenor: tenor,
deadline: block.timestamp,
maxAPR: apr,
exactAmountIn: true
})
);
vm.stopPrank();
//@audit-info candy james the lender of credit position
assertEq(size.getCreditPosition(CREDIT_POSITION_ID_START).lender, candy);
apr = size.getBorrowOfferAPR(candy, tenor);
//@audit-info james can not buy the credit position from candy due to `not for sale`
vm.startPrank(james);
vm.expectRevert(abi.encodeWithSelector(Errors.CREDIT_NOT_FOR_SALE.selector, CREDIT_POSITION_ID_START));
size.buyCreditMarket(
BuyCreditMarketParams({
borrower: candy,
creditPositionId: CREDIT_POSITION_ID_START,
amount: 120e6,
tenor: 365 days,
deadline: block.timestamp,
minAPR: apr - 1,
exactAmountIn: false
})
);
vm.stopPrank();
//@audit-info candy has to alter `forSale` status to `true` manually.
_setUserConfiguration(candy, 1.7e18, false, true, creditPositionIds);
//@audit-info the `forSale` status of the credit position is marked as `true`
assertTrue(size.getCreditPosition(CREDIT_POSITION_ID_START).forSale);
//@audit-info james bought the credit position successfully
vm.startPrank(james);
size.buyCreditMarket(
BuyCreditMarketParams({
borrower: candy,
creditPositionId: CREDIT_POSITION_ID_START,
amount: 120e6,
tenor: 365 days,
deadline: block.timestamp,
minAPR: apr - 1,
exactAmountIn: false
})
);
vm.stopPrank();
//@audit-info james became the lender of credit position
assertEq(size.getCreditPosition(CREDIT_POSITION_ID_START).lender, james);
}
As we can see, as a speculator, Candy might suffer a loss because the credit position she is forced to buy is not able to be bought from her by any other buyer.
Recommended Mitigation Steps
No one should be allowed to sell their not for sale credit positions or use them to compensate their debts:
function validateSellCreditMarket(State storage state, SellCreditMarketParams calldata params) external view {
LoanOffer memory loanOffer = state.data.users[params.lender].loanOffer;
uint256 tenor;
// validate msg.sender
// N/A
// validate lender
if (loanOffer.isNull()) {
revert Errors.INVALID_LOAN_OFFER(params.lender);
}
// validate creditPositionId
if (params.creditPositionId == RESERVED_ID) {
tenor = params.tenor;
// validate tenor
if (tenor < state.riskConfig.minTenor || tenor > state.riskConfig.maxTenor) {
revert Errors.TENOR_OUT_OF_RANGE(tenor, state.riskConfig.minTenor, state.riskConfig.maxTenor);
}
} else {
CreditPosition storage creditPosition = state.getCreditPosition(params.creditPositionId);
DebtPosition storage debtPosition = state.getDebtPositionByCreditPositionId(params.creditPositionId);
if (msg.sender != creditPosition.lender) {
revert Errors.BORROWER_IS_NOT_LENDER(msg.sender, creditPosition.lender);
}
if (!state.isCreditPositionTransferrable(params.creditPositionId)) {
revert Errors.CREDIT_POSITION_NOT_TRANSFERRABLE(
params.creditPositionId,
state.getLoanStatus(params.creditPositionId),
state.collateralRatio(debtPosition.borrower)
);
}
+ User storage user = state.data.users[creditPosition.lender];
+ if (user.allCreditPositionsForSaleDisabled || !creditPosition.forSale) {
+ revert Errors.CREDIT_NOT_FOR_SALE(params.creditPositionId);
+ }
tenor = debtPosition.dueDate - block.timestamp; // positive since the credit position is transferrable, so the loan must be ACTIVE
// validate amount
if (params.amount > creditPosition.credit) {
revert Errors.NOT_ENOUGH_CREDIT(params.amount, creditPosition.credit);
}
}
// validate amount
if (params.amount < state.riskConfig.minimumCreditBorrowAToken) {
revert Errors.CREDIT_LOWER_THAN_MINIMUM_CREDIT(params.amount, state.riskConfig.minimumCreditBorrowAToken);
}
// validate tenor
if (block.timestamp + tenor > loanOffer.maxDueDate) {
revert Errors.DUE_DATE_GREATER_THAN_MAX_DUE_DATE(block.timestamp + tenor, loanOffer.maxDueDate);
}
// validate deadline
if (params.deadline < block.timestamp) {
revert Errors.PAST_DEADLINE(params.deadline);
}
// validate maxAPR
uint256 apr = loanOffer.getAPRByTenor(
VariablePoolBorrowRateParams({
variablePoolBorrowRate: state.oracle.variablePoolBorrowRate,
variablePoolBorrowRateUpdatedAt: state.oracle.variablePoolBorrowRateUpdatedAt,
variablePoolBorrowRateStaleRateInterval: state.oracle.variablePoolBorrowRateStaleRateInterval
}),
tenor
);
if (apr > params.maxAPR) {
revert Errors.APR_GREATER_THAN_MAX_APR(apr, params.maxAPR);
}
// validate exactAmountIn
// N/A
}
function validateCompensate(State storage state, CompensateParams calldata params) external view {
CreditPosition storage creditPositionWithDebtToRepay =
state.getCreditPosition(params.creditPositionWithDebtToRepayId);
DebtPosition storage debtPositionToRepay =
state.getDebtPositionByCreditPositionId(params.creditPositionWithDebtToRepayId);
uint256 amountToCompensate = Math.min(params.amount, creditPositionWithDebtToRepay.credit);
// validate creditPositionWithDebtToRepayId
if (state.getLoanStatus(params.creditPositionWithDebtToRepayId) != LoanStatus.ACTIVE) {
revert Errors.LOAN_NOT_ACTIVE(params.creditPositionWithDebtToRepayId);
}
// validate creditPositionToCompensateId
if (params.creditPositionToCompensateId == RESERVED_ID) {
uint256 tenor = debtPositionToRepay.dueDate - block.timestamp;
// validate tenor
if (tenor < state.riskConfig.minTenor || tenor > state.riskConfig.maxTenor) {
revert Errors.TENOR_OUT_OF_RANGE(tenor, state.riskConfig.minTenor, state.riskConfig.maxTenor);
}
} else {
CreditPosition storage creditPositionToCompensate =
state.getCreditPosition(params.creditPositionToCompensateId);
DebtPosition storage debtPositionToCompensate =
state.getDebtPositionByCreditPositionId(params.creditPositionToCompensateId);
if (!state.isCreditPositionTransferrable(params.creditPositionToCompensateId)) {
revert Errors.CREDIT_POSITION_NOT_TRANSFERRABLE(
params.creditPositionToCompensateId,
state.getLoanStatus(params.creditPositionToCompensateId),
state.collateralRatio(debtPositionToCompensate.borrower)
);
}
+ User storage user = state.data.users[creditPosition.lender];
+ if (user.allCreditPositionsForSaleDisabled || !creditPosition.forSale) {
+ revert Errors.CREDIT_NOT_FOR_SALE(params.creditPositionId);
+ }
if (
debtPositionToRepay.dueDate
< state.getDebtPositionByCreditPositionId(params.creditPositionToCompensateId).dueDate
) {
revert Errors.DUE_DATE_NOT_COMPATIBLE(
params.creditPositionWithDebtToRepayId, params.creditPositionToCompensateId
);
}
if (creditPositionToCompensate.lender != debtPositionToRepay.borrower) {
revert Errors.INVALID_LENDER(creditPositionToCompensate.lender);
}
if (params.creditPositionToCompensateId == params.creditPositionWithDebtToRepayId) {
revert Errors.INVALID_CREDIT_POSITION_ID(params.creditPositionToCompensateId);
}
amountToCompensate = Math.min(amountToCompensate, creditPositionToCompensate.credit);
}
// validate msg.sender
if (msg.sender != debtPositionToRepay.borrower) {
revert Errors.COMPENSATOR_IS_NOT_BORROWER(msg.sender, debtPositionToRepay.borrower);
}
// validate amount
if (amountToCompensate == 0) {
revert Errors.NULL_AMOUNT();
}
}
Assessed type
Context
aviggiano (Size) confirmed and commented:
This was not very well specified in our documentation, so it might be an issue.
On the one hand, not enforcing
forSalefor credit owners is good because we do not want to force users to do 2 actions if they are the ones selling them. On the other hand, passive buyers can end up with credits that can’t be resold.Additional feedback from the team:
I think we should reset the variable upon sale.
So I think this issue is technically valid.
Fixed in https://github.com/SizeCredit/size-solidity/pull/130.
The root cause is that
sellCreditMarket()andcompensate()don’t checkforSaleflag of a credit position. (creditPositionToCompensateIdincompensate()).There are two impacts:
- The new lender would have a credit position of
forSale = falseIn case of a full sale. (#184, #335)- The credit position’s
forSalewould be changed from false to true in case of a partial sale. (#228)Will consider them as duplicates because they have the same root cause and can be mitigated by resetting the flag consistently upon sale.
Note: For full discussion, see here.
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 validation logic in sellCreditMarket() and compensate() omitted a check for the forSale flag and the allCreditPositionsForSaleDisabled setting.
Impact
A credit buyer could be forced to buy a not-for-sale credit position with no way to sell it passively, potentially suffering a loss due to missing an arbitrage opportunity.
What the attacker needed
A credit position marked not for sale; the owner of that position acts as seller; a buyer willing to purchase the position
The fix
The protocol was fixed by resetting the forSale flag consistently upon sale, as confirmed by the team.
How it was found
- Alice marks her credit position as not for sale.
- Alice calls sellCreditMarket() to sell that not-for-sale credit position to Candy.
- Candy becomes the new lender but cannot sell the position because it remains not for sale.
- Candy must manually change the forSale status to true before anyone can buy it.
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.
In this lending protocol, a lender can mark their credit position as 'not for sale' to prevent others from buying it. However, the functions that let the owner sell or use the position to offset debt did not check this flag, so the owner could still transfer it. The new owner then got stuck with a position that could not be sold again, which could lead to a financial loss.
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.