AuditBase
Sign InGet Started
lowL032

Signature Malleability of EVM's ecrecover

Learn about the vulnerability of calling Solidity's ecrecover() function directly and the risk of replay attacks. Discover how using OpenZeppelin's ECDSA library can provide a secure solution with guaranteed unique signatures.

Category

low-severity

Languages

solidity

Analysis Layer

static

Severity

low

In Ethereum, ecrecover is a built-in function that extracts the signer's address from a signature, making it essential for mechanisms that rely on message signing and signature verification. However, due to its susceptibility to signature malleability, it can potentially lead to security vulnerabilities if not properly handled. Signature malleability refers to the ability to alter a signature without invalidating it, thus potentially allowing different inputs to produce valid signatures that look as though they were signed by the same signer.

Problem

The ecrecover function can return a valid address from a malformed signature (one that has been subtly altered but still valid under the secp256k1 curve used in Ethereum). This can lead to issues where an attacker could manipulate a transaction signature to deceive a contract into accepting it as authentic, despite it being tampered with.

Solution

To mitigate risks associated with signature malleability in ecrecover, smart contracts should include additional checks to verify the integrity of the signature components, specifically the v, r, and s parameters. Ensuring that these components fall within specific ranges can help in confirming the authenticity of the signature.

Example Code

pragma solidity ^0.8.0;

contract SignatureValidator {
    function isSignatureValid(bytes32 hash, uint8 v, bytes32 r, bytes32 s) public pure returns (bool) {
        // Standard checks for signature validity
        require(uint256(s) <= 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0, "Invalid signature 's' value");
        require(v == 27 || v == 28, "Invalid signature 'v' value");

        address signer = ecrecover(hash, v, r, s);
        require(signer != address(0), "Invalid signer");

        return (signer == expectedSigner); // replace 'expectedSigner' with the actual expected signer's address
    }
}

Conclusion

While ecrecover is a powerful tool for signature verification in Ethereum, its susceptibility to signature malleability poses a significant risk. By implementing additional validation steps and understanding the underlying cryptography, developers can safeguard their contracts against potential exploits. This careful approach to signature validation ensures that only genuinely signed and untampered data is accepted, maintaining the integrity and security of the application.