Skip to content

Commit 9dc0ff6

Browse files
authored
Bound aligned_i by s->count in hash_search() (--inplace) (#1133)
When updating_basis_file is active (--inplace), hash_search() advances aligned_offset by s->blength and increments the 32-bit signed counter aligned_i until aligned_offset catches up to offset. If the source file is larger than 2 GiB and the peer supplies a small s->blength and s->count, continuing to increment aligned_i past s->count serves no purpose (as no basis blocks exist beyond s->count - 1) and can overflow INT32_MAX into a negative index that bypasses the one-sided aligned_i < s->count check. Stop the catch-up loop once aligned_i reaches s->count so aligned_i remains strictly within [0, s->count] and cannot overflow. Reported-by: Mominul Islam Hemal <mominulislam-2021411216@cs.du.ac.bd>
1 parent b70a61f commit 9dc0ff6

2 files changed

Lines changed: 191 additions & 1 deletion

File tree

‎match.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ static void hash_search(int f,struct sum_struct *s,
277277
* the adjacent want_i optimization. */
278278
if (updating_basis_file) {
279279
/* All the generator's chunks start at blength boundaries. */
280-
while (aligned_offset < offset) {
280+
while (aligned_offset < offset && aligned_i < s->count) {
281281
aligned_offset += s->blength;
282282
aligned_i++;
283283
}

‎testsuite/aligned_overflow_test.py‎

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
#!/usr/bin/env python3
2+
"""aligned_i overflow in hash_search() (--inplace > 2 GiB). Reported by Mominul Hemal
3+
4+
Verifies that when `aligned_offset < offset` advances `aligned_i` in
5+
hash_search(), `aligned_i` is bounded by `s->count` and cannot overflow a
6+
32-bit signed integer on files > 2 GiB.
7+
"""
8+
9+
import socket
10+
import struct
11+
import subprocess
12+
import threading
13+
14+
from rsyncfns import FROMDIR, TODIR, TOOLDIR, rmtree, test_fail
15+
16+
RSYNC_BIN = str(TOOLDIR / 'rsync')
17+
18+
MPLEX_BASE = 7
19+
MSG_DATA = 0
20+
NDX_DONE = -1
21+
ITEM_TRANSFER = 1 << 15
22+
23+
24+
def w_shortint(x: int) -> bytes:
25+
return struct.pack('<H', x & 0xFFFF)
26+
27+
28+
def w_int(x: int) -> bytes:
29+
return struct.pack('<i', x & 0xFFFFFFFF if x >= 0 else x)
30+
31+
32+
def w_varint(x: int) -> bytes:
33+
if x < 0:
34+
x = (1 << 32) + x
35+
b = bytearray()
36+
while True:
37+
b.append(x & 0x7F)
38+
x >>= 7
39+
if x == 0:
40+
break
41+
b[-1] |= 0x80
42+
return bytes(b)
43+
44+
45+
def w_sum_head(count: int, blength: int, s2length: int, remainder: int) -> bytes:
46+
return w_int(count) + w_int(blength) + w_int(s2length) + w_int(remainder)
47+
48+
49+
def get_checksum1(buf: bytes) -> int:
50+
sb = [c - 256 if c >= 128 else c for c in buf]
51+
s1 = s2 = 0
52+
for val in sb:
53+
s1 = (s1 + val) & 0xFFFFFFFF
54+
s2 = (s2 + s1) & 0xFFFFFFFF
55+
return ((s1 & 0xFFFF) + ((s2 & 0xFFFF) << 16)) & 0xFFFFFFFF
56+
57+
58+
class MinimalDaemonReceiver:
59+
def __init__(self, sock: socket.socket):
60+
self.sock = sock
61+
self._ndx_prev_positive = -1
62+
63+
def _readline(self) -> str:
64+
line = bytearray()
65+
while not line.endswith(b'\n'):
66+
c = self.sock.recv(1)
67+
if not c:
68+
raise IOError("EOF reading line")
69+
line += c
70+
return line.decode('latin-1').rstrip('\n')
71+
72+
def handshake(self):
73+
self.sock.sendall(b"@RSYNCD: 30.0\n")
74+
self._readline() # client greeting
75+
self._readline() # module name
76+
self.sock.sendall(b"@RSYNCD: OK\n")
77+
self.sock.sendall(w_varint(0) + w_int(0))
78+
79+
def send_data(self, payload: bytes):
80+
hdr = struct.pack('<I', ((MPLEX_BASE + MSG_DATA) << 24) | len(payload))
81+
self.sock.sendall(hdr + payload)
82+
83+
def w_ndx(self, ndx: int) -> bytes:
84+
if ndx == NDX_DONE:
85+
return b'\x00'
86+
diff = ndx - self._ndx_prev_positive
87+
self._ndx_prev_positive = ndx
88+
return bytes([diff])
89+
90+
91+
BIG_SIZE = (1 << 31) + 64 # 2 GiB + 64 bytes
92+
NEEDLE_OFFSET = (1 << 31) + 8 # Matching byte past 2^31
93+
NEEDLE_BYTE = b'\x01'
94+
95+
rmtree(FROMDIR)
96+
rmtree(TODIR)
97+
FROMDIR.mkdir(parents=True, exist_ok=True)
98+
TODIR.mkdir(parents=True, exist_ok=True)
99+
100+
bigfile = FROMDIR / 'bigfile.bin'
101+
out_log = TODIR / 'client.log'
102+
103+
with open(bigfile, 'wb') as f:
104+
f.truncate(BIG_SIZE)
105+
f.seek(0)
106+
f.write(NEEDLE_BYTE)
107+
f.seek(NEEDLE_OFFSET)
108+
f.write(NEEDLE_BYTE)
109+
110+
lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
111+
lsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
112+
lsock.bind(('127.0.0.1', 0))
113+
port = lsock.getsockname()[1]
114+
lsock.listen(1)
115+
lsock.settimeout(30)
116+
117+
csock = None
118+
t = None
119+
rc = None
120+
logf = open(out_log, 'wb')
121+
proc = subprocess.Popen(
122+
[
123+
RSYNC_BIN, '-a', '--inplace', '--debug=deltasum1',
124+
str(bigfile), f'rsync://127.0.0.1:{port}/mod/',
125+
],
126+
stdout=logf,
127+
stderr=logf,
128+
)
129+
130+
try:
131+
csock, _ = lsock.accept()
132+
receiver = MinimalDaemonReceiver(csock)
133+
receiver.handshake()
134+
135+
def drain_stream():
136+
try:
137+
csock.settimeout(90)
138+
while csock.recv(1 << 20):
139+
pass
140+
except OSError:
141+
pass
142+
143+
t = threading.Thread(target=drain_stream, daemon=True)
144+
t.start()
145+
146+
sum1 = get_checksum1(NEEDLE_BYTE)
147+
# Send transfer request followed by 4 NDX_DONEs (phase 1, phase 2,
148+
# end of send_files, and final goodbye) so the sender exits cleanly
149+
# as soon as hash_search() completes.
150+
buf = (
151+
receiver.w_ndx(0)
152+
+ w_shortint(ITEM_TRANSFER)
153+
+ w_sum_head(1, 1, 0, 0)
154+
+ w_int(sum1)
155+
+ (receiver.w_ndx(NDX_DONE) * 4)
156+
)
157+
receiver.send_data(buf)
158+
159+
rc = proc.wait(timeout=120)
160+
t.join(timeout=5)
161+
finally:
162+
if csock is not None:
163+
try:
164+
csock.close()
165+
except OSError:
166+
pass
167+
lsock.close()
168+
if proc.poll() is None:
169+
proc.terminate()
170+
try:
171+
proc.wait(timeout=5)
172+
except subprocess.TimeoutExpired:
173+
proc.kill()
174+
proc.wait()
175+
if t is not None:
176+
t.join(timeout=5)
177+
logf.close()
178+
bigfile.unlink(missing_ok=True)
179+
180+
out_text = out_log.read_text(encoding='utf-8', errors='replace')
181+
print(f"sender exited with rc={rc}", flush=True)
182+
print(out_text[-1000:], flush=True)
183+
184+
if rc != 0 or 'total: matches=' not in out_text:
185+
test_fail(
186+
f"sender crashed or did not complete hash_search() past 2 GiB "
187+
f"(rc={rc}):\n{out_text[-1000:]}"
188+
)
189+
190+
print("aligned_overflow test passed: completed >2 GiB hash_search() cleanly", flush=True)

0 commit comments

Comments
 (0)