Smart contract automatic detection tool "Chain must test", how to take you to unlock the Web3.0 world

After we released the new version of [Chain Bitest], a large number of developers have come to try it out. Today, we need to introduce this tool in detail.

【Chain Bitest】Smart contract automatic detection tool, which can be used to detect blockchain smart contract vulnerabilities. The platform simulates a separate test chain for each user. Users can independently deploy, test and verify smart contracts on the test chain. It is a comprehensive platform integrating smart contract development, testing and verification.

In the process of verification, the platform adopts formal verification and other technologies to model the execution environment, verify the security attributes through mathematical reasoning and other methods, find out the security problems that may occur when the contract is running, and assist contract developers to find Potential security risks, locate the location of the vulnerability, and enhance the security of the contract. It mainly includes four aspects of detection: code specification detection, standard specification detection, function call detection, and business logic security detection.

The most indispensable thing in the Web3.0 world is smart contracts. Today, follow us to learn this smart contract automatic detection tool and unlock the Web3.0 world together.

ONE code specification detection

1. Memory ABIEncoderV2 array

Level: ERROR

Description: There is a BUG in the solc compiler of version 0.4.7-0.5.9. This BUG will cause the abi.encode interface to generate wrong results when processing multi-dimensional arrays.

sample

When the compiler version is 0.5.9, the return value of the nested array badArr is wrong, which is [[1, 2], [2, 3], [3, 4]]. When the compiler version is 0.6.7, the return value of the nested array badArr is correct, which is [[1, 2], [3, 4], [5, 6]].

Fix suggestions: Avoid using 0.4.7-0.5.9 compilers, or disable the abi.encode interface in 0.4.7-0.5.9 compilers.

2. Multiple constructors

Level: ERROR

Description: In version 0.4.22 of the compiler, the contract allows the contract to have constructors in two formats at the same time (declaring the constructor with the constructor keyword, or declaring the function with the contract name). So the variables in the constructor are in danger of overwriting each other.

sample

In constructor(), x is initialized to 1, while in Test(), x is initialized to 2. The constructor defined later will be invalid, so x will eventually be initialized to 1.

Suggested fix: use only one constructor.

3. Public mapping nested structure variables

Level: ERROR

Description: Public variables have a default read-only getter function, but public mapping nested reference structures will result in illegal getter functions.

sample

The public mappingm nests the reference type struct, which will cause the default read method m[1].a for public variables to fail.

Fix suggestions: Avoid using public mapping nested reference types, or use pragma experimental ABIEncoderV2.

4. Read and write control characters from right to left

Level: ERROR

Description: Unicode [U+202E] forces compilers to read right-to-left, as opposed to normal, and may mislead users.

sample

The _f function expects the input values ​​i, j, and m to be passed to a, b, and c in sequence. Due to the existence of U+202E, the input function needs to be given in the order of j, i, and m.

Fix suggestion: Avoid using U+202E characters.

5. State variable coverage

Level: ERROR

Description: The inheritance of contracts includes the inheritance of state variables. Overloading the state variables of the base class contract in the sub-contract may cause logic errors in the use of variables.

sample

The contract Test is a sub-contract of Base, and the definition of a in Test overloads the state variable a in Base. Calling f1() will return a in Base, and calling f2() will return a in Test.

Fix suggestion: Avoid overloading base class contract state variables.

6. Uninitialized storage variable

Level: ERROR

Description: The address of the uninitialized storage state variable will point to the address of the first state variable, using it may cause data overwriting or data loss.

sample

The storage pointer of the uninitialized variable st will point to the state variable a, and the assignment of st.b will overwrite the variable a so that the value of a becomes 2.

Repair suggestion: Initialize the storage local variable before using it, or use the memory local variable instead.

7. Constant function changes state

Level: WARNING

Description: Before Solidity 0.5.0, its mutability was defined as a constant/prue/view function, but the statement in the function body was changed. Such a function can be compiled, but only a warning is reported, and further calls to the function will fail. This problem has been fixed in versions after 0.5.0, and the state modification implemented in the constant function cannot be compiled.

sample

The variable a changes its value in the f() function, but the a() function is marked as view. So calling f() doesn't change a.

Suggested fix: Ensure mutability is correct for pre-Solidity 0.5.0 contracts.

8. Delete the structure containing mapping

Level: WARNING

Description: When using delete to reset the struct containing the mapping, the mapping in the struct will not be reset, which may lead to subsequent logic errors.

sample

The contract instantiates a struct a after initialization, and initializes ai to 10 and aj[10] to 100. Using delete a in f1 resets structure a. Data can be read in f2(), and the result is that the variable ai has been reset to 0, but the data in aj[10] is still 100.

Repair suggestion: Avoid using delete to reset the struct containing mapping.

9. Return value mismatch

Level: WARNING

Description: The return name and type are declared in the returns statement, but the actual return value does not match the variable in the declaration.

sample

The function f() defines a return type and name as uint a, and the return statement directly returns 100, which does not match the return statement.

Suggested fix: Make sure the value in the return statement matches the return statement in the returns statement.

10. Reuse base class constructors

Level: WARNING

Description: Inheritance is allowed between contracts, and child contracts inherit the state variables, functions, and constructors of the parent contract. Constructors may be reused multiple times when subcontracts inherit multiple constructors.

sample

Both Test1 and Test2 are sub-contracts that inherit the contract test, and reuse the constructor to initialize their respective state variables a to 1 and 2. The contract Test3 inherits the contracts Test1 and Test2, so it has two different constructors, resulting in Test3 The state variable a is assigned multiple times, and finally assigned a value of 2.

Suggested fix: Make sure subcontracts have uniquely inherited constructors.

TWO standard specification testing

1. Unchecked transfer operations

Level: ERROR

Description: When the contract defines the transfer/transferFrom interface of the ERC20 standard, it is necessary to check the return value of the transfer/transferFrom interface, otherwise it will lead to an error in the judgment of the transfer status.

sample

The return value of token.transferFrom(msg.sender, address(this), amount); needs to be checked.

Repair suggestion: Verify the results of all transfer functions.

2. Wrong ERC20 interface

Level: WARNING

Description: When defining the standard ERC20 interface, it is not completely consistent with the standard ERC20 interface.

sample

The Transfer event of the ERC20 standard is event Transfer(address indexed from, address indexed to, uint256 value);.

Repair suggestion: set ERC20 events and interfaces completely according to ERC20 standards.

3. Wrong ERC721 interface

Level: WARNING

Description: The defined standard ERC721 interface is not exactly the same as the standard ERC721 interface.

sample

The function ownerOf(uint256 tokenId) is the interface of ERC721, but it lacks parameters or return value.

Repair suggestion: compare with ERC721

THREE function call detection

1 Controlled proxy calls

Level: ERROR

Description: Delegate call is a way to call a contract. The operation space of the delegate call is on the side where the call is initiated, so calls without any permission control or unknown call address can be hacked.

sample

addr can be manipulated arbitrarily by the caller.

Repair suggestion: Set permission control for the function where the delegatecall is executed, and specify the caller.

2. Unchecked underlying calls

Level: ERROR

Description: The underlying call of the smart contract has return data. Failure to execute the call contract will not cause the execution failure of the call initiation contract. If the call operation fails and the return value is not checked, it may cause a difference between the expected logic and the actual state.

sample

address(f).call(abi.encodePacked(function_selector)); realizes the call to the function f() in the contract Base. However, the use of call does not verify the return value of the call, which will result in the inability to judge the call status.

Repair suggestion: Verify the return value of all underlying call methods.

3. Unchecked send method

Level: ERROR

Description: The transfer function send of the smart contract has a return value. If the transfer fails, the code will continue to execute, and the call will not roll back the state. Therefore, you should check the return value when using send to transfer money, and use this to determine whether the transfer is successful.

sample

The function f uses send to transfer ether. Since the return value of send is not verified, it is impossible to know whether the transfer is really successful.

Repair suggestion: When using send to transfer money, verify the return value.

4. Bottom call

Level: INFO

Description: Using low-level calls is risky. Low-level calls do not check for code existence or call success.

Repair suggestion: avoid low-level calls

FOUR business logic security detection

1. The transfer address is unknown

Level: ERROR

Description: The transfer function does not add any permission restrictions, and the transfer recipient can be set, and anyone can obtain the contract funds.

sample

The function f() does not have any permission control, and the recipient of the transfer is msg.sender. Call f() to get all funds of the contract.

Repair suggestion: When the contract has an external transfer function, add the correct permission control to the transfer function included.

2. Modification of dynamic array length

Level: ERROR

Description: Below solc version 0.6.0, the length information of the dynamic array type can be directly modified, and the change of the length information will directly affect the stored array data.

sample

After the contract is deployed, the 20th bit of data a[20] in the dynamic array a is 1. If f(10) is called to modify the length of a to 10, the value pointed to by a[20] will be lost.

Repair suggestion: Avoid direct or indirect modification of the length of the dynamic array.

3. Use enumerations sparingly

Level: ERROR

Description: Before version 0.4.5, calls of enumeration types will not perform overflow judgment.

sample

E is an enum type with length 3, even if it tries to read the 10th of E, the bug() function will not recover.

Repair suggestions: Avoid using the solc compiler of version 0.4.0-0.4.4, or perform interval judgment on enumeration values.

4. Contract to lock ETH

Level: ERROR

Description: In the smart contract, there is a function to receive ether currency, but there is no function to issue currency, which will cause the ether currency to be locked in the contract.

sample

The function f() has a payable symbol, but the contract has no ability to spend/transfer ether.

Repair suggestion: remove the payable attribute of the money receiving function, or add a function that can consume Ether/transfer Ether outward.

5. Wrong decorator

Level: ERROR

Description: The decorator acts as a state/permission control. If the _; code segment cannot be reached in the decorator, the function cannot be executed and a logic error will occur.

sample

Modifier bug1() has an if statement, when bool_test is false, _; will not reach, then function use() will not be used.

Repair suggestion: ensure that the decorator can reach the _; code segment, and execute the decorator function correctly.

6. Missing return value

Level: ERROR

Description: There is a return value in the function return statement, but there is no corresponding return implementation.

sample

The function f() is declared to return a value of type uint, and the contract lacks the return keyword in the function body, which will result in returning 0 (the minimum value of type uint).

Repair suggestions: add the corresponding return value or delete the return statement.

7. Re-entrancy risk

Level: ERROR

Description: One of the main dangers of calling external contracts is that they can take over control flow. In a reentrancy attack (aka recursive call attack), the malicious contract calls back to the calling contract before the first call to the function has completed. This can cause different invocations of the function to interact in undesired ways. Changing key state variables after a call is prone to reentrancy hazards.

sample

After the function f() judges the amount owned by the address, the call is used to send the ether, and finally the storage variable book changes after the transfer operation. Therefore, an attacker can call f() in a loop to withdraw ether.

Repair suggestion: Use check-validate-interactive mode to avoid re-entry attacks.

8. Contract self-destruct function

Level: ERROR

Description: The contract contains a self-destruct function and does not use any authentication, which will make the contract in an unstable state.

sample

Anyone can destroy the contract and withdraw the funds in the contract by calling f().

Repair suggestions: Try to avoid using self-destruct functions, or add correct permission control.

9. There is an uninitialized function pointer in the constructor

Level: ERROR

Description: There are uninitialized function pointers in the constructor of the contract, calling these pointers directly will cause an error.

sample

f is a function pointer in constructor, and before the function pointer is fully realized, it is called. This behavior causes the deployment to fail.

Fix suggestion: Do not call a function pointer until the function pointer is fully materialized.

10. Uninitialized state variables

Level: ERROR

Description: Use of uninitialized state variables could lead to logic errors.

sample

State variable a is uninitialized and will be defaulted to address 0. When the transfer operation is performed, the ether will be lost (the transfer goes to the 0x0 address).

Fix suggestion: Initialize the state variable when declaring the state variable as much as possible.

11. Hackable upgrade contracts

Level: ERROR

Description: The contract contains a self-destruct function, and the initialization function can be called by anyone.

sample

The function initialize() will initialize the contract, but anyone can call it. If the attacker calls initialize() before the owner, the attacker can call kill() at any time to invalidate the function of the proxy contract.

Repair suggestion: execute the initialization function function in the constructor in the contract to ensure that the owner cannot be modified arbitrarily.

12. Assertion errors

Level: ERROR

Description: The constraints of assert must be met.

Repair suggestion: Please check the code logic to find the problem and fix it.

13. Integer overflow

Level: ERROR

Description: Overflow means that the result of an operation exceeds the upper limit that the result type can represent.

Repair suggestion: Please add overflow judgment or use SafeMath library for calculation.

14. Integer underflow

Level: ERROR

Description: Underflow indicates that the result in a computation operation exceeds the lower bound that can be represented by the result type.

Repair suggestions: Please add underflow judgment or use SafeMath

Only a few cases are selected above

View more security detection items

You can copy the following website to read

https://beosinofficial.gitbook.io/vaas-zhong-wen/

One of the most powerful "single products" of Chengdu Lian'an

Lianbitest v3.1 strong advanced

The first batch of applications has been opened

Add wx: qiuqiupapa520

Ask him to send you a trial link

Guess you like

Origin blog.csdn.net/CDLianan/article/details/124450479