eBPF-based security platform
bpm: refuse a repository or a title the buffer would cut
Daniel Borkmann4d1115ed74d2
diff --git a/builtin-bpm.c b/builtin-bpm.cindex 7f62f979..9e3afd38 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -168,7 +168,9 @@ static int download_package(const char *raw, const char *destination, int fd)url, sizeof(url));if (rc)goto out;- bpm_trim(layers[i].title, title, sizeof(title));+ /* A cut title would name another file; fall back to the package name. */+ if (bpm_trim(layers[i].title, title, sizeof(title)))+ title[0] = '\0';if (destination) {bpm_copy_string(filename, sizeof(filename), destination);} else if (title[0] && !strchr(title, '/') &&diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.cindex 4bc98882..19479a31 100644--- a/tools/shirudo-build/bpm-publish.c+++ b/tools/shirudo-build/bpm-publish.c@@ -703,7 +703,11 @@ static int cmd_bpm_tree_append(int argc, char **argv)}if (shirudo_build_check_keys(key, cert))return -EACCES;- bpm_trim(raw_repository, repo_ref, sizeof(repo_ref));+ if (bpm_trim(raw_repository, repo_ref, sizeof(repo_ref))) {+ pr_err("bpm tree append: repository is longer than %zu bytes",+ sizeof(repo_ref) - 1);+ return -ENAMETOOLONG;+ }rc = bpm_pkg_load_info(package, &info);if (rc)diff --git a/util/bpm_oci.c b/util/bpm_oci.cindex 83d69210..00559a55 100644--- a/util/bpm_oci.c+++ b/util/bpm_oci.c@@ -1063,19 +1063,23 @@ int bpm_oci_parse_tree_repo(const char *raw, struct bpm_oci_client *c,return bpm_oci_parse_endpoint(endpoint, c);}-void bpm_trim(const char *in, char *out, size_t out_sz)+int bpm_trim(const char *in, char *out, size_t out_sz){const char *start = in, *end;+ int rc = 0;while (*start && isspace((__u8)*start))start++;end = start + strlen(start);while (end > start && isspace((__u8)end[-1]))end--;- if ((size_t)(end - start) >= out_sz)+ if ((size_t)(end - start) >= out_sz) {end = start + out_sz - 1;+ rc = -E2BIG;+ }memmove(out, start, end - start);out[end - start] = '\0';+ return rc;}int bpm_oci_url(const struct bpm_oci_client *c, const char *repo,diff --git a/util/bpm_oci.h b/util/bpm_oci.hindex 6b8fbc4f..03b568c1 100644--- a/util/bpm_oci.h+++ b/util/bpm_oci.h@@ -87,7 +87,8 @@ struct bpm_descriptor {};void bpm_copy_string(char *dst, size_t dst_sz, const char *src);-void bpm_trim(const char *in, char *out, size_t out_sz);+/* Blanks trimmed off both ends; -E2BIG when out had to cut it, as strscpy. */+int bpm_trim(const char *in, char *out, size_t out_sz);bool bpm_name_ok(const char *s);bool bpm_version_ok(const char *s);bool bpm_package_name_ok(const char *s);