getTransactionCount vulnerable to run out of gas and DoS even if it's a view function. So whatever clients rely on this view function would not be able todo his job anymore as calling this function would always revert.
Impact
getTransactionCount will always revert once ~1300 transactions (in Remix the gas limit seems to be 3M) have been submitted throught the wallet. The gas limit seems to be 30M thought, I'm not sure why in Remix it revert at 3M, but if it needs to reach 30M, then the cap is around 12000 transactions (see PoC)
Recommendation
The problem is the for loop for (uint i=0; i<transactionCount; i++) which will iterate over all the txs submitted. Even if it's only to update a counter, it will exhaust all the gas available at some point, and this seems to be around having submitted 1300 transactions.
Similar to what getTransactionIds is doing, you would need to introduce a range of ids you want (uint from, uint to), and ensure the for loop is only considering this range.
/// @dev Returns total number of transactions after filers are applied.
/// @param pending Include pending transactions.
/// @param executed Include executed transactions.
/// @return Total number of transactions after filters are applied.
function getTransactionCount(bool pending, bool executed)
public
constant
returns (uint count)
{
for (uint i=0; i<transactionCount; i++)
if ( pending && !transactions[i].executed
|| executed && transactions[i].executed)
count += 1;
}
Proof of concept
You will have to clone the immunefi poc repo and follow the steps below. I had to upgrade the MultiSigWallet code to match solc v0.8.19 compiler in order todo this PoC. Nevertheles, I also tested in Remix using the same compiler (v0.4.26, also using the original code) and the it start reverting at around 1300 txs. Once you run the test, you can see the gas used which is above 30M.
/** *Submitted for verification at Etherscan.io on 2021-03-04*//** *Submitted for verification at Etherscan.io on 2018-05-10*/pragmasolidity ^0.8.13;/// @title Multisignature wallet - Allows multiple parties to agree on transactions before execution./// @author Stefan George - <stefan.george@consensys.net>contract MultiSigWallet {uintconstantpublic MAX_OWNER_COUNT =50;eventConfirmation(addressindexed sender, uintindexed transactionId);eventRevocation(addressindexed sender, uintindexed transactionId);eventSubmission(uintindexed transactionId);eventExecution(uintindexed transactionId);eventExecutionFailure(uintindexed transactionId);eventDeposit(addressindexed sender, uint value);eventOwnerAddition(addressindexed owner);eventOwnerRemoval(addressindexed owner);eventRequirementChange(uint required);mapping (uint=> Transaction) public transactions;mapping (uint=>mapping (address=>bool)) public confirmations;mapping (address=>bool) public isOwner;address[] public owners;uintpublic required;uintpublic transactionCount;structTransaction {address destination;uint value;bytes data;bool executed; }modifieronlyWallet() {if (msg.sender !=address(this))revert(); _; }modifierownerDoesNotExist(address owner) {if (isOwner[owner])revert(); _; }modifierownerExists(address owner) {if (!isOwner[owner])revert(); _; }modifiertransactionExists(uint transactionId) {if (transactions[transactionId].destination ==address(0))revert(); _; }modifierconfirmed(uint transactionId,address owner) {if (!confirmations[transactionId][owner])revert(); _; }modifiernotConfirmed(uint transactionId,address owner) {if (confirmations[transactionId][owner])revert(); _; }modifiernotExecuted(uint transactionId) {if (transactions[transactionId].executed)revert(); _; }modifiernotNull(address_address) {if (_address ==address(0))revert(); _; }modifiervalidRequirement(uint ownerCount,uint_required) {if ( ownerCount > MAX_OWNER_COUNT|| _required > ownerCount|| _required ==0|| ownerCount ==0)revert(); _; }/* * Public functions *//// @dev Contract constructor sets initial owners and required number of confirmations./// @param _owners List of initial owners./// @param _required Number of required confirmations.constructor(address[] memory_owners,uint_required)validRequirement(_owners.length, _required) {for (uint i=0; i<_owners.length; i++) {if (isOwner[_owners[i]] || _owners[i] ==address(0))revert(); isOwner[_owners[i]] =true; } owners = _owners; required = _required; }/// @dev Allows to add a new owner. Transaction has to be sent by wallet./// @param owner Address of new owner.functionaddOwner(address owner)publiconlyWalletownerDoesNotExist(owner)notNull(owner)validRequirement(owners.length + 1,required) { isOwner[owner] =true; owners.push(owner);emitOwnerAddition(owner); }/// @dev Allows to remove an owner. Transaction has to be sent by wallet./// @param owner Address of owner.functionremoveOwner(address owner)publiconlyWalletownerExists(owner) { isOwner[owner] =false;for (uint i=0; i<owners.length -1; i++)if (owners[i] == owner) { owners[i] = owners[owners.length -1];break; } owners.pop();if (required > owners.length)changeRequirement(owners.length);emitOwnerRemoval(owner); }/// @dev Allows to replace an owner with a new owner. Transaction has to be sent by wallet./// @param owner Address of owner to be replaced./// @param owner Address of new owner.functionreplaceOwner(address owner,address newOwner)publiconlyWalletownerExists(owner)ownerDoesNotExist(newOwner) {for (uint i=0; i<owners.length; i++)if (owners[i] == owner) { owners[i] = newOwner;break; } isOwner[owner] =false; isOwner[newOwner] =true;emitOwnerRemoval(owner);emitOwnerAddition(newOwner); }/// @dev Allows to change the number of required confirmations. Transaction has to be sent by wallet./// @param _required Number of required confirmations.functionchangeRequirement(uint_required)publiconlyWalletvalidRequirement(owners.length,_required) { required = _required;emitRequirementChange(_required); }/// @dev Allows an owner to submit and confirm a transaction./// @param destination Transaction target address./// @param value Transaction ether value./// @param data Transaction data payload.functionsubmitTransaction(address destination,uint value,bytescalldata data)publicreturns (uint transactionId) { transactionId =addTransaction(destination, value, data);confirmTransaction(transactionId); }/// @dev Allows an owner to confirm a transaction./// @param transactionId Transaction ID.functionconfirmTransaction(uint transactionId)publicownerExists(msg.sender)transactionExists(transactionId)notConfirmed(transactionId, msg.sender) { confirmations[transactionId][msg.sender] =true;emitConfirmation(msg.sender, transactionId);executeTransaction(transactionId); }/// @dev Allows an owner to revoke a confirmation for a transaction./// @param transactionId Transaction ID.functionrevokeConfirmation(uint transactionId)publicownerExists(msg.sender)confirmed(transactionId, msg.sender)notExecuted(transactionId) { confirmations[transactionId][msg.sender] =false;emitRevocation(msg.sender, transactionId); }/// @dev Allows anyone to execute a confirmed transaction./// @param transactionId Transaction ID.functionexecuteTransaction(uint transactionId)publicnotExecuted(transactionId) {if (isConfirmed(transactionId)) { Transaction storage txl = transactions[transactionId]; txl.executed =true; (bool success,) = txl.destination.call{value:txl.value}(txl.data);if (success) {emitExecution(transactionId); } else {emitExecutionFailure(transactionId); txl.executed =false; } } }/// @dev Returns the confirmation status of a transaction./// @param transactionId Transaction ID.functionisConfirmed(uint transactionId)publicviewreturns (bool) {uint count =0;for (uint i=0; i<owners.length; i++) {if (confirmations[transactionId][owners[i]]) count +=1;if (count == required)returntrue; } }/* * Internal functions *//// @dev Adds a new transaction to the transaction mapping, if transaction does not exist yet./// @param destination Transaction target address./// @param value Transaction ether value./// @param data Transaction data payload.functionaddTransaction(address destination,uint value,bytescalldata data)internalnotNull(destination)returns (uint transactionId) { transactionId = transactionCount; transactions[transactionId] =Transaction({ destination: destination, value: value, data: data, executed:false }); transactionCount +=1;emitSubmission(transactionId); }/* * Web3 call functions *//// @dev Returns number of confirmations of a transaction./// @param transactionId Transaction ID.functiongetConfirmationCount(uint transactionId)publicviewreturns (uint count) {for (uint i=0; i<owners.length; i++)if (confirmations[transactionId][owners[i]]) count +=1; }/// @dev Returns total number of transactions after filers are applied./// @param pending Include pending transactions./// @param executed Include executed transactions.functiongetTransactionCount(bool pending,bool executed)publicviewreturns (uint count) {for (uint i=0; i<transactionCount; i++)if ( pending &&!transactions[i].executed|| executed && transactions[i].executed) count +=1; }/// @dev Returns list of owners.functiongetOwners()publicviewreturns (address[] memory allo) {return owners; }/// @dev Returns array with owner addresses, which confirmed transaction./// @param transactionId Transaction ID.functiongetConfirmations(uint transactionId)publicviewreturns (address[] memory_confirmations) {address[] memory confirmationsTemp =newaddress[](owners.length);uint count =0;uint i;for (i=0; i<owners.length; i++)if (confirmations[transactionId][owners[i]]) { confirmationsTemp[count] = owners[i]; count +=1; } _confirmations =newaddress[](count);for (i=0; i<count; i++) _confirmations[i] = confirmationsTemp[i]; }/// @dev Returns list of transaction IDs in defined range./// @param from Index start position of transaction array./// @param to Index end position of transaction array./// @param pending Include pending transactions./// @param executed Include executed transactions.functiongetTransactionIds(uint from,uint to,bool pending,bool executed)publicviewreturns (uint[] memory_transactionIds) {uint[] memory transactionIdsTemp =newuint[](transactionCount);uint count =0;uint i;for (i=0; i<transactionCount; i++)if ( pending &&!transactions[i].executed|| executed && transactions[i].executed) { transactionIdsTemp[count] = i; count +=1; } _transactionIds =newuint[](to - from);for (i=from; i<to; i++) _transactionIds[i - from] = transactionIdsTemp[i]; }}
I've also tested it in Remix the following way:
Add the MultiSigWallet.sol to Remix
Add the following code at the end of the Constructor
// add dummy txs
bytes memory localBytes = new bytes(1);
for (uint k=0; k<1100; k++) {
submitTransaction(owners[0], 0, localBytes);
}
Deploy with 2 valid owner and 2 required
Call getTransactionCount with true, true
Check the execution gas cost, it will be almost 3M, which is the limit.
Call submitTransaction about 150 times to reach 1250 (valid address, 0 and 0x00)
Call getTransactionCount with true, true
This will revert, running out of gas. While all those transaction are pending, even if you try to call it only looking for executed, it will still blow up or be very close to.