Kernel modules arch linux

Signed kernel modules

Signed kernel modules provide a mechanism for the kernel to verify the integrity of a module.

Overview

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 code. They are compiled during the normal kernel build.
  • Out-of-tree modules which are not part of the kernel source distribution. They are built outside of the kernel tree, requiring the 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.

During a standard kernel compilation, the kernel build tools create a private/public key pair and sign every in-tree module (using the 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 that the module is unchanged.

The kernel can be enabled to always verify modules and report any failures to standard logs. The choice to permit the loading and use of a module which could not be verified can be either compiled into kernel or turned on at runtime using a kernel parameter as explained below.

Summary of what needs to be done

The starting point is based on a custom kernel package as outlined in Kernel/Arch Build System. We will modify the build to sign the standard in-tree kernel modules and to provide the prerequisites for signing and verifying out-of-tree modules.

  • 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 into the kernel. We will create a separate public/private key pair on each build.

Each kernel build needs to made aware of the key pair to be used for signing out-of-tree modules. A kernel configuration parameter is now used to make the kernel aware of additional signing keys: CONFIG_SYSTEM_TRUSTED_KEYS=»/path/to/oot-signing_keys.pem» .

Keys and signing tools will be stored in the current module build directory. Nothing needs to be done to clean this as removal is handled by the standard module cleanup. The private and public keys are both installed in /usr/lib/modules/kernel_versionbuild/certs-local .

Читайте также:  Linux check process ram

Kernel configuration

CONFIG_SYSTEM_TRUSTED_KEYS will be updated automatically using the script genkeys.py provided below. In addition, the following configuration options should be set either manually by editing the .config file, or via make menuconfig in the Linux src directory and subsequently copying the updated .config file back to the build file config . It is preferable to use elliptic curve type keys and zstd compression.

CONFIG_MODULE_SIG=y Enable Loadable module suppot ---> Module Signature Verification - activate CONFIG_MODULE_SIG_FORCE=n Require modules to be validly signed -> leave off (for now) This allows the decision to enforce verified modules only as boot command line. If you are comfortable all is working then by all means change this to 'y' Command line version of this is : module.sig_enforce=1 CONFIG_MODULE_SIG_HASH=sha512 Automatically sign all modules - activate Which hash algorithm -> SHA-512 CONFIG_MODULE_COMPRESS_ZSTD=y Compress modules on installation - activate Compression algorithm (ZSTD) CONFIG_MODULE_SIG_KEY_TYPE_ECDSA=y Cryptographic API ---> Certificates for Signature Checking ---> Type of module signing key to be generated -> ECDSA CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS=n Enable Loadable module support ---> Allow loading of modules with missing namespace imports - set to no

Kernel command line

When you have confirmed that the modules are being signed and that the kernel works as it should, you can enable the following kernel parameter to require that the kernel only permits verified modules to be loaded:

Before forcing verified modules on, please confirm that the system logs do not show any module signature failures being reported.

Tools needed

kernel build package

In the directory where the kernel package is built:

This directory will provide the tools to create the keys, as well as signing kernel modules.

Put these files into certs-local :

  • x509.oot.genkey
  • genkeys.py
  • install-certs.py
  • sign_module.py
  • signer_class.py
  • utils.py

The files genkeys.py and its companion configuration file x509.oot.genkey are used to create key pairs.

genkeys.py also provides the kernel with the key information by updating the configuration file(s) used to build the kernel.

The script sign_module.py signs out-of-tree kernel modules. It can be run manually and is invoked by dkms/kernel-sign.sh . It handles modules compressed with xzand gzip and depends on python-zstandard to help handle those compressed with zstd.

genkeys.py will create the key pairs in a directory named by date-time

genkeys.py will check and update kernel configs given by the —config config(s) option. It takes either a single config file, or a shell glob for multiple files. e.g. —config ‘conf/config.*’. All configs will be updated with the same key. The default keytype is ec (elliptic curve) and the default hash is sha512. These can be changed with command line options. See genkeys.py -h for more details.

It also creates a soft link current to the same directory holding the current key pairs.

Читайте также:  Arch linux update kernel

install-certs.py is to be called from the package_headers() function of PKGBUILD to install the signing keys. Example is given below.

These files are available and links are provided below.

DKMS support

Add 2 files to the dkms directory:

These will be installed in /etc/dkms and provide the means for DKMS to automatically sign modules using the local key. This is the recommended way to sign out-of-tree kernel modules. As explained below, once this is installed, all that is needed is for DKMS to automatically sign modules is to make a soft link for each module to the configuration file.

$ cd /etc/dkms # ln -s kernel-sign.conf module_name.conf 
# ln -s kernel-sign.conf vboxdrv.conf

The link creation can easily be added to an arch package to simplify further if desired.

Modify PKGBUILD

We need to make changes to kernel build as follows:

prepare()

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

The default key regeneration refresh period is 7 days, but this can be changed on the command line. So if you want to create new keys monthly, then add «—refresh 30days» as an argument to genekeys.py. You can refresh on every build by using «—refresh always». Refresh units can be seconds,minutes,hours,days or weeks.

_package-headers()

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. " certs_local_src="https://wiki.archlinux.org/certs-local" certs_local_dst="$/certs-local" $certs_local_src/install-certs.py $certs_local_dst # DKMS tools dkms_src="https://wiki.archlinux.org/title/$certs_local_src/dkms" dkms_dst="$/etc/dkms" mkdir -p $dkms_dst rsync -a $dkms_src/ $dkms_dst/ >

Files required

The 5 supporting files referenced above are available for download from the github.com/gene-git/Arch-SKM repository:

Note: There is also an aur version of these files available ( arch-sign-modules AUR ). After building and install they can be found at /usr/src/certs-local

Remember to ensure that the scripts are executable.

Helper scripts

The factual accuracy of this article or section is disputed.

Reason: The arch-sign-modules AUR package comes from a fork of the Arch-SKM repository which is used in the previous sections. It is not clear what is wrong with the original repository nor why changes resulted in a fork instead of fixing/improving the original repository. (Discuss in Talk:Signed kernel modules)

The abk helper script reduces the manual steps for building a fully signed custom kernel to 3 commands to Update / Build & Install the kernel:

abk -u kernel-name abk -b kernel-name abk -i kernel-name

With signed kernel module support for:

Источник

Dynamic Kernel Module Support

Dynamic Kernel Module Support (DKMS) is a program/framework that enables generating Linux kernel modules whose sources generally reside outside the kernel source tree. The concept is to have DKMS modules automatically rebuilt when a new kernel is installed.

Читайте также:  Linux on the chromebook pixel

This means that a user does not have to wait for a company, project, or package maintainer to release a new version of the module. Since the introduction of pacman hooks, the rebuild of the modules is handled automatically when a kernel is upgraded.

Installation

Install the dkms package and the headers for the target kernel/kernels. For example, for the default linux kernel this would be linux-headers . Other kernels have their own respective headers packages.

A good number of modules that lie outside the kernel source tree have a DKMS variant; a few are hosted in the official repositories, most are found in the AUR.

Upgrades

Though the rebuild of the DKMS modules is usually seamless during a kernel upgrade, it may still happen that the rebuild fails. You should pay extra attention to the pacman output. This applies in particular if the system relies on the DKMS module to boot successfully and/or if you use DKMS with a custom kernel not in the official repositories.

To deal with changes in the kernel, fix bugs, or add necessary features consider upgrading the DKMS package before rebooting.

Usage

Usage for invoking DKMS manually.

Tab-completion is available by doing:

# source /usr/share/bash-completion/completions/dkms

List modules

To list the current status of modules, versions and kernels within the tree:

Rebuild modules

Rebuild all modules for the currently running kernel:

# dkms autoinstall -k 3.16.4-1-ARCH

To build a specific module for the currently running kernel:

# dkms install -m nvidia -v 334.21

To build a module for all kernels:

# dkms install nvidia/334.21 --all

Remove modules

To remove a module (old ones are not automatically removed):

# dkms remove -m nvidia -v 331.49 --all
# dkms remove nvidia/331.49 --all

If the package dkms is removed the information regarding previous module build files is lost. If this is the case, go through /usr/lib/modules/kernel_release and /var/lib/dkms/package_name and delete all files and directories no longer in use.

DKMS package creation

Initial ramdisk

In case you have got any kernel modules installed via DKMS that are used in initial ramdisk, e.g. zfs-dkms AUR , you may want to write a pacman hook to automate the process of regenerating initramfs image(s).

For example, when using linux and mkinitcpio, to update ZFS module after each zfs-dkms AUR upgrade,

/etc/pacman.d/hooks/90-mkinitcpio-dkms-linux.hook
[Trigger] Operation=Install Operation=Upgrade Operation=Remove Type=Package Target=zfs-dkms Target=linux [Action] Description=Update dkms modules in Linux initcpio Depends=mkinitcpio When=PostTransaction NeedsTargets Exec=/bin/sh -c 'while read -r trg; do case $trg in linux) exit 0; esac; done; /usr/bin/mkinitcpio -p linux'

You may add more targets to the hook and make additional copies of the hook if you have installed other kernels. Note the 90- prefix is necessary to make sure it runs after the DKMS hooks.

See also

Источник

Оцените статью
Adblock
detector