Path: blob/master/tools/testing/selftests/kexec/test_kexec_file_load.sh
26285 views
#!/bin/sh1# SPDX-License-Identifier: GPL-2.02#3# Loading a kernel image via the kexec_file_load syscall can verify either4# the IMA signature stored in the security.ima xattr or the PE signature,5# both signatures depending on the IMA policy, or none.6#7# To determine whether the kernel image is signed, this test depends8# on pesign and getfattr. This test also requires the kernel to be9# built with CONFIG_IKCONFIG enabled and either CONFIG_IKCONFIG_PROC10# enabled or access to the extract-ikconfig script.1112TEST="KEXEC_FILE_LOAD"13. ./kexec_common_lib.sh1415trap "{ rm -f $IKCONFIG ; }" EXIT1617# Some of the IMA builtin policies may require the kexec kernel image to18# be signed, but these policy rules may be replaced with a custom19# policy. Only CONFIG_IMA_APPRAISE_REQUIRE_KEXEC_SIGS persists after20# loading a custom policy. Check if it is enabled, before reading the21# IMA runtime sysfs policy file.22# Return 1 for IMA signature required and 0 for not required.23is_ima_sig_required()24{25local ret=02627kconfig_enabled "CONFIG_IMA_APPRAISE_REQUIRE_KEXEC_SIGS=y" \28"IMA kernel image signature required"29if [ $? -eq 1 ]; then30log_info "IMA signature required"31return 132fi3334# The architecture specific or a custom policy may require the35# kexec kernel image be signed. Policy rules are walked36# sequentially. As a result, a policy rule may be defined, but37# might not necessarily be used. This test assumes if a policy38# rule is specified, that is the intent.3940# First check for appended signature (modsig), then xattr41if [ $ima_read_policy -eq 1 ]; then42check_ima_policy "appraise" "func=KEXEC_KERNEL_CHECK" \43"appraise_type=imasig|modsig"44ret=$?45if [ $ret -eq 1 ]; then46log_info "IMA or appended(modsig) signature required"47else48check_ima_policy "appraise" "func=KEXEC_KERNEL_CHECK" \49"appraise_type=imasig"50ret=$?51[ $ret -eq 1 ] && log_info "IMA signature required";52fi53fi54return $ret55}5657# The kexec_file_load_test() is complicated enough, require pesign.58# Return 1 for PE signature found and 0 for not found.59check_for_pesig()60{61which pesign > /dev/null 2>&1 || log_skip "pesign not found"6263pesign -i $KERNEL_IMAGE --show-signature | grep -q "No signatures"64local ret=$?65if [ $ret -eq 1 ]; then66log_info "kexec kernel image PE signed"67else68log_info "kexec kernel image not PE signed"69fi70return $ret71}7273# The kexec_file_load_test() is complicated enough, require getfattr.74# Return 1 for IMA signature found and 0 for not found.75check_for_imasig()76{77local ret=07879which getfattr > /dev/null 2>&180if [ $? -eq 1 ]; then81log_skip "getfattr not found"82fi8384line=$(getfattr -n security.ima -e hex --absolute-names $KERNEL_IMAGE 2>&1)85echo $line | grep -q "security.ima=0x03"86if [ $? -eq 0 ]; then87ret=188log_info "kexec kernel image IMA signed"89else90log_info "kexec kernel image not IMA signed"91fi92return $ret93}9495# Return 1 for appended signature (modsig) found and 0 for not found.96check_for_modsig()97{98local module_sig_string="~Module signature appended~"99local ret=0100101tail --bytes $((${#module_sig_string} + 1)) $KERNEL_IMAGE | \102grep -q "$module_sig_string"103if [ $? -eq 0 ]; then104ret=1105log_info "kexec kernel image modsig signed"106else107log_info "kexec kernel image not modsig signed"108fi109return $ret110}111112kexec_file_load_test()113{114local succeed_msg="kexec_file_load succeeded"115local failed_msg="kexec_file_load failed"116local key_msg="try enabling the CONFIG_INTEGRITY_PLATFORM_KEYRING"117118line=$(kexec --load --kexec-file-syscall $KERNEL_IMAGE 2>&1)119120if [ $? -eq 0 ]; then121kexec --unload --kexec-file-syscall122123# In secureboot mode with an architecture specific124# policy, make sure either an IMA or PE signature exists.125if [ $secureboot -eq 1 ] && [ $arch_policy -eq 1 ] && \126[ $ima_signed -eq 0 ] && [ $pe_signed -eq 0 ] \127&& [ $ima_modsig -eq 0 ]; then128log_fail "$succeed_msg (missing sig)"129fi130131if [ $kexec_sig_required -eq 1 -o $pe_sig_required -eq 1 ] \132&& [ $pe_signed -eq 0 ]; then133log_fail "$succeed_msg (missing PE sig)"134fi135136if [ $ima_sig_required -eq 1 ] && [ $ima_signed -eq 0 ] \137&& [ $ima_modsig -eq 0 ]; then138log_fail "$succeed_msg (missing IMA sig)"139fi140141if [ $pe_sig_required -eq 0 ] && [ $ima_appraise -eq 1 ] \142&& [ $ima_sig_required -eq 0 ] && [ $ima_signed -eq 0 ] \143&& [ $ima_read_policy -eq 0 ]; then144log_fail "$succeed_msg (possibly missing IMA sig)"145fi146147if [ $pe_sig_required -eq 0 ] && [ $ima_appraise -eq 0 ]; then148log_info "No signature verification required"149elif [ $pe_sig_required -eq 0 ] && [ $ima_appraise -eq 1 ] \150&& [ $ima_sig_required -eq 0 ] && [ $ima_signed -eq 0 ] \151&& [ $ima_read_policy -eq 1 ]; then152log_info "No signature verification required"153fi154155log_pass "$succeed_msg"156fi157158# Check the reason for the kexec_file_load failure159echo $line | grep -q "Required key not available"160if [ $? -eq 0 ]; then161if [ $platform_keyring -eq 0 ]; then162log_pass "$failed_msg (-ENOKEY), $key_msg"163else164log_pass "$failed_msg (-ENOKEY)"165fi166fi167168if [ $kexec_sig_required -eq 1 -o $pe_sig_required -eq 1 ] \169&& [ $pe_signed -eq 0 ]; then170log_pass "$failed_msg (missing PE sig)"171fi172173if [ $ima_sig_required -eq 1 ] && [ $ima_signed -eq 0 ]; then174log_pass "$failed_msg (missing IMA sig)"175fi176177if [ $pe_sig_required -eq 0 ] && [ $ima_appraise -eq 1 ] \178&& [ $ima_sig_required -eq 0 ] && [ $ima_read_policy -eq 0 ] \179&& [ $ima_signed -eq 0 ]; then180log_pass "$failed_msg (possibly missing IMA sig)"181fi182183log_pass "$failed_msg"184return 0185}186187# kexec requires root privileges188require_root_privileges189190# get the kernel config191get_kconfig192193kconfig_enabled "CONFIG_KEXEC_FILE=y" "kexec_file_load is enabled"194if [ $? -eq 0 ]; then195log_skip "kexec_file_load is not enabled"196fi197198# Determine which kernel config options are enabled199kconfig_enabled "CONFIG_IMA_APPRAISE=y" "IMA enabled"200ima_appraise=$?201202kconfig_enabled "CONFIG_IMA_ARCH_POLICY=y" \203"architecture specific policy enabled"204arch_policy=$?205206kconfig_enabled "CONFIG_INTEGRITY_PLATFORM_KEYRING=y" \207"platform keyring enabled"208platform_keyring=$?209210kconfig_enabled "CONFIG_IMA_READ_POLICY=y" "reading IMA policy permitted"211ima_read_policy=$?212213kconfig_enabled "CONFIG_KEXEC_SIG_FORCE=y" \214"kexec signed kernel image required"215kexec_sig_required=$?216217kconfig_enabled "CONFIG_KEXEC_BZIMAGE_VERIFY_SIG=y" \218"PE signed kernel image required"219pe_sig_required=$?220221is_ima_sig_required222ima_sig_required=$?223224get_secureboot_mode225secureboot=$?226227# Are there pe and ima signatures228if [ "$(get_arch)" == 'ppc64le' ]; then229pe_signed=0230else231check_for_pesig232pe_signed=$?233fi234235check_for_imasig236ima_signed=$?237238check_for_modsig239ima_modsig=$?240241# Test loading the kernel image via kexec_file_load syscall242kexec_file_load_test243244245