In a previous post we discussed the future of Ethereum’s scalability by analyzing the concepts presented at Devcon3. Let’s take a moment and imagine that all these scalability issues are now solved, and Ethereum’s Smart Contracts are working without issues.

Are those users going to be good willed or are they possible adversaries who interfere with the smooth functionality of the contracts?

Smart contracts are “immutable”. Once they are deployed, their code is impossible to change, making it impossible to fix any discovered bugs.

In a potential future where whole organizations are governed by smart contract code, there is an immense need for proper security.Past hacks such as TheDAO or this year’s Parity hacks (July, November) have raised developers’ awareness, but we still have a long way to go.

“This is Disneyland for hackers”

In this article we will go through some of the famous security pitfallsand their mitigations.

1. Overflows & Underflows

An overflow is when a number gets incremented above its maximum value. Solidity can handle up to 256 bit numbers (up to 2²⁵⁶-1), so incrementing by 1 would result into 0.

  0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
+ 0x000000000000000000000000000000000001
----------------------------------------
= 0x000000000000000000000000000000000000

After reaching the maximum reading, an odometer or trip meter restarts from zero, called odometer rollover.[source]

Likewise, in the inverse case, when the number is unsigned, decrementing will underflow the number, resulting in the maximum possible value.

  0x000000000000000000000000000000000000
- 0x000000000000000000000000000000000001
----------------------------------------
= 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

You can test the bug here:

As dangerous as both cases are, the underflow case is the more likely to happen, for example in the case where a token holder has X tokens but attempts to spend X+1. If the code does not check for it, the attacker might end up being allowed to spend more tokens than he had and have a maxed out balance.

Mitigation: It has been a standard for a while now to use OpenZeppelin’s SafeMath library.

You can test the fixed bug here:

2. Visibility & delegatecall

For the people who were around in July, this bug will be familiar, after all it was the Parity wallet hack which cost users about 30 million dollars.

Solidity visibility modifiers and their differences.

Publicfunctions can be called by anyone (by functions from inside the contract, by functions from inherited contracts or by outside users)

Externalfunctions can only be accessed externally, which means they cannot be called by other functions of the contract. The gist below does not compile, the external visibility of cannotBeCalled does not allow it to be called by the contract’s functions (however it can be called by another contract)

External is cheaper to use because it uses the calldata opcode while public needs to copy all the arguments to memory, as described here.

Privateand internalare simpler: private means that the function can only be called from inside the contract, while internal proves a more relaxed restriction allowing contracts that inherit from the parent contract to use that function.

That said, keep your functions private or internal unless there is a need for outside interaction.

Delegatecall

Paraphrased from the solidity docs:

“Delegatecall is identical to a message call apart from the fact that the code at the target address is executed in the context of the calling contract and msg.senderand msg.value do not change their values.

This means that a contract can dynamically load code from a different address at runtime. Storage, current address and balance still refer to the calling contract, only the code is taken from the called address.”

This low-level function has been very useful as it’s the backbone for implementing Libraries and modularizing code. However it opens up the doors to vulnerabilities as essentially your contract is allowing anyone to do whatever they want with their state.

In the example below, an attacker can call contract Delegate’s public function pwn and since the call is in the context of Delegation, they can claim ownership of the contract.

The Parity hack involved a combination of both insecure visibility modifiers and misuse of delegate call with abritrary data. The vulnerable contract’s function implemented delegatecall and a function from another contract that could modify ownership was left public. That allowed an attacker to craft the msg.data field to call the vulnerable function.

As for what would be included in the msg.data field, that is the signature of the function that you want to call. Signature here means the first 8 bytes of the sha3 (alias for keccak256)hash of the function prototype.

In this case:
web3.sha3("pwn()").slice(0, 10) --> 0xdd365b8b
If the function takes an argument, pwn(uint256 x):
web3.sha3("pwn(uint256").slice(0,10) --> 0x35f4581b

3. Reentrancy (TheDAO hack)

Solidity’s call function when called with value forwards all the gas it received. In the snippet below, the call is made before actually reducing the sender’s balance. This opened up a vulnerability which is described very well in reddit comment when TheDAO hack happened:

“In simple words, it’s like the bank teller doesn’t change your balance until she has given you all the money you requested. “Can I withdraw $500? Wait, before that, can I withdraw $500?”

And so on. The smart contracts as designed only check you have $500 at the beinning, once, and allow themselves to be interrupted.”

As described in detail here, the fix is to reduce the sender’s balance beforemaking the transfer of value. For people who have worked with parallel programming, another solution is using mutexes,mitigating all kinds of race conditions altogether.

Currently, using require(msg.sender.transfer(_value)) is the best way to handle these kinds of situations.


This concludes Part 1. In the next article, we will discuss some lesser known exploits, the tools you should add to your workflow and the future of smart contract security.

原文: https://medium.com/loom-network/how-to-secure-your-smart-contracts-6-solidity-vulnerabilities-and-how-to-avoid-them-part-1-c33048d4d17d

How to Secure Your Smart Contracts: 6 Solidity Vulnerabilities and how to avoid them (Part 1)相关推荐

  1. How to Secure Your Smart Contracts: 6 Solidity Vulnerabilities and how to avoid them (Part 2)

    While Part 1 discussed some more high profile or obvious vulnerabilities, this post will be about vu ...

  2. Understanding Ethereum Smart Contracts

    You might have heard the term "smart contract," and you might even know that they are &quo ...

  3. Reversing Ethereum Smart Contracts: Part 2

    In my previous tutorial, we began reversing engineering the Greeter.sol contract. Specifically, we l ...

  4. Part 2 — Making Sense of Smart Contracts

    The term "smart contract" has no clear and settled definition. The idea has long been hype ...

  5. 阅读论文Formal verification of smart contracts based on users and blockchain behaviors models

    1 题目(Formal verification of smart contracts based on users and blockchain behaviors models) 1.1 作者.出 ...

  6. (ASE2018)ContractFuzzer: Fuzzing Smart Contracts for Vulnerability Detection 解析

    (ASE2018)ContractFuzzer: Fuzzing Smart Contracts for Vulnerability Detection 解析 前言 一 摘要及主要贡献 摘要 贡献 二 ...

  7. Formal Verification of Smart Contracts Short Paper

    Formal Verification of Smart Contracts: Short Paper ABSTRACT 提出将使用F*框架用于编写代码 1. INTRODUCTION 本文目的:通过 ...

  8. Protecting Personal Data using Smart Contracts

    Protecting Personal Data using Smart Contracts 译:使用智能合约保护个人数据 原文链接:https://arxiv.org/pdf/1910.12298. ...

  9. [论文02]ZEUS Analyzing Safety of Smart Contracts

    ZEUS: Analyzing Safety of Smart Contracts 0.ABSTRACT 1. introdution 贡献 :happy: 分类已知或者之前发现但为研究漏洞,报告可能 ...

最新文章

  1. PYG教程【三】对Cora数据集进行半监督节点分类
  2. 大数据时代,如何才能提高自身竞争力?
  3. dz自动开起html,discuz论坛开启markdown 允许html代码嵌入js
  4. 数学的威力有多大?足以震慑全球......
  5. [导入]网页色彩搭配技巧
  6. python 取列表偶数和奇数位置的值
  7. opencv图像分析与处理(15)- 图像压缩中的编码方法:霍夫曼编码、Golomb编码、Rice编码、算术编码及其实现
  8. mysql ssd优化测试_MySQL服务器SSD性能问题分析与测试
  9. NLP系列——(2)特征提取
  10. 计算机专业游戏留学,启程:一个游戏专业留学生的第一周
  11. 方舟服务器在线人数查询软件,方舟生存进化怎么查看在线人数
  12. 7.6批量下载网易云歌曲
  13. Oracle 中递归查询
  14. Hdu 4090 GemAnd Prince (搜索_2010年北京区域赛)
  15. vba调用python代码_Python替代Excel Vba系列(终):vba中调用Python
  16. 大专生学Java,到底有没有出路
  17. 植物大战僵尸经典android,植物大战僵尸经典版
  18. csapp-lad1
  19. 西电数据挖掘实验1——二分网络上的链路预测
  20. APACHE-ATLAS-2.1.0简介(一)

热门文章

  1. Java Web 高性能开发,前端的高性能
  2. Visual Studio 2010/2013 UTF8编码调试时显示中文
  3. 8080处理器计算机启动
  4. sharepoint中一些gridview的使用方法
  5. Python学习笔记:常用第三方模块3
  6. 在.c文件中调用cuda函数
  7. 原:开辟内存时,可以以结构体为单位,这样测试,是对的吧?
  8. 计算机中位运算的一些性质与技巧
  9. NASA PHM数据集相关
  10. [云炬创业基础笔记]第二章创业者测试24