在早期启动期间使用 initramfs 加载密钥和设置 IMA/EVM
Using initramfs to load keys and setup IMA/EVM during early boot
我正在尝试在 Debian Buster 内核 v5.7.13 中创建一个初始化脚本,为 Linux 的 IMA 子系统加载一些密钥。按照 this man page for evmctl
上的说明,我 wrote/copied 在 /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"; }' /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 脚本?当系统完全启动时,我可以正常执行脚本。
感谢您的帮助。
我刚才想出了解决方案,但一直没有时间回答我自己的问题:)
问题是我没有将 evmctl
或 keyctl
二进制文件复制到 initramfs 中,这就是它找不到它们的原因。为了加载这两个二进制文件,我使用了以下钩子脚本:
#!/bin/sh
# Includes IMA's necessary components in the initramfs image
# Place in /etc/initramfs-tools/hooks
PREREQ=""
prereqs()
{
echo "$PREREQ"
}
case 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 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)
手册中描述了很多这个过程,如果将来有人访问这个并且想知道我是如何得出这个答案以及我是如何制作我的脚本的。
我正在尝试在 Debian Buster 内核 v5.7.13 中创建一个初始化脚本,为 Linux 的 IMA 子系统加载一些密钥。按照 this man page for evmctl
上的说明,我 wrote/copied 在 /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"; }' /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 脚本?当系统完全启动时,我可以正常执行脚本。
感谢您的帮助。
我刚才想出了解决方案,但一直没有时间回答我自己的问题:)
问题是我没有将 evmctl
或 keyctl
二进制文件复制到 initramfs 中,这就是它找不到它们的原因。为了加载这两个二进制文件,我使用了以下钩子脚本:
#!/bin/sh
# Includes IMA's necessary components in the initramfs image
# Place in /etc/initramfs-tools/hooks
PREREQ=""
prereqs()
{
echo "$PREREQ"
}
case 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 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)
手册中描述了很多这个过程,如果将来有人访问这个并且想知道我是如何得出这个答案以及我是如何制作我的脚本的。