linux - 在早期启动期间使用 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 脚本?当系统完全启动时,我能够很好地执行脚本。
感谢您的帮助。
解决方案
我不久前想出了解决方案,但从来没有回答我自己的问题:)
问题是我没有将evmctl
orkeyctl
二进制文件复制到 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)
如果将来有人访问此过程并想知道我是如何得出这个答案的以及我是如何编写脚本的,那么手册中描述了很多这个过程。
推荐阅读
- javascript - 如何使用 Array.reduce 方法获得奇数、偶数的总和?
- python - 来自列表中多个值的元组
- java - 如何链接两个期货
- react-native - 如何使用 react-native-splash-screen 包在 iOS 中添加动画 GIF 作为启动画面
- html - Android 10 webview 表单仅在双击时提交
- c++ - 我可以添加 GetMutableSizePrefixedRoot(或 GetSizePrefixedMutableRoot)吗?
- javascript - Angular,Highchart 热图等高线图未显示在 Firefox 上,适用于 Chrome
- c++ - 为什么 Metal Shading Language 是 C++ 而 Metal 本身不是?
- javascript - 在 p5js 中转移背景颜色选择
- operating-system - 文件系统:比较权限模型