智能合约中整数溢出和下溢漏洞

简介: 智能合约中整数溢出和下溢漏洞

整数溢出和下溢:

当数学运算的结果超出整数类型所能表示的范围时,会导致数值错误地回绕,这可以被攻击者利用来获取额外的代币或资源。

溢出示例

假设我们有一个智能合约,它接收用户存款并存储在一个变量中。如果用户尝试存入的金额加上现有的余额超出了整数的最大值(在Solidity中,uint256类型的最大值是2^256-1),就会发生溢出。

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract OverflowExample {
    uint256 public balance;
    function deposit(uint256 amount) public {
        balance += amount;
    }
    function getBalance() public view returns (uint256) {
        return balance;
    }
}

测试溢出

为了测试溢出,我们假设balance已经是uint256类型的最大值,再尝试存入任何正数,都将导致溢出,即结果将从最大值回绕到0。

// 假设balance已经是uint256的最大值
uint256 maxUint256 = type(uint256).max;
balance = maxUint256;
// 尝试存入任何正数都会导致溢出
deposit(1);
// 此时,balance将变为0

下溢示例

下溢通常发生在减法操作中,如果从一个较小的数中减去一个较大的数,结果将低于最小整数值(对于无符号整数,最小值是0),从而导致下溢。

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract UnderflowExample {
    uint256 public balance;
    function withdraw(uint256 amount) public {
        balance -= amount;
    }
    function getBalance() public view returns (uint256) {
        return balance;
    }
}

测试下溢

在无符号整数中,下溢实际上会导致值从0回绕到最大值,但这通常不是预期的行为,因此仍然被视为错误。

// 假设balance为0
balance = 0;
// 尝试取出任何正数都会导致下溢
withdraw(1);
// 此时,balance将变成uint256的最大值

解决方案

为了避免整数溢出和下溢,Solidity提供了安全数学库SafeMath,它包含了检查溢出和下溢的整数运算函数。自Solidity 0.8.0起,安全数学操作符checkedAdd, checkedSub, checkedMul, 和 checkedDiv被引入,可以自动检测并抛出异常。

using SafeMath for uint256;
function deposit(uint256 amount) public {
    balance = balance.checkedAdd(amount);
}
function withdraw(uint256 amount) public {
    balance = balance.checkedSub(amount);
}

这样,如果检测到溢出或下溢,Solidity将自动抛出异常,阻止交易执行,从而保护合约免受此类错误的影响。


目录
打赏
0
2
2
0
52
分享
相关文章
智能合约中短地址攻击(Short Address Attack)
智能合约中短地址攻击(Short Address Attack)
186 7
postman接口测试工具详解
postman接口测试工具详解
257 7
论系统的木桶理论与性能瓶颈
论系统的木桶理论与性能瓶颈
97 7
|
12月前
外包公司中能学到技术的都是那些人?
外包公司中能学到技术的都是那些人?
121 7
跨链桥中双向桥、单向桥、中继桥或侧链桥区别
跨链桥中双向桥、单向桥、中继桥或侧链桥区别
145 7
如何进行智能合约的安全测试
如何进行智能合约的安全测试
143 7
智能合约中Gas限制和DoS攻击漏洞
智能合约中Gas限制和DoS攻击漏洞
130 7
智能合约中时间依赖漏洞
智能合约中时间依赖漏洞
234 6
|
12月前
|
max code size exceeded
max code size exceeded
124 6
WBTC与BTC的主要区别
WBTC与BTC的主要区别
582 6
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等