Skip to content

eBPF-based security platform

bpm: find BPF headers from invocation directory

John Fastabend079b40c2ebfa
diff --git a/tools/shirudo-build/bpm-build.c b/tools/shirudo-build/bpm-build.c
index 63173a46..bc7fde91 100644
--- a/tools/shirudo-build/bpm-build.c
+++ b/tools/shirudo-build/bpm-build.c
@@ -771,6 +771,18 @@ static const char *build_vmlinux_from_cwd(char *out, size_t cap)
return NULL;
}
+static const char *build_shirudo_bpf_include(char *out)
+{
+ char header[PATH_MAX];
+
+ if (!realpath("bpf/lib", out))
+ return NULL;
+ if (strsprintf(header, sizeof(header), "%s/shirudo_serializer.h",
+ out) < 0 || access(header, R_OK))
+ return NULL;
+ return out;
+}
+
static void build_stage_close(struct bpm_build_spec *spec)
{
char object[PATH_MAX + 8];
@@ -794,7 +806,8 @@ static void build_stage_close(struct bpm_build_spec *spec)
* ended up with.
*/
static int build_upatches(struct bpm_build_spec *spec, const char *pkey,
- const char *cert, const char *vmlinux_override)
+ const char *cert, const char *vmlinux_override,
+ const char *shirudo_bpf_incdir)
{
const char *vmlinux_h = vmlinux_override;
@@ -829,7 +842,8 @@ static int build_upatches(struct bpm_build_spec *spec, const char *pkey,
}
if (strsprintf(object, sizeof(object), "%s.bpf.o", load) < 0)
return -ENAMETOOLONG;
- rc = shirudo_build_compile(source, object, vmlinux_h, NULL);
+ rc = shirudo_build_compile(source, object, vmlinux_h, NULL,
+ shirudo_bpf_incdir);
if (rc) {
pr_err("bpm build: compile %s failed", u->source);
return rc < 0 ? rc : -EIO;
@@ -1135,8 +1149,10 @@ static int shirudo_build_bpm_build(int argc, char **argv)
const char *context = ".", *file_arg = NULL, *version = NULL;
const char *keys_arg = NULL, *vmlinux_arg = NULL;
const char *vmlinux_override = NULL;
+ const char *shirudo_bpf_incdir = NULL;
char build_file[PATH_MAX], resolved_file[PATH_MAX];
char keys_dir[PATH_MAX], vmlinux_abs[PATH_MAX];
+ char shirudo_bpf_incdir_buf[PATH_MAX];
char keys_abs[PATH_MAX], pkey[PATH_MAX], cert_path[PATH_MAX];
char fingerprint[SHIRUDO_CRYPTO_SHA256_LEN * 2 + 1];
__u8 digest[SHIRUDO_CRYPTO_SHA256_LEN];
@@ -1210,6 +1226,7 @@ static int shirudo_build_bpm_build(int argc, char **argv)
sizeof(vmlinux_abs));
if (!getcwd(out_dir, sizeof(out_dir)))
return -errno;
+ shirudo_bpf_incdir = build_shirudo_bpf_include(shirudo_bpf_incdir_buf);
if (file_arg) {
if (strscpy(build_file, file_arg, sizeof(build_file)) < 0)
return -ENAMETOOLONG;
@@ -1261,7 +1278,8 @@ static int shirudo_build_bpm_build(int argc, char **argv)
rc = build_stage_open(spec);
if (rc)
goto out_cert;
- rc = build_upatches(spec, pkey, cert_path, vmlinux_override);
+ rc = build_upatches(spec, pkey, cert_path, vmlinux_override,
+ shirudo_bpf_incdir);
if (!rc)
rc = build_archive(spec, version, cert_path, fingerprint, out_dir);
build_stage_close(spec);
diff --git a/tools/shirudo-build/compile.c b/tools/shirudo-build/compile.c
index 57790237..6858c019 100644
--- a/tools/shirudo-build/compile.c
+++ b/tools/shirudo-build/compile.c
@@ -7,6 +7,7 @@
*/
#include <errno.h>
+#include <limits.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
@@ -55,18 +56,23 @@ static const char *locate_bpf_headers(void)
static const char *locate_shirudo_bpf_headers(void)
{
static const char in_tree[] = "-I./bpf/lib";
+ static const char installed[] = "-I" SHIRUDO_BPF_INCDIR;
struct stat st;
if (stat("./bpf/lib/shirudo_serializer.h", &st) == 0)
return in_tree;
+ if (stat(SHIRUDO_BPF_INCDIR "/shirudo_serializer.h", &st) == 0)
+ return installed;
return NULL;
}
int shirudo_build_compile(const char *source, const char *output,
- const char *vmlinux_h, const char *extra_cflags)
+ const char *vmlinux_h, const char *extra_cflags,
+ const char *shirudo_bpf_incdir)
{
const char *argv[SHIRUDO_BUILD_MAX_ARGS] = {};
char inc_vmlinux[1024 + 8];
+ char inc_shirudo_bpf_buf[PATH_MAX + 8];
char vmlinux_dir[1024];
char *cf_copy = NULL;
int rc = 0;
@@ -122,6 +128,14 @@ int shirudo_build_compile(const char *source, const char *output,
{
const char *inc_shirudo_bpf = locate_shirudo_bpf_headers();
+ if (shirudo_bpf_incdir) {
+ if (strsprintf(inc_shirudo_bpf_buf,
+ sizeof(inc_shirudo_bpf_buf), "-I%s",
+ shirudo_bpf_incdir) < 0)
+ return -ENAMETOOLONG;
+ inc_shirudo_bpf = inc_shirudo_bpf_buf;
+ }
+
if (inc_shirudo_bpf)
push(argv, &n, inc_shirudo_bpf);
}
diff --git a/tools/shirudo-build/compile.h b/tools/shirudo-build/compile.h
index 9c8a44f5..863d2d7c 100644
--- a/tools/shirudo-build/compile.h
+++ b/tools/shirudo-build/compile.h
@@ -10,6 +10,7 @@
int shirudo_build_compile(const char *source, const char *output,
const char *vmlinux_h,
- const char *extra_cflags);
+ const char *extra_cflags,
+ const char *shirudo_bpf_incdir);
#endif /* SHIRUDO_BUILD_COMPILE_H */
diff --git a/tools/shirudo-build/shirudo-build.c b/tools/shirudo-build/shirudo-build.c
index 39fde6a7..38ac956f 100644
--- a/tools/shirudo-build/shirudo-build.c
+++ b/tools/shirudo-build/shirudo-build.c
@@ -145,7 +145,7 @@ static int compile_to_temp(const char *source, const char *vmlinux_h,
return -errno;
close(fd);
- rc = shirudo_build_compile(source, tmpl, vmlinux_h, extra_cflags);
+ rc = shirudo_build_compile(source, tmpl, vmlinux_h, extra_cflags, NULL);
if (rc) {
unlink(tmpl);
return rc;
@@ -395,7 +395,7 @@ int main(int argc, char **argv)
return 1;
}
rc = shirudo_build_compile(input, artifact_buf,
- vmlinux_h, extra_cflags);
+ vmlinux_h, extra_cflags, NULL);
if (rc) {
pr_err("compile %s: %d", input, rc);
return 1;