eBPF-based security platform
ci: refuse operator binaries that link a library outside the list
Daniel Borkmannddc501a8dbe3
diff --git a/.github/scripts/check-operator-linkage.sh b/.github/scripts/check-operator-linkage.shnew file mode 100755index 00000000..f9635629--- /dev/null+++ b/.github/scripts/check-operator-linkage.sh@@ -0,0 +1,43 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause++set -eu++export LC_ALL=C+readelf=${READELF:-readelf}++# The libraries AGENTS.md admits, by soname without its version.+allowed='libc libcrypto libssl libcurl libtss2-esys libtss2-tctildr libelf libz libluajit-5.1'++if [ "$#" -eq 0 ]; then+ printf 'usage: %s <operator-binary>...\n' "$0" >&2+ exit 2+fi++work=$(mktemp -d)+trap 'rm -rf "$work"' EXIT HUP INT TERM++bad=0+for bin; do+ "$readelf" -lW "$bin" >"$work/programs"+ "$readelf" -dW "$bin" >"$work/dynamic"+ interp=$(sed -n 's/.*\[Requesting program interpreter: \(.*\)\]$/\1/p' \+ "$work/programs")+ sed -n 's/.*(NEEDED).*Shared library: \[\(.*\)\]$/\1/p' \+ "$work/dynamic" >"$work/needed"+ while read -r lib; do+ if [ -n "$interp" ] && [ "$lib" = "${interp##*/}" ]; then+ continue+ fi+ case " $allowed " in+ *" ${lib%%.so*} "*)+ continue+ ;;+ esac+ printf 'shirudo: %s links %s, which is not a library an operator binary may link\n' \+ "$bin" "$lib" >&2+ bad=1+ done <"$work/needed"+done++exit "$bad"diff --git a/.github/workflows/deb.yml b/.github/workflows/deb.ymlindex 443421be..c1f435b7 100644--- a/.github/workflows/deb.yml+++ b/.github/workflows/deb.yml@@ -111,6 +111,9 @@ jobs:apt-get updateapt-get install -y /host/shirudo-operator_*.deb+ sh .github/scripts/check-operator-linkage.sh /usr/bin/shirudo \+ /usr/libexec/shirudo/shirudo-build+/usr/bin/shirudo genkey# The default runtime image size must hold the static target.@@ -311,6 +314,8 @@ jobs:dnf -q install -y ./shirudo-operator-*.rpmrpm -q shirudo-operator clangrpm -qV shirudo-operator+ sh .github/scripts/check-operator-linkage.sh /usr/bin/shirudo \+ /usr/libexec/shirudo/shirudo-buildshirudo genkeyrm -rf /tmp/shirudo-smoke && mkdir -p /tmp/shirudo-smokecp testing/fixtures/basic.bpf.c /tmp/shirudo-smoke/diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rstindex 7c5847e2..cd9a400d 100644--- a/Documentation/contribute/code/developer-guide.rst+++ b/Documentation/contribute/code/developer-guide.rst@@ -448,6 +448,13 @@ the commit at ``HEAD`` alone without them.commit in the working tree.* - ``check-static-target.sh <binary>``- That a target binary is a static PIE.+ * - ``check-operator-linkage.sh <binary>...``+ - That each operator binary links no shared library but ``libc``,+ ``libcrypto``, ``libssl``, ``libcurl``, ``libtss2-esys``,+ ``libtss2-tctildr``, ``libelf``, ``libz``, ``libluajit-5.1`` and+ its own dynamic loader, in any version. CI runs it on the binaries+ the deb and rpm operator packages install. A ``SANITIZE`` build+ fails it, because it also links the sanitizer runtimes.* - ``check-runtime-image.sh <binary>``- That a runtime image of the default size of``shirudo-gen-runtime`` holds the target binary, and how muchdiff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex 23043ce3..c36b00d1 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -52,6 +52,11 @@ signed config image.- ``check-coccinelle.sh``: a match that fails the check with itsfile, line and rule, the vendored sources left out, a rule namedalone, the rewrite of ``-D patch``, and the tree itself+ * - ``t0007-check-operator-linkage``+ - ``check-operator-linkage.sh``: the libraries an operator binary+ may link and its own interpreter, another loader, a library+ outside the list or named like one on it, and the operator+ binaries when built``build`` (1)~~~~~~~~~~~~~@@ -582,10 +587,10 @@ Coverage by area``t3108``, ``t5004``, ``t5103``, ``t6007``, ``t6114``,``t7001``, ``t8001``* - Build and packaging- - ``t0005``, ``t1003``, ``t1004``, ``t1005``, ``t1008``,- ``t7004``, ``t8003``+ - ``t0005``, ``t0007``, ``t1003``, ``t1004``, ``t1005``,+ ``t1008``, ``t7004``, ``t8003``* - CI scripts- - ``t0001`` to ``t0006``+ - ``t0001`` to ``t0007``* - Parsers- ``t3001``, ``t3011``, ``t5002``, ``t5006``, ``t5008``. Unit:``hex``, ``json``, ``string``. Every fuzz targetdiff --git a/testing/ci-checks/t0007-check-operator-linkage.sh b/testing/ci-checks/t0007-check-operator-linkage.shnew file mode 100755index 00000000..d2dccdb3--- /dev/null+++ b/testing/ci-checks/t0007-check-operator-linkage.sh@@ -0,0 +1,107 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t0007 -- .github/scripts/check-operator-linkage.sh: the libraries an+# operator binary may link and its own interpreter, against stub+# libraries, the ones it refuses, and the operator binaries when built.++cd "$(dirname "$0")/.."+. ./test-lib.sh+CC=${CC:-cc}+for t in "$CC" readelf; do+ command -v "$t" >/dev/null 2>&1 || test_skip_all "$t not installed"+done+ROOT=$(cd .. && pwd)+S=$ROOT/.github/scripts/check-operator-linkage.sh+W=$(mktemp -d)+trap 'rm -rf "$W"' EXIT++# Empty shared libraries in $W, each with its file name as its soname.+stub() {+ for so; do+ "$CC" -shared -fPIC -Wl,-soname,"$so" -o "$W/$so" -x c /dev/null+ done+}++# A program <name> in $W that needs every library named after it.+prog() {+ out=$1+ shift+ printf 'int main(void) { return 0; }\n' |+ "$CC" -x c - -x none -o "$W/$out" -Wl,--no-as-needed "$@"+}++check() {+ sh "$S" "$@" >"$W/out" 2>"$W/err"+}++stub libcrypto.so.3 libssl.so.3 libcurl.so.4 libtss2-esys.so.0 \+ libtss2-tctildr.so.0 libelf.so.1 libz.so.1 libluajit-5.1.so.2 \+ libfoo.so.1 libcurl-gnutls.so.4 libzstd.so.1 ld-linux-other.so.2+prog ok "$W/libcrypto.so.3" "$W/libssl.so.3" "$W/libcurl.so.4" \+ "$W/libtss2-esys.so.0" "$W/libtss2-tctildr.so.0" "$W/libelf.so.1" \+ "$W/libz.so.1" "$W/libluajit-5.1.so.2"+prog foo "$W/libz.so.1" "$W/libfoo.so.1"+interp=$(readelf -lW "$W/ok" |+ sed -n 's/.*\[Requesting program interpreter: \(.*\)\]$/\1/p')++test_expect_success 'the libraries an operator binary may link pass' '+ readelf -dW "$W/ok" | grep -q "Shared library: \[libluajit-5.1.so.2\]" &&+ check "$W/ok"+'++test_expect_success 'its own interpreter passes, another loader does not' '+ test -n "$interp" &&+ prog interp "$interp" &&+ check "$W/interp" &&+ prog loader "$W/ld-linux-other.so.2" &&+ ! check "$W/loader" &&+ grep -q "links ld-linux-other.so.2" "$W/err"+'++test_expect_success 'a library outside the list fails and is named' '+ ! check "$W/foo" &&+ grep -q "$W/foo links libfoo.so.1" "$W/err" &&+ ! grep -q "libz.so.1" "$W/err"+'++test_expect_success 'a library named like an admitted one fails' '+ prog gnutls "$W/libcurl-gnutls.so.4" &&+ ! check "$W/gnutls" &&+ grep -q "links libcurl-gnutls.so.4" "$W/err" &&+ prog zstd "$W/libzstd.so.1" &&+ ! check "$W/zstd" &&+ grep -q "links libzstd.so.1" "$W/err"+'++test_expect_success 'every binary named is checked' '+ ! check "$W/ok" "$W/foo" &&+ grep -q "$W/foo links libfoo.so.1" "$W/err"+'++test_expect_success 'a file readelf cannot read fails' '+ ! check "$W/none"+'++test_expect_success 'no binary is a usage error' '+ rc=0 &&+ { sh "$S" >/dev/null 2>&1 || rc=$?; } &&+ test "$rc" -eq 2+'++# A sanitized build also links its sanitizer runtimes.+OPERATOR=$ROOT/shirudo+HELPER=$ROOT/tools/shirudo-build/shirudo-build+if [ -x "$OPERATOR" ] && [ -x "$HELPER" ] &&+ ! readelf -dW "$OPERATOR" | grep -q 'Shared library: \[lib[a-z]*san'; then+ test_expect_success 'the operator binaries pass' '+ check "$OPERATOR" "$HELPER"+ '+else+ TAP_COUNT=$((TAP_COUNT + 1))+ printf 'ok %d - the operator binaries pass # SKIP not built, or sanitized\n' \+ "$TAP_COUNT"+fi++test_done