首页 > 解决方案 > 在早期启动期间使用 initramfs 加载密钥并设置 IMA/EVM

问题描述

我正在尝试在 Debian Buster 内核 v5.7.13 中创建一个为 Linux 的 IMA 子系统加载一些密钥的初始化脚本。按照此手册页evmctl上的说明,我编写/复制了一个脚本,/etc/initramfs-tools/scripts/local-top/ima.sh如下所示:

#!/bin/sh

# mount securityfs if not mounted
SECFS=/sys/kernel/security
grep -q  $SECFS /proc/mounts || mount -n -t securityfs securityfs $SECFS

# search for IMA trusted keyring, then for untrusted
ima_id="`awk '/\.ima/ { printf "%d", "0x"$1; }' /proc/keys`"
if [ -z "$ima_id" ]; then
    ima_id=`/bin/keyctl search @u keyring _ima 2>/dev/null`
    if [ -z "$ima_id" ]; then
        ima_id=`keyctl newring _ima @u`
    fi
fi
# import IMA X509 certificate
# evmctl import /etc/keys/x509_ima.der $ima_id
evmctl import /etc/keys/x509_evm.der $ima_id

# search for EVM keyring
evm_id=`keyctl search @u keyring _evm 2>/dev/null`
if [ -z "$evm_id" ]; then
    evm_id=`keyctl newring _evm @u`
fi
# import EVM X509 certificate
evmctl import /etc/keys/x509_evm.der $evm_id

# a) import EVM encrypted key
cat /etc/keys/kmk | keyctl padd user kmk @u
keyctl add encrypted evm-key "load `cat /etc/keys/evm-key`" @u

# enable EVM
echo "1" > /sys/kernel/security/evm

之后,我通过运行update-initramfs -u它来更新我的 initramfs 图像,该图像运行完成而没有错误。但是,当我尝试启动机器时,出现以下错误(从我的 VM 截屏)。

initramfs 错误

我在这里错过了一步吗?如何使某些文件可用于我的 initramfs 脚本?当系统完全启动时,我能够很好地执行脚本。

感谢您的帮助。

标签: linuxinitramfs

解决方案


我不久前想出了解决方案,但从来没有回答我自己的问题:)

问题是我没有将evmctlorkeyctl二进制文件复制到 initramfs 中,这就是它找不到它们的原因。为了加载这两个二进制文件,我使用了以下钩子脚本:

#!/bin/sh
# Includes IMA's necessary components in the initramfs image
# Place in /etc/initramfs-tools/hooks

PREREQ=""
prereqs()
{
    echo "$PREREQ"
}

case $1 in
    prereqs)
        prereqs
        exit 0
        ;;
esac

. /usr/share/initramfs-tools/hook-functions
# Begin real processing below this line

# Copy executables we need to initramfs
copy_exec /usr/bin/keyctl /usr/bin
copy_exec /usr/bin/evmctl /usr/bin

# Copy other files to initramfs
mkdir -p $DESTDIR/etc/keys
cp -a /etc/keys/x509_ima.der $DESTDIR/etc/keys

exit 0

以及在系统启动期间执行的以下脚本:

#!/bin/sh
# Load keys for IMA
# Place in /etc/initramfs-tools/scripts/local-top

PREREQ=""
prereqs()
{
    echo "$PREREQ"
}

case $1 in
    prereqs)
        prereqs
        exit 0
        ;;
esac

. /scripts/functions
# Begin real processing below this line
if [ ! -x "/usr/bin/keyctl" ]; then
    panic "keyctl executable not found"
fi

if [ ! -x "/usr/bin/evmctl" ]; then
    panic "evmctl executable not found"
fi

if [ ! -f "/etc/keys/x509_ima.der" ]; then
    panic "IMA x509 certificate not found"
fi

# Mount securityfs if not mounted
SECFS=/sys/kernel/security
grep -q  $SECFS /proc/mounts || mount -n -t securityfs securityfs $SECFS

# Create an IMA untrusted keyring
ima_id=`keyctl newring _ima @u`

# Import IMA x509 Certificate
evmctl import /etc/keys/x509_ima.der $ima_id

exit 0

然后,为了生成 initramfs 映像,我使用了该mkinitramfs命令。initramfs-tools(8)如果将来有人访问此过程并想知道我是如何得出这个答案的以及我是如何编写脚本的,那么手册中描述了很多这个过程。


推荐阅读