| 1 |
|
| 2 |
"""Regression test for issue #231: high-numbered I/O descriptors. |
| 3 |
|
| 4 |
rsync's I/O readiness loops used to call select() with fd_set bitmaps, which can |
| 5 |
only represent descriptors below FD_SETSIZE. If rsync is started with many |
| 6 |
descriptors already open -- e.g. inherited from a parent that leaked fds -- its |
| 7 |
own socket and pipe fds get allocated at or above that limit. FD_SET() and |
| 8 |
FD_ISSET() then index past the end of the fixed-size fd_set, which is undefined |
| 9 |
behaviour: with a plain build select() reports the fd ready while FD_ISSET() |
| 10 |
reads the out-of-bounds bit as 0, so the read/write never happens and rsync |
| 11 |
spins at 100% CPU forever; with a fortified libc the process instead aborts |
| 12 |
("bit out of range 0 - FD_SETSIZE in fd_set"). Either way the transfer never |
| 13 |
completes. |
| 14 |
|
| 15 |
We reproduce that by opening enough inheritable dummy fds to push rsync's |
| 16 |
descriptors past FD_SETSIZE, then running an ordinary transfer with |
| 17 |
close_fds=False so the child inherits them. With the poll()-based I/O the |
| 18 |
transfer finishes immediately; without it, it hangs or aborts. |
| 19 |
|
| 20 |
FD_SETSIZE is *not* hardcoded: it is 1024 with glibc but 65536 on 64-bit |
| 21 |
Solaris, and assuming 1024 there would open too few fds to cross the limit and |
| 22 |
pass vacuously. We ask the C library for the real value instead, and skip when |
| 23 |
we cannot (no compiler, or the fd limit cannot be raised far enough). |
| 24 |
""" |
| 25 |
|
| 26 |
import os |
| 27 |
import resource |
| 28 |
import shlex |
| 29 |
import shutil |
| 30 |
import subprocess |
| 31 |
import tempfile |
| 32 |
|
| 33 |
from rsyncfns import ( |
| 34 |
FROMDIR, TODIR, rmtree, rsync_argv, test_fail, test_skipped, |
| 35 |
) |
| 36 |
|
| 37 |
TIMEOUT = 30 |
| 38 |
|
| 39 |
|
| 40 |
def probe_fd_setsize(): |
| 41 |
"""Ask the C library for FD_SETSIZE rather than assuming a value.""" |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
cc = shlex.split(os.environ.get('CC') or '') |
| 46 |
if not cc: |
| 47 |
found = shutil.which('cc') or shutil.which('gcc') |
| 48 |
if not found: |
| 49 |
return None |
| 50 |
cc = [found] |
| 51 |
with tempfile.TemporaryDirectory() as td: |
| 52 |
src = os.path.join(td, 'fdss.c') |
| 53 |
exe = os.path.join(td, 'fdss') |
| 54 |
with open(src, 'w') as f: |
| 55 |
f.write('#include <stdio.h>\n' |
| 56 |
'#include <sys/select.h>\n' |
| 57 |
'int main(void){ printf("%d\\n", (int)FD_SETSIZE); return 0; }\n') |
| 58 |
try: |
| 59 |
if subprocess.run(cc + [src, '-o', exe], |
| 60 |
capture_output=True).returncode != 0: |
| 61 |
return None |
| 62 |
proc = subprocess.run([exe], capture_output=True, text=True) |
| 63 |
except OSError: |
| 64 |
return None |
| 65 |
if proc.returncode != 0: |
| 66 |
return None |
| 67 |
try: |
| 68 |
return int(proc.stdout.strip()) |
| 69 |
except ValueError: |
| 70 |
return None |
| 71 |
|
| 72 |
|
| 73 |
fd_setsize = probe_fd_setsize() |
| 74 |
if not fd_setsize: |
| 75 |
test_skipped("could not determine FD_SETSIZE (no usable C compiler)") |
| 76 |
|
| 77 |
|
| 78 |
ndummy = fd_setsize + 80 |
| 79 |
want = ndummy + 64 |
| 80 |
|
| 81 |
soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) |
| 82 |
if soft < want: |
| 83 |
if hard != resource.RLIM_INFINITY and hard < want: |
| 84 |
test_skipped(f"RLIMIT_NOFILE hard cap {hard} < {want}; cannot place fds " |
| 85 |
f"above FD_SETSIZE ({fd_setsize}) to exercise issue #231") |
| 86 |
resource.setrlimit(resource.RLIMIT_NOFILE, (want, hard)) |
| 87 |
|
| 88 |
rmtree(FROMDIR) |
| 89 |
rmtree(TODIR) |
| 90 |
FROMDIR.mkdir(parents=True, exist_ok=True) |
| 91 |
TODIR.mkdir(parents=True, exist_ok=True) |
| 92 |
|
| 93 |
payload = {f'f{i}': os.urandom(1000) for i in range(20)} |
| 94 |
for name, data in payload.items(): |
| 95 |
(FROMDIR / name).write_bytes(data) |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
dummies = [] |
| 101 |
try: |
| 102 |
while True: |
| 103 |
fd = os.open(os.devnull, os.O_RDONLY) |
| 104 |
os.set_inheritable(fd, True) |
| 105 |
dummies.append(fd) |
| 106 |
if fd >= ndummy: |
| 107 |
break |
| 108 |
|
| 109 |
argv = rsync_argv('-a', f'{FROMDIR}/', f'{TODIR}/') |
| 110 |
try: |
| 111 |
proc = subprocess.run(argv, timeout=TIMEOUT, close_fds=False) |
| 112 |
except subprocess.TimeoutExpired: |
| 113 |
test_fail(f"rsync did not finish within {TIMEOUT}s with fds above " |
| 114 |
f"FD_SETSIZE ({fd_setsize}) -- select()/fd_set overflow " |
| 115 |
"(issue #231 regression)") |
| 116 |
finally: |
| 117 |
for fd in dummies: |
| 118 |
os.close(fd) |
| 119 |
|
| 120 |
if proc.returncode != 0: |
| 121 |
test_fail(f"rsync exited {proc.returncode} with fds above FD_SETSIZE " |
| 122 |
f"({fd_setsize}); a fortified libc aborts on the fd_set overflow " |
| 123 |
"(issue #231 regression)") |
| 124 |
|
| 125 |
for name, data in payload.items(): |
| 126 |
if (TODIR / name).read_bytes() != data: |
| 127 |
test_fail(f"{name} differs after a high-fd transfer") |
| 128 |
|
| 129 |
print(f"issue #231: transfer with fds above FD_SETSIZE ({fd_setsize}) " |
| 130 |
"completed correctly") |