VirtualBox

Changeset 96679 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Sep 9, 2022 2:19:56 PM (2 years ago)
Author:
vboxsync
Message:

Linux Host: vboxdrv.sh: Adjust Secure Boot related part of the script, bugref:10287.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Installer/linux/vboxdrv.sh

    r96407 r96679  
    118118esac
    119119
     120# Try to find a tool for modules signing.
     121SIGN_TOOL=$(which kmodsign 2>/dev/null)
     122# Attempt to use in-kernel signing tool if kmodsign not found.
     123if test -z "$SIGN_TOOL"; then
     124    if test -x "/lib/modules/$KERN_VER/build/scripts/sign-file"; then
     125        SIGN_TOOL="/lib/modules/$KERN_VER/build/scripts/sign-file"
     126    fi
     127fi
     128
     129if type update-secureboot-policy >/dev/null 2>&1; then
     130    HAVE_UPDATE_SECUREBOOT_POLICY_TOOL=true
     131fi
     132
    120133[ -r /etc/default/virtualbox ] && . /etc/default/virtualbox
    121134
     
    311324}
    312325
     326# Reads CONFIG_MODULE_SIG_HASH from kernel config.
     327kernel_module_sig_hash()
     328{
     329    /lib/modules/"$KERN_VER"/build/scripts/config \
     330        --file /lib/modules/"$KERN_VER"/build/.config \
     331        --state CONFIG_MODULE_SIG_HASH 2>/dev/null
     332}
     333
     334# Returns "1" if kernel module signature hash algorithm
     335# is supported by us. Or empty string otherwise.
     336module_sig_hash_supported()
     337{
     338    sig_hashalgo="$1"
     339    [ -n "$sig_hashalgo" ] || return
     340
     341    # Go through supported list.
     342    [    "$sig_hashalgo" = "sha1"   \
     343      -o "$sig_hashalgo" = "sha224" \
     344      -o "$sig_hashalgo" = "sha256" \
     345      -o "$sig_hashalgo" = "sha384" \
     346      -o "$sig_hashalgo" = "sha512" ] || return
     347
     348    echo "1"
     349}
     350
     351# Returns "1" if module is signed and signature can be verified
     352# with public key provided in DEB_PUB_KEY. Or empty string otherwise.
     353module_signed()
     354{
     355    mod="$1"
     356    [ -n "$mod" ] || return
     357
     358    extraction_tool=/lib/modules/"$(uname -r)"/build/scripts/extract-module-sig.pl
     359    mod_path=$(module_path "$mod" 2>/dev/null)
     360    openssl_tool=$(which openssl 2>/dev/null)
     361    # Do not use built-in printf!
     362    printf_tool=$(which printf 2>/dev/null)
     363
     364    # Make sure all the tools required for signature validation are available.
     365    [ -x "$extraction_tool" ] || return
     366    [ -n "$mod_path"        ] || return
     367    [ -n "$openssl_tool"    ] || return
     368    [ -n "$printf_tool"     ] || return
     369
     370    # Make sure openssl can handle hash algorithm.
     371    sig_hashalgo=$(modinfo -F sig_hashalgo vboxdrv 2>/dev/null)
     372    [ "$(module_sig_hash_supported $sig_hashalgo)" = "1" ] || return
     373
     374    # Generate file names for temporary stuff.
     375    mod_pub_key=$(mktemp -u)
     376    mod_signature=$(mktemp -u)
     377    mod_unsigned=$(mktemp -u)
     378
     379    # Convert public key in DER format into X509 certificate form.
     380    "$openssl_tool" x509 -pubkey -inform DER -in "$DEB_PUB_KEY" -out "$mod_pub_key" 2>/dev/null
     381    # Extract raw module signature and convert it into binary format.
     382    "$printf_tool" \\x$(modinfo -F signature "$mod" | sed -z 's/[ \t\n]//g' | sed -e "s/:/\\\x/g") 2>/dev/null > "$mod_signature"
     383    # Extract unsigned module for further digest calculation.
     384    "$extraction_tool" -0 "$mod_path" 2>/dev/null > "$mod_unsigned"
     385
     386    # Verify signature.
     387    rc=""
     388    "$openssl_tool" dgst "-$sig_hashalgo" -binary -verify "$mod_pub_key" -signature "$mod_signature" "$mod_unsigned" 2>&1 >/dev/null && rc="1"
     389    # Clean up.
     390    rm -f $mod_pub_key $mod_signature $mod_unsigned
     391
     392    # Check result.
     393    [ "$rc" = "1" ] || return
     394
     395    echo "1"
     396}
     397
    313398# Returns "1" if externally built module is available in the system and its
    314399# version and revision number do match to current VirtualBox installation.
     
    339424    mod_dir="$(dirname "$mod_path" | sed 's;^.*/;;')"
    340425    [ "$mod_dir" = "misc" ] || return
     426
     427    # In case if system is running in Secure Boot mode, check if module is signed.
     428    if test -n "$HAVE_SEC_BOOT"; then
     429        [ "$(module_signed "$mod")" = "1" ] || return
     430    fi
    341431
    342432    echo "1"
     
    595685    sync
    596686    succ_msg "VirtualBox kernel modules built"
    597     # Secure boot on Ubuntu and Debian.
    598     if test -n "$HAVE_SEC_BOOT" &&
    599         type update-secureboot-policy >/dev/null 2>&1; then
    600         SHIM_NOTRIGGER=y update-secureboot-policy --new-key
    601     fi
    602     if test -f "$DEB_PUB_KEY" && test -f "$DEB_PRIV_KEY"; then
    603         HAVE_DEB_KEY=true
     687
     688    # Secure boot on Ubuntu, Debian and Oracle Linux.
     689    if test -n "$HAVE_SEC_BOOT"; then
     690        begin_msg "Signing VirtualBox kernel modules" console
     691
     692        # Generate new signing key if needed.
     693        [ -n "$HAVE_UPDATE_SECUREBOOT_POLICY_TOOL" ] && SHIM_NOTRIGGER=y update-secureboot-policy --new-key
     694
     695        # Check if signing keys are in place.
     696        if test ! -f "$DEB_PUB_KEY" || ! test -f "$DEB_PRIV_KEY"; then
     697            # update-secureboot-policy tool present in the system, but keys were not generated.
     698            [ -n "$HAVE_UPDATE_SECUREBOOT_POLICY_TOOL" ] && failure "Unable to find signing keys, aborting"
     699            # update-secureboot-policy not present in the system, recommend generate keys manually.
     700            failure "
     701
     702System is running in Secure Boot mode, however your distribution
     703does not provide tools for automatic generation of keys needed for
     704modules signing. Please consider to generate and enroll them manually:
     705
     706    sudo mkdir -p /var/lib/shim-signed/mok
     707    sudo openssl req -nodes -new -x509 -newkey rsa:2048 -outform DER -keyout $DEB_PRIV_KEY -out $DEB_PUB_KEY
     708    sudo sudo mokutil --import $DEB_PUB_KEY
     709    sudo reboot
     710
     711Restart \"rcvboxdrv setup\" after system is rebooted.
     712"
     713        fi
     714
     715        # Check if signing tool is available.
     716        [ -n "$SIGN_TOOL" ] || failure "Unable to find signing tool"
     717
     718        # Get kernel signature hash algorithm from kernel config and validate it.
     719        sig_hashalgo=$(kernel_module_sig_hash)
     720        [ "$(module_sig_hash_supported $sig_hashalgo)" = "1" ] \
     721            || failure "Unsupported kernel signature hash algorithm $sig_hashalgo"
     722
     723        # Sign modules.
    604724        for i in $MODULE_LIST; do
    605             kmodsign sha512 /var/lib/shim-signed/mok/MOK.priv \
    606                 /var/lib/shim-signed/mok/MOK.der \
    607                 /lib/modules/"$KERN_VER"/misc/"$i".ko
     725            "$SIGN_TOOL" "$sig_hashalgo" "$DEB_PRIV_KEY" "$DEB_PUB_KEY" \
     726                /lib/modules/"$KERN_VER"/misc/"$i".ko 2>/dev/null || failure "Unable to sign $i.ko"
    608727        done
    609         # update-secureboot-policy "expects" DKMS modules.
    610         # Work around this and talk to the authors as soon
    611         # as possible to fix it.
    612         mkdir -p /var/lib/dkms/vbox-temp
    613         update-secureboot-policy --enroll-key 2>/dev/null ||
    614             begin_msg "Failed to enroll secure boot key." console
    615         rmdir -p /var/lib/dkms/vbox-temp 2>/dev/null
     728
     729        # Enroll signing key if needed.
     730        if test -n "$HAVE_UPDATE_SECUREBOOT_POLICY_TOOL"; then
     731            # update-secureboot-policy "expects" DKMS modules.
     732            # Work around this and talk to the authors as soon
     733            # as possible to fix it.
     734            mkdir -p /var/lib/dkms/vbox-temp
     735            update-secureboot-policy --enroll-key 2>/dev/null ||
     736                begin_msg "Failed to enroll secure boot key." console
     737            rmdir -p /var/lib/dkms/vbox-temp 2>/dev/null
     738
     739            # Indicate that key has been enrolled and reboot is needed.
     740            HAVE_DEB_KEY=true
     741        fi
     742        succ_msg "Signing completed"
    616743    fi
    617744}
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette