hashProposal uses wrong typeshash when hashing the encoded Proposal struct data
The hashProposal function used uint256 instead of bytes32 for the questionId field in the EIP-712 type hash, causing signature verification to fail and breaking EIP-712 compatibility. An attacker could not exploit this directly, but legitimate signatures would be rejected.
0xAadi Follow with 0xShoonya, Ironsidesec and 5 others
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2024-09-predict-fun-judging/issues/266
Found by
0xAadi, 0xShoonya, Ironsidesec, KiroBrejka, ZanyBonzy, h2134, infect3d, valuevalk
Summary
acceptLoanOfferAndFillOrder, _refinance, matchProposals use _assertValidSignature which hashes proposal data and verifies the signature. But the hashed proposal type hash computation is wrong due to usage of uint256 questionId instead of bytes32 questionId
There are 2 impacts. So, even if one is acceptable/wrong, then the issue impact on another.
- This will break the signature verification.
- And breaking the strict EIP712's compatibility (mentioned in readme) where atomic types should be the same as the data format in the struct. Mentioned in
Definition of typed structured datasection.
Root Cause
Using uint256 questionId instead of bytes32 questionId inside the type hash of hashProposal()
Internal pre-conditions
No response
External pre-conditions
No response
Attack Path
Issue flow :
- look at line 50 below, the
questionIdis in bytes 32. And when hashing a proposal data, the type hash of proposal struct format should also use bytes32 for question id. But here its using uint256. Check on line 819 below. - Due to this, the type hash will be different result. look at the chisel example below. The hashes are different, so the signature hash is using wrong digest to verify the signature. Should have used bytes32 itself.
This breaks the EIP712 , where atomic types like uint, bytes1 to bytes32, address should be directly used. And only strings, bytes data are dynamic types, should be keccack hashed and then derive the type hash.
IPredictDotLoan.sol
45: struct Proposal {
---- SNIP ----
49: QuestionType questionType;
50: > bytes32 questionId;
51: bool outcome;
52: uint256 interestRatePerSecond;
---- SNIP ----
59: uint256 protocolFeeBasisPoints;
60: }
PredictDotLoan.sol
814: function hashProposal(Proposal calldata proposal) public view returns (bytes32 digest) {
815: digest = _hashTypedDataV4(
816: keccak256(
817: abi.encode(
818: keccak256(
819: >>> "Proposal(address from,uint256 loanAmount,uint256 collateralAmount,uint8 questionType,uint256 questionId,bool outcome,uint256 interestRatePerSecond,uint256 duration,uint256 validUntil,uint256 salt,uint256 nonce,uint8 proposalType,uint256 protocolFeeBasisPoints)"
820: ),
---- SNIP ----
824: proposal.questionType,
825: proposal.questionId,
---- SNIP ----
834: )
835: )
836: );
837: }
Impact
2 impacts
- due to wrong type hash computation leading to wrong digest validation in the signature validator, the signatures might fail.
- breaking the EIP712 mentioned in
readmewhere it strictly complains. The atomic types should not be hashed or converted to other types.
PoC
No response
Mitigation
function hashProposal(Proposal calldata proposal) public view returns (bytes32 digest) {
digest = _hashTypedDataV4(
keccak256(
abi.encode(
keccak256(
"Proposal(address from,uint256 loanAmount,uint256 collateralAmount,uint8 questionType,
- uint256 questionId,
bool outcome,uint256 interestRatePerSecond,uint256 duration,uint256 validUntil,uint256 salt,uint256 nonce,uint8 proposalType,uint256 protocolFeeBasisPoints)"
),
keccak256(
"Proposal(address from,uint256 loanAmount,uint256 collateralAmount,uint8 questionType,
+ bytes32 questionId,
bool outcome,uint256 interestRatePerSecond,uint256 duration,uint256 validUntil,uint256 salt,uint256 nonce,uint8 proposalType,uint256 protocolFeeBasisPoints)"
),
proposal.from,
---- SNIP ----
)
)
);
}
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/PredictDotFun/predict-dot-loan/pull/37
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 type hash string for the Proposal struct incorrectly specified uint256 for questionId instead of bytes32, which is the actual type in the struct.
Impact
Signatures might fail because the wrong digest is used for verification, and the contract breaks EIP-712 compatibility.
What the attacker needed
None
The fix
The type hash string should use bytes32 for questionId instead of uint256, and the protocol team fixed this issue.
How it was found
- Look at the Proposal struct where questionId is declared as bytes32.
- Look at the hashProposal function where the type hash string uses uint256 for questionId.
- Observe that the resulting type hash differs from the correct one, so the digest used for signature verification is wrong.
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 report is about a smart contract that checks digital signatures. The contract builds a fingerprint of the data to verify the signature, but it used the wrong data type for one field, so the fingerprint was always wrong. That meant valid signatures would be rejected, breaking the feature.
What is Crypto?
Encryption, signatures, randomness or certificate checks that do not protect what they claim to.