Lesson 3 - Integer Overflow
Last updated
Was this helpful?
Last updated
Was this helpful?
In this lesson you will learn how improperly handled integer overflows (or underflows) can yield vulnerabilities in your smart contract. Integer typed variables in ink! can overflow or underflow. This means that if they are assigned values outside of their bounds, they will wrap around. If this is not addressed carefully, this may allow an attacker to set values outside the bounds of what is expected by the smart contract author.
To be able to understand and complete this lesson, we believe that the minimum requirement should be the completion of . If you desire to be even more prepared please have a look at the official website of .
At the end of this lesson, you will be identify integer overflow/underflow vulnerabilities in smart contract code. You will also understand a few ways to prevent these attacks beyond explicit checks. These include: appropriate compiler configuration, safe math, and wrapping methods.
Bob develops a smart contract for as a simple token bank. The bank can be initialized with a specific quantity of tokens. Alice can then withdraw tokens into her personal account. His smart contract code can be found below.
==- Hint First, identify lines where mathematical operations occur. Then check for a possible overflow or underflow. ==- Answer The underflow vulnerability is on line 50.
==-
Did you succeed? Congratulations! If not, see the exploit below.
==- Reveal Exploit This test demonstrates an exploit that allows Alice to withdraw more tokens than Bob has made available.
==-
Luckily, there exist a secure way to implement the smart contract! Before revealing the solution, should take some time and try to secure the smart contract. Any success? Let’s reveal two possible solutions. ==- Solution 1 How can we fix the code so that we catch the underflow? We can use safe math that catches underflows.
Keep in mind, this costs additional computations throughout the code. ==-
If you want, you can verify that the solution is secure by replaying the attack described above.
Unfortunately, Bob did not follow the and this has for consequences that his contract is vulnerable. Do you see where the security vulnerability occurs?
Can you think about a way to exploit this vulnerability? You may download the contract here, deploy it on Aleph Zero , and try to attack it.
==- Solution 2 Set the compiler flags debug-assertion and overflow-checks to true. These can be set in for profile.release. This will ensure there will be a panic during runtime for overflows and underflows.
If you are up to the challenge, see if you know the answer to this question: ==- Reveal Question True or false: Exceeding the maximum value for an integer type will always panic. ==- Answers False. By default, debug builds will panic on overflow while release builds will not. This can be modified in Cargo.profile or by providing appropriate compiler flags. Even with overflow checking on, panics can be averted with saturating_, wrapping_, checked_, and overflowing_ methods. See in the the rust docs. ==-