eBPF-based security platform
bpm: take a WWW-Authenticate scheme only as a whole token
Daniel Borkmann9fc7d94b24cf
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex d7741b19..801ba74f 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -225,8 +225,9 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.* - ``bpm-oci-challenge``- ``parse_challenge()``- ``WWW-Authenticate`` values up to 17 bytes- - Outputs terminated in their buffers; only ``Bearer`` with a realm;- each value a quoted value of the header+ - Outputs terminated in their buffers; only the whole token+ ``Bearer`` as the scheme, with a realm; each value a quoted value+ of the header* - ``bpm-oci-token``- ``token_from_body()``, ``token_from_reply()``- Answers up to 17 bytes, as text and as the bytes of a response,diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 9855ca8b..0e6d011d 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -677,7 +677,9 @@ signed config image.- The token a registry's realm returns goes with the next request,and a realm that answers with an empty body, or with a NUL ahead ofor behind a token, fails the pull before any request carries a- token, with registry credentials set as without+ token, with registry credentials set as without; a challenge+ whose scheme only starts with ``Bearer`` or ``Basic`` gets neither+ a token nor credentials* - ``t7101-bpm-tree``- Tree sync with a selector* - ``t7102-bpm-tree-sequential``diff --git a/testing/functional/bpm/t7012-bpm-bearer-token.sh b/testing/functional/bpm/t7012-bpm-bearer-token.shindex 5f3ea47c..5294f5d0 100755--- a/testing/functional/bpm/t7012-bpm-bearer-token.sh+++ b/testing/functional/bpm/t7012-bpm-bearer-token.sh@@ -6,7 +6,8 @@# returns goes with the next request, and a realm that answers with an# empty body fails the pull before any request carries a token, with# registry credentials set as without. So does an answer that holds a-# NUL, which is no text to take a token from.+# NUL, which is no text to take a token from. A challenge whose scheme+# only starts with Bearer or Basic gets neither a token nor credentials.cd "$(dirname "$0")/../..". ./test-lib.sh@@ -23,8 +24,9 @@ trap cleanup EXIT# The realm of repository ns/<body> answers 200 with that body: "full"# with a token, "empty" with nothing, "nul-behind" and "nul-ahead" with a-# token behind or ahead of a NUL. Each request is logged with the-# credentials it carried.+# token behind or ahead of a NUL. Repositories ns/glued-bearer and+# ns/glued-basic challenge with "Bearerx," and "Basicx". Each request is+# logged with the credentials it carried.cat >"$W/registry.py" <<'PY'import http.server, sys@@ -56,6 +58,12 @@ class Handler(http.server.BaseHTTPRequestHandler):self.reply(200, b'{"token":"first-token"}\0{"token":"other"}')elif self.path.startswith("/token/nul-ahead"):self.reply(200, b'\0{"token":"hidden-token"}')+ elif "/ns/glued-bearer/manifests/" in self.path:+ challenge = 'Bearerx,realm="http://127.0.0.1:%d/token/full"' % port+ self.reply(401, headers=[("WWW-Authenticate", challenge)])+ elif "/ns/glued-basic/manifests/" in self.path:+ self.reply(401, headers=[("WWW-Authenticate",+ 'Basicx realm="registry"')])elif "/manifests/" in self.path:body = self.path.split("/")[3]challenge = ('Bearer realm="http://127.0.0.1:%d/token/%s",'@@ -123,4 +131,18 @@ test_expect_success 'an answer with a NUL ahead of a token gives none' '! grep -q "Bearer" "$W/requests"'+test_expect_success 'a scheme that only starts with Bearer fetches no token' '+ PULL=glued-bearer &&+ ! pull env &&+ grep -q "^/v2/ns/glued-bearer/manifests/1.0.0 none$" "$W/requests" &&+ ! grep -q "^/token/" "$W/requests"+'++test_expect_success 'a scheme that only starts with Basic gets no credentials' '+ PULL=glued-basic &&+ ! pull env BPM_OCI_USERNAME=user BPM_OCI_PASSWORD=secret &&+ grep -q "^/v2/ns/glued-basic/manifests/1.0.0 none$" "$W/requests" &&+ ! grep -q "Basic" "$W/requests"+'+test_donediff --git a/testing/proofs/cbmc/bpm-oci-challenge.c b/testing/proofs/cbmc/bpm-oci-challenge.cindex f59a4f21..287a9c0f 100644--- a/testing/proofs/cbmc/bpm-oci-challenge.c+++ b/testing/proofs/cbmc/bpm-oci-challenge.c@@ -3,9 +3,10 @@* Copyright (c) 2026 Authors of shirudo** parse_challenge() of util/bpm_oci.c, on a registry's WWW-Authenticate- * value of up to TEXT_MAX bytes: it takes only a Bearer challenge with a- * realm, each value it takes is a quoted one of the challenge, and every- * value is terminated inside its buffer, as long as fits it or one more.+ * value of up to TEXT_MAX bytes: it takes only a challenge whose scheme is+ * the whole token Bearer, with a realm, each value it takes is a quoted+ * one of the challenge, and every value is terminated inside its buffer,+ * as long as fits it or one more.*/#include "proof.h"@@ -63,8 +64,9 @@ void harness(void)"every value terminated inside its buffer");if (ret)return;- __CPROVER_assert(str_has_prefix_nocase(header, "Bearer"),- "only a Bearer challenge");+ __CPROVER_assert(str_has_prefix_nocase(header, "Bearer") &&+ (header[6] == ' ' || !header[6]),+ "only a Bearer challenge, its scheme a whole token");__CPROVER_assert(realm[0] && quoted_after(header, len, "realm", realm),"a realm, quoted in the challenge");__CPROVER_assert(!service[0] ||diff --git a/util/bpm_oci.c b/util/bpm_oci.cindex c33ecf25..114a5e24 100644--- a/util/bpm_oci.c+++ b/util/bpm_oci.c@@ -562,13 +562,28 @@ static int curl_once(struct bpm_oci_client *c, const char *method,follow);}+/*+ * What follows the scheme of a WWW-Authenticate challenge, or NULL when+ * the challenge is of another scheme. A scheme is a token, ended by a+ * space or by the value and taken in either case (RFC 9110 11.1, 11.3):+ * a "Basicx" challenge asks for no Basic credentials.+ */+static const char *challenge_params(const char *challenge, const char *scheme)+{+ const char *rest = str_after_prefix_nocase(challenge, scheme);++ if (!rest || (*rest && *rest != ' '))+ return NULL;+ return rest;+}+static int parse_challenge(const char *header, char *realm, size_t rsz,char *service, size_t ssz, char *scope, size_t zsz){- const char *p = header;- const char *q;+ const char *p, *q;- if (!p || !str_skip_prefix_nocase(&p, "Bearer"))+ p = header ? challenge_params(header, "Bearer") : NULL;+ if (!p)return -EINVAL;while (*p) {while (*p == ' ' || *p == ',')@@ -759,12 +774,12 @@ static int get_memory(struct bpm_oci_client *c, const char *url,if (ret)goto out;if (r.status == 401 && r.www_auth[0]) {- if (str_has_prefix_nocase(r.www_auth, "Bearer")) {+ if (challenge_params(r.www_auth, "Bearer")) {ret = get_bearer_token(c, r.www_auth);if (!ret && strsprintf(auth, sizeof(auth), "Bearer %s",c->token) < 0)ret = -E2BIG;- } else if (str_has_prefix_nocase(r.www_auth, "Basic") &&+ } else if (challenge_params(r.www_auth, "Basic") &&c->username[0]) {char encoded[BPM_OCI_BASIC_ENCODED_SIZE];@@ -826,7 +841,7 @@ int bpm_oci_get_file(struct bpm_oci_client *c, const char *url,return ret;ret = curl_once(c, "GET", url, accept, NULL, &r, &sink, true);if (!ret && r.status == 401 && r.www_auth[0] &&- str_has_prefix_nocase(r.www_auth, "Bearer")) {+ challenge_params(r.www_auth, "Bearer")) {ret = sink_restart(&sink, fd, (__u64)size);if (!ret)ret = get_bearer_token(c, r.www_auth);@@ -839,7 +854,7 @@ int bpm_oci_get_file(struct bpm_oci_client *c, const char *url,ret = curl_once(c, "GET", url, accept, auth, &r, &sink, true);}} else if (!ret && r.status == 401 && r.www_auth[0] &&- str_has_prefix_nocase(r.www_auth, "Basic") && c->username[0]) {+ challenge_params(r.www_auth, "Basic") && c->username[0]) {char encoded[BPM_OCI_BASIC_ENCODED_SIZE];ret = sink_restart(&sink, fd, (__u64)size);@@ -887,12 +902,12 @@ static int bpm_oci_request(struct bpm_oci_client *c, const char *method,if (ret)return ret;if (r->status == 401 && r->www_auth[0]) {- if (str_has_prefix_nocase(r->www_auth, "Bearer")) {+ if (challenge_params(r->www_auth, "Bearer")) {ret = get_bearer_token(c, r->www_auth);if (!ret && strsprintf(auth, sizeof(auth), "Bearer %s",c->token) < 0)ret = -E2BIG;- } else if (str_has_prefix_nocase(r->www_auth, "Basic") &&+ } else if (challenge_params(r->www_auth, "Basic") &&c->username[0]) {char encoded[BPM_OCI_BASIC_ENCODED_SIZE];