eBPF-based security platform
ci: add an SPDX checker and run it in the Lint workflow
Daniel Borkmann91b4c3c3d953
diff --git a/.github/scripts/check-spdx.sh b/.github/scripts/check-spdx.shnew file mode 100755index 00000000..9050335c--- /dev/null+++ b/.github/scripts/check-spdx.sh@@ -0,0 +1,213 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# .github/scripts/check-spdx.sh -- verify the SPDX-License-Identifier tags of the+# tracked sources against the licensing rules in AGENTS.md: every source+# file carries a tag in the kernel's form for its type, the expression is+# BSD-2-Clause or GPL-2.0 OR BSD-2-Clause, and everything compiled into a+# BPF object, the headers it includes too, and every source a Beefile+# packages carries the dual licence.+#+# .github/scripts/check-spdx.sh+#+# Runs from anywhere inside the checkout. The vendored trees keep their+# own licences and are not checked.++set -eu++BSD='BSD-2-Clause'+DUAL='GPL-2.0 OR BSD-2-Clause'++cd "$(git rev-parse --show-toplevel)"++work=$(mktemp -d)+trap 'rm -rf "$work"' EXIT++bad=0+checked=0++report() {+ bad=$((bad + 1))+ if [ -n "${GITHUB_ACTIONS:-}" ]; then+ printf '::error file=%s::%s\n' "$1" "$2"+ fi+ printf '%s: %s\n' "$1" "$2" >&2+}++# Vendored code keeps its own licence, the kernel's coding-style.rst is+# carried verbatim and vmlinux.h is generated from BTF.+excluded() {+ case "$1" in+ vendor/*|Documentation/coding-style.rst|vmlinux.h|*/vmlinux.h)+ return 0 ;;+ esac+ return 1+}++# The comment form of the tag by file type, the kernel's rules with --+# for Lua and .. for reStructuredText. Any other file needs a tag only+# when it starts with a shebang.+kind_of() {+ case "$1" in+ *.c) echo c ;;+ *.h) echo h ;;+ *.lua) echo lua ;;+ *.rst) echo rst ;;+ *.sh|*.bash|*.py|*.in|*.timer|*.service|*.mk)+ echo hash ;;+ Makefile|*/Makefile|Makefile.*|*/Makefile.*)+ echo hash ;;+ *)+ if [ "$(head -c 2 "$1")" = '#!' ]; then+ echo hash+ fi ;;+ esac+}++# The tag sits on the first line, or on the second after a shebang or+# the .. that opens a reStructuredText comment block.+check_file() {+ f=$1+ kind=$2+ l1=''+ l2=''+ { IFS= read -r l1 || :; IFS= read -r l2 || :; } <"$f"+ want=1+ line=$l1+ case "$kind:$l1" in+ 'hash:#!'*|'rst:..')+ want=2+ line=$l2 ;;+ esac++ case "$line" in+ *SPDX-License-Identifier:*)+ ;;+ *)+ n=$(head -n 20 "$f" | grep -n -m1 'SPDX-License-Identifier:' |+ cut -d: -f1 || :)+ if [ -n "$n" ]; then+ report "$f" "SPDX tag on line $n, expected on line $want"+ else+ report "$f" "no SPDX-License-Identifier tag on line $want"+ fi+ return ;;+ esac++ expr=${line#*SPDX-License-Identifier:}+ expr=$(printf '%s' "$expr" |+ sed 's/^[[:space:]]*//; s/[[:space:]]*\*\/[[:space:]]*$//; s/[[:space:]]*$//')+ suffix=''+ case "$kind" in+ c)+ prefix='// ' ;;+ h)+ prefix='/* '+ suffix=' */' ;;+ lua)+ prefix='-- ' ;;+ hash)+ prefix='# ' ;;+ rst)+ if [ "$want" -eq 2 ]; then+ prefix=$(printf '%s' "$line" | sed 's/SPDX.*//')+ case "$prefix" in+ *[!\ ]*|'')+ prefix=' ' ;;+ esac+ else+ prefix='.. '+ fi ;;+ esac+ printf '%s\t%s\n' "$f" "$expr" >>"$work/exprs"++ case "$expr" in+ "$BSD"|"$DUAL")+ ;;+ *)+ report "$f" "licence expression '$expr' is neither '$BSD' nor '$DUAL'"+ return ;;+ esac+ expected="${prefix}SPDX-License-Identifier: ${expr}${suffix}"+ if [ "$line" != "$expected" ]; then+ report "$f" "the tag of a $kind file reads '$expected', found: $line"+ fi+}++expr_of() {+ awk -F '\t' -v f="$1" '$1 == f { print $2; exit }' "$work/exprs"+}++: >"$work/files"+: >"$work/exprs"+git ls-files | while IFS= read -r f; do+ excluded "$f" && continue+ printf '%s\n' "$f" >>"$work/files"+done++while IFS= read -r f; do+ kind=$(kind_of "$f")+ [ -n "$kind" ] || continue+ checked=$((checked + 1))+ check_file "$f" "$kind"+done <"$work/files"++# Everything clang -target bpf compiles, the LSM and its loaders under+# bpf/ and every *.bpf.c, plus the headers they include by relative+# path, found as the build finds them: beside the includer, then in+# bpf/lib. Each line is the file and the file that pulled it in.+grep -E '^bpf/[^/]+\.c$|\.bpf\.c$' "$work/files" |+ sed 's/$/\t/' >"$work/bpf.pending"+: >"$work/bpf.seen"+while [ -s "$work/bpf.pending" ]; do+ : >"$work/bpf.next"+ while IFS=' ' read -r f from; do+ grep -qxF "$f" "$work/bpf.seen" && continue+ printf '%s\n' "$f" >>"$work/bpf.seen"+ expr=$(expr_of "$f")+ if [ "$expr" != "$DUAL" ]; then+ report "$f" "compiled into a BPF object${from:+ (included from $from)} but licensed '${expr:-no tag}'; BPF objects carry '$DUAL'"+ fi+ for inc in $(sed -n 's/^[[:space:]]*#[[:space:]]*include[[:space:]]*"\([^"]*\)".*/\1/p' "$f"); do+ for dir in "$(dirname "$f")" bpf/lib; do+ [ -f "$dir/$inc" ] || continue+ cand=$(realpath --relative-to=. "$dir/$inc")+ if grep -qxF "$cand" "$work/files"; then+ printf '%s\t%s\n' "$cand" "$f" >>"$work/bpf.next"+ fi+ break+ done+ done+ done <"$work/bpf.pending"+ mv "$work/bpf.next" "$work/bpf.pending"+done++# A package's sources carry the licence its Beefile declares.+grep -E '(^|/)Beefile$' "$work/files" >"$work/beefiles" || :+while IFS= read -r bee; do+ dir=$(dirname "$bee")+ lic=$(sed -n 's/^license[[:space:]]*=[[:space:]]*"\([^"]*\)".*/\1/p' "$bee" | head -n 1)+ if [ -z "$lic" ]; then+ report "$bee" "no license field"+ continue+ fi+ sed -n 's/^source[[:space:]]*=[[:space:]]*"\([^"]*\)".*/\1/p' "$bee" >"$work/sources"+ while IFS= read -r src; do+ case "$src" in+ *.c|*.h|*.lua) ;;+ *) continue ;;+ esac+ path="$dir/$src"+ expr=$(expr_of "$path")+ if [ "$expr" != "$lic" ]; then+ report "$path" "shipped by $bee under '$lic' but licensed '${expr:-no tag}'"+ fi+ done <"$work/sources"+done <"$work/beefiles"++if [ "$bad" -gt 0 ]; then+ printf '%d SPDX problems in %d files\n' "$bad" "$checked" >&2+ exit 1+fi+printf 'SPDX: %d files checked\n' "$checked"diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.ymlindex 97e50afe..2fa13b98 100644--- a/.github/workflows/lint.yml+++ b/.github/workflows/lint.yml@@ -23,3 +23,14 @@ jobs:with:severity: errorignore_paths: vendor++ spdx:+ name: SPDX+ runs-on: ubuntu-latest++ steps:+ - name: Check out repository+ uses: actions/checkout@v4++ - name: Check the SPDX tag of every tracked source+ run: .github/scripts/check-spdx.shdiff --git a/AGENTS.md b/AGENTS.mdindex 6eaf2a0e..b231ea84 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -296,6 +296,11 @@ one usually still compiles and passes tests.- `make -C Documentation html-docs` renders `Documentation/*.rst` with docutils in strictmode into `Documentation/output/`; CI runs it on every push, so run itafter touching any document (python3-docutils provides `rst2html`).+- `.github/scripts/check-spdx.sh` checks the SPDX tag of every tracked+ source against the licensing rules above: the form for the file type,+ the expression, the dual licence for everything a BPF object compiles+ and a Beefile ships. CI runs it on every pull request; run it before+ handing work over.- `make -C packaging bindeb-pkg`, `make -C packaging binrpm-pkg` and `make -C packaging bintar-pkg` build thedistribution packages. The target package is static apart from glibcand portable; the operator package links the host's shared librariesdiff --git a/Documentation/contributing.rst b/Documentation/contributing.rstindex a9dfd38d..bc5dca0f 100644--- a/Documentation/contributing.rst+++ b/Documentation/contributing.rst@@ -199,6 +199,11 @@ Before sendingtouches memory handling or binary formats; CI runs the same sweep.* Run ``make -C Documentation html-docs`` when the change touches ``Documentation/``;every document must render clean under docutils' strict mode.+* Run ``.github/scripts/check-spdx.sh`` when the change adds a file:+ every source carries an SPDX tag in the kernel's form for its type,+ ``BSD-2-Clause`` for userspace and ``GPL-2.0 OR BSD-2-Clause`` for+ everything compiled into a BPF object, the headers it includes and+ the decoders a package ships. CI runs it.* Keep each commit to one logical change that builds and passes on itsown, so the history bisects.* Read `threat-model <threat-model.rst>`__ before changing anything on adiff --git a/testing/t0042-check-spdx.sh b/testing/t0042-check-spdx.shnew file mode 100755index 00000000..cf819cb7--- /dev/null+++ b/testing/t0042-check-spdx.sh@@ -0,0 +1,136 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# t0042 -- .github/scripts/check-spdx.sh: the tag every source needs, its form+# by file type, the two expressions, the dual licence of everything a+# BPF object compiles and a Beefile ships, on a throwaway repository,+# and the tree itself.++cd "$(dirname "$0")"+. ./test-lib.sh+command -v git >/dev/null 2>&1 || test_skip_all "git not installed"+ROOT=$(cd .. && pwd)+S=$ROOT/.github/scripts/check-spdx.sh+W=$(mktemp -d)+trap 'rm -rf "$W"' EXIT++# A file with the given first lines, each argument one line.+put() {+ f=$1+ shift+ mkdir -p "$(dirname "$f")"+ printf '%s\n' "$@" >"$f"+}++repo() {+ rm -rf "$1" && mkdir -p "$1" && git init -q "$1"+}++check() {+ (cd "$1" && git add -A && sh "$S") >"$W/out" 2>"$W/err"+}++R=$W/good+repo "$R"+put "$R/good.c" '// SPDX-License-Identifier: BSD-2-Clause' 'int x;'+put "$R/good.h" '/* SPDX-License-Identifier: BSD-2-Clause */' 'int x;'+put "$R/good.sh" '#!/bin/sh' '# SPDX-License-Identifier: BSD-2-Clause'+put "$R/lib.sh" '# SPDX-License-Identifier: BSD-2-Clause'+put "$R/tool" '#!/bin/bash' '# SPDX-License-Identifier: BSD-2-Clause'+put "$R/Makefile" '# SPDX-License-Identifier: BSD-2-Clause'+put "$R/Makefile.config" '# SPDX-License-Identifier: BSD-2-Clause'+put "$R/unit.service.in" '# SPDX-License-Identifier: BSD-2-Clause'+put "$R/d.lua" '-- SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause'+put "$R/block.rst" '..' ' SPDX-License-Identifier: BSD-2-Clause' ' Copyright (c) 2026'+put "$R/line.rst" '.. SPDX-License-Identifier: BSD-2-Clause'+put "$R/notes.md" 'no tag here'+put "$R/data.json" '{}'+put "$R/vendor/x.c" 'int x;'+put "$R/vendor/linux/vmlinux.h" 'struct task_struct;'+put "$R/pkg/vmlinux.h" 'struct task_struct;'+put "$R/Documentation/coding-style.rst" 'Linux kernel coding style'+put "$R/bpf/lsm.c" '// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause' \+ '#include "vmlinux.h"' '#include <bpf/bpf_helpers.h>' '#include "common.h"'+put "$R/bpf/lib/common.h" '/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */'+put "$R/pkg/Beefile" '[package]' 'license = "GPL-2.0 OR BSD-2-Clause"' \+ '[[files]]' 'source = "p.bpf.c"' '[[files]]' 'source = "decoders/x.lua"' \+ '[[files]]' 'source = "gen.py"'+put "$R/pkg/p.bpf.c" '// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause' \+ '#include "vmlinux.h"' '#include "common.h"'+put "$R/pkg/decoders/x.lua" '-- SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause'+put "$R/pkg/gen.py" '#!/usr/bin/env python3' '# SPDX-License-Identifier: BSD-2-Clause'++test_expect_success 'a tree of well-tagged files passes' '+ check "$R" &&+ grep -q "SPDX: 16 files checked" "$W/out"+'++B=$W/bad+repo "$B"+put "$B/style.c" '/* SPDX-License-Identifier: BSD-2-Clause */'+put "$B/style.h" '// SPDX-License-Identifier: BSD-2-Clause'+put "$B/late.sh" '#!/bin/sh' '#' '# SPDX-License-Identifier: BSD-2-Clause'+put "$B/none.py" 'import os'+put "$B/tool" '#!/bin/bash' 'echo'+put "$B/other.c" '// SPDX-License-Identifier: MIT'+put "$B/dual.c" '// SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0'+put "$B/indent.rst" '..' 'SPDX-License-Identifier: BSD-2-Clause'+put "$B/prog.bpf.c" '// SPDX-License-Identifier: BSD-2-Clause'+put "$B/bpf/lsm.c" '// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause' \+ '#include "lib.h"' ' # include "sub/inner.h"'+put "$B/bpf/lib.h" '/* SPDX-License-Identifier: BSD-2-Clause */'+put "$B/bpf/sub/inner.h" '/* SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause */' \+ '#include "../deep.h"'+put "$B/bpf/deep.h" '/* SPDX-License-Identifier: BSD-2-Clause */'+put "$B/fx/a.bpf.c" '// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause' \+ '#include "common.h"'+put "$B/bpf/lib/common.h" '/* SPDX-License-Identifier: BSD-2-Clause */'+put "$B/pkg/Beefile" '[package]' 'license = "GPL-2.0 OR BSD-2-Clause"' \+ '[[files]]' 'source = "decoders/x.lua"' '[[files]]' 'source = "gen.py"'+put "$B/pkg/decoders/x.lua" '-- SPDX-License-Identifier: BSD-2-Clause'+put "$B/pkg/gen.py" '# SPDX-License-Identifier: BSD-2-Clause'+put "$B/nolic/Beefile" '[package]' 'name = "x"'++test_expect_success 'a tree of badly tagged files fails' '+ ! check "$B" &&+ grep -q "^14 SPDX problems" "$W/err"+'++test_expect_success 'the form of the tag follows the file type' '+ grep -q "^style.c: the tag of a c file reads .// SPDX-License-Identifier: BSD-2-Clause., found: /\* SPDX" "$W/err" &&+ grep -q "^style.h: the tag of a h file reads ./\* SPDX-License-Identifier: BSD-2-Clause \*/., found: // SPDX" "$W/err" &&+ grep -q "^indent.rst: the tag of a rst file reads . SPDX-License-Identifier: BSD-2-Clause., found: SPDX" "$W/err"+'++test_expect_success 'the tag sits on the first line, or the second after a shebang' '+ grep -q "^late.sh: SPDX tag on line 3, expected on line 2" "$W/err" &&+ grep -q "^none.py: no SPDX-License-Identifier tag on line 1" "$W/err" &&+ grep -q "^tool: no SPDX-License-Identifier tag on line 2" "$W/err"+'++test_expect_success 'only the two expressions of the tree are accepted' '+ grep -q "^other.c: licence expression .MIT. is neither" "$W/err" &&+ grep -q "^dual.c: licence expression .BSD-2-Clause OR GPL-2.0. is neither" "$W/err"+'++test_expect_success 'BPF objects and the headers they include carry the dual licence' '+ grep -q "^prog.bpf.c: compiled into a BPF object but licensed .BSD-2-Clause." "$W/err" &&+ grep -q "^bpf/lib.h: compiled into a BPF object (included from bpf/lsm.c) but licensed" "$W/err" &&+ grep -q "^bpf/deep.h: compiled into a BPF object (included from bpf/sub/inner.h) but licensed" "$W/err" &&+ grep -q "^bpf/lib/common.h: compiled into a BPF object (included from fx/a.bpf.c) but licensed" "$W/err" &&+ ! grep -q "^bpf/sub/inner.h" "$W/err"+'++test_expect_success 'a package ships its sources under the licence of its Beefile' '+ grep -q "^pkg/decoders/x.lua: shipped by pkg/Beefile under .GPL-2.0 OR BSD-2-Clause. but licensed .BSD-2-Clause." "$W/err" &&+ ! grep -q "^pkg/gen.py" "$W/err" &&+ grep -q "^nolic/Beefile: no license field" "$W/err"+'++test_expect_success 'the tree passes' '+ (cd "$ROOT" && sh "$S") >"$W/out" 2>"$W/err" &&+ grep -q "^SPDX: [0-9]* files checked" "$W/out"+'++test_done