Skip to content

eBPF-based security platform

bpm: bound a package and leave nothing of a failed step behind

Daniel Borkmann6d62cab6396e
diff --git a/builtin-bpm.c b/builtin-bpm.c
index 2375de45..6cc668cb 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -654,8 +654,8 @@ static int key_file_fingerprint(const char *path, char hex[65])
static int install_key_file(const char *src)
{
char hex[65], dst[PATH_MAX];
+ bool active, made = false;
struct stat st;
- bool active;
int rc;
rc = key_file_fingerprint(src, hex);
@@ -684,15 +684,23 @@ static int install_key_file(const char *src)
rc = copy_file_ex(src, dst, 0644, true);
if (rc)
return rc;
+ made = true;
}
if (active)
return 0;
if (symlink(dst, SHIRUDO_CERT_PATH)) {
- if (errno != EEXIST)
- return -errno;
- return cert_matches_active(src) ? -EPERM : 0;
+ if (errno != EEXIST) {
+ rc = -errno;
+ goto undo;
+ }
+ rc = cert_matches_active(src) ? -EPERM : 0;
+ goto undo;
}
- return seal_inherit_path(SHIRUDO_CERT_PATH);
+ rc = seal_inherit_path(SHIRUDO_CERT_PATH);
+undo:
+ if (rc && made)
+ unlink(dst);
+ return rc;
}
static int extract_package_cert(const char *archive, char *out, size_t out_sz)
@@ -1312,8 +1320,12 @@ static int package_install(const struct bpm_source *s, const struct bpm_index_en
char saved[PATH_MAX];
if (strsprintf(saved, sizeof(saved), "%s/%s-%s.bee", dir,
- entry->name, entry->version) >= 0)
- (void)rename(archive, saved);
+ entry->name, entry->version) < 0 ||
+ rename(archive, saved)) {
+ pr_warn("bpm: cannot keep %s; dropping it",
+ archive);
+ unlink(archive);
+ }
}
break;
}
diff --git a/util/bpm_oci.c b/util/bpm_oci.c
index 5989e759..83d69210 100644
--- a/util/bpm_oci.c
+++ b/util/bpm_oci.c
@@ -405,18 +405,21 @@ static int curl_once_ex(struct bpm_oci_client *c, const char *method,
const char *auth, struct bpm_http_response *r,
struct bpm_file_sink *file, bool follow)
{
- CURL *curl;
- struct curl_slist *headers = NULL;
- CURLcode cc;
- long status = 0;
+ struct curl_slist *headers = NULL, *next;
char errbuf[CURL_ERROR_SIZE] = {};
+ long status = 0;
+ CURLcode cc;
+ CURL *curl;
if (accept) {
char h[256];
if (strsprintf(h, sizeof(h), "Accept: %s", accept) < 0)
goto too_long;
- headers = curl_slist_append(headers, h);
+ next = curl_slist_append(headers, h);
+ if (!next)
+ goto no_memory;
+ headers = next;
}
if (content_type) {
char h[256];
@@ -424,14 +427,20 @@ static int curl_once_ex(struct bpm_oci_client *c, const char *method,
if (strsprintf(h, sizeof(h), "Content-Type: %s",
content_type) < 0)
goto too_long;
- headers = curl_slist_append(headers, h);
+ next = curl_slist_append(headers, h);
+ if (!next)
+ goto no_memory;
+ headers = next;
}
if (auth) {
char h[8192];
if (strsprintf(h, sizeof(h), "Authorization: %s", auth) < 0)
goto too_long;
- headers = curl_slist_append(headers, h);
+ next = curl_slist_append(headers, h);
+ if (!next)
+ goto no_memory;
+ headers = next;
}
curl = curl_easy_init();
if (!curl) {
@@ -506,6 +515,9 @@ static int curl_once_ex(struct bpm_oci_client *c, const char *method,
too_long:
curl_slist_free_all(headers);
return -E2BIG;
+no_memory:
+ curl_slist_free_all(headers);
+ return -ENOMEM;
}
static int curl_once(struct bpm_oci_client *c, const char *method,
@@ -751,6 +763,8 @@ int bpm_oci_get_file(struct bpm_oci_client *c, const char *url,
if (size < 0 || fd < 0)
return -EINVAL;
+ if ((__u64)size > BPM_MAX_PACKAGE_BYTES)
+ return -EFBIG;
rc = sink_restart(&sink, fd, (__u64)size);
if (rc)
return rc;
diff --git a/util/bpm_oci.h b/util/bpm_oci.h
index 451effa4..6b8fbc4f 100644
--- a/util/bpm_oci.h
+++ b/util/bpm_oci.h
@@ -27,6 +27,8 @@ struct shirudo_json_emit;
#define BPM_MAX_ENTRIES 512
#define BPM_MAX_STATE 256
#define BPM_MAX_HTTP_BODY (256u * 1024u * 1024u)
+#define BPM_MAX_PACKAGE_BYTES (256u * 1024u * 1024u)
+#define BPM_MAX_PACKAGE_ENTRIES 4096u
#define BPM_OCI_USERNAME_SIZE 256
#define BPM_OCI_PASSWORD_SIZE 4096
#define BPM_OCI_TOKEN_SIZE 4096
diff --git a/util/bpm_package.c b/util/bpm_package.c
index 0d4a5a56..e3e6dace 100644
--- a/util/bpm_package.c
+++ b/util/bpm_package.c
@@ -545,6 +545,8 @@ int bpm_pkg_extract(const char *archive, const char *dest)
{
gzFile gz = gzopen(archive, "rb");
char root[256] = {};
+ __u64 total = 0;
+ __u32 entries = 0;
__u8 h[512];
int rc = 0;
@@ -588,6 +590,12 @@ int bpm_pkg_extract(const char *archive, const char *dest)
type = h[156];
if (!type)
type = '0';
+ if (++entries > BPM_MAX_PACKAGE_ENTRIES ||
+ size > BPM_MAX_PACKAGE_BYTES - total) {
+ rc = -EFBIG;
+ break;
+ }
+ total += size;
if (!root[0]) {
slash = strchr(name, '/');
if (!slash || slash == name || (size_t)(slash - name) >= sizeof(root)) {
@@ -614,6 +622,21 @@ int bpm_pkg_extract(const char *archive, const char *dest)
rc = seal_mkdir_p(path, 0700);
if (rc)
break;
+ /* Read as the other walkers do, whatever a directory carries. */
+ remain = size;
+ while (remain) {
+ __u8 buf[65536];
+ size_t want = remain > sizeof(buf) ? sizeof(buf) : (size_t)remain;
+
+ n = gzread(gz, buf, (__u32)want);
+ if (n <= 0) {
+ rc = -EIO;
+ break;
+ }
+ remain -= (size_t)n;
+ }
+ if (rc)
+ break;
} else if (type == '0') {
char parent[PATH_MAX];
int fd;
diff --git a/util/bpm_tree_state.c b/util/bpm_tree_state.c
index 35ffe881..a58041e9 100644
--- a/util/bpm_tree_state.c
+++ b/util/bpm_tree_state.c
@@ -411,32 +411,7 @@ int tree_selector_matches(const struct shirudo_tree_selector *s)
static int tree_read_file(const char *path, __u8 **body, size_t *len)
{
- struct stat st;
- __u8 *p;
- ssize_t got;
- int fd = open(path, O_RDONLY | O_CLOEXEC);
-
- if (fd < 0)
- return -errno;
- if (fstat(fd, &st) || st.st_size < 0 || st.st_size > SHIRUDO_JSON_MAX_BYTES) {
- close(fd);
- return -E2BIG;
- }
- p = xmalloc((size_t)st.st_size + 1);
- if (!p) {
- close(fd);
- return -ENOMEM;
- }
- got = read(fd, p, (size_t)st.st_size);
- close(fd);
- if (got != st.st_size) {
- xfree(p, (size_t)st.st_size + 1);
- return -EIO;
- }
- p[got] = '\0';
- *body = p;
- *len = (size_t)got;
- return 0;
+ return read_file(path, SHIRUDO_JSON_MAX_BYTES, body, len);
}
/* hex is exactly 64 hex digits: a SHA-256 digest. */