grub2-signing-extension/README.md

177 lines
6.2 KiB
Markdown
Raw Normal View History

2015-03-16 19:38:36 +00:00
# grub2-signing-extension for GRUB2
2015-03-17 06:23:23 +00:00
GRUB2 has got a function called "check\_signatures" which automatically checks if your GRUB2 files are signed and have a good signature. If the files aren't signed or have a bad signature GRUB2 won't run them to prevent running malicious software.
2015-03-16 19:38:36 +00:00
The GRUB2 signing extension are some scripts which helps you to verify, sign and unsign your GRUB2 bootloader files using gpg.
## Requirements
You need
* GRUB2 ( sys-boot/grub:2 )
2018-01-12 20:56:23 +00:00
* GNUpg >= 2.1 ( >= app-crypt/gnupg-2.1 )
2015-03-16 19:38:36 +00:00
## Preparation
Before you can use the signing and verification feature you need to generate a keypair as root. Please use a secure passphrase.
`# gpg --gen-key`
To make gpg able to sign and verify files in a `su` environment we need to activate the gpg-agent for root.
Edit the file _/root/.gnupg/gpg.conf_ and add the line `use-agent`.
Save the file and create _/root/.gnupg/gpg-agent.conf_ with the following content
pinentry-program /usr/bin/pinentry-curses
no-grab
default-cache-ttl 1800
## How to install the GRUB2 check\_signatures feature and using the grub2-signing-extension
First, export your public key.
`# gpg --export -o ~/pubkey`
2015-03-17 07:12:04 +00:00
Next step, `mount /boot` and (re)install GRUB2. You need to install the public key into the core and instruct to load the modules `gcry_sha256` `gcry_dsa` and `gcry_rsa` at start. So you need the following arguments to install it this way
2015-03-16 19:38:36 +00:00
`grub-install /dev/sda -k /root/pubkey --modules="gcry_sha256 gcry_dsa gcry_rsa"`
2015-03-16 19:38:36 +00:00
Now download the grub2-signing-extension and run `make install` as root. You will now have `grub2-sign`, `grub2-unsign`, `grub2-verify` and `grub2-update-kernel-signature` as runable scripts.
2015-03-16 19:38:36 +00:00
To _enable_ GRUB2's check\_signatures feature insert the following content at the end of the file of */etc/grub.d/00_header*
cat << EOF
set check_signatures=enforce
EOF
Run `grub-mkconfig -o /boot/grub/grub.cfg` to make the new configuration valid.
2015-03-16 19:38:36 +00:00
Now the time is come to sign your GRUB2 bootloader. Just run `grub2-sign`, enter your passphrase and that's it.
2018-03-22 12:55:28 +00:00
**It's also recommended to install a password in GRUB2! [See ADDENDUM]**
2015-03-16 19:38:36 +00:00
2015-03-16 20:43:11 +00:00
## How to update the signatures on change
On every change at the GRUB2 core files you need to run `grub2-unsign` first before you make your changes. Please notice, if you reinstall GRUB2, you should do it as it is said above. Otherwise the signature check won't work.
If you do some changes or updates for the kernel or initramfs, you may want to use `grub2-update-kernel-signature` instead.
2015-03-16 20:43:11 +00:00
## Files
If you didn't read the instruction above here is what the scripts does:
* `grub2-sign` is signing the bootloader files with root's keypair.
* `grub2-unsign` is removing the signatures of the bootloader files.
* `grub2-verify` is checking if your signatures are good. If not, you will see which signature is bad.
* `grub2-update-kernel-signature` is renewing the signatures in /boot/. (without subdirs) regardless if grub2-verify fails.
2015-03-16 20:43:11 +00:00
2018-03-22 12:47:16 +00:00
## Exit codes
You might be interested in the exit codes of `grub2-verify` to use it in your monitoring tools:
```
0 - Everything is okay
1 - Found bad signatures
2 - No signatures found at all [GRUB2 is completely unsigned]
3 - Missing signatures [There are correct signatures but some files are unsigned]
```
2015-03-16 20:43:11 +00:00
## Troubleshooting
2018-03-22 13:50:46 +00:00
### I receive an error
#### gpg: signing failed: Permission denied
2018-03-22 13:48:38 +00:00
2018-03-22 13:50:46 +00:00
Make sure that the tty you are in belongs to you (root).
Do: `chown root:root $(tty)`
2018-03-22 13:48:38 +00:00
2015-03-16 20:43:11 +00:00
### I forgot to run grub2-unsign before I made changes. What now?
Run `grub2-verify` to see, which signature is bad. Remove the signature and run `grub2-unsign`, after this `grub2-sign`.
Alternatively, if you just updated your kernel/initramfs, run `grub2-update-kernel-signatures`.
2015-03-16 20:43:11 +00:00
### How can I switch off GRUB2's check\_signature feature?
2015-03-16 20:43:11 +00:00
Open */etc/grub.d/00_header* and remove the part
cat << EOF
set check_signatures=enforce
EOF
2018-01-18 00:00:14 +00:00
Run `grub2-unsign` and `grub-mkconfig -o /boot/grub/grub.cfg`.
2015-03-16 20:43:11 +00:00
Also you should reinstall grub2, using something like `grub-install /dev/sda`.
2015-03-16 20:43:11 +00:00
### Suddenly I can't boot! This is YOUR FAULT!
No. An important signature is bad. So GRUB2 didn't run this part of code/configuration/kernel/whatever.
You could do a chroot using an USB dongle with a GNU/Linux distribution on it. If you're chrooted to your system run `grub2-verify`.
If you think this happened through an update shortly done by you, you may want to run `gpg-agent --daemon ; grub2-update-kernel-signatures`.
2015-03-16 20:43:11 +00:00
### Okay, I really got some bad signatures not caused by me. What do I do now?
2015-03-16 20:43:11 +00:00
Check your system thoroughly. Check it about malicious software. Check it about malicious connections. CHECK EVERYTHING.
2015-03-16 20:43:11 +00:00
# ADDENDUM
2015-03-16 19:38:36 +00:00
## How to install a GRUB2 password
2018-01-18 00:00:14 +00:00
Run `grub-mkpasswd-pbkdf2` and type a password. Please take care because in the GRUB2 standard installation the keyboard layout is set to en\_US.
2015-03-16 19:38:36 +00:00
Copy the content of *grub.pbkdf2.[...]* to your clipboard. Open the file */etc/grub.d/00_header* and insert this at the end of the file
cat << EOF
set superusers="yourUsername"
export superusers
password_pbkdf2 yourUsername grub.pbkdf2.[...this string from the clipboard...]
EOF
2015-03-16 20:43:11 +00:00
To boot GNU/Linux automatically and without authentication open */etc/grub.d/10_linux* and change the following lines like this
2015-03-16 19:38:36 +00:00
echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-$version-$type-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
else
echo "menuentry '$(echo "$os" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-simple-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
fi
to
echo "menuentry '$(echo "$title" | grub_quote)' --unrestricted ${CLASS} \$menuentry_id_option 'gnulinux-$version-$type-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
else
echo "menuentry '$(echo "$os" | grub_quote)' --unrestricted ${CLASS} \$menuentry_id_option 'gnulinux-simple-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
fi
The important changing is the flag *--unrestricted*.
2015-03-16 19:42:56 +00:00
Run `grub2-unsign` to unsign the bootloader.
2018-01-18 00:00:14 +00:00
Then run `grub-mkconfig -o /boot/grub/grub.cfg` to write the new config.
2015-03-16 19:42:56 +00:00
After this run `grub2-sign` again to sign the new changings.
2015-03-16 20:43:11 +00:00