Skip to content

eBPF-based security platform

bpm: follow registry redirects for blobs fetched into memory

Daniel Borkmann1977be621c90
diff --git a/builtin-bpm.c b/builtin-bpm.c
index f8d92c28..d1e6caef 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -772,7 +772,7 @@ static int repo_update_one(const char *name)
if (!strcmp(layers[i].media, BPM_INDEX_MEDIA)) {
char layer_hex[65];
if ((rc = bpm_oci_url(&c, BPM_INDEX_REPOSITORY, "blobs", layers[i].digest, url, sizeof(url)))) goto out;
- rc = bpm_oci_get_memory(&c, url, BPM_INDEX_MEDIA, &index);
+ rc = bpm_oci_get_blob(&c, url, BPM_INDEX_MEDIA, &index);
if (!rc && (bpm_sha256_hex(index.data, index.len, layer_hex, sizeof(layer_hex)) ||
!bpm_digest_matches(layers[i].digest, layer_hex)))
rc = -EKEYREJECTED;
@@ -783,7 +783,7 @@ static int repo_update_one(const char *name)
for (int i = 0; i < nl; i++) if (!strcmp(layers[i].media, BPM_INDEX_SIG_MEDIA)) {
char sig_hex[65];
if ((rc = bpm_oci_url(&c, BPM_INDEX_REPOSITORY, "blobs", layers[i].digest, url, sizeof(url)))) goto out;
- rc = bpm_oci_get_memory(&c, url, BPM_INDEX_SIG_MEDIA, &sig);
+ rc = bpm_oci_get_blob(&c, url, BPM_INDEX_SIG_MEDIA, &sig);
if (!rc && (bpm_sha256_hex(sig.data, sig.len, sig_hex, sizeof(sig_hex)) ||
!bpm_digest_matches(layers[i].digest, sig_hex)))
rc = -EKEYREJECTED;
@@ -1205,7 +1205,7 @@ static int tree_pull_artifact(const struct shirudo_tree_operation *op,
url, sizeof(url));
if (rc)
goto out;
- rc = bpm_oci_get_memory(&c, url, layers[i].media, &blob);
+ rc = bpm_oci_get_blob(&c, url, layers[i].media, &blob);
if (rc)
goto out;
if (bpm_sha256_hex(blob.data, blob.len, hex, sizeof(hex)) ||
diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.c
index f70832b6..6dc7f069 100644
--- a/tools/shirudo-build/bpm-publish.c
+++ b/tools/shirudo-build/bpm-publish.c
@@ -145,7 +145,7 @@ static int fetch_index(struct bpm_oci_client *c, const char *cert,
url, sizeof(url));
if (rc)
goto out;
- rc = bpm_oci_get_memory(c, url, layers[i].media, dst);
+ rc = bpm_oci_get_blob(c, url, layers[i].media, dst);
if (rc)
goto out;
if ((layers[i].size >= 0 && (int64_t)dst->len != layers[i].size) ||
diff --git a/util/bpm_oci.c b/util/bpm_oci.c
index de77c75f..66df2e6d 100644
--- a/util/bpm_oci.c
+++ b/util/bpm_oci.c
@@ -325,7 +325,8 @@ int bpm_oci_parse_endpoint(const char *endpoint, struct bpm_oci_client *c)
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,
- const char *auth, struct bpm_http_response *r, FILE *file)
+ const char *auth, struct bpm_http_response *r, FILE *file,
+ bool follow)
{
CURL *curl;
struct curl_slist *headers = NULL;
@@ -359,10 +360,10 @@ static int curl_once_ex(struct bpm_oci_client *c, const char *method,
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
/*
* OCI registries commonly redirect blob GETs to a signed CDN URL.
- * Follow only for file downloads, never for manifest or upload calls.
+ * Follow only for blobs, never for manifest or upload calls.
*/
- curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, file ? 1L : 0L);
- if (file) {
+ curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, follow ? 1L : 0L);
+ if (follow) {
const char *redirect_protocols =
!strncmp(c->base_url, "https://", 8) ? "https" : "http,https";
@@ -416,9 +417,10 @@ static int curl_once_ex(struct bpm_oci_client *c, const char *method,
static int curl_once(struct bpm_oci_client *c, const char *method,
const char *url, const char *accept, const char *auth,
- struct bpm_http_response *r, FILE *file)
+ struct bpm_http_response *r, FILE *file, bool follow)
{
- return curl_once_ex(c, method, url, accept, NULL, NULL, 0, auth, r, file);
+ return curl_once_ex(c, method, url, accept, NULL, NULL, 0, auth, r, file,
+ follow);
}
static int parse_challenge(const char *header, char *realm, size_t rsz,
@@ -551,14 +553,14 @@ out:
return rc;
}
-int bpm_oci_get_memory(struct bpm_oci_client *c, const char *url,
- const char *accept, struct bpm_buffer *out)
+static int get_memory(struct bpm_oci_client *c, const char *url,
+ const char *accept, struct bpm_buffer *out, bool follow)
{
struct bpm_http_response r = {};
char auth[BPM_OCI_AUTH_VALUE_SIZE];
int rc;
- rc = curl_once(c, "GET", url, accept, NULL, &r, NULL);
+ rc = curl_once(c, "GET", url, accept, NULL, &r, NULL, follow);
if (rc) goto out;
if (r.status == 401 && r.www_auth[0]) {
if (!strncasecmp(r.www_auth, "Bearer", 6)) {
@@ -572,7 +574,7 @@ int bpm_oci_get_memory(struct bpm_oci_client *c, const char *url,
snprintf(auth, sizeof(auth), "Basic %s", encoded);
} else { rc = -EACCES; goto out; }
bpm_buffer_free(&r.body); r = (struct bpm_http_response){};
- rc = curl_once(c, "GET", url, accept, auth, &r, NULL);
+ rc = curl_once(c, "GET", url, accept, auth, &r, NULL, follow);
}
if (!rc && (r.status < 200 || r.status >= 300)) rc = r.status == 404 ? -ENOENT : -EACCES;
if (!rc) { *out = r.body; r.body.data = NULL; }
@@ -581,6 +583,18 @@ out:
return rc;
}
+int bpm_oci_get_memory(struct bpm_oci_client *c, const char *url,
+ const char *accept, struct bpm_buffer *out)
+{
+ return get_memory(c, url, accept, out, false);
+}
+
+int bpm_oci_get_blob(struct bpm_oci_client *c, const char *url,
+ const char *accept, struct bpm_buffer *out)
+{
+ return get_memory(c, url, accept, out, true);
+}
+
int bpm_oci_get_file(struct bpm_oci_client *c, const char *url,
const char *accept, const char *path)
{
@@ -591,7 +605,7 @@ int bpm_oci_get_file(struct bpm_oci_client *c, const char *url,
fp = fopen(path, "wb");
if (!fp) return -errno;
- rc = curl_once(c, "GET", url, accept, NULL, &r, fp);
+ rc = curl_once(c, "GET", url, accept, NULL, &r, fp, true);
if (!rc && r.status == 401 && r.www_auth[0] && !strncasecmp(r.www_auth, "Bearer", 6)) {
fclose(fp); unlink(path); fp = fopen(path, "wb");
if (!fp) {
@@ -603,7 +617,7 @@ int bpm_oci_get_file(struct bpm_oci_client *c, const char *url,
if (!rc) {
snprintf(auth, sizeof(auth), "Bearer %s", c->token);
memset(&r, 0, sizeof(r));
- rc = curl_once(c, "GET", url, accept, auth, &r, fp);
+ rc = curl_once(c, "GET", url, accept, auth, &r, fp, true);
}
} else if (!rc && r.status == 401 && r.www_auth[0] &&
!strncasecmp(r.www_auth, "Basic", 5) && c->username[0]) {
@@ -618,7 +632,7 @@ int bpm_oci_get_file(struct bpm_oci_client *c, const char *url,
if (!rc) {
snprintf(auth, sizeof(auth), "Basic %s", encoded);
memset(&r, 0, sizeof(r));
- rc = curl_once(c, "GET", url, accept, auth, &r, fp);
+ rc = curl_once(c, "GET", url, accept, auth, &r, fp, true);
}
}
if (!rc && (r.status < 200 || r.status >= 300)) rc = r.status == 404 ? -ENOENT : -EACCES;
@@ -643,7 +657,7 @@ static int bpm_oci_request(struct bpm_oci_client *c, const char *method,
*r = (struct bpm_http_response){};
rc = curl_once_ex(c, method, url, accept, content_type, body, body_len,
- NULL, r, NULL);
+ NULL, r, NULL, false);
if (rc)
return rc;
if (r->status == 401 && r->www_auth[0]) {
@@ -666,7 +680,7 @@ static int bpm_oci_request(struct bpm_oci_client *c, const char *method,
bpm_buffer_free(&r->body);
*r = (struct bpm_http_response){};
rc = curl_once_ex(c, method, url, accept, content_type, body,
- body_len, auth, r, NULL);
+ body_len, auth, r, NULL, false);
if (rc)
return rc;
}
diff --git a/util/bpm_oci.h b/util/bpm_oci.h
index 4019a5e5..a001d63a 100644
--- a/util/bpm_oci.h
+++ b/util/bpm_oci.h
@@ -106,9 +106,14 @@ int bpm_oci_parse_push_repo(const char *raw, struct bpm_oci_client *c,
char *repository, size_t repository_sz);
int bpm_oci_url(const struct bpm_oci_client *c, const char *repo,
const char *kind, const char *ref, char *out, size_t sz);
-/* 2xx fills out; 404 is -ENOENT, other failures -EACCES. */
+/*
+ * 2xx fills out; 404 is -ENOENT, other failures -EACCES. A blob GET
+ * follows the registry's redirects, a manifest GET does not.
+ */
int bpm_oci_get_memory(struct bpm_oci_client *c, const char *url,
const char *accept, struct bpm_buffer *out);
+int bpm_oci_get_blob(struct bpm_oci_client *c, const char *url,
+ const char *accept, struct bpm_buffer *out);
int bpm_oci_get_file(struct bpm_oci_client *c, const char *url,
const char *accept, const char *path);
int bpm_oci_push_blob(struct bpm_oci_client *c, const char *repository,
diff --git a/util/bpm_package.c b/util/bpm_package.c
index d8f6bf02..d01e822a 100644
--- a/util/bpm_package.c
+++ b/util/bpm_package.c
@@ -442,7 +442,8 @@ int bpm_tree_fetch_publication(struct bpm_oci_client *c,
if ((rc = bpm_oci_url(c, repository, "blobs", layers[i].digest,
url, sizeof(url))))
goto out;
- if ((rc = bpm_oci_get_memory(c, url, layers[i].media, dst)))
+ rc = bpm_oci_get_blob(c, url, layers[i].media, dst);
+ if (rc)
goto out;
if ((layers[i].size >= 0 && (int64_t)dst->len != layers[i].size) ||
bpm_sha256_hex(dst->data, dst->len, hex, sizeof(hex)) ||