Nếu hệ thống của bạn đang hỗ trợ EFI Secure Boot thì dù cho bạn cài đặt các kernel module bằng apt thì vẫn cần phải “sign” chúng để có thể sử dụng.
Tạo một 2048 bit RSA certificate (MOK.der, và private key là MOK.priv) trong thư mục /root/module-signing, dùng cho việc sign kernel module. Ta sẽ để expiry date của certificate này hẳn 10 năm (3650 ngày).
$ su - $ mkdir -p /root/module-signing $ cd /root/module-signing $ openssl req \ -new -x509 -newkey rsa:2048 -keyout MOK.priv -outform DER -out MOK.der -nodes \ -days 36500 \ -subj "/CN=$(hostname -s | cut -b1-31) Secure Boot Module Signature key/" $ chmod 600 MOK.priv
Sử dụng công cụ mokutil
để đăng ký key pair vừa tạo với Secure Boot.
$ sudo mokutil --import /root/module-signing/MOK.der input password: input password again:
Nhập mật khẩu sẽ được dùng để xác thực trong quá trình cập nhật Secure Boot.
Sau đó hãy khởi động lại máy.
Màn hình khởi động của Bootloader sẽ hiển thị và yêu cầu ta ấn một phím bất kỳ để tiếp tục vào màn hình MOK Manager. Chọn Enroll MOK
trong menu, rồi chọn Yes
. Tiếp đó hệ thống sẽ yêu cầu mật khẩu bạn đã nhập ở trên.
Sau khi nhập mật khẩu chính xác, key pair bạn tạo sẽ được đăng ký với Secure Boot.
Lấy ví dụ điển hình là các kernel module của VirtualBox. Sau khi khởi động lại, ta tiến hành ‘sign’ các kernel module của VirtualBox bằng đoạn lệnh sau. Lưu ý là ta có thể chọn hash algorithm là hash256 hoặc hash512 (mạnh hơn nhưng ít phổ biến hơn):
for modfile in $(dirname $(modinfo -n vboxdrv))/*.ko; do echo "Signing ${modfile}" sudo kmodsign sha256 \ /root/module-signing/MOK.priv \ /root/module-signing/MOK.der \ "${modfile}" done
Shellscript
/usr/local/bin/sign_ko.sh
#!/bin/bash # # sign_ko.sh - Sign kernel modules for secure boot. # set -eo pipefail shopt -s nullglob HASHALGO=sha512 OPENSSL=/usr/bin/openssl MOKDIR=/var/lib/shim-signed/mok/ MOKUTIL=/usr/bin/mokutil MODINFO=/sbin/modinfo KMODSIGN=/usr/bin/kmodsign KVER=$(uname -r) usage() { echo "Usage: $(basename "${0}") [-h] [-r <rev>] <modules...>" echo echo " Signs all given kernel modules." echo " If no revision is specified with -r, the revision" echo " ${KVER} will be processed." exit 1 } while getopts hr: OPTION; do case ${OPTION} in r) KVER=${OPTARG} ;; h) usage >&2 ;; *) echo "Unknown option: ${OPTION}!" >&2 exit 1 ;; esac done shift $(( OPTIND - 1 )) if [ $# -eq 0 ]; then echo No kernel module was specified. Exiting. >&2 exit 1 fi if [ ! -d "/lib/modules/${KVER}" ]; then echo Revision ${KVER} not found. Exiting. >&2 exit 1 fi if [ ! -x ${KMODSIGN} ]; then # Detect an alternative kmodsign tool KMODSIGN=/usr/src/linux-headers-$(uname -r)/scripts/sign-file if [ ! -x ${KMODSIGN} ]; then echo "No usable 'kmodsign' binary found - aborting" >&2 exit 1 fi echo "KMODSIGN=${KMODSIGN}" fi gen_x509_keys() { mkdir -p ${MOKDIR} cd ${MOKDIR} ${OPENSSL} req -new -x509 -newkey rsa:2048 \ -keyout MOK.priv -outform DER -out MOK.der \ -nodes -days 36500 \ -subj "/CN=$(hostname -s | cut -b1-31) Secure Boot Module Signature key/" chmod 600 MOK.priv ${MOKUTIL} --import MOK.der reboot } get_x509_subject() { ${OPENSSL} x509 -in ${1} -subject -noout | sed 's/subject=CN = //' } get_ko_path() { ${MODINFO} -n ${1} 2>/dev/null } get_ko_signer() { ${MODINFO} ${1} | grep signer: | sed 's/^signer:[ ]\+//' } SUBJECT=$(get_x509_subject ${MOKDIR}/MOK.der) for module in "$@"; do name=$(basename ${module} .ko) kobj=$(get_ko_path ${module}) if [ $? -eq 0 ]; then SIGNER=$(get_ko_signer ${kobj}) if [ "${SIGNER}" = "${SUBJECT}" ]; then echo Module "'${name}'" has been signed with a correct certificate continue fi if [ -n "${SIGNER}" ]; then echo Module "'${name}'" has been signed by ${SIGNER} - will be overwritten fi if [ -z "${KBUILD_SIGN_PIN}" ]; then read -rs -p "Enter MOK passphrase: " KBUILD_SIGN_PIN export KBUILD_SIGN_PIN echo fi ${KMODSIGN} ${HASHALGO} ${MOKDIR}/MOK.priv ${MOKDIR}/MOK.der ${kobj} echo Successfully signed module "'${name}'" else echo Module "'${name}'" not found! >&2 fi done
Cấp quyền executable
cho script.
sudo chmod +x /usr/local/bin/sign_ko.sh
Thực thi script trên bằng sudo
.
sudo sign_ko.sh -r 6.11.0-21-generic vboxdrv vboxnetadp vboxnetflt