Skip to content

Commit

Permalink
fix: validate destination address cairo_message (#1339)
Browse files Browse the repository at this point in the history
<!--- Please provide a general summary of your changes in the title
above -->

<!-- Give an estimate of the time you spent on this PR in terms of work
days.
Did you spend 0.5 days on this PR or rather 2 days?  -->

Time spent on this PR:

## Pull request type

<!-- Please try to limit your pull request to one type,
submit multiple pull requests if needed. -->

Please check the type of change your PR introduces:

- [x] Bugfix
- [ ] Feature
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] Documentation content changes
- [ ] Other (please describe):

## What is the current behavior?

<!-- Please describe the current behavior that you are modifying,
or link to a relevant issue. -->

Resolves #1338

## What is the new behavior?

Destination address for cairo_message is checked to be well formatted

<!-- Reviewable:start -->
- - -
This change is [<img src="https://reviewable.io/review_button.svg"
height="34" align="absmiddle"
alt="Reviewable"/>](https://reviewable.io/reviews/kkrt-labs/kakarot/1339)
<!-- Reviewable:end -->
  • Loading branch information
obatirou authored Aug 19, 2024
1 parent 30c5486 commit 9dc1ce4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/kakarot/precompiles/kakarot_precompiles.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ namespace KakarotPrecompiles {
// [to_address: address][data_offset: uint256][data_len: uint256][data: bytes[]]

// Load target EVM address
let target_address = Helpers.bytes32_to_felt(input);
let invalid_address = Helpers.bytes_to_felt(12, input);
if (invalid_address != 0) {
let (revert_reason_len, revert_reason) = Errors.precompileInputError();
return (revert_reason_len, revert_reason, CAIRO_MESSAGE_GAS, TRUE);
}
let target_address = Helpers.bytes20_to_felt(input + 12);

let data_bytes_len = Helpers.bytes32_to_felt(input + 2 * 32);
let data_fits_in_input = is_nn(input_len - 3 * 32 - data_bytes_len);
Expand Down
8 changes: 8 additions & 0 deletions tests/src/kakarot/precompiles/test_precompiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,20 @@ def test__cairo_precompiles(
[],
True,
),
(
0x75002,
encode(["uint256", "bytes"], [2**161, encode(["uint128"], [0x2A])]),
0xC0DE,
[],
True,
),
],
ids=[
"ok_32_bytes_data",
"ok_1_bytes_data",
"ko_data_len_not_matching_actual_length",
"ko_input_too_short",
"ko_invalid_address",
],
)
class TestKakarotMessaging:
Expand Down

0 comments on commit 9dc1ce4

Please sign in to comment.