Difference between revisions of "Signed kernel module support"

From Funtoo
Jump to navigation Jump to search
(fixing pathing on a command to be absolute, add module.sig_enforce boot.conf example)
Line 24: Line 24:
###i## chmod -R 644 /etc/kernel/certs/linux/signing_key.pem
###i## chmod -R 644 /etc/kernel/certs/linux/signing_key.pem
}}
}}


Now, build debian-sources with your own keys:
Now, build debian-sources with your own keys:
Line 31: Line 30:
}}
}}


== Manually signing modules ==
If you ever need to manually sign a kernel module, you can use the scripts/sign-file script available in the Linux kernel source tree. It requires four arguments:
# The hash algorithm to use, such as sha512.
# The private key location.
# The certificate (which includes the public key) location.
# The kernel module to sign.
{{console|body=
###i## /usr/src/linux/scripts/sign-file sha512 /etc/kernel/certs/linux/signing_key.pem /etc/kernel/certs/linux/signing_key.x509 ${MODULE_KO}
}}
== Non-valid signatures and unsigned modules==
{{note|If module.sig_enforce is disabled (default) it will also load modules that are unsigned.}}


'''Optional:''' Enable ''' module.sig_enforce=1'''
If module.sig_enforce=1 is enabled is supplied on the kernel command line, the kernel will only load validly signed modules for which it has a public key. Otherwise, it will also load modules that are unsigned. Any module for which the kernel has a key, but which proves to have a signature mismatch will not be permitted to load.
{{note|If module.sig_enforce is enabled supplied on the kernel command line, the kernel will only load validly signed modules for which it has a public key. Otherwise, it will also load modules that are unsigned. Any module for which the kernel has a key, but which proves to have a signature mismatch will not be permitted to load.}}
Any module that has an unparseable signature will be rejected.
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 on your '''/etc/boot.conf''' to require that the kernel only permits verified modules to be loaded:
 
You need sign all modules into initramfs first:
 
{{console|body=
###i## mkdir /tmp/initram ; cd /tmp/initram
###i## cp /boot/initramfs-debian-sources-x86_64-6.1.4_p1 .
###i## cat initramfs-debian-sources-x86_64-6.1.4_p1 {{!}} xz -d {{!}} cpio -id
###i## find /tmp/initram/lib/modules/ -name "*ko" -exec /usr/src/linux/scripts/sign-file sha256 /etc/kernel/certs/linux/signing_key.pem /etc/kernel/certs/linux/signing_key.x509 '{}' \;
###i## mv /boot/initramfs-debian-sources-x86_64-6.1.4_p1 /boot/initramfs-debian-sources-x86_64-6.1.4_p1.old
###i## find . {{!}} cpio -H newc -o {{!}} xz --check=crc32 --x86 --lzma2 >/boot/initramfs-debian-sources-x86_64-6.1.4_p1
}}
 
Include this parameter on your kernel line into '''/etc/boot.conf''':
 
{{console|body=
module.sig_enforce=1
}}


Example /etc/boot.conf enabling kernel module signature verification:
Example /etc/boot.conf enabling kernel module signature verification:
Line 45: Line 76:
}}
}}


One that is added regenerate your Funtoo Grub config with:
Update configuration file that GRUB will use for booting:
 
{{console|body=
{{console|body=
###i## ego boot
###i## ego boot update
}}
 
== Manually signing modules ==
If you ever need to manually sign a kernel module, you can use the scripts/sign-file script available in the Linux kernel source tree. It requires four arguments:
 
# The hash algorithm to use, such as sha512.
# The private key location.
# The certificate (which includes the public key) location.
# The kernel module to sign.
 
 
{{console|body=
###i## /usr/src/linux/scripts/sign-file sha512 /etc/kernel/certs/linux/signing_key.pem /etc/kernel/certs/linux/signing_key.x509 ${MODULE_KO}
}}
}}



Revision as of 07:08, January 8, 2023

Since the Linux kernel version 3.7.x, support for the signed kernel modules has been useful. When enabled, the Linux 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.

When the Linux kernel is building with module signature verification support enabled, then you can use your own keys. We recommend the debian-sources kernel, just enabling the useflag "sign-modules".

root # echo "sys-kernel/debian-sources sign-modules" >> /etc/portage/package.use
root # mkdir -p /etc/kernel/certs/linux

We will manually generate the private/public key files using the x509.genkey key generation configuration file and the openssl command. Here is an example to generate the public/private key files:

root # openssl req -new -nodes -sha256 -x509 -newkey rsa:2048 -days 36500 -addext extendedKeyUsage=1.3.6.1.5.5.7.3.3 -subj '/CN=Funtoo Secure Boot/' -out /etc/kernel/certs/linux/signing_key.cert -keyout /etc/kernel/certs/linux/signing_key.asc
root # cat /etc/kernel/certs/linux/signing_key.asc /etc/kernel/certs/linux/signing_key.cert > /etc/kernel/certs/linux/signing_key.pem
root # openssl x509 -outform der -in /etc/kernel/certs/linux/signing_key.pem -out /etc/kernel/certs/linux/signing_key.x509

Create DER file to sign grub and SHIM (secure boot):

root # openssl x509 -in /etc/kernel/certs/linux/signing_key.cert -outform der -out /etc/kernel/certs/linux/signing_key.der

Fix permissions:

root # chmod -R 644 /etc/kernel/certs/linux/signing_key.pem

Now, build debian-sources with your own keys:

root # emerge sys-kernel/debian-sources

Manually signing modules

If you ever need to manually sign a kernel module, you can use the scripts/sign-file script available in the Linux kernel source tree. It requires four arguments:

  1. The hash algorithm to use, such as sha512.
  2. The private key location.
  3. The certificate (which includes the public key) location.
  4. The kernel module to sign.
root # /usr/src/linux/scripts/sign-file sha512 /etc/kernel/certs/linux/signing_key.pem /etc/kernel/certs/linux/signing_key.x509 ${MODULE_KO}

Non-valid signatures and unsigned modules

   Note

If module.sig_enforce is disabled (default) it will also load modules that are unsigned.

If module.sig_enforce=1 is enabled is supplied on the kernel command line, the kernel will only load validly signed modules for which it has a public key. Otherwise, it will also load modules that are unsigned. Any module for which the kernel has a key, but which proves to have a signature mismatch will not be permitted to load. Any module that has an unparseable signature will be rejected. 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 on your /etc/boot.conf to require that the kernel only permits verified modules to be loaded:

You need sign all modules into initramfs first:

root # mkdir /tmp/initram ; cd /tmp/initram
root # cp /boot/initramfs-debian-sources-x86_64-6.1.4_p1 .
root # cat initramfs-debian-sources-x86_64-6.1.4_p1 | xz -d | cpio -id
root # find /tmp/initram/lib/modules/ -name "*ko" -exec /usr/src/linux/scripts/sign-file sha256 /etc/kernel/certs/linux/signing_key.pem /etc/kernel/certs/linux/signing_key.x509 '{}' \;
root # mv /boot/initramfs-debian-sources-x86_64-6.1.4_p1 /boot/initramfs-debian-sources-x86_64-6.1.4_p1.old
root # find . | cpio -H newc -o | xz --check=crc32 --x86 --lzma2 >/boot/initramfs-debian-sources-x86_64-6.1.4_p1

Include this parameter on your kernel line into /etc/boot.conf:

module.sig_enforce=1

Example /etc/boot.conf enabling kernel module signature verification:

   /etc/boot.conf - module.sig_enforce boot.conf
"Funtoo Linux genkernel signing enforced" {
	kernel kernel[-v]
	initrd initramfs[-v]
	params += real_root=auto rootfstype=auto module.sig_enforce=1
}

Update configuration file that GRUB will use for booting:

root # ego boot update

ready to shim

Now that our system has a signed kernel and modules, we can load them up for secure boot using the fedora shim.