Scribble
Search…
Property Checking with Scribble and Mythril

Intro

In this tutorial we will learn how to:
  • start using scribble
  • add a custom property to an ERC20 token contract
  • look for violations of our custom property with mythril
Without further ado...

Dependencies

Make sure that you have the following installed:
  • python3 and pip3
  • nodejs version 12.x and npm.

Setup

To get setup you first need to install scribble:
1
npm install -g eth-scribble
Copied!
Next install mythril:
1
pip3 install -U mythril
Copied!
Now we are ready to begin!

Background

Before we begin, we should clarify what we mean by 'custom properties'. Custom properties in general can be any predicate you have in mind about your code. For example, a custom property can be:
  • Only the current owner of the contract may change the owner state variable
  • My contract will allow trades only after the initialize()function was called
  • The number of minted tokens only increases
  • etc..
In order to be able to reason about these properties formally however, we need to translate their English statements into a formal language that automated tools understand. We will show how to do this using the Scribble spec language. Note that the language choice imposes certain limitations on what can be expressed. We are actively working on adding more expressive power to Scribble.

Our example

For this tutorial we will consider a simple ERC20 token, and annotate it with a custom property expressed in the Scribble language. Take the code below and save it as Token.sol in a directory of your choice:
Token.sol
1
pragma solidity ^0.6.0;
2
3
contract ERC20 {
4
uint256 private _totalSupply;
5
mapping (address => uint256) private _balances;
6
mapping (address => mapping (address => uint256)) private _allowances;
7
8
constructor() public {
9
_totalSupply = 1000000;
10
_balances[msg.sender] = 1000000;
11
}
12
13
function totalSupply() external view returns (uint256) {
14
return _totalSupply;
15
}
16
17
function balanceOf(address _owner) external view returns (uint256) {
18
return _balances[_owner];
19
}
20
21
function allowance(address _owner, address _spender) external view returns (uint256) {
22
return _allowances[_owner][_spender];
23
}
24
25
function transfer(address _to, uint256 _value) external returns (bool) {
26
address from = msg.sender;
27
require(_value <= _balances[from]);
28
29
30
uint256 newBalanceFrom = _balances[from] - _value;
31
uint256 newBalanceTo = _balances[_to] + _value;
32
_balances[from] = newBalanceFrom;
33
_balances[_to] = newBalanceTo;
34
35
emit Transfer(msg.sender, _to, _value);
36
return true;
37
}
38
39
function approve(address _spender, uint256 _value) external returns (bool) {
40
address owner = msg.sender;
41
_allowances[owner][_spender] = _value;
42
emit Approval(owner, _spender, _value);
43
return true;
44
}
45
46
function transferFrom(address _from, address _to, uint256 _value) external returns (bool) {
47
uint256 allowed = _allowances[_from][msg.sender];
48
require(_value <= allowed);
49
require(_value <= _balances[_from]);
50
_balances[_from] -= _value;
51
_balances[_to] += _value;
52
_allowances[_from][msg.sender] -= _value;
53
emit Transfer(_from, _to, _value);
54
return true;
55
}
56
57
event Transfer(address indexed _from, address indexed _to, uint256 _value);
58
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
59
}
Copied!

Custom Properties

Lets consider the transfer function above.
There are several properties that could be specified over the transfer function:
  1. 1.
    If the transfer function succeeds then the recipient had sufficient balance at the start
  2. 2.
    If the transfer succeeds then the sender will have `_value` subtracted from it’s balance
  3. 3.
    If the transfer succeeds then the receiver will have `_value` added to it’s balance
  4. 4.
    If the transfer succeeds then the sum of the balances between the sender and receiver remains he same
For this example we’ll go with property #4;
💡 Try to write annotations for the other two properties after you’ve finished this getting started guide!
Formalizing the property
First things first, we need to dissect what the property actually entails. There are two main parts: if the transfer succeeds and the sum of the sender and receiver balances is the same before and after the transaction.
if the transfer succeeds This specifies when you expect something to hold. In the scribble specification language we use if_succeeds function annotations to check properties after successful termination of a function. Check out the specification docs for detailed information on the if_succeeds keyword.
the sum of the sender and receiver balances is the same before and after the transaction This is the boolean expression that we expect to hold. Note that this property introduces a statement that relates the initial and final state of the contract. That is, we want to know that the sum of the balances (_balances[sender] + _balances[receiver]) doesn’t change relative to the state before the transaction. To allow you to express such properties the old() is used. Check out the specification docs for more information on old.
Using this keyword we can formulate the following expression:
1
old(_balances[_to]) + old(_balances[msg.sender]) == _balances[_to] + _balances[msg.sender]
Copied!
Now we can combine the two to get to our specification:
1
if_succeeds {:msg "Transfer does not modify the sum of balances" } old(_balances[_to]) + old(_balances[msg.sender]) == _balances[_to] + _balances[msg.sender];
Copied!
Adding the annotation
if_succeeds annotations are function specifications, which in the Scribble specification language are placed above the function definitions using a docstring comment.
1
pragma solidity ^0.6.0;
2
3
contract ERC20 {
4
mapping (address => uint256) private _balances;
5
/// #if_succeeds {:msg "Transfer does not modify the sum of balances"} old(_balances[_to]) + old(_balances[msg.sender]) == _balances[_to] + _balances[msg.sender];
6
function transfer(address _to, uint256 _value) public returns (bool) {
7
// ...
8
}
9
}
Copied!

🔍 Analysis

To see if the property holds, you can run mythril from the command line as follows:
1
myth analyze Token.instrumented.sol -t 1 --execution-timeout 60
Copied!
The -t option tells mythril to explore only 1 transaction deep, and --execution-timeout specifies a timeout of 60 sec as the name implies. This should be sufficient, however if for some reason you don't see the expected failures, try a 2 minute timeout.
In about a minute mythril should return with two violations - an assertion violation and a user assertion violation:
1
==== Exception State ====
2
SWC ID: 110
3
Severity: Medium
4
Contract: ERC20
5
Function name: transfer(address,uint256)
6
PC address: 2014
7
Estimated Gas Usage: 18755 - 64531
8
A user-provided assertion failed.
9
A user-provided assertion failed with the message '0: Transfer does not modify the sum of balances'
10
--------------------
11
In file: Token.instrumented.sol:40
12
13
AssertionFailed("0: Transfer does not modify the sum of balances")
14
15
--------------------
16
Initial State:
17
18
Account: [CREATOR], balance: 0x40008290082002429, nonce:0, storage:{}
19
Account: [ATTACKER], balance: 0x0, nonce:0, storage:{}
20
Account: [SOMEGUY], balance: 0x0, nonce:0, storage:{}
21
22
Transaction Sequence:
23
24
Caller: [CREATOR], calldata: , value: 0x0
25
Caller: [CREATOR], function: transfer(address,uint256), txdata: 0xa9059cbb000000000000000000000000affeaffeaffeaffeaffeaffeaffeaffeaffeaffe0000000000000000000000000000000000000000000000000000000000004f00, value: 0x0
26
27
==== Exception State ====
28
SWC ID: 110
29
Severity: Medium
30
Contract: ERC20
31
Function name: transfer(address,uint256)
32
PC address: 2021
33
Estimated Gas Usage: 19521 - 65553
34
An assertion violation was triggered.
35
It is possible to trigger an assertion violation. Note that Solidity assert() statements should only be used to check invariants. Review the transaction trace generated for this issue and either make sure your program logic is correct, or use require() instead of assert() if your goal is to constrain user inputs or enforce preconditions. Remember to validate inputs from both callers (for instance, via passed arguments) and callees (for instance, via return values).
36
--------------------
37
In file: Token.instrumented.sol:41
38
39
assert(false)
40
41
--------------------
42
Initial State:
43
44
Account: [CREATOR], balance: 0x10820020411009202, nonce:0, storage:{}
45
Account: [ATTACKER], balance: 0x0, nonce:0, storage:{}
46
Account: [SOMEGUY], balance: 0x0, nonce:0, storage:{}
47
48
Transaction Sequence:
49
50
Caller: [CREATOR], calldata: , value: 0x0
51
Caller: [CREATOR], function: transfer(address,uint256), txdata: 0xa9059cbb000000000000000000000000affeaffeaffeaffeaffeaffeaffeaffeaffeaffe0000000000000000000000000000000000000000000000000000000000080000, value: 0x0
Copied!
And voila! We have found a counterexample to our property! Note that the first failure directly incudes the user-readable label from the assertion, which makes it easy to identify which property was violated if you have multiple:
1
A user-provided assertion failed with the message '0: Transfer does not modify the sum of balances'
Copied!
If you were to decode the transaction data:
1
txdata: 0xa9059cbb000000000000000000000000affeaffeaffeaffeaffeaffeaffeaffeaffeaffe0000000000000000000000000000000000000000000000000000000000004f00
Copied!
You would find that the recipient of the transfer is 0xaffeaffeaffeaffeaffeaffeaffeaffeaffeaffe which is the address of the CREATOR account in mythril. That however is also the same account that sent the transaction:
1
Caller: [CREATOR], function: transfer(address,uint256),....
Copied!
So the failure occurs when the sender and receiver of transfer are the same. We will leave it up to the reader to look at the code and figure out what goes wrong in this case.
Last modified 6mo ago