Files
s3-gateway/tests/no-symlinks/race.py
T
57_WolveandClaude Opus 5.5 11d219961a
build / build (push) Successful in 9m26s
patches: add v1.11.0 no-symlinks patch and its leil-rig proof
0005-posix-no-symlinks.patch makes VersityGW's posix backend resolve every
path below its root and versioning directory from a descriptor of that
directory, opened once at start: each directory on the way with openat2
and RESOLVE_NO_SYMLINKS|RESOLVE_BENEATH (one O_NOFOLLOW open per element
where openat2 is missing), the last element acted on without following
it. Upstream followed a link anywhere on a key's way, so on a host that
mounts more than one namespace a link planted in a bucket served,
overwrote and deleted another namespace's objects (Norn M5 stage 13).

A link, FIFO, socket or device is no object: at the end of a key it
reads as missing (404) and is never opened, on the way it answers as a
file there does (404, 409 ObjectParentIsFile), listings skip it and
never enter it, and PUT, copy, complete and DELETE act on the entry
itself, never on what a link points to (owner, 2026-11-27).
--bucketlinks still follows a link at the bucket level only.

Proven on leil-rig on 2026-11-27, against a stock 0001-0004 control:
- tests/no-symlinks/proof.sh, throwaway gateways on leil-test-1:
  87/87 (control 38/87).
- tests/no-symlinks/rig-replay.sh, with the build pinned on the rig's
  four instances: 75/75 twice (control 17/75).
- rig-gateway-check.sh 20/20 in both namespaces.
- go test, every package but plugins/: 35/35.
The rig was put back on 26e5a763.

docs/no-symlinks.md has the rules, the proof and what it costs: GET
+3-9%, HEAD +8-25%, listings 2x on a local disk and within noise on
LeilFS. README.md and docs/multi-version-pipeline.md name the patch and
how to check a rebase of it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 01:50:15 -06:00

99 lines
3.6 KiB
Python

#!/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 <dir> <link> <bucket> <prefix> <inside> <outside> <seconds>
<dir> is the directory of <bucket>/<prefix>/ on disk, holding obj.txt with the
text <inside>. <link> is a link beside it to a directory outside the
namespace, holding obj.txt and victim.txt with the text <outside>. For
<seconds> one thread exchanges the two names (renameat2 RENAME_EXCHANGE) as
fast as it can while this one sends, in turn, GET <prefix>/obj.txt, PUT
<prefix>/new-<n>.txt and DELETE <prefix>/victim.txt. Exits 1 if a GET served
<outside>. 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:])