Skip to content

Commit ec1981d

Browse files
ernestognwclaudeAmxx
committed
MultiSignerERC7913: decode signature from calldata to avoid abi.decode reverts (M-08) (#6642)
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
1 parent 6601cd4 commit ec1981d

4 files changed

Lines changed: 122 additions & 6 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openzeppelin-solidity': patch
3+
---
4+
5+
`MultiSignerERC7913`: Decode the multisignature payload directly from calldata and return `false` on malformed encoding instead of reverting during `abi.decode`, so paymaster/account validation can surface `SIG_VALIDATION_FAILED` rather than bubble up a revert. The `_validateSignatures` and `_validateThreshold` override parameters change from `bytes[] memory` to `bytes[] calldata`.

‎contracts/utils/cryptography/signers/MultiSignerERC7913.sol‎

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,10 @@ abstract contract MultiSignerERC7913 is AbstractSigner {
221221
bytes calldata signature
222222
) internal view virtual override returns (bool) {
223223
if (signature.length == 0) return false; // For ERC-7739 compatibility
224-
(bytes[] memory signers, bytes[] memory signatures) = abi.decode(signature, (bytes[], bytes[]));
225-
return _validateThreshold(signers) && _validateSignatures(hash, signers, signatures);
224+
(bool success, bytes[] calldata signers, bytes[] calldata signatures) = _tryDecodeMultisignatureCalldata(
225+
signature
226+
);
227+
return success && _validateThreshold(signers) && _validateSignatures(hash, signers, signatures);
226228
}
227229

228230
/**
@@ -238,8 +240,8 @@ abstract contract MultiSignerERC7913 is AbstractSigner {
238240
*/
239241
function _validateSignatures(
240242
bytes32 hash,
241-
bytes[] memory signers,
242-
bytes[] memory signatures
243+
bytes[] calldata signers,
244+
bytes[] calldata signatures
243245
) internal view virtual returns (bool valid) {
244246
for (uint256 i = 0; i < signers.length; ++i) {
245247
if (!isSigner(signers[i])) {
@@ -253,7 +255,77 @@ abstract contract MultiSignerERC7913 is AbstractSigner {
253255
* @dev Validates that the number of signers meets the {threshold} requirement.
254256
* Assumes the signers were already validated. See {_validateSignatures} for more details.
255257
*/
256-
function _validateThreshold(bytes[] memory validatingSigners) internal view virtual returns (bool) {
258+
function _validateThreshold(bytes[] calldata validatingSigners) internal view virtual returns (bool) {
257259
return validatingSigners.length >= threshold();
258260
}
261+
262+
/**
263+
* @dev Decodes an `abi.encode(bytes[], bytes[])` multisignature payload from calldata without memory
264+
* allocation. Returns `success = false` on malformed encoding so callers can report an invalid
265+
* signature instead of reverting during validation (see ERC-4337 `SIG_VALIDATION_FAILED` semantics).
266+
*/
267+
function _tryDecodeMultisignatureCalldata(
268+
bytes calldata signature
269+
) private pure returns (bool success, bytes[] calldata signers, bytes[] calldata signatures) {
270+
unchecked {
271+
uint256 bufferLength = signature.length;
272+
273+
// Check #1: Theoretical minimum length of a valid multisignature encoding is 0x40 bytes.
274+
// 64 bytes of zero is a valid encoding for two empty arrays.
275+
if (bufferLength < 0x40) return (false, _emptyBytesArray(), _emptyBytesArray());
276+
277+
// Read the offset pointers to the signers and signatures arrays
278+
// Read is done in assembly to avoid the cost of creating calldata slices.
279+
uint256 signersOffset;
280+
uint256 signaturesOffset;
281+
assembly ("memory-safe") {
282+
signersOffset := calldataload(signature.offset)
283+
signaturesOffset := calldataload(add(signature.offset, 0x20))
284+
}
285+
286+
// Check #2: The length fields that the offset pointers point to must be within the bounds of the buffer.
287+
if (signersOffset > bufferLength - 0x20 || signaturesOffset > bufferLength - 0x20)
288+
return (false, _emptyBytesArray(), _emptyBytesArray());
289+
290+
// Read the length fields
291+
// Read is done in assembly to avoid the cost of creating calldata slices.
292+
uint256 signersLength;
293+
uint256 signaturesLength;
294+
assembly ("memory-safe") {
295+
signersLength := calldataload(add(signature.offset, signersOffset))
296+
signaturesLength := calldataload(add(signature.offset, signaturesOffset))
297+
}
298+
299+
// Data is just after the length fields
300+
uint256 signersDataOffset = signersOffset + 0x20;
301+
uint256 signaturesDataOffset = signaturesOffset + 0x20;
302+
303+
// Check #3 & #4:
304+
// - Cap lengths at 2**64-1 (Solidity's own dynamic-array limit) so `length * 0x20` cannot overflow.
305+
// - The data for each array must fit within the bounds of the buffer.
306+
if (
307+
signersLength > type(uint64).max ||
308+
signaturesLength > type(uint64).max ||
309+
0x20 * signersLength > bufferLength - signersDataOffset ||
310+
0x20 * signaturesLength > bufferLength - signaturesDataOffset
311+
) return (false, _emptyBytesArray(), _emptyBytesArray());
312+
313+
// Assembly cast
314+
assembly ("memory-safe") {
315+
success := 1 // true
316+
signers.offset := add(signature.offset, signersDataOffset)
317+
signers.length := signersLength
318+
signatures.offset := add(signature.offset, signaturesDataOffset)
319+
signatures.length := signaturesLength
320+
}
321+
}
322+
}
323+
324+
/// @dev Returns an empty `bytes[]` calldata slice, used as a placeholder on decode failure.
325+
function _emptyBytesArray() private pure returns (bytes[] calldata result) {
326+
assembly ("memory-safe") {
327+
result.offset := 0
328+
result.length := 0
329+
}
330+
}
259331
}

‎contracts/utils/cryptography/signers/MultiSignerERC7913Weighted.sol‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ abstract contract MultiSignerERC7913Weighted is MultiSignerERC7913 {
195195
* implementations of this function may exist in the contract, so important side effects may be missed
196196
* depending on the linearization order.
197197
*/
198-
function _validateThreshold(bytes[] memory signers) internal view virtual override returns (bool) {
198+
function _validateThreshold(bytes[] calldata signers) internal view virtual override returns (bool) {
199199
unchecked {
200200
uint64 weight = 0;
201201
for (uint256 i = 0; i < signers.length; ++i) {

‎test/account/AccountMultiSigner.test.js‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,5 +290,44 @@ describe('AccountMultiSigner', function () {
290290
await expect(this.mock.$_rawSignatureValidation(MESSAGE_HASH, prepareMultisig(signers, signatures))).to.eventually
291291
.be.false;
292292
});
293+
294+
describe('returns false (does not revert) on malformed outer encoding', function () {
295+
const word = v => ethers.zeroPadValue(ethers.toBeHex(v), 0x20);
296+
const encode = (...items) => ethers.concat(items.map(word));
297+
298+
it('supports minimal encoding', async function () {
299+
await expect(this.mock.$_rawSignatureValidation(MESSAGE_HASH, encode(0, 0))).to.eventually.be.false;
300+
await expect(this.mock.$_rawSignatureValidation(MESSAGE_HASH, encode(0x20, 0))).to.eventually.be.false;
301+
});
302+
303+
it('shorter than the minimum head layout (64 bytes)', async function () {
304+
await expect(this.mock.$_rawSignatureValidation(MESSAGE_HASH, '0xdeadbeef')).to.eventually.be.false;
305+
});
306+
307+
it('offset points past the calldata', async function () {
308+
await expect(this.mock.$_rawSignatureValidation(MESSAGE_HASH, encode(0xffff, 0xffff, 0, 0))).to.eventually.be
309+
.false;
310+
});
311+
312+
it('offset near type(uint256).max (would overflow in checked arithmetic)', async function () {
313+
await expect(
314+
this.mock.$_rawSignatureValidation(MESSAGE_HASH, encode(ethers.MaxUint256, ethers.MaxUint256, 0, 0)),
315+
).to.eventually.be.false;
316+
});
317+
318+
it('array length exceeds Solidity dynamic-array cap (2**64-1)', async function () {
319+
await expect(this.mock.$_rawSignatureValidation(MESSAGE_HASH, encode(0x40, 0x60, MAX_UINT64 + 1n, 0))).to
320+
.eventually.be.false;
321+
await expect(this.mock.$_rawSignatureValidation(MESSAGE_HASH, encode(0x40, 0x60, 0, MAX_UINT64 + 1n))).to
322+
.eventually.be.false;
323+
});
324+
325+
it('array length exceeds the remaining buffer', async function () {
326+
await expect(this.mock.$_rawSignatureValidation(MESSAGE_HASH, encode(0x40, 0x60, 10, 0))).to.eventually.be
327+
.false;
328+
await expect(this.mock.$_rawSignatureValidation(MESSAGE_HASH, encode(0x40, 0x60, 0, 10))).to.eventually.be
329+
.false;
330+
});
331+
});
293332
});
294333
});

0 commit comments

Comments
 (0)