Skip to content

eBPF-based security platform

build: link the target as fully static PIE

John Fastabend09377a94ccb2
diff --git a/.github/scripts/check-static-target.sh b/.github/scripts/check-static-target.sh
new file mode 100755
index 00000000..261f6f7d
--- /dev/null
+++ b/.github/scripts/check-static-target.sh
@@ -0,0 +1,27 @@
+#!/bin/sh
+# SPDX-License-Identifier: BSD-2-Clause
+
+set -eu
+
+export LC_ALL=C
+readelf=${READELF:-readelf}
+
+if [ "$#" -ne 1 ]; then
+ printf 'usage: %s <target-binary>\n' "$0" >&2
+ exit 2
+fi
+
+work=$(mktemp -d)
+trap 'rm -rf "$work"' EXIT HUP INT TERM
+
+"$readelf" -hW "$1" >"$work/header"
+"$readelf" -lW "$1" >"$work/programs"
+"$readelf" -dW "$1" >"$work/dynamic"
+
+if ! grep -Eq 'Type:.*DYN' "$work/header" ||
+ ! grep -Eq '\(FLAGS_1\).*PIE' "$work/dynamic" ||
+ grep -Eq '[[:space:]]INTERP[[:space:]]' "$work/programs" ||
+ grep -Eq '\((NEEDED|RPATH|RUNPATH)\)' "$work/dynamic"; then
+ printf 'shirudo: %s must be static PIE without an interpreter, shared dependencies or library search paths\n' "$1" >&2
+ exit 1
+fi
diff --git a/.github/scripts/gen-sbom.sh b/.github/scripts/gen-sbom.sh
index e40689ae..0474275c 100755
--- a/.github/scripts/gen-sbom.sh
+++ b/.github/scripts/gen-sbom.sh
@@ -6,8 +6,8 @@
# materials next to each package, listing what it carries that a package
# manager cannot see: the target binary links OpenSSL, libcurl, TPM2-TSS
# and the distribution's libelf, zlib, zstd and LuaJIT archives
-# statically, the static tarball bundles shared libraries, and both roles
-# link the vendored libbpf.
+# statically, including the C and compiler runtimes, and both roles link
+# the vendored libbpf.
#
# .github/scripts/gen-sbom.sh <package>...
#
@@ -17,6 +17,7 @@
# static libdir has to be one of the source-built dependencies or belong
# to a distribution package, or the script fails rather than leave it
# out. The shirudo version is SHIRUDO_CI_VERSION, else git describe.
+# CC must match the compiler used to link the target runtime.
set -eu
@@ -99,6 +100,7 @@ deb_license() {
zlib) echo Zlib ;;
libzstd) echo 'BSD-3-Clause OR GPL-2.0-only' ;;
luajit*) echo MIT ;;
+ glibc) echo LGPL-2.1-or-later ;;
gcc-*) echo 'GPL-3.0-or-later WITH GCC-exception-3.1' ;;
*) echo NOASSERTION ;;
esac
@@ -171,16 +173,14 @@ target_components() {
;;
esac
done
-}
-
-# The shared libraries the static tarball carries in lib/shirudo/.
-bundled_components() {
- tar -tzf "$1" | grep '/lib/shirudo/[^/][^/]*$' >"$work/bundled" || true
- while IFS= read -r entry; do
- rm -rf "$work/x" && mkdir "$work/x"
- tar -xzf "$1" -C "$work/x" "$entry"
- deb_component "$work/x/$entry" "$TARGET_SYSTEM_LIBDIR/${entry##*/}"
- done <"$work/bundled"
+ for runtime_name in libc.a libm.a libgcc.a libgcc_eh.a rcrt1.o; do
+ runtime_path=$(${CC:-cc} -print-file-name="$runtime_name") ||
+ die "cannot locate $runtime_name with ${CC:-cc}"
+ [ -f "$runtime_path" ] ||
+ die "compiler runtime missing: $runtime_name"
+ runtime_path=$(readlink -f "$runtime_path")
+ deb_component "$runtime_path" "$runtime_path"
+ done
}
libbpf_component >"$work/common"
@@ -213,11 +213,6 @@ for pkg; do
target_components >"$work/target"
fi
cat "$work/target" >>"$work/components"
- case $pkg in
- *.tar.gz)
- bundled_components "$pkg" >>"$work/components"
- ;;
- esac
fi
base=${pkg##*/}
diff --git a/.github/workflows/deb.yml b/.github/workflows/deb.yml
index 8cc95e29..97d836dc 100644
--- a/.github/workflows/deb.yml
+++ b/.github/workflows/deb.yml
@@ -193,17 +193,16 @@ jobs:
# Fedora job below builds against this bpf-next one, not its host's.
cp vendor/linux/vmlinux.h /tmp/shirudo-packages/vmlinux.h
- # Both installers relocate under DESTDIR; the static image also
- # carries its remaining shared libraries next to the binary.
+ # Both installers relocate under DESTDIR and carry static PIE.
for tarball in shirudo.tar.gz shirudo-static.tar.gz; do
rm -rf /tmp/bintar && mkdir -p /tmp/bintar/archive /tmp/bintar/root
tar -C /tmp/bintar/archive -xzf "\$tarball" --strip-components=1
DESTDIR=/tmp/bintar/root /tmp/bintar/archive/install.sh
test -x /tmp/bintar/root/usr/local/bin/shirudo
+ sh .github/scripts/check-static-target.sh /tmp/bintar/root/usr/local/bin/shirudo
test -f /tmp/bintar/root/usr/lib/systemd/system/shirudo.service
/tmp/bintar/root/usr/local/bin/shirudo version
done
- test -d /tmp/bintar/root/usr/local/lib/shirudo
make -C packaging bindeb-pkg \
DEB_RAW_VERSION="\$SHIRUDO_CI_VERSION" \
@@ -214,12 +213,9 @@ jobs:
SHIRUDO_TARGET_STATIC_LIBDIR=/tmp/shirudo-target-deps/lib
cp shirudo-*.deb /tmp/shirudo-packages/
- # These libraries must be present in the target executable rather
- # than in its DT_NEEDED list.
- ! readelf -d shirudo-target | grep -Eq \
- 'lib(rtmp|psl|z|tss2-esys|tss2-tctildr|elf|luajit-5\.1)\.so'
+ sh .github/scripts/check-static-target.sh shirudo-target
- # The target rpm is static apart from glibc and portable, so it is
+ # The target rpm is fully static and portable, so it is
# built here; the operator rpm links the distribution's shared
# libraries and is built natively in the Fedora job below.
make -C packaging binrpm-pkg PKG_ROLES=target \
diff --git a/AGENTS.md b/AGENTS.md
index 7da43439..89d70713 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -312,7 +312,7 @@ one usually still compiles and passes tests.
## Building, testing, packaging
- `make` builds the operator binary; `make shirudo-build` the helper;
- `make target` the static-OpenSSL target profile (`make target-deps`
+ `make target` the fully static PIE target profile (`make target-deps`
builds OpenSSL, libcurl and tpm2-tss from source first).
- A compiler warning fails every build the Makefiles drive, the BPF
objects, vendored libbpf, test helpers and fuzz targets included;
@@ -351,7 +351,7 @@ one usually still compiles and passes tests.
- Shell that a workflow runs lives in `.github/scripts/`, not inline in
the workflow file.
- `make -C packaging bindeb-pkg`, `make -C packaging binrpm-pkg` and `make -C packaging bintar-pkg` build the
- distribution packages. The target package is static apart from glibc
+ distribution packages. The target package is fully static PIE
and portable; the operator package links the host's shared libraries
and is built on the distribution it is for (`PKG_ROLES=operator`),
against a bpf-next `vmlinux.h` (`BPF_VMLINUX_H=`). Package targets
diff --git a/Documentation/initrd.rst b/Documentation/initrd.rst
index f07092f3..bd23840e 100644
--- a/Documentation/initrd.rst
+++ b/Documentation/initrd.rst
@@ -84,8 +84,9 @@ installed binary.
3) What the initrd needs
------------------------
-* The target binary at its installed path. It is static apart from
- the C library.
+* The target binary at its installed path. It is fully static PIE,
+ including the C library, and needs no ELF interpreter or shared
+ libraries to start.
* ``/etc/shirudo/keys/cert.der``, a copy of ``/var/lib/shirudo/keys/cert.der``,
and ``/etc/shirudo/keys/trusted/`` with the further certificates and
their vouches. These copies are measured with the initrd, so a
diff --git a/Documentation/requirements.rst b/Documentation/requirements.rst
index f6b338f0..09d00eff 100644
--- a/Documentation/requirements.rst
+++ b/Documentation/requirements.rst
@@ -289,7 +289,7 @@ the private key under ``/var/lib/shirudo/keys/``.
state-changing command.
* The target binary from ``make target`` links OpenSSL, libcurl,
tpm2-tss with its device TCTI, libbpf, libelf, zlib, zstd and
- LuaJIT statically and needs only the C library at runtime, so it
+ LuaJIT, glibc, libm and the compiler runtime statically as a PIE, so it
runs on a distribution whose own OpenSSL predates ML-DSA and reaches
the TPM from an initrd that holds no tpm2-tss shared objects.
* A TPM 2.0 exposed as ``/dev/tpmrm0`` (discrete, firmware or cloud
@@ -315,10 +315,36 @@ an OpenSSL-backed libcurl and tpm2-tss from source and stages them as
static archives. The build host then needs static ``libelf``,
``zlib``, ``zstd`` and ``libluajit-5.1`` archives;
``SHIRUDO_TARGET_STATIC_LIBDIR`` names their directory when it is not
-the system one. The operator package, by contrast, links the host's
+the system one. The configured C compiler must also provide matching
+static glibc, libm, startup objects and compiler runtime archives with
+static PIE support (``libc6-dev`` on Debian-family hosts,
+``glibc-static`` on Fedora). These come from that compiler's sysroot,
+not ``SHIRUDO_TARGET_STATIC_LIBDIR``. Missing or incompatible archives
+fail configuration; the target never falls back to shared libraries.
+The operator package, by contrast, links the host's
shared libraries and must be built on the distribution it is installed
on.
+The target ELF has no interpreter, shared-library dependencies or
+library search paths. Its static PIE startup relocates it without
+``ld-linux``, retaining address randomisation, and ignores
+``LD_PRELOAD``. This describes the ELF linkage, not a prohibition on
+runtime module loading: glibc's NSS and explicit ``dlopen`` paths may
+still load shared objects. The build's glibc linker warnings for those
+interfaces must not be mistaken for an ELF dependency.
+
+Both installer tarball names remain available and carry the same target
+executable, without bundled shared libraries. The package SBOMs include
+the embedded glibc and compiler runtime versions as well as the other
+static dependencies. glibc retains its LGPL licence and notices; the
+GCC runtime retains its runtime exception. The project's source licence
+does not replace those dependency licences. To rebuild or relink the
+target, use the source revision and dependency versions recorded with
+the release, supply the replacement runtime through ``CC`` and its
+sysroot, and run ``make target``. Retain that source, the build rules
+and the dependency sources/notices with the distribution's relinking
+materials. Rebuild the initrd and reboot after replacing the target
+binary, as described in `operations <operations.rst>`__.
7) Documentation and tests
--------------------------
diff --git a/Makefile.config b/Makefile.config
index c2c1d8a0..d0f461a1 100644
--- a/Makefile.config
+++ b/Makefile.config
@@ -106,7 +106,7 @@ target-static-tls-search-libs := $(sort $(foreach d,$(static-libcurl-dir) \
$(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) \
- -Wl,-Bstatic -lcurl -lssl -lcrypto -Wl,-Bdynamic \
+ -lcurl -lssl -lcrypto \
$(target-static-tls-extra-libs)
# The target image keeps its non-TLS runtime closure independent of the host
@@ -127,20 +127,17 @@ target-static-runtime-archives := $(shell \
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) \
- -Wl,-Bstatic -ltss2-esys -ltss2-tctildr -ltss2-tcti-device -ltss2-sys \
+ -ltss2-esys -ltss2-tctildr -ltss2-tcti-device -ltss2-sys \
-ltss2-mu \
- -lelf -lz -lzstd -Wl,-E -lluajit-5.1 -lcrypto -lz -Wl,-Bdynamic \
+ -lelf -lz -lzstd -lluajit-5.1 -lcrypto -lz \
-pthread -lm -ldl
-# libz.a is linked after the static libcurl and libelf consumers. Do not let
-# the libcurl private closure select libz.so before that point.
-target-static-tls-dynamic-link-libs := $(filter-out -lz -lssl -lcrypto,$(target-static-tls-extra-libs))
-
# 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 := $(target-static-tls-link-libs) -pthread -ldl
+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)
@@ -169,6 +166,33 @@ 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 -x c -c -o /dev/null - 2>/dev/null && echo yes)
@@ -216,6 +240,8 @@ config.mk:
"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)" \
@@ -255,8 +281,7 @@ config.mk:
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-search-libs) -Wl,-Bstatic -lcurl -lssl -lcrypto"; \
- echo "TARGET_TLS_DYNAMIC_LINK_LIBS := $(target-static-tls-dynamic-link-libs)"; \
+ 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)"; \
diff --git a/Makefile.shirudo b/Makefile.shirudo
index ed587911..f314f3b4 100644
--- a/Makefile.shirudo
+++ b/Makefile.shirudo
@@ -18,8 +18,8 @@ CONFIG_MK ?= config.mk
# Both profiles run on OpenSSL: libcrypto for SHA-256, certificates and
# CMS, libssl behind libcurl for TLS. The operator links the system
-# shared libraries; the target links the static OpenSSL and libcurl
-# staged by make target-deps.
+# shared libraries; the target links static dependencies staged by
+# make target-deps and the compiler's static C and compiler runtimes.
CRYPTO_SRC := util/crypto_openssl.c
TLS_SRC := util/tls_openssl.c
ifeq ($(PROFILE),target)
@@ -99,6 +99,7 @@ HARDEN_CFLAGS += -fstack-clash-protection -fzero-call-used-regs=used \
-mbranch-protection=standard
endif
HARDEN_LDFLAGS := -pie -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack
+HARDEN_STATIC_PIE_LDFLAGS := $(filter-out -pie,$(HARDEN_LDFLAGS))
CFLAGS_BASE := -O2 $(DEBUG_CFLAGS) -std=gnu11 $(WARNINGS) $(HARDEN_CFLAGS) \
-D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE \
@@ -143,6 +144,10 @@ DYNAMIC_LIBS := \
CFLAGS += $(LUAJIT_CFLAGS) -DSHIRUDO_HAVE_LUAJIT
DYNAMIC_LIBS += $(LUAJIT_LIBS)
+ifeq ($(PROFILE),target)
+CFLAGS += -fPIE
+endif
+
ifeq ($(SHIRUDO_BUNDLE_RPATH),1)
RPATH_LDFLAGS := -Wl,--disable-new-dtags -Wl,-rpath,'$$ORIGIN/../lib/shirudo'
else
@@ -330,7 +335,7 @@ FORCE_VMLINUX_BTF:
@printf ' CC $@\n'
$(Q)$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
-build/target/%.o: %.c
+build/target/%.o: %.c Makefile.shirudo $(CONFIG_MK)
@printf ' CC $@\n'
$(Q)mkdir -p $(dir $@)
$(Q)$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
@@ -384,14 +389,17 @@ shirudo: $(MAIN_OBJ) $(BUILTIN_OBJS) $(UTIL_OBJS) $(VERSION_OBJ) $(LIBBPF_A)
$(MAIN_OBJ) $(BUILTIN_OBJS) $(UTIL_OBJS) $(VERSION_OBJ) \
$(STATIC_LIBS) $(DYNAMIC_LIBS)
-shirudo-target: $(MAIN_OBJ) $(BUILTIN_OBJS) $(UTIL_OBJS) $(VERSION_OBJ) $(LIBBPF_A)
- @printf ' LD $@ (static OpenSSL)\n'
- $(Q)$(LD) $(CFLAGS) $(TARGET_TLS_CFLAGS) $(HARDEN_LDFLAGS) $(LDFLAGS) \
- $(RPATH_LDFLAGS) -o $@ \
+shirudo-target: $(MAIN_OBJ) $(BUILTIN_OBJS) $(UTIL_OBJS) $(VERSION_OBJ) $(LIBBPF_A) \
+ Makefile.shirudo $(CONFIG_MK) .github/scripts/check-static-target.sh
+ @printf ' LD $@ (static PIE)\n'
+ $(Q)$(LD) $(CFLAGS) $(TARGET_TLS_CFLAGS) \
+ $(HARDEN_STATIC_PIE_LDFLAGS) $(LDFLAGS) \
+ -static-pie -static-libgcc -o $@ \
$(MAIN_OBJ) $(BUILTIN_OBJS) $(UTIL_OBJS) $(VERSION_OBJ) \
$(STATIC_LIBS) $(TARGET_TLS_STATIC_LINK_LIBS) \
- $(TARGET_STATIC_RUNTIME_LINK_LIBS) \
- -Wl,-Bdynamic $(TARGET_TLS_DYNAMIC_LINK_LIBS)
+ $(TARGET_STATIC_RUNTIME_LINK_LIBS)
+ $(Q)sh .github/scripts/check-static-target.sh $@ || \
+ { rm -f $@; exit 1; }
# util/loader.o has a make-level dep on every embedded bpf object.
util/loader.o: $(BPF_OBJS)
@@ -474,7 +482,7 @@ install-target: shirudo-target install-common systemd/shirudo.service \
systemd/shirudo-bpm-repo-update.timer \
systemd/shirudo-bpm-tree-sync.service \
systemd/shirudo-bpm-tree-sync.timer
- @printf ' INSTALL $(DESTDIR)$(bindir)/shirudo (static OpenSSL)\n'
+ @printf ' INSTALL $(DESTDIR)$(bindir)/shirudo (static PIE)\n'
$(Q)install -d $(DESTDIR)$(bindir)
$(Q)install -m 0755 shirudo-target $(DESTDIR)$(bindir)/shirudo
@printf ' INSTALL $(DESTDIR)$(bindir)/shirudo-gen-initrd\n'
diff --git a/Makefile.target-deps b/Makefile.target-deps
index d249c4d5..436cc803 100644
--- a/Makefile.target-deps
+++ b/Makefile.target-deps
@@ -142,7 +142,7 @@ $(CURL_STAMP): $(OPENSSL_STAMP)
@$(MAKE) -C "$(CURL_SOURCE)" DESTDIR= install
@touch "$@"
-$(TSS_STAMP): $(OPENSSL_STAMP)
+$(TSS_STAMP): Makefile.target-deps $(OPENSSL_STAMP)
@mkdir -p "$(TARGET_SOURCE_DIR)" "$(TARGET_STATIC_LIBDIR)"
@if test ! -d "$(TSS_SOURCE)"; then \
git clone --depth 1 --branch "$(TSS_VERSION)" \
diff --git a/packaging/Makefile b/packaging/Makefile
index b840e6fd..7b2286dd 100644
--- a/packaging/Makefile
+++ b/packaging/Makefile
@@ -33,7 +33,6 @@ dracutmodulesdir ?= /usr/lib/dracut/modules.d
INSTALLER_TARBALL ?= shirudo.tar.gz
INSTALLER_STAGE ?= build/shirudo-installer
INSTALLER_ROOT := $(INSTALLER_STAGE)/root
-INSTALLER_PRIVATE_LIBDIR ?= $(libdir)/shirudo
INSTALL_PATHS := prefix=$(prefix) exec_prefix=$(exec_prefix) \
bindir=$(bindir) libexecdir=$(libexecdir) \
@@ -52,7 +51,7 @@ STATIC_IMAGE_ROOT := $(STATIC_IMAGE_STAGE)/root
# rebuild with this prefix.
PKG_PREFIX ?= /usr
PKG_RAW_VERSION ?= $(shell cd $(ROOT) && ($(SHIRUDO_GIT_DESCRIBE) 2>/dev/null || cat $(ROOT)/VERSION))
-# Which roles to package. The target package is static apart from glibc
+# Which roles to package. The target package is fully static
# and portable across distributions; the operator package links the
# build host's shared libraries and belongs on the distribution it is
# built on, so PKG_ROLES=operator builds just that one there.
@@ -105,15 +104,11 @@ PKG_PATHS := prefix=$(PKG_PREFIX) exec_prefix=$(PKG_PREFIX) \
systemdunitdir=/usr/lib/systemd/system
# Build two target-installer tarballs (kernel-style `bin*-pkg`). Both target
-# binaries statically link OpenSSL and OpenSSL-backed libcurl. The static image
-# additionally bundles the remaining dynamic libraries into lib/shirudo/ via
-# an $ORIGIN-relative rpath for embedded / legacy / minimal targets.
+# binaries are fully static PIE, including their C and compiler runtimes.
# Each tarball contains a staged install-target tree under root/ plus an
-# install.sh wrapper. The two flavors need different target binary builds (the
-# bundled one carries an rpath), so the recipe rebuilds the target artifact
-# between them.
-bintar-pkg: config $(ROOT)/packaging/tar/install.sh $(ROOT)/packaging/tar/bundle-libs.sh
- @printf ' STAGE $(INSTALLER_STAGE_PATH) (system libs)\n'
+# install.sh wrapper. Keep both archive names for existing consumers.
+bintar-pkg: config $(ROOT)/packaging/tar/install.sh
+ @printf ' STAGE $(INSTALLER_STAGE_PATH) (static PIE)\n'
$(Q)rm -rf $(INSTALLER_STAGE_PATH) $(INSTALLER_TARBALL_PATH)
$(Q)rm -f $(ROOT)/shirudo $(ROOT)/shirudo-target
$(Q)install -d $(INSTALLER_STAGE_PATH) $(dir $(INSTALLER_TARBALL_PATH))
@@ -122,7 +117,6 @@ bintar-pkg: config $(ROOT)/packaging/tar/install.sh $(ROOT)/packaging/tar/bundle
+$(Q)$(MAKE) -C $(ROOT) -f Makefile.shirudo PROFILE=target CONFIG_MK=config-target.mk \
$(INSTALL_PATHS) \
LDFLAGS='$(LDFLAGS)' \
- SHIRUDO_BUNDLE_RPATH=0 \
DESTDIR=$(INSTALLER_ROOT_PATH) \
install-target
$(Q)install -m 0755 $(ROOT)/packaging/tar/install.sh $(INSTALLER_STAGE_PATH)/install.sh
@@ -130,21 +124,10 @@ bintar-pkg: config $(ROOT)/packaging/tar/install.sh $(ROOT)/packaging/tar/bundle
@printf ' TAR $(INSTALLER_TARBALL_PATH)\n'
$(Q)tar -C $(dir $(INSTALLER_STAGE_PATH)) -czf $(INSTALLER_TARBALL_PATH) \
$(notdir $(INSTALLER_STAGE_PATH))
- @printf ' STAGE $(STATIC_IMAGE_STAGE_PATH) (bundled libs)\n'
+ @printf ' STAGE $(STATIC_IMAGE_STAGE_PATH) (static PIE)\n'
$(Q)rm -rf $(STATIC_IMAGE_STAGE_PATH) $(STATIC_IMAGE_TARBALL_PATH)
- $(Q)rm -f $(ROOT)/shirudo $(ROOT)/shirudo-target
$(Q)install -d $(STATIC_IMAGE_STAGE_PATH) $(dir $(STATIC_IMAGE_TARBALL_PATH))
- +$(Q)$(MAKE) -C $(ROOT) target-deps
- +$(Q)$(MAKE) -C $(ROOT) target-config
- +$(Q)$(MAKE) -C $(ROOT) -f Makefile.shirudo PROFILE=target CONFIG_MK=config-target.mk \
- $(INSTALL_PATHS) \
- LDFLAGS='$(LDFLAGS)' \
- SHIRUDO_BUNDLE_RPATH=1 \
- DESTDIR=$(STATIC_IMAGE_ROOT_PATH) \
- install-target
- $(Q)$(ROOT)/packaging/tar/bundle-libs.sh \
- "$(abspath $(STATIC_IMAGE_ROOT_PATH)$(bindir)/shirudo)" \
- "$(abspath $(STATIC_IMAGE_ROOT_PATH)$(INSTALLER_PRIVATE_LIBDIR))"
+ $(Q)cp -a $(INSTALLER_ROOT_PATH) $(STATIC_IMAGE_ROOT_PATH)
$(Q)install -m 0755 $(ROOT)/packaging/tar/install.sh \
$(STATIC_IMAGE_STAGE_PATH)/install.sh
$(Q)install -m 0644 $(ROOT)/LICENSE $(ROOT)/README.md $(ROOT)/VERSION $(STATIC_IMAGE_STAGE_PATH)/
diff --git a/packaging/rpm/buildrpm.sh b/packaging/rpm/buildrpm.sh
index 1fca2c14..33a1939e 100755
--- a/packaging/rpm/buildrpm.sh
+++ b/packaging/rpm/buildrpm.sh
@@ -73,6 +73,9 @@ dirs=$(cd "$stage" && find . -mindepth 1 -type d -path '*/shirudo*' | sed 's#^\.
# RPMs are the whole output, like the two debs.
printf '%%global debug_package %%{nil}\n'
printf '%%global _build_id_links none\n'
+ if [ "$role" = target ]; then
+ printf '%s\n' '%global __requires_exclude ^rtld[(]GNU_HASH[)]$'
+ fi
printf 'Name: %s\n' "$pkg"
printf 'Version: %s\n' "$version"
printf 'Release: %s\n' "$release"
diff --git a/testing/Makefile b/testing/Makefile
index f9e3a6a9..3e491fc2 100644
--- a/testing/Makefile
+++ b/testing/Makefile
@@ -122,6 +122,7 @@ TEST_VM_BUNDLE_SOURCES := $(sort $(wildcard \
TEST_VM_BUNDLE_ARTIFACTS := $(VM_KERNEL_STAMP)
TEST_VM_BUNDLE_INPUTS := $(sort \
$(ROOT)/Makefile $(ROOT)/Makefile.config $(ROOT)/Makefile.shirudo $(ROOT)/Makefile.version $(ROOT)/config.mk \
+ $(ROOT)/Makefile.target-deps \
$(ROOT)/testing/Makefile $(ROOT)/packaging/Makefile \
$(ROOT)/vendor/libbpf/Makefile $(ROOT)/vendor/libbpf/COMMIT \
$(ROOT)/Documentation/Makefile $(ROOT)/upatches/kernel-obs/Makefile \
diff --git a/testing/helper/test-preload.c b/testing/helper/test-preload.c
new file mode 100644
index 00000000..67f860a5
--- /dev/null
+++ b/testing/helper/test-preload.c
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: BSD-2-Clause
+
+#include <fcntl.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static void __attribute__((constructor)) mark_preload(void)
+{
+ const char *path = getenv("SHIRUDO_PRELOAD_MARKER");
+ int descriptor;
+
+ if (!path)
+ return;
+ descriptor = open(path, O_WRONLY | O_CREAT | O_EXCL, 0600);
+ if (descriptor >= 0)
+ close(descriptor);
+}
diff --git a/testing/t0022-static-tls.sh b/testing/t0022-static-tls.sh
index 57f67cba..1ccd79b8 100644
--- a/testing/t0022-static-tls.sh
+++ b/testing/t0022-static-tls.sh
@@ -12,6 +12,38 @@ if ! command -v readelf >/dev/null 2>&1 || [ ! -x "$TARGET" ]; then
test_skip_all 'target static profile has not been built'
fi
+WORK=$(mktemp -d)
+trap 'rm -rf "$WORK"' EXIT
+
+test_expect_success 'target is static PIE without an interpreter or shared dependencies' '
+ sh ../.github/scripts/check-static-target.sh "$TARGET" &&
+ "$TARGET" version >"$WORK/version"
+'
+
+test_expect_success 'the static ELF checker rejects the dynamic operator' '
+ ! sh ../.github/scripts/check-static-target.sh "$OPERATOR"
+'
+
+test_expect_success 'the static ELF checker rejects non-PIE and search paths' '
+ printf "int main(void) { return 0; }\n" |
+ ${CC:-cc} -x c - -static -o "$WORK/nonpie" &&
+ ! sh ../.github/scripts/check-static-target.sh "$WORK/nonpie" &&
+ printf "int main(void) { return 0; }\n" |
+ ${CC:-cc} -x c - -static-pie -Wl,-rpath,/untrusted \
+ -o "$WORK/rpath" &&
+ ! sh ../.github/scripts/check-static-target.sh "$WORK/rpath"
+'
+
+test_expect_success 'preload constructors run in the operator but not the target' '
+ ${CC:-cc} -shared -fPIC helper/test-preload.c -o "$WORK/preload.so" &&
+ env LD_PRELOAD="$WORK/preload.so" SHIRUDO_PRELOAD_MARKER="$WORK/operator" \
+ "$OPERATOR" version >"$WORK/operator-version" &&
+ test -f "$WORK/operator" &&
+ env LD_PRELOAD="$WORK/preload.so" SHIRUDO_PRELOAD_MARKER="$WORK/target" \
+ "$TARGET" version >"$WORK/target-version" &&
+ test ! -e "$WORK/target"
+'
+
test_expect_success 'target binary has no shared TLS closure' '
test -x "$TARGET" &&
! readelf -d "$TARGET" | grep -Eq "lib(curl|ssl|crypto|mbedtls|mbedx509|mbedcrypto|gnutls)\\.so"
diff --git a/testing/t0032-binrpm-pkg.sh b/testing/t0032-binrpm-pkg.sh
index 9ecebe12..3dafc69a 100644
--- a/testing/t0032-binrpm-pkg.sh
+++ b/testing/t0032-binrpm-pkg.sh
@@ -156,4 +156,14 @@ test_expect_success 'PKG_ROLES=operator leaves the target package and its static
! grep -q "builddeb.sh operator" "$WORK/plan-deb"
'
+test_expect_success 'static PIE target packages do not require a shared runtime or loader' '
+ cc -static-pie -static-libgcc -o "$WORK/static-stub" "$WORK/stub.c" &&
+ install -m 0755 "$WORK/static-stub" "$WORK/target/usr/bin/shirudo" &&
+ ../packaging/rpm/buildrpm.sh target "$WORK/target" 1.2.3 1 "$ARCH" \
+ "Test <[email protected]>" "$WORK/static.rpm" \
+ >"$WORK/static.out" 2>&1 &&
+ rpm -qp --requires "$WORK/static.rpm" >"$WORK/static.requires" 2>/dev/null &&
+ ! grep -Eq "^lib.*\\.so|^rtld\\(" "$WORK/static.requires"
+'
+
test_done
diff --git a/testing/t0046-gen-sbom.sh b/testing/t0046-gen-sbom.sh
index 9a9e88c3..bd2b11be 100755
--- a/testing/t0046-gen-sbom.sh
+++ b/testing/t0046-gen-sbom.sh
@@ -25,7 +25,9 @@ pc() {
mkdir -p "$W/bin" "$W/sys" "$W/deps/lib" "$W/out"
echo elf >"$W/sys/libelf.a"
echo zlib >"$W/sys/libz.a"
-echo gcc >"$W/sys/libgcc_s.so.1"
+for archive in libc.a libm.a libgcc.a libgcc_eh.a rcrt1.o; do
+ printf '%s\n' "$archive" >"$W/sys/$archive"
+done
pc "$W/openssl/lib" openssl 3.5.9
echo ssl >"$W/openssl/lib/libssl.a"
echo crypto >"$W/openssl/lib/libcrypto.a"
@@ -41,7 +43,8 @@ cat >"$W/bin/dpkg" <<'EOF'
case $2 in
*/libelf.a) echo "libelf-dev:amd64: $2" ;;
*/libz.a) echo "zlib1g-dev:amd64: $2" ;;
-*/libgcc_s.so.1) echo "libgcc-s1:amd64: $2" ;;
+*/libc.a|*/libm.a|*/rcrt1.o) echo "libc6-dev:amd64: $2" ;;
+*/libgcc.a|*/libgcc_eh.a) echo "libgcc-14-dev:amd64: $2" ;;
*) echo "dpkg-query: no path found matching pattern $2" >&2; exit 1 ;;
esac
EOF
@@ -51,20 +54,32 @@ for p; do :; done
case $p in
libelf-dev:amd64) echo 'libelf-dev 0.192-4 amd64 elfutils' ;;
zlib1g-dev:amd64) echo 'zlib1g-dev 1:1.3.dfsg+really1.3.1-1 amd64 zlib' ;;
-libgcc-s1:amd64) echo 'libgcc-s1 14.2.0-19 amd64 gcc-14' ;;
+libc6-dev:amd64) echo 'libc6-dev 2.42-1 amd64 glibc' ;;
+libgcc-14-dev:amd64) echo 'libgcc-14-dev 14.2.0-19 amd64 gcc-14' ;;
*) exit 1 ;;
esac
EOF
-chmod +x "$W/bin/dpkg" "$W/bin/dpkg-query"
+cat >"$W/bin/cc" <<'EOF'
+#!/bin/sh
+root=$(CDPATH= cd "$(dirname "$0")/../sys" && pwd)
+if [ "${SHIRUDO_TEST_UNOWNED_RUNTIME:-0}" = 1 ] &&
+ [ "$1" = -print-file-name=libgcc.a ]; then
+ printf '%s/unowned.a\n' "$root"
+ exit 0
+fi
+printf '%s/%s\n' "$root" "${1#-print-file-name=}"
+EOF
+chmod +x "$W/bin/dpkg" "$W/bin/dpkg-query" "$W/bin/cc"
echo deb >"$W/out/shirudo-target_1.0_amd64.deb"
echo deb >"$W/out/shirudo-operator_1.0_amd64.deb"
-mkdir -p "$W/img/shirudo-static-image/root/usr/local/lib/shirudo"
-cp "$W/sys/libgcc_s.so.1" "$W/img/shirudo-static-image/root/usr/local/lib/shirudo/"
+mkdir -p "$W/img/shirudo-static-image/root/usr/local/bin"
+echo target >"$W/img/shirudo-static-image/root/usr/local/bin/shirudo"
tar -C "$W/img" -czf "$W/out/shirudo-static.tar.gz" shirudo-static-image
run() {
env PATH="$W/bin:$PATH" SHIRUDO_CI_VERSION=v1.0 SOURCE_DATE_EPOCH=0 \
+ CC="$W/bin/cc" \
SHIRUDO_TARGET_PKG_CONFIG_PATH="$W/curl/lib/pkgconfig:$W/openssl/lib/pkgconfig" \
SHIRUDO_TARGET_STATIC_LIBDIR="$W/deps/lib" \
TARGET_SYSTEM_LIBDIR="$W/sys" "$@" >/dev/null 2>"$W/err"
@@ -79,10 +94,10 @@ T=shirudo-target_1.0_amd64.deb
test_expect_success 'a target package lists its static closure and libbpf' '
run sh "$S" "$W/out/$T" &&
- doc $T "[.packages[].name] | sort == [\"curl\", \"libbpf\", \"libelf-dev\", \"openssl\", \"shirudo-target\", \"tpm2-tss\", \"zlib1g-dev\"]" &&
+ doc $T "[.packages[].name] | sort == [\"curl\", \"libbpf\", \"libc6-dev\", \"libelf-dev\", \"libgcc-14-dev\", \"openssl\", \"shirudo-target\", \"tpm2-tss\", \"zlib1g-dev\"]" &&
doc $T ".packages[] | select(.name == \"openssl\") | .versionInfo == \"3.5.9\"" &&
doc $T ".packages[] | select(.name == \"tpm2-tss\") | .versionInfo == \"4.1.3\"" &&
- doc $T "[.relationships[] | select(.relationshipType == \"CONTAINS\")] | length == 6"
+ doc $T "[.relationships[] | select(.relationshipType == \"CONTAINS\")] | length == 8"
'
test_expect_success 'the document describes the package by its checksum and version' '
@@ -97,9 +112,23 @@ test_expect_success 'a distribution archive carries its deb purl and licence' '
doc $T ".packages[] | select(.name == \"libelf-dev\") | .licenseDeclared == \"GPL-2.0-or-later OR LGPL-3.0-or-later\""
'
-test_expect_success 'the static tarball adds the libraries it bundles' '
+test_expect_success 'the static tarball records embedded C and compiler runtimes' '
run sh "$S" "$W/out/shirudo-static.tar.gz" &&
- doc shirudo-static.tar.gz ".packages[] | select(.name == \"libgcc-s1\") | .versionInfo == \"14.2.0-19\""
+ doc shirudo-static.tar.gz ".packages[] | select(.name == \"libgcc-14-dev\") | .versionInfo == \"14.2.0-19\" and .licenseDeclared == \"GPL-3.0-or-later WITH GCC-exception-3.1\"" &&
+ doc shirudo-static.tar.gz ".packages[] | select(.name == \"libc6-dev\") | .versionInfo == \"2.42-1\" and .licenseDeclared == \"LGPL-2.1-or-later\""
+'
+
+test_expect_success 'a missing compiler runtime fails' '
+ mv "$W/sys/libgcc.a" "$W/sys/unowned.a" &&
+ ! run sh "$S" "$W/out/$T" &&
+ mv "$W/sys/unowned.a" "$W/sys/libgcc.a" &&
+ grep -q "compiler runtime missing: libgcc.a" "$W/err"
+'
+
+test_expect_success 'a compiler runtime without distribution provenance fails' '
+ cp "$W/sys/libgcc.a" "$W/sys/unowned.a" &&
+ ! run env SHIRUDO_TEST_UNOWNED_RUNTIME=1 sh "$S" "$W/out/$T" &&
+ grep -q "cannot account for .*unowned.a" "$W/err"
'
test_expect_success 'an operator package lists libbpf alone and needs no target build' '