Skip to content

eBPF-based security platform

bpm: take no token from an answer that holds a NUL

Daniel Borkmann8e5c797efd7f
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index c0f4d8c3..d102406e 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -202,11 +202,13 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
- Outputs terminated in their buffers; only ``Bearer`` with a realm;
each value a quoted value of the header
* - ``bpm-oci-token``
- - ``token_from_body()``
- - Answers up to 17 bytes
+ - ``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``
+ ``access_token``; 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
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rst
index 3b93a5b5..1b2305a8 100644
--- a/Documentation/contribute/testing/test-coverage.rst
+++ b/Documentation/contribute/testing/test-coverage.rst
@@ -662,9 +662,9 @@ 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 fails the pull before
- any request carries a token, with registry credentials set as
- without
+ 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
* - ``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.sh
index 80c733c1..5f3ea47c 100755
--- a/testing/functional/bpm/t7012-bpm-bearer-token.sh
+++ b/testing/functional/bpm/t7012-bpm-bearer-token.sh
@@ -5,7 +5,8 @@
# 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
# empty body fails the pull before any request carries a token, with
-# registry credentials set as without.
+# registry credentials set as without. So does an answer that holds a
+# NUL, which is no text to take a token from.
cd "$(dirname "$0")/../.."
. ./test-lib.sh
@@ -21,7 +22,8 @@ cleanup() {
trap cleanup EXIT
# The realm of repository ns/<body> answers 200 with that body: "full"
-# with a token, "empty" with nothing. Each request is logged with the
+# 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.
cat >"$W/registry.py" <<'PY'
import http.server, sys
@@ -50,6 +52,10 @@ class Handler(http.server.BaseHTTPRequestHandler):
self.reply(200, b'{"token":"granted"}')
elif self.path.startswith("/token/empty"):
self.reply(200)
+ elif self.path.startswith("/token/nul-behind"):
+ 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 "/manifests/" in self.path:
body = self.path.split("/")[3]
challenge = ('Bearer realm="http://127.0.0.1:%d/token/%s",'
@@ -103,4 +109,18 @@ test_expect_success 'an empty token response fails with credentials set too' '
! grep -q "Bearer" "$W/requests"
'
+test_expect_success 'an answer with a NUL behind a token gives none' '
+ PULL=nul-behind &&
+ ! pull env BPM_OCI_USERNAME=user BPM_OCI_PASSWORD=secret &&
+ grep -q "^/token/nul-behind Basic " "$W/requests" &&
+ ! grep -q "Bearer" "$W/requests"
+'
+
+test_expect_success 'an answer with a NUL ahead of a token gives none' '
+ PULL=nul-ahead &&
+ ! pull env BPM_OCI_USERNAME=user BPM_OCI_PASSWORD=secret &&
+ grep -q "^/token/nul-ahead Basic " "$W/requests" &&
+ ! grep -q "Bearer" "$W/requests"
+'
+
test_done
diff --git a/testing/proofs/cbmc/bpm-oci-token.c b/testing/proofs/cbmc/bpm-oci-token.c
index eec928d7..962920b4 100644
--- a/testing/proofs/cbmc/bpm-oci-token.c
+++ b/testing/proofs/cbmc/bpm-oci-token.c
@@ -5,11 +5,18 @@
* 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.
+ * 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.
*/
+/* util/string.c is included behind a file that includes its header. */
+#define STRING_INTERNAL
+
#include "proof.h"
#include "../../../util/bpm_oci.c"
+#include "../../../util/string.c"
#define TEXT_MAX 17
#define OUT_MAX 3
@@ -51,7 +58,7 @@ static bool quoted_in(const char *s, size_t len, const char *value)
return false;
}
-void harness(void)
+static void answer_text(void)
{
char out[OUT_MAX], was[OUT_MAX];
char *body = text();
@@ -71,3 +78,42 @@ void harness(void)
strstr(body, "\"access_token\"")),
"a quoted value of an answer naming a token");
}
+
+/* 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);
+ bool nul = false;
+ size_t len, i;
+ int ret;
+
+ __CPROVER_assume(len <= TEXT_MAX && !body[len]);
+ reply.data = (__u8 *)body;
+ reply.len = len;
+ for (i = 0; i < len; i++)
+ if (!body[i])
+ nul = true;
+ memcpy(was, out, sizeof(out));
+ memcpy(same, out, sizeof(out));
+ __CPROVER_assert(token_from_reply(&none, out, sizeof(out)) == -EINVAL &&
+ !memcmp(out, was, sizeof(out)),
+ "no token from an answer without a body");
+ ret = token_from_reply(&reply, out, sizeof(out));
+ if (nul) {
+ __CPROVER_assert(ret == -EINVAL &&
+ !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");
+}
+
+void harness(void)
+{
+ answer_text();
+ answer_bytes();
+}
diff --git a/util/bpm_oci.c b/util/bpm_oci.c
index 53120db6..f2a4b209 100644
--- a/util/bpm_oci.c
+++ b/util/bpm_oci.c
@@ -641,13 +641,19 @@ static int token_from_body(const char *body, char *out, size_t out_sz)
return 0;
}
+/*
+ * The answer is text: with a NUL in it the search above would take it
+ * to end there, whatever the server sent behind it.
+ */
static int token_from_reply(const struct bpm_buffer *reply, char *out,
size_t out_sz)
{
+ char *body = (char *)reply->data;
+
/* curl never calls the write callback for an empty body. */
- if (!reply->data)
+ if (!body || span_to_str(body, reply->cap, body, reply->len))
return -EINVAL;
- return token_from_body((char *)reply->data, out, out_sz);
+ return token_from_body(body, out, out_sz);
}
static int get_bearer_token(struct bpm_oci_client *c, const char *challenge)