eBPF-based security platform
bpm: take a token server's token from its answer's own members
Daniel Borkmann7c4be75483f8
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex 6b267182..89df336e 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -231,11 +231,11 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.* - ``bpm-oci-token``- ``token_from_body()``, ``token_from_reply()``- Answers up to 17 bytes, as text and as the bytes of a response,- NULs included- - Nothing written on refusal; a token without controls, quotes or- backslashes, quoted in the answer under ``token`` or- ``access_token``; no token from a response without a body or- with a NUL among its bytes+ NULs included, and every outcome ``json_parse()`` may give+ - The whole answer parsed; nothing written on refusal; a token+ without controls, quotes or backslashes, the value of ``token``,+ else of ``access_token``, from an answer parsed whole; no token+ from a response without a body or with a NUL among its bytes* - ``bpm-oci-header``- ``bpm_header()``- Header lines up to 26 bytes, NULs included, with the line's copy@@ -1686,7 +1686,11 @@ those parts has no proof. So it is with the package state:and neither that nor reading back what ``bpm_state_emit()`` writes has aproof, as a string of any contents moves all that follows it in thedocument, which costs CBMC more memory than it has. The ``bpm_state``-unit cases check both on fixed entries. The zip directory walk of+unit cases check both on fixed entries. Nor has the parse of a token+server's answer: ``bpm-oci-token`` takes every outcome of+``json_parse()`` its contract allows, as parsing 17 bytes of any value+outgrew 6 GB, and the ``bpm_oci`` unit cases and the ``oci-http`` fuzz+target read real answers. The zip directory walk of``jar_premain_class()`` in :src:`tools/shirudo-build/jar.c` has neithera fuzz target nor a proof; its unit cases cut and flip a jar byte bybyte.diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 9e322ae6..0ee58945 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -682,11 +682,12 @@ signed config image.digest and a registry that serves another manifest* - ``t7012-bpm-bearer-token``- 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 of- or behind a token, fails the pull before any request carries a- token, with registry credentials set as without; a challenge- whose scheme only starts with ``Bearer`` or ``Basic`` gets neither- a token nor credentials+ the answer's own member and not one a string or a nested object+ names, and a realm that answers with an empty body, or with a NUL+ ahead of or behind a token, fails the pull before any request+ carries a 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 5294f5d0..69d4ce9e 100755--- a/testing/functional/bpm/t7012-bpm-bearer-token.sh+++ b/testing/functional/bpm/t7012-bpm-bearer-token.sh@@ -3,7 +3,8 @@# Copyright (c) 2026 Authors of shirudo## t7012 -- a registry that asks for a bearer token: the token its realm-# returns goes with the next request, and a realm that answers with an+# returns goes with the next request, the answer's own member and not+# one a string or a nested object names, 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. A challenge whose scheme@@ -24,9 +25,10 @@ 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. Repositories ns/glued-bearer and-# ns/glued-basic challenge with "Bearerx," and "Basicx". Each request is-# logged with the credentials it carried.+# token behind or ahead of a NUL, "quoted" and "nested" with a token+# behind a string and an object that name another. 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@@ -58,6 +60,10 @@ 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 self.path.startswith("/token/quoted"):+ self.reply(200, b'{"note":"\\"token\\":\\"evil\\"","token":"good"}')+ elif self.path.startswith("/token/nested"):+ self.reply(200, b'{"data":{"token":"evil"},"token":"good"}')elif "/ns/glued-bearer/manifests/" in self.path:challenge = 'Bearerx,realm="http://127.0.0.1:%d/token/full"' % portself.reply(401, headers=[("WWW-Authenticate", challenge)])@@ -103,6 +109,20 @@ test_expect_success 'the token the realm returns goes with the next request' 'grep -q "^/v2/ns/full/manifests/1.0.0 Bearer granted$" "$W/requests"'+test_expect_success 'the token is the member of the answer, not one a string names' '+ PULL=quoted &&+ ! pull env &&+ grep -q "^/v2/ns/quoted/manifests/1.0.0 Bearer good$" "$W/requests" &&+ ! grep -q "Bearer evil" "$W/requests"+'++test_expect_success 'the token is the member of the answer, not a nested one' '+ PULL=nested &&+ ! pull env &&+ grep -q "^/v2/ns/nested/manifests/1.0.0 Bearer good$" "$W/requests" &&+ ! grep -q "Bearer evil" "$W/requests"+'+test_expect_success 'an empty token response fails before a token is sent' 'PULL=empty &&! pull env &&diff --git a/testing/proofs/cbmc/bpm-oci-token.c b/testing/proofs/cbmc/bpm-oci-token.cindex 4614924d..fe776eea 100644--- a/testing/proofs/cbmc/bpm-oci-token.c+++ b/testing/proofs/cbmc/bpm-oci-token.c@@ -2,36 +2,77 @@/** Copyright (c) 2026 Authors of shirudo*- * token_from_body() of util/bpm_oci.c, on a token server's answer of up to- * TEXT_MAX bytes: the token it takes is a quoted value of an answer that- * names one, terminated and fit for a header line, and a refused answer- * writes no token. And token_from_reply(), on the response buffer curl- * filled with up to TEXT_MAX bytes of any value: an answer without a- * body or with a NUL among its bytes gives no token, and any other gives- * what token_from_body() takes from all of its bytes.+ * token_from_body() of util/bpm_oci.c on a token server's answer of up to+ * TEXT_MAX bytes, for every outcome json_parse() may give it: the whole+ * answer goes to json_parse(), the token it takes is the value of+ * "token", else that of "access_token", terminated and fit for a header+ * line, and a refused answer writes no token. And token_from_reply(), on+ * the response buffer curl filled with up to TEXT_MAX bytes of any value:+ * an answer without a body or with a NUL among its bytes gives no token,+ * and any other has all of its bytes parsed. json_parse() itself is held+ * to its contract by the json- proofs.*/-/* util/string.c is included behind a file that includes its header. */+/* util/malloc.c and util/string.c behind a file that includes their headers. */+#define MALLOC_INTERNAL#define STRING_INTERNAL#include "proof.h"#include "../../../util/bpm_oci.c"+#include "../../../util/malloc.c"#include "../../../util/string.c"#define TEXT_MAX 17#define OUT_MAX 3+#define FIELDS 2-static char *text(void)+/* What the model of json_parse() was handed and what it gave. */+static struct {+ bool called;+ const char *data;+ size_t len;+ int ret;+ bool seen[FIELDS];+ char value[FIELDS][OUT_MAX];+} parse;++/*+ * json_parse() as util/json.h promises it: one of its errors or 0, and+ * each declared string field written anywhere within its room and, where+ * seen, terminated there.+ */+int json_parse(const char *data, size_t len, struct json_field *fields,+ size_t n_fields){- char *s = any_bytes(TEXT_MAX + 1);+ size_t i, j;+ int ret;- __CPROVER_assume(!s[TEXT_MAX]);- return s;-}+ __CPROVER_assert(n_fields == FIELDS, "the two members declared");+ __CPROVER_assume(ret == 0 || ret == -EINVAL || ret == -E2BIG ||+ ret == -ENOSPC);+ parse.called = true;+ parse.data = data;+ parse.len = len;+ parse.ret = ret;+ for (i = 0; i < n_fields; i++) {+ char *dst = fields[i].dst;+ bool seen;-static bool terminated(const char *s, size_t size)-{- return memchr(s, '\0', size);+ __CPROVER_assert(fields[i].type == JSON_T_STRING &&+ fields[i].dst_cap <= OUT_MAX,+ "a string field no larger than out");+ for (j = 0; j < fields[i].dst_cap; j++) {+ char c;++ dst[j] = c;+ parse.value[i][j] = c;+ }+ if (seen)+ __CPROVER_assume(memchr(dst, '\0', fields[i].dst_cap));+ fields[i].seen = seen;+ parse.seen[i] = seen;+ }+ return ret;}static bool header_safe(const char *s)@@ -43,48 +84,47 @@ static bool header_safe(const char *s)return true;}-static bool quoted_in(const char *s, size_t len, const char *value)+/* What every answer the parser saw gives: no token, or the right one. */+static void outcome(int ret, const char *out, const char *was){- size_t v = strlen(value), i, j;- bool match;-- for (i = 0; i + v + 2 <= len; i++) {- match = s[i] == '"' && s[i + v + 1] == '"';- for (j = 0; match && j < v; j++)- match = s[i + 1 + j] == value[j];- if (match)- return true;+ const char *member = parse.seen[0] ? parse.value[0] : parse.value[1];++ if (ret) {+ __CPROVER_assert(!memcmp(out, was, OUT_MAX),+ "a refused answer writes no token");+ return;}- return false;+ __CPROVER_assert(!parse.ret && (parse.seen[0] || parse.seen[1]),+ "a token only from an answer parsed whole that has one");+ __CPROVER_assert(memchr(out, '\0', OUT_MAX), "a terminated token");+ __CPROVER_assert(!strcmp(out, member),+ "the value of token, else that of access_token");+ __CPROVER_assert(header_safe(out),+ "no blank, control, quote or backslash in the token");}static void answer_text(void){+ char *body = any_bytes(TEXT_MAX + 1);char out[OUT_MAX], was[OUT_MAX];- char *body = text();- size_t len = strlen(body);+ int ret;+ __CPROVER_assume(!body[TEXT_MAX]);memcpy(was, out, sizeof(out));- if (token_from_body(body, out, sizeof(out))) {- __CPROVER_assert(!memcmp(out, was, sizeof(out)),- "a refused answer writes no token");- return;- }- __CPROVER_assert(terminated(out, sizeof(out)), "a terminated token");- __CPROVER_assert(header_safe(out),- "no blank, control, quote or backslash in the token");- __CPROVER_assert(quoted_in(body, len, out) &&- (str_contains(body, "\"token\"") ||- str_contains(body, "\"access_token\"")),- "a quoted value of an answer naming a token");+ parse.called = false;+ ret = token_from_body(body, out, sizeof(out));+ __CPROVER_assert(parse.called && parse.data == body &&+ parse.len == strlen(body),+ "the whole answer parsed");+ outcome(ret, out, was);}/* The response as bpm_buffer_append() leaves it: a NUL behind its bytes. */static void answer_bytes(void){- char out[OUT_MAX], was[OUT_MAX], same[OUT_MAX];struct bpm_buffer reply = { .cap = TEXT_MAX + 1 }, none = {};char *body = any_bytes(TEXT_MAX + 1);+ char out[OUT_MAX], was[OUT_MAX];bool nul = false;size_t len, i;int ret;@@ -96,20 +136,21 @@ static void answer_bytes(void)if (!body[i])nul = true;memcpy(was, out, sizeof(out));- memcpy(same, out, sizeof(out));+ parse.called = false;__CPROVER_assert(token_from_reply(&none, out, sizeof(out)) == -EINVAL &&- !memcmp(out, was, sizeof(out)),+ !memcmp(out, was, sizeof(out)) && !parse.called,"no token from an answer without a body");ret = token_from_reply(&reply, out, sizeof(out));if (nul) {- __CPROVER_assert(ret == -EINVAL &&+ __CPROVER_assert(ret == -EINVAL && !parse.called &&!memcmp(out, was, sizeof(out)),"no token from an answer with a NUL among its bytes");return;}- __CPROVER_assert(ret == token_from_body(body, same, sizeof(same)) &&- !memcmp(out, same, sizeof(out)),- "else the token of the answer's text, or none");+ __CPROVER_assert(parse.called && parse.data == body &&+ parse.len == len,+ "else every byte of the answer parsed");+ outcome(ret, out, was);}void harness(void)diff --git a/testing/unit/bpm_oci.c b/testing/unit/bpm_oci.cindex 1ed8e500..bbdd030c 100644--- a/testing/unit/bpm_oci.c+++ b/testing/unit/bpm_oci.c@@ -13,6 +13,8 @@#include "io.h"#include "unit.h"+#include "../../util/bpm_oci.c"+#define DIGEST "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"#define MEDIA "\"mediaType\":\"application/vnd.shirudo.upatch.v1\""@@ -117,10 +119,79 @@ static void auth_file_with_a_nul_is_refused(void)EXPECT_STREQ(c.username, "");}+/*+ * The token is what the answer's own member "token" holds, or+ * "access_token" without one: a substring search took it from a string+ * value or a nested object that said "token" first.+ */+static void token_is_a_member_of_the_answer(void)+{+ static const struct {+ const char *body;+ const char *token;+ } answers[] = {+ { "{\"token\":\"granted\"}", "granted" },+ { " { \"access_token\" : \"abc.def\", \"expires_in\": 300 } ",+ "abc.def" },+ { "{\"access_token\":\"a\",\"token\":\"t\"}", "t" },+ { "{\"token\":null,\"access_token\":\"a\"}", "a" },+ { "{\"note\":\"\\\"token\\\":\\\"evil\\\"\",\"token\":\"good\"}",+ "good" },+ { "{\"note\":\"token\",\"x\":\"evil\",\"token\":\"good\"}", "good" },+ { "{\"data\":{\"token\":\"evil\"},\"token\":\"good\"}", "good" },+ { "{\"data\":[{\"token\":\"evil\"}],\"access_token\":\"good\"}",+ "good" },+ { "{\"token\":\"\\u0041\\/b\"}", "A/b" },+ { "{\"token\":\"1234567\"}", "1234567" },+ };+ char out[8];+ size_t i;++ for (i = 0; i < ARRAY_SIZE(answers); i++) {+ EXPECT_EQ(token_from_body(answers[i].body, out, sizeof(out)), 0);+ EXPECT_STREQ(out, answers[i].token);+ }+}++/*+ * The token goes into a header line as it is, so an answer whose token+ * holds a blank, a control, a quote or a backslash gives none, and so+ * does one that is no JSON object with a token among its members.+ */+static void token_refused_leaves_none(void)+{+ static const char *const answers[] = {+ "{\"token\":\"a b\"}",+ "{\"token\":\"a\\tb\"}",+ "{\"token\":\"a\x7f\"}",+ "{\"token\":\"a\\\"b\"}",+ "{\"token\":\"a\\\\b\"}",+ "{\"token\":\"12345678\"}",+ "{\"token\":42}",+ "{\"token\":\"a\",\"token\":\"b\"}",+ "{\"token\":\"a\"} {",+ "\"token\":\"a\"",+ "{\"data\":{\"token\":\"evil\"}}",+ "{\"token\":\"\xff\"}",+ "{}",+ "",+ };+ char out[8];+ size_t i;++ for (i = 0; i < ARRAY_SIZE(answers); i++) {+ strscpy_literal(out, "kept");+ EXPECT_EQ(token_from_body(answers[i], out, sizeof(out)), -EINVAL);+ EXPECT_STREQ(out, "kept");+ }+}+static const struct unit_case cases[] = {UNIT_CASE(manifest_layer_is_a_whole_descriptor),UNIT_CASE(digest_names_a_blob_as_a_registry_does),UNIT_CASE(auth_file_with_a_nul_is_refused),+ UNIT_CASE(token_is_a_member_of_the_answer),+ UNIT_CASE(token_refused_leaves_none),{}};diff --git a/util/bpm_oci.c b/util/bpm_oci.cindex c0cd4569..8440f721 100644--- a/util/bpm_oci.c+++ b/util/bpm_oci.c@@ -616,38 +616,54 @@ static int parse_challenge(const char *header, char *realm, size_t rsz,return realm[0] ? 0 : -EINVAL;}-/* The token in a token server's JSON answer, "token" or "access_token". */-static int token_from_body(const char *body, char *out, size_t out_sz)+/*+ * The token goes into a header line as it is: no blank, line break or+ * other control, and no quote or backslash, which only an escape in the+ * answer can have put there.+ */+static bool token_header_safe(const char *token){- const char *token, *end;-- token = span_find(body, strlen(body), "\"token\"");- if (!token)- token = span_find(body, strlen(body), "\"access_token\"");- if (token)- token = strchr(token, ':');- if (!token)- return -EINVAL;- token++;- while (*token && isspace((__u8)*token))- token++;- if (*token++ != '"')- return -EINVAL;- end = strchr(token, '"');- if (!end || (size_t)(end - token) >= out_sz)- return -EINVAL;- /* It goes into a header line as it is: no line break, no escape. */- for (const char *p = token; p < end; p++) {- if ((__u8)*p < 0x21 || *p == '\\' || (__u8)*p == 0x7f)- return -EINVAL;+ for (; *token; token++) {+ if ((__u8)*token < 0x21 || *token == '"' || *token == '\\' ||+ (__u8)*token == 0x7f)+ return false;}- memcpy(out, token, end - token);- out[end - token] = '\0';- return 0;+ return true;+}++/*+ * The token of a token server's JSON answer: its member "token", or+ * "access_token" where it has none, as the token flow of the+ * Distribution project names both. They are parsed apart from out,+ * which a refusal leaves alone.+ */+static int token_from_body(const char *body, char *out, size_t out_sz)+{+ struct json_field fields[2] = {};+ const char *token;+ char *found;+ int ret;++ found = xcalloc(2, out_sz);+ if (!found)+ return -ENOMEM;+ fields[0] = (struct json_field){ .key = "token", .type = JSON_T_STRING,+ .dst = found, .dst_cap = out_sz };+ fields[1] = (struct json_field){ .key = "access_token",+ .type = JSON_T_STRING, .dst = found + out_sz, .dst_cap = out_sz };+ ret = json_parse(body, strlen(body), fields, ARRAY_SIZE(fields));+ token = fields[0].seen ? found : found + out_sz;+ if (ret || !(fields[0].seen || fields[1].seen) ||+ !token_header_safe(token))+ ret = -EINVAL;+ else+ memcpy(out, token, strlen(token) + 1);+ xfree(found, 2 * out_sz);+ return ret;}/*- * The answer is text: with a NUL in it the search above would take it+ * The answer is text: with a NUL in it token_from_body() would take it* to end there, whatever the server sent behind it.*/static int token_from_reply(const struct bpm_buffer *reply, char *out,