Skip to content

Commit

Permalink
(re-)sign all the modules after processing debuginfo
Browse files Browse the repository at this point in the history
Extracting debuginfo strips away modules signatures (which would be
invalid anyway, as the module file is modified). Include the code for
signing modules again, taken from Fedora's kernel.spec.

Fixes QubesOS/qubes-issues#5497
  • Loading branch information
marmarek committed Dec 5, 2019
1 parent 5d68948 commit 8aca753
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
27 changes: 27 additions & 0 deletions kernel.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
# debuginfo build is disabled by default to save disk space (it needs 2-3GB build time)
%define with_debuginfo 0

# Sign all modules
%global signmodules 1

%if !%{with_debuginfo}
%global debug_package %{nil}
%define setup_config --disable CONFIG_DEBUG_INFO
Expand All @@ -58,6 +61,7 @@ BuildRequires: qubes-kernel-vm-support
BuildRequires: dracut
BuildRequires: busybox
BuildRequires: bc
BuildRequires: openssl
BuildRequires: openssl-devel
BuildRequires: gcc-plugin-devel
BuildRequires: elfutils-libelf-devel
Expand Down Expand Up @@ -111,10 +115,12 @@ Source0: linux-%{upstream_version}.tar.gz
Source5: WireGuard-0.0.20191012.tar.xz
Source16: guards
Source17: apply-patches
Source18: mod-sign.sh
Source33: check-for-config-changes
Source34: gen-config
Source100: config-base
Source101: config-qubes
%define modsign_cmd %{SOURCE18}

Patch0: 0001-xen-netfront-detach-crash.patch
Patch1: 0002-mce-hide-EBUSY-initialization-error-on-Xen.patch
Expand Down Expand Up @@ -221,6 +227,27 @@ if [ -d "%_builddir/wireguard" ]; then
make -C %kernel_build_dir M=%_builddir/wireguard/src modules
fi


%define __modsign_install_post \
if [ "%{signmodules}" -eq "1" ]; then \
%{modsign_cmd} certs/signing_key.pem certs/signing_key.x509 $RPM_BUILD_ROOT/lib/modules/%kernelrelease/ \
fi \
%{nil}

#
# Disgusting hack alert! We need to ensure we sign modules *after* all
# invocations of strip occur, which is in __debug_install_post if
# find-debuginfo.sh runs, and __os_install_post if not.
#

%define __spec_install_post \
%{?__debug_package:%{__debug_install_post}}\
%{__arch_install_post}\
%{__os_install_post}\
%{?__remove_unwanted_dbginfo_install_post}\
%{__modsign_install_post}


%install

# get rid of /usr/lib/rpm/brp-strip-debug
Expand Down
37 changes: 37 additions & 0 deletions mod-sign.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#! /bin/bash

# The modules_sign target checks for corresponding .o files for every .ko that
# is signed. This doesn't work for package builds which re-use the same build
# directory for every flavour, and the .config may change between flavours.
# So instead of using this script to just sign lib/modules/$KernelVer/extra,
# sign all .ko in the buildroot.

# This essentially duplicates the 'modules_sign' Kbuild target and runs the
# same commands for those modules.

MODSECKEY=$1
MODPUBKEY=$2
moddir=$3

modules=`find $moddir -type f -name '*.ko'`

NPROC=`nproc`
[ -z "$NPROC" ] && NPROC=1

# NB: this loop runs 2000+ iterations. Try to be fast.
echo "$modules" | xargs -r -n16 -P $NPROC sh -c "
for mod; do
./scripts/sign-file sha256 $MODSECKEY $MODPUBKEY \$mod
rm -f \$mod.sig \$mod.dig
done
" DUMMYARG0 # xargs appends ARG1 ARG2..., which go into $mod in for loop.

RANDOMMOD=$(echo "$modules" | sort -R | head -n 1)
if [ "~Module signature appended~" != "$(tail -c 28 $RANDOMMOD)" ]; then
echo "*****************************"
echo "*** Modules are unsigned! ***"
echo "*****************************"
exit 1
fi

exit 0

0 comments on commit 8aca753

Please sign in to comment.