eBPF-based security platform
shirudo-build: derive a source's default upatch name in one place
Daniel Borkmann02baa2037dd6
diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.cindex 9b812bd8..40bd9c3a 100644--- a/tools/shirudo-build/bpm-build.c+++ b/tools/shirudo-build/bpm-build.c@@ -1129,17 +1129,20 @@ static int build_entry_name(char *out, size_t cap, const char *top,return 0;}-static void build_upatch_name(const char *source, char *out, size_t cap)+int build_upatch_name(const char *source, char *out, size_t cap){const char *base = strrchr(source, '/');- size_t n;+ ssize_t n;- strscpy(out, base ? base + 1 : source, cap);- n = strlen(out);- if (n > 6 && (!strcmp(out + n - 6, ".bpf.c") || !strcmp(out + n - 6, ".bpf.o")))+ n = strscpy(out, base ? base + 1 : source, cap);+ if (n < 0)+ return n;+ if (name_has_suffix(out, n, ".bpf.c") ||+ name_has_suffix(out, n, ".bpf.o"))out[n - 6] = '\0';- else if (n > 2 && !strcmp(out + n - 2, ".o"))+ else if (name_has_suffix(out, n, ".o"))out[n - 2] = '\0';+ return 0;}/*@@ -1306,7 +1309,8 @@ static int build_upatches(struct bpm_build_spec *spec, const char *pkey,pr_err("bpm build: %s is neither .bpf.c nor .bpf.o", u->source);return -EINVAL;}- build_upatch_name(source, name, sizeof(name));+ if (build_upatch_name(source, name, sizeof(name)))+ return -ENAMETOOLONG;for (size_t j = 0; j < u->n_policies; j++)nr_labels += u->policies[j].n_paths;if (nr_labels) {@@ -2184,8 +2188,10 @@ static int __build_bpm_config_update(int argc, char **argv,ret = build_package_verify(load, cert_path);if (ret)break;- build_upatch_name(upatch->source, expected_name,- sizeof(expected_name));+ ret = build_upatch_name(upatch->source, expected_name,+ sizeof(expected_name));+ if (ret)+ break;ret = upatch_open(load, &parsed);if (ret)break;diff --git a/tools/shirudo-build/bpm.h b/tools/shirudo-build/bpm.hindex 46ef85bb..a3a4a29c 100644--- a/tools/shirudo-build/bpm.h+++ b/tools/shirudo-build/bpm.h@@ -107,6 +107,9 @@ bool build_is_help(const char *arg);int build_check_keys(const char *pkey, const char *cert);+/* The file name of source less .bpf.c, .bpf.o or .o. 0 or -E2BIG. */+int build_upatch_name(const char *source, char *out, size_t cap);+int build_keys_vouch(int argc, char **argv);#endif /* SHIRUDO_BUILD_BPM_H */diff --git a/tools/shirudo-build/shirudo-build.c b/tools/shirudo-build/shirudo-build.cindex 56fe0670..cb29f385 100644--- a/tools/shirudo-build/shirudo-build.c+++ b/tools/shirudo-build/shirudo-build.c@@ -81,23 +81,6 @@ static void usage(FILE *fp)CERT_PATH, VMLINUX_PATH);}-static const char *derive_name(const char *input, char *buf, size_t buf_sz)-{- const char *base = strrchr(input, '/');- size_t n;-- strscpy(buf, base ? base + 1 : input, buf_sz);-- n = strlen(buf);- if (n > 6 && strcmp(buf + n - 6, ".bpf.c") == 0)- buf[n - 6] = '\0';- else if (n > 6 && strcmp(buf + n - 6, ".bpf.o") == 0)- buf[n - 6] = '\0';- else if (n > 2 && strcmp(buf + n - 2, ".o") == 0)- buf[n - 2] = '\0';- return buf;-}-/* foo/bar.bpf.c lands at foo/bar.bpf.upatch beside its input. */static int derive_output(const char *input, char *buf, size_t buf_sz){@@ -410,8 +393,13 @@ int main(int argc, char **argv)return 2;}- if (!name)- name = derive_name(input, name_buf, sizeof(name_buf));+ if (!name) {+ if (build_upatch_name(input, name_buf, sizeof(name_buf))) {+ pr_err("upatch name from %s too long", input);+ return 2;+ }+ name = name_buf;+ }ret = build_package_load(bpf_o, src_path, output, name,NULL, 0, pkey, cert);