Some fixes for IMA/EVM
cover Some fixes for IMA/EVM
From: Ming Liu <[email protected]>
Ming Liu (5):
ima-evm-utils: set native REQUIRED_DISTRO_FEATURES to empty
initramfs-framework-ima: fix a wrong path
meta: refactor IMA/EVM sign rootfs
initramfs-framework-ima: let ima_enabled return 0 ima-evm-rootfs.bbclass: avoid generating /etc/fstab for wic
meta-integrity/README.md | 4 ++- meta-integrity/classes/ima-evm-rootfs.bbclass | 33 +++++++++---------- .../initrdscripts/initramfs-framework-ima.bb | 2 +- .../initrdscripts/initramfs-framework-ima/ima | 3 +- .../ima-evm-keys/ima-evm-keys_1.0.bb | 16 +++++++++ .../ima-evm-utils/ima-evm-utils_git.bb | 1 + 6 files changed, 38 insertions(+), 21 deletions(-) create mode 100644 meta-integrity/recipes-security/ima-evm-keys/ima-evm-keys_1.0.bb
Signature · 1 line
2.29.0
PATCH 1/5 ima-evm-utils: set native REQUIRED_DISTRO_FEATURES to empty
From: Ming Liu <[email protected]>
'ima' does not have to be in native DISTRO_FEATURES, unset it to avoid sanity check, this fixes a following error:
--- .../recipes-security/ima-evm-utils/ima-evm-utils_git.bb | 1 + 1 file changed, 1 insertion(+)
meta-integrity/recipes-security/ima-evm-utils/ima-evm-utils_git.bb +1 -0
@@ -26,6 +26,7 @@ S = "${WORKDIR}/git" inherit pkgconfig autotools features_check REQUIRED_DISTRO_FEATURES = "ima" +REQUIRED_DISTRO_FEATURES_class-native = "" EXTRA_OECONF_append_class-target = " --with-kernel-headers=${STAGING_KERNEL_BUILDDIR}"
Signature · 1 line
2.29.0
PATCH 2/5 initramfs-framework-ima: fix a wrong path
In reply to Ming LiuFrom: Ming Liu <[email protected]>
/etc/ima-policy > /etc/ima/ima-policy.
--- .../recipes-core/initrdscripts/initramfs-framework-ima/ima | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
meta-integrity/recipes-core/initrdscripts/initramfs-framework-ima/ima +1 -1
@@ -46,7 +46,7 @@ ima_run() { # ("[Linux-ima-user] IMA policy loading via cat") and we get better error reporting when # checking the write of each line. To minimize the risk of policy loading going wrong we # also remove comments and blank lines ourselves. - if ! (set -e; while read i; do if echo "$i" | grep -q -e '^#' -e '^ *$'; then debug "Skipping IMA policy: $i"; else debug "Writing IMA policy: $i"; if echo $i; then sleep ${bootparam_ima_delay:-0}; else fatal "Invalid line in IMA policy: $i"; exit 1; fi; fi; done) </etc/ima-policy >/sys/kernel/security/ima/policy; then + if ! (set -e; while read i; do if echo "$i" | grep -q -e '^#' -e '^ *$'; then debug "Skipping IMA policy: $i"; else debug "Writing IMA policy: $i"; if echo $i; then sleep ${bootparam_ima_delay:-0}; else fatal "Invalid line in IMA policy: $i"; exit 1; fi; fi; done) </etc/ima/ima-policy >/sys/kernel/security/ima/policy; then fatal "Could not load IMA policy." fi }
Signature · 1 line
2.29.0
PATCH 3/5 meta: refactor IMA/EVM sign rootfs
In reply to Ming LiuFrom: Ming Liu <[email protected]>
The current logic in ima-evm-rootfs.bbclass does not guarantee ima_evm_sign_rootfs is the last function in IMAGE_PREPROCESS_COMMAND by appending to it, for instance, if there are other "_append" being used as it's the case in openembedded-core/meta/classes/image.bbclass:
| IMAGE_PREPROCESS_COMMAND_append = " ${@ 'systemd_preset_all;' \
| if bb.utils.contains('DISTRO_FEATURES', 'systemd', True, False, d) \
| and not bb.utils.contains('IMAGE_FEATURES', 'stateless-rootfs', True,
| False, d) else ''} reproducible_final_image_task; "and ima-evm-rootfs should be in IMAGE_CLASSES instead of in INHERIT since that would impact all recipes but not only image recipes.
To fix the above issues, we introduce a ima_evm_sign_handler setting IMA/EVM rootfs signing requirements/dependencies in event bb.event.RecipePreFinalise, it checks 'ima' distro feature to decide if IMA/EVM rootfs signing logic should be applied or not.
We also need split public keys to ima-evm-keys recipe, so it could be added both in initramfs and rootfs, so initramfs recipe does not have to inherit ima-evm-rootfs
--- meta-integrity/README.md | 4 ++- meta-integrity/classes/ima-evm-rootfs.bbclass | 30 ++++++++----------- .../initrdscripts/initramfs-framework-ima.bb | 2 +- .../ima-evm-keys/ima-evm-keys_1.0.bb | 16 ++++++++++ 4 files changed, 32 insertions(+), 20 deletions(-) create mode 100644 meta-integrity/recipes-security/ima-evm-keys/ima-evm-keys_1.0.bb
meta-integrity/README.md +3 -1
@@ -73,8 +73,10 @@ Adding the layer only enables IMA (see below regarding EVM) during compilation of the Linux kernel. To also activate it when building the image, enable image signing in the local.conf like this: - INHERIT += "ima-evm-rootfs" + IMAGE_CLASSES += "ima-evm-rootfs" IMA_EVM_KEY_DIR = "${INTEGRITY_BASE}/data/debug-keys" + IMA_EVM_PRIVKEY = "${IMA_EVM_KEY_DIR}/privkey_ima.pem" + IMA_EVM_X509 = "${IMA_EVM_KEY_DIR}/x509_ima.der" This uses the default keys provided in the "data" directory of the layer. Because everyone has access to these private keys, such an image
meta-integrity/classes/ima-evm-rootfs.bbclass +12 -18
@@ -37,15 +37,6 @@ ima_evm_sign_rootfs () { # reasons (including a change of the signing keys) without also # re-running do_rootfs. - # Copy file(s) which must be on the device. Note that - # evmctl uses x509_evm.der also for "ima_verify", which is probably - # a bug (should default to x509_ima.der). Does not matter for us - # because we use the same key for both. - install -d ./${sysconfdir}/keys - rm -f ./${sysconfdir}/keys/x509_evm.der - install "${IMA_EVM_X509}" ./${sysconfdir}/keys/x509_evm.der - ln -sf x509_evm.der ./${sysconfdir}/keys/x509_ima.der - # Fix /etc/fstab: it must include the "i_version" mount option for # those file systems where writing files is allowed, otherwise # these changes will not get detected at runtime.
@@ -80,13 +71,16 @@ ima_evm_sign_rootfs () { } # Signing must run as late as possible in the do_rootfs task. -# IMAGE_PREPROCESS_COMMAND runs after ROOTFS_POSTPROCESS_COMMAND, so -# append (not prepend!) to IMAGE_PREPROCESS_COMMAND, and do it with -# _append instead of += because _append gets evaluated later. In -# particular, we must run after prelink_image in -# IMAGE_PREPROCESS_COMMAND, because prelinking changes executables. - -IMAGE_PREPROCESS_COMMAND_append = " ima_evm_sign_rootfs ; " +# To guarantee that, we append it to IMAGE_PREPROCESS_COMMAND in +# RecipePreFinalise event handler, this ensures it's the last +# function in IMAGE_PREPROCESS_COMMAND. +python ima_evm_sign_handler () { + if not e.data or 'ima' not in e.data.getVar('DISTRO_FEATURES').split(): + return -# evmctl must have been installed first. -do_rootfs[depends] += "ima-evm-utils-native:do_populate_sysroot" + e.data.appendVar('IMAGE_PREPROCESS_COMMAND', ' ima_evm_sign_rootfs; ') + e.data.appendVar('IMAGE_INSTALL', ' ima-evm-keys') + e.data.appendVarFlag('do_rootfs', 'depends', ' ima-evm-utils-native:do_populate_sysroot') +} +addhandler ima_evm_sign_handler +ima_evm_sign_handler[eventmask] = "bb.event.RecipePreFinalise"
meta-integrity/recipes-core/initrdscripts/initramfs-framework-ima.bb +1 -1
@@ -27,5 +27,5 @@ do_install () { FILES_${PN} = "/init.d ${sysconfdir}" -RDEPENDS_${PN} = "keyutils ${IMA_POLICY}" +RDEPENDS_${PN} = "keyutils ima-evm-keys ${IMA_POLICY}" RDEPENDS_${PN} += "initramfs-framework-base"
meta-integrity/recipes-security/ima-evm-keys/ima-evm-keys_1.0.bb new file +16 -0
@@ -0,0 +1,16 @@ +SUMMARY = "IMA/EMV public keys" +LICENSE = "MIT" +LIC_FILES_CHKSUM = "file://${COREBASE}/meta/COPYING.MIT;md5=3da9cfbcb788c80a0384361b4de20420" + +inherit features_check +REQUIRED_DISTRO_FEATURES = "ima" + +ALLOW_EMPTY_${PN} = "1" + +do_install () { + if [ -e "${IMA_EVM_X509}" ]; then + install -d ${D}/${sysconfdir}/keys + install "${IMA_EVM_X509}" ${D}${sysconfdir}/keys/x509_evm.der + lnr ${D}${sysconfdir}/keys/x509_evm.der ${D}${sysconfdir}/keys/x509_ima.der + fi +}
Signature · 1 line
2.29.0
PATCH 4/5 initramfs-framework-ima: let ima_enabled return 0
In reply to Ming LiuFrom: Ming Liu <[email protected]>
Otherwise, ima script would not run as intended.
--- .../recipes-core/initrdscripts/initramfs-framework-ima/ima | 1 + 1 file changed, 1 insertion(+)
meta-integrity/recipes-core/initrdscripts/initramfs-framework-ima/ima +1 -0
@@ -6,6 +6,7 @@ ima_enabled() { if [ "$bootparam_no_ima" = "true" ]; then return 1 fi + return 0 } ima_run() {
Signature · 1 line
2.29.0
PATCH 5/5 ima-evm-rootfs.bbclass: avoid generating /etc/fstab for wic
In reply to Ming LiuFrom: Ming Liu <[email protected]>
Or else wic will fail without "--no-fstab-update" option.
--- meta-integrity/classes/ima-evm-rootfs.bbclass | 3 +++ 1 file changed, 3 insertions(+)
meta-integrity/classes/ima-evm-rootfs.bbclass +3 -0
@@ -28,6 +28,9 @@ IMA_EVM_ROOTFS_HASHED ?= ". -depth 0 -false" # the iversion flags (needed by IMA when allowing writing). IMA_EVM_ROOTFS_IVERSION ?= "" +# Avoid re-generating fstab when ima is enabled. +WIC_CREATE_EXTRA_ARGS_append = "${@bb.utils.contains('DISTRO_FEATURES', 'ima', ' --no-fstab-update', '', d)}" + ima_evm_sign_rootfs () { cd ${IMAGE_ROOTFS}
Signature · 1 line
2.29.0
I suppose, patch 3 can be split into logical chunks. Other patches are:
ср, 17 февр. 2021 г. в 17:09, Ming Liu <[email protected]>:
Quoted 25 lines
From: Ming Liu <[email protected]>
Ming Liu (5):
ima-evm-utils: set nativeREQUIRED_DISTRO_FEATURESto empty
initramfs-framework-ima: fix a wrong path
meta: refactor IMA/EVM sign rootfs
initramfs-framework-ima: letima_enabledreturn 0 ima-evm-rootfs.bbclass: avoid generating/etc/fstabfor wicmeta-integrity/README.md | 4 ++- meta-integrity/classes/ima-evm-rootfs.bbclass | 33 +++++++++---------- .../initrdscripts/initramfs-framework-ima.bb | 2 +- .../initrdscripts/initramfs-framework-ima/ima | 3 +- .../ima-evm-keys/ima-evm-keys_1.0.bb | 16 +++++++++ .../ima-evm-utils/ima-evm-utils_git.bb | 1 + 6 files changed, 38 insertions(+), 21 deletions(-) create mode 100644 meta-integrity/recipes-security/ima-evm-keys/ima-evm-keys_1.0.bb--
2.29.0
Signature · 2 lines
With best wishes Dmitry