VirtualBox

Changeset 96680 in vbox


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

Linux Additions: vboxadd.sh: Add support for Secure Boot, bugref:10287.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/linux/installer/vboxadd.sh

    r96407 r96680  
    289289}
    290290
     291# Secure boot state.
     292case "`mokutil --sb-state 2>/dev/null`" in
     293    *"disabled in shim"*) unset HAVE_SEC_BOOT;;
     294    *"SecureBoot enabled"*) HAVE_SEC_BOOT=true;;
     295    *) unset HAVE_SEC_BOOT;;
     296esac
     297# So far we can only sign modules on Ubuntu and on Debian 10 and later.
     298DEB_PUB_KEY=/var/lib/shim-signed/mok/MOK.der
     299DEB_PRIV_KEY=/var/lib/shim-signed/mok/MOK.priv
     300# Check if key already enrolled.
     301unset HAVE_DEB_KEY
     302case "`mokutil --test-key "$DEB_PUB_KEY" 2>/dev/null`" in
     303    *"is already"*) DEB_KEY_ENROLLED=true;;
     304    *) unset DEB_KEY_ENROLLED;;
     305esac
     306
     307# Try to find a tool for modules signing.
     308SIGN_TOOL=$(which kmodsign 2>/dev/null)
     309# Attempt to use in-kernel signing tool if kmodsign not found.
     310if test -z "$SIGN_TOOL"; then
     311    if test -x "/lib/modules/$KERN_VER/build/scripts/sign-file"; then
     312        SIGN_TOOL="/lib/modules/$KERN_VER/build/scripts/sign-file"
     313    fi
     314fi
     315
     316if type update-secureboot-policy >/dev/null 2>&1; then
     317    HAVE_UPDATE_SECUREBOOT_POLICY_TOOL=true
     318fi
     319
     320# Reads CONFIG_MODULE_SIG_HASH from kernel config.
     321kernel_module_sig_hash()
     322{
     323    /lib/modules/"$KERN_VER"/build/scripts/config \
     324        --file /lib/modules/"$KERN_VER"/build/.config \
     325        --state CONFIG_MODULE_SIG_HASH 2>/dev/null
     326}
     327
     328# Returns "1" if kernel module signature hash algorithm
     329# is supported by us. Or empty string otherwise.
     330module_sig_hash_supported()
     331{
     332    sig_hashalgo="$1"
     333    [ -n "$sig_hashalgo" ] || return
     334
     335    # Go through supported list.
     336    [    "$sig_hashalgo" = "sha1"   \
     337      -o "$sig_hashalgo" = "sha224" \
     338      -o "$sig_hashalgo" = "sha256" \
     339      -o "$sig_hashalgo" = "sha384" \
     340      -o "$sig_hashalgo" = "sha512" ] || return
     341
     342    echo "1"
     343}
     344
     345sign_modules()
     346{
     347    KERN_VER="$1"
     348    test -n "$KERN_VER" || return 1
     349
     350    # Make list of mudules to sign.
     351    MODULE_LIST="vboxguest vboxsf"
     352    # vboxvideo might not present on for older kernels.
     353    [ -f "/lib/modules/"$KERN_VER"/misc/vboxvideo.ko" ] && MODULE_LIST="$MODULE_LIST vboxvideo"
     354
     355    # Secure boot on Ubuntu, Debian and Oracle Linux.
     356    if test -n "$HAVE_SEC_BOOT"; then
     357        begin "Signing VirtualBox Guest Additions kernel modules"
     358
     359        # Generate new signing key if needed.
     360        [ -n "$HAVE_UPDATE_SECUREBOOT_POLICY_TOOL" ] && SHIM_NOTRIGGER=y update-secureboot-policy --new-key
     361
     362        # Check if signing keys are in place.
     363        if test ! -f "$DEB_PUB_KEY" || ! test -f "$DEB_PRIV_KEY"; then
     364            # update-secureboot-policy tool present in the system, but keys were not generated.
     365            [ -n "$HAVE_UPDATE_SECUREBOOT_POLICY_TOOL" ] && failure "Unable to find signing keys, aborting"
     366            # update-secureboot-policy not present in the system, recommend generate keys manually.
     367            fail "
     368
     369System is running in Secure Boot mode, however your distribution
     370does not provide tools for automatic generation of keys needed for
     371modules signing. Please consider to generate and enroll them manually:
     372
     373    sudo mkdir -p /var/lib/shim-signed/mok
     374    sudo openssl req -nodes -new -x509 -newkey rsa:2048 -outform DER -keyout $DEB_PRIV_KEY -out $DEB_PUB_KEY
     375    sudo sudo mokutil --import $DEB_PUB_KEY
     376    sudo reboot
     377
     378Restart \"rcvboxadd setup\" after system is rebooted.
     379"
     380        fi
     381
     382        # Check if signing tool is available.
     383        [ -n "$SIGN_TOOL" ] || fail "Unable to find signing tool"
     384
     385        # Get kernel signature hash algorithm from kernel config and validate it.
     386        sig_hashalgo=$(kernel_module_sig_hash)
     387        [ "$(module_sig_hash_supported $sig_hashalgo)" = "1" ] \
     388            || failure "Unsupported kernel signature hash algorithm $sig_hashalgo"
     389
     390        # Sign modules.
     391        for i in $MODULE_LIST; do
     392            "$SIGN_TOOL" "$sig_hashalgo" "$DEB_PRIV_KEY" "$DEB_PUB_KEY" \
     393                /lib/modules/"$KERN_VER"/misc/"$i".ko || fail "Unable to sign $i.ko"
     394        done
     395        # Enroll signing key if needed.
     396        if test -n "$HAVE_UPDATE_SECUREBOOT_POLICY_TOOL"; then
     397            # update-secureboot-policy "expects" DKMS modules.
     398            # Work around this and talk to the authors as soon
     399            # as possible to fix it.
     400            mkdir -p /var/lib/dkms/vbox-temp
     401            update-secureboot-policy --enroll-key 2>/dev/null ||
     402                fail "Failed to enroll secure boot key."
     403            rmdir -p /var/lib/dkms/vbox-temp 2>/dev/null
     404
     405            # Indicate that key has been enrolled and reboot is needed.
     406            HAVE_DEB_KEY=true
     407        fi
     408    fi
     409}
     410
    291411# Build and install the VirtualBox guest kernel modules
    292412setup_modules()
     
    296416    # Match (at least): vboxguest.o; vboxguest.ko; vboxguest.ko.xz
    297417    set /lib/modules/"$KERN_VER"/misc/vboxguest.*o*
    298     test ! -f "$1" || return 0
     418    #test ! -f "$1" || return 0
    299419    test -d /lib/modules/"$KERN_VER"/build || return 0
    300420    export KERN_VER
     
    343463    echo "override vboxsf * misc" >> /etc/depmod.d/vboxvideo-upstream.conf
    344464    echo "override vboxvideo * misc" >> /etc/depmod.d/vboxvideo-upstream.conf
     465
     466    sign_modules "${KERN_VER}"
     467
    345468    update_initramfs "${KERN_VER}"
    346469    return 0
     
    458581}
    459582
     583
     584# Returns "1" if module is signed and signature can be verified
     585# with public key provided in DEB_PUB_KEY. Or empty string otherwise.
     586module_signed()
     587{
     588    mod="$1"
     589    [ -n "$mod" ] || return
     590
     591    extraction_tool=/lib/modules/"$(uname -r)"/build/scripts/extract-module-sig.pl
     592    mod_path=$(module_path "$mod" 2>/dev/null)
     593    openssl_tool=$(which openssl 2>/dev/null)
     594    # Do not use built-in printf!
     595    printf_tool=$(which printf 2>/dev/null)
     596
     597    # Make sure all the tools required for signature validation are available.
     598    [ -x "$extraction_tool" ] || return
     599    [ -n "$mod_path"        ] || return
     600    [ -n "$openssl_tool"    ] || return
     601    [ -n "$printf_tool"     ] || return
     602
     603    # Make sure openssl can handle hash algorithm.
     604    sig_hashalgo=$(modinfo -F sig_hashalgo vboxdrv 2>/dev/null)
     605    [ "$(module_sig_hash_supported $sig_hashalgo)" = "1" ] || return
     606
     607    # Generate file names for temporary stuff.
     608    mod_pub_key=$(mktemp -u)
     609    mod_signature=$(mktemp -u)
     610    mod_unsigned=$(mktemp -u)
     611
     612    # Convert public key in DER format into X509 certificate form.
     613    "$openssl_tool" x509 -pubkey -inform DER -in "$DEB_PUB_KEY" -out "$mod_pub_key" 2>/dev/null
     614    # Extract raw module signature and convert it into binary format.
     615    "$printf_tool" \\x$(modinfo -F signature "$mod" | sed -z 's/[ \t\n]//g' | sed -e "s/:/\\\x/g") 2>/dev/null > "$mod_signature"
     616    # Extract unsigned module for further digest calculation.
     617    "$extraction_tool" -0 "$mod_path" 2>/dev/null > "$mod_unsigned"
     618
     619    # Verify signature.
     620    rc=""
     621    "$openssl_tool" dgst "-$sig_hashalgo" -binary -verify "$mod_pub_key" -signature "$mod_signature" "$mod_unsigned" 2>&1 >/dev/null && rc="1"
     622    # Clean up.
     623    rm -f $mod_pub_key $mod_signature $mod_unsigned
     624
     625    # Check result.
     626    [ "$rc" = "1" ] || return
     627
     628    echo "1"
     629}
     630
    460631# Returns "1" if externally built module is available in the system and its
    461632# version and revision number do match to current VirtualBox installation.
     
    487658    [ "$mod_dir" = "misc" ] || return
    488659
     660    # In case if system is running in Secure Boot mode, check if module is signed.
     661    if test -n "$HAVE_SEC_BOOT"; then
     662        [ "$(module_signed "$mod")" = "1" ] || return
     663    fi
     664
    489665    echo "1"
    490666}
     
    503679setup()
    504680{
     681    info "Setting up modules"
     682
    505683    # chcon is needed on old Fedora/Redhat systems.  No one remembers which.
    506684    test ! -e /etc/selinux/config ||
     
    511689        # Prevent unnecessary rebuilding in order to speed up booting process.
    512690        if test "$(setup_complete)" = "1"; then
    513             info "VirtualBox Guest Additions kernel modules $VBOX_VERSION $VBOX_REVISION are"
    514             info "already available for kernel $TARGET_VER and do not require to be rebuilt."
     691            info "VirtualBox Guest Additions kernel modules $VBOX_VERSION $VBOX_REVISION are \
     692already available for kernel $TARGET_VER and do not require to be rebuilt."
    515693        else
    516694            info "Building the VirtualBox Guest Additions kernel modules.  This may take a while."
     
    590768    fi
    591769    setup
     770
     771    # Warn if Secure Boot setup not yet complete.
     772    if test -n "$HAVE_SEC_BOOT" && test -z "$DEB_KEY_ENROLLED"; then
     773        if test -n "$HAVE_DEB_KEY"; then
     774            info "You must re-start your system to finish secure boot set-up."
     775        else
     776            info "You must sign vboxguest, vboxsf and
     777vboxvideo (if present) kernel modules before using
     778VirtualBox Guest Additions. See the documentation
     779for your Linux distribution."
     780        fi
     781    fi
     782
    592783    if test -z "${INSTALL_NO_MODULE_BUILDS}"; then
    593784        test -d /sys &&
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