| 1 |
|
| 2 |
"""An aborted in-place update must not relax an existing file's mode. |
| 3 |
|
| 4 |
The proposed-3.5 receiver retries an EACCES output open by chmod'ing an |
| 5 |
existing output or in-place partial file to 0600. This test's fake remote |
| 6 |
sender closes after the receiver has requested the file and before sending any |
| 7 |
file-data token. The transfer must fail, but mode 0444 must survive. A normal |
| 8 |
successful --inplace update of a read-only file is also checked so that the |
| 9 |
3.5 compatibility feature remains intact. |
| 10 |
""" |
| 11 |
|
| 12 |
import os |
| 13 |
import select |
| 14 |
import struct |
| 15 |
import subprocess |
| 16 |
import sys |
| 17 |
|
| 18 |
import rsync_proto as rp |
| 19 |
from rsyncfns import SCRATCHDIR, makepath, rmtree, rsync_argv, test_fail |
| 20 |
|
| 21 |
|
| 22 |
CF_INPLACE_PARTIAL_DIR = 1 << 6 |
| 23 |
FNAMECMP_PARTIAL_DIR = 0x81 |
| 24 |
ITEM_BASIS_TYPE_FOLLOWS = 1 << 11 |
| 25 |
|
| 26 |
|
| 27 |
def write_all(data): |
| 28 |
view = memoryview(data) |
| 29 |
while view: |
| 30 |
n = os.write(1, view) |
| 31 |
if n <= 0: |
| 32 |
raise RuntimeError("short write to rsync client") |
| 33 |
view = view[n:] |
| 34 |
|
| 35 |
|
| 36 |
def read_exact(n, timeout=10): |
| 37 |
out = bytearray() |
| 38 |
while len(out) < n: |
| 39 |
ready, _, _ = select.select([0], [], [], timeout) |
| 40 |
if not ready: |
| 41 |
raise RuntimeError(f"timeout reading {n} protocol bytes") |
| 42 |
chunk = os.read(0, n - len(out)) |
| 43 |
if not chunk: |
| 44 |
raise RuntimeError(f"EOF after {len(out)}/{n} protocol bytes") |
| 45 |
out += chunk |
| 46 |
return bytes(out) |
| 47 |
|
| 48 |
|
| 49 |
def frame(payload): |
| 50 |
word = ((rp.MPLEX_BASE + rp.MSG_DATA) << 24) | len(payload) |
| 51 |
return struct.pack("<I", word) + payload |
| 52 |
|
| 53 |
|
| 54 |
def wait_for_generator_request(): |
| 55 |
"""Consume multiplexed client output until index 0's request appears.""" |
| 56 |
data = bytearray() |
| 57 |
for _ in range(256): |
| 58 |
word = struct.unpack("<I", read_exact(4))[0] |
| 59 |
length = word & 0xFFFFFF |
| 60 |
tag = (word >> 24) - rp.MPLEX_BASE |
| 61 |
payload = read_exact(length) if length else b"" |
| 62 |
if tag != rp.MSG_DATA: |
| 63 |
continue |
| 64 |
data += payload |
| 65 |
if len(data) >= 7 and b"\x01" in data[4:]: |
| 66 |
return |
| 67 |
if len(data) > 1024 * 1024: |
| 68 |
break |
| 69 |
raise RuntimeError(f"generator did not request index 0: {data[:64].hex()}") |
| 70 |
|
| 71 |
|
| 72 |
def fake_sender(basis_type): |
| 73 |
write_all(rp.w_int(30)) |
| 74 |
client_protocol = struct.unpack("<i", read_exact(4))[0] |
| 75 |
if client_protocol < 30: |
| 76 |
raise RuntimeError(f"client protocol is unexpectedly {client_protocol}") |
| 77 |
write_all(rp.w_varint(CF_INPLACE_PARTIAL_DIR) + rp.w_int(0x12345678)) |
| 78 |
|
| 79 |
entry = rp.FileEntry("f", mode=rp.S_IFREG | 0o444, length=4096, |
| 80 |
modtime=1700000000, protocol=30) |
| 81 |
write_all(frame(entry.encode() + rp.end_of_flist(protocol=30))) |
| 82 |
wait_for_generator_request() |
| 83 |
|
| 84 |
response = bytearray() |
| 85 |
response += b"\x01" |
| 86 |
response += rp.w_shortint(rp.ITEM_TRANSFER | ITEM_BASIS_TYPE_FOLLOWS) |
| 87 |
response += rp.w_byte(basis_type) |
| 88 |
response += rp.w_sum_head(0, 0, 0, 0) |
| 89 |
write_all(frame(bytes(response))) |
| 90 |
|
| 91 |
|
| 92 |
fake_arg = next((arg for arg in sys.argv if arg.startswith("--fake-server=")), None) |
| 93 |
if fake_arg: |
| 94 |
try: |
| 95 |
scenario = fake_arg.split("=", 1)[1] |
| 96 |
fake_sender(FNAMECMP_PARTIAL_DIR if scenario == "partial" else rp.FNAMECMP_FNAME) |
| 97 |
except Exception as exc: |
| 98 |
print(f"fake sender failed: {exc!r}", file=sys.stderr) |
| 99 |
sys.exit(2) |
| 100 |
sys.exit(0) |
| 101 |
|
| 102 |
|
| 103 |
if os.geteuid() == 0: |
| 104 |
print("SKIP: root bypasses the read-only output-file precondition") |
| 105 |
sys.exit(77) |
| 106 |
|
| 107 |
base = SCRATCHDIR / "readonly-partial-abort-mode-regression" |
| 108 |
rmtree(base) |
| 109 |
script = os.path.abspath(__file__) |
| 110 |
|
| 111 |
|
| 112 |
def run_abort_case(scenario): |
| 113 |
dest = base / scenario / "dest" |
| 114 |
if scenario == "partial": |
| 115 |
existing = dest / ".rsync-partial" / "f" |
| 116 |
options = ("--partial-dir=.rsync-partial", "--no-whole-file") |
| 117 |
else: |
| 118 |
existing = dest / "f" |
| 119 |
options = ("--inplace", "--no-whole-file") |
| 120 |
makepath(existing.parent) |
| 121 |
existing.write_bytes(b"old data\n") |
| 122 |
os.chmod(existing, 0o444) |
| 123 |
|
| 124 |
rsh = f"{sys.executable} {script} --fake-server={scenario}" |
| 125 |
proc = subprocess.run( |
| 126 |
rsync_argv("--protocol=30", "-r", *options, "-e", rsh, |
| 127 |
"fake:f", str(dest) + "/"), |
| 128 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, timeout=20, |
| 129 |
) |
| 130 |
|
| 131 |
failures = [] |
| 132 |
if proc.returncode == 0: |
| 133 |
failures.append(f"truncated {scenario} stream unexpectedly succeeded:\n{proc.stdout}") |
| 134 |
mode = os.stat(existing).st_mode & 0o7777 |
| 135 |
if mode != 0o444: |
| 136 |
failures.append( |
| 137 |
f"aborted {scenario} update changed pre-existing mode 0444 to {mode:04o}; " |
| 138 |
f"rsync output:\n{proc.stdout}" |
| 139 |
) |
| 140 |
return failures |
| 141 |
|
| 142 |
|
| 143 |
def run_success_case(): |
| 144 |
source = base / "success" / "source" |
| 145 |
dest = base / "success" / "dest" |
| 146 |
makepath(source) |
| 147 |
makepath(dest) |
| 148 |
(source / "f").write_bytes(b"replacement data\n") |
| 149 |
(dest / "f").write_bytes(b"old data\n") |
| 150 |
os.chmod(source / "f", 0o444) |
| 151 |
os.chmod(dest / "f", 0o444) |
| 152 |
proc = subprocess.run( |
| 153 |
rsync_argv("--inplace", "-r", str(source) + "/", str(dest) + "/"), |
| 154 |
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, timeout=20, |
| 155 |
) |
| 156 |
failures = [] |
| 157 |
if proc.returncode != 0: |
| 158 |
failures.append(f"successful read-only --inplace update failed:\n{proc.stdout}") |
| 159 |
if (dest / "f").read_bytes() != b"replacement data\n": |
| 160 |
failures.append("successful read-only --inplace update did not install the new data") |
| 161 |
mode = os.stat(dest / "f").st_mode & 0o7777 |
| 162 |
if mode != 0o444: |
| 163 |
failures.append(f"successful read-only --inplace update left mode {mode:04o}") |
| 164 |
return failures |
| 165 |
|
| 166 |
|
| 167 |
failures = run_abort_case("inplace") + run_abort_case("partial") + run_success_case() |
| 168 |
if failures: |
| 169 |
test_fail("\n\n".join(failures)) |
| 170 |
|
| 171 |
print("PASS: aborted updates preserved mode and successful read-only inplace still works") |