SWC智能合约漏洞库

在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

SWC-123/违反require要求

Solidity的require()语句旨在验证函数的外部输入。在大多数情况下,此类外部输入是由调用方提供的, 但也可以由被调用方返回。在前一种情况下,我们将其称为前提条件违规。违反要求存在两个可能的问题:

  • 提供外部输入的合约中存在错误。
  • 用于表达需求的条件太强而导致无法满足

CWE漏洞分类

CWE-573:调用方未正确遵循规范

整改方案

如果所需的逻辑条件太强,则应将其削弱以允许所有有效的外部输入。

否则,该错误必须在提供外部输入的合约中,并且应该考虑通过确保没有提供无效的输入来修正其代码。

参考文献

示例合约

require_simple.sol

pragma solidity ^0.4.25;

contract Bar {
    Foo private f = new Foo();
    function doubleBaz() public view returns (int256) {
        return 2 * f.baz(0);
    }
}

contract Foo {
    function baz(int256 x) public pure returns (int256) {
        require(0 < x);
        return 42;
    }
}

require_simple.yaml

description: Simple requirement violation
issues:
- id: SWC-123
  count: 1
  locations:
  - bytecode_offsets:
      '0xba541cbb2ac6dda7664b6ebdc6372297425c2eeabd88e0af5ca6310f9cf7bbd2':
      - 263
      '0x2dbd524f734f1b06b19cbefbbb5e84bbc6203a04bc930c26141c40bb8ecf467b':
      - 145
    line_numbers:
      requirement_simple.sol: [6, 12]

require_simple_fixed.sol

pragma solidity ^0.4.25;

contract Bar {
    Foo private f = new Foo();
    function doubleBaz() public view returns (int256) {
        return 2 * f.baz(1); //Changes the external contract to not hit the overly strong requirement.
    }
}

contract Foo {
    function baz(int256 x) public pure returns (int256) {
        require(0 < x); //You can also fix the contract by changing the input to the uint type and removing the require
        return 42;
    }
}

require_simple_fixed.yaml

description: Simple requirement violation
issues:
- id: SWC-123
  count: 0
  locations: []