# The State of Ethereum Smart Contracts Security: Vulnerabilities, Countermeasures, and Tool Support
## 4 Smart-Contracts Vulnerabilities and Countermeasures
### Vulnerabilities and Countermeasures  
#### 4.2.1 Re-Entrancy  
Re-entrancy describes a situation when a contract \(A\) calls contract \(B\), which could call \(A\) back and execute \(A\)'s call again. The _fallback mechanism_ of Solidity causes this situation. The fallback function will be executed when calls from other contracts cannot find a matching function. When the caller uses the call function without giving any function signature, callee's fallback function will be activated. This function could call the caller's function to re-enter the caller. This mechanism could force unexpected and uncontrolled transfers of Ether in some circumstances. In the following code excerpt shown in Listing 1 (modified based on [73]), call is used to send Ether to the caller. In the beginning, the attacker calls the withdraw() function in contract \(A\) to ask for a one-Ether withdrawal. Contract \(A\) uses call to send the Ether and does not attach any function signature. Contract attacker's fallback function responds to this call by calling \(A\)'s withdraw function again. The second call is regarded as a \"nested\" call inside the previous withdrawal call because the previous one has not finished yet. This is the time that a recursive call begins to form. Normally, the caller will receive \begin{table}
\begin{tabular}{c c c c} \hline \hline
**Level** & **Vulnerability** & **CWE Index** & **Real-World Attack** \\ \hline \multirow{6}{*}{Solidity} & Re-entrancy & CWE-841: Improper Enforcement of & The DAO \\  & Behavioral Workflow & Attack [10] \\ \cline{2-4}  & Arithmetic issues & CWE-682: Incorrect Calculation & PoWHcoin \\  & & attack [64] \\ \cline{2-4}  & Delegatecall to insecure contracts & CWE-829: Inclusion of Functionality from & Parity \\  & Untrusted Control Sphere & Wallet(Second \\  & & Hack) [65] \\ \cline{2-4}  & Selfdestruct & CWE-284: Improper Access Control & Parity Library \\ \cline{2-4}  & Tx.origin & CWE-477: Use of Obsolete Function & - \\ \cline{2-4}  & Mishandled exceptions & CWE-252: Unchecked Return Value & KingdomTheEther \\  & & attack [67] \\ \cline{2-4}  & Default visibility & CWE-710: Improper Adherence to & Parity \\  & Coding Standards & Wallet(First \\  & & Hack) [68] \\ \cline{2-4}  & External contract referencing & CWE-829: Inclusion of Functionality from & \\  & Untrusted Control Sphere & Honey Pot [69] \\ \hline \multirow{6}{*}{EVM} & Short address/parameter issues & CWE-88: Improper Neutralization of & \\  & Argument Delimiters in a Command & - \\  & & (’Argument Injection’) & \\ \cline{2-4}  & Freezing Ether & CWE-17: Code Development & - \\  & & (Specification, Design, and Implementation) & \\ \hline \multirow{6}{*}{Blockchain} & Transaction order dependence & CWE-362: Concurrent Execution using & Attack on \\  & & Shared Resource with Improper Synchronization & Bancor [70] \\ \cline{1-1}  & & (’Race Condition’) & \\ \cline{1-1} \cline{2-4}  & Generating randomness & CWE-330: Use of Insufficiently Random Values & PRNG \\ \cline{1-1}  & & contract [71] \\ \cline{1-1} \cline{2-4}  & Timestamp dependence & CWE-829: Inclusion of Functionality & GovernMental \\ \cline{1-1}  & from Untrusted Control Sphere & attack [72] \\ \hline \hline \end{tabular}
\end{table} 
Table 2: Our taxonomy of smart-contract vulnerabilities. one Ether, and the caller's balance will be deducted as the execution of the instruction balances[msg.sender] -- _amount;. However, in a _re-entrancy situation_, this instruction will never be executed because the call function above this line will lead to recursively calling the withdraw() function until the total balance of Contract A is less than one Ether or the gas limit is reached. ```
contractA{ mapping(address=>uint)publicbalances; functionwithdraw(uint_amount)public{ msg.sender.call{value:_amount}(''''); balances[msg.sender] -=_amount; } /*Othercodes=/ } contractAttacker{ Apublica; fallback()externalpayable{ a.withdraw(1 ether); } functionattack(){ a.withdraw(1 ether); } }
```  
_Countermeasures_. There are three techniques to prevent re-entrancy.