eBPF-based security platform
Build slim x86-64 packages for test VMs
Michi Mutsuzakia034cf3c3d47
diff --git a/testing/kernel/build-kernel.sh b/testing/kernel/build-kernel.shindex a5582422..cf7cc290 100755--- a/testing/kernel/build-kernel.sh+++ b/testing/kernel/build-kernel.sh@@ -1,10 +1,8 @@#!/usr/bin/env bash# SPDX-License-Identifier: BSD-2-Clause# Copyright (c) 2026 Authors of shirudo-# build-kernel.sh: build bootable kernel packages from the upstream/bpf-next-# branch of this repository, the only base patches/ supports for now, with-# the hardened configuration assembled from kernel.conf, kernel.<arch>.conf-# and blacklist.conf.+# build-kernel.sh: build kernel packages from upstream/bpf-next with the+# hardened full profile or the small CI VM-test profile.## The source is checked out as a detached git worktree under build/src,# patches/*.patch are applied on top, the configuration is assembled out@@ -29,6 +27,8 @@ usage: ${0##*/} [options]are installed, otherwise gcc)-b, --base BASE defconfig (default) or a path to a .config tostart from instead of the arch defconfig+ --profile PROFILE full (default) hardened packages or ci (small+ VM-test kernel)--localversion S use S verbatim as CONFIG_LOCALVERSION instead ofthe kernel.conf value plus -YYYYMMDD (build day)--module-key PEM sign modules with this key+certificate PEM@@ -71,6 +71,7 @@ OUT="$REPO/build"JOBS="$(nproc)"TOOLCHAIN=""BASE="defconfig"+PROFILE="full"LOCALVERSION=""MODULE_KEY=""OPENSSL_PREFIX="${OPENSSL_PREFIX:-}"@@ -89,6 +90,7 @@ while [ $# -gt 0 ]; do-j|--jobs) JOBS="$2"; shift 2 ;;-t|--toolchain) TOOLCHAIN="$2"; shift 2 ;;-b|--base) BASE="$2"; shift 2 ;;+ --profile) PROFILE="$2"; shift 2 ;;--localversion) LOCALVERSION="$2"; shift 2 ;;--module-key) MODULE_KEY="$(readlink -f "$2")"; shift 2 ;;--openssl) OPENSSL_PREFIX="$2"; shift 2 ;;@@ -141,11 +143,25 @@ if [ -n "$MODULE_KEY" ]; thendie "module key must be one PEM file with the private key and its certificate: $MODULE_KEY"fi-FRAGMENTS=("$REPO/kernel.conf" "$REPO/kernel.$ARCH.conf")-BLACKLIST="$REPO/blacklist.conf"-for f in "${FRAGMENTS[@]}" "$BLACKLIST"; do+case "$PROFILE" in+full)+ FRAGMENTS=("$REPO/kernel.conf" "$REPO/kernel.$ARCH.conf")+ BLACKLIST="$REPO/blacklist.conf"+ ;;+ci)+ [ "$ARCH" = x86-64 ] || die "--profile ci supports only --arch x86-64"+ [ "$BASE" = defconfig ] || die "--base cannot be combined with --profile ci"+ FRAGMENTS=("$REPO/kernel-ci.conf")+ BLACKLIST=""+ ;;+*) die "unsupported --profile $PROFILE (full, ci)" ;;+esac+for f in "${FRAGMENTS[@]}"; do[ -f "$f" ] || die "missing $f"done+if [ -n "$BLACKLIST" ]; then+ [ -f "$BLACKLIST" ] || die "missing $BLACKLIST"+fi# An ML-DSA module signing key (kernel.conf) is only selectable when# Kconfig's OPENSSL_SUPPORTS_ML_DSA probe finds an openssl CLI with# ML-DSA on PATH, so this must hold before the configuration is assembled,@@ -284,14 +300,18 @@ lint_file() {grep -qx "$sym" "$KNOWN" || log "lint: $(basename "$f"): CONFIG_$sym does not exist in $REF"done < <(sed -nE 's/^CONFIG_([A-Za-z0-9_]+)=.*/\1/p; s/^# CONFIG_([A-Za-z0-9_]+) is not set$/\1/p' "$f" | sort -u)}-for f in "${FRAGMENTS[@]}" "$BLACKLIST"; do lint_file "$f"; done+for f in "${FRAGMENTS[@]}"; do lint_file "$f"; done+[ -z "$BLACKLIST" ] || lint_file "$BLACKLIST"# ---------------------------------------------------------------------------# Configuration: base, fragments, blacklist, olddefconfig# ---------------------------------------------------------------------------kmake() { make -C "$SRC" O="$OBJ" ARCH="$KARCH" "${MAKE_TC[@]}" "$@"; }-if [ "$BASE" = defconfig ]; then+if [ "$PROFILE" = ci ]; then+ log "base: $KARCH allnoconfig"+ kmake -s allnoconfig+elif [ "$BASE" = defconfig ]; thenlog "base: $KARCH $DEFCONFIG"kmake -s "$DEFCONFIG"else@@ -320,12 +340,14 @@ if [ -n "$MODULE_KEY" ]; then"$SRC/scripts/config" --file "$OBJ/.config" --set-str MODULE_SIG_KEY "$MODULE_KEY"fi-log "applying blacklist.conf"BLACKLISTED=()-while IFS= read -r sym; do BLACKLISTED+=("$sym"); done < <(sed -nE 's/^# CONFIG_([A-Za-z0-9_]+) is not set$/\1/p' "$BLACKLIST")-for sym in "${BLACKLISTED[@]}"; do- "$SRC/scripts/config" --file "$OBJ/.config" --disable "$sym"-done+if [ -n "$BLACKLIST" ]; then+ log "applying $(basename "$BLACKLIST")"+ while IFS= read -r sym; do BLACKLISTED+=("$sym"); done < <(sed -nE 's/^# CONFIG_([A-Za-z0-9_]+) is not set$/\1/p' "$BLACKLIST")+ for sym in "${BLACKLISTED[@]}"; do+ "$SRC/scripts/config" --file "$OBJ/.config" --disable "$sym"+ done+filog "make olddefconfig"kmake -s olddefconfig@@ -484,7 +506,10 @@ rm -f "$OBJ/.vmlinux.btf-tmp"echo "toolchain=$TOOLCHAIN"echo "compiler=$(grep -E '^CONFIG_CC_VERSION_TEXT=' "$OBJ/.config" | cut -d= -f2-)"echo "base=$BASE"- echo "fragments_sha256=$(cat "${FRAGMENTS[@]}" "$BLACKLIST" | sha256sum | cut -d' ' -f1)"+ config_inputs=("${FRAGMENTS[@]}")+ [ -z "$BLACKLIST" ] || config_inputs+=("$BLACKLIST")+ echo "profile=$PROFILE"+ echo "fragments_sha256=$(cat "${config_inputs[@]}" | sha256sum | cut -d' ' -f1)"echo "module_key=${MODULE_KEY:-generated in $OBJ/certs/signing_key.pem}"echo "libcrypto=$(pkg-config --modversion libcrypto 2>/dev/null || echo unknown)"echo "built_at=$(date -u +%Y-%m-%dT%H:%M:%SZ)"diff --git a/testing/kernel/kernel-ci.conf b/testing/kernel/kernel-ci.confnew file mode 100644index 00000000..bfe932f2--- /dev/null+++ b/testing/kernel/kernel-ci.conf@@ -0,0 +1,156 @@+# kernel-ci.conf: minimal test-kernel configuration. [S]+# This profile builds a small, non-deployable kernel for shirudo CI. [S]+CONFIG_LOCALVERSION="-shirudo-ci"+CONFIG_64BIT=y+CONFIG_SMP=y+CONFIG_PERF_EVENTS=y+# Signed BPF admission and attach mechanisms exercised by the VM suite. [S]+CONFIG_BPF=y+CONFIG_BPF_SYSCALL=y+CONFIG_BPF_JIT=y+CONFIG_BPF_LSM=y+CONFIG_BPF_EVENTS=y+CONFIG_UPROBE_EVENTS=y+CONFIG_CGROUPS=y+CONFIG_CGROUP_BPF=y+CONFIG_FTRACE=y+CONFIG_FTRACE_SYSCALLS=y+CONFIG_FUNCTION_TRACER=y+CONFIG_DYNAMIC_FTRACE=y+CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS=y+CONFIG_UPROBES=y+CONFIG_SECCOMP=y+CONFIG_SECCOMP_FILTER=y+CONFIG_CROSS_MEMORY_ATTACH=y+CONFIG_DEBUG_INFO=y+CONFIG_DEBUG_KERNEL=y+CONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT=y+CONFIG_DEBUG_INFO_BTF=y+CONFIG_DEBUG_INFO_BTF_MODULES=n+CONFIG_KEYS=y+CONFIG_ASYMMETRIC_KEY_TYPE=y+CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE=y+CONFIG_X509_CERTIFICATE_PARSER=y+CONFIG_PKCS8_PRIVATE_KEY_PARSER=y+CONFIG_PKCS7_MESSAGE_PARSER=y+CONFIG_SYSTEM_DATA_VERIFICATION=y+CONFIG_SYSTEM_TRUSTED_KEYS=""+CONFIG_UTS_NS=y+# ML-DSA verification is required for the signed-loader tests. [S]+CONFIG_MODULE_SIG=y+CONFIG_MODULE_SIG_ALL=n+CONFIG_CRYPTO_RSA=y+CONFIG_CRYPTO_SHA256=y+CONFIG_CRYPTO_MLDSA=y+CONFIG_CRYPTO_SHA512=y+CONFIG_TCG_TPM=y+CONFIG_TCG_TIS_CORE=y+CONFIG_TCG_TIS=y+CONFIG_TCG_CRB=y+# The test target uses the BPF LSM as its only LSM. [S]+CONFIG_SECURITY=y+CONFIG_SECURITY_NETWORK=y+CONFIG_SECURITYFS=y+CONFIG_LSM="bpf"+# Initrd, dm-verity and filesystem support for the test VM. [S]+CONFIG_DEVTMPFS=y+CONFIG_DEVTMPFS_MOUNT=y+CONFIG_BINFMT_ELF=y+CONFIG_BINFMT_SCRIPT=y+CONFIG_INOTIFY_USER=y+CONFIG_MODULES=y+CONFIG_MODULE_UNLOAD=y+CONFIG_BLK_DEV=y+CONFIG_BLOCK=y+CONFIG_MD=y+CONFIG_BLK_DEV_DM=y+CONFIG_BLK_DEV_INITRD=y+CONFIG_RD_GZIP=y+CONFIG_RD_XZ=y+CONFIG_RD_ZSTD=y+CONFIG_BLK_DEV_LOOP=y+CONFIG_BLK_DEV_NVME=y+CONFIG_PARTITION_ADVANCED=y+CONFIG_EFI=y+CONFIG_EFI_STUB=y+CONFIG_EFI_PARTITION=y+CONFIG_FAT_FS=y+CONFIG_VFAT_FS=y+CONFIG_ISO9660_FS=y+CONFIG_NLS_CODEPAGE_437=y+CONFIG_NLS_ASCII=y+CONFIG_NLS_ISO8859_1=y+CONFIG_NLS_UTF8=y+CONFIG_NLS_DEFAULT="utf8"+# VM networking, file sharing and packet paths exercised by tests. [S]+CONFIG_VIRTIO_MENU=y+CONFIG_VIRTIO=y+CONFIG_VIRTIO_PCI=y+CONFIG_VIRTIO_PCI_LEGACY=y+CONFIG_VIRTIO_BLK=y+CONFIG_VIRTIO_NET=y+CONFIG_PCI=y+CONFIG_PCI_MSI=y+CONFIG_PCIEPORTBUS=y+CONFIG_NET=y+CONFIG_NET_NS=y+CONFIG_USER_NS=y+CONFIG_PACKET=y+CONFIG_NETDEVICES=y+CONFIG_NET_CORE=y+CONFIG_ETHERNET=y+CONFIG_NET_VENDOR_AMAZON=y+CONFIG_ENA_ETHERNET=y+CONFIG_INET=y+CONFIG_IPV6=y+CONFIG_UNIX=y+CONFIG_NETWORK_FILESYSTEMS=y+CONFIG_NET_9P=y+CONFIG_NET_9P_VIRTIO=y+CONFIG_9P_FS=y+CONFIG_OVERLAY_FS=y+CONFIG_KEXEC_FILE=y+CONFIG_MISC_FILESYSTEMS=y+CONFIG_SQUASHFS=y+CONFIG_SQUASHFS_ZLIB=y+CONFIG_SQUASHFS_XZ=y+CONFIG_SQUASHFS_ZSTD=y+CONFIG_EXT4_FS=y+CONFIG_EXT4_FS_SECURITY=y+CONFIG_DM_VERITY=y+CONFIG_DM_VERITY_VERIFY_ROOTHASH_SIG=y+CONFIG_TMPFS=y+CONFIG_TMPFS_XATTR=y+CONFIG_TTY=y+CONFIG_ACPI=y+CONFIG_PNP=y+CONFIG_PNPACPI=y+CONFIG_NETFILTER=y+CONFIG_NETFILTER_INGRESS=y+CONFIG_NF_CONNTRACK=y+CONFIG_NF_TABLES=m+CONFIG_VETH=m+CONFIG_BRIDGE=m+CONFIG_VLAN_8021Q=m+CONFIG_NF_FLOW_TABLE=m+CONFIG_NF_FLOW_TABLE_INET=m+CONFIG_NF_FLOW_TABLE_PROCFS=y+CONFIG_PROC_FS=y+CONFIG_CC_OPTIMIZE_FOR_SIZE=y+# QEMU x86-64 VM boot support. [S]+CONFIG_X86_64=y+CONFIG_X86=y+CONFIG_HYPERVISOR_GUEST=y+CONFIG_PARAVIRT=y+CONFIG_PARAVIRT_CLOCK=y+CONFIG_KVM_GUEST=y+CONFIG_KEXEC=y+CONFIG_SERIAL_8250=y+CONFIG_SERIAL_8250_CONSOLE=y+CONFIG_KERNEL_XZ=y+CONFIG_KERNEL_GZIP=n+CONFIG_KERNEL_ZSTD=n+CONFIG_KERNEL_LZMA=n+CONFIG_KERNEL_LZO=n+CONFIG_KERNEL_LZ4=n+CONFIG_KERNEL_UNCOMPRESSED=ndiff --git a/testing/kernel/patches/0021-bpf-Read-security.bpf-xattrs-for-LSM-programs-without-the-callers-permission.patch b/testing/kernel/patches/0021-bpf-Read-security.bpf-xattrs-for-LSM-programs-without-the-callers-permission.patchnew file mode 100644index 00000000..9994cee0--- /dev/null+++ b/testing/kernel/patches/0021-bpf-Read-security.bpf-xattrs-for-LSM-programs-without-the-callers-permission.patch@@ -0,0 +1,64 @@+From 4d30e9a670741ab7ab778f28bebf445624416059 Mon Sep 17 00:00:00 2001+From: Daniel Borkmann <[email protected]>+Date: Tue, 3 Nov 2026 10:49:48 +0000+Subject: [PATCH] bpf: Read security.bpf xattrs for LSM programs without the+ caller's permission++bpf_get_dentry_xattr() and bpf_get_file_xattr() check that the current+task may read the inode before they return an xattr, for user. and+security.bpf. names alike. For a BPF LSM program the labels it keeps+under security.bpf. are its own, and the task whose operation it decides+is the one the program may refuse: its read permission on the inode says+nothing about the label. The in-kernel LSMs read their labels with+__vfs_getxattr() in kernel context for that reason.++The check also lets that task choose what the program sees. A read that+fails with -EACCES or -EPERM looks to the program like a missing label+unless it fails every such operation closed, which it cannot do without+refusing every write-only open and every unlink of an unreadable file on+the system. Root arranges the failure for any labeled inode: an idmapped+bind mount shows uid 0's files as another id's to the permission check,+an ACL grants another id a write without a read, and a task at that id+through setfsuid() keeps CAP_SYS_ADMIN but not the DAC capabilities, so+the kernel lets it write what the program then takes for unlabeled.+Another LSM's inode_permission policy does the same.++Skip the permission check for security.bpf. names. The kfuncs are+registered for LSM and struct_ops programs, which run in kernel context+with the labels they maintain; user. names keep the check.++Signed-off-by: Daniel Borkmann <[email protected]>+---+ fs/bpf_fs_kfuncs.c | 16 +++++++++++++---+ 1 file changed, 13 insertions(+), 3 deletions(-)++diff --git a/fs/bpf_fs_kfuncs.c b/fs/bpf_fs_kfuncs.c+index 35ccd98a4de6..3447159eb919 100644+--- a/fs/bpf_fs_kfuncs.c++++ b/fs/bpf_fs_kfuncs.c+@@ -106,9 +106,19 @@ static int bpf_xattr_read_permission(const char *name, struct inode *inode)+ if (!inode)+ return -EINVAL;++- /* Allow reading xattr with user. and security.bpf. prefix */+- if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&+- !match_security_bpf_prefix(name))++ /*++ * The labels a BPF LSM keeps under security.bpf. are its own, read++ * as SELinux, Smack and EVM read theirs with __vfs_getxattr(): in++ * kernel context, not with the permission of the task whose++ * operation the program decides. That task's read permission says++ * nothing about the label, and a read that fails for it, through an++ * idmapped mount, an ACL or another LSM's policy, would hide the++ * label from the program that enforces it. user. xattrs stay the++ * task's to read.++ */++ if (match_security_bpf_prefix(name))++ return 0;++ if (strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))+ return -EPERM;++ return inode_permission(&nop_mnt_idmap, inode, MAY_READ);+--+2.43.0+diff --git a/testing/kernel/patches/0022-bpf-Let-sleepable-LSM-programs-attach-to-the-remaining-inode-hooks.patch b/testing/kernel/patches/0022-bpf-Let-sleepable-LSM-programs-attach-to-the-remaining-inode-hooks.patchnew file mode 100644index 00000000..ce1c7fc1--- /dev/null+++ b/testing/kernel/patches/0022-bpf-Let-sleepable-LSM-programs-attach-to-the-remaining-inode-hooks.patch@@ -0,0 +1,61 @@+From 4394d88fc34a232779f9b1322c5cf6169e3fab13 Mon Sep 17 00:00:00 2001+From: Daniel Borkmann <[email protected]>+Date: Tue, 3 Nov 2026 13:52:42 +0000+Subject: [PATCH] bpf: Let sleepable LSM programs attach to the remaining inode+ hooks++A BPF LSM program reads an xattr with bpf_get_dentry_xattr() or+bpf_get_file_xattr(), which sleep, so only a sleepable program can+decide an operation by the label an inode carries. The set of hooks+such a program may attach to holds inode_create, inode_symlink,+inode_mknod, inode_unlink, inode_rename, inode_rmdir, inode_setattr,+inode_setxattr and inode_removexattr, but not inode_mkdir and+inode_link, the other two hooks that decide a new directory entry, nor+inode_set_acl, inode_remove_acl and inode_file_setattr, which decide+an ACL and an inode flag change, nor inode_copy_up, which decides an+overlay copy-up. Each of them is called from process context with the+inode lock held, as the hooks already in the set are, and may sleep.++Add them, so that a program which labels directories can read the+label of the directory a mkdir or a link changes, the label of the+inode an ACL or flag change targets, and the label of the inode an+overlay copies up, instead of a cache of its own that the inode's+eviction empties.++Signed-off-by: Daniel Borkmann <[email protected]>+---+ kernel/bpf/bpf_lsm.c | 6 +++++++ 1 file changed, 6 insertions(+)++diff --git a/kernel/bpf/bpf_lsm.c b/kernel/bpf/bpf_lsm.c+index f58dce888ff4..688c249680ef 100644+--- a/kernel/bpf/bpf_lsm.c++++ b/kernel/bpf/bpf_lsm.c+@@ -333,18 +333,24 @@ BTF_ID(func, bpf_lsm_file_open)+ BTF_ID(func, bpf_lsm_file_post_open)+ BTF_ID(func, bpf_lsm_file_receive)+++BTF_ID(func, bpf_lsm_inode_copy_up)+ BTF_ID(func, bpf_lsm_inode_create)++BTF_ID(func, bpf_lsm_inode_file_setattr)+ BTF_ID(func, bpf_lsm_inode_free_security)+ BTF_ID(func, bpf_lsm_inode_getattr)+ BTF_ID(func, bpf_lsm_inode_getxattr)++BTF_ID(func, bpf_lsm_inode_link)++BTF_ID(func, bpf_lsm_inode_mkdir)+ BTF_ID(func, bpf_lsm_inode_mknod)+ BTF_ID(func, bpf_lsm_inode_need_killpriv)+ BTF_ID(func, bpf_lsm_inode_post_setxattr)+ BTF_ID(func, bpf_lsm_inode_post_removexattr)+ BTF_ID(func, bpf_lsm_inode_readlink)++BTF_ID(func, bpf_lsm_inode_remove_acl)+ BTF_ID(func, bpf_lsm_inode_removexattr)+ BTF_ID(func, bpf_lsm_inode_rename)+ BTF_ID(func, bpf_lsm_inode_rmdir)++BTF_ID(func, bpf_lsm_inode_set_acl)+ BTF_ID(func, bpf_lsm_inode_setattr)+ BTF_ID(func, bpf_lsm_inode_setxattr)+ BTF_ID(func, bpf_lsm_inode_symlink)+--+2.43.0+diff --git a/testing/kernel/patches/0023-bpf-Treat-dentry-d_parent-as-a-trusted-pointer.patch b/testing/kernel/patches/0023-bpf-Treat-dentry-d_parent-as-a-trusted-pointer.patchnew file mode 100644index 00000000..6375790c--- /dev/null+++ b/testing/kernel/patches/0023-bpf-Treat-dentry-d_parent-as-a-trusted-pointer.patch@@ -0,0 +1,39 @@+From 66a9f20316aea80c61881a1c4c722261587375ee Mon Sep 17 00:00:00 2001+From: Daniel Borkmann <[email protected]>+Date: Tue, 3 Nov 2026 13:52:42 +0000+Subject: [PATCH] bpf: Treat dentry->d_parent as a trusted pointer++An LSM program that decides a new directory entry in inode_create,+inode_mkdir, inode_symlink, inode_mknod or inode_link receives the+directory as a struct inode and the new entry as a dentry. The+directory's label lives in an xattr that bpf_get_dentry_xattr() reads+from a dentry, and the one the program has for it is dentry->d_parent,+which the verifier treats as an untrusted pointer, so the kfunc refuses+it and the program cannot read the label of the directory it decides+on.++d_parent of a dentry an inode hook receives is stable while the hook+runs, under the parent's inode lock the caller holds, and is never NULL,+since the root's points to itself. Add it beside d_inode as trusted or+null, which keeps a program checking it before use.++Signed-off-by: Daniel Borkmann <[email protected]>+---+ kernel/bpf/verifier.c | 1 ++ 1 file changed, 1 insertion(+)++diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c+index 935b163f81e4..0ef1d5076485 100644+--- a/kernel/bpf/verifier.c++++ b/kernel/bpf/verifier.c+@@ -6509,6 +6509,7 @@ BTF_TYPE_SAFE_TRUSTED(struct scx_sub_detach_args) {++ BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct dentry) {+ struct inode *d_inode;++ struct dentry *d_parent;+ };++ BTF_TYPE_SAFE_TRUSTED_OR_NULL(struct linux_binprm) {+--+2.43.0+