eBPF-based security platform
bpm: give every registry transfer a total deadline
Daniel Borkmann47aad262c073
diff --git a/Documentation/concepts/background/oci.rst b/Documentation/concepts/background/oci.rstindex 0952a524..b35becaf 100644--- a/Documentation/concepts/background/oci.rst+++ b/Documentation/concepts/background/oci.rst@@ -162,8 +162,12 @@ publication and fetches each artifact by the manifest and layer digeststhe publication names.The client follows redirects for blob requests only and never sends-the ``Authorization`` header to another host. It accepts no response-larger than 256 MiB. Credentials come from ``BPM_OCI_USERNAME`` and+the ``Authorization`` header to another host. It reads no response+larger than 16 MiB into memory and writes no package larger than 256 MiB+to disk. Each transfer ends after a minute plus the time the most it may+carry takes at 128 KiB a second, so that a registry that answers a few+bytes at a time cannot hold a command, and the config operation lock it+holds, any longer. Credentials come from ``BPM_OCI_USERNAME`` and``BPM_OCI_PASSWORD``, or from the registry's ``auth`` entry in``REGISTRY_AUTH_FILE``, ``$DOCKER_CONFIG/config.json`` or``$HOME/.docker/config.json``, and ``BPM_OCI_BEARER_TOKEN`` supplies adiff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 231532a6..350511bd 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -551,6 +551,9 @@ signed config image.sealed segment selects and takes it once a later segment deletedthat ``add``, and fetches no sealed segment for an ``add`` withoutXDP classes+ * - ``t7010-bpm-slow-registry``+ - A registry that trickles a package fast enough for the low-speed+ limit is cut off at the transfer's total deadline* - ``t7101-bpm-tree``- Tree sync with a selector* - ``t7102-bpm-tree-sequential``diff --git a/Documentation/operate/plan/overhead.rst b/Documentation/operate/plan/overhead.rstindex 7ac30dd0..416bcd91 100644--- a/Documentation/operate/plan/overhead.rst+++ b/Documentation/operate/plan/overhead.rst@@ -185,7 +185,8 @@ Larger inputs raise the peak. These limits bound it:- 16 MiB. ``upatch load`` holds the file twice and allocates a16 MiB verifier log.* - Registry responses read into memory- - 256 MiB: manifests, the repository index and its signature.+ - 16 MiB: manifests, the repository index and its signature, tree+ publications and the upatch artifacts they name.* - Package archive- 256 MiB, written to disk, and 256 MiB unpacked.* - JSON documents, the repository index and the eventlog among themdiff --git a/Documentation/security/threat-model.rst b/Documentation/security/threat-model.rstindex fe063aed..01ce59ec 100644--- a/Documentation/security/threat-model.rst+++ b/Documentation/security/threat-model.rst@@ -1507,7 +1507,12 @@ R8 Registry withholding and replaybase alike; a target behind the point where the branches part takeseither. A registry can still withhold the latest publication or asealed segment, which stops a walk, and keep serving a stale tag,- which a target takes for current until a later one reaches it.+ which a target takes for current until a later one reaches it. A+ registry that answers slowly holds each transfer of a timer's+ command, and the config operation lock every other command waits for+ up to 30 seconds, until that transfer's deadline: a minute plus the+ time its largest response, 16 MiB, or its package's size, takes at+ 128 KiB a second (``t7010``).Detection: compare the upatch payloads and tree operations that``attest verify`` certifies with what you published, and the pushedindex with the versions you released.diff --git a/testing/functional/bpm/t7010-bpm-slow-registry.sh b/testing/functional/bpm/t7010-bpm-slow-registry.shnew file mode 100755index 00000000..ab8171b4--- /dev/null+++ b/testing/functional/bpm/t7010-bpm-slow-registry.sh@@ -0,0 +1,87 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t7010 -- a registry that trickles a blob, a few bytes a second and so+# fast enough for the low-speed limit, cannot hold a transfer, and with+# it the config operation lock on a target, past its total deadline: a+# minute for a small package, where the blob would take five.++cd "$(dirname "$0")/../.."+. ./test-lib.sh++SHIRUDO=${SHIRUDO:-../shirudo}+command -v python3 >/dev/null 2>&1 || test_skip_all "no python3"+W=$(mktemp -d)+SERVER=+cleanup() {+ [ -z "$SERVER" ] || kill "$SERVER" 2>/dev/null || :+ rm -rf "$W"+}+trap cleanup EXIT++cat >"$W/registry.py" <<'PY'+import hashlib, http.server, json, sys, time++SIZE = 1000+DIGEST = "sha256:" + hashlib.sha256(b"\0" * SIZE).hexdigest()+MANIFEST = json.dumps({+ "schemaVersion": 2,+ "mediaType": "application/vnd.oci.image.manifest.v1+json",+ "layers": [{"mediaType": "application/vnd.shirudo.bpm.package.v1+gzip",+ "digest": DIGEST, "size": SIZE}],+}).encode()+++class Handler(http.server.BaseHTTPRequestHandler):+ protocol_version = "HTTP/1.1"++ def log_message(self, *args):+ pass++ def do_GET(self):+ if "/manifests/" in self.path:+ self.send_response(200)+ self.send_header("Content-Type",+ "application/vnd.oci.image.manifest.v1+json")+ self.send_header("Content-Length", str(len(MANIFEST)))+ self.end_headers()+ self.wfile.write(MANIFEST)+ return+ self.send_response(200)+ self.send_header("Content-Length", str(SIZE))+ self.end_headers()+ try:+ for _ in range(SIZE // 3):+ self.wfile.write(b"\0\0\0")+ self.wfile.flush()+ time.sleep(1)+ except OSError:+ pass+++server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Handler)+with open(sys.argv[1], "w") as f:+ f.write(str(server.server_address[1]))+server.serve_forever()+PY+python3 "$W/registry.py" "$W/port" &+SERVER=$!+for _ in 1 2 3 4 5 6 7 8 9 10; do+ [ -s "$W/port" ] && break+ sleep 0.5+done+[ -s "$W/port" ] || test_skip_all "the trickling registry did not start"+PORT=$(cat "$W/port")++BIN=$(cd "$(dirname "$SHIRUDO")" && pwd)/$(basename "$SHIRUDO")++test_expect_success 'a trickled package download stops at its deadline' '+ start=$(date +%s) &&+ ! (cd "$W" && BPM_OCI_SCHEME=http "$BIN" bpm pull \+ "oci://127.0.0.1:$PORT/ns/slow:1.0.0" >"$W/out" 2>&1) &&+ elapsed=$(($(date +%s) - start)) &&+ [ "$elapsed" -ge 50 ] &&+ [ "$elapsed" -lt 120 ]+'+test_donediff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.cindex e75e25fa..8f4fd6a9 100644--- a/tools/shirudo-build/bpm-publish.c+++ b/tools/shirudo-build/bpm-publish.c@@ -379,7 +379,7 @@ static int push_package(const char *package_path, const char *raw_repository,return -errno;if (!S_ISREG(st.st_mode))return -EINVAL;- ret = read_file(package_path, BPM_MAX_HTTP_BODY, &archive,+ ret = read_file(package_path, BPM_MAX_PACKAGE_BYTES, &archive,&archive_len);if (ret)return ret;@@ -1390,7 +1390,7 @@ static int cmd_bpm_tree_append(int argc, char **argv)}member = unload ? u->tree.unload_artifact : u->tree.load_artifact;ret = bpm_pkg_read_member_in(package, member, package_dir, NULL, 0,- &body, &body_len, BPM_MAX_HTTP_BODY);+ &body, &body_len, UPATCH_FILE_MAX);if (ret) {pr_err("bpm tree append: package entry %s not found", member);goto out;diff --git a/util/bpm_oci.c b/util/bpm_oci.cindex 30de7a23..92eb863e 100644--- a/util/bpm_oci.c+++ b/util/bpm_oci.c@@ -406,15 +406,24 @@ int bpm_oci_parse_endpoint(const char *endpoint, struct bpm_oci_client *c)return load_registry_auth(c->registry, c);}+#define BPM_TRANSFER_BASE_S 60u+#define BPM_TRANSFER_RATE_MIN (128u * 1024u)+/*- * A registry that stops answering must not hold a timer's unit forever;- * a slow transfer that still moves is left alone.+ * A registry that stops answering, or answers a few bytes at a time,+ * must not hold a timer's unit, and with it the config operation lock+ * every other command waits for, longer than the most a transfer may+ * carry takes at a floor rate: room bytes, in a minute and at 128 KiB a+ * second.*/-static void bpm_curl_deadlines(CURL *curl)+static void bpm_curl_deadlines(CURL *curl, __u64 room){+ __u64 seconds = BPM_TRANSFER_BASE_S + room / BPM_TRANSFER_RATE_MIN;+curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 30L);curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1L);curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, 60L);+ curl_easy_setopt(curl, CURLOPT_TIMEOUT, (long)seconds);}static int curl_once_ex(struct bpm_oci_client *c, const char *method,@@ -475,7 +484,8 @@ static int curl_once_ex(struct bpm_oci_client *c, const char *method,curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);- bpm_curl_deadlines(curl);+ bpm_curl_deadlines(curl, (file ? file->room : BPM_MAX_HTTP_BODY) ++ body_len);/** OCI registries commonly redirect blob GETs to a signed CDN URL.* Follow only for blobs, never for manifest or upload calls.@@ -691,7 +701,8 @@ static int get_bearer_token(struct bpm_oci_client *c, const char *challenge)"https");curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);- bpm_curl_deadlines(curl);+ curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);+ bpm_curl_deadlines(curl, BPM_MAX_HTTP_BODY);curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, bpm_write_memory);curl_easy_setopt(curl, CURLOPT_WRITEDATA, &r);curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);diff --git a/util/bpm_oci.h b/util/bpm_oci.hindex 9bc4dc8c..922b56c5 100644--- a/util/bpm_oci.h+++ b/util/bpm_oci.h@@ -26,7 +26,8 @@ struct json_emit;#define BPM_MAX_MEDIA 192#define BPM_MAX_ENTRIES 512#define BPM_MAX_STATE 256-#define BPM_MAX_HTTP_BODY (256u * 1024u * 1024u)+/* The largest response read into memory, an upatch artifact. */+#define BPM_MAX_HTTP_BODY (16u * 1024u * 1024u)#define BPM_MAX_PACKAGE_BYTES (256u * 1024u * 1024u)#define BPM_MAX_PACKAGE_ENTRIES 4096u#define BPM_OCI_USERNAME_SIZE 256