The colRedeemed variable is wrongly retrieved in LibBytes::readProposalData function
The LibBytes::readProposalData function used an addition operation instead of a bitwise AND to extract the colRedeemed field from packed proposal data, producing an incorrect value. This could cause the RedemptionFacet contract to use a wrong colRedeemed amount when claiming a redemption.
Submitted by Bube, also found by maxim371 and Rhaydden
The LibBytes::readProposalData function uses inline assembly for efficient data extraction from a byte array. The colRedeemed variable, which represents an 11-byte value within the ProposalData structure, is intended to be extracted by applying a mask to isolate the relevant bytes. However, the current implementation incorrectly uses the add operation. That leads to retrieve incorrect value of colRedeemed variable:
function readProposalData(address SSTORE2Pointer, uint8 slateLength) internal view returns (MTypes.ProposalData[] memory) {
bytes memory slate = SSTORE2.read(SSTORE2Pointer);
// ProposalData is 51 bytes
require(slate.length % 51 == 0, "Invalid data length");
MTypes.ProposalData[] memory data = new MTypes.ProposalData[](slateLength);
for (uint256 i = 0; i < slateLength; i++) {
// 32 offset for array length, multiply by each ProposalData
uint256 offset = i * 51 + 32;
address shorter; // bytes20
uint8 shortId; // bytes1
uint64 CR; // bytes8
uint88 ercDebtRedeemed; // bytes11
uint88 colRedeemed; // bytes11
assembly {
// mload works 32 bytes at a time
let fullWord := mload(add(slate, offset))
// read 20 bytes
shorter := shr(96, fullWord) // 0x60 = 96 (256-160)
// read 8 bytes
shortId := and(0xff, shr(88, fullWord)) // 0x58 = 88 (96-8), mask of bytes1 = 0xff * 1
// read 64 bytes
CR := and(0xffffffffffffffff, shr(24, fullWord)) // 0x18 = 24 (88-64), mask of bytes8 = 0xff * 8
fullWord := mload(add(slate, add(offset, 29))) // (29 offset)
// read 88 bytes
ercDebtRedeemed := shr(168, fullWord) // (256-88 = 168)
// read 88 bytes
@> colRedeemed := add(0xffffffffffffffffffffff, shr(80, fullWord)) // (256-88-88 = 80), mask of bytes11 = 0xff * 11
}
data[i] = MTypes.ProposalData({
shorter: shorter,
shortId: shortId,
CR: CR,
ercDebtRedeemed: ercDebtRedeemed,
colRedeemed: colRedeemed
});
}
return data;
}
The add operation would incorrectly add the mask to the shifted value, potentially resulting in an incorrect value for colRedeemed. The correct operation should use and to apply the mask and isolate the 11-byte colRedeemed value.
The RedemptionFacet contract calls the LibBytes::readProposalData function and uses colRedeemed variable in claimRedemption function.
Proof of Concept
Link to the code here.
The following contract Assembly is a simple contract that contains two functions: incorrectColRedeemed with the logic from the LibBytes contract and the correctColRedeemed with the correct logic:
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.23;
contract Assembly {
function incorrectColRedeemed(uint256 fullWord) public pure returns (uint88) {
uint88 col;
assembly {
col := add(0xffffffffffffffffffffff, shr(80, fullWord))
}
return col;
}
function correctColRedeemed(uint256 fullWord) public pure returns (uint256) {
uint88 col;
assembly {
col := and(0xffffffffffffffffffffff, shr(80, fullWord))
}
return col;
}
}
The following test file contains test function test_assembly that compares the returned value from the both functions and shows the differences between the results:
contract TestAssembly is Test {
Assembly asm = new Assembly();
uint256 testFullWord = 0x1234599990abcdef1234567890abcdef1234567890abcdef1234567890abcdef;
function test_assembly() external view {
assert(asm.incorrectColRedeemed(testFullWord) < asm.correctColRedeemed(testFullWord));
console.log('Incorrect:', asm.incorrectColRedeemed(testFullWord));
console.log('Correct: ', asm.correctColRedeemed(testFullWord));
}
Tools Used
Foundry
Recommended Mitigation Steps
Replace the add operation with an and operation to correctly apply the mask:
colRedeemed := and(0xffffffffffffffffffffff, shr(80, fullWord)).
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 a typo in the inline assembly: the mask was applied with `add` instead of `and`, so the mask value was added to the shifted word rather than isolating the 11-byte field.
Impact
The RedemptionFacet contract could use an incorrect colRedeemed value in claimRedemption, potentially leading to wrong redemption accounting.
What the attacker needed
None
The fix
The `add` operation should be replaced with `and` to correctly apply the mask.
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.
Smart contracts sometimes pack several small numbers into one large number to save space. To pull one number out, you use a bitwise AND with a mask, which keeps only the bits you want. Here the code used addition instead, so the extracted number was wrong.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.