| 1 | #!/usr/bin/env python3 |
| 2 | # Regression test for an out-of-bounds write in options.c parse_size_arg(). |
| 3 | # |
| 4 | # C99 snprintf() returns the number of bytes it *would* have written, so when |
| 5 | # a daemon client supplies an oversized --max-size / --min-size / --bwlimit / |
| 6 | # --block-size / --max-alloc value (each daemon arg may be up to |
| 7 | # BIGPATHBUFLEN-1 = 5119 bytes via read_args), the `len` at the failure: label |
| 8 | # can far exceed sizeof err_buf (200). The trailing |
| 9 | # err_buf[len] = '\n'; err_buf[len+1] = '\0'; |
| 10 | # then writes 0x0A,0x00 at an attacker-chosen offset thousands of bytes past |
| 11 | # err_buf into adjacent .bss. Reachable from an unauthenticated client against |
| 12 | # any daemon module before chroot/privilege-drop. |
| 13 | # |
| 14 | # The client itself parses --max-size locally and would reject the oversized |
| 15 | # value before connecting, so we forward it to the *daemon's* option parser |
| 16 | # with -M (--remote-option), which the client passes through unparsed. |
| 17 | # |
| 18 | # Oracle: |
| 19 | # * fixed: parse_size_arg() clamps `len` to sizeof err_buf - 2 before the |
| 20 | # trailing writes; the daemon rejects the value with a clean |
| 21 | # "--max-size=... is invalid" error and stays alive. |
| 22 | # * pre-fix: the OOB .bss write corrupts adjacent globals. On a normal build |
| 23 | # this may not fault deterministically, but under ASAN it is a |
| 24 | # hard global-buffer-overflow abort of the daemon child. This test |
| 25 | # therefore asserts the GREEN behaviour (clean reject, no crash) |
| 26 | # on every build and is a true RED detector under the ASAN matrix. |
| 27 | |
| 28 | import re |
| 29 | import subprocess |
| 30 | |
| 31 | from rsyncfns import ( |
| 32 | SCRATCHDIR, makepath, rmtree, rsync_argv, start_test_daemon, |
| 33 | test_fail, write_daemon_conf, |
| 34 | ) |
| 35 | |
| 36 | DAEMON_PORT = 12939 |
| 37 | |
| 38 | mod = SCRATCHDIR / 'sizemod' |
| 39 | rmtree(mod) |
| 40 | makepath(mod) |
| 41 | (mod / 'f.txt').write_text("data\n") |
| 42 | |
| 43 | conf = write_daemon_conf([ |
| 44 | ('mod', {'path': str(mod), 'read only': 'yes'}), |
| 45 | ]) |
| 46 | url = start_test_daemon(conf, DAEMON_PORT) |
| 47 | |
| 48 | dst = SCRATCHDIR / 'sizeout' |
| 49 | rmtree(dst) |
| 50 | makepath(dst) |
| 51 | |
| 52 | big = '9' * 5000 # >> sizeof err_buf (200); ~4.9 KB past err_buf if unclamped |
| 53 | |
| 54 | proc = subprocess.run( |
| 55 | rsync_argv('-a', f'-M--max-size={big}', f'{url}mod/', f'{dst}/'), |
| 56 | stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) |
| 57 | |
| 58 | # parse_size_arg() builds the "--max-size=<value> is invalid" message in |
| 59 | # err_buf and only THEN does the (pre-fix) out-of-bounds err_buf[len] write, |
| 60 | # before returning -1 to the option-error reporter that forwards err_buf to |
| 61 | # the client. So the daemon echoing the rejected --max-size value back is |
| 62 | # proof it ran parse_size_arg to completion without faulting on the trailing |
| 63 | # write. (On a normal build the OOB write lands silently in .bss, so pre- and |
| 64 | # post-fix look identical here and the test passes either way; under the ASAN |
| 65 | # matrix the pre-fix write is a hard global-buffer-overflow abort that happens |
| 66 | # BEFORE err_buf is reported, so the value never comes back and this assert |
| 67 | # fires -- that is the RED signal.) |
| 68 | out = proc.stdout |
| 69 | if not re.search(r'--max-size=9{100,}', out): |
| 70 | test_fail( |
| 71 | "daemon did not echo back the rejected oversized --max-size value " |
| 72 | f"(rc={proc.returncode}); parse_size_arg likely aborted on the " |
| 73 | f"unclamped err_buf[len] out-of-bounds write.\noutput:\n{out}") |
| 74 | |
| 75 | print("daemon-size-arg-overflow: oversized --max-size rejected by the daemon " |
| 76 | "after parse_size_arg ran to completion (err_buf length is clamped).") |