From ArchWiki


Signed [[1]] Kernel modules provide a mechanism for the kernel to verify the integrity of a module.


The Linux Kernel distinguishes and keeps separate the verification of modules from requiring or forcing modules to verify before allowing them to be loaded. Kernel modules fall into 2 classes:

  • Standard 'in tree' modules which come with the kernel
    • source and are compiled during the normal kernel build.
  • Out of tree modules which are not part of the kernel source distribution:
    • Built outside of the kernel tree
    • Require kernel headers package for each kernel they are to be built for. They can be built manually for a specific kernel and packaged - or they can be built whenever needed using dkms
    • Examples of such packages, provided by arch, include virtualbox and wireguard.
Note: During a standard kernel compile, the kernel build tools create a private/public key pair and signs every in tree module (using private key). The public key is saved in the kernel itself. When a module is subsequently loaded, the public key can then be used to verify the module is unchanged.

The kernel can be enabled to always verify modules, and it then reports any failures to standard logs, the choice to permit the loading and use of a module which does not verify can be either compiled in to kernel or turned on at run time using a command line option to kernel boot command as explained below.

How to sign kernel modules using a custom kernel

The starting point is based on a custom kernel package as outlined in this article Arch_Build_System. We will modify the build to:

  • Sign the standard in tree kernel modules
  • Provide what is needed to have signed out of tree modules and for the kernel to verify those modules.

Note: The goal here is to have:
  • In tree modules signed during the standard kernel build process.
    • The standard kernel build creates a fresh public/private key pair on each build.
  • Out of tree modules are signed and the associated public key is compiled in to the kernel.
    • We will create a separate public/private key pair on each build.

Summary of what needs to be done


       Each kernel build needs to made aware of the key/cert being used.
       These change with each new kernel build :
       Kernel build now has additional config :

       By installing in current module dir then cleaning is automatic
       install certs in /usr/lib/modules/<kernel-vers>-<build>/certs-local

Kernel Config

Note: CONFIG_SYSTEM_TRUSTED_KEYS will be added automatically as explained below.
       In addition the following config options should be set manually editing the 'config' file, or via:

       % make menuconfig:

in the linux 'src' area and subsequently copying the updated '.config' file back to the build file 'config'
       Enable Loadable module suppot --->
       Module Signature Verification           -  activate
       Require modules to be validly signed -> leave off
               This allows the decision to enforce verified modules only as boot command line.
               If you're comfortable all is working then by all means change this to 'y'
               Command line version of this is : module.sig_enforce=1

       Automatically sign all modules  - activate
       Which hash algorithm    -> SHA-512
       Compress modules on installation        - activate
               Compression algorithm (XZ)

       Allow loading of modules with missing namespace imports - set to no

Boot Command Line

       Once you're comfortable things working well you can enable 
       Boot time command to force only verified modules allowed:


Tools needed

kernel build package

       In the directory where the kernel package is built:

       % mkdir certs-local

       This dir will have the tools to create the keys, as well as signing kernel modules.
       Put the 4 files into certs-local:
       The files and its config x509.oot.genkey are used to create key pairs.
       the file is run after that to provide the kernel with the key info by updating
       the config file used to build the kernel.
       The script sign_manual will be used to sign out of tree kernel modules. will create the key pairs in a dir named by date-time.
       It also creates file 'current_key_dir' with that dir name and a soft link 'current' to 
       the same dir with the 'current' key pairs.

dkms support

       % mkdir certs-local/dkms

       Add 2 files to the dkms dir:
       These will be installed in /etc/dkms and provide a mechanism for dkms to automatically
       sign modules (using the local key above) - this is the reccommended way to sign kernel 
       modules. As explained, below - once this is installed - all that is needed to have dkms
       automatically sign modules is to make a soft link:
       % cd /etc/dkms
       % ln -s kernel-sign.conf <package-name>

       % ln -s kernel-sign.conf virtualbox

       The link creation n easily be added to an arch package to simplify further.


We need to make changes to kernel build as follows:


Note: Add the following to the top of the prepare() function:
       prepare() {

           msg2 "Rebuilding local signing key..."
           cd ../certs-local

           msg2 "Updating kernel config with new key..."
           ./ ../config
           cd ../src



Note: Add the following to the bottom of the _package-headers() function:
        _package-headers() {


         # Out of Tree Module signing
         # This is run in the kernel source / build directory
         msg2 "Local Signing certs for out of tree modules..."


         mkdir -p ${certs_local_dst}
         rsync -a $certs_local_src/{current,$key_dir,$signer} $certs_local_dst/

         # dkms tools
         mkdir -p $dkms_dst

         rsync -a $dkms_src/{kernel-sign.conf,} $dkms_dst/

Files Required

Note: These are the 6 supporting files referenced above
Tip: Don't forget to make the scripts executable.


chmod +x certs-local/*.sh certs-local/dkms/*.sh


# Arg: config file
# Update kernel config : CONFIG_SYSTEM_TRUSTED_KEYS to use current keys



sed -e "s|^CONFIG_SYSTEM_TRUSTED_KEYS=.*$|CONFIG_SYSTEM_TRUSTED_KEYS=\"../../certs-local/$KeyDir/signing_key.pem\"|"  < $Config  > $Config.tmp

mv $Config.tmp $Config

exit 0


[ req ]
default_bits = 4096
distinguished_name = req_distinguished_name
prompt = no
string_mask = utf8only
x509_extensions = myexts 

[ req_distinguished_name ]
#O = Unspecified company
CN = Out of tree kernel module signing key
#emailAddress =

[ myexts ]


# Create new pub/priv key pair for signing out of tree kernel modules.
# Each key pair is stored by date-time

Dt=$(date +'%Y%m%d-%H%M')

mkdir -p $Dt


openssl req -new -nodes -utf8 -sha512 -days 36500 -batch -x509 -config ./x509.oot.genkey \
        -outform PEM -out $KernKey -keyout $KernKey

chmod 0600 $KernKey
openssl pkey -in $KernKey -out $PrivKey
openssl x509 -outform der -in $KernKey -out $KernCrt

rm -f current; ln -s $Dt current

rm -f current_key_dir
echo "$Dt" > ./current_key_dir

exit 0


# Installed as /etc/dkms/kernel-sign.conf
# link this to any module to be signed
# e.g. ln -s /etc/dkms/kernel-sign.conf /etc/dkms/virtualbox.conf



# Installed in /etc/dkms/
#  This is called via POST_BUILD for each module 
#  We use this to sign in the dkms build directory.  

cd ../$kernelver/$arch/module/


if [ -f $SIGN ] ;then

   list=$(/bin/ls -1 *.ko *.ko.xz 2>/dev/null)

   if [ "$list" != "" ]  ; then
       for mod in $list
           echo "DKMS: Signing kernel ($kernelver) module: $mod"
           $SIGN "$mod"
   echo "kernel $kernelver doesn't have out of tree module signing tools"
   echo "skipping signing out of tree modules"