eBPF-based security platform
bpm: copy strings with strscpy()
Daniel Borkmannda22addded2d
diff --git a/Makefile.shirudo b/Makefile.shirudoindex 3d0806e8..9d172729 100644--- a/Makefile.shirudo+++ b/Makefile.shirudo@@ -608,7 +608,8 @@ testing/helper/test-config: testing/helper/test-config.c util/config.o util/io.o@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) -o $@ $^-testing/helper/test-bpm-source: testing/helper/test-bpm-source.c util/bpm_source_config.o+testing/helper/test-bpm-source: testing/helper/test-bpm-source.c util/bpm_source_config.o \+ util/string.o@printf ' CC $@\n'$(Q)$(CC) $(CFLAGS) -o $@ $^diff --git a/builtin-bpm.c b/builtin-bpm.cindex 9fc9a360..ec07fe7f 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -287,7 +287,7 @@ static int load_source(const char *name, struct bpm_source *s)fclose(fp);return -EPERM;}- snprintf(s->name, sizeof(s->name), "%s", name);+ strscpy(s->name, name, sizeof(s->name));while (fgets(line, sizeof(line), fp)) {rc = bpm_parse_source_line(line,s->endpoint, sizeof(s->endpoint),@@ -1037,7 +1037,7 @@ static int package_install(const struct bpm_source *s, const struct bpm_index_enrc = -E2BIG;break;}- snprintf(loaded[nloaded], sizeof(loaded[nloaded]), "%s", up);+ strscpy(loaded[nloaded], up, sizeof(loaded[nloaded]));nloaded++;}closedir(d);diff --git a/testing/helper/test-bpm-source.c b/testing/helper/test-bpm-source.cindex 89247918..e0196a3d 100644--- a/testing/helper/test-bpm-source.c+++ b/testing/helper/test-bpm-source.c@@ -22,6 +22,7 @@ int main(void)char key_file[256] = {};char reference[80] = {};char endpoint[512] = {};+ char long_line[512];if (bpm_parse_source_line(endpoint_line, endpoint,sizeof(endpoint), key_file,@@ -47,5 +48,16 @@ int main(void)sizeof(reference)) != -EINVAL)return 1;printf("tree endpoint=%s\ntree reference=%s\n", endpoint, reference);+ /* A value that does not fit is refused, not cut. */+ snprintf(long_line, sizeof(long_line), "reference = sha256:%0*d\n",+ (int)sizeof(reference), 0);+ if (bpm_parse_tree_line(long_line, endpoint, sizeof(endpoint),+ reference, sizeof(reference)) != -E2BIG)+ return 1;+ snprintf(long_line, sizeof(long_line), "key_file = /%0*d\n",+ (int)sizeof(key_file), 0);+ if (bpm_parse_source_line(long_line, endpoint, sizeof(endpoint),+ key_file, sizeof(key_file)) != -E2BIG)+ return 1;return 0;}diff --git a/util/bpm_oci.c b/util/bpm_oci.cindex 011a5e39..faf2ff62 100644--- a/util/bpm_oci.c+++ b/util/bpm_oci.c@@ -21,6 +21,7 @@#include <curl/curl.h>#include <shirudo/misc.h>+#include <shirudo/string.h>#include "bpm_oci.h"#include "crypto.h"@@ -264,8 +265,11 @@ static int load_registry_auth(const char *registry, struct bpm_oci_client *c)int rc;if (getenv("BPM_OCI_USERNAME") && getenv("BPM_OCI_PASSWORD")) {- snprintf(c->username, sizeof(c->username), "%s", getenv("BPM_OCI_USERNAME"));- snprintf(c->password, sizeof(c->password), "%s", getenv("BPM_OCI_PASSWORD"));+ if (strscpy(c->username, getenv("BPM_OCI_USERNAME"),+ sizeof(c->username)) < 0 ||+ strscpy(c->password, getenv("BPM_OCI_PASSWORD"),+ sizeof(c->password)) < 0)+ return -E2BIG;return 0;}if (!authfile && docker) {@@ -340,9 +344,10 @@ 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;}- snprintf(transport, sizeof(transport), "%s", scheme);+ strscpy(transport, scheme, sizeof(transport));}- snprintf(c->registry, sizeof(c->registry), "%s", p);+ if (strscpy(c->registry, p, sizeof(c->registry)) < 0)+ return -ENAMETOOLONG;if (snprintf(c->base_url, sizeof(c->base_url), "%s://%s", transport, p) >=(int)sizeof(c->base_url))return -ENAMETOOLONG;diff --git a/util/bpm_package.c b/util/bpm_package.cindex 28b694c8..0eceb55c 100644--- a/util/bpm_package.c+++ b/util/bpm_package.c@@ -450,7 +450,7 @@ int bpm_pkg_extract(const char *archive, const char *dest)rc = -EINVAL;break;}- snprintf(name, sizeof(name), "%s", (char *)h);+ strscpy(name, (char *)h, sizeof(name));if (!bpm_archive_rel_ok(name) || bpm_tar_octal((char *)h + 124, 12, &size)) {rc = -EINVAL;break;@@ -471,7 +471,7 @@ int bpm_pkg_extract(const char *archive, const char *dest)rc = -EINVAL;break;}- snprintf(rel, sizeof(rel), "%s", name + strlen(root) + 1);+ strscpy(rel, name + strlen(root) + 1, sizeof(rel));if ((!*rel && type != '5') || (*rel && !bpm_archive_rel_ok(rel))) {rc = -EINVAL;break;@@ -488,7 +488,7 @@ int bpm_pkg_extract(const char *archive, const char *dest)char parent[PATH_MAX];FILE *fp;- snprintf(parent, sizeof(parent), "%s", path);+ strscpy(parent, path, sizeof(parent));slash = strrchr(parent, '/');if (slash) {*slash = 0;diff --git a/util/bpm_source_config.c b/util/bpm_source_config.cindex c098cf76..66287a3c 100644--- a/util/bpm_source_config.c+++ b/util/bpm_source_config.c@@ -5,10 +5,10 @@#include <ctype.h>#include <errno.h>-#include <stdio.h>#include <string.h>#include <shirudo/misc.h>+#include <shirudo/string.h>#include "bpm_source_config.h"@@ -37,10 +37,10 @@ int bpm_parse_source_line(char *line,val++;val[strcspn(val, "\r\n")] = '\0';- if (!strcmp(key, "endpoint"))- snprintf(endpoint, endpoint_sz, "%s", val);- else if (!strcmp(key, "key_file"))- snprintf(key_file, key_file_sz, "%s", val);+ if (!strcmp(key, "endpoint") && strscpy(endpoint, val, endpoint_sz) < 0)+ return -E2BIG;+ if (!strcmp(key, "key_file") && strscpy(key_file, val, key_file_sz) < 0)+ return -E2BIG;return 0;}@@ -77,9 +77,11 @@ int bpm_parse_tree_line(char *line,value[len - 1] = '\0';value++;}- if (!strcmp(key, "endpoint"))- snprintf(endpoint, endpoint_sz, "%s", value);- else if (!strcmp(key, "reference"))- snprintf(reference, reference_sz, "%s", value);+ if (!strcmp(key, "endpoint") &&+ strscpy(endpoint, value, endpoint_sz) < 0)+ return -E2BIG;+ if (!strcmp(key, "reference") &&+ strscpy(reference, value, reference_sz) < 0)+ return -E2BIG;return 0;}diff --git a/util/bpm_tree.c b/util/bpm_tree.cindex f713ce84..a856cf83 100644--- a/util/bpm_tree.c+++ b/util/bpm_tree.c@@ -244,7 +244,7 @@ static int tp_selector_string_map(struct tree_parser *p,tp_string_array(p, dst[*n].values, &values,SHIRUDO_TREE_MAX_SELECTOR_VALUES))return -EINVAL;- strcpy(dst[*n].key, key);+ strscpy(dst[*n].key, key, sizeof(dst[*n].key));dst[*n].n_values = values;(*n)++;}diff --git a/util/bpm_tree_state.c b/util/bpm_tree_state.cindex e088f721..dbf67b25 100644--- a/util/bpm_tree_state.c+++ b/util/bpm_tree_state.c@@ -22,6 +22,7 @@#include <zlib.h>#include <shirudo/misc.h>+#include <shirudo/string.h>#include "bpm_tree.h"#include "chain.h"@@ -40,11 +41,9 @@ static int kv_add(struct shirudo_tree_kv *dst, int *n, int max,{if (*n >= max)return -E2BIG;- if (strlen(key) >= sizeof(dst[0].key) ||- strlen(value) >= sizeof(dst[0].value))+ if (strscpy(dst[*n].key, key, sizeof(dst[0].key)) < 0 ||+ strscpy(dst[*n].value, value, sizeof(dst[0].value)) < 0)return -ENOSPC;- strcpy(dst[*n].key, key);- strcpy(dst[*n].value, value);(*n)++;return 0;}@@ -208,9 +207,8 @@ static int selector_config_line(const char *line,char copy[1024], *start, *eq, *end, *value;int index;- if (strlen(line) >= sizeof(copy))+ if (strscpy(copy, line, sizeof(copy)) < 0)return 0;- strcpy(copy, line);start = copy;while (*start && isspace((__u8)*start))start++;@@ -253,9 +251,8 @@ static int selector_config_line(const char *line,index = selector_hash_find(s, slots, start);if (index < 0)return 0;- if (strlen(value) >= SHIRUDO_TREE_STRING_MAX)+ if (strscpy(values[index], value, SHIRUDO_TREE_STRING_MAX) < 0)return -ENOSPC;- strcpy(values[index], value);found[index] = true;return 0;}