Skip to content

eBPF-based security platform

bpm: scrub the registry credentials on every path

Daniel Borkmannf6f1e18370b2
diff --git a/builtin-bpm.c b/builtin-bpm.c
index b3224faa..e8fcb883 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -71,7 +71,7 @@ static int remove_tree(const char *path);
static int prepare_direct_install(const char *raw, struct bpm_source *source,
struct bpm_index_entry *entry)
{
- struct bpm_oci_client c = {};
+ struct bpm_oci_client c __cleanup(bpm_oci_client_clear) = {};
struct bpm_descriptor layers[8];
struct bpm_buffer manifest = {};
char repository[BPM_MAX_ENDPOINT], tag[BPM_MAX_NAME];
@@ -135,7 +135,7 @@ out:
*/
static int download_package(const char *raw, const char *destination, int fd)
{
- struct bpm_oci_client c = {};
+ struct bpm_oci_client c __cleanup(bpm_oci_client_clear) = {};
struct bpm_descriptor layers[8];
struct bpm_buffer manifest = {};
char repository[BPM_MAX_ENDPOINT], tag[BPM_MAX_NAME];
@@ -1020,7 +1020,7 @@ static int load_cached_index(const char *source, struct bpm_index_entry *entries
static int repo_update_one(const char *name)
{
struct bpm_source s;
- struct bpm_oci_client c = {};
+ struct bpm_oci_client c __cleanup(bpm_oci_client_clear) = {};
struct bpm_descriptor layers[8];
int nl = 0;
struct bpm_buffer manifest = {}, index = {}, sig = {};
@@ -1130,7 +1130,7 @@ static void sort_old_pairs(char (*unloads)[PATH_MAX], char (*loads)[PATH_MAX],
static int package_install(const struct bpm_source *s, const struct bpm_index_entry *entry,
const char *old_path, const char *old_version)
{
- struct bpm_oci_client c = {};
+ struct bpm_oci_client c __cleanup(bpm_oci_client_clear) = {};
struct bpm_descriptor layers[8];
int nl = 0;
struct bpm_buffer manifest = {};
@@ -1489,6 +1489,7 @@ static int cmd_bpm_config(int argc, const char **argv)
return 0;
}
if (!strcmp(argv[2], "add") && argc >= 5) {
+ struct bpm_oci_client probe __cleanup(bpm_oci_client_clear) = {};
struct bpm_source s = {};
const char *key = NULL;
char managed[PATH_MAX];
@@ -1507,7 +1508,7 @@ static int cmd_bpm_config(int argc, const char **argv)
}
if (!key || cert_read_file(key, &cert) ||
!bpm_name_ok(s.name) ||
- bpm_oci_parse_endpoint(s.endpoint, &(struct bpm_oci_client){})) {
+ bpm_oci_parse_endpoint(s.endpoint, &probe)) {
cert_free(&cert);
pr_err("bpm: register add requires a valid oci:// endpoint and --key-file certificate");
return 2;
@@ -1585,7 +1586,7 @@ static int tree_fetch_publication(const struct bpm_tree_config *cfg,
struct tree_publication *pub,
char *manifest_digest, size_t digest_sz)
{
- struct bpm_oci_client c = {};
+ struct bpm_oci_client c __cleanup(bpm_oci_client_clear) = {};
char repository[BPM_MAX_ENDPOINT];
int ret = bpm_oci_parse_tree_repo(cfg->endpoint, &c,
repository, sizeof(repository));
@@ -1621,7 +1622,7 @@ static int tree_verify_upatch(const char *path, const char *expected,
static int tree_pull_artifact(const struct tree_operation *op,
char *path, size_t path_sz)
{
- struct bpm_oci_client c = {};
+ struct bpm_oci_client c __cleanup(bpm_oci_client_clear) = {};
struct bpm_descriptor layers[BPM_MAX_ENTRIES];
struct bpm_buffer manifest = {}, blob = {};
char repository[BPM_MAX_ENDPOINT], url[PATH_MAX], cache[PATH_MAX];
diff --git a/include/shirudo/compiler.h b/include/shirudo/compiler.h
index 8ac81678..8d465d75 100644
--- a/include/shirudo/compiler.h
+++ b/include/shirudo/compiler.h
@@ -11,6 +11,7 @@
#define __printf(a, b) __attribute__((format(printf, a, b)))
#define __must_check __attribute__((warn_unused_result))
#define __noreturn __attribute__((noreturn))
+#define __cleanup(func) __attribute__((cleanup(func)))
/*
* An empty asm that may read memory through ptr, so that a store to that
diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.c
index 8003cc44..2d80ffe3 100644
--- a/tools/shirudo-build/bpm-build.c
+++ b/tools/shirudo-build/bpm-build.c
@@ -1733,7 +1733,7 @@ out:
static int build_download_package(const char *reference, char *path)
{
- struct bpm_oci_client client = {};
+ struct bpm_oci_client client __cleanup(bpm_oci_client_clear) = {};
struct bpm_descriptor layers[8] = {};
struct bpm_buffer manifest = {};
char repository[BPM_MAX_ENDPOINT], tag[BPM_MAX_NAME];
diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.c
index 3df7e969..db2b8bd5 100644
--- a/tools/shirudo-build/bpm-publish.c
+++ b/tools/shirudo-build/bpm-publish.c
@@ -294,7 +294,7 @@ static int push_package(const char *package_path, const char *raw_repository,
const char *index_key, const char *index_cert,
const char **reference_out)
{
- struct bpm_oci_client c = {};
+ struct bpm_oci_client c __cleanup(bpm_oci_client_clear) = {};
struct bpm_package_info info = {};
struct bpm_descriptor config, layer, key_config, key_layer, subject;
struct json_emit e = {};
@@ -602,7 +602,7 @@ static int cmd_bpm_tree_append(int argc, char **argv)
struct bpm_build_spec *spec = NULL;
__u8 *beefile = NULL, *sig = NULL;
struct bpm_package_info info = {};
- struct bpm_oci_client c = {};
+ struct bpm_oci_client c __cleanup(bpm_oci_client_clear) = {};
struct json_emit e;
char empty_config[] = "{}";
char package_dir[101];
diff --git a/util/bpm_oci.c b/util/bpm_oci.c
index 1a1ba56f..d46b2894 100644
--- a/util/bpm_oci.c
+++ b/util/bpm_oci.c
@@ -101,6 +101,11 @@ static int bpm_buffer_append(struct bpm_buffer *b, const void *data, size_t len)
return 0;
}
+void bpm_oci_client_clear(struct bpm_oci_client *c)
+{
+ memzero_explicit(c, sizeof(*c));
+}
+
void bpm_buffer_free(struct bpm_buffer *b)
{
if (b->data)
@@ -201,9 +206,9 @@ static int decode_basic(const char *in, char *user, size_t usz,
char *pass, size_t psz)
{
__u8 out[BPM_OCI_BASIC_RAW_SIZE];
+ int bits = -8, ret = -EINVAL, x;
size_t n = 0;
__u32 val = 0;
- int bits = -8, x;
char *colon;
for (; *in && *in != '"' && n < sizeof(out); in++) {
@@ -211,7 +216,7 @@ static int decode_basic(const char *in, char *user, size_t usz,
continue;
x = base64_value((__u8)*in);
if (x < 0)
- return -EINVAL;
+ goto out;
/* No more than 12 bits are ever pending. */
val = ((val << 6) | (__u32)x) & 0xfff;
bits += 6;
@@ -220,22 +225,25 @@ static int decode_basic(const char *in, char *user, size_t usz,
bits -= 8;
}
}
- if (n >= sizeof(out))
- return -E2BIG;
- out[n] = '\0';
- if (memchr(out, '\0', n)) {
- memzero_explicit(out, sizeof(out));
- return -EINVAL;
+ if (n >= sizeof(out)) {
+ ret = -E2BIG;
+ goto out;
}
+ out[n] = '\0';
+ if (memchr(out, '\0', n))
+ goto out;
colon = memchr(out, ':', n);
if (!colon || (size_t)(colon - (char *)out) >= usz ||
n - (size_t)(colon - (char *)out) - 1 >= psz)
- return -EINVAL;
+ goto out;
memcpy(user, out, (size_t)(colon - (char *)out));
user[colon - (char *)out] = '\0';
memcpy(pass, colon + 1, n - (size_t)(colon - (char *)out) - 1);
pass[n - (size_t)(colon - (char *)out) - 1] = '\0';
- return 0;
+ ret = 0;
+out:
+ memzero_explicit(out, sizeof(out));
+ return ret;
}
static int encode_basic(const char *user, const char *pass,
@@ -245,13 +253,18 @@ static int encode_basic(const char *user, const char *pass,
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
char raw[BPM_OCI_BASIC_RAW_SIZE];
size_t len, need, i, o = 0;
+ int ret = 0;
- if (strsprintf(raw, sizeof(raw), "%s:%s", user, pass) < 0)
- return -E2BIG;
+ if (strsprintf(raw, sizeof(raw), "%s:%s", user, pass) < 0) {
+ ret = -E2BIG;
+ goto out;
+ }
len = strlen(raw);
need = 4 * ((len + 2) / 3) + 1;
- if (need > out_sz)
- return -ENOSPC;
+ if (need > out_sz) {
+ ret = -ENOSPC;
+ goto out;
+ }
for (i = 0; i < len; i += 3) {
__u32 v = (__u8)raw[i] << 16;
@@ -265,7 +278,9 @@ static int encode_basic(const char *user, const char *pass,
out[o++] = i + 2 < len ? alphabet[v & 63] : '=';
}
out[o] = '\0';
- return 0;
+out:
+ memzero_explicit(raw, sizeof(raw));
+ return ret;
}
static int load_registry_auth(const char *registry, struct bpm_oci_client *c)
@@ -437,10 +452,13 @@ static int curl_once_ex(struct bpm_oci_client *c, const char *method,
}
if (auth) {
char h[8192];
+ int n;
- if (strsprintf(h, sizeof(h), "Authorization: %s", auth) < 0)
+ n = strsprintf(h, sizeof(h), "Authorization: %s", auth);
+ next = n < 0 ? NULL : curl_slist_append(headers, h);
+ memzero_explicit(h, sizeof(h));
+ if (n < 0)
goto too_long;
- next = curl_slist_append(headers, h);
if (!next)
goto no_memory;
headers = next;
@@ -697,6 +715,8 @@ static int get_bearer_token(struct bpm_oci_client *c, const char *challenge)
goto out;
ret = token_from_body((char *)r.body.data, c->token, sizeof(c->token));
out:
+ memzero_explicit(basic, sizeof(basic));
+ memzero_explicit(auth_header, sizeof(auth_header));
curl_free(esc_service);
curl_free(esc_scope);
curl_slist_free_all(headers);
@@ -719,31 +739,29 @@ static int get_memory(struct bpm_oci_client *c, const char *url,
if (r.status == 401 && r.www_auth[0]) {
if (!strncasecmp(r.www_auth, "Bearer", 6)) {
ret = get_bearer_token(c, r.www_auth);
- if (ret)
- goto out;
- if (strsprintf(auth, sizeof(auth), "Bearer %s",
- c->token) < 0) {
+ if (!ret && strsprintf(auth, sizeof(auth), "Bearer %s",
+ c->token) < 0)
ret = -E2BIG;
- goto out;
- }
} else if (!strncasecmp(r.www_auth, "Basic", 5) && c->username[0]) {
char encoded[BPM_OCI_BASIC_ENCODED_SIZE];
ret = encode_basic(c->username, c->password, encoded, sizeof(encoded));
- if (ret)
- goto out;
- if (strsprintf(auth, sizeof(auth), "Basic %s",
- encoded) < 0) {
+ if (!ret && strsprintf(auth, sizeof(auth), "Basic %s",
+ encoded) < 0)
ret = -E2BIG;
- goto out;
- }
+ memzero_explicit(encoded, sizeof(encoded));
} else {
ret = -EACCES;
- goto out;
}
- bpm_buffer_free(&r.body);
- r = (struct bpm_http_response){};
- ret = curl_once(c, "GET", url, accept, auth, &r, NULL, follow);
+ if (!ret) {
+ bpm_buffer_free(&r.body);
+ r = (struct bpm_http_response){};
+ ret = curl_once(c, "GET", url, accept, auth, &r, NULL,
+ follow);
+ }
+ memzero_explicit(auth, sizeof(auth));
+ if (ret)
+ goto out;
}
if (!ret && (r.status < 200 || r.status >= 300))
ret = r.status == 404 ? -ENOENT : -EACCES;
@@ -807,12 +825,14 @@ int bpm_oci_get_file(struct bpm_oci_client *c, const char *url,
if (!ret && strsprintf(auth, sizeof(auth), "Basic %s",
encoded) < 0)
ret = -E2BIG;
+ memzero_explicit(encoded, sizeof(encoded));
if (!ret) {
bpm_buffer_free(&r.body);
r = (struct bpm_http_response){};
ret = curl_once(c, "GET", url, accept, auth, &r, &sink, true);
}
}
+ memzero_explicit(auth, sizeof(auth));
/* A failed write ends the transfer too; it, not the transfer, is the cause. */
if (sink.overflow)
ret = -EFBIG;
@@ -845,28 +865,29 @@ static int bpm_oci_request(struct bpm_oci_client *c, const char *method,
if (r->status == 401 && r->www_auth[0]) {
if (!strncasecmp(r->www_auth, "Bearer", 6)) {
ret = get_bearer_token(c, r->www_auth);
- if (ret)
- return ret;
- if (strsprintf(auth, sizeof(auth), "Bearer %s",
- c->token) < 0)
- return -E2BIG;
+ if (!ret && strsprintf(auth, sizeof(auth), "Bearer %s",
+ c->token) < 0)
+ ret = -E2BIG;
} else if (!strncasecmp(r->www_auth, "Basic", 5) && c->username[0]) {
char encoded[BPM_OCI_BASIC_ENCODED_SIZE];
ret = encode_basic(c->username, c->password, encoded,
sizeof(encoded));
- if (ret)
- return ret;
- if (strsprintf(auth, sizeof(auth), "Basic %s",
- encoded) < 0)
- return -E2BIG;
+ if (!ret && strsprintf(auth, sizeof(auth), "Basic %s",
+ encoded) < 0)
+ ret = -E2BIG;
+ memzero_explicit(encoded, sizeof(encoded));
} else {
- return -EACCES;
+ ret = -EACCES;
+ }
+ if (!ret) {
+ bpm_buffer_free(&r->body);
+ *r = (struct bpm_http_response){};
+ ret = curl_once_ex(c, method, url, accept, content_type,
+ body, body_len, auth, r, NULL,
+ false);
}
- bpm_buffer_free(&r->body);
- *r = (struct bpm_http_response){};
- ret = curl_once_ex(c, method, url, accept, content_type, body,
- body_len, auth, r, NULL, false);
+ memzero_explicit(auth, sizeof(auth));
if (ret)
return ret;
}
diff --git a/util/bpm_oci.h b/util/bpm_oci.h
index 1a3227fd..956142e2 100644
--- a/util/bpm_oci.h
+++ b/util/bpm_oci.h
@@ -97,6 +97,12 @@ int bpm_sha256_hex(const void *data, size_t len, char *hex, size_t hex_sz);
bool bpm_digest_matches(const char *want, const char *hex);
void bpm_buffer_free(struct bpm_buffer *b);
+/*
+ * Scrub the credentials and token a client holds; declare a client on
+ * the stack with __cleanup(bpm_oci_client_clear).
+ */
+void bpm_oci_client_clear(struct bpm_oci_client *c);
+
/*
* Registry access. Endpoints are oci://host[:port]; BPM_OCI_SCHEME,
* BPM_OCI_CA_FILE, BPM_OCI_USERNAME/PASSWORD, REGISTRY_AUTH_FILE and