Skip to content

eBPF-based security platform

15.4 KB
Raw Blame
# SPDX-License-Identifier: BSD-2-Clause
# Copyright (c) 2026 Authors of shirudo
#
# Makefile.config -- feature-detection probes.
#
# Driven by the top-level Makefile. Writes a cached profile config file that
# Makefile.shirudo -includes. Re-runs on `make distclean` (the cache
# file is removed) or when this file is edited (timestamp-driven).
#
# All probes fail loud and actionable; there is no legacy-fallback
# path in shirudo. The bar is "current upstream kernel + current
# upstream clang" -- if a prerequisite is missing, the build aborts
# with an explicit install hint.

PKG_CONFIG ?= pkg-config
CC         ?= cc
CONFIG_OUT ?= config.mk
SHIRUDO_PROFILE ?= operator
SHIRUDO_TARGET_STATIC_LIBDIR ?=

ifeq ($(SHIRUDO_PROFILE),target)
TARGET_PKG_CONFIG_ENV := $(if $(SHIRUDO_TARGET_PKG_CONFIG_PATH),PKG_CONFIG_PATH='$(SHIRUDO_TARGET_PKG_CONFIG_PATH)',)
profile-pkg-config = $(TARGET_PKG_CONFIG_ENV) $(PKG_CONFIG)
else
profile-pkg-config = $(PKG_CONFIG)
endif

# Each probe sets a yes/no string and (on yes) records the cflags +
# libs that the actual build will use.
# OpenSSL 3.5 is the first release with ML-DSA (FIPS 204), shirudo's
# default signing algorithm; both profiles parse certificates and verify
# CMS with libcrypto, so both need it.
probe-libcrypto    := $(shell $(profile-pkg-config) --exists "libcrypto >= 3.5" && echo yes)
probe-libssl       := $(shell $(profile-pkg-config) --exists "libssl >= 3.5" && echo yes)
probe-openssl      := $(shell command -v openssl >/dev/null 2>&1 && \
			  openssl list -public-key-algorithms 2>/dev/null | \
			  grep -qi 'ML-DSA' && echo yes)
probe-tss2-esys    := $(shell $(profile-pkg-config) --exists "tss2-esys >= 4.0" && echo yes)
probe-tss2-tctildr := $(shell $(profile-pkg-config) --exists tss2-tctildr && echo yes)
probe-libelf       := $(shell $(profile-pkg-config) --exists libelf && echo yes)
probe-zlib         := $(shell $(profile-pkg-config) --exists zlib && echo yes)
probe-libcurl      := $(shell $(profile-pkg-config) --exists "libcurl >= 7.75" && echo yes)
probe-luajit        := $(shell $(profile-pkg-config) --exists luajit && echo yes)

static-libssl-dir := $(patsubst -L%,%,$(firstword $(filter -L%,$(shell $(profile-pkg-config) --libs-only-L libssl 2>/dev/null))))
static-libcrypto-dir := $(patsubst -L%,%,$(firstword $(filter -L%,$(shell $(profile-pkg-config) --libs-only-L libcrypto 2>/dev/null))))
static-libcurl-dir := $(patsubst -L%,%,$(firstword $(filter -L%,$(shell $(profile-pkg-config) --libs-only-L libcurl 2>/dev/null))))
probe-static-tls-archives := $(shell \
    test -n "$(static-libssl-dir)" && \
    test -n "$(static-libcrypto-dir)" && \
    test -n "$(static-libcurl-dir)" && \
    test -f "$(static-libssl-dir)/libssl.a" && \
    test -f "$(static-libcrypto-dir)/libcrypto.a" && \
    test -f "$(static-libcurl-dir)/libcurl.a" && echo yes)
probe-curl-openssl := $(shell \
    command -v curl-config >/dev/null 2>&1 && \
    curl-config --ssl-backends 2>/dev/null | grep -q OpenSSL && echo yes)

# The target's static-only libcurl records its TLS requirement in libcurl.pc
# Libs, so a plain --libs query shows which backend it was built against.
target-static-tls-pkg-libs := $(shell $(profile-pkg-config) --libs libcurl 2>/dev/null)
probe-curl-tls := $(shell \
    printf '%s' '$(target-static-tls-pkg-libs)' | \
    grep -q -- '-lssl' && \
    printf '%s' '$(target-static-tls-pkg-libs)' | \
    grep -q -- '-lcrypto' && \
    ! printf '%s' '$(target-static-tls-pkg-libs)' | \
    grep -Eq -- '-l(mbedtls|mbedx509|mbedcrypto|gnutls|wolfssl)' && echo yes)

# The target profile compiles against the staged OpenSSL headers (they
# must match the static archives) but never links libcrypto or libssl
# dynamically: those come from the static TLS and runtime closures below.
ifeq ($(SHIRUDO_PROFILE),target)
profile-libcrypto-cflags := $(shell $(profile-pkg-config) --cflags libcrypto 2>/dev/null)
profile-libcrypto-libs :=
profile-libssl-cflags := $(shell $(profile-pkg-config) --cflags libssl 2>/dev/null)
profile-libssl-libs :=
static-tls-cflags :=
static-tls-link-libs :=
else
profile-libcrypto-cflags := $(shell $(profile-pkg-config) --cflags libcrypto 2>/dev/null)
profile-libcrypto-libs := $(shell $(profile-pkg-config) --libs libcrypto 2>/dev/null)
profile-libssl-cflags := $(shell $(profile-pkg-config) --cflags libssl 2>/dev/null)
profile-libssl-libs := $(shell $(profile-pkg-config) --libs libssl 2>/dev/null)
static-tls-cflags := $(shell $(profile-pkg-config) --cflags libcurl libssl libcrypto 2>/dev/null)
static-tls-pkg-libs := $(shell $(profile-pkg-config) --static --libs libcurl 2>/dev/null)
static-tls-search-libs := $(sort $(foreach d,$(static-libcurl-dir) \
    $(static-libssl-dir) $(static-libcrypto-dir),$(if $(d),-L$(d))) \
    $(filter -L%,$(static-tls-pkg-libs)))
static-tls-extra-libs := $(filter-out -L% -lcurl -lssl -lcrypto,$(static-tls-pkg-libs))
static-tls-link-libs := $(static-tls-search-libs) \
    -Wl,-Bstatic -lcurl -lssl -lcrypto -Wl,-Bdynamic \
    $(static-tls-extra-libs)
endif

target-static-tls-archives := $(shell \
    test -n "$(static-libcurl-dir)" && \
    test -n "$(static-libssl-dir)" && \
    test -n "$(static-libcrypto-dir)" && \
    test -f "$(static-libcurl-dir)/libcurl.a" && \
    test -f "$(static-libssl-dir)/libssl.a" && \
    test -f "$(static-libcrypto-dir)/libcrypto.a" && echo yes)
target-static-tls-cflags := $(shell $(profile-pkg-config) --cflags libcurl libssl libcrypto 2>/dev/null)
target-static-tls-search-libs := $(sort $(foreach d,$(static-libcurl-dir) \
    $(static-libssl-dir) $(static-libcrypto-dir),$(if $(d),-L$(d))) \
    $(filter -L%,$(target-static-tls-pkg-libs)))
target-static-tls-extra-libs := $(filter-out -L% -lcurl -lssl -lcrypto,$(target-static-tls-pkg-libs))
target-static-tls-link-libs := $(target-static-tls-search-libs) \
    -lcurl -lssl -lcrypto \
    $(target-static-tls-extra-libs)

# The target image keeps its non-TLS runtime closure independent of the host
# shared-library versions.
target-static-runtime-libdir = $(if $(SHIRUDO_TARGET_STATIC_LIBDIR),$(SHIRUDO_TARGET_STATIC_LIBDIR),$(or \
    $(shell $(profile-pkg-config) --variable=libdir libelf 2>/dev/null), \
    $(shell $(profile-pkg-config) --variable=libdir zlib 2>/dev/null)))
target-static-runtime-archives := $(shell \
    test -f "$(target-static-runtime-libdir)/libcrypto.a" && \
    test -f "$(target-static-runtime-libdir)/libtss2-esys.a" && \
    test -f "$(target-static-runtime-libdir)/libtss2-tctildr.a" && \
    test -f "$(target-static-runtime-libdir)/libtss2-tcti-device.a" && \
    test -f "$(target-static-runtime-libdir)/libtss2-sys.a" && \
    test -f "$(target-static-runtime-libdir)/libtss2-mu.a" && \
    test -f "$(target-static-runtime-libdir)/libelf.a" && \
    test -f "$(target-static-runtime-libdir)/libz.a" && \
    test -f "$(target-static-runtime-libdir)/libzstd.a" && \
    test -f "$(target-static-runtime-libdir)/libluajit-5.1.a" && echo yes)
target-static-runtime-search-libs := -L$(target-static-runtime-libdir)
target-static-runtime-link-libs := $(target-static-runtime-search-libs) \
    -ltss2-esys -ltss2-tctildr -ltss2-tcti-device -ltss2-sys \
    -ltss2-mu \
    -lelf -lz -lzstd -lluajit-5.1 -lcrypto -lz \
    -pthread -lm -ldl

# One link probe for both static TLS closures: the target's staged
# OpenSSL/libcurl, or the operator's when SHIRUDO_REQUIRE_STATIC_TLS=1.
ifeq ($(SHIRUDO_PROFILE),target)
probe-tls-cflags := $(target-static-tls-cflags)
probe-tls-link-libs := -static-pie -static-libgcc \
    $(target-static-tls-link-libs) -pthread -ldl
probe-static-tls-wanted := yes
else ifeq ($(SHIRUDO_REQUIRE_STATIC_TLS),1)
probe-tls-cflags := $(static-tls-cflags)
probe-tls-link-libs := $(static-tls-link-libs)
probe-static-tls-wanted := yes
else
probe-static-tls-wanted :=
endif

ifeq ($(probe-static-tls-wanted),yes)
probe-static-tls-link := $(shell \
    tmp=`mktemp -d "$${TMPDIR:-/tmp}/shirudo-static-tls.XXXXXX"` || exit 1; \
    trap 'rm -rf "$$tmp"' EXIT HUP INT TERM; \
    printf '%s\n' \
        '#include <curl/curl.h>' \
        '#include <openssl/ssl.h>' \
        'int main(void) {' \
        '    SSL_CTX *ctx = SSL_CTX_new(TLS_method());' \
        '    int rc = ctx == NULL || curl_global_init(CURL_GLOBAL_DEFAULT) != 0;' \
        '    SSL_CTX_free(ctx);' \
        '    return rc;' \
        '}' > "$$tmp/probe.c"; \
    $(CC) $(probe-tls-cflags) "$$tmp/probe.c" \
        $(probe-tls-link-libs) -o "$$tmp/probe" >/dev/null 2>&1 && echo yes)
else
probe-static-tls-link := yes
endif

ifeq ($(SHIRUDO_PROFILE),target)
probe-static-target-link := $(shell \
    tmp=`mktemp -d "$${TMPDIR:-/tmp}/shirudo-static-target.XXXXXX"` || exit 1; \
    trap 'rm -rf "$$tmp"' EXIT HUP INT TERM; \
    printf '%s\n' \
        '#include <curl/curl.h>' '#include <openssl/ssl.h>' \
        '#include <tss2/tss2_esys.h>' '#include <tss2/tss2_tctildr.h>' \
        '#include <libelf.h>' '#include <lauxlib.h>' \
        'int main(void) {' \
        '    SSL_CTX *ssl = SSL_CTX_new(TLS_method());' \
        '    lua_State *lua = luaL_newstate();' \
        '    TSS2_TCTI_CONTEXT *tcti = NULL;' \
        '    ESYS_CONTEXT *esys = NULL;' \
        '    curl_global_init(CURL_GLOBAL_DEFAULT);' \
        '    Tss2_TctiLdr_Initialize("device:/dev/tpmrm0", &tcti);' \
        '    Esys_Initialize(&esys, tcti, NULL);' \
        '    Esys_Finalize(&esys); Tss2_TctiLdr_Finalize(&tcti);' \
        '    SSL_CTX_free(ssl); lua_close(lua);' \
        '    return elf_version(EV_CURRENT) == EV_NONE;' \
        '}' > "$$tmp/probe.c"; \
    $(CC) -fPIE -static-pie -static-libgcc $(target-static-tls-cflags) \
        $$($(profile-pkg-config) --cflags tss2-esys tss2-tctildr libelf luajit) \
        "$$tmp/probe.c" $(target-static-tls-link-libs) \
        $(target-static-runtime-link-libs) -o "$$tmp/probe" >/dev/null 2>&1 && \
        sh .github/scripts/check-static-target.sh "$$tmp/probe" && echo yes)
endif

probe-clang-bpf := $(shell printf 'int main(void){return 0;}\n' | \
                     clang -target bpf -mcpu=v4 -x c -c -o /dev/null - 2>/dev/null && echo yes)

# Probe the vendored BPF UAPI headers shirudo builds against (not the
# host's /usr/include/linux/bpf.h, which on stock distros lags the kernel
# by months); refresh them with make bpf-deps.
BPF_NEXT_UAPI := vendor/libbpf/include/uapi
probe-keyring-id := $(shell printf '%s\n%s\n%s\n' \
                     '#include <stddef.h>' \
                     '#include <linux/bpf.h>' \
                     'int x = offsetof(union bpf_attr, keyring_id);' | \
                     $(CC) -xc -I $(BPF_NEXT_UAPI) -c -o /dev/null - \
                     2>/dev/null && echo yes)

.PHONY: config.mk
config.mk:
	@:; \
	fail=0; \
	check() { \
	    local name="$$1" got="$$2" hint="$$3"; \
	    if [ "$$got" != "yes" ]; then \
	        printf 'shirudo: missing prerequisite: %s\n' "$$name" >&2; \
	        printf '         %s\n' "$$hint" >&2; \
	        fail=1; \
	    fi; \
	}; \
	check "tss2-esys >= 4.0" "$(probe-tss2-esys)" \
	      "install libtss2-dev / tpm2-tss-devel"; \
	check "tss2-tctildr"     "$(probe-tss2-tctildr)" \
	      "install libtss2-dev / tpm2-tss-devel"; \
	check "libelf"           "$(probe-libelf)" \
	      "install libelf-dev / elfutils-libelf-devel"; \
	check "zlib"             "$(probe-zlib)" \
	      "install zlib1g-dev / zlib-devel"; \
	check "libcurl >= 7.75"  "$(probe-libcurl)" \
	      "install libcurl development files"; \
	check "libcrypto >= 3.5" "$(probe-libcrypto)" \
	      "install OpenSSL 3.5+; Ubuntu 26.04+: sudo apt install openssl libssl-dev; Fedora 43+: sudo dnf install openssl openssl-devel; Ubuntu 24.04 ships 3.0; found: $$($(profile-pkg-config) --modversion libcrypto 2>/dev/null || echo none)"; \
	check "libssl >= 3.5"    "$(probe-libssl)" \
	      "install OpenSSL 3.5+ development files (TLS 1.3 with hybrid ML-KEM)"; \
	check "openssl with ML-DSA support" "$(probe-openssl)" \
	      "Ubuntu 26.04+: sudo apt install openssl; Fedora 43+: sudo dnf install openssl"; \
	if [ "$(SHIRUDO_PROFILE)" = "target" ]; then \
	check "static OpenSSL/libcurl archives" "$(target-static-tls-archives)" \
	      "run make target-deps (builds OpenSSL $(3.5+) and a static OpenSSL-backed libcurl) or point SHIRUDO_TARGET_PKG_CONFIG_PATH at equivalent static builds"; \
	check "static target runtime archives" "$(target-static-runtime-archives)" \
	      "install static libelf, zlib, zstd and LuaJIT archives, run make target-deps, or set SHIRUDO_TARGET_STATIC_LIBDIR"; \
	check "complete static PIE target link" "$(probe-static-target-link)" \
	      "install binutils and compiler-matched static libc/libm, startup and compiler runtime archives (libc6-dev on Debian, glibc-static on Fedora), and static PIE-compatible target dependencies"; \
	check "OpenSSL-backed libcurl" "$(probe-curl-tls)" \
	      "build libcurl with --with-openssl against the staged OpenSSL and point SHIRUDO_TARGET_PKG_CONFIG_PATH at it"; \
	check "static OpenSSL/libcurl link" "$(probe-static-tls-link)" \
	      "install compatible static OpenSSL/libcurl archives and dependencies"; \
	else \
	if [ "$(SHIRUDO_REQUIRE_STATIC_TLS)" = "1" ]; then \
	    check "static libcurl/libssl/libcrypto archives" "$(probe-static-tls-archives)" \
	          "install static OpenSSL and libcurl development archives"; \
	    check "OpenSSL-backed libcurl" "$(probe-curl-openssl)" \
	          "install an OpenSSL-backed libcurl development package"; \
	    check "static libcurl/OpenSSL link" "$(probe-static-tls-link)" \
	          "install compatible static libcurl/OpenSSL archives and dependencies"; \
	fi; \
	fi; \
	check "luajit"           "$(probe-luajit)" \
	      "install libluajit-5.1-dev / luajit-devel"; \
	check "clang -target bpf -mcpu=v4" "$(probe-clang-bpf)" \
	      "install current upstream clang/llvm"; \
	check "vendored BPF UAPI keyring_id" "$(probe-keyring-id)" \
	      "vendor/libbpf/include/uapi predates signed BPF; run make bpf-deps"; \
	if [ "$$fail" = "1" ]; then exit 1; fi; \
	{ \
	    echo "# SPDX-License-Identifier: BSD-2-Clause"; \
	    echo "# AUTO-GENERATED by Makefile.config. Do not edit; do not commit."; \
    echo "LIBCRYPTO_CFLAGS := $(profile-libcrypto-cflags)"; \
    echo "LIBCRYPTO_LIBS   := $(profile-libcrypto-libs)"; \
    echo "LIBSSL_CFLAGS    := $(profile-libssl-cflags)"; \
    echo "LIBSSL_LIBS      := $(profile-libssl-libs)"; \
	    echo "TSS2_CFLAGS      := $$($(profile-pkg-config) --cflags tss2-esys tss2-tctildr)"; \
	    echo "TSS2_LIBS        := $$($(profile-pkg-config) --libs tss2-esys tss2-tctildr)"; \
	    echo "LIBELF_CFLAGS    := $$($(profile-pkg-config) --cflags libelf)"; \
	    echo "LIBELF_LIBS      := $$($(profile-pkg-config) --libs libelf)"; \
	    echo "ZLIB_CFLAGS      := $$($(profile-pkg-config) --cflags zlib)"; \
	    echo "ZLIB_LIBS        := $$($(profile-pkg-config) --libs zlib)"; \
	    echo "LIBCURL_CFLAGS   := $$($(profile-pkg-config) --cflags libcurl)"; \
	    echo "LIBCURL_LIBS     := $$($(profile-pkg-config) --libs libcurl)"; \
	    echo "STATIC_TLS_CFLAGS := $(static-tls-cflags)"; \
	    echo "STATIC_TLS_LINK_LIBS := $(static-tls-link-libs)"; \
	    echo "TARGET_TLS_CFLAGS := $(target-static-tls-cflags)"; \
	    echo "TARGET_TLS_STATIC_LINK_LIBS := $(target-static-tls-link-libs)"; \
	    echo "TARGET_TLS_LINK_LIBS := $(target-static-tls-link-libs)"; \
	    echo "TARGET_STATIC_RUNTIME_LINK_LIBS := $(target-static-runtime-link-libs)"; \
	    echo "HAVE_LUAJIT      := $(probe-luajit)"; \
	    echo "LUAJIT_CFLAGS    := $$($(PKG_CONFIG) --cflags luajit 2>/dev/null)"; \
	    echo "LUAJIT_LIBS      := $$($(PKG_CONFIG) --libs luajit 2>/dev/null)"; \
	} > $(CONFIG_OUT)
	@printf 'shirudo: wrote %s (%s profile)\n' "$(CONFIG_OUT)" "$(SHIRUDO_PROFILE)"