【问题标题】:Using initramfs to load keys and setup IMA/EVM during early boot在早期启动期间使用 initramfs 加载密钥并设置 IMA/EVM
【发布时间】:2020-08-10 16:01:10
【问题描述】:

我正在尝试在 Debian Buster 内核 v5.7.13 中创建一个初始化脚本,它为 Linux 的 IMA 子系统加载一些密钥。按照this man page for 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 脚本?系统完全启动后,我可以正常执行脚本。

感谢您的帮助。

【问题讨论】:

    标签: linux initramfs


    【解决方案1】:

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

    问题是我没有将 evmctlkeyctl 二进制文件复制到 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) 手册中描述了很多这个过程,如果有人将来访问这个并且想知道我是如何得出这个答案的以及我是如何制作我的脚本的。

    【讨论】:

      猜你喜欢
      • 2018-01-17
      • 1970-01-01
      • 1970-01-01
      • 2016-07-26
      • 2016-04-05
      • 1970-01-01
      • 2018-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多