Coffnix:Script to sign kernel modules

From Funtoo
Revision as of 19:35, July 6, 2018 by Coffnix (talk | contribs)
Jump to navigation Jump to search

Since the Linux kernel version 3.7.x, support for the signed kernel modules has been useful. When enabled, the Linux kernel kernel will be fixed. This allows the system to be "hardened", not using the unsigned kernel, or kernel modules to be loaded with a wrong key, to be loaded. Malicious kernel modules are a common system for rootkits to enter a Linux system.

If you want to sign an embedded module in the kernel:

--- Enable loadable module support
[*]   Module signature verification
[*]     Require modules to be validly signed
[*]     Automatically sign all modules
      Which hash algorithm should modules be signed with? (Sign modules with SHA-512) --->

Manually sign modules, for example virtualbox modules ( (app-emulation/virtualbox-modules):

for i in $(find /lib/modules/$(uname -r) -iname "*vbox*.ko"); do
     perl /usr/src/linux/scripts/sign-file sha512 /usr/src/linux/signing_key.priv /usr/src/linux/signing_key.x509 $i

If you use kernel 4.3.3 or higher:

for i in $(find ${MODULES_DIR} -maxdepth 1 -type d|grep -vw "${MODULES_DIR}"|sed s,'/lib/modules/',,g);do
    echo -e "Assinando módulo para kernel ${i}..."
    for z in $(find /lib/modules/${i} -type f -iname "*vbox*.ko");do
        ${KERNEL_DIR}/scripts/sign-file sha512 ${KERNEL_DIR}/certs/signing_key.pem ${KERNEL_DIR}/certs/signing_key.x509 ${z}

Automate the signature of the modules

Create the directories:

root #  mkdir -p /etc/funtoo/scripts
root #  mkdir -p /etc/funtoo/msv-sign

Create the config file, containing the list of modules to sign. Ex: app-emulation/virtualbox-modules

root #  cat /etc/funtoo/msv-sign/virtualbox-modules

Create the script to sign the modules:

root #  cat /etc/funtoo/scripts/

if [ -z "${1}" ];then
        echo -e "ERROR: Please type name of ebuild. Exː \n\n # ${0} virtualbox-modules\n"
        exit 1

KERNEL_DIR="/usr/src/$(readlink /usr/src/linux)"
MODULES_DIR="/lib/modules/$(readlink /usr/src/linux|sed s,linux-,,g)"

while read MODULE;do
        # Sign modules
        MODULE_KO="$(find ${MODULES_DIR} -type f -name "${MODULE}.ko")"
        ${KERNEL_DIR}/scripts/sign-file sha512 ${KERNEL_DIR}/certs/signing_key.pem ${KERNEL_DIR}/certs/signing_key.x509 ${MODULE_KO}

        # reload modules
        rmmod ${MODULE} 2> /dev/null
        modprobe ${MODULE} 2> /dev/null

done < /etc/funtoo/msv-sign/${1}

Run in debug modeː

root #  bash -x /etc/funtoo/scripts/ virtualbox-modules