Please note, this content is no longer actively maintained.
The content of the SWC registry has not been thoroughly updated since 2020. It is known to be incomplete and may contain errors as well as crucial omissions.
For currently maintained guidance on known Smart Contract vulnerabilities written primarily as guidance for security reviewers, please see the EEA EthTrust Security Levels specification. As well as the latest release version, an Editor's draft is available, that represents the latest work of the group developing the specification.
General guidance for developers on what to consider to ensure security, that is currently maintained, is also available through the Smart Contract Security Verification Standard (SCSVS).
Title
Message call with hardcoded gas amount
Relationships
CWE-655: Improper Initialization
Description
The transfer()
and send()
functions forward a fixed amount of 2300 gas. Historically, it has often been recommended to use these functions for value transfers to guard against reentrancy attacks. However, the gas cost of EVM instructions may change significantly during hard forks which may break already deployed contract systems that make fixed assumptions about gas costs. For example. EIP 1884 broke several existing smart contracts due to a cost increase of the SLOAD instruction.
Remediation
Avoid the use of transfer()
and send()
and do not otherwise specify a fixed amount of gas when performing calls. Use .call.value(...)("")
instead. Use the checks-effects-interactions pattern and/or reentrancy locks to prevent reentrancy attacks.
References
- ChainSecurity - Ethereum Istanbul Hardfork: The Security Perspective
- Steve Marx - Stop Using Solidity’s transfer() Now
- EIP 1884
Samples
hardcoded_gas_limits.sol
/*
* @author: Bernhard Mueller (ConsenSys / MythX)
*/
pragma solidity 0.6.4;
interface ICallable {
function callMe() external;
}
contract HardcodedNotGood {
address payable _callable = 0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa;
ICallable callable = ICallable(_callable);
constructor() public payable {
}
function doTransfer(uint256 amount) public {
_callable.transfer(amount);
}
function doSend(uint256 amount) public {
_callable.send(amount);
}
function callLowLevel() public {
_callable.call.value(0).gas(10000)("");
}
function callWithArgs() public {
callable.callMe{gas: 10000}();
}
}