Skip to content

eBPF-based security platform

bpm: check every string copy of the package and tree readers

Daniel Borkmann66c672ab0a4b
diff --git a/builtin-bpm.c b/builtin-bpm.c
index f2a7081c..2f849485 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -1294,7 +1294,11 @@ static int package_install(const struct bpm_source *s, const struct bpm_index_en
ret = -E2BIG;
break;
}
- strscpy(loaded[nloaded], up, sizeof(loaded[nloaded]));
+ if (strscpy(loaded[nloaded], up,
+ sizeof(loaded[nloaded])) < 0) {
+ ret = -ENAMETOOLONG;
+ break;
+ }
nloaded++;
}
closedir(d);
diff --git a/util/bpm_oci.c b/util/bpm_oci.c
index 114a5e24..2e4c5494 100644
--- a/util/bpm_oci.c
+++ b/util/bpm_oci.c
@@ -365,7 +365,7 @@ static int load_registry_auth(const char *registry, struct bpm_oci_client *c)
int bpm_oci_parse_endpoint(const char *endpoint, struct bpm_oci_client *c)
{
const char *p, *ca_file, *scheme = getenv("BPM_OCI_SCHEME");
- char transport[16] = "https";
+ const char *transport = "https";
p = endpoint;
if (!p || !str_skip_prefix(&p, "oci://"))
@@ -377,7 +377,7 @@ int bpm_oci_parse_endpoint(const char *endpoint, struct bpm_oci_client *c)
pr_err("bpm OCI: BPM_OCI_SCHEME must be https or http");
return -EINVAL;
}
- strscpy(transport, scheme, sizeof(transport));
+ transport = scheme;
}
if (strscpy(c->registry, p, sizeof(c->registry)) < 0)
return -ENAMETOOLONG;
diff --git a/util/bpm_package.c b/util/bpm_package.c
index 7a63cf82..62f5ebfc 100644
--- a/util/bpm_package.c
+++ b/util/bpm_package.c
@@ -604,7 +604,10 @@ static int visit_members_gz(gzFile gz, const char *package_dir,
ret = -EINVAL;
break;
}
- strscpy(name, (char *)h, sizeof(name));
+ if (strscpy(name, (char *)h, sizeof(name)) < 0) {
+ ret = -EINVAL;
+ break;
+ }
type = h[156] ? h[156] : '0';
name_len = strlen(name);
if (type == '5' && name_len && name[name_len - 1] == '/')
@@ -638,7 +641,10 @@ static int visit_members_gz(gzFile gz, const char *package_dir,
ret = -EPERM;
break;
}
- strscpy(rel, below + (*below == '/'), sizeof(rel));
+ if (strscpy(rel, below + (*below == '/'), sizeof(rel)) < 0) {
+ ret = -EINVAL;
+ break;
+ }
if (!*rel) {
if (type != '5') {
ret = -EINVAL;
@@ -753,7 +759,10 @@ int bpm_pkg_extract(const char *archive, const char *dest)
ret = -EINVAL;
break;
}
- strscpy(name, (char *)h, sizeof(name));
+ if (strscpy(name, (char *)h, sizeof(name)) < 0) {
+ ret = -EINVAL;
+ break;
+ }
if (!bpm_archive_rel_ok(name) || bpm_tar_octal((char *)h + 124, 12, &size)) {
ret = -EINVAL;
break;
@@ -781,8 +790,8 @@ int bpm_pkg_extract(const char *archive, const char *dest)
ret = -EINVAL;
break;
}
- strscpy(rel, below + 1, sizeof(rel));
- if ((!*rel && type != '5') || (*rel && !bpm_archive_rel_ok(rel))) {
+ if (strscpy(rel, below + 1, sizeof(rel)) < 0 ||
+ (!*rel && type != '5') || (*rel && !bpm_archive_rel_ok(rel))) {
ret = -EINVAL;
break;
}
@@ -813,7 +822,10 @@ int bpm_pkg_extract(const char *archive, const char *dest)
char parent[PATH_MAX];
int fd;
- strscpy(parent, path, sizeof(parent));
+ if (strscpy(parent, path, sizeof(parent)) < 0) {
+ ret = -ENAMETOOLONG;
+ break;
+ }
slash = strrchr(parent, '/');
if (slash) {
*slash = 0;
diff --git a/util/bpm_tree.c b/util/bpm_tree.c
index 086a0ef2..3eeddd02 100644
--- a/util/bpm_tree.c
+++ b/util/bpm_tree.c
@@ -153,9 +153,10 @@ static int tree_list_value(void *ctx)
{
struct tree_list *l = ctx;
- if (*l->n >= TREE_MAX_SELECTOR_VALUES)
+ if (*l->n >= TREE_MAX_SELECTOR_VALUES ||
+ strscpy(l->values[*l->n], l->r->value, sizeof(l->values[0])) < 0)
return -E2BIG;
- strscpy(l->values[(*l->n)++], l->r->value, TREE_STRING_MAX);
+ (*l->n)++;
return 0;
}
@@ -179,7 +180,8 @@ static int tree_map_pair(void *ctx, const char *key)
ret = tree_selector_kv_add(m->kvs, m->n, m->max, &kv);
if (ret)
return ret;
- strscpy(kv->key, key, sizeof(kv->key));
+ if (strscpy(kv->key, key, sizeof(kv->key)) < 0)
+ return -E2BIG;
memcpy(kv->values, r->pair, sizeof(kv->values));
kv->n_values = r->n_pair;
r->n_pair = 0;