Skip to content

Commit 5a8ddd6

Browse files
authored
Merge pull request #338 from crytic/update-cairo
Update cairo
2 parents 1fa8c5d + ed6da91 commit 5a8ddd6

File tree

12 files changed

+150
-286
lines changed

12 files changed

+150
-286
lines changed

not-so-smart-contracts/cairo/README.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@ Each _Not So Smart Contract_ consists of a standard set of information:
1414

1515
## Vulnerabilities
1616

17-
| Not So Smart Contract | Description |
18-
| ------------------------------------------------------------------------------ | ------------------------------------------------------------ |
19-
| [Improper access controls](access_controls) | Flawed access controls due to StarkNet account abstraction |
20-
| [Integer division errors](integer_division) | Unforeseen results from division in a finite field |
21-
| [View state modifications](view_state) | Lack of prevention for state modifications in view functions |
22-
| [Arithmetic overflow](arithmetic_overflow) | Insecure arithmetic in Cairo by default |
23-
| [Signature replays](replay_protection) | Necessary robust reuse protection due to account abstraction |
24-
| [L1 to L2 Address Conversion](L1_to_L2_address_conversion) | Essential L2 address checks for L1 to L2 messaging |
25-
| [Incorrect Felt Comparison](incorrect_felt_comparison) | Unexpected results from felt comparison |
26-
| [Namespace Storage Var Collision](namespace_storage_var_collision) | Storage variables unscoped by namespaces |
27-
| [Dangerous Public Imports in Libraries](dangerous_public_imports_in_libraries) | Ability to call nonimported external functions |
17+
| Not So Smart Contract | Description |
18+
| ---------------------------------------------------------------------------- | ------------------------------------------------------------ |
19+
| [Arithmetic overflow](arithmetic_overflow) | Insecure arithmetic in Cairo for the felt252 type |
20+
| [L1 to L2 Address Conversion](L1_to_L2_address_conversion) | Essential L2 address checks for L1 to L2 messaging |
21+
| [L1 to L2 message failure](l1_to_l2_message_failure) | Messages sent from L1 may not be processed by the sequencer |
22+
| [Overconstrained L1 <-> L2 interaction](overconstrained_l1_l2_interaction) | Asymmetrical checks on the L1 or L2 side can cause a DOS |
23+
| [Signature replays](replay_protection) | Necessary robust reuse protection due to account abstraction |
24+
| [Unchecked from address in L1 Handler](unchecked_from_address_in_l1_handler) | Access control issue when sending messages from L1 to L2 |
2825

2926
## Credits
3027

not-so-smart-contracts/cairo/access_controls/README.md

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
1-
# Arithmetic Overflow
1+
# Arithmetic Overflow with Felt Type
22

3-
The default primitive type, the field element (felt), behaves much like an integer in other languages, but there are a few important differences to keep in mind. A felt can be interpreted as a signed integer in the range (-P/2, P/2) or as an unsigned integer in the range (0, P]. P represents the prime used by Cairo, which is currently a 252-bit number. Arithmetic operations using felts are unchecked for overflow, which can lead to unexpected results if not properly accounted for. Since the range of values includes both negative and positive values, multiplying two positive numbers can result in a negative value and vice versa—multiplying two negative numbers does not always produce a positive result.
3+
The default primitive type, the field element (felt), behaves much like an integer in other languages, but there are a few important differences to keep in mind. A felt can be interpreted as an unsigned integer in the range [0, P], where P, a 252 bit prime, represents the order of the field used by Cairo. Arithmetic operations using felts are unchecked for overflow or underflow, which can lead to unexpected results if not properly accounted for. Do note that Cairo's builtin primitives for unsigned integers are overflow/underflow safe and will revert.
44

5-
StarkNet also provides the Uint256 module, offering developers a more typical 256-bit integer. However, the arithmetic provided by this module is also unchecked, so overflow is still a concern. For more robust integer support, consider using SafeUint256 from OpenZeppelin's Contracts for Cairo.
5+
## Example
66

7-
## Attack Scenarios
7+
The following simplified code highlights the risk of felt underflow. The `check_balance` function is used to validate if a user has a large enough balance. However, the check is faulty because passing an amount such that `amt > balance` will underflow and the check will be true.
88

9-
## Mitigations
9+
```Cairo
10+
11+
struct Storage {
12+
balances: LegacyMap<ContractAddress, felt252>
13+
}
14+
15+
fn check_balance(self: @ContractState, amt: felt252) {
16+
let caller = get_caller_address();
17+
let balance = self.balances.read(caller);
18+
assert(balance - amt >= 0);
19+
}
1020
11-
- Always add checks for overflow when working with felts or Uint256s directly.
12-
- Consider using the [OpenZeppelin Contracts for Cairo's SafeUint256 functions](https://github.com/OpenZeppelin/cairo-contracts/blob/main/src/openzeppelin/security/safemath/library.cairo) instead of performing arithmetic directly.
21+
```
22+
23+
## Mitigations
1324

14-
## Examples
25+
- Always add checks for overflow when working with felts directly.
26+
- Use the default signed integer types unless a felt is explicitly required.
27+
- Consider using Caracal, as it comes with a detector for checking potential overflows when doing felt252 arithmetic operaitons.

not-so-smart-contracts/cairo/dangerous_public_imports_in_libraries/README.md

Lines changed: 0 additions & 54 deletions
This file was deleted.

not-so-smart-contracts/cairo/incorrect_felt_comparison/README.md

Lines changed: 0 additions & 47 deletions
This file was deleted.

not-so-smart-contracts/cairo/integer_division/README.md

Lines changed: 0 additions & 39 deletions
This file was deleted.

not-so-smart-contracts/cairo/consider_L1_to_L2_message_failure.md renamed to not-so-smart-contracts/cairo/l1_to_l2_message_failure/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Consider L1 to L2 Message Failure
1+
# L1 to L2 Message Failure
22

33
In Starknet, Ethereum contracts can send messages from L1 to L2 using a bridge. However, it is not guaranteed that the message will be processed by the sequencer. For instance, a message can fail to be processed if there is a sudden spike in the gas price and the value provided is too low. To address this issue, Starknet developers have provided an API to cancel ongoing messages.
44

not-so-smart-contracts/cairo/namespace_storage_var_collision/README.md

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)