-
Notifications
You must be signed in to change notification settings - Fork 609
fix!: multiple traces had ghost row injection vulnerabilities #19480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| #include "barretenberg/vm2/constraining/testing/check_relation.hpp" | ||
| #include "barretenberg/vm2/generated/relations/emit_unencrypted_log.hpp" | ||
| #include "barretenberg/vm2/generated/relations/lookups_emit_unencrypted_log.hpp" | ||
| #include "barretenberg/vm2/generated/relations/perms_emit_unencrypted_log.hpp" | ||
| #include "barretenberg/vm2/simulation/events/emit_unencrypted_log_event.hpp" | ||
| #include "barretenberg/vm2/simulation/events/event_emitter.hpp" | ||
| #include "barretenberg/vm2/simulation/events/gt_event.hpp" | ||
|
|
@@ -18,6 +19,7 @@ | |
| #include "barretenberg/vm2/testing/macros.hpp" | ||
| #include "barretenberg/vm2/testing/public_inputs_builder.hpp" | ||
| #include "barretenberg/vm2/tracegen/gt_trace.hpp" | ||
| #include "barretenberg/vm2/tracegen/memory_trace.hpp" | ||
| #include "barretenberg/vm2/tracegen/opcodes/emit_unencrypted_log_trace.hpp" | ||
| #include "barretenberg/vm2/tracegen/precomputed_trace.hpp" | ||
| #include "barretenberg/vm2/tracegen/public_inputs_trace.hpp" | ||
|
|
@@ -31,6 +33,8 @@ using simulation::EmitUnencryptedLogWriteEvent; | |
| using simulation::TrackedSideEffects; | ||
| using testing::PublicInputsBuilder; | ||
| using tracegen::EmitUnencryptedLogTraceBuilder; | ||
| using tracegen::MemoryTraceBuilder; | ||
| using tracegen::PrecomputedTraceBuilder; | ||
| using tracegen::PublicInputsTraceBuilder; | ||
| using tracegen::TestTraceContainer; | ||
| using FF = AvmFlavorSettings::FF; | ||
|
|
@@ -639,6 +643,93 @@ TEST(EmitUnencryptedLogConstrainingTest, NegativeContractAddressConsistency) | |
| "CONTRACT_ADDRESS_CONSISTENCY"); | ||
| } | ||
|
|
||
| // ===================================================================== | ||
| // Ghost Row Injection Vulnerability Tests | ||
| // ===================================================================== | ||
| // These tests verify that ghost rows (sel=0) cannot fire permutations. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we keep this one, I would welcome a little comment that it is more of a "defensive/sanity" measure because this is a "memory read" and therefore there is no known exploitability. |
||
| // This is a defensive/sanity check: even though the situation is hard to exploit, | ||
| // we still enforce the selector gating to prevent accidental ghost reads. | ||
| // The vulnerability: is_write_memory_value is only boolean-constrained, | ||
| // not constrained to be 0 when sel=0. This allows ghost rows to fire | ||
| // the #[READ_MEM] permutation via sel_should_read_memory. | ||
| // | ||
| // VULNERABILITY SUMMARY: | ||
| // - is_write_memory_value is only boolean-constrained | ||
| // - When sel=0, is_write_memory_value can still be set to 1 | ||
| // - This makes sel_should_read_memory = 1 (via derived constraint) | ||
| // - This fires the #[READ_MEM] permutation from a ghost row | ||
| // | ||
| // REQUIRED FIX: | ||
| // Gate by sel to avoid ghost rows triggering memory reads. | ||
| // sel_should_read_memory = sel * is_write_memory_value * (1 - error_out_of_bounds); | ||
|
|
||
| // This test verifies that the fix for the ghost row injection vulnerability works. | ||
| // The constraint `is_write_memory_value * (1 - sel) = 0` should prevent ghost rows | ||
| // from setting is_write_memory_value=1 when sel=0. | ||
| TEST(EmitUnencryptedLogConstrainingTest, NegativeGhostRowInjectionBlocked) | ||
| { | ||
| TestTraceContainer trace; | ||
| MemoryTraceBuilder memory_trace_builder; | ||
| PrecomputedTraceBuilder precomputed_trace_builder; | ||
|
|
||
| uint32_t malicious_clk = 42; | ||
| uint16_t malicious_space_id = 1; | ||
| MemoryAddress malicious_log_addr = 0xDEAD; | ||
| FF malicious_value = 0x1337; | ||
| MemoryTag malicious_tag = MemoryTag::FF; | ||
|
|
||
| std::vector<simulation::MemoryEvent> mem_events = { | ||
| { | ||
| .execution_clk = malicious_clk, | ||
| .mode = simulation::MemoryMode::READ, | ||
| .addr = malicious_log_addr, | ||
| .value = MemoryValue::from<FF>(malicious_value), | ||
| .space_id = malicious_space_id, | ||
| }, | ||
| }; | ||
|
|
||
| precomputed_trace_builder.process_sel_range_8(trace); | ||
| precomputed_trace_builder.process_sel_range_16(trace); | ||
| precomputed_trace_builder.process_misc(trace, 1 << 16); | ||
| precomputed_trace_builder.process_tag_parameters(trace); | ||
| memory_trace_builder.process(mem_events, trace); | ||
|
|
||
| uint32_t memory_row = 0; | ||
| for (uint32_t row = 0; row < trace.get_num_rows(); row++) { | ||
| if (trace.get(C::memory_sel, row) == 1) { | ||
| memory_row = row; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| // Attempt ghost row injection: sel=0 but is_write_memory_value=1 | ||
| uint32_t ghost_row = 0; | ||
| trace.set(ghost_row, | ||
| std::vector<std::pair<Column, FF>>{ | ||
| { C::precomputed_first_row, 1 }, | ||
| { C::precomputed_clk, ghost_row }, | ||
| { C::precomputed_zero, 0 }, | ||
| { C::emit_unencrypted_log_sel, 0 }, | ||
| { C::emit_unencrypted_log_is_write_memory_value, 1 }, | ||
| { C::emit_unencrypted_log_error_out_of_bounds, 0 }, | ||
| { C::emit_unencrypted_log_sel_should_read_memory, 1 }, | ||
| { C::emit_unencrypted_log_execution_clk, malicious_clk }, | ||
| { C::emit_unencrypted_log_space_id, malicious_space_id }, | ||
| { C::emit_unencrypted_log_log_address, malicious_log_addr }, | ||
| { C::emit_unencrypted_log_value, malicious_value }, | ||
| { C::emit_unencrypted_log_tag, static_cast<uint8_t>(malicious_tag) }, | ||
| { C::emit_unencrypted_log_public_inputs_value, malicious_value }, | ||
| }); | ||
|
|
||
| trace.set(C::memory_sel_unencrypted_log_read, memory_row, 1); | ||
|
|
||
| // The fix: sel_should_read_memory = sel * is_write_memory_value * (1 - error_out_of_bounds) | ||
| // Gating by sel should cause the relation check to fail | ||
| // because sel_should_read_memory=1 and sel=0 violates this constraint | ||
| EXPECT_THROW_WITH_MESSAGE(check_relation<emit_unencrypted_log>(trace), | ||
| "SEL_SHOULD_READ_MEMORY_IS_SEL_AND_WRITE_MEM_AND_NO_ERR"); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| } // namespace bb::avm2::constraining | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍