2 # SPDX-License-Identifier: GPL-2.0
4 # Kselftest framework defines: ksft_pass=0, ksft_fail=1, ksft_skip=4
6 VERBOSE="${VERBOSE:-1}"
7 IKCONFIG="/tmp/config-`uname -r`"
8 KERNEL_IMAGE="/boot/vmlinuz-`uname -r`"
9 SECURITYFS=$(grep "securityfs" /proc/mounts | awk '{print $2}')
13 [ $VERBOSE -ne 0 ] && echo "[INFO] $1"
16 # The ksefltest framework requirement returns 0 for PASS.
19 [ $VERBOSE -ne 0 ] && echo "$1 [PASS]"
23 # The ksefltest framework requirement returns 1 for FAIL.
26 [ $VERBOSE -ne 0 ] && echo "$1 [FAIL]"
30 # The ksefltest framework requirement returns 4 for SKIP.
33 [ $VERBOSE -ne 0 ] && echo "$1"
37 # Check efivar SecureBoot-$(the UUID) and SetupMode-$(the UUID).
38 # The secure boot mode can be accessed either as the last integer
39 # of "od -An -t u1 /sys/firmware/efi/efivars/SecureBoot-*" or from
40 # "od -An -t u1 /sys/firmware/efi/vars/SecureBoot-*/data". The efi
41 # SetupMode can be similarly accessed.
42 # Return 1 for SecureBoot mode enabled and SetupMode mode disabled.
45 local efivarfs="/sys/firmware/efi/efivars"
46 local secure_boot_file="$efivarfs/../vars/SecureBoot-*/data"
47 local setup_mode_file="$efivarfs/../vars/SetupMode-*/data"
48 local secureboot_mode=0
51 # Make sure that efivars is mounted in the normal location
52 if ! grep -q "^\S\+ $efivarfs efivarfs" /proc/mounts; then
53 log_skip "efivars is not mounted on $efivarfs"
56 # Due to globbing, quoting "secure_boot_file" and "setup_mode_file"
57 # is not possible. (Todo: initialize variables using find or ls.)
58 if [ ! -e $secure_boot_file ] || [ ! -e $setup_mode_file ]; then
59 log_skip "unknown secureboot/setup mode"
62 secureboot_mode=`od -An -t u1 $secure_boot_file`
63 setup_mode=`od -An -t u1 $setup_mode_file`
65 if [ $secureboot_mode -eq 1 ] && [ $setup_mode -eq 0 ]; then
66 log_info "secure boot mode enabled"
69 log_info "secure boot mode not enabled"
73 require_root_privileges()
75 if [ $(id -ru) -ne 0 ]; then
76 log_skip "requires root privileges"
80 # Look for config option in Kconfig file.
81 # Return 1 for found and 0 for not found.
87 grep -E -q $config $IKCONFIG
95 # Attempt to get the kernel config first via proc, and then by
96 # extracting it from the kernel image or the configs.ko using
97 # scripts/extract-ikconfig.
101 local proc_config="/proc/config.gz"
102 local module_dir="/lib/modules/`uname -r`"
103 local configs_module="$module_dir/kernel/kernel/configs.ko"
105 if [ ! -f $proc_config ]; then
106 modprobe configs > /dev/null 2>&1
108 if [ -f $proc_config ]; then
109 cat $proc_config | gunzip > $IKCONFIG 2>/dev/null
110 if [ $? -eq 0 ]; then
115 local extract_ikconfig="$module_dir/source/scripts/extract-ikconfig"
116 if [ ! -f $extract_ikconfig ]; then
117 log_skip "extract-ikconfig not found"
120 $extract_ikconfig $KERNEL_IMAGE > $IKCONFIG 2>/dev/null
121 if [ $? -eq 1 ]; then
122 if [ ! -f $configs_module ]; then
123 log_skip "CONFIG_IKCONFIG not enabled"
125 $extract_ikconfig $configs_module > $IKCONFIG
126 if [ $? -eq 1 ]; then
127 log_skip "CONFIG_IKCONFIG not enabled"
133 # Make sure that securityfs is mounted
136 if [ -z $SECURITYFS ]; then
137 SECURITYFS=/sys/kernel/security
138 mount -t securityfs security $SECURITYFS
141 if [ ! -d "$SECURITYFS" ]; then
142 log_fail "$SECURITYFS :securityfs is not mounted"
146 # The policy rule format is an "action" followed by key-value pairs. This
147 # function supports up to two key-value pairs, in any order.
148 # For example: action func=<keyword> [appraise_type=<type>]
149 # Return 1 for found and 0 for not found.
159 local ima_policy=$SECURITYFS/ima/policy
160 if [ ! -e $ima_policy ]; then
161 log_fail "$ima_policy not found"
164 if [ -n $keypair2 ]; then
165 grep -e "^$action.*$keypair1" "$ima_policy" | \
166 grep -q -e "$keypair2"
168 grep -q -e "^$action.*$keypair1" "$ima_policy"
171 # invert "grep -q" result, returning 1 for found.
172 [ $? -eq 0 ] && ret=1