Skip to content

eBPF-based security platform

proofs: reach bpm_header()'s line and challenge limits in bpm-oci-header

Daniel Borkmannb9e7fc032b61
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rst
index b64b2c01..ef9b0615 100644
--- a/Documentation/contribute/testing/proof-coverage.rst
+++ b/Documentation/contribute/testing/proof-coverage.rst
@@ -187,9 +187,13 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.
``access_token``
* - ``bpm-oci-header``
- ``bpm_header()``
- - Header lines up to 20 bytes, NULs included
- - The whole line taken; values terminated, without a leading blank,
- a media type without parameters, a location without CR or LF
+ - Header lines up to 26 bytes, NULs included, with the line's copy
+ and the challenge's field set to 24 and 4 bytes for their 4096
+ and 2048
+ - The whole line taken, and one too long for the copy read not at
+ all; values terminated, without a leading blank, a media type
+ without parameters, a location without CR or LF; a challenge
+ that fits kept whole, a longer one dropped rather than cut
* - ``bpm-oci-descriptor``
- ``on_descriptor()``, ``on_index_entry()``
- Any descriptor or index entry, into an empty or a full table
diff --git a/testing/proofs/cbmc/bpm-oci-header.c b/testing/proofs/cbmc/bpm-oci-header.c
index b39c381d..e837bd31 100644
--- a/testing/proofs/cbmc/bpm-oci-header.c
+++ b/testing/proofs/cbmc/bpm-oci-header.c
@@ -4,11 +4,17 @@
*
* bpm_header() of util/bpm_oci.c, libcurl's header callback, for every
* header line of up to LINE_MAX bytes, NULs included: it takes the whole
- * line, and the challenge, media type and location it keeps are terminated
- * inside their fields, the media type without parameters, neither with a
- * line break.
+ * line, reads none longer than its copy, and the challenge, media type and
+ * location it keeps are terminated inside their fields, the media type
+ * without parameters, neither with a line break, a challenge longer than
+ * its field dropped rather than cut. The copy and the challenge's field
+ * are of 24 and 4 bytes here, for 4096 and 2048, which are past what
+ * CBMC can hold: the checks against them are the same expressions.
*/
+#define BPM_OCI_HEADER_LINE_MAX 24
+#define BPM_OCI_CHALLENGE_MAX 4
+
/* util/string.c is included behind a file that includes its header. */
#define STRING_INTERNAL
#define PROOF_MEMCPY_BYTEWISE
@@ -17,12 +23,47 @@
#include "../../../util/bpm_oci.c"
#include "../../../util/string.c"
-#define LINE_MAX 20
+/* Past the copy, and room for a challenge longer than its field. */
+#define LINE_MAX (BPM_OCI_HEADER_LINE_MAX + 2)
/* What the line leaves in a field ends inside the line's length. */
-static bool short_string(const char *s)
+static bool short_string(const char *s, size_t size)
+{
+ return memchr(s, '\0', min(size, (size_t)LINE_MAX + 1));
+}
+
+static char lower(char c)
+{
+ return c >= 'A' && c <= 'Z' ? c - 'A' + 'a' : c;
+}
+
+/* The line starts with the header's name, in any case. */
+static bool named(const char *line, size_t n, const char *name)
+{
+ size_t i, len = strlen(name);
+
+ for (i = 0; i < len; i++)
+ if (i >= n || lower(line[i]) != lower(name[i]))
+ return false;
+ return true;
+}
+
+/* The challenge a WWW-Authenticate line holds: from its first non-blank. */
+static void challenge(const char *line, size_t n, const char *kept)
{
- return memchr(s, '\0', LINE_MAX + 1);
+ size_t from = strlen("WWW-Authenticate:"), len = 0;
+
+ while (from < n && isspace((__u8)line[from]))
+ from++;
+ while (from + len < n && line[from + len])
+ len++;
+ if (len >= BPM_OCI_CHALLENGE_MAX) {
+ __CPROVER_assert(!kept[0],
+ "a challenge longer than its field dropped");
+ return;
+ }
+ __CPROVER_assert(!memcmp(kept, line + from, len) && !kept[len],
+ "a challenge that fits kept whole");
}
static bool has_any(const char *s, const char *set)
@@ -45,9 +86,15 @@ void harness(void)
__CPROVER_assume(n <= LINE_MAX);
__CPROVER_assert(bpm_header(line, 1, n, &r) == n,
"the whole line taken");
- __CPROVER_assert(short_string(r.www_auth) &&
- short_string(r.content_type) &&
- short_string(r.location),
+ if (n >= BPM_OCI_HEADER_LINE_MAX) {
+ __CPROVER_assert(!r.www_auth[0] && !r.content_type[0] &&
+ !r.location[0],
+ "a line longer than the copy read not at all");
+ return;
+ }
+ __CPROVER_assert(short_string(r.www_auth, sizeof(r.www_auth)) &&
+ short_string(r.content_type, sizeof(r.content_type)) &&
+ short_string(r.location, sizeof(r.location)),
"every value terminated inside its field");
__CPROVER_assert(!has_any(r.content_type, " ;\r\n"),
"a media type alone");
@@ -57,4 +104,6 @@ void harness(void)
!isspace((__u8)r.content_type[0]) &&
!isspace((__u8)r.location[0]),
"no value starts with a blank");
+ if (named(line, n, "WWW-Authenticate:"))
+ challenge(line, n, r.www_auth);
}
diff --git a/testing/proofs/cbmc/proofs.list b/testing/proofs/cbmc/proofs.list
index 03a5fa33..a4fb68f3 100644
--- a/testing/proofs/cbmc/proofs.list
+++ b/testing/proofs/cbmc/proofs.list
@@ -77,7 +77,7 @@ bpm-semver-transitive --unwind 9 --unwindset prerelease_compare.0:2,memchr.0:2,m
bpm-semver-prerelease --unwind 7
bpm-oci-challenge --unwind 18
bpm-oci-token --unwind 18
-bpm-oci-header --unwind 22
+bpm-oci-header --unwind 28
bpm-oci-descriptor --unwind 130 LEN=0,1
bpm-names --unwind 131
bpm-tree-selector --unwind 10 --malloc-may-fail --malloc-fail-null LEN=0,1
diff --git a/util/bpm_oci.c b/util/bpm_oci.c
index 970eadc8..a7c8edef 100644
--- a/util/bpm_oci.c
+++ b/util/bpm_oci.c
@@ -157,7 +157,7 @@ static int sink_restart(struct bpm_file_sink *s, int fd, __u64 room)
static size_t bpm_header(void *ptr, size_t size, size_t nmemb, void *ud)
{
struct bpm_http_response *r = ud;
- char line[4096];
+ char line[BPM_OCI_HEADER_LINE_MAX];
size_t n = size * nmemb;
char *v;
diff --git a/util/bpm_oci.h b/util/bpm_oci.h
index 922b56c5..d9c3272a 100644
--- a/util/bpm_oci.h
+++ b/util/bpm_oci.h
@@ -62,9 +62,21 @@ struct bpm_buffer {
size_t cap;
};
+/*
+ * A WWW-Authenticate challenge kept whole or dropped, and the header line
+ * bpm_header() reads or skips. A proof sets them lower: CBMC cannot hold
+ * a line of 4 KiB.
+ */
+#ifndef BPM_OCI_CHALLENGE_MAX
+#define BPM_OCI_CHALLENGE_MAX 2048
+#endif
+#ifndef BPM_OCI_HEADER_LINE_MAX
+#define BPM_OCI_HEADER_LINE_MAX 4096
+#endif
+
struct bpm_http_response {
struct bpm_buffer body;
- char www_auth[2048];
+ char www_auth[BPM_OCI_CHALLENGE_MAX];
char content_type[BPM_MAX_MEDIA];
char location[PATH_MAX];
long status;