Repository navigation
match: Fix hash_search() duplicate-chain regression (#1115) and collision flood #1132
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
Open
seks99x
wants to merge
1
commit into
RsyncProject:master
Choose a base branch
from
seks99x:fix-hash-search-collisions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+142
−9
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| #!/usr/bin/env python3 | ||
| """Combined regression test for hash_search() weak-checksum collisions. | ||
|
|
||
| Tests both collision pathologies in a single transfer without wall-clock timers: | ||
|
|
||
| 1. Duplicate-chain unlinking (issue #1115): | ||
| The basis file contains 8 blocks of A followed by 1,500 identical blocks of B | ||
| that share the same weak checksum. Because B is added last, all 1,500 B blocks | ||
| sit in front of A on the hash chain (> MAX_CHAIN_LEN = 1024). Unlinking | ||
| duplicate false-alarm nodes on the first mismatch allows hash_search() to | ||
| reach and match all 8 blocks of A (verified via `Matched data`). | ||
|
|
||
| 2. Consecutive false-offset block skipping: | ||
| The source file then has 10 blocks of C, which also share the same weak | ||
| checksum but do not exist in the basis file. Rather than rolling 1 byte at | ||
| a time and computing a strong checksum at all ~6,337 byte offsets, skipping | ||
| a block after 4 consecutive false offsets keeps `hash_hits` bounded at O(N/B). | ||
| """ | ||
|
|
||
| import re | ||
|
|
||
| from rsyncfns import ( | ||
| FROMDIR, TODIR, assert_same, rmtree, run_rsync, test_fail, | ||
| ) | ||
|
|
||
| BLOCK = 704 # Valid block size (>= 700, multiple of 8) | ||
| N_MATCH = 8 # Blocks of A that must match behind the chain | ||
| N_DUP_CHAIN = 1500 # Duplicate B blocks in front of A (> MAX_CHAIN_LEN 1024) | ||
| N_FLOOD = 10 # Unmatched C blocks that collide on every byte offset | ||
| C_BYTE = 100 | ||
|
|
||
|
|
||
| def make_block(perturb_offset: int = -1) -> bytes: | ||
| b = bytearray([C_BYTE]) * BLOCK | ||
| if perturb_offset >= 0: | ||
| # (+1, -2, +1) preserves get_checksum1 (both s1 and s2) while | ||
| # producing a distinct strong checksum. | ||
| b[perturb_offset] = C_BYTE + 1 | ||
| b[perturb_offset + 1] = C_BYTE - 2 | ||
| b[perturb_offset + 2] = C_BYTE + 1 | ||
| return bytes(b) | ||
|
|
||
|
|
||
| rmtree(FROMDIR) | ||
| rmtree(TODIR) | ||
| FROMDIR.mkdir(parents=True, exist_ok=True) | ||
| TODIR.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| src = FROMDIR / 'image.bin' | ||
| dst = TODIR / 'image.bin' | ||
|
|
||
| block_a = make_block(-1) # Pure C_BYTE | ||
| block_b = make_block(0) # Perturbed at 0..2 (same sum1, different sum2) | ||
| block_c = make_block(4) # Perturbed at 4..6 (same sum1, different sum2) | ||
|
|
||
| # Basis: 8 blocks of A buried behind 1,500 duplicate blocks of B. | ||
| dst.write_bytes((block_a * N_MATCH) + (block_b * N_DUP_CHAIN)) | ||
|
|
||
| # Source: 8 blocks of A (should match!) + 10 blocks of C (unmatched collision flood). | ||
| src.write_bytes((block_a * N_MATCH) + (block_c * N_FLOOD)) | ||
|
|
||
| proc = run_rsync('-a', '-I', '--no-whole-file', f'--block-size={BLOCK}', | ||
| '--no-compress', '--stats', '--debug=deltasum1', | ||
| f'{src}', f'{dst}', capture_output=True) | ||
|
|
||
| out = proc.stdout + proc.stderr | ||
|
|
||
| m_delta = re.search(r'hash_hits=(\d+)\s+false_alarms=(\d+)', out) | ||
| m_match = re.search(r'Matched data:\s*([\d,]+)\s*bytes', out) | ||
| if not m_delta or not m_match: | ||
| test_fail(f"could not find deltasum/stats in rsync output:\n{out}") | ||
|
|
||
| hash_hits = int(m_delta.group(1)) | ||
| false_alarms = int(m_delta.group(2)) | ||
| matched_bytes = int(m_match.group(1).replace(',', '')) | ||
|
|
||
| expected_matched = N_MATCH * BLOCK # 5,632 bytes | ||
|
|
||
| # 1. Check issue #1115 (must reach and match all 8 blocks of A behind 1,500 B blocks) | ||
| if matched_bytes != expected_matched: | ||
| test_fail( | ||
| f"issue #1115 regression: matched {matched_bytes} bytes " | ||
| f"(expected {expected_matched}); valid blocks behind {N_DUP_CHAIN} " | ||
| f"duplicate chain entries were cut off by MAX_CHAIN_LEN") | ||
|
|
||
| # 2. Check consecutive false-offset block skip (must not roll 1 byte at a time across C) | ||
| # Without block skipping, hash_hits is ~6,345 (8 + 9*704 + 1). | ||
| # With block skipping after 4 consecutive misses, hash_hits is 21 (8 + 4 + 9). | ||
| MAX_EXPECTED_HITS = (N_MATCH + N_FLOOD) * 4 # 72 << 6,345 | ||
|
|
||
| if hash_hits > MAX_EXPECTED_HITS: | ||
| test_fail( | ||
| f"collision flood rolled 1 byte at a time: hash_hits={hash_hits} " | ||
| f"(expected <= {MAX_EXPECTED_HITS}, false_alarms={false_alarms})") | ||
|
|
||
| assert_same(dst, src, label='combined collision transfer') | ||
|
|
||
| print(f"collision test passed: matched={matched_bytes} bytes, " | ||
| f"hash_hits={hash_hits} (<= {MAX_EXPECTED_HITS}), " | ||
| f"false_alarms={false_alarms}") |
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.
Four misses do not prove that the next offsets cannot match. This skips valid basis matches and can substantially increase literal data. Split out the #1115 chain correction; any collision-flood policy needs explicit CPU and bandwidth bounds plus regressions with valid matches at each skip boundary. Also cover
--inplacesame-offset preservation when mutating the chain.