fix(bloom): guard contains and add against a zero-capacity filter #1635
while this test _does_ cover the target codepath, I think it's worth adding another specifically around the bincode deserialization that would most likely be hit on a live validator run
Requested changes.
Thanks for feedback!
Will create follow up commit to handle this.
Added deserialization tests at three layers (defense in depth):
Each panics in pos without the guard.
Still getting familiar with Sig's conventions, so if this is more coverage than you'd want here, let me know and I'll trim.
I'm just now realizing if self.keys is empty _and_ capacity is 0, this will return true, which seems like a pre-existing bug we may want to account for here now that we're checking the capacity
Currently the test ensures a panic didn't occur, but isn't ensuring this is functioning as intended. I think adding the assertion here would close the loop in terms of ensuring this test is full testing what it needs.
defer result.deinit();
try std.testing.expectEqual(@as(usize, 8), result.items.len);
some small nits, otherwise looks good!
Thanks, applied the fix.
Yeah, i missed the case when keys are empty.
Agave contains returns false when keys.isempty || bits.isempty.
Extended the guard to cover empty keys too.
Pull request overview
Fixes a remote crash vector in the Bloom filter used by gossip pull requests by preventing modulo-by-zero when a filter has keys but a zero-capacity bitset (reachable via wire encoding).
Changes:
Bloom.contains and Bloom.add when the bitset has zero capacity (and treat “no keys” as matching nothing).Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
| ---- | ----------- |
| src/bloom/bloom.zig | Adds zero-capacity (and empty-keys) guards to prevent divide-by-zero and asserts behavior via new tests. |
| src/gossip/pull_response.zig | Adds regression test ensuring pull response filtering won’t panic on a zero-capacity Bloom filter. |
| src/gossip/message.zig | Adds regression test ensuring a deserialized PullRequest with a zero-capacity filter does not crash and behaves safely. |
---
💡 <a href="/Syndica/sig/new/main?filename=.github/instructions/*.instructions.md" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Add Copilot custom instructions</a> for smarter, more guided reviews. <a href="https://docs.github.com/en/copilot/customizing-copilot/adding-repository-custom-instructions-for-github-copilot" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Learn how to get started</a>.
Approved these changes.
Approved these changes.
ajw221
valentynkit
Intent
Closes #1633. A gossip
PullRequestcan carry a bloom filter whosekeyslist is non-empty while its bit array has zero capacity. Building the pull response callsBloom.containson that filter, which reachespos:When
capacity()is zero this divides by zero, so one unauthenticated UDP packet aborts the node.The zero-capacity state is reachable from the wire.
BitVec(T)storesbitsas an optional slice with a separatelen, andkeysdeserializes independently, sokeys = [k]withbits = nullandlen = 0is a valid encoding. Agave avoids this because itsBloomchecksbits.is_empty()incontainsandaddand useschecked_reminpos; Sig'sBloomomits that guard.Implementation
Return early from
contains(false) andadd(no-op) when the filter has no bits, before any key is hashed. A zero-capacity filter has no set bits, so it matches nothing, which is whatfalsealready means on the response path. The guard goes incontainsandaddrather thanposbecause a guardedposwould still hand an index tobits.isSet, which assertsindex < bit_lengthon the empty bitset. Putting it in the primitive also covers every caller, not just the gossip path.Ramifications
The behavior change is limited to zero-capacity filters:
containsreturnsfalseandaddis a no-op instead of panicking. Filters that have capacity are untouched. The bug itself is a remote, unauthenticated, single-packet crash, with no amplification (the pull response is already bounded by the filter mask andmax_number_values) and no memory unsafety.A defense-in-depth option is to also reject such a filter in
GossipMessage.sanitize(the empty.PullRequestarm), as #1565 does for EpochSlots, which would drop the packet and count it as invalid. I kept this PR at the bloom layer to match Agave; I can add the sanitize check as well if you'd prefer.Tests
test "zero-capacity filter does not divide by zero"insrc/bloom/bloom.zigbuilds a filter withn_bits = 0and one key, then exercisescontainsandadd. It aborts inposwithout the guard and passes with it.