Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Module signing and secure boot
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
sunox
Tux's lil' helper
Tux's lil' helper


Joined: 26 Jan 2022
Posts: 136

PostPosted: Mon Dec 25, 2023 11:22 pm    Post subject: Module signing and secure boot Reply with quote

I have secure boot set up and working with a signed kernel, and I'm just wondering what is the standard way of signing kernel modules in a secure boot environment. I'm reading various things and getting various answers and it's a bit confusing.

On the Gentoo Secure Boot page (https://wiki.gentoo.org/wiki/Secure_Boot#USE_flags) it says this:

Code:
In addition to the kernel itself, the kernel modules must also be signed to boot successfully with Secure Boot enabled. For this purpose the modules-sign global use flag can be used in addition to the MODULES_SIGN_KEY and MODULES_SIGN_HASH environment variables.


I sign my kernel using a key and cert from the db.

Code:
sbsign --key db.key --cert db.crt --output /boot/EFI/gentoo-efi-3/vmlinuz.efi /boot/EFI//gentoo-efi-1/vmlinuz.efi


Can I just use db/key for MODULES_SIGN_KEY? What about the cert? What hash should I specify?

Interestingly, neither of these environment variables are mentioned on this page: https://wiki.gentoo.org/wiki/Signed_kernel_module_support. And on that page secure boot is only mentioned once in passing.

The kernel seems to have module signing support, and you achieve this by setting kernel options and optionally putting keys in the source directory. It seems like this might break the secure boot chain though, but maybe this isn't so important.

Any guidance is appreciated.

Oh and Merry Christmas / Happy Holidays :)
Back to top
View user's profile Send private message
pietinger
Moderator
Moderator


Joined: 17 Oct 2006
Posts: 4169
Location: Bavaria

PostPosted: Tue Dec 26, 2023 1:19 am    Post subject: Re: Module signing and secure boot Reply with quote

sunox wrote:
Can I just use db/key for MODULES_SIGN_KEY? What about the cert? What hash should I specify?

You can ... and I suggest to do so. The hash depends how you had built your ...
sunox wrote:
I sign my kernel using a key and cert from the db.


I am a lazy guy and used this script for creating my keys:
http://www.rodsbooks.com/efi-bootloaders/controlling-sb.html
(I changed only the period of validity of the keys from 3650 days to 9999 days)

So I have used:
Quote:
openssl req -new -x509 -newkey rsa:2048 -subj "/CN=$NAME DB/" -keyout DB.key \
-out DB.crt -days 9999 -nodes -sha256

=> sha256 ... ;-)

Just check your creation of your keys.

sunox wrote:
Oh and Merry Christmas / Happy Holidays :)

Thanks. Same wishes back to you
_________________
https://wiki.gentoo.org/wiki/User:Pietinger
Back to top
View user's profile Send private message
pietinger
Moderator
Moderator


Joined: 17 Oct 2006
Posts: 4169
Location: Bavaria

PostPosted: Tue Dec 26, 2023 1:31 am    Post subject: Re: Module signing and secure boot Reply with quote

Sorry ... I forgot this:
sunox wrote:
The kernel seems to have module signing support, and you achieve this by setting kernel options and optionally putting keys in the source directory. It seems like this might break the secure boot chain though, but maybe this isn't so important.

No, it is important.

Your keys should be on one (of two) USB sticks (I mount it to /mnt/stick) ... you can configure the directory of the keys:
Code:
-*- Cryptographic API  --->
    Certificates for signature checking  --->
        (certs/signing_key.pem) File name or PKCS#11 URI of module signing key


See more: /usr/src/linux/Documentation/admin-guide/module-signing.rst

I am using SecureBoot too, but no module signing ... because I have no module support in my kernel :lol:
_________________
https://wiki.gentoo.org/wiki/User:Pietinger
Back to top
View user's profile Send private message
sunox
Tux's lil' helper
Tux's lil' helper


Joined: 26 Jan 2022
Posts: 136

PostPosted: Tue Dec 26, 2023 2:55 am    Post subject: Reply with quote

Thanks! I'll give this a try tomorrow.

I'm just wondering how exactly MODULES_SIGN_KEY is different from the "signing key" asked for in the kernel. Is it fair to say that MODULES_SIGN_KEY is where you put the private key (which does the signing) and "File name of the module signing key" where you put the certificate containing the public key (which does the verifying)? The confusing thing is that it asks for a "signing" key under the subheader "Certificates for signature checking".

Also confusing is that the kernel lets you specify the hash you want to use (e.g. CONFIG_MODULE_SIG_SHA256). Is this redundant with MODULES_SIGN_HASH?

And these two are just used for signing (and verifying?) packages (not including the kernel)?

Code:

SECUREBOOT_SIGN_KEY="..."
SECUREBOOT_SIGN_CERT="..."
Back to top
View user's profile Send private message
sunox
Tux's lil' helper
Tux's lil' helper


Joined: 26 Jan 2022
Posts: 136

PostPosted: Tue Dec 26, 2023 3:39 am    Post subject: Reply with quote

According to the reference below the MODULES_SIGN* variables all work with the modules-sign USE flag. This USE flag seems to only pertain to distribution kernels, and maybe external modules? So maybe not relevant in my case? (I build from gentoo-sources and I don't think I have any "external" kernels. None of my packages have the modules-sign USE flag)

https://devmanual.gentoo.org/eclass-reference/kernel-build.eclass/index.html

According to the next reference the SECUREBOOT_SIGN* variables work with any packages that have the "secureboot" USE flag. For me this is only "systemd-utils".

https://wiki.gentoo.org/wiki/Secure_Boot

Then I guess the kernel options mentioned above are what do the signing and verifying of the (non-external?) modules.

I'm just seeing your reference to the module-signing.rst doc which looks good. I'll check that tomorrow. Thanks.
Back to top
View user's profile Send private message
pietinger
Moderator
Moderator


Joined: 17 Oct 2006
Posts: 4169
Location: Bavaria

PostPosted: Tue Dec 26, 2023 2:52 pm    Post subject: Reply with quote

Maybe I am wrong here ... then I would like to hear a protest ... but AFAIK all the automatic stuff Gentoo provides for Signing/SecureBoot is only for our dist-kernel (and genkernel?) but not for a manual kernel configuration approach. So, just look into all kernel documentattions and forget the settings in make.conf ... ;-)

As said before I dont sign my external kernel modules because I dont have a module support in my kernel (monolithic kernel). For SecureBoot I did this (english post):
https://forums.gentoo.org/viewtopic-p-8492354.html#8492354

And yes, I have used these keys also for another solution: IMA
(It is described in this german post: https://forums.gentoo.org/viewtopic-t-1158503.html)
_________________
https://wiki.gentoo.org/wiki/User:Pietinger


Last edited by pietinger on Tue Dec 26, 2023 3:23 pm; edited 1 time in total
Back to top
View user's profile Send private message
pietinger
Moderator
Moderator


Joined: 17 Oct 2006
Posts: 4169
Location: Bavaria

PostPosted: Tue Dec 26, 2023 3:15 pm    Post subject: Reply with quote

Maybe another word to asymetric encryption which is used for signing ... maybe you already know, but maybe interesting for other readers ...

For asymetric encryption you will need a key pair: Two keys which are connected; with one of them you can encrypt something, with the other you can decrypt. You can use this for encryption ... and signing ... it is the same ... only with a changed use of the keys. So I explain shortly asymetric encryption:

You want to send a encrypted mail to a friend. Then you need one key of your friend (called "open key"). You encrypt with this "open key" and only your friend can decrypt it with his second key == the "private key" or "secret key".

Signing is the same the other way round:

You want sign the mail you sent to your friend so it shows that it is really from you:

You encrypt it with YOUR private key (only you have) and your friend can decrypt it with the open key from you (everybody have). Because only you was able to encrypt it with your private key, everybody can check with the well-known open key from you that really you have encrypted it. When we talk about signing then almost everywhere this "open key" is called "certificate". Now what I told is not really true. Not the whole mail was decrypted for signing it. ONLY a HASH was build from your mail ... and ONLY this HASH was encrypted ... So, your friend decrypts the hash of your mail and check if it is the same hash he builds from your mail.

So, for signing you need a secret private key (here in format PEM === Base64) and a "certificate" (here in format DER == ASN.1). The certificate - everybody knows (compiled into the kernel) - is used for verifying if your kernel modules (*) are really encrypted with your secret private key (on your USB stick).

(*) As said before: Not the kernel module itself is encrypted - only the hash of this kernel module is encrypted == signed.
_________________
https://wiki.gentoo.org/wiki/User:Pietinger
Back to top
View user's profile Send private message
sunox
Tux's lil' helper
Tux's lil' helper


Joined: 26 Jan 2022
Posts: 136

PostPosted: Tue Dec 26, 2023 4:03 pm    Post subject: Reply with quote

Nice explanation, thanks.

The kernel's MODULE_SIG_KEY wants the signing key and the corresponding certificate in the same file - both in PEM form. I just concatenated my key and cert together so it looked like this:

Code:
-----BEGIN PRIVATE KEY-----
...
-----END PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----


and everything went well.
Back to top
View user's profile Send private message
sunox
Tux's lil' helper
Tux's lil' helper


Joined: 26 Jan 2022
Posts: 136

PostPosted: Tue Dec 26, 2023 4:07 pm    Post subject: Reply with quote

This is off-topic, but I would like to shed some of my installed modules if possible. Is there a good way of telling if a module is ever used or not? Maybe checking loaded modules (via lsmod) against the contents of /lib/modules?
Back to top
View user's profile Send private message
pietinger
Moderator
Moderator


Joined: 17 Oct 2006
Posts: 4169
Location: Bavaria

PostPosted: Tue Dec 26, 2023 4:11 pm    Post subject: Reply with quote

sunox wrote:
[...] Maybe checking loaded modules (via lsmod) against the contents of /lib/modules?

Yes. 8)

(Use your CD-Rom device before ... :lol: )
_________________
https://wiki.gentoo.org/wiki/User:Pietinger
Back to top
View user's profile Send private message
sunox
Tux's lil' helper
Tux's lil' helper


Joined: 26 Jan 2022
Posts: 136

PostPosted: Tue Dec 26, 2023 5:34 pm    Post subject: Reply with quote

Thanks. First thing I did was somehow exclude a codepage FAT needed to mount, which is what my boot partition is formatted as. Fun stuff.

Feels good to be secure booted. I guess the last thing I should do is find a better way of storing my keys.
Back to top
View user's profile Send private message
pwnenuser
n00b
n00b


Joined: 26 Mar 2024
Posts: 4

PostPosted: Tue Apr 02, 2024 5:41 pm    Post subject: Reply with quote

i have a similar kind of situation, i am using secure boot setup with gentoo-kernel-bin and having nvidia graphics and without signing modules i cant use nvidia drivers

[https://wiki.gentoo.org/wiki/NVIDIA/nvidia-drivers#Kernel_module_signing

according to wiki but dist-kernel does not provide "signing_key.pem" keys

and if i am using my own created keys in MODULES_SIGN_KEY with own created MODULES_SIGN_CERT it has no effect even i tried to sign manully it didnt worked
while reading wiki
[url] https://wiki.gentoo.org/wiki/Signed_kernel_module_support [/url]
Code:
 
Warning
When using the pre-built distribution kernels (e.g. sys-kernel/gentoo-kernel-bin) the key that was used to sign the in-tree modules will not be available for signing out-of-tree modules. If module signatures are enforced on these kernels, any out-of-tree modules must be signed with a different key. The public certificate for these out-of-tree modules must be loaded into the kernel key-chain, keyctl may be used for this purpose but is not persistent across reboots. To persistently add additional certificates to the keychain Shim must be used.


so i used shim with my own created keys but it didnt worked
btw i am using uki with systemd boot

what should i do? i dont know which part i am doing wrong
Back to top
View user's profile Send private message
AndrewAmmerlaan
Developer
Developer


Joined: 25 Jun 2014
Posts: 266
Location: Nijmegen

PostPosted: Fri Apr 19, 2024 9:29 pm    Post subject: Reply with quote

> so i used shim with my own created keys but it didnt worked

What about shim did not work? Note you'll have to adjust your firmware to load shim instead of sd-boot, and then have shim load sd-boot by naming it grubx64.efi and putting it in the same folder as shim (yes sd-boot has to be named as if it is grub).
_________________
OS: Gentoo 6.7.3-gentoo-dist, ~amd64, 17.1/desktop/plasma/systemd/merged-usr
MB: MSI Z370-A PRO
CPU: Intel Core i9-9900KS
GPU: Intel Arc A770 16GB & Intel UHD Graphics 630
SSD: Samsung 970 EVO Plus 2 TB
RAM: Crucial Ballistix 32GB DDR4-2400
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum