eBPF-based security platform
bpm: accept authentication challenges only from the registry
KP Singh3971958a98f6
diff --git a/Documentation/concepts/background/oci.rst b/Documentation/concepts/background/oci.rstindex e446ab13..497d5ef5 100644--- a/Documentation/concepts/background/oci.rst+++ b/Documentation/concepts/background/oci.rst@@ -101,6 +101,10 @@ authentication instead. Container tools keep one base64``user:password`` per registry host in the ``auths`` object of a``config.json``.+Shirudo follows blob redirects to storage hosts. Only the configured+registry's scheme, host and port may return a challenge that starts+authentication. The registry may name a separate token service.+How shirudo uses a registry---------------------------diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex bb1dcb16..a9a7cb83 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -241,10 +241,10 @@ or shorter, and "lengths 0 to 6" every input of each of those lengths.- Header lines up to 26 bytes, NULs included, with the line's copyand the challenge's field set to 24 and 4 bytes for their 4096and 2048- - The whole line taken, and one too long for the copy or holding a- NUL read not at all; values terminated, without a leading blank,- a location without CR or LF; a challenge that fits kept whole, a- longer one dropped rather than cut+ - A new HTTP response clears the previous challenge and location.+ Other lines too long for the copy or containing a NUL are ignored.+ Values are terminated and leading blanks skipped. A location has+ no CR or LF. A challenge must fit in full.* - ``bpm-oci-buffer``- ``bpm_buffer_append()``- A buffer of 0, 3, 8, 12 and 15 bytes holding any shorter body,diff --git a/testing/proofs/cbmc/bpm-oci-header.c b/testing/proofs/cbmc/bpm-oci-header.cindex 43419034..b5e4f945 100644--- a/testing/proofs/cbmc/bpm-oci-header.c+++ b/testing/proofs/cbmc/bpm-oci-header.c@@ -7,7 +7,7 @@* line, reads none longer than its copy nor one holding a NUL, and the* challenge and location it keeps are terminated inside their fields, the* location without a line break, a challenge longer than its field- * dropped rather than cut.+ * dropped rather than cut. A new HTTP response clears both prior fields.* 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.@@ -85,8 +85,17 @@ void harness(void)size_t n;__CPROVER_assume(n <= LINE_MAX);+ if (named(line, n, "HTTP/")) {+ r.www_auth[0] = 'a';+ r.location[0] = 'b';+ }__CPROVER_assert(bpm_header(line, 1, n, &r) == n,"the whole line taken");+ if (named(line, n, "HTTP/")) {+ __CPROVER_assert(!r.www_auth[0] && !r.location[0],+ "no headers carried to a new response");+ return;+ }if (n >= BPM_OCI_HEADER_LINE_MAX) {__CPROVER_assert(!r.www_auth[0] && !r.location[0],"a line longer than the copy read not at all");diff --git a/testing/unit/bpm_oci.c b/testing/unit/bpm_oci.cindex bbdd030c..40e5674c 100644--- a/testing/unit/bpm_oci.c+++ b/testing/unit/bpm_oci.c@@ -186,7 +186,29 @@ static void token_refused_leaves_none(void)}}+static void registry_owns_the_auth_challenge(void)+{+ const char *base = "https://Registry.example";+ struct bpm_http_response r = {};+ char header[] = "WWW-Authenticate: Basic realm=registry\r\n";+ char status[] = "HTTP/2 401\r\n";++ EXPECT_TRUE(registry_origin(base, "https://registry.example:443/blob"));+ EXPECT_FALSE(registry_origin(base, "https://cdn.example/blob"));+ EXPECT_FALSE(registry_origin(base, "https://registry.example:444/blob"));+ EXPECT_FALSE(registry_origin(base, "http://registry.example/blob"));+ EXPECT_FALSE(registry_origin(base, NULL));+ EXPECT_FALSE(registry_origin("https://[fe80::1%25eth0]",+ "https://[fe80::1%25eth1]/blob"));++ bpm_header(header, 1, sizeof(header) - 1, &r);+ EXPECT_TRUE(r.www_auth[0]);+ bpm_header(status, 1, sizeof(status) - 1, &r);+ EXPECT_FALSE(r.www_auth[0]);+}+static const struct unit_case cases[] = {+ UNIT_CASE(registry_owns_the_auth_challenge),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),diff --git a/util/bpm_oci.c b/util/bpm_oci.cindex 27f44a73..0246c091 100644--- a/util/bpm_oci.c+++ b/util/bpm_oci.c@@ -161,6 +161,12 @@ static size_t bpm_header(void *ptr, size_t size, size_t nmemb, void *ud)size_t n = size * nmemb;char *v;+ /* A redirect, proxy response or 1xx starts a separate header block. */+ if (span_has_prefix_nocase(ptr, n, "HTTP/")) {+ r->www_auth[0] = '\0';+ r->location[0] = '\0';+ return n;+ }if (span_to_str(line, sizeof(line), ptr, n))return n;v = line;@@ -422,6 +428,43 @@ static int bpm_curl_deadlines(CURL *curl, __u64 room)return 0;}+static bool origin_part_matches(CURLU *registry, CURLU *peer, CURLUPart part)+{+ char *a = NULL, *b = NULL;+ CURLUcode ra, rb;+ bool match;++ ra = curl_url_get(registry, part, &a, CURLU_DEFAULT_PORT);+ rb = curl_url_get(peer, part, &b, CURLU_DEFAULT_PORT);+ if (ra || rb)+ match = part == CURLUPART_ZONEID &&+ ra == CURLUE_NO_ZONEID && rb == CURLUE_NO_ZONEID;+ else if (part == CURLUPART_ZONEID)+ match = !strcmp(a, b);+ else+ match = str_equals_nocase(a, b);+ curl_free(a);+ curl_free(b);+ return match;+}++static bool registry_origin(const char *base, const char *url)+{+ CURLU *registry = curl_url(), *peer = curl_url();+ bool match;++ match = registry && peer && url &&+ !curl_url_set(registry, CURLUPART_URL, base, CURLU_DISALLOW_USER) &&+ !curl_url_set(peer, CURLUPART_URL, url, CURLU_DISALLOW_USER) &&+ origin_part_matches(registry, peer, CURLUPART_SCHEME) &&+ origin_part_matches(registry, peer, CURLUPART_HOST) &&+ origin_part_matches(registry, peer, CURLUPART_PORT) &&+ origin_part_matches(registry, peer, CURLUPART_ZONEID);+ curl_url_cleanup(peer);+ curl_url_cleanup(registry);+ return match;+}+static int curl_once_ex(struct bpm_oci_client *c, const char *method,const char *url, const char *accept,const char *content_type, const void *body, size_t body_len,@@ -430,6 +473,7 @@ static int curl_once_ex(struct bpm_oci_client *c, const char *method,{struct curl_slist *headers = NULL, *next;char errbuf[CURL_ERROR_SIZE] = {};+ char *effective = NULL;long status = 0;CURLcode cc;CURL *curl;@@ -545,6 +589,12 @@ static int curl_once_ex(struct bpm_oci_client *c, const char *method,cc = curl_easy_perform(curl);curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);r->status = status;+ /* A retry starts at url. Only the registry may ask for one. */+ if (r->www_auth[0] &&+ (status != 401 || !registry_origin(c->base_url, url) ||+ curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effective) ||+ !registry_origin(c->base_url, effective)))+ r->www_auth[0] = '\0';if (cc != CURLE_OK) {pr_err("bpm OCI request %s: %s", url, errbuf[0] ? errbuf : curl_easy_strerror(cc));status = -1;