Skip to content

Commit

Permalink
Disallow truncated data section in toplevel containers
Browse files Browse the repository at this point in the history
  • Loading branch information
gumb0 committed Jun 5, 2024
1 parent 985269f commit f71b993
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
12 changes: 10 additions & 2 deletions lib/evmone/eof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,8 +654,14 @@ EOFValidationError validate_eof1(evmc_revision rev, bytes_view main_container) n
visited_code_sections.end())
return EOFValidationError::unreachable_code_sections;

if (referenced_by_eofcreate && !header.has_full_data(container.size()))
return EOFValidationError::eofcreate_with_truncated_container;
// Check if truncated data section is allowed.
if (!header.has_full_data(container.size()))
{
if (main_container == container)
return EOFValidationError::toplevel_container_truncated;
if (referenced_by_eofcreate)
return EOFValidationError::eofcreate_with_truncated_container;
}

// Enqueue subcontainers
for (size_t subcont_idx = 0; subcont_idx < subcontainer_count; ++subcont_idx)
Expand Down Expand Up @@ -929,6 +935,8 @@ std::string_view get_error_message(EOFValidationError err) noexcept
return "invalid_container_section_index";
case EOFValidationError::eofcreate_with_truncated_container:
return "eofcreate_with_truncated_container";
case EOFValidationError::toplevel_container_truncated:
return "toplevel_container_truncated";

Check warning on line 939 in lib/evmone/eof.cpp

View check run for this annotation

Codecov / codecov/patch

lib/evmone/eof.cpp#L938-L939

Added lines #L938 - L939 were not covered by tests
case EOFValidationError::impossible:
return "impossible";
}
Expand Down
1 change: 1 addition & 0 deletions lib/evmone/eof.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ enum class EOFValidationError
too_many_container_sections,
invalid_container_section_index,
eofcreate_with_truncated_container,
toplevel_container_truncated,

impossible,
};
Expand Down
2 changes: 2 additions & 0 deletions test/unittests/eof_validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ std::string_view get_tests_error_message(EOFValidationError err) noexcept
return "EOF_InvalidContainerSectionIndex";
case EOFValidationError::eofcreate_with_truncated_container:
return "EOF_EofCreateWithTruncatedContainer";
case EOFValidationError::toplevel_container_truncated:
return "EOF_ToplevelContainerTruncated";

Check warning on line 91 in test/unittests/eof_validation.cpp

View check run for this annotation

Codecov / codecov/patch

test/unittests/eof_validation.cpp#L90-L91

Added lines #L90 - L91 were not covered by tests
case EOFValidationError::impossible:
return "impossible";
}
Expand Down
15 changes: 10 additions & 5 deletions test/unittests/eof_validation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,14 @@ TEST_F(eof_validation, EOF1_truncated_section)
EOFValidationError::invalid_section_bodies_size);
add_test_case("EF0001 010004 0200010002 040000 00 00800000 FE",
EOFValidationError::invalid_section_bodies_size);
// Data section may be truncated
add_test_case("EF0001 010004 0200010001 040002 00 00800000 FE", EOFValidationError::success);
add_test_case("EF0001 010004 0200010001 040002 00 00800000 FE AA", EOFValidationError::success);

// Data section may be truncated in runtime subcontainer
add_test_case(
eof_bytecode(returncontract(0, 0, 2), 2).container(eof_bytecode(OP_INVALID).data("", 2)),
EOFValidationError::success);
add_test_case(
eof_bytecode(returncontract(0, 0, 1), 2).container(eof_bytecode(OP_INVALID).data("aa", 2)),
EOFValidationError::success);
}

TEST_F(eof_validation, EOF1_code_section_offset)
Expand Down Expand Up @@ -1016,9 +1021,9 @@ TEST_F(eof_validation, EOF1_embedded_container)
EOFValidationError::success);

// no data section in container, but anticipated aux_data
// data section is allowed to be truncated in runtime subcontainer
add_test_case(
"EF0001 010004 0200010006 0300010014 040002 00 00800001 6000E0000000 "
"EF000101000402000100010400000000800000FE",
eof_bytecode(returncontract(0, 0, 2), 2).container(eof_bytecode(OP_INVALID).data("", 2)),
EOFValidationError::success);

// with data section
Expand Down

0 comments on commit f71b993

Please sign in to comment.