QuickAccManager Smart Contract signature verification can be exploited
The QuickAccManager smart contract's signature verification could be bypassed because it trusted an attacker-controlled identity contract address in the signature. An attacker could craft a malicious signature that made the contract believe a valid signature was provided, allowing arbitrary transactions to be executed and funds stolen.
Submitted by cmichel
Several different signature modes can be used and Identity.execute forwards the signature parameter to the SignatureValidator library. The returned signer is then used for the privileges check:
address signer = SignatureValidator.recoverAddrImpl(hash, signature, true);
// signer will be QuickAccountContract
require(privileges[signer] != bytes32(0), 'INSUFFICIENT_PRIVILEGE');
It’s possible to create a smart contract mode signature (SignatureMode.SmartWallet) for arbitrary transactions as the QuickAccManager.isValidSignature uses an attacker-controlled id identity contract for the privileges check. An attacker can just create an attacker contract returning the desired values and the smart-wallet signature appears to be valid:
// @audit id is attacker-controlled
(address payable id, uint timelock, bytes memory sig1, bytes memory sig2) = abi.decode(signature, (address, uint, bytes, bytes));
// @audit this may not be used for authorization, attacker can return desired value
if (Identity(id).privileges(address(this)) == accHash) {
// bytes4(keccak256("isValidSignature(bytes32,bytes)")
return 0x1626ba7e;
} else {
return 0xffffffff;
}
POC
Assume an Identity contract is set up with a QuickAccManager as the privileges account, i.e. privileges[accHash] != 0.
We can construct a SignatureMode.SmartWallet signature for an arbitrary hash:
- Call
Identity.execute(txns, spoofedSignature)wherespoofedSignature = abi.encode(attackerContract, timelock=0, sig1=0, sig2=0, address(quickAccountManager), SignatureMode.SmartWallet) - This will call
recoverAddrImpl(txnsHash, spoofedSignature, true), decode the bytes at the end ofspoofedSignatureand determinemode = SignatureMode.SmartWalletandwallet = quickAccountManager. It will cut off these arguments and callquickAccountManager.isValidSignature(txnsHash, (attackerContract, 0, 0, 0)) - The
QuickAccManagerwill decode the signature, constructaccHashwhich is the hash of all zeroes (due to failed signatures returning 0). It will then callattacker.privileges(address(this))and the attacker contract can return theaccHashthat matches an account hash of failed signatures, i.e.,keccak256(abi.encode(QuickAccount(0,0,0))). The comparison is satisfied and it returns the success value. - The checks in
Identity.executepass and the transactionstxnsare executed.
Impact
Any Identity contract using QuickAccManager can be exploited. Funds can then be stolen from the wallet.
Recommendation
The issue is that QuickAccManager blindly trusts the values in signature. It might be enough to remove the id from the signature and use msg.sender as the identity instead: Identity(msg.sender).privileges(address(this)) == accHash. This seems to work with the current Identity implementation but might not work if this is extended and the isValidSignature is called from another contract and wants to verify a signature on a different identity. In that case, the Identity/SignatureValidator may not blindly forward the attacker-supplied signature and instead needs to re-encode the parameters with trusted values before calling QuickAccManager.
Ivshti (Ambire) confirmed and patched:
great find! Mitigated in https://github.com/AmbireTech/adex-protocol-eth/commit/17c073d037ded76d56d6145faa92c1959fd47226 but still figuring out whether this is the best way to do it
GalloDaSballo (judge) commented:
May need to sit on this one for another day before I can fully comment
Fundamentally by calling Identity.execute with mostly 0 data, you are able to call back to
QuickAccManager. isValidSignaturewhich, due to the implementation ofecrecoverat the time, will return valid checks for address(0), allowing to bypass all the logic and returning true for the signature, allowing for the execution of arbitrary code.Again, need to sit on this one
But wouldn’t you also be able to set a malicious smartContractWallet as the
IERC1271Wallet, hence you can sidestep the entire logic, as your malicious contract wallet can be programmed to always return true on any input value?
@GalloDaSballo (judge) this doesn’t have to do with
address(0)Using smart wallets for signatures by itself is not a problem - since they authorize as themselves.
The fundamental root of this issue is that ERC 1271 was designed with the assumption that 1 contract = 1 wallet. And as such,
isValidSignatureonly returnstrue/false. This makes sense, as essentially we’re asking the wallet “is this a valid signature from you”, and then the wallet decides how to actually validate this it depending on it’s own behavior and permissions.However, the QuickAccManager is a singleton contract - one single QuickAccManager represents multiple users. As such, combining it with ERC 1271 is a logical misunderstanding, as we can’t really ask it “is this a valid sig for X identity” through the ERC 1271 interface. So instead, we encode the identity that we’re signing as in the sig itself, but then a malicious user could call a top-level identity with a sig that validates in the singleton
QuickAccManager, but meant to validate with a differerent identity.Because what we pass to
isValidSignatureis opaque data (the smart wallet may be any contract with any logic, not just ourQuickAccManager) we can’t just peak into the sig and see if it’s meant to validate with the caller identity.Excellent finding IMO
The current mitigation is hacky, and essentially leads to an
isValidSignatureimplementation that is unusable (and doesn’t make sense) off-chain, but we prefer it to introducing a new sig type especially for QuickAccManager.
GalloDaSballo (judge) commented:
@Ivshti (Ambire) To clarify: Would adding
privileges[QuickAccountManager] = bytes32(uint(1))enable the exploit?
@GalloDaSballo (judge) yes, it would. Any authorized quickAcc would enable the exploit
GalloDaSballo (judge) commented:
I’m starting to get this
The
idsent toisValidSignatureis an untrusted, unverified address The contract at that address can be programmed to have a functionprivilegeswhich would return anybytes32value to matchaccHashThis effectively allows to run arbitrary transactions.A way to mitigate would be to have a way to ensure the called
idis trusted A registry of trusted ids may be effectiveThe mitigation the sponsor has chosen does solve for only using trusted Identities as in the case of a malicious Identity, the Identity would just validate it’s own transaction, not putting other Identities funds at risk.
An alternative solution would be to change the
IdentityFactoryto use the OpenZeppelin Clones Library (or similar) to ensure that the correct Logic is deployed (by deploying a minimal-proxy pointing to the trusted implementation). This would require a fair tech-lift and would limit the type of deployments that the IdentityFactory can perform.The exploit was severe and the sponsor has mitigated by checking the
msg.senderagainst theidprovided in the signature
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 QuickAccManager blindly trusted the identity contract address provided in the signature, allowing an attacker to supply a malicious contract that returned a valid-looking privileges hash.
Impact
Any Identity contract using QuickAccManager could be exploited, allowing funds to be stolen from the wallet.
What the attacker needed
An Identity contract with QuickAccManager as a privilege; Attacker can deploy a malicious contract
The fix
The issue was mitigated by checking that the msg.sender matches the id provided in the signature, ensuring only trusted identities are used.
How it was found
- Call Identity.execute with a spoofed signature that encodes an attacker contract, zero timelock, zero signatures, the QuickAccManager address, and SignatureMode.SmartWallet.
- The SignatureValidator decodes the signature, determines the mode and wallet, and calls QuickAccManager.isValidSignature with the attacker contract and zero values.
- QuickAccManager decodes the signature, computes a hash of zero values, and calls the attacker contract's privileges function.
- The attacker contract returns the hash of zero values, which matches the computed hash, so isValidSignature returns success.
- The checks in Identity.execute pass and the arbitrary transactions are executed.
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 are programs on a blockchain that can hold and transfer funds. This bug let an attacker trick a wallet contract into thinking a fake signature was valid, so they could make the wallet send funds to themselves. The problem was that the wallet trusted a piece of data that the attacker could control.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.